4 * This module handles shared rooms, inter-Citadel mail, and outbound
5 * mailing list processing.
7 * Copyright (C) 2000-2001 by Art Cancro and others.
8 * This code is released under the terms of the GNU General Public License.
14 * Don't allow polls during network processing
15 * Use table isn't saving properly across sessions
26 #include <sys/types.h>
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
33 # include <sys/time.h>
44 #include "sysdep_decls.h"
45 #include "citserver.h"
48 #include "dynloader.h"
55 #include "internet_addressing.h"
56 #include "serv_network.h"
57 #include "clientsocket.h"
62 * When we do network processing, it's accomplished in two passes; one to
63 * gather a list of rooms and one to actually do them. It's ok that rplist
64 * is global; this process *only* runs as part of the housekeeping loop and
65 * therefore only one will run at a time.
67 struct RoomProcList *rplist = NULL;
70 * We build a map of network nodes during processing.
72 struct NetMap *the_netmap = NULL;
76 * Manage the use table. This is a list of messages which have recently
77 * arrived on the system. It is maintained and queried to prevent the same
78 * message from being entered into the database multiple times if it happens
79 * to arrive multiple times by accident.
81 int network_usetable(int operation, struct CtdlMessage *msg) {
83 static struct UseTable *ut = NULL;
84 struct UseTable *uptr = NULL;
85 char *serialized_table = NULL;
94 serialized_table = CtdlGetSysConfig(USETABLE);
95 if (serialized_table == NULL) return(0);
97 for (i=0; i<num_tokens(serialized_table, '\n'); ++i) {
98 extract_token(buf, serialized_table, i, '\n');
99 uptr = (struct UseTable *)
100 mallok(sizeof(struct UseTable));
103 extract(msgid, buf, 0);
104 uptr->message_id = strdoop(msgid);
105 uptr->timestamp = extract_long(buf, 1);
110 phree(serialized_table);
111 serialized_table = NULL;
115 /* Bail out if we can't generate a message ID */
119 if (msg->cm_fields['I'] == NULL) {
122 if (strlen(msg->cm_fields['I']) == 0) {
126 /* Generate the message ID */
127 strcpy(msgid, msg->cm_fields['I']);
128 if (haschar(msgid, '@') == 0) {
130 if (msg->cm_fields['N'] != NULL) {
131 strcat(msgid, msg->cm_fields['N']);
138 /* Compare to the existing list */
139 for (uptr = ut; uptr != NULL; uptr = uptr->next) {
140 if (!strcasecmp(msgid, uptr->message_id)) {
145 /* If we got to this point, it's unique: add it. */
146 uptr = (struct UseTable *)
147 mallok(sizeof(struct UseTable));
148 if (uptr == NULL) return(0);
150 uptr->message_id = strdoop(msgid);
151 uptr->timestamp = time(NULL);
156 /* Figure out how big the serialized buffer should be */
158 for (uptr = ut; uptr != NULL; uptr = uptr->next) {
159 stlen = stlen + strlen(uptr->message_id) + 20;
161 serialized_table = mallok(stlen);
164 if (serialized_table != NULL) {
165 sprintf(&serialized_table[strlen(
166 serialized_table)], "%s|%ld\n",
171 /* Now free the memory */
174 phree(uptr->message_id);
179 CtdlPutSysConfig(USETABLE, serialized_table);
180 phree(serialized_table);
185 /* should never get here unless illegal operation specified */
191 * Read the network map from its configuration file into memory.
193 void read_network_map(void) {
194 char *serialized_map = NULL;
197 struct NetMap *nmptr;
199 serialized_map = CtdlGetSysConfig(IGNETMAP);
200 if (serialized_map == NULL) return; /* if null, no entries */
202 /* Use the string tokenizer to grab one line at a time */
203 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
204 extract_token(buf, serialized_map, i, '\n');
205 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
206 extract(nmptr->nodename, buf, 0);
207 nmptr->lastcontact = extract_long(buf, 1);
208 extract(nmptr->nexthop, buf, 2);
209 nmptr->next = the_netmap;
213 phree(serialized_map);
218 * Write the network map from memory back to the configuration file.
220 void write_network_map(void) {
221 char *serialized_map = NULL;
222 struct NetMap *nmptr;
224 serialized_map = strdoop("");
226 if (the_netmap != NULL) {
227 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
228 serialized_map = reallok(serialized_map,
229 (strlen(serialized_map)+SIZ) );
230 if (strlen(nmptr->nodename) > 0) {
231 sprintf(&serialized_map[strlen(serialized_map)],
240 CtdlPutSysConfig(IGNETMAP, serialized_map);
241 phree(serialized_map);
243 /* Now free the list */
244 while (the_netmap != NULL) {
245 nmptr = the_netmap->next;
254 * Check the network map and determine whether the supplied node name is
255 * valid. If it is not a neighbor node, supply the name of a neighbor node
256 * which is the next hop. If it *is* a neighbor node, we also fill in the
259 int is_valid_node(char *nexthop, char *secret, char *node) {
260 char *ignetcfg = NULL;
265 struct NetMap *nmptr;
272 * First try the neighbor nodes
274 ignetcfg = CtdlGetSysConfig(IGNETCFG);
275 if (ignetcfg == NULL) {
276 if (nexthop != NULL) {
283 if (nexthop != NULL) {
287 /* Use the string tokenizer to grab one line at a time */
288 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
289 extract_token(linebuf, ignetcfg, i, '\n');
290 extract(buf, linebuf, 0);
291 if (!strcasecmp(buf, node)) {
292 if (nexthop != NULL) {
295 if (secret != NULL) {
296 extract(secret, linebuf, 1);
304 return(retval); /* yup, it's a direct neighbor */
308 * If we get to this point we have to see if we know the next hop
310 if (the_netmap != NULL) {
311 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
312 if (!strcasecmp(nmptr->nodename, node)) {
313 if (nexthop != NULL) {
314 strcpy(nexthop, nmptr->nexthop);
322 * If we get to this point, the supplied node name is bogus.
324 lprintf(5, "Invalid node name <%s>\n", node);
332 void cmd_gnet(char *argbuf) {
337 if (CtdlAccessCheck(ac_room_aide)) return;
338 assoc_file_name(filename, &CC->quickroom, "netconfigs");
339 cprintf("%d Network settings for room #%ld <%s>\n",
341 CC->quickroom.QRnumber, CC->quickroom.QRname);
343 fp = fopen(filename, "r");
345 while (fgets(buf, sizeof buf, fp) != NULL) {
346 buf[strlen(buf)-1] = 0;
347 cprintf("%s\n", buf);
356 void cmd_snet(char *argbuf) {
357 char tempfilename[SIZ];
362 if (CtdlAccessCheck(ac_room_aide)) return;
363 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
364 assoc_file_name(filename, &CC->quickroom, "netconfigs");
366 fp = fopen(tempfilename, "w");
368 cprintf("%d Cannot open %s: %s\n",
369 ERROR+INTERNAL_ERROR,
374 cprintf("%d %s\n", SEND_LISTING, tempfilename);
375 while (client_gets(buf), strcmp(buf, "000")) {
376 fprintf(fp, "%s\n", buf);
380 /* Now copy the temp file to its permanent location
381 * (We use /bin/mv instead of link() because they may be on
382 * different filesystems)
385 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
392 * Spools out one message from the list.
394 void network_spool_msg(long msgnum, void *userdata) {
395 struct SpoolControl *sc;
396 struct namelist *nptr;
400 char *newpath = NULL;
401 size_t instr_len = SIZ;
402 struct CtdlMessage *msg = NULL;
403 struct CtdlMessage *imsg;
404 struct ser_ret sermsg;
410 int delete_after_send = 0; /* Set to 1 to delete after spooling */
412 sc = (struct SpoolControl *)userdata;
415 * Process mailing list recipients
417 if (sc->listrecps != NULL) {
419 /* First, copy it to the spoolout room */
420 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
421 if (err != 0) return;
424 * Figure out how big a buffer we need to allocate
426 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
427 instr_len = instr_len + strlen(nptr->name);
433 lprintf(9, "Generating delivery instructions\n");
434 instr = mallok(instr_len);
436 lprintf(1, "Cannot allocate %d bytes for instr...\n",
441 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
442 "bounceto|postmaster@%s\n" ,
443 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
445 /* Generate delivery instructions for each recipient */
446 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
447 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
452 * Generate a message from the instructions
454 imsg = mallok(sizeof(struct CtdlMessage));
455 memset(imsg, 0, sizeof(struct CtdlMessage));
456 imsg->cm_magic = CTDLMESSAGE_MAGIC;
457 imsg->cm_anon_type = MES_NORMAL;
458 imsg->cm_format_type = FMT_RFC822;
459 imsg->cm_fields['A'] = strdoop("Citadel");
460 imsg->cm_fields['M'] = instr;
462 /* Save delivery instructions in spoolout room */
463 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
464 CtdlFreeMessage(imsg);
468 * Process IGnet push shares
470 if (sc->ignet_push_shares != NULL) {
472 msg = CtdlFetchMessage(msgnum);
475 /* Prepend our node name to the Path field whenever
476 * sending a message to another IGnet node
478 if (msg->cm_fields['P'] == NULL) {
479 msg->cm_fields['P'] = strdoop("username");
481 newpath = mallok(strlen(msg->cm_fields['P']) +
482 strlen(config.c_nodename) + 2);
483 sprintf(newpath, "%s!%s", config.c_nodename,
484 msg->cm_fields['P']);
485 phree(msg->cm_fields['P']);
486 msg->cm_fields['P'] = newpath;
489 * Force the message to appear in the correct room
490 * on the far end by setting the C field correctly
492 if (msg->cm_fields['C'] != NULL) {
493 phree(msg->cm_fields['C']);
495 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
498 * Determine if this message is set to be deleted
499 * after sending out on the network
501 if (msg->cm_fields['S'] != NULL) {
502 if (!strcasecmp(msg->cm_fields['S'],
504 delete_after_send = 1;
509 * Now serialize it for transmission
511 serialize_message(&sermsg, msg);
513 /* Now send it to every node */
514 for (nptr = sc->ignet_push_shares; nptr != NULL;
519 /* Check for valid node name */
520 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
521 lprintf(3, "Invalid node <%s>\n",
526 /* Check for split horizon */
527 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
528 bang = num_tokens(msg->cm_fields['P'], '!');
529 if (bang > 1) for (i=0; i<(bang-1); ++i) {
530 extract_token(buf, msg->cm_fields['P'],
532 if (!strcasecmp(buf, nptr->name)) {
537 /* Send the message */
540 "./network/spoolout/%s",
542 fp = fopen(filename, "ab");
551 CtdlFreeMessage(msg);
555 /* update lastsent */
556 sc->lastsent = msgnum;
558 /* Delete this message if delete-after-send is set */
559 if (delete_after_send) {
560 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
569 * Batch up and send all outbound traffic from the current room
571 void network_spoolout_room(char *room_to_spool) {
576 struct SpoolControl sc;
577 /* struct namelist *digestrecps = NULL; */
578 struct namelist *nptr;
580 lprintf(7, "Spooling <%s>\n", room_to_spool);
581 if (getroom(&CC->quickroom, room_to_spool) != 0) {
582 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
586 memset(&sc, 0, sizeof(struct SpoolControl));
587 assoc_file_name(filename, &CC->quickroom, "netconfigs");
589 fp = fopen(filename, "r");
591 lprintf(7, "Outbound batch processing skipped for <%s>\n",
592 CC->quickroom.QRname);
596 lprintf(5, "Outbound batch processing started for <%s>\n",
597 CC->quickroom.QRname);
599 while (fgets(buf, sizeof buf, fp) != NULL) {
600 buf[strlen(buf)-1] = 0;
602 extract(instr, buf, 0);
603 if (!strcasecmp(instr, "lastsent")) {
604 sc.lastsent = extract_long(buf, 1);
606 else if (!strcasecmp(instr, "listrecp")) {
607 nptr = (struct namelist *)
608 mallok(sizeof(struct namelist));
609 nptr->next = sc.listrecps;
610 extract(nptr->name, buf, 1);
613 else if (!strcasecmp(instr, "ignet_push_share")) {
614 nptr = (struct namelist *)
615 mallok(sizeof(struct namelist));
616 nptr->next = sc.ignet_push_shares;
617 extract(nptr->name, buf, 1);
618 sc.ignet_push_shares = nptr;
626 /* Do something useful */
627 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
628 network_spool_msg, &sc);
631 /* Now rewrite the config file */
632 fp = fopen(filename, "w");
634 lprintf(1, "ERROR: cannot open %s: %s\n",
635 filename, strerror(errno));
638 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
640 /* Write out the listrecps while freeing from memory at the
641 * same time. Am I clever or what? :)
643 while (sc.listrecps != NULL) {
644 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
645 nptr = sc.listrecps->next;
649 while (sc.ignet_push_shares != NULL) {
650 fprintf(fp, "ignet_push_share|%s\n",
651 sc.ignet_push_shares->name);
652 nptr = sc.ignet_push_shares->next;
653 phree(sc.ignet_push_shares);
654 sc.ignet_push_shares = nptr;
660 lprintf(5, "Outbound batch processing finished for <%s>\n",
661 CC->quickroom.QRname);
666 * Batch up and send all outbound traffic from the current room
668 void network_queue_room(struct quickroom *qrbuf, void *data) {
669 struct RoomProcList *ptr;
671 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
672 if (ptr == NULL) return;
674 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
681 * Learn topology from path fields
683 void network_learn_topology(char *node, char *path) {
685 struct NetMap *nmptr;
689 if (num_tokens(path, '!') < 3) return;
690 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
691 if (!strcasecmp(nmptr->nodename, node)) {
692 extract_token(nmptr->nexthop, path, 0, '!');
693 nmptr->lastcontact = time(NULL);
698 /* If we got here then it's not in the map, so add it. */
699 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
700 strcpy(nmptr->nodename, node);
701 nmptr->lastcontact = time(NULL);
702 extract_token(nmptr->nexthop, path, 0, '!');
703 nmptr->next = the_netmap;
711 * Bounce a message back to the sender
713 void network_bounce(struct CtdlMessage *msg, char *reason) {
714 static int serialnum = 0;
717 struct ser_ret sermsg;
718 char *oldpath = NULL;
721 lprintf(9, "entering network_bounce()\n");
723 if (msg == NULL) return;
726 * Give it a fresh message ID
728 if (msg->cm_fields['I'] != NULL) {
729 phree(msg->cm_fields['I']);
731 sprintf(buf, "%ld.%04x.%04x@%s",
732 time(NULL), getpid(), ++serialnum, config.c_fqdn);
733 msg->cm_fields['I'] = strdoop(buf);
736 * FIXME ... right now we're just sending a bounce; we really want to
737 * include the text of the bounced message.
739 if (msg->cm_fields['M'] != NULL) {
740 phree(msg->cm_fields['M']);
742 msg->cm_fields['M'] = strdoop(reason);
743 msg->cm_format_type = 0;
746 * Turn the message around
748 if (msg->cm_fields['R'] == NULL) {
749 phree(msg->cm_fields['R']);
752 if (msg->cm_fields['D'] == NULL) {
753 phree(msg->cm_fields['D']);
756 msg->cm_fields['R'] = msg->cm_fields['A'];
757 msg->cm_fields['D'] = msg->cm_fields['N'];
758 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
759 msg->cm_fields['N'] = strdoop(config.c_nodename);
761 if (!strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
762 phree(msg->cm_fields['D']);
766 * If this is a bounce of a bounce, send it to the Aide> room
767 * instead of looping around forever
769 if (msg->cm_fields['D'] == NULL) if (msg->cm_fields['R'] != NULL)
770 if (!strcasecmp(msg->cm_fields['R'], BOUNCESOURCE)) {
771 phree(msg->cm_fields['R']);
772 if (msg->cm_fields['C'] != NULL) {
773 phree(msg->cm_fields['C']);
775 msg->cm_fields['C'] = strdoop(AIDEROOM);
778 /* prepend our node to the path */
779 if (msg->cm_fields['P'] != NULL) {
780 oldpath = msg->cm_fields['P'];
781 msg->cm_fields['P'] = NULL;
784 oldpath = strdoop("unknown_user");
786 msg->cm_fields['P'] = mallok(strlen(oldpath) + SIZ);
787 sprintf(msg->cm_fields['P'], "%s!%s", config.c_nodename, oldpath);
790 /* serialize the message */
791 serialize_message(&sermsg, msg);
794 sprintf(filename, "./network/spoolin/bounce.%04x.%04x",
795 getpid(), serialnum);
797 fp = fopen(filename, "ab");
804 CtdlFreeMessage(msg);
805 lprintf(9, "leaving network_bounce()\n");
812 * Process a buffer containing a single message from a single file
813 * from the inbound queue
815 void network_process_buffer(char *buffer, long size) {
816 struct CtdlMessage *msg;
821 struct usersupp tempUS;
823 char target_room[ROOMNAMELEN];
824 struct ser_ret sermsg;
825 char *oldpath = NULL;
829 /* Set default target room to trash */
830 strcpy(target_room, TWITROOM);
832 /* Load the message into memory */
833 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
834 memset(msg, 0, sizeof(struct CtdlMessage));
835 msg->cm_magic = CTDLMESSAGE_MAGIC;
836 msg->cm_anon_type = buffer[1];
837 msg->cm_format_type = buffer[2];
839 for (pos = 3; pos < size; ++pos) {
841 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
842 pos = pos + strlen(&buffer[(int)pos]);
845 /* Check for message routing */
846 if (msg->cm_fields['D'] != NULL) {
847 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
849 /* route the message */
850 if (is_valid_node(NULL, NULL,
851 msg->cm_fields['D']) == 0) {
853 /* prepend our node to the path */
854 if (msg->cm_fields['P'] != NULL) {
855 oldpath = msg->cm_fields['P'];
856 msg->cm_fields['P'] = NULL;
859 oldpath = strdoop("unknown_user");
861 msg->cm_fields['P'] =
862 mallok(strlen(oldpath) + SIZ);
863 sprintf(msg->cm_fields['P'], "%s!%s",
864 config.c_nodename, oldpath);
867 /* serialize the message */
868 serialize_message(&sermsg, msg);
872 "./network/spoolout/%s",
873 msg->cm_fields['D']);
874 fp = fopen(filename, "ab");
881 CtdlFreeMessage(msg);
885 else { /* invalid destination node name */
888 "A message you sent could not be delivered due to an invalid destination node"
889 " name. Please check the address and try sending the message again.\n");
898 * Check to see if we already have a copy of this message
900 if (network_usetable(UT_INSERT, msg) != 0) {
901 /* FIXME - post a msg in Aide> telling us what happened */
902 CtdlFreeMessage(msg);
907 /* Learn network topology from the path */
908 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
909 network_learn_topology(msg->cm_fields['N'],
910 msg->cm_fields['P']);
913 /* Does it have a recipient? If so, validate it... */
914 if (msg->cm_fields['R'] != NULL) {
916 safestrncpy(recp, msg->cm_fields['R'], sizeof(recp));
918 e = alias(recp); /* alias and mail type */
919 if ((recp[0] == 0) || (e == MES_ERROR)) {
922 "A message you sent could not be delivered due to an invalid address.\n"
923 "Please check the address and try sending the message again.\n");
928 else if (e == MES_LOCAL) {
929 a = getuser(&tempUS, recp);
933 "A message you sent could not be delivered because the user does not exist\n"
934 "on this system. Please check the address and try again.\n");
940 MailboxName(target_room, &tempUS, MAILROOM);
945 else if (msg->cm_fields['C'] != NULL) {
946 safestrncpy(target_room,
951 else if (msg->cm_fields['O'] != NULL) {
952 safestrncpy(target_room,
957 /* save the message into a room */
958 msg->cm_flags = CM_SKIP_HOOKS;
959 CtdlSaveMsg(msg, "", target_room, 0);
960 CtdlFreeMessage(msg);
965 * Process a single message from a single file from the inbound queue
967 void network_process_message(FILE *fp, long msgstart, long msgend) {
972 hold_pos = ftell(fp);
973 size = msgend - msgstart + 1;
974 buffer = mallok(size);
975 if (buffer != NULL) {
976 fseek(fp, msgstart, SEEK_SET);
977 fread(buffer, size, 1, fp);
978 network_process_buffer(buffer, size);
982 fseek(fp, hold_pos, SEEK_SET);
987 * Process a single file from the inbound queue
989 void network_process_file(char *filename) {
991 long msgstart = (-1L);
996 lprintf(7, "network: processing <%s>\n", filename);
998 fp = fopen(filename, "rb");
1000 lprintf(5, "Error opening %s: %s\n",
1001 filename, strerror(errno));
1005 /* Look for messages in the data stream and break them out */
1006 while (ch = getc(fp), ch >= 0) {
1009 if (msgstart >= 0L) {
1010 msgend = msgcur - 1;
1011 network_process_message(fp, msgstart, msgend);
1019 msgend = msgcur - 1;
1020 if (msgstart >= 0L) {
1021 network_process_message(fp, msgstart, msgend);
1030 * Process anything in the inbound queue
1032 void network_do_spoolin(void) {
1037 dp = opendir("./network/spoolin");
1038 if (dp == NULL) return;
1040 while (d = readdir(dp), d != NULL) {
1041 sprintf(filename, "./network/spoolin/%s", d->d_name);
1042 network_process_file(filename);
1054 * receive network spool from the remote system
1056 void receive_spool(int sock, char *remote_nodename) {
1058 long bytes_received;
1060 static char pbuf[IGNET_PACKET_SIZE];
1061 char tempfilename[PATH_MAX];
1065 strcpy(tempfilename, tmpnam(NULL));
1066 if (sock_puts(sock, "NDOP") < 0) return;
1067 if (sock_gets(sock, buf) < 0) return;
1068 lprintf(9, "<%s\n", buf);
1069 if (buf[0] != '2') {
1072 download_len = extract_long(&buf[4], 0);
1074 bytes_received = 0L;
1075 fp = fopen(tempfilename, "w");
1077 lprintf(9, "cannot open download file locally: %s\n",
1082 while (bytes_received < download_len) {
1083 sprintf(buf, "READ %ld|%ld",
1085 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1086 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1087 if (sock_puts(sock, buf) < 0) {
1089 unlink(tempfilename);
1092 if (sock_gets(sock, buf) < 0) {
1094 unlink(tempfilename);
1097 if (buf[0] == '6') {
1098 plen = extract_long(&buf[4], 0);
1099 if (sock_read(sock, pbuf, plen) < 0) {
1101 unlink(tempfilename);
1104 fwrite((char *) pbuf, plen, 1, fp);
1105 bytes_received = bytes_received + plen;
1110 if (sock_puts(sock, "CLOS") < 0) {
1111 unlink(tempfilename);
1114 if (sock_gets(sock, buf) < 0) {
1115 unlink(tempfilename);
1118 lprintf(9, "%s\n", buf);
1119 sprintf(buf, "mv %s ./network/spoolin/%s.%ld",
1120 tempfilename, remote_nodename, (long) getpid());
1127 * transmit network spool to the remote system
1129 void transmit_spool(int sock, char *remote_nodename)
1134 long bytes_to_write, thisblock;
1138 if (sock_puts(sock, "NUOP") < 0) return;
1139 if (sock_gets(sock, buf) < 0) return;
1140 lprintf(9, "<%s\n", buf);
1141 if (buf[0] != '2') {
1145 sprintf(sfname, "./network/spoolout/%s", remote_nodename);
1146 fd = open(sfname, O_RDONLY);
1148 if (errno == ENOENT) {
1149 lprintf(9, "Nothing to send.\n");
1151 lprintf(5, "cannot open upload file locally: %s\n",
1156 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1157 bytes_to_write = plen;
1158 while (bytes_to_write > 0L) {
1159 sprintf(buf, "WRIT %ld", bytes_to_write);
1160 if (sock_puts(sock, buf) < 0) {
1164 if (sock_gets(sock, buf) < 0) {
1168 thisblock = atol(&buf[4]);
1169 if (buf[0] == '7') {
1170 if (sock_write(sock, pbuf,
1171 (int) thisblock) < 0) {
1175 bytes_to_write = bytes_to_write - thisblock;
1184 if (sock_puts(sock, "UCLS 1") < 0) return;
1185 if (sock_gets(sock, buf) < 0) return;
1186 lprintf(9, "<%s\n", buf);
1187 if (buf[0] == '2') {
1195 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1197 void network_poll_node(char *node, char *secret, char *host, char *port) {
1201 if (network_talking_to(node, NTT_CHECK)) return;
1202 network_talking_to(node, NTT_ADD);
1203 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1205 sock = sock_connect(host, port, "tcp");
1207 lprintf(7, "Could not connect: %s\n", strerror(errno));
1208 network_talking_to(node, NTT_REMOVE);
1212 lprintf(9, "Connected!\n");
1214 /* Read the server greeting */
1215 if (sock_gets(sock, buf) < 0) goto bail;
1216 lprintf(9, ">%s\n", buf);
1218 /* Identify ourselves */
1219 sprintf(buf, "NETP %s|%s", config.c_nodename, secret);
1220 lprintf(9, "<%s\n", buf);
1221 if (sock_puts(sock, buf) <0) goto bail;
1222 if (sock_gets(sock, buf) < 0) goto bail;
1223 lprintf(9, ">%s\n", buf);
1224 if (buf[0] != '2') goto bail;
1226 /* At this point we are authenticated. */
1227 receive_spool(sock, node);
1228 transmit_spool(sock, node);
1230 sock_puts(sock, "QUIT");
1231 bail: sock_close(sock);
1232 network_talking_to(node, NTT_REMOVE);
1238 * Poll other Citadel nodes and transfer inbound/outbound network data.
1240 void network_poll_other_citadel_nodes(void) {
1241 char *ignetcfg = NULL;
1249 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1250 if (ignetcfg == NULL) return; /* no nodes defined */
1252 /* Use the string tokenizer to grab one line at a time */
1253 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1254 extract_token(linebuf, ignetcfg, i, '\n');
1255 extract(node, linebuf, 0);
1256 extract(secret, linebuf, 1);
1257 extract(host, linebuf, 2);
1258 extract(port, linebuf, 3);
1259 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1260 && (strlen(host) > 0) && strlen(port) > 0) {
1261 network_poll_node(node, secret, host, port);
1275 * network_do_queue()
1277 * Run through the rooms doing various types of network stuff.
1279 void network_do_queue(void) {
1280 static int doing_queue = 0;
1281 static time_t last_run = 0L;
1282 struct RoomProcList *ptr;
1285 * Run no more frequently than once every n seconds
1287 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
1290 * This is a simple concurrency check to make sure only one queue run
1291 * is done at a time. We could do this with a mutex, but since we
1292 * don't really require extremely fine granularity here, we'll do it
1293 * with a static variable instead.
1295 if (doing_queue) return;
1297 last_run = time(NULL);
1300 * Poll other Citadel nodes.
1302 network_poll_other_citadel_nodes();
1305 * Load the network map and use table into memory.
1308 network_usetable(UT_LOAD, NULL);
1311 * Go ahead and run the queue
1313 lprintf(7, "network: loading outbound queue\n");
1314 ForEachRoom(network_queue_room, NULL);
1316 lprintf(7, "network: running outbound queue\n");
1317 while (rplist != NULL) {
1318 network_spoolout_room(rplist->name);
1320 rplist = rplist->next;
1324 lprintf(7, "network: processing inbound queue\n");
1325 network_do_spoolin();
1327 /* Save the usetable and network map back to disk */
1328 network_usetable(UT_SAVE, NULL);
1329 write_network_map();
1331 lprintf(7, "network: queue run completed\n");
1338 * cmd_netp() - authenticate to the server as another Citadel node polling
1339 * for network traffic
1341 void cmd_netp(char *cmdbuf)
1349 extract(node, cmdbuf, 0);
1350 extract(pass, cmdbuf, 1);
1352 if (is_valid_node(nexthop, secret, node) != 0) {
1353 cprintf("%d authentication failed\n", ERROR);
1357 if (strcasecmp(pass, secret)) {
1358 cprintf("%d authentication failed\n", ERROR);
1362 if (network_talking_to(node, NTT_CHECK)) {
1363 cprintf("%d Already talking to %s right now\n", ERROR);
1367 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1368 network_talking_to(node, NTT_ADD);
1369 cprintf("%d authenticated as network node '%s'\n", OK,
1376 * Module entry point
1378 char *Dynamic_Module_Init(void)
1380 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1381 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
1382 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1383 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);