]> code.citadel.org Git - citadel.git/blobdiff - citadel/clientsocket.c
* Removed the 'protocol' parameter from the sock_connect() function. All we have...
[citadel.git] / citadel / clientsocket.c
index 7c69fe67333c93d3d82698f0291c3e078e667fb0..67414f93a56ee76bcdd21acdb7fe4ee07a0ee6ba 100644 (file)
@@ -54,7 +54,7 @@
 #define INADDR_NONE 0xffffffff
 #endif
 
-int sock_connect(char *host, char *service, char *protocol)
+int sock_connect(char *host, char *service)
 {
        struct hostent *phe;
        struct servent *pse;
@@ -67,13 +67,11 @@ int sock_connect(char *host, char *service, char *protocol)
                return(-1);
        if ((service == NULL) || IsEmptyStr(service)) 
                return(-1);
-       if ((protocol == NULL) || IsEmptyStr(protocol)) 
-               return(-1);
 
        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
 
-       pse = getservbyname(service, protocol);
+       pse = getservbyname(service, "tcp");
        if (pse) {
                sin.sin_port = pse->s_port;
        } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
@@ -89,16 +87,11 @@ int sock_connect(char *host, char *service, char *protocol)
                        host, strerror(errno));
                return(-1);
        }
-       if ((ppe = getprotobyname(protocol)) == 0) {
-               CtdlLogPrintf(CTDL_CRIT, "Can't get %s protocol entry: %s\n",
-                       protocol, strerror(errno));
+       if ((ppe = getprotobyname("tcp")) == 0) {
+               CtdlLogPrintf(CTDL_CRIT, "Can't get tcp protocol entry: %s\n", strerror(errno));
                return(-1);
        }
-       if (!strcmp(protocol, "udp")) {
-               type = SOCK_DGRAM;
-       } else {
-               type = SOCK_STREAM;
-       }
+       type = SOCK_STREAM;
 
        s = socket(PF_INET, type, ppe->p_proto);
        if (s < 0) {
@@ -152,7 +145,7 @@ int socket_read_blob(int *Socket,
                     int bytes, 
                     int timeout)
 {
-       CitContext *CCC=CC;
+       CitContext *CCC = MyContext();
        const char *Error;
        int retval = 0;
 
@@ -177,9 +170,10 @@ int socket_read_blob(int *Socket,
 
 int sock_read_to(int *sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
 {
-       CitContext *CCC=CC;
+       CitContext *CCC = MyContext();
        int rc;
 
+       FlushStrBuf(CCC->MigrateBuf);
        rc = socket_read_blob(sock, 
                              CCC->sMigrateBuf, 
                              bytes, 
@@ -205,10 +199,11 @@ int sock_read_to(int *sock, char *buf, int bytes, int timeout, int keep_reading_
 
 int CtdlSockGetLine(int *sock, StrBuf *Target)
 {
-       CitContext *CCC=CC;
+       CitContext *CCC = MyContext();
        const char *Error;
        int rc;
 
+       FlushStrBuf(Target);
        rc = StrBufTCP_read_buffered_line_fast(Target, 
                                               CCC->sReadBuf,
                                               &CCC->sPos,
@@ -233,9 +228,10 @@ int CtdlSockGetLine(int *sock, StrBuf *Target)
 int sock_getln(int *sock, char *buf, int bufsize)
 {
        int i, retval;
-       CitContext *CCC=CC;
+       CitContext *CCC = MyContext();
        const char *pCh;
 
+       FlushStrBuf(CCC->sMigrateBuf);
        retval = CtdlSockGetLine(sock, CCC->sMigrateBuf);
 
        i = StrLength(CCC->sMigrateBuf);
@@ -255,8 +251,9 @@ int sock_getln(int *sock, char *buf, int bufsize)
        FlushStrBuf(CCC->sMigrateBuf);
        if (retval < 0) {
                safestrncpy(&buf[i], "000", bufsize - i);
+               i += 3;
        }
-       return(retval >= 0);
+       return i;
 }
 
 
@@ -274,14 +271,19 @@ INLINE int sock_read(int *sock, char *buf, int bytes, int keep_reading_until_ful
  * sock_write() - send binary to server.
  * Returns the number of bytes written, or -1 for error.
  */
-int sock_write(int sock, char *buf, int nbytes)
+int sock_write(int *sock, const char *buf, int nbytes)
 {
        int bytes_written = 0;
        int retval;
-       while (bytes_written < nbytes) {
-               retval = write(sock, &buf[bytes_written],
+
+       while ((*sock != -1)  && 
+              (bytes_written < nbytes))
+       {
+               retval = write(*sock, &buf[bytes_written],
                               nbytes - bytes_written);
                if (retval < 1) {
+                       sock_close(*sock);
+                       *sock = -1;
                        return (-1);
                }
                bytes_written = bytes_written + retval;
@@ -316,7 +318,7 @@ int ml_sock_gets(int *sock, char *buf) {
  * sock_puts() - send line to server - implemented in terms of serv_write()
  * Returns the number of bytes written, or -1 for error.
  */
-int sock_puts(int sock, char *buf)
+int sock_puts(int *sock, char *buf)
 {
        int i, j;