]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/network/serv_network.c
Use IOBuffer with its StrBuf + const char* inside instead of having two of them
[citadel.git] / citadel / modules / network / serv_network.c
index e89f26eec7b78c03e21d0efe98a82a877a0fffd1..300b1b6bc37aaaffac17d18a94caebcc446f2951 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$ 
- *
  * This module handles shared rooms, inter-Citadel mail, and outbound
  * mailing list processing.
  *
 #  include <time.h>
 # endif
 #endif
+#ifdef HAVE_SYSCALL_H
+# include <syscall.h>
+#else 
+# if HAVE_SYS_SYSCALL_H
+#  include <sys/syscall.h>
+# endif
+#endif
 
 #include <sys/wait.h>
 #include <string.h>
@@ -405,8 +410,11 @@ void cmd_gnet(char *argbuf) {
 void cmd_snet(char *argbuf) {
        char tempfilename[PATH_MAX];
        char filename[PATH_MAX];
-       char buf[SIZ];
-       FILE *fp, *newfp;
+       int TmpFD;
+       StrBuf *Line;
+       struct stat StatBuf;
+       long len;
+       int rc;
 
        unbuffer_output();
 
@@ -415,40 +423,67 @@ void cmd_snet(char *argbuf) {
        }
        else if (CtdlAccessCheck(ac_room_aide)) return;
 
-       CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
-       assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
+       len = assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
+       memcpy(tempfilename, filename, len + 1);
 
-       fp = fopen(tempfilename, "w");
-       if (fp == NULL) {
-               cprintf("%d Cannot open %s: %s\n",
+       memset(&StatBuf, 0, sizeof(struct stat));
+       if ((stat(filename, &StatBuf)  == -1) || (StatBuf.st_size == 0))
+               StatBuf.st_size = 80; /* Not there or empty? guess 80 chars line. */
+
+       sprintf(tempfilename + len, ".%d", CC->cs_pid);
+       errno = 0;
+       TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
+
+       if ((TmpFD > 0) && (errno == 0))
+       {
+               char *tmp = malloc(StatBuf.st_size * 2);
+               memset(tmp, ' ', StatBuf.st_size * 2);
+               rc = write(TmpFD, tmp, StatBuf.st_size * 2);
+               free(tmp);
+               if ((rc <= 0) || (rc != StatBuf.st_size * 2))
+               {
+                       close(TmpFD);
+                       cprintf("%d Unable to allocate the space required for %s: %s\n",
+                               ERROR + INTERNAL_ERROR,
+                               tempfilename,
+                               strerror(errno));
+                       unlink(tempfilename);
+                       return;
+               }       
+               lseek(TmpFD, SEEK_SET, 0);
+       }
+       else {
+               cprintf("%d Unable to allocate the space required for %s: %s\n",
                        ERROR + INTERNAL_ERROR,
                        tempfilename,
                        strerror(errno));
+               unlink(tempfilename);
+               return;
        }
+       Line = NewStrBuf();
 
        cprintf("%d %s\n", SEND_LISTING, tempfilename);
-       while (client_getln(buf, sizeof buf) >= 0 && strcmp(buf, "000")) {
-               fprintf(fp, "%s\n", buf);
+
+       len = 0;
+       while (rc = CtdlClientGetLine(Line), 
+              (rc >= 0))
+       {
+               if ((rc == 3) && (strcmp(ChrPtr(Line), "000") == 0))
+                       break;
+               StrBufAppendBufPlain(Line, HKEY("\n"), 0);
+               write(TmpFD, ChrPtr(Line), StrLength(Line));
+               len += StrLength(Line);
        }
-       fclose(fp);
+       FreeStrBuf(&Line);
+       ftruncate(TmpFD, len);
+       close(TmpFD);
 
        /* Now copy the temp file to its permanent location.
         * (We copy instead of link because they may be on different filesystems)
         */
        begin_critical_section(S_NETCONFIGS);
-       fp = fopen(tempfilename, "r");
-       if (fp != NULL) {
-               newfp = fopen(filename, "w");
-               if (newfp != NULL) {
-                       while (fgets(buf, sizeof buf, fp) != NULL) {
-                               fprintf(newfp, "%s", buf);
-                       }
-                       fclose(newfp);
-               }
-               fclose(fp);
-       }
+       rename(tempfilename, filename);
        end_critical_section(S_NETCONFIGS);
-       unlink(tempfilename);
 }
 
 
@@ -647,6 +682,9 @@ void network_spool_msg(long msgnum, void *userdata) {
                 */
                msg = CtdlFetchMessage(msgnum, 1);
                if (msg != NULL) {
+                       int len, rlen;
+                       char *pCh;
+
                        if (msg->cm_fields['V'] == NULL){
                                /* local message, no enVelope */
                                StrBuf *Buf;
@@ -676,9 +714,26 @@ void network_spool_msg(long msgnum, void *userdata) {
                        if (msg->cm_fields['U'] == NULL) {
                                msg->cm_fields['U'] = strdup("(no subject)");
                        }
-                       snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
-                       free(msg->cm_fields['U']);
-                       msg->cm_fields['U'] = strdup(buf);
+                       
+                       len  = strlen(msg->cm_fields['U']);
+                       rlen = strlen(CC->room.QRname);
+                       pCh  = strstr(msg->cm_fields['U'], CC->room.QRname);
+                       if ((pCh == NULL) ||
+                           (*(pCh + rlen) != ']') ||
+                           (pCh == msg->cm_fields['U']) ||
+                           (*(pCh - 1) != '[')
+                               )
+                       {
+                               char *pBuff;
+
+                               rlen += len + 4;
+                               pBuff = malloc (rlen * sizeof(char));
+
+                               snprintf(pBuff, rlen, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
+                               free(msg->cm_fields['U']);
+                               msg->cm_fields['U'] = pBuff;
+                       }
+                       /* else we won't modify the buffer, since the roomname is already here. */
 
                        /* Set the recipient of the list message to the
                         * email address of the room itself.
@@ -1051,60 +1106,96 @@ void free_spoolcontrol_struct(SpoolControl **scc)
 
 int writenfree_spoolcontrol_file(SpoolControl **scc, char *filename)
 {
-       FILE *fp;
+       char tempfilename[PATH_MAX];
+       int TmpFD;
        SpoolControl *sc;
        namelist *nptr = NULL;
        maplist *mptr = NULL;
+       long len;
+       time_t unixtime;
+       struct timeval tv;
+       long reltid; /* if we don't have SYS_gettid, use "random" value */
+       StrBuf *Cfg;
+       int rc;
+
+       len = strlen(filename);
+       memcpy(tempfilename, filename, len + 1);
+
+
+#if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
+       reltid = syscall(SYS_gettid);
+#endif
+       gettimeofday(&tv, NULL);
+       /* Promote to time_t; types differ on some OSes (like darwin) */
+       unixtime = tv.tv_sec;
 
+       sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
        sc = *scc;
-       fp = fopen(filename, "w");
-       if (fp == NULL) {
+       errno = 0;
+       TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
+       Cfg = NewStrBuf();
+       if ((TmpFD < 0) || (errno != 0)) {
                CtdlLogPrintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
                        filename, strerror(errno));
                free_spoolcontrol_struct(scc);
+               unlink(tempfilename);
        }
        else {
-               fprintf(fp, "lastsent|%ld\n", sc->lastsent);
+               StrBufAppendPrintf(Cfg, "lastsent|%ld\n", sc->lastsent);
 
                /* Write out the listrecps while freeing from memory at the
                 * same time.  Am I clever or what?  :)
                 */
                while (sc->listrecps != NULL) {
-                       fprintf(fp, "listrecp|%s\n", sc->listrecps->name);
+                       StrBufAppendPrintf(Cfg, "listrecp|%s\n", sc->listrecps->name);
                        nptr = sc->listrecps->next;
                        free(sc->listrecps);
                        sc->listrecps = nptr;
                }
                /* Do the same for digestrecps */
                while (sc->digestrecps != NULL) {
-                       fprintf(fp, "digestrecp|%s\n", sc->digestrecps->name);
+                       StrBufAppendPrintf(Cfg, "digestrecp|%s\n", sc->digestrecps->name);
                        nptr = sc->digestrecps->next;
                        free(sc->digestrecps);
                        sc->digestrecps = nptr;
                }
                /* Do the same for participates */
                while (sc->participates != NULL) {
-                       fprintf(fp, "participate|%s\n", sc->participates->name);
+                       StrBufAppendPrintf(Cfg, "participate|%s\n", sc->participates->name);
                        nptr = sc->participates->next;
                        free(sc->participates);
                        sc->participates = nptr;
                }
                while (sc->ignet_push_shares != NULL) {
-                       fprintf(fp, "ignet_push_share|%s", sc->ignet_push_shares->remote_nodename);
+                       StrBufAppendPrintf(Cfg, "ignet_push_share|%s", sc->ignet_push_shares->remote_nodename);
                        if (!IsEmptyStr(sc->ignet_push_shares->remote_roomname)) {
-                               fprintf(fp, "|%s", sc->ignet_push_shares->remote_roomname);
+                               StrBufAppendPrintf(Cfg, "|%s", sc->ignet_push_shares->remote_roomname);
                        }
-                       fprintf(fp, "\n");
+                       StrBufAppendPrintf(Cfg, "\n");
                        mptr = sc->ignet_push_shares->next;
                        free(sc->ignet_push_shares);
                        sc->ignet_push_shares = mptr;
                }
                if (sc->misc != NULL) {
-                       fwrite(sc->misc, strlen(sc->misc), 1, fp);
+                       StrBufAppendBufPlain(Cfg, sc->misc, -1, 0);
                }
                free(sc->misc);
 
-               fclose(fp);
+               rc = write(TmpFD, ChrPtr(Cfg), StrLength(Cfg));
+               if ((rc >=0 ) && (rc == StrLength(Cfg))) 
+               {
+                       close(TmpFD);
+                       rename(tempfilename, filename);
+               }
+               else {
+                       CtdlLogPrintf(CTDL_EMERG, 
+                                     "unable to write %s; [%s]; not enough space on the disk?\n", 
+                                     tempfilename, 
+                                     strerror(errno));
+                       close(TmpFD);
+                       unlink(tempfilename);
+               }
+               FreeStrBuf(&Cfg);
                free(sc);
                *scc=NULL;
        }
@@ -1850,13 +1941,12 @@ void network_consolidate_spoolout(void) {
  * receive network spool from the remote system
  */
 void receive_spool(int *sock, char *remote_nodename) {
-       long download_len = 0L;
-       long bytes_received = 0L;
+       int download_len = 0L;
+       int bytes_received = 0L;
        char buf[SIZ];
-       static char pbuf[IGNET_PACKET_SIZE];
        char tempfilename[PATH_MAX];
        char permfilename[PATH_MAX];
-       long plen;
+       int plen;
        FILE *fp;
 
        snprintf(tempfilename, 
@@ -1883,59 +1973,66 @@ void receive_spool(int *sock, char *remote_nodename) {
        if (buf[0] != '2') {
                return;
        }
+
        download_len = extract_long(&buf[4], 0);
+       if (download_len <= 0) {
+               return;
+       }
 
-       if (download_len>0) {
-               bytes_received = 0L;
-               fp = fopen(tempfilename, "w");
-               if (fp == NULL) {
-                       CtdlLogPrintf(CTDL_CRIT, "cannot open download file locally: %s\n",
-                                     strerror(errno));
+       bytes_received = 0L;
+       fp = fopen(tempfilename, "w");
+       if (fp == NULL) {
+               CtdlLogPrintf(CTDL_CRIT, "Cannot create %s: %s\n", tempfilename, strerror(errno));
+               return;
+       }
+
+       CtdlLogPrintf(CTDL_DEBUG, "Expecting to transfer %d bytes\n", download_len);
+       while (bytes_received < download_len) {
+               /*
+                * If shutting down we can exit here and unlink the temp file.
+                * this shouldn't loose us any messages.
+                */
+               if (CtdlThreadCheckStop())
+               {
+                       fclose(fp);
+                       unlink(tempfilename);
                        return;
                }
-
-               CtdlLogPrintf(CTDL_DEBUG, "For this download we are expecting %d bytes\n", download_len);
-               while (bytes_received < download_len) {
-                       /*
-                        * If shutting down we can exit here and unlink the temp file.
-                        * this shouldn't loose us any messages.
-                        */
-                       if (CtdlThreadCheckStop())
-                       {
-                               fclose(fp);
-                               unlink(tempfilename);
-                               return;
-                       }
-                       snprintf(buf, sizeof buf, "READ %ld|%ld",
-                                bytes_received,
-                                ((download_len - bytes_received > IGNET_PACKET_SIZE)
-                                 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
-                       
-                       if (sock_puts(sock, buf) < 0) {
-                               fclose(fp);
-                               unlink(tempfilename);
-                               return;
-                       }
-                       if (sock_getln(sock, buf, sizeof buf) < 0) {
+               snprintf(buf, sizeof buf, "READ %d|%d",
+                        bytes_received,
+                        ((download_len - bytes_received > IGNET_PACKET_SIZE)
+                         ? IGNET_PACKET_SIZE : (download_len - bytes_received))
+               );
+               
+               if (sock_puts(sock, buf) < 0) {
+                       fclose(fp);
+                       unlink(tempfilename);
+                       return;
+               }
+               if (sock_getln(sock, buf, sizeof buf) < 0) {
+                       fclose(fp);
+                       unlink(tempfilename);
+                       return;
+               }
+               
+               if (buf[0] == '6') {
+                       plen = extract_int(&buf[4], 0);
+                       StrBuf *pbuf = NewStrBuf();
+                       if (socket_read_blob(sock, pbuf, plen, CLIENT_TIMEOUT) != plen) {
+                               CtdlLogPrintf(CTDL_INFO, "Short read from peer; aborting.\n");
                                fclose(fp);
                                unlink(tempfilename);
+                               FreeStrBuf(&pbuf);
                                return;
                        }
-                       
-                       if (buf[0] == '6') {
-                               plen = extract_long(&buf[4], 0);
-                               if (sock_read(sock, pbuf, plen, 1) < 0) {
-                                       fclose(fp);
-                                       unlink(tempfilename);
-                                       return;
-                               }
-                               fwrite((char *) pbuf, plen, 1, fp);
-                               bytes_received = bytes_received + plen;
-                       }
+                       fwrite(ChrPtr(pbuf), plen, 1, fp);
+                       bytes_received += plen;
+                       FreeStrBuf(&pbuf);
                }
-
-               fclose(fp);
        }
+
+       fclose(fp);
+
        /* Last chance for shutdown exit */
        if (CtdlThreadCheckStop())
        {
@@ -1955,18 +2052,18 @@ void receive_spool(int *sock, char *remote_nodename) {
                unlink(tempfilename);
                return;
        }
-       if (download_len > 0) {
-               CtdlLogPrintf(CTDL_NOTICE, "Received %ld octets from <%s>\n", download_len, remote_nodename);
-       }
+
        CtdlLogPrintf(CTDL_DEBUG, "%s\n", buf);
-       
-       /* Now move the temp file to its permanent location.
+
+       /*
+        * Now move the temp file to its permanent location.
         */
        if (link(tempfilename, permfilename) != 0) {
                CtdlLogPrintf(CTDL_ALERT, "Could not link %s to %s: %s\n",
                        tempfilename, permfilename, strerror(errno)
                );
        }
+       
        unlink(tempfilename);
 }
 
@@ -2075,7 +2172,7 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
        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");
+       sock = sock_connect(host, port);
        if (sock < 0) {
                CtdlLogPrintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
                network_talking_to(node, NTT_REMOVE);
@@ -2083,9 +2180,9 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
        }
        
        CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
-       CCC->sReadBuf = NewStrBuf();
+       CCC->SBuf.Buf = NewStrBuf();
        CCC->sMigrateBuf = NewStrBuf();
-       CCC->sPos = NULL;
+       CCC->SBuf.ReadWritePointer = NULL;
 
        /* Read the server greeting */
        if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
@@ -2122,7 +2219,7 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
 
        sock_puts(&sock, "QUIT");
 bail:  
-       FreeStrBuf(&CCC->sReadBuf);
+       FreeStrBuf(&CCC->SBuf.Buf);
        FreeStrBuf(&CCC->sMigrateBuf);
        if (sock != -1)
                sock_close(sock);
@@ -2216,15 +2313,10 @@ void create_spool_dirs(void) {
  * 
  * Run through the rooms doing various types of network stuff.
  */
-void *network_do_queue(void *args) {
+void network_do_queue(void) {
        static time_t last_run = 0L;
        struct RoomProcList *ptr;
        int full_processing = 1;
-       struct CitContext networkerCC;
-
-       /* Give the networker its own private CitContext */
-       CtdlFillSystemContext(&networkerCC, "network");
-       citthread_setspecific(MyConKey, (void *)&networkerCC );
 
        /*
         * Run the full set of processing tasks no more frequently
@@ -2244,8 +2336,7 @@ void *network_do_queue(void *args) {
         * with a static variable instead.
         */
        if (doing_queue) {
-               CtdlClearSystemContext();
-               return NULL;
+               return;
        }
        doing_queue = 1;
 
@@ -2320,20 +2411,6 @@ void *network_do_queue(void *args) {
        }
 
        doing_queue = 0;
-
-       /* 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");
-       }
-       CtdlClearSystemContext();
-       return NULL;
 }
 
 
@@ -2402,6 +2479,24 @@ int network_room_handler (struct ctdlroom *room)
        return 0;
 }
 
+void *ignet_thread(void *arg) {
+       struct CitContext ignet_thread_CC;
+
+       CtdlLogPrintf(CTDL_DEBUG, "ignet_thread() initializing\n");
+       CtdlFillSystemContext(&ignet_thread_CC, "IGnet Queue");
+       citthread_setspecific(MyConKey, (void *)&ignet_thread_CC);
+
+       while (!CtdlThreadCheckStop()) {
+               network_do_queue();
+               CtdlThreadSleep(60);
+       }
+
+       CtdlClearSystemContext();
+       return(NULL);
+}
+
+
+
 
 /*
  * Module entry point
@@ -2415,11 +2510,9 @@ CTDL_MODULE_INIT(network)
                CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
                CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
                CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
-               CtdlRegisterRoomHook(network_room_handler);
+               CtdlRegisterRoomHook(network_room_handler);
                CtdlRegisterCleanupHook(destroy_network_queue_room);
+               CtdlThreadCreate("SMTP Send", CTDLTHREAD_BIGSTACK, ignet_thread, NULL);
        }
-       else
-               CtdlThreadSchedule("IGnet Network", CTDLTHREAD_BIGSTACK, network_do_queue, NULL, 0);
-       /* return our Subversion id for the Log */
-       return "$Id$";
+       return "network";
 }