Back to index

citadel  8.12
Defines | Functions
clientsocket.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define sock_close(sock)   close(sock)
#define sock_shutdown(sock, how)   shutdown(sock, how)
#define CLIENT_TIMEOUT   600

Functions

int sock_connect (char *host, char *service)
int sock_write (int *sock, const char *buf, int nbytes)
int sock_write_timeout (int *sock, const char *buf, int nbytes, int timeout)
int ml_sock_gets (int *sock, char *buf, int nSec)
int sock_getln (int *sock, char *buf, int bufsize)
int CtdlSockGetLine (int *sock, StrBuf *Target, int nSec)
int sock_puts (int *sock, char *buf)
int socket_read_blob (int *Socket, StrBuf *Target, int bytes, int timeout)

Define Documentation

#define CLIENT_TIMEOUT   600

Definition at line 34 of file clientsocket.h.

#define sock_close (   sock)    close(sock)

Definition at line 28 of file clientsocket.h.

#define sock_shutdown (   sock,
  how 
)    shutdown(sock, how)

Definition at line 29 of file clientsocket.h.


Function Documentation

int CtdlSockGetLine ( int *  sock,
StrBuf *  Target,
int  nSec 
)

Definition at line 151 of file clientsocket.c.

{
       CitContext *CCC = MyContext();
       const char *Error;
       int rc;

       FlushStrBuf(Target);
       rc = StrBufTCP_read_buffered_line_fast(Target,
                                          CCC->SBuf.Buf,
                                          &CCC->SBuf.ReadWritePointer,
                                          sock, nSec, 1, &Error);
       if ((rc < 0) && (Error != NULL))
              syslog(LOG_CRIT, "CtdlSockGetLine() failed: %s", Error);
       return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ml_sock_gets ( int *  sock,
char *  buf,
int  nSec 
)

Definition at line 296 of file clientsocket.c.

{
       int rc = 0;
       char bigbuf[1024];
       int g;

       g = sock_getln_err(sock, buf, SIZ, &rc, nSec);
       if (rc < 0)
              return rc;
       if (g < 4)
              return (g);
       if (buf[3] != '-')
              return (g);

       do {
              g = sock_getln_err(sock, bigbuf, SIZ, &rc, nSec);
              if (rc < 0)
                     return rc;
              if (g < 0)
                     return (g);
       } while ((g >= 4) && (bigbuf[3] == '-'));

       return (strlen(buf));
}

Here is the call graph for this function:

int sock_connect ( char *  host,
char *  service 
)

Definition at line 51 of file clientsocket.c.

{
       struct in6_addr serveraddr;
       struct addrinfo hints;
       struct addrinfo *res = NULL;
       struct addrinfo *ai = NULL;
       int rc = (-1);
       int sock = (-1);

       if ((host == NULL) || IsEmptyStr(host))
              return (-1);
       if ((service == NULL) || IsEmptyStr(service))
              return (-1);

       memset(&hints, 0x00, sizeof(hints));
       hints.ai_flags = AI_NUMERICSERV;
       hints.ai_family = AF_UNSPEC;
       hints.ai_socktype = SOCK_STREAM;

       /*
        * Handle numeric IPv4 and IPv6 addresses
        */
       rc = inet_pton(AF_INET, host, &serveraddr);
       if (rc == 1) {                                          /* dotted quad */
              hints.ai_family = AF_INET;
              hints.ai_flags |= AI_NUMERICHOST;
       } else {
              rc = inet_pton(AF_INET6, host, &serveraddr);
              if (rc == 1) {                                   /* IPv6 address */
                     hints.ai_family = AF_INET6;
                     hints.ai_flags |= AI_NUMERICHOST;
              }
       }

       /* Begin the connection process */

       rc = getaddrinfo(host, service, &hints, &res);
       if (rc != 0) {
              syslog(LOG_ERR, "%s: %s", host, gai_strerror(rc));
              return(-1);
       }

       /*
        * Try all available addresses until we connect to one or until we run out.
        */
       for (ai = res; ai != NULL; ai = ai->ai_next) {
              sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
              if (sock < 0) {
                     syslog(LOG_ERR, "socket() failed: %s", strerror(errno));
                     freeaddrinfo(res);
                     return(-1);
              }
              rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
              if (rc >= 0) {
                        freeaddrinfo(res);
                     return(sock);
              }
              else {
                     syslog(LOG_ERR, "connect() failed: %s", strerror(errno));
                     close(sock);
              }
       }
       freeaddrinfo(res);
       return(-1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sock_getln ( int *  sock,
char *  buf,
int  bufsize 
)

Definition at line 173 of file clientsocket.c.

{
       int i, retval;
       CitContext *CCC = MyContext();
       const char *pCh;

       FlushStrBuf(CCC->sMigrateBuf);
       retval = CtdlSockGetLine(sock, CCC->sMigrateBuf, 5);

       i = StrLength(CCC->sMigrateBuf);
       pCh = ChrPtr(CCC->sMigrateBuf);

       memcpy(buf, pCh, i + 1);

       FlushStrBuf(CCC->sMigrateBuf);
       if (retval < 0) {
              safestrncpy(&buf[i], "000", bufsize - i);
              i += 3;
       }
       return i;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sock_puts ( int *  sock,
char *  buf 
)

Definition at line 326 of file clientsocket.c.

{
       int i, j;

       i = sock_write(sock, buf, strlen(buf));
       if (i < 0)
              return (i);
       j = sock_write(sock, "\n", 1);
       if (j < 0)
              return (j);
       return (i + j);
}

Here is the call graph for this function:

int sock_write ( int *  sock,
const char *  buf,
int  nbytes 
)

Definition at line 200 of file clientsocket.c.

{ return sock_write_timeout(sock, buf, nbytes, 50); }

Here is the call graph for this function:

Here is the caller graph for this function:

int sock_write_timeout ( int *  sock,
const char *  buf,
int  nbytes,
int  timeout 
)

*Error = strerror(errno);

*Error = strerror(errno);

Definition at line 202 of file clientsocket.c.

{
       int nSuccessLess = 0;
       int bytes_written = 0;
       int retval;
       fd_set rfds;
        int fdflags;
       int IsNonBlock;
       struct timeval tv;
       int selectresolution = 100;

       fdflags = fcntl(*sock, F_GETFL);
       IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;

       while ((nSuccessLess < timeout) && 
              (*sock != -1) && 
              (bytes_written < nbytes)) 
       {
              if (IsNonBlock){
                     tv.tv_sec = selectresolution;
                     tv.tv_usec = 0;
                     
                     FD_ZERO(&rfds);
                     FD_SET(*sock, &rfds);
                     if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
                            close (*sock);
                            *sock = -1;
                            return -1;
                     }
              }
              if (IsNonBlock && !  FD_ISSET(*sock, &rfds)) {
                     nSuccessLess ++;
                     continue;
              }
              retval = write(*sock, &buf[bytes_written],
                            nbytes - bytes_written);
              if (retval < 1) {
                     sock_close(*sock);
                     *sock = -1;
                     return (-1);
              }
              bytes_written = bytes_written + retval;
              if (IsNonBlock && (bytes_written == nbytes)){
                     tv.tv_sec = selectresolution;
                     tv.tv_usec = 0;
                     
                     FD_ZERO(&rfds);
                     FD_SET(*sock, &rfds);
                     if (select(*sock + 1, NULL, &rfds, NULL, &tv) == -1) {
                            close (*sock);
                            *sock = -1;
                            return -1;
                     }
              }
       }
       return (bytes_written);
}

Here is the caller graph for this function:

int socket_read_blob ( int *  Socket,
StrBuf *  Target,
int  bytes,
int  timeout 
)

Definition at line 132 of file clientsocket.c.

{
       CitContext *CCC = MyContext();
       const char *Error;
       int retval = 0;


       retval = StrBufReadBLOBBuffered(Target,
                                   CCC->SBuf.Buf,
                                   &CCC->SBuf.ReadWritePointer,
                                   Socket, 1, bytes, O_TERM, &Error);
       
       if (retval < 0) {
              syslog(LOG_CRIT, "socket_read_blob() failed: %s", Error);
       }
       return retval;
}

Here is the call graph for this function: