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
25 #include <sys/types.h>
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
32 # include <sys/time.h>
43 #include "sysdep_decls.h"
44 #include "citserver.h"
47 #include "dynloader.h"
54 #include "internet_addressing.h"
55 #include "serv_network.h"
56 #include "clientsocket.h"
61 * When we do network processing, it's accomplished in two passes; one to
62 * gather a list of rooms and one to actually do them. It's ok that rplist
63 * is global; this process *only* runs as part of the housekeeping loop and
64 * therefore only one will run at a time.
66 struct RoomProcList *rplist = NULL;
69 * We build a map of network nodes during processing.
71 struct NetMap *the_netmap = NULL;
75 * Manage the use table. This is a list of messages which have recently
76 * arrived on the system. It is maintained and queried to prevent the same
77 * message from being entered into the database multiple times if it happens
78 * to arrive multiple times by accident.
80 int network_usetable(int operation, struct CtdlMessage *msg) {
82 static struct UseTable *ut = NULL;
83 struct UseTable *uptr = NULL;
84 char *serialized_table = NULL;
93 serialized_table = CtdlGetSysConfig(USETABLE);
94 if (serialized_table == NULL) return(0);
96 for (i=0; i<num_tokens(serialized_table, '\n'); ++i) {
97 extract_token(buf, serialized_table, i, '\n');
98 uptr = (struct UseTable *)
99 mallok(sizeof(struct UseTable));
102 extract(msgid, buf, 0);
103 uptr->message_id = strdoop(msgid);
104 uptr->timestamp = extract_long(buf, 1);
109 phree(serialized_table);
110 serialized_table = NULL;
114 /* Bail out if we can't generate a message ID */
118 if (msg->cm_fields['I'] == NULL) {
121 if (strlen(msg->cm_fields['I']) == 0) {
125 /* Generate the message ID */
126 strcpy(msgid, msg->cm_fields['I']);
127 if (haschar(msgid, '@') == 0) {
129 if (msg->cm_fields['N'] != NULL) {
130 strcat(msgid, msg->cm_fields['N']);
137 /* Compare to the existing list */
138 for (uptr = ut; uptr != NULL; uptr = uptr->next) {
139 if (!strcasecmp(msgid, uptr->message_id)) {
144 /* If we got to this point, it's unique: add it. */
145 uptr = (struct UseTable *)
146 mallok(sizeof(struct UseTable));
147 if (uptr == NULL) return(0);
149 uptr->message_id = strdoop(msgid);
150 uptr->timestamp = time(NULL);
155 /* Figure out how big the serialized buffer should be */
157 for (uptr = ut; uptr != NULL; uptr = uptr->next) {
158 stlen = stlen + strlen(uptr->message_id) + 20;
160 serialized_table = mallok(stlen);
161 memset(serialized_table, 0, stlen);
164 if ( (serialized_table != NULL)
165 && ( (ut->timestamp - time(NULL)) <
167 sprintf(&serialized_table[strlen(
168 serialized_table)], "%s|%ld\n",
173 /* Now free the memory */
176 phree(uptr->message_id);
181 CtdlPutSysConfig(USETABLE, serialized_table);
182 phree(serialized_table);
187 /* should never get here unless illegal operation specified */
193 * Read the network map from its configuration file into memory.
195 void read_network_map(void) {
196 char *serialized_map = NULL;
199 struct NetMap *nmptr;
201 serialized_map = CtdlGetSysConfig(IGNETMAP);
202 if (serialized_map == NULL) return; /* if null, no entries */
204 /* Use the string tokenizer to grab one line at a time */
205 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
206 extract_token(buf, serialized_map, i, '\n');
207 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
208 extract(nmptr->nodename, buf, 0);
209 nmptr->lastcontact = extract_long(buf, 1);
210 extract(nmptr->nexthop, buf, 2);
211 nmptr->next = the_netmap;
215 phree(serialized_map);
220 * Write the network map from memory back to the configuration file.
222 void write_network_map(void) {
223 char *serialized_map = NULL;
224 struct NetMap *nmptr;
226 serialized_map = strdoop("");
228 if (the_netmap != NULL) {
229 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
230 serialized_map = reallok(serialized_map,
231 (strlen(serialized_map)+SIZ) );
232 if (strlen(nmptr->nodename) > 0) {
233 sprintf(&serialized_map[strlen(serialized_map)],
242 CtdlPutSysConfig(IGNETMAP, serialized_map);
243 phree(serialized_map);
245 /* Now free the list */
246 while (the_netmap != NULL) {
247 nmptr = the_netmap->next;
256 * Check the network map and determine whether the supplied node name is
257 * valid. If it is not a neighbor node, supply the name of a neighbor node
258 * which is the next hop. If it *is* a neighbor node, we also fill in the
261 int is_valid_node(char *nexthop, char *secret, char *node) {
262 char *ignetcfg = NULL;
267 struct NetMap *nmptr;
274 * First try the neighbor nodes
276 ignetcfg = CtdlGetSysConfig(IGNETCFG);
277 if (ignetcfg == NULL) {
278 if (nexthop != NULL) {
285 if (nexthop != NULL) {
289 /* Use the string tokenizer to grab one line at a time */
290 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
291 extract_token(linebuf, ignetcfg, i, '\n');
292 extract(buf, linebuf, 0);
293 if (!strcasecmp(buf, node)) {
294 if (nexthop != NULL) {
297 if (secret != NULL) {
298 extract(secret, linebuf, 1);
306 return(retval); /* yup, it's a direct neighbor */
310 * If we get to this point we have to see if we know the next hop
312 if (the_netmap != NULL) {
313 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
314 if (!strcasecmp(nmptr->nodename, node)) {
315 if (nexthop != NULL) {
316 strcpy(nexthop, nmptr->nexthop);
324 * If we get to this point, the supplied node name is bogus.
326 lprintf(5, "Invalid node name <%s>\n", node);
334 void cmd_gnet(char *argbuf) {
339 if (CtdlAccessCheck(ac_room_aide)) return;
340 assoc_file_name(filename, &CC->quickroom, "netconfigs");
341 cprintf("%d Network settings for room #%ld <%s>\n",
343 CC->quickroom.QRnumber, CC->quickroom.QRname);
345 fp = fopen(filename, "r");
347 while (fgets(buf, sizeof buf, fp) != NULL) {
348 buf[strlen(buf)-1] = 0;
349 cprintf("%s\n", buf);
358 void cmd_snet(char *argbuf) {
359 char tempfilename[SIZ];
364 if (CtdlAccessCheck(ac_room_aide)) return;
365 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
366 assoc_file_name(filename, &CC->quickroom, "netconfigs");
368 fp = fopen(tempfilename, "w");
370 cprintf("%d Cannot open %s: %s\n",
371 ERROR+INTERNAL_ERROR,
376 cprintf("%d %s\n", SEND_LISTING, tempfilename);
377 while (client_gets(buf), strcmp(buf, "000")) {
378 fprintf(fp, "%s\n", buf);
382 /* Now copy the temp file to its permanent location
383 * (We use /bin/mv instead of link() because they may be on
384 * different filesystems)
387 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
394 * Spools out one message from the list.
396 void network_spool_msg(long msgnum, void *userdata) {
397 struct SpoolControl *sc;
398 struct namelist *nptr;
402 char *newpath = NULL;
403 size_t instr_len = SIZ;
404 struct CtdlMessage *msg = NULL;
405 struct CtdlMessage *imsg;
406 struct ser_ret sermsg;
412 int delete_after_send = 0; /* Set to 1 to delete after spooling */
414 sc = (struct SpoolControl *)userdata;
417 * Process mailing list recipients
419 if (sc->listrecps != NULL) {
421 /* First, copy it to the spoolout room */
422 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
423 if (err != 0) return;
426 * Figure out how big a buffer we need to allocate
428 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
429 instr_len = instr_len + strlen(nptr->name);
435 lprintf(9, "Generating delivery instructions\n");
436 instr = mallok(instr_len);
438 lprintf(1, "Cannot allocate %d bytes for instr...\n",
443 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
444 "bounceto|postmaster@%s\n" ,
445 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
447 /* Generate delivery instructions for each recipient */
448 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
449 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
454 * Generate a message from the instructions
456 imsg = mallok(sizeof(struct CtdlMessage));
457 memset(imsg, 0, sizeof(struct CtdlMessage));
458 imsg->cm_magic = CTDLMESSAGE_MAGIC;
459 imsg->cm_anon_type = MES_NORMAL;
460 imsg->cm_format_type = FMT_RFC822;
461 imsg->cm_fields['A'] = strdoop("Citadel");
462 imsg->cm_fields['M'] = instr;
464 /* Save delivery instructions in spoolout room */
465 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
466 CtdlFreeMessage(imsg);
470 * Process IGnet push shares
472 if (sc->ignet_push_shares != NULL) {
474 msg = CtdlFetchMessage(msgnum);
477 /* Prepend our node name to the Path field whenever
478 * sending a message to another IGnet node
480 if (msg->cm_fields['P'] == NULL) {
481 msg->cm_fields['P'] = strdoop("username");
483 newpath = mallok(strlen(msg->cm_fields['P']) +
484 strlen(config.c_nodename) + 2);
485 sprintf(newpath, "%s!%s", config.c_nodename,
486 msg->cm_fields['P']);
487 phree(msg->cm_fields['P']);
488 msg->cm_fields['P'] = newpath;
491 * Force the message to appear in the correct room
492 * on the far end by setting the C field correctly
494 if (msg->cm_fields['C'] != NULL) {
495 phree(msg->cm_fields['C']);
497 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
500 * Determine if this message is set to be deleted
501 * after sending out on the network
503 if (msg->cm_fields['S'] != NULL) {
504 if (!strcasecmp(msg->cm_fields['S'],
506 delete_after_send = 1;
511 * Now serialize it for transmission
513 serialize_message(&sermsg, msg);
515 /* Now send it to every node */
516 for (nptr = sc->ignet_push_shares; nptr != NULL;
521 /* Check for valid node name */
522 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
523 lprintf(3, "Invalid node <%s>\n",
528 /* Check for split horizon */
529 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
530 bang = num_tokens(msg->cm_fields['P'], '!');
531 if (bang > 1) for (i=0; i<(bang-1); ++i) {
532 extract_token(buf, msg->cm_fields['P'],
534 if (!strcasecmp(buf, nptr->name)) {
539 /* Send the message */
542 "./network/spoolout/%s",
544 fp = fopen(filename, "ab");
553 CtdlFreeMessage(msg);
557 /* update lastsent */
558 sc->lastsent = msgnum;
560 /* Delete this message if delete-after-send is set */
561 if (delete_after_send) {
562 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
571 * Batch up and send all outbound traffic from the current room
573 void network_spoolout_room(char *room_to_spool) {
578 struct SpoolControl sc;
579 /* struct namelist *digestrecps = NULL; */
580 struct namelist *nptr;
582 lprintf(7, "Spooling <%s>\n", room_to_spool);
583 if (getroom(&CC->quickroom, room_to_spool) != 0) {
584 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
588 memset(&sc, 0, sizeof(struct SpoolControl));
589 assoc_file_name(filename, &CC->quickroom, "netconfigs");
591 fp = fopen(filename, "r");
593 lprintf(7, "Outbound batch processing skipped for <%s>\n",
594 CC->quickroom.QRname);
598 lprintf(5, "Outbound batch processing started for <%s>\n",
599 CC->quickroom.QRname);
601 while (fgets(buf, sizeof buf, fp) != NULL) {
602 buf[strlen(buf)-1] = 0;
604 extract(instr, buf, 0);
605 if (!strcasecmp(instr, "lastsent")) {
606 sc.lastsent = extract_long(buf, 1);
608 else if (!strcasecmp(instr, "listrecp")) {
609 nptr = (struct namelist *)
610 mallok(sizeof(struct namelist));
611 nptr->next = sc.listrecps;
612 extract(nptr->name, buf, 1);
615 else if (!strcasecmp(instr, "ignet_push_share")) {
616 nptr = (struct namelist *)
617 mallok(sizeof(struct namelist));
618 nptr->next = sc.ignet_push_shares;
619 extract(nptr->name, buf, 1);
620 sc.ignet_push_shares = nptr;
628 /* Do something useful */
629 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
630 network_spool_msg, &sc);
633 /* Now rewrite the config file */
634 fp = fopen(filename, "w");
636 lprintf(1, "ERROR: cannot open %s: %s\n",
637 filename, strerror(errno));
640 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
642 /* Write out the listrecps while freeing from memory at the
643 * same time. Am I clever or what? :)
645 while (sc.listrecps != NULL) {
646 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
647 nptr = sc.listrecps->next;
651 while (sc.ignet_push_shares != NULL) {
652 fprintf(fp, "ignet_push_share|%s\n",
653 sc.ignet_push_shares->name);
654 nptr = sc.ignet_push_shares->next;
655 phree(sc.ignet_push_shares);
656 sc.ignet_push_shares = nptr;
662 lprintf(5, "Outbound batch processing finished for <%s>\n",
663 CC->quickroom.QRname);
668 * Batch up and send all outbound traffic from the current room
670 void network_queue_room(struct quickroom *qrbuf, void *data) {
671 struct RoomProcList *ptr;
673 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
674 if (ptr == NULL) return;
676 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
683 * Learn topology from path fields
685 void network_learn_topology(char *node, char *path) {
687 struct NetMap *nmptr;
691 if (num_tokens(path, '!') < 3) return;
692 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
693 if (!strcasecmp(nmptr->nodename, node)) {
694 extract_token(nmptr->nexthop, path, 0, '!');
695 nmptr->lastcontact = time(NULL);
700 /* If we got here then it's not in the map, so add it. */
701 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
702 strcpy(nmptr->nodename, node);
703 nmptr->lastcontact = time(NULL);
704 extract_token(nmptr->nexthop, path, 0, '!');
705 nmptr->next = the_netmap;
713 * Bounce a message back to the sender
715 void network_bounce(struct CtdlMessage *msg, char *reason) {
716 static int serialnum = 0;
719 struct ser_ret sermsg;
720 char *oldpath = NULL;
723 lprintf(9, "entering network_bounce()\n");
725 if (msg == NULL) return;
728 * Give it a fresh message ID
730 if (msg->cm_fields['I'] != NULL) {
731 phree(msg->cm_fields['I']);
733 sprintf(buf, "%ld.%04x.%04x@%s",
734 time(NULL), getpid(), ++serialnum, config.c_fqdn);
735 msg->cm_fields['I'] = strdoop(buf);
738 * FIXME ... right now we're just sending a bounce; we really want to
739 * include the text of the bounced message.
741 if (msg->cm_fields['M'] != NULL) {
742 phree(msg->cm_fields['M']);
744 msg->cm_fields['M'] = strdoop(reason);
745 msg->cm_format_type = 0;
748 * Turn the message around
750 if (msg->cm_fields['R'] == NULL) {
751 phree(msg->cm_fields['R']);
754 if (msg->cm_fields['D'] == NULL) {
755 phree(msg->cm_fields['D']);
758 msg->cm_fields['R'] = msg->cm_fields['A'];
759 msg->cm_fields['D'] = msg->cm_fields['N'];
760 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
761 msg->cm_fields['N'] = strdoop(config.c_nodename);
763 if (!strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
764 phree(msg->cm_fields['D']);
768 * If this is a bounce of a bounce, send it to the Aide> room
769 * instead of looping around forever
771 if (msg->cm_fields['D'] == NULL) if (msg->cm_fields['R'] != NULL)
772 if (!strcasecmp(msg->cm_fields['R'], BOUNCESOURCE)) {
773 phree(msg->cm_fields['R']);
774 if (msg->cm_fields['C'] != NULL) {
775 phree(msg->cm_fields['C']);
777 msg->cm_fields['C'] = strdoop(AIDEROOM);
780 /* prepend our node to the path */
781 if (msg->cm_fields['P'] != NULL) {
782 oldpath = msg->cm_fields['P'];
783 msg->cm_fields['P'] = NULL;
786 oldpath = strdoop("unknown_user");
788 msg->cm_fields['P'] = mallok(strlen(oldpath) + SIZ);
789 sprintf(msg->cm_fields['P'], "%s!%s", config.c_nodename, oldpath);
792 /* serialize the message */
793 serialize_message(&sermsg, msg);
796 sprintf(filename, "./network/spoolin/bounce.%04x.%04x",
797 getpid(), serialnum);
799 fp = fopen(filename, "ab");
806 CtdlFreeMessage(msg);
807 lprintf(9, "leaving network_bounce()\n");
814 * Process a buffer containing a single message from a single file
815 * from the inbound queue
817 void network_process_buffer(char *buffer, long size) {
818 struct CtdlMessage *msg;
823 struct usersupp tempUS;
825 char target_room[ROOMNAMELEN];
826 struct ser_ret sermsg;
827 char *oldpath = NULL;
832 /* Set default target room to trash */
833 strcpy(target_room, TWITROOM);
835 /* Load the message into memory */
836 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
837 memset(msg, 0, sizeof(struct CtdlMessage));
838 msg->cm_magic = CTDLMESSAGE_MAGIC;
839 msg->cm_anon_type = buffer[1];
840 msg->cm_format_type = buffer[2];
842 for (pos = 3; pos < size; ++pos) {
844 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
845 pos = pos + strlen(&buffer[(int)pos]);
848 /* Check for message routing */
849 if (msg->cm_fields['D'] != NULL) {
850 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
852 /* route the message */
853 if (is_valid_node(NULL, NULL,
854 msg->cm_fields['D']) == 0) {
856 /* prepend our node to the path */
857 if (msg->cm_fields['P'] != NULL) {
858 oldpath = msg->cm_fields['P'];
859 msg->cm_fields['P'] = NULL;
862 oldpath = strdoop("unknown_user");
864 msg->cm_fields['P'] =
865 mallok(strlen(oldpath) + SIZ);
866 sprintf(msg->cm_fields['P'], "%s!%s",
867 config.c_nodename, oldpath);
870 /* serialize the message */
871 serialize_message(&sermsg, msg);
875 "./network/spoolout/%s",
876 msg->cm_fields['D']);
877 fp = fopen(filename, "ab");
884 CtdlFreeMessage(msg);
888 else { /* invalid destination node name */
891 "A message you sent could not be delivered due to an invalid destination node"
892 " name. Please check the address and try sending the message again.\n");
901 * Check to see if we already have a copy of this message
903 if (network_usetable(UT_INSERT, msg) != 0) {
904 sprintf(buf, "Loopzapper rejected message <%s>\n",
905 msg->cm_fields['I']);
907 CtdlFreeMessage(msg);
912 /* Learn network topology from the path */
913 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
914 network_learn_topology(msg->cm_fields['N'],
915 msg->cm_fields['P']);
918 /* Does it have a recipient? If so, validate it... */
919 if (msg->cm_fields['R'] != NULL) {
921 safestrncpy(recp, msg->cm_fields['R'], sizeof(recp));
923 e = alias(recp); /* alias and mail type */
924 if ((recp[0] == 0) || (e == MES_ERROR)) {
927 "A message you sent could not be delivered due to an invalid address.\n"
928 "Please check the address and try sending the message again.\n");
933 else if (e == MES_LOCAL) {
934 a = getuser(&tempUS, recp);
938 "A message you sent could not be delivered because the user does not exist\n"
939 "on this system. Please check the address and try again.\n");
945 MailboxName(target_room, &tempUS, MAILROOM);
950 else if (msg->cm_fields['C'] != NULL) {
951 safestrncpy(target_room,
956 else if (msg->cm_fields['O'] != NULL) {
957 safestrncpy(target_room,
962 /* save the message into a room */
963 msg->cm_flags = CM_SKIP_HOOKS;
964 CtdlSaveMsg(msg, "", target_room, 0);
965 CtdlFreeMessage(msg);
970 * Process a single message from a single file from the inbound queue
972 void network_process_message(FILE *fp, long msgstart, long msgend) {
977 hold_pos = ftell(fp);
978 size = msgend - msgstart + 1;
979 buffer = mallok(size);
980 if (buffer != NULL) {
981 fseek(fp, msgstart, SEEK_SET);
982 fread(buffer, size, 1, fp);
983 network_process_buffer(buffer, size);
987 fseek(fp, hold_pos, SEEK_SET);
992 * Process a single file from the inbound queue
994 void network_process_file(char *filename) {
996 long msgstart = (-1L);
1001 lprintf(7, "network: processing <%s>\n", filename);
1003 fp = fopen(filename, "rb");
1005 lprintf(5, "Error opening %s: %s\n",
1006 filename, strerror(errno));
1010 /* Look for messages in the data stream and break them out */
1011 while (ch = getc(fp), ch >= 0) {
1014 if (msgstart >= 0L) {
1015 msgend = msgcur - 1;
1016 network_process_message(fp, msgstart, msgend);
1024 msgend = msgcur - 1;
1025 if (msgstart >= 0L) {
1026 network_process_message(fp, msgstart, msgend);
1035 * Process anything in the inbound queue
1037 void network_do_spoolin(void) {
1042 dp = opendir("./network/spoolin");
1043 if (dp == NULL) return;
1045 while (d = readdir(dp), d != NULL) {
1046 sprintf(filename, "./network/spoolin/%s", d->d_name);
1047 network_process_file(filename);
1059 * receive network spool from the remote system
1061 void receive_spool(int sock, char *remote_nodename) {
1063 long bytes_received;
1065 static char pbuf[IGNET_PACKET_SIZE];
1066 char tempfilename[PATH_MAX];
1070 strcpy(tempfilename, tmpnam(NULL));
1071 if (sock_puts(sock, "NDOP") < 0) return;
1072 if (sock_gets(sock, buf) < 0) return;
1073 lprintf(9, "<%s\n", buf);
1074 if (buf[0] != '2') {
1077 download_len = extract_long(&buf[4], 0);
1079 bytes_received = 0L;
1080 fp = fopen(tempfilename, "w");
1082 lprintf(9, "cannot open download file locally: %s\n",
1087 while (bytes_received < download_len) {
1088 sprintf(buf, "READ %ld|%ld",
1090 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1091 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1092 if (sock_puts(sock, buf) < 0) {
1094 unlink(tempfilename);
1097 if (sock_gets(sock, buf) < 0) {
1099 unlink(tempfilename);
1102 if (buf[0] == '6') {
1103 plen = extract_long(&buf[4], 0);
1104 if (sock_read(sock, pbuf, plen) < 0) {
1106 unlink(tempfilename);
1109 fwrite((char *) pbuf, plen, 1, fp);
1110 bytes_received = bytes_received + plen;
1115 if (sock_puts(sock, "CLOS") < 0) {
1116 unlink(tempfilename);
1119 if (sock_gets(sock, buf) < 0) {
1120 unlink(tempfilename);
1123 lprintf(9, "%s\n", buf);
1124 sprintf(buf, "mv %s ./network/spoolin/%s.%ld",
1125 tempfilename, remote_nodename, (long) getpid());
1132 * transmit network spool to the remote system
1134 void transmit_spool(int sock, char *remote_nodename)
1139 long bytes_to_write, thisblock;
1143 if (sock_puts(sock, "NUOP") < 0) return;
1144 if (sock_gets(sock, buf) < 0) return;
1145 lprintf(9, "<%s\n", buf);
1146 if (buf[0] != '2') {
1150 sprintf(sfname, "./network/spoolout/%s", remote_nodename);
1151 fd = open(sfname, O_RDONLY);
1153 if (errno == ENOENT) {
1154 lprintf(9, "Nothing to send.\n");
1156 lprintf(5, "cannot open upload file locally: %s\n",
1161 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1162 bytes_to_write = plen;
1163 while (bytes_to_write > 0L) {
1164 sprintf(buf, "WRIT %ld", bytes_to_write);
1165 if (sock_puts(sock, buf) < 0) {
1169 if (sock_gets(sock, buf) < 0) {
1173 thisblock = atol(&buf[4]);
1174 if (buf[0] == '7') {
1175 if (sock_write(sock, pbuf,
1176 (int) thisblock) < 0) {
1180 bytes_to_write = bytes_to_write - thisblock;
1189 if (sock_puts(sock, "UCLS 1") < 0) return;
1190 if (sock_gets(sock, buf) < 0) return;
1191 lprintf(9, "<%s\n", buf);
1192 if (buf[0] == '2') {
1200 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1202 void network_poll_node(char *node, char *secret, char *host, char *port) {
1206 if (network_talking_to(node, NTT_CHECK)) return;
1207 network_talking_to(node, NTT_ADD);
1208 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1210 sock = sock_connect(host, port, "tcp");
1212 lprintf(7, "Could not connect: %s\n", strerror(errno));
1213 network_talking_to(node, NTT_REMOVE);
1217 lprintf(9, "Connected!\n");
1219 /* Read the server greeting */
1220 if (sock_gets(sock, buf) < 0) goto bail;
1221 lprintf(9, ">%s\n", buf);
1223 /* Identify ourselves */
1224 sprintf(buf, "NETP %s|%s", config.c_nodename, secret);
1225 lprintf(9, "<%s\n", buf);
1226 if (sock_puts(sock, buf) <0) goto bail;
1227 if (sock_gets(sock, buf) < 0) goto bail;
1228 lprintf(9, ">%s\n", buf);
1229 if (buf[0] != '2') goto bail;
1231 /* At this point we are authenticated. */
1232 receive_spool(sock, node);
1233 transmit_spool(sock, node);
1235 sock_puts(sock, "QUIT");
1236 bail: sock_close(sock);
1237 network_talking_to(node, NTT_REMOVE);
1243 * Poll other Citadel nodes and transfer inbound/outbound network data.
1245 void network_poll_other_citadel_nodes(void) {
1246 char *ignetcfg = NULL;
1254 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1255 if (ignetcfg == NULL) return; /* no nodes defined */
1257 /* Use the string tokenizer to grab one line at a time */
1258 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1259 extract_token(linebuf, ignetcfg, i, '\n');
1260 extract(node, linebuf, 0);
1261 extract(secret, linebuf, 1);
1262 extract(host, linebuf, 2);
1263 extract(port, linebuf, 3);
1264 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1265 && (strlen(host) > 0) && strlen(port) > 0) {
1266 network_poll_node(node, secret, host, port);
1280 * network_do_queue()
1282 * Run through the rooms doing various types of network stuff.
1284 void network_do_queue(void) {
1285 static int doing_queue = 0;
1286 static time_t last_run = 0L;
1287 struct RoomProcList *ptr;
1290 * Run no more frequently than once every n seconds
1292 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
1295 * This is a simple concurrency check to make sure only one queue run
1296 * is done at a time. We could do this with a mutex, but since we
1297 * don't really require extremely fine granularity here, we'll do it
1298 * with a static variable instead.
1300 if (doing_queue) return;
1302 last_run = time(NULL);
1305 * Poll other Citadel nodes.
1307 network_poll_other_citadel_nodes();
1310 * Load the network map and use table into memory.
1313 network_usetable(UT_LOAD, NULL);
1316 * Go ahead and run the queue
1318 lprintf(7, "network: loading outbound queue\n");
1319 ForEachRoom(network_queue_room, NULL);
1321 lprintf(7, "network: running outbound queue\n");
1322 while (rplist != NULL) {
1323 network_spoolout_room(rplist->name);
1325 rplist = rplist->next;
1329 lprintf(7, "network: processing inbound queue\n");
1330 network_do_spoolin();
1332 /* Save the usetable and network map back to disk */
1333 network_usetable(UT_SAVE, NULL);
1334 write_network_map();
1336 lprintf(7, "network: queue run completed\n");
1343 * cmd_netp() - authenticate to the server as another Citadel node polling
1344 * for network traffic
1346 void cmd_netp(char *cmdbuf)
1354 extract(node, cmdbuf, 0);
1355 extract(pass, cmdbuf, 1);
1357 if (is_valid_node(nexthop, secret, node) != 0) {
1358 cprintf("%d authentication failed\n", ERROR);
1362 if (strcasecmp(pass, secret)) {
1363 cprintf("%d authentication failed\n", ERROR);
1367 if (network_talking_to(node, NTT_CHECK)) {
1368 cprintf("%d Already talking to %s right now\n", ERROR);
1372 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1373 network_talking_to(node, NTT_ADD);
1374 cprintf("%d authenticated as network node '%s'\n", OK,
1381 * Module entry point
1383 char *Dynamic_Module_Init(void)
1385 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1386 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
1387 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1388 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);