]> code.citadel.org Git - citadel.git/blobdiff - citadel/ipc_c_tcp.c
Fixes for Cygwin (see ChangeLog)
[citadel.git] / citadel / ipc_c_tcp.c
index c408136babdb4de2152bd5ecdc535cf264184db8..7f99b246403079c016b87eef6da67a440b16cf27 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * ipc_c_std.c
+ * ipc_c_tcp.c
  * 
  * Citadel/UX client/server IPC - client module using TCP/IP
  *
- * version 1.3
+ * version 1.3 $Id$
  *
  */
 
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
 #include <netdb.h>
 #include <string.h>
 #include <pwd.h>
 #include <errno.h>
-
-void logoff();
+#include <stdarg.h>
+#include "citadel.h"
+#include "citadel_decls.h"
+#include "ipc.h"
+#ifndef HAVE_SNPRINTF
+#include "snprintf.h"
+#endif
 
 /*
  * If server_is_local is set to nonzero, the client assumes that it is running
@@ -45,24 +51,20 @@ char server_is_local = 0;
 
 int serv_sock;
 
-u_long inet_addr();
-
-void timeout() {
+void timeout(int signum) {
        printf("\rConnection timed out.\n");
        logoff(3);
        }
 
-int connectsock(host,service,protocol)
-char *host;
-char *service;
-char *protocol; {
+int connectsock(char *host, char *service, char *protocol)
+{
        struct hostent *phe;
        struct servent *pse;
        struct protoent *ppe;
        struct sockaddr_in sin;
        int s,type;
 
-       bzero((char *)&sin,sizeof(sin));
+       memset(&sin,0,sizeof(sin));
        sin.sin_family = AF_INET;
 
        pse=getservbyname(service,protocol);
@@ -77,7 +79,7 @@ char *protocol; {
        
        phe=gethostbyname(host);
        if (phe) {
-               bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
+               memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
                }
        else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
                fprintf(stderr,"Can't get %s host entry: %s\n",
@@ -124,16 +126,13 @@ char *protocol; {
  * convert service and host entries into a six-byte numeric in the format
  * expected by a SOCKS v4 server
  */
-void numericize(buf,host,service,protocol)
-unsigned char buf[];
-char *host;
-char *service;
-char *protocol; {
+void numericize(char *buf, char *host, char *service, char *protocol)
+{
        struct hostent *phe;
        struct servent *pse;
        struct sockaddr_in sin;
 
-       bzero((char *)&sin,sizeof(sin));
+       memset(&sin,0,sizeof(sin));
        sin.sin_family = AF_INET;
 
        pse=getservbyname(service,protocol);
@@ -151,7 +150,7 @@ char *protocol; {
        
        phe=gethostbyname(host);
        if (phe) {
-               bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
+               memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
                }
        else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
                fprintf(stderr,"Can't get %s host entry: %s\n",
@@ -167,17 +166,16 @@ char *protocol; {
 /*
  * input binary data from socket
  */
-void serv_read(buf,bytes)
-char buf[];
-int bytes; {
+void serv_read(char *buf, int bytes)
+{
        int len,rlen;
 
        len = 0;
        while(len<bytes) {
                rlen = read(serv_sock,&buf[len],bytes-len);
                if (rlen<1) {
-                       printf("\rNetwork error - connection terminated.\n");
-                       printf("%s\n", strerror(errno));
+                       /* printf("\rNetwork error - connection terminated.\n");
+                       printf("%s\n", strerror(errno)); */
                        logoff(3);
                        }
                len = len + rlen;
@@ -188,17 +186,17 @@ int bytes; {
 /*
  * send binary to server
  */
-void serv_write(buf, nbytes)
-char buf[];
-int nbytes; {
+void serv_write(char *buf, int nbytes)
+{
        int bytes_written = 0;
        int retval;
        while (bytes_written < nbytes) {
                retval = write(serv_sock, &buf[bytes_written],
                        nbytes - bytes_written);
                if (retval < 1) {
+                       /*
                        printf("\rNetwork error - connection terminated.\n");
-                       printf("%s\n", strerror(errno));
+                       printf("%s\n", strerror(errno)); */
                        logoff(3);
                        }
                bytes_written = bytes_written + retval;
@@ -210,23 +208,35 @@ int nbytes; {
 /*
  * input string from socket - implemented in terms of serv_read()
  */
-void serv_gets(buf)
-char buf[]; {
-       buf[0] = 0;
-       do {
-               buf[strlen(buf) + 1] = 0;
-               if (strlen(buf) < 255) serv_read(&buf[strlen(buf)], 1);
-               } while (buf[strlen(buf)-1] != 10);
-       if (strlen(buf) > 0) buf[strlen(buf)-1] = 0;
-       /* printf("> %s\n", buf); */
+void serv_gets(char *buf)
+{
+       int i;
+
+       /* Read one character at a time.
+        */
+       for (i = 0;;i++) {
+               serv_read(&buf[i], 1);
+               if (buf[i] == '\n' || i == 255)
+                       break;
+               }
+
+       /* If we got a long line, discard characters until the newline.
+        */
+       if (i == 255)
+               while (buf[i] != '\n')
+                       serv_read(&buf[i], 1);
+
+       /* Strip the trailing newline.
+        */
+       buf[i] = 0;
        }
 
 
 /*
  * send line to server - implemented in terms of serv_write()
  */
-void serv_puts(buf)
-char *buf; {
+void serv_puts(char *buf)
+{
        /* printf("< %s\n", buf); */
        serv_write(buf, strlen(buf));
        serv_write("\n", 1);
@@ -236,14 +246,13 @@ char *buf; {
 /*
  * attach to server
  */
-void attach_to_server(argc,argv)
-int argc;
-char *argv[]; {
+void attach_to_server(int argc, char **argv)
+{
        int a;
        char cithost[256];      int host_copied = 0;
        char citport[256];      int port_copied = 0;
        char socks4[256];
-       unsigned char buf[256];
+       char buf[256];
        struct passwd *p;
 
        strcpy(cithost,DEFAULT_HOST);   /* default host */
@@ -292,7 +301,7 @@ char *argv[]; {
        printf("Attaching to server...\r");
        fflush(stdout);
 
-       sprintf(buf,"%c%c",
+       snprintf(buf,sizeof buf,"%c%c",
                4,                      /* version 4 */
                1);                     /* method = connect */
        serv_write(buf,2);
@@ -316,7 +325,7 @@ char *argv[]; {
 /*
  * return the file descriptor of the server socket so we can select() on it.
  */
-int getsockfd() {
+int getsockfd(void) {
        return serv_sock;
        }
 
@@ -324,7 +333,7 @@ int getsockfd() {
 /*
  * return one character
  */
-char serv_getc() {
+char serv_getc(void) {
        char buf[2];
        char ch;