* Initial work on IPv6-enabling citserver
[citadel.git] / citadel / sysdep.c
index 2625d615d2fd5ab38c9a6beaae2ec53084a53d29..f4ac8d70bb5d39ef54b0f68f5b6d384f102bddc3 100644 (file)
@@ -93,7 +93,7 @@ int verbosity = DEFAULT_VERBOSITY;            /* Logging level */
 
 int syslog_facility = LOG_DAEMON;
 int enable_syslog = 0;
-
+int print_to_logfile = 1;
 
 /*
  * CtdlLogPrintf()  ...   Write logging information
@@ -113,85 +113,54 @@ void vCtdlLogPrintf(enum LogLevel loglevel, const char *format, va_list arg_ptr)
        }
 
        /* stderr output code */
-       if (enable_syslog || running_as_daemon) return;
+       if (enable_syslog || !print_to_logfile) return;
 
        /* if we run in forground and syslog is disabled, log to terminal */
        if (loglevel <= verbosity) { 
                struct timeval tv;
                struct tm tim;
                time_t unixtime;
-               StrBuf *lBuf;
-               CitContext *CCC = CC;
+               CitContext *CCC = MyContext();
                ThreadTSD *cTSD = CTP;
                CtdlThreadNode *node = NULL;
                long lwpid = 0;
 
-               if (cTSD != NULL) 
+               if (cTSD != NULL) {
                        node = cTSD->self;
-               if ((node != NULL) && (node->reltid != 0))
-               {
+               }
+
+               if ((node != NULL) && (node->reltid != 0)) {
                        lwpid = node->reltid;
                }
 
                gettimeofday(&tv, NULL);
+
                /* Promote to time_t; types differ on some OSes (like darwin) */
                unixtime = tv.tv_sec;
                localtime_r(&unixtime, &tim);
 
-               if ((CCC != NULL) && (CCC != &masterCC))
-                       lBuf = CCC->lBuf;
-               else 
-                       lBuf = NewStrBuf();
-               if (lBuf == NULL) {
-                       char buf[SIZ], buf2[SIZ];
-                       
-                       if ((CCC != NULL) && (CCC->cs_pid != 0)) {
-                               sprintf(buf,
-                                       "%04d/%02d/%02d %2d:%02d:%02d.%06ld xx [%3d] ",
-                                       tim.tm_year + 1900, tim.tm_mon + 1,
-                                       tim.tm_mday, tim.tm_hour, tim.tm_min,
-                                       tim.tm_sec, (long)tv.tv_usec,
-                                       CCC->cs_pid);
-                       } else {
-                               sprintf(buf,
-                                       "%04d/%02d/%02d %2d:%02d:%02d.%06ld xx ",
-                                       tim.tm_year + 1900, tim.tm_mon + 1,
-                                       tim.tm_mday, tim.tm_hour, tim.tm_min,
-                                       tim.tm_sec, (long)tv.tv_usec);
-                       }
-                       vsnprintf(buf2, SIZ, format, arg_ptr);   
+               fprintf(stderr,
+                       "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
+                       tim.tm_year + 1900, tim.tm_mon + 1,
+                       tim.tm_mday, tim.tm_hour, tim.tm_min,
+                       tim.tm_sec, (long)tv.tv_usec
+               );
 
-                       fprintf(stderr, ":%s%s", buf, buf2);
+               if (lwpid != 0) {
+                       fprintf(stderr, "[LWP:%ld] ", lwpid);
                }
-               else {
-                       StrBufPrintf(lBuf,
-                                    "%04d/%02d/%02d %2d:%02d:%02d.%06ld ",
-                                    tim.tm_year + 1900, tim.tm_mon + 1,
-                                    tim.tm_mday, tim.tm_hour, tim.tm_min,
-                                    tim.tm_sec, (long)tv.tv_usec);
-
                        
-                       if (lwpid != 0)
-                               StrBufAppendPrintf(lBuf,
-                                                  "[LWP:%d] ",
-                                                  lwpid);
-                               
-                       if (CCC != NULL) {
-                               if (CCC->cs_pid != 0)
-                                       StrBufAppendPrintf(lBuf,
-                                                          "[%3d] ",
-                                                          CCC->cs_pid);
-                               else if (CCC->user.usernum != 0)
-                                       StrBufAppendPrintf(lBuf,
-                                                          "[:%d] ",
-                                                          CCC->user.usernum);
+               if (CCC != NULL) {
+                       if (CCC->cs_pid != 0) {
+                               fprintf(stderr, "[%3d] ", CCC->cs_pid);
+                       }
+                       else if (CCC->user.usernum != 0) {
+                               fprintf(stderr, "[:%ld] ", CCC->user.usernum);
                        }
-                       StrBufVAppendPrintf(lBuf, format, arg_ptr);   
-                       fwrite(ChrPtr(lBuf), 1, StrLength(lBuf), stderr);
                }
-               fflush(stderr);
-               if (CCC == NULL) FreeStrBuf(&lBuf);
 
+               vfprintf(stderr, format, arg_ptr);   
+               fflush(stderr);
        }
 }   
 
@@ -288,90 +257,121 @@ void init_sysdep(void) {
 }
 
 
-
-
-/*
+/* 
  * This is a generic function to set up a master socket for listening on
- * a TCP port.  The server shuts down if the bind fails.
+ * a TCP port.  The server shuts down if the bind fails.  (IPv4/IPv6 version)
  *
+ * ip_addr     IP address to bind
+ * port_number port number to bind
+ * queue_len   number of incoming connections to allow in the queue
  */
-int ig_tcp_server(char *ip_addr, int port_number, int queue_len, char **errormessage)
+int ctdl_tcp_server(char *ip_addr, int port_number, int queue_len, char *errormessage)
 {
-       struct sockaddr_in sin;
-       int s, i;
-       int actual_queue_len;
-
-       actual_queue_len = queue_len;
-       if (actual_queue_len < 5) actual_queue_len = 5;
-
-       memset(&sin, 0, sizeof(sin));
-       sin.sin_family = AF_INET;
-       sin.sin_port = htons((u_short)port_number);
-       if (ip_addr == NULL) {
-               sin.sin_addr.s_addr = INADDR_ANY;
+       struct protoent *p;
+       struct sockaddr_in6 sin6;
+       struct sockaddr_in sin4;
+       int s, i, b;
+       int ip_version = 6;
+
+       memset(&sin6, 0, sizeof(sin6));
+       memset(&sin4, 0, sizeof(sin4));
+       sin6.sin6_family = AF_INET6;
+       sin4.sin_family = AF_INET;
+
+       if (    (ip_addr == NULL)                                                       /* any IPv6 */
+               || (IsEmptyStr(ip_addr))
+               || (!strcmp(ip_addr, "*"))
+       ) {
+               ip_version = 6;
+               sin6.sin6_addr = in6addr_any;
        }
-       else {
-               sin.sin_addr.s_addr = inet_addr(ip_addr);
+       else if (!strcmp(ip_addr, "0.0.0.0"))                                           /* any IPv4 */
+       {
+               ip_version = 4;
+               sin4.sin_addr.s_addr = INADDR_ANY;
        }
-                                                                               
-       if (sin.sin_addr.s_addr == !INADDR_ANY) {
-               sin.sin_addr.s_addr = INADDR_ANY;
+       else if ((strchr(ip_addr, '.')) && (!strchr(ip_addr, ':')))                     /* specific IPv4 */
+       {
+               ip_version = 4;
+               if (inet_pton(AF_INET, ip_addr, &sin4.sin_addr) <= 0) {
+                       snprintf(errormessage, SIZ,
+                                "Error binding to [%s] : %s", ip_addr, strerror(errno)
+                       );
+                       CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+                       return (-1);
+               }
+       }
+       else                                                                            /* specific IPv6 */
+       {
+               ip_version = 6;
+               if (inet_pton(AF_INET6, ip_addr, &sin6.sin6_addr) <= 0) {
+                       snprintf(errormessage, SIZ,
+                                "Error binding to [%s] : %s", ip_addr, strerror(errno)
+                       );
+                       CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+                       return (-1);
+               }
+       }
+
+       if (port_number == 0) {
+               snprintf(errormessage, SIZ,
+                        "Can't start: no port number specified."
+               );
+               CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+               return (-1);
        }
+       sin6.sin6_port = htons((u_short) port_number);
+       sin4.sin_port = htons((u_short) port_number);
 
-       s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+       p = getprotobyname("tcp");
 
+       s = socket( ((ip_version == 6) ? PF_INET6 : PF_INET), SOCK_STREAM, (p->p_proto));
        if (s < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
-                                "citserver: Can't create a socket: %s",
-                                strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
-               return(-1);
+               snprintf(errormessage, SIZ,
+                        "Can't create a listening socket: %s", strerror(errno)
+               );
+               CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+               return (-1);
        }
-
+       /* Set some socket options that make sense. */
        i = 1;
        setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
 
-       if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
-                                "citserver: Can't bind: %s",
-                                strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
-               close(s);
-               return(-1);
+       if (ip_version == 6) {
+               b = bind(s, (struct sockaddr *) &sin6, sizeof(sin6));
        }
-
-       /* set to nonblock - we need this for some obscure situations */
-       if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
-                                "citserver: Can't set socket to non-blocking: %s",
-                                strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
-               close(s);
-               return(-1);
+       else {
+               b = bind(s, (struct sockaddr *) &sin4, sizeof(sin4));
        }
 
-       if (listen(s, actual_queue_len) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
-                                "citserver: Can't listen: %s",
-                                strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
-               close(s);
-               return(-1);
+       if (b < 0) {
+               snprintf(errormessage, SIZ,
+                        "Can't bind: %s", strerror(errno)
+               );
+               CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+               return (-1);
        }
 
-       return(s);
+       fcntl(s, F_SETFL, O_NONBLOCK);
+
+       if (listen(s, ((queue_len >= 5) ? queue_len : 5) ) < 0) {
+               snprintf(errormessage, SIZ,
+                        "Can't listen: %s", strerror(errno)
+               );
+               CtdlLogPrintf(CTDL_ALERT, "%s\n", errormessage);
+               return (-1);
+       }
+       return (s);
 }
 
 
 
+
+
 /*
  * Create a Unix domain socket and listen on it
  */
-int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
+int ctdl_uds_server(char *sockpath, int queue_len, char *errormessage)
 {
        struct sockaddr_un addr;
        int s;
@@ -386,10 +386,10 @@ int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
 
        i = unlink(sockpath);
        if ((i != 0) && (errno != ENOENT)) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, "citserver: can't unlink %s: %s",
-                       sockpath, strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               snprintf(errormessage, SIZ, "citserver: can't unlink %s: %s",
+                       sockpath, strerror(errno)
+               );
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                return(-1);
        }
 
@@ -399,40 +399,36 @@ int ig_uds_server(char *sockpath, int queue_len, char **errormessage)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
+               snprintf(errormessage, SIZ, 
                         "citserver: Can't create a socket: %s",
                         strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                return(-1);
        }
 
        if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
+               snprintf(errormessage, SIZ, 
                         "citserver: Can't bind: %s",
                         strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                return(-1);
        }
 
        /* set to nonblock - we need this for some obscure situations */
        if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
+               snprintf(errormessage, SIZ, 
                         "citserver: Can't set socket to non-blocking: %s",
                         strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                close(s);
                return(-1);
        }
 
        if (listen(s, actual_queue_len) < 0) {
-               *errormessage = (char*) malloc(SIZ + 1);
-               snprintf(*errormessage, SIZ, 
+               snprintf(errormessage, SIZ, 
                         "citserver: Can't listen: %s",
                         strerror(errno));
-               CtdlLogPrintf(CTDL_EMERG, "%s\n", *errormessage);
+               CtdlLogPrintf(CTDL_EMERG, "%s\n", errormessage);
                return(-1);
        }