Back to index

webcit  8.12-dfsg
Defines | Functions
tcp_sockets.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define GetServerStatus(a, b)   GetServerStatusMsg(a, b, 0, 0)
 to migrate old calls....

Functions

int uds_connectsock (char *)
int tcp_connectsock (char *, char *)
int serv_getln (char *strbuf, int bufsize)
int StrBuf_ServGetln (StrBuf *buf)
int GetServerStatusMsg (StrBuf *Line, long *FullState, int PutImportantMessage, int MajorOK)
 parse & check the server reply
int serv_puts (const char *string)
int serv_write (const char *buf, int nbytes)
int serv_putbuf (const StrBuf *string)
int serv_printf (const char *format,...) __attribute__((__format__(__printf__
int int serv_read_binary (StrBuf *Ret, size_t total_len, StrBuf *Buf)
int StrBuf_ServGetBLOB (StrBuf *buf, long BlobSize)
int StrBuf_ServGetBLOBBuffered (StrBuf *buf, long BlobSize)
int read_server_text (StrBuf *Buf, long *nLines)
void text_to_server (char *ptr)
void text_to_server_qp (char *ptr)
void server_to_text (void)
int lingering_close (int fd)

Function Documentation

int lingering_close ( int  fd)

Definition at line 838 of file tcp_sockets.c.

{
       char buf[SIZ];
       int i;
       fd_set set;
       struct timeval tv, start;

       gettimeofday(&start, NULL);
       if (fd == -1)
              return -1;
       shutdown(fd, 1);
       do {
              do {
                     gettimeofday(&tv, NULL);
                     tv.tv_sec = SLEEPING - (tv.tv_sec - start.tv_sec);
                     tv.tv_usec = start.tv_usec - tv.tv_usec;
                     if (tv.tv_usec < 0) {
                            tv.tv_sec--;
                            tv.tv_usec += 1000000;
                     }
                     FD_ZERO(&set);
                     FD_SET(fd, &set);
                     i = select(fd + 1, &set, NULL, NULL, &tv);
              } while (i == -1 && errno == EINTR);

              if (i <= 0)
                     break;

              i = read(fd, buf, sizeof buf);
       } while (i != 0 && (i != -1 || errno == EINTR));

       return close(fd);
}

Here is the caller graph for this function:

int read_server_text ( StrBuf *  Buf,
long *  nLines 
)

Definition at line 475 of file serv_func.c.

{
       wcsession *WCC = WC;
       StrBuf *ReadBuf;
       long nRead;
       long nTotal = 0;
       long nlines;
       
       nlines = 0;
       ReadBuf = NewStrBuf();
       while ((WCC->serv_sock!=-1) &&
              (nRead = StrBuf_ServGetln(ReadBuf), (nRead >= 0) &&
              ((nRead != 3)||(strcmp(ChrPtr(ReadBuf), "000") != 0))))
       {
              StrBufAppendBuf(Buf, ReadBuf, 0);
              StrBufAppendBufPlain(Buf, HKEY("\n"), 0);
              nTotal += nRead;
              nlines ++;
       }
       FreeStrBuf(&ReadBuf);
       *nLines = nlines;
       return nTotal;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int serv_getln ( char *  strbuf,
int  bufsize 
)

Definition at line 142 of file tcp_sockets.c.

{
       wcsession *WCC = WC;
       int len;

       *strbuf = '\0';
       StrBuf_ServGetln(WCC->MigrateReadLineBuf);
       len = StrLength(WCC->MigrateReadLineBuf);
       if (len > bufsize)
              len = bufsize - 1;
       memcpy(strbuf, ChrPtr(WCC->MigrateReadLineBuf), len);
       FlushStrBuf(WCC->MigrateReadLineBuf);
       strbuf[len] = '\0';
#ifdef SERV_TRACE
       syslog(9, "%3d<<<%s\n", WC->serv_sock, strbuf);
#endif
       return len;
}

Here is the call graph for this function:

int serv_printf ( const char *  format,
  ... 
)
int serv_putbuf ( const StrBuf *  string)

Definition at line 348 of file tcp_sockets.c.

{
#ifdef SERV_TRACE
       syslog(9, "%3d>>>%s\n", WC->serv_sock, ChrPtr(string));
#endif
       FlushReadBuf();

       if (!serv_write(ChrPtr(string), StrLength(string)))
              return 0;
       return serv_write("\n", 1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int serv_puts ( const char *  string)

Definition at line 332 of file tcp_sockets.c.

{
#ifdef SERV_TRACE
       syslog(9, "%3d>>>%s\n", WC->serv_sock, string);
#endif
       FlushReadBuf();

       if (!serv_write(string, strlen(string)))
              return 0;
       return serv_write("\n", 1);
}

Here is the call graph for this function:

int int serv_read_binary ( StrBuf *  Ret,
size_t  total_len,
StrBuf *  Buf 
)

Definition at line 394 of file tcp_sockets.c.

{
       wcsession *WCC = WC;
       size_t bytes_read = 0;
       size_t this_block = 0;
       int rc = 6;
       int ServerRc = 6;

       if (Ret == NULL) {
              return -1;
       }

       while ((bytes_read < total_len) && (ServerRc == 6)) {

              if (WCC->serv_sock==-1) {
                     FlushStrBuf(Ret); 
                     return -1; 
              }

              serv_printf("READ "SIZE_T_FMT"|"SIZE_T_FMT, bytes_read, total_len-bytes_read);
              if ( (rc = StrBuf_ServGetln(Buf) > 0) &&
                   (ServerRc = GetServerStatus(Buf, NULL), ServerRc == 6) ) 
              {
                     if (rc < 0)
                            return rc;
                     StrBufCutLeft(Buf, 4);
                     this_block = StrTol(Buf);
                     rc = StrBuf_ServGetBLOBBuffered(Ret, this_block);
                     if (rc < 0) {
                            syslog(1, "Server connection broken during download\n");
                            wc_backtrace();
                            if (WCC->serv_sock > 0) close(WCC->serv_sock);
                            WCC->serv_sock = (-1);
                            WCC->connected = 0;
                            WCC->logged_in = 0;
                            return rc;
                     }
                     bytes_read += rc;
              }
       }

       return StrLength(Ret);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int serv_write ( const char *  buf,
int  nbytes 
)

Definition at line 302 of file tcp_sockets.c.

{
       wcsession *WCC = WC;
       int bytes_written = 0;
       int retval;

       FlushReadBuf();
       while (bytes_written < nbytes) {
              retval = write(WCC->serv_sock, &buf[bytes_written],
                            nbytes - bytes_written);
              if (retval < 1) {
                     const char *ErrStr = strerror(errno);
                     syslog(1, "serv_write(): Server connection broken: %s\n",
                            (ErrStr)?ErrStr:"");
                     if (WCC->serv_sock > 0) close(WCC->serv_sock);
                     WCC->serv_sock = (-1);
                     WCC->connected = 0;
                     WCC->logged_in = 0;
                     return 0;
              }
              bytes_written = bytes_written + retval;
       }
       return 1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void server_to_text ( void  )

Definition at line 452 of file serv_func.c.

{
       char buf[SIZ];

       int count = 0;

       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
              if ((buf[0] == 32) && (count > 0)) {
                     wc_printf("\n");
              }
              wc_printf("%s", buf);
              ++count;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int StrBuf_ServGetBLOB ( StrBuf *  buf,
long  BlobSize 
)

Definition at line 232 of file tcp_sockets.c.

{
       wcsession *WCC = WC;
       const char *ErrStr;
       int rc;
       
       WCC->ReadPos = NULL;
       rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &ErrStr);
       if (rc < 0)
       {
              syslog(1, "StrBuf_ServGetBLOB(): Server connection broken: %s\n",
                     (ErrStr)?ErrStr:"");
              wc_backtrace();
              if (WCC->serv_sock > 0) close(WCC->serv_sock);
              WCC->serv_sock = (-1);
              WCC->connected = 0;
              WCC->logged_in = 0;
       }
#ifdef SERV_TRACE
        else
                syslog(9, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
#endif

       return rc;
}

Here is the call graph for this function:

int StrBuf_ServGetBLOBBuffered ( StrBuf *  buf,
long  BlobSize 
)

Definition at line 200 of file tcp_sockets.c.

{
       wcsession *WCC = WC;
       const char *ErrStr;
       int rc;
       
       rc = StrBufReadBLOBBuffered(buf, 
                                WCC->ReadBuf, 
                                &WCC->ReadPos,
                                &WCC->serv_sock, 
                                1, 
                                BlobSize, 
                                NNN_TERM,
                                &ErrStr);
       if (rc < 0)
       {
              syslog(1, "StrBuf_ServGetBLOBBuffered(): Server connection broken: %s\n",
                     (ErrStr)?ErrStr:"");
              wc_backtrace();
              if (WCC->serv_sock > 0) close(WCC->serv_sock);
              WCC->serv_sock = (-1);
              WCC->connected = 0;
              WCC->logged_in = 0;
       }
#ifdef SERV_TRACE
        else
                syslog(9, "%3d<<<BLOB: %d bytes\n", WC->serv_sock, StrLength(buf));
#endif

       return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int StrBuf_ServGetln ( StrBuf *  buf)

Definition at line 162 of file tcp_sockets.c.

{
       wcsession *WCC = WC;
       const char *ErrStr = NULL;
       int rc;
       
       if (!WCC->connected)
              return -1;

       FlushStrBuf(buf);
       rc = StrBufTCP_read_buffered_line_fast(buf, 
                                          WCC->ReadBuf, 
                                          &WCC->ReadPos, 
                                          &WCC->serv_sock, 
                                          5, 1, 
                                          &ErrStr);
       if (rc < 0)
       {
              syslog(1, "StrBuf_ServGetln(): Server connection broken: %s\n",
                     (ErrStr)?ErrStr:"");
              wc_backtrace();
              if (WCC->serv_sock > 0) close(WCC->serv_sock);
              WCC->serv_sock = (-1);
              WCC->connected = 0;
              WCC->logged_in = 0;
       }
#ifdef SERV_TRACE
       else 
       {
              long pos = 0;
              if (WCC->ReadPos != NULL)
                     pos = WCC->ReadPos - ChrPtr(WCC->ReadBuf);
              syslog(9, "%3d<<<[%ld]%s\n", WC->serv_sock, pos, ChrPtr(buf));
       }
#endif
       return rc;
}

Here is the call graph for this function:

int tcp_connectsock ( char *  ,
char *   
)

Definition at line 64 of file tcp_sockets.c.

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

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

       syslog(9, "tcp_connectsock(%s,%s)\n", host, service);

       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(1, "%s: %s\n", host, gai_strerror(rc));
              freeaddrinfo(res);
              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) {

              if (ai->ai_family == AF_INET) syslog(9, "Trying IPv4\n");
              else if (ai->ai_family == AF_INET6) syslog(9, "Trying IPv6\n");
              else syslog(9, "This is going to fail.\n");

              s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
              if (s < 0) {
                     syslog(1, "socket() failed: %s\n", strerror(errno));
                     freeaddrinfo(res);
                     return(-1);
              }
              rc = connect(s, ai->ai_addr, ai->ai_addrlen);
              if (rc >= 0) {
                     freeaddrinfo(res);
                     return(s);
              }
              else {
                     syslog(1, "connect() failed: %s\n", strerror(errno));
                     close(s);
              }
       }
        freeaddrinfo(res);
       return(-1);
}

Here is the caller graph for this function:

void text_to_server ( char *  ptr)

Definition at line 338 of file serv_func.c.

{
       char buf[256];
       int ch, a, pos, len;

       pos = 0;
       buf[0] = 0;

       while (ptr[pos] != 0) {
              ch = ptr[pos++];
              if (ch == 10) {
                     len = strlen(buf);
                     while ( (isspace(buf[len - 1]))
                            && (buf[0] !=  '\0') 
                            && (buf[1] !=  '\0') )
                            buf[--len] = 0;
                     serv_puts(buf);
                     buf[0] = 0;
                     if (ptr[pos] != 0) strcat(buf, " ");
              } else {
                     a = strlen(buf);
                     buf[a + 1] = 0;
                     buf[a] = ch;
                     if ((ch == 32) && (strlen(buf) > 200)) {
                            buf[a] = 0;
                            serv_puts(buf);
                            buf[0] = 0;
                     }
                     if (strlen(buf) > 250) {
                            serv_puts(buf);
                            buf[0] = 0;
                     }
              }
       }
       serv_puts(buf);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void text_to_server_qp ( char *  ptr)

Definition at line 379 of file serv_func.c.

{
       unsigned char ch, buf[256];
       int pos;
       int output_len = 0;

       pos = 0;
       buf[0] = 0;
       output_len = 0;

       while (ptr[pos] != 0) {
              ch = (unsigned char)(ptr[pos++]);

              if (ch == 13) {
                     /* ignore carriage returns */
              }
              else if (ch == 10) {
                     /* hard line break */
                     if (output_len > 0) {
                            if (isspace(buf[output_len-1])) {
                                   sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
                                   output_len += 2;
                            }
                     }
                     buf[output_len++] = 0;
                     serv_puts((char *)buf);
                     output_len = 0;
              }
              else if (ch == 9) {
                     buf[output_len++] = ch;
              }
              else if ( (ch >= 32) && (ch <= 60) ) {
                     buf[output_len++] = ch;
              }
              else if ( (ch >= 62) && (ch <= 126) ) {
                     buf[output_len++] = ch;
              }
              else {
                     sprintf((char *)&buf[output_len], "=%02X", ch);
                     output_len += 3;
              }
              
              if (output_len > 72) {
                     /* soft line break */
                     if (isspace(buf[output_len-1])) {
                            sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
                            output_len += 2;
                     }
                     buf[output_len++] = '=';
                     buf[output_len++] = 0;
                     serv_puts((char *)buf);
                     output_len = 0;
              }
       }

       /* end of data - transmit anything that's left */
       if (output_len > 0) {
              if (isspace(buf[output_len-1])) {
                     sprintf((char *)&buf[output_len-1], "=%02X", buf[output_len-1]);
                     output_len += 2;
              }
              buf[output_len++] = 0;
              serv_puts((char *)buf);
              output_len = 0;
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int uds_connectsock ( char *  )

Definition at line 36 of file tcp_sockets.c.

{
       struct sockaddr_un addr;
       int s;

       memset(&addr, 0, sizeof(addr));
       addr.sun_family = AF_UNIX;
       strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);

       s = socket(AF_UNIX, SOCK_STREAM, 0);
       if (s < 0) {
              syslog(1, "Can't create socket [%s]: %s\n", sockpath, strerror(errno));
              return(-1);
       }

       if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
              syslog(1, "Can't connect [%s]: %s\n", sockpath, strerror(errno));
              close(s);
              return(-1);
       }

       return s;
}

Here is the caller graph for this function: