]> code.citadel.org Git - citadel.git/blobdiff - webcit/tcp_sockets.c
* stuff
[citadel.git] / webcit / tcp_sockets.c
index 50a9b299efb607ddf6c2c3da583ed2a0bc776d74..9c32b71588a744752ec4fea7101838e329ad309f 100644 (file)
@@ -19,6 +19,8 @@
 #include <sys/time.h>
 #include <limits.h>
 #include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/un.h>
 #include <netdb.h>
 #include <string.h>
 #include <pwd.h>
 #include <pthread.h>
 #include <signal.h>
 #include "webcit.h"
+#include "webserver.h"
 
 
-
-
-
-char server_is_local = 0;
-
 #ifndef INADDR_NONE
 #define INADDR_NONE 0xffffffff
 #endif
 
-extern int errno;
-
 RETSIGTYPE timeout(int signum)
 {
-       fprintf(stderr, "Connection timed out.\n");
+       lprintf(1, "Connection timed out.\n");
        exit(3);
 }
 
-int connectsock(char *host, char *service, char *protocol)
+
+
+/*
+ * Connect a unix domain socket
+ */
+int uds_connectsock(char *sockpath)
+{
+       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) {
+               lprintf(1, "Can't create socket: %s\n",
+                       strerror(errno));
+               return(-1);
+       }
+
+       if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+               lprintf(1, "Can't connect: %s\n",
+                       strerror(errno));
+               return(-1);
+       }
+
+       return s;
+}
+
+
+/*
+ * Connect a TCP/IP socket
+ */
+int tcp_connectsock(char *host, char *service)
 {
        struct hostent *phe;
        struct servent *pse;
        struct protoent *ppe;
        struct sockaddr_in sin;
-       int s, type;
+       int s;
 
-       bzero((char *) &sin, sizeof(sin));
+       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) {
-               fprintf(stderr, "Can't get %s service entry\n", service);
+               lprintf(1, "Can't get %s service entry\n", service);
                return (-1);
        }
        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",
+               lprintf(1, "Can't get %s host entry: %s\n",
                        host, strerror(errno));
                return (-1);
        }
-       if ((ppe = getprotobyname(protocol)) == 0) {
-               fprintf(stderr, "Can't get %s protocol entry: %s\n",
-                       protocol, strerror(errno));
+       if ((ppe = getprotobyname("tcp")) == 0) {
+               lprintf(1, "Can't get TCP protocol entry: %s\n",
+                       strerror(errno));
                return (-1);
        }
-       if (!strcmp(protocol, "udp"))
-               type = SOCK_DGRAM;
-       else
-               type = SOCK_STREAM;
 
-       s = socket(PF_INET, type, ppe->p_proto);
+       s = socket(PF_INET, SOCK_STREAM, ppe->p_proto);
        if (s < 0) {
-               fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
+               lprintf(1, "Can't create socket: %s\n", strerror(errno));
                return (-1);
        }
        signal(SIGALRM, timeout);
        alarm(30);
 
        if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-               fprintf(stderr, "can't connect to %s.%s: %s\n",
+               lprintf(1, "Can't connect to %s.%s: %s\n",
                        host, service, strerror(errno));
                return (-1);
        }
@@ -115,7 +141,7 @@ void serv_read(char *buf, int bytes)
        while (len < bytes) {
                rlen = read(WC->serv_sock, &buf[len], bytes - len);
                if (rlen < 1) {
-                       fprintf(stderr, "Server connection broken: %s\n",
+                       lprintf(1, "Server connection broken: %s\n",
                                strerror(errno));
                        WC->connected = 0;
                        WC->logged_in = 0;
@@ -142,7 +168,7 @@ void serv_gets(char *strbuf)
                strbuf[len++] = ch;
        } while ((ch != 10) && (ch != 13) && (ch != 0) && (len < 255));
        strbuf[len - 1] = 0;
-       /* fprintf(stderr, ">%s\n", strbuf); */
+       /* lprintf(9, ">%s\n", strbuf); */
 }
 
 
@@ -158,7 +184,7 @@ void serv_write(char *buf, int nbytes)
                retval = write(WC->serv_sock, &buf[bytes_written],
                               nbytes - bytes_written);
                if (retval < 1) {
-                       fprintf(stderr, "Server connection broken: %s\n",
+                       lprintf(1, "Server connection broken: %s\n",
                                strerror(errno));
                        WC->connected = 0;
                        WC->logged_in = 0;
@@ -174,7 +200,7 @@ void serv_write(char *buf, int nbytes)
  */
 void serv_puts(char *string)
 {
-       char buf[256];
+       char buf[SIZ];
 
        sprintf(buf, "%s\n", string);
        serv_write(buf, strlen(buf));
@@ -187,7 +213,7 @@ void serv_puts(char *string)
 void serv_printf(const char *format,...)
 {
        va_list arg_ptr;
-       char buf[256];
+       char buf[SIZ];
 
        va_start(arg_ptr, format);
        vsprintf(buf, format, arg_ptr);
@@ -195,5 +221,5 @@ void serv_printf(const char *format,...)
 
        strcat(buf, "\n");
        serv_write(buf, strlen(buf));
-       /* fprintf(stderr, "<%s", buf); */
+       /* lprintf(9, "<%s", buf); */
 }