]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/network/serv_network.c
* Documented all of the 'TODO: document me.' commands in file_ops.c
[citadel.git] / citadel / modules / network / serv_network.c
index e2d16dd9aa08e521799e10ba0cbfa84cdbf15c23..fa50506609dfe7839fdf83cb707ae5ed89c81c62 100644 (file)
@@ -68,7 +68,6 @@
 #include "support.h"
 #include "config.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
@@ -716,20 +715,17 @@ void network_spool_msg(long msgnum, void *userdata) {
                                fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
                        }
 
-                       CC->redirect_buffer = malloc(SIZ);
-                       CC->redirect_len = 0;
-                       CC->redirect_alloc = SIZ;
-
+                       CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
+                       
                        safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
                        CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
 
-                       striplt(CC->redirect_buffer);
-                       fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
+                       StrBufTrim(CC->redirect_buffer);
+                       fwrite(HKEY("\n"), 1, sc->digestfp);
+                       fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
+                       fwrite(HKEY("\n"), 1, sc->digestfp);
 
-                       free(CC->redirect_buffer);
-                       CC->redirect_buffer = NULL;
-                       CC->redirect_len = 0;
-                       CC->redirect_alloc = 0;
+                       FreeStrBuf(&CC->redirect_buffer);
 
                        sc->num_msgs_spooled += 1;
                        free(msg);
@@ -939,31 +935,31 @@ int read_spoolcontrol_file(SpoolControl **scc, char *filename)
                buf[strlen(buf)-1] = 0;
 
                extract_token(instr, buf, 0, '|', sizeof instr);
-               if (!strcasecmp(instr, "lastsent")) {
+               if (!strcasecmp(instr, strof(lastsent))) {
                        sc->lastsent = extract_long(buf, 1);
                }
-               else if (!strcasecmp(instr, "listrecp")) {
+               else if (!strcasecmp(instr, strof(listrecp))) {
                        nptr = (namelist *)
                                malloc(sizeof(namelist));
                        nptr->next = sc->listrecps;
                        extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
                        sc->listrecps = nptr;
                }
-               else if (!strcasecmp(instr, "participate")) {
+               else if (!strcasecmp(instr, strof(participate))) {
                        nptr = (namelist *)
                                malloc(sizeof(namelist));
                        nptr->next = sc->participates;
                        extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
                        sc->participates = nptr;
                }
-               else if (!strcasecmp(instr, "digestrecp")) {
+               else if (!strcasecmp(instr, strof(digestrecp))) {
                        nptr = (namelist *)
                                malloc(sizeof(namelist));
                        nptr->next = sc->digestrecps;
                        extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
                        sc->digestrecps = nptr;
                }
-               else if (!strcasecmp(instr, "ignet_push_share")) {
+               else if (!strcasecmp(instr, strof(ignet_push_share))) {
                        extract_token(nodename, buf, 1, '|', sizeof nodename);
                        extract_token(roomname, buf, 2, '|', sizeof roomname);
                        mptr = (maplist *) malloc(sizeof(maplist));
@@ -978,12 +974,12 @@ int read_spoolcontrol_file(SpoolControl **scc, char *filename)
                         * timestamps.
                         */
                        skipthisline = 0;
-                       if (!strncasecmp(buf, "subpending|", 11)) {
+                       if (!strncasecmp(buf, strof(subpending)"|", 11)) {
                                if (time(NULL) - extract_long(buf, 4) > EXP) {
                                        skipthisline = 1;
                                }
                        }
-                       if (!strncasecmp(buf, "unsubpending|", 13)) {
+                       if (!strncasecmp(buf, strof(unsubpending)"|", 13)) {
                                if (time(NULL) - extract_long(buf, 3) > EXP) {
                                        skipthisline = 1;
                                }
@@ -1799,7 +1795,7 @@ void receive_spool(int *sock, char *remote_nodename) {
        FILE *fp, *newfp;
 
        CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
-       if (sock_puts(*sock, "NDOP") < 0) return;
+       if (sock_puts(sock, "NDOP") < 0) return;
        if (sock_getln(sock, buf, sizeof buf) < 0) return;
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
        if (buf[0] != '2') {
@@ -1830,7 +1826,7 @@ void receive_spool(int *sock, char *remote_nodename) {
                        bytes_received,
                     ((download_len - bytes_received > IGNET_PACKET_SIZE)
                 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
-               if (sock_puts(*sock, buf) < 0) {
+               if (sock_puts(sock, buf) < 0) {
                        fclose(fp);
                        unlink(tempfilename);
                        return;
@@ -1859,7 +1855,7 @@ void receive_spool(int *sock, char *remote_nodename) {
                unlink(tempfilename);
                return;
        }
-       if (sock_puts(*sock, "CLOS") < 0) {
+       if (sock_puts(sock, "CLOS") < 0) {
                unlink(tempfilename);
                return;
        }
@@ -1922,7 +1918,7 @@ void transmit_spool(int *sock, char *remote_nodename)
        int fd;
        char sfname[128];
 
-       if (sock_puts(*sock, "NUOP") < 0) return;
+       if (sock_puts(sock, "NUOP") < 0) return;
        if (sock_getln(sock, buf, sizeof buf) < 0) return;
        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
        if (buf[0] != '2') {
@@ -1953,7 +1949,7 @@ void transmit_spool(int *sock, char *remote_nodename)
                        }
                        
                        snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
-                       if (sock_puts(*sock, buf) < 0) {
+                       if (sock_puts(sock, buf) < 0) {
                                close(fd);
                                return;
                        }
@@ -1963,7 +1959,7 @@ void transmit_spool(int *sock, char *remote_nodename)
                        }
                        thisblock = atol(&buf[4]);
                        if (buf[0] == '7') {
-                               if (sock_write(*sock, pbuf,
+                               if (sock_write(sock, pbuf,
                                   (int) thisblock) < 0) {
                                        close(fd);
                                        return;
@@ -1982,7 +1978,7 @@ ABORTUPL:
        if(CtdlThreadCheckStop())
                return;
                
-       if (sock_puts(*sock, "UCLS 1") < 0) return;
+       if (sock_puts(sock, "UCLS 1") < 0) return;
        /**
         * From here on we must complete or messages will get lost
         */
@@ -2006,9 +2002,11 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
        char buf[SIZ];
        char err_buf[SIZ];
        char connected_to[SIZ];
+       CitContext *CCC=CC;
 
        if (network_talking_to(node, NTT_CHECK)) return;
        network_talking_to(node, NTT_ADD);
+       CtdlLogPrintf(CTDL_DEBUG, "network: polling <%s>\n", node);
        CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
 
        sock = sock_connect(host, port, "tcp");
@@ -2019,6 +2017,9 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
        }
        
        CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
+       CCC->sReadBuf = NewStrBuf();
+       CCC->sMigrateBuf = NewStrBuf();
+       CCC->sPos = NULL;
 
        /* Read the server greeting */
        if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
@@ -2028,7 +2029,10 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
        extract_token (connected_to, buf, 1, ' ', sizeof connected_to);
        if (strcmp(connected_to, node))
        {
-               snprintf (err_buf, sizeof(err_buf), "Connected to node \"%s\" but I was expecting to connect to node \"%s\".", connected_to, node);
+               snprintf(err_buf, sizeof(err_buf),
+                       "Connected to node \"%s\" but I was expecting to connect to node \"%s\".",
+                       connected_to, node
+               );
                CtdlLogPrintf(CTDL_ERR, "%s\n", err_buf);
                CtdlAideMessage(err_buf, "Network error");
        }
@@ -2036,10 +2040,12 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
                /* We're talking to the correct node.  Now identify ourselves. */
                snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
                CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-               if (sock_puts(sock, buf) <0) goto bail;
+               if (sock_puts(&sock, buf) <0) goto bail;
                if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
                CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
-               if (buf[0] != '2') goto bail;
+               if (buf[0] != '2') {
+                       goto bail;
+               }
        
                /* At this point we are authenticated. */
                if (!CtdlThreadCheckStop())
@@ -2048,8 +2054,12 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
                        transmit_spool(&sock, node);
        }
 
-       sock_puts(sock, "QUIT");
-bail:  sock_close(sock);
+       sock_puts(&sock, "QUIT");
+bail:  
+       FreeStrBuf(&CCC->sReadBuf);
+       FreeStrBuf(&CCC->sMigrateBuf);
+       if (sock != -1)
+               sock_close(sock);
        network_talking_to(node, NTT_REMOVE);
 }
 
@@ -2071,7 +2081,7 @@ void network_poll_other_citadel_nodes(int full_poll) {
        char spoolfile[256];
 
        if (working_ignetcfg == NULL) {
-               CtdlLogPrintf(CTDL_DEBUG, "No nodes defined - not polling\n");
+               CtdlLogPrintf(CTDL_DEBUG, "network: no neighbor nodes are configured - not polling.\n");
                return;
        }
 
@@ -2089,10 +2099,11 @@ void network_poll_other_citadel_nodes(int full_poll) {
                        poll = full_poll;
                        if (poll == 0) {
                                snprintf(spoolfile, 
-                                                sizeof spoolfile,
-                                                "%s/%s",
-                                                ctdl_netout_dir, 
-                                                node);
+                                        sizeof spoolfile,
+                                        "%s/%s",
+                                        ctdl_netout_dir, 
+                                        node
+                               );
                                if (access(spoolfile, R_OK) == 0) {
                                        poll = 1;
                                }
@@ -2120,6 +2131,10 @@ void create_spool_dirs(void) {
                CtdlLogPrintf(CTDL_EMERG, "unable to create directory [%s]: %s", ctdl_netin_dir, strerror(errno));
        if (chown(ctdl_netin_dir, CTDLUID, (-1)) != 0)
                CtdlLogPrintf(CTDL_EMERG, "unable to set the access rights for [%s]: %s", ctdl_netin_dir, strerror(errno));
+       if ((mkdir(ctdl_nettmp_dir, 0700) != 0) && (errno != EEXIST))
+               CtdlLogPrintf(CTDL_EMERG, "unable to create directory [%s]: %s", ctdl_nettmp_dir, strerror(errno));
+       if (chown(ctdl_nettmp_dir, CTDLUID, (-1)) != 0)
+               CtdlLogPrintf(CTDL_EMERG, "unable to set the access rights for [%s]: %s", ctdl_nettmp_dir, strerror(errno));
        if ((mkdir(ctdl_netout_dir, 0700) != 0) && (errno != EEXIST))
                CtdlLogPrintf(CTDL_EMERG, "unable to create directory [%s]: %s", ctdl_netout_dir, strerror(errno));
        if (chown(ctdl_netout_dir, CTDLUID, (-1)) != 0)
@@ -2151,7 +2166,9 @@ void *network_do_queue(void *args) {
         */
        if ( (time(NULL) - last_run) < config.c_net_freq ) {
                full_processing = 0;
-               CtdlLogPrintf(CTDL_DEBUG, "Network full processing in %ld seconds.\n", config.c_net_freq - (time(NULL)- last_run));
+               CtdlLogPrintf(CTDL_DEBUG, "Network full processing in %ld seconds.\n",
+                       config.c_net_freq - (time(NULL)- last_run)
+               );
        }
 
        /*
@@ -2160,7 +2177,9 @@ void *network_do_queue(void *args) {
         * don't really require extremely fine granularity here, we'll do it
         * with a static variable instead.
         */
-       if (doing_queue) return NULL;
+       if (doing_queue) {
+               return NULL;
+       }
        doing_queue = 1;
 
        /* Load the IGnet Configuration into memory */
@@ -2214,8 +2233,9 @@ void *network_do_queue(void *args) {
        }
 
        /* If there is anything in the inbound queue, process it */
-       if (!CtdlThreadCheckStop())
+       if (!CtdlThreadCheckStop()) {
                network_do_spoolin();
+       }
 
        /* Save the network map back to disk */
        write_network_map();
@@ -2233,10 +2253,18 @@ void *network_do_queue(void *args) {
        }
 
        doing_queue = 0;
-       if (!CtdlThreadCheckStop()) // Only reschedule if system is not stopping
-               CtdlThreadSchedule("IGnet Network", CTDLTHREAD_BIGSTACK, network_do_queue, NULL, time(NULL) + 60);
-       else
+
+       /* Reschedule this task to happen again periodically, unless the thread system indicates
+        * that the server is shutting down.
+        */
+       if (!CtdlThreadCheckStop()) {
+               CtdlThreadSchedule("IGnet Network", CTDLTHREAD_BIGSTACK,
+                       network_do_queue, NULL, time(NULL) + 60
+               );
+       }
+       else {
                CtdlLogPrintf(CTDL_DEBUG, "network: Task STOPPED.\n");
+       }
        return NULL;
 }