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"
65 * When we do network processing, it's accomplished in two passes; one to
66 * gather a list of rooms and one to actually do them. It's ok that rplist
67 * is global; this process *only* runs as part of the housekeeping loop and
68 * therefore only one will run at a time.
70 struct RoomProcList *rplist = NULL;
73 * We build a map of network nodes during processing.
75 struct NetMap *the_netmap = NULL;
79 * Manage the use table. This is a list of messages which have recently
80 * arrived on the system. It is maintained and queried to prevent the same
81 * message from being entered into the database multiple times if it happens
82 * to arrive multiple times by accident.
84 int network_usetable(int operation, struct CtdlMessage *msg) {
86 static struct UseTable *ut = NULL;
87 struct UseTable *uptr = NULL;
88 char *serialized_table = NULL;
97 serialized_table = CtdlGetSysConfig(USETABLE);
98 if (serialized_table == NULL) return(0);
100 for (i=0; i<num_tokens(serialized_table, '\n'); ++i) {
101 extract_token(buf, serialized_table, i, '\n');
102 uptr = (struct UseTable *)
103 mallok(sizeof(struct UseTable));
106 extract(msgid, buf, 0);
107 uptr->message_id = strdoop(msgid);
108 uptr->timestamp = extract_long(buf, 1);
113 phree(serialized_table);
114 serialized_table = NULL;
118 /* Bail out if we can't generate a message ID */
122 if (msg->cm_fields['I'] == NULL) {
125 if (strlen(msg->cm_fields['I']) == 0) {
129 /* Generate the message ID */
130 strcpy(msgid, msg->cm_fields['I']);
131 if (haschar(msgid, '@') == 0) {
133 if (msg->cm_fields['N'] != NULL) {
134 strcat(msgid, msg->cm_fields['N']);
141 /* Compare to the existing list */
142 for (uptr = ut; uptr != NULL; uptr = uptr->next) {
143 if (!strcasecmp(msgid, uptr->message_id)) {
148 /* If we got to this point, it's unique: add it. */
149 uptr = (struct UseTable *)
150 mallok(sizeof(struct UseTable));
151 if (uptr == NULL) return(0);
153 uptr->message_id = strdoop(msgid);
154 uptr->timestamp = time(NULL);
159 /* Figure out how big the serialized buffer should be */
161 for (uptr = ut; uptr != NULL; uptr = uptr->next) {
162 stlen = stlen + strlen(uptr->message_id) + 20;
164 serialized_table = mallok(stlen);
165 memset(serialized_table, 0, stlen);
168 if ( (serialized_table != NULL)
169 && ( (ut->timestamp - time(NULL)) <
171 sprintf(&serialized_table[strlen(
172 serialized_table)], "%s|%ld\n",
174 (long)ut->timestamp);
177 /* Now free the memory */
180 phree(uptr->message_id);
185 CtdlPutSysConfig(USETABLE, serialized_table);
186 phree(serialized_table);
191 /* should never get here unless illegal operation specified */
197 * Read the network map from its configuration file into memory.
199 void read_network_map(void) {
200 char *serialized_map = NULL;
203 struct NetMap *nmptr;
205 serialized_map = CtdlGetSysConfig(IGNETMAP);
206 if (serialized_map == NULL) return; /* if null, no entries */
208 /* Use the string tokenizer to grab one line at a time */
209 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
210 extract_token(buf, serialized_map, i, '\n');
211 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
212 extract(nmptr->nodename, buf, 0);
213 nmptr->lastcontact = extract_long(buf, 1);
214 extract(nmptr->nexthop, buf, 2);
215 nmptr->next = the_netmap;
219 phree(serialized_map);
224 * Write the network map from memory back to the configuration file.
226 void write_network_map(void) {
227 char *serialized_map = NULL;
228 struct NetMap *nmptr;
230 serialized_map = strdoop("");
232 if (the_netmap != NULL) {
233 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
234 serialized_map = reallok(serialized_map,
235 (strlen(serialized_map)+SIZ) );
236 if (strlen(nmptr->nodename) > 0) {
237 sprintf(&serialized_map[strlen(serialized_map)],
240 (long)nmptr->lastcontact,
246 CtdlPutSysConfig(IGNETMAP, serialized_map);
247 phree(serialized_map);
249 /* Now free the list */
250 while (the_netmap != NULL) {
251 nmptr = the_netmap->next;
260 * Check the network map and determine whether the supplied node name is
261 * valid. If it is not a neighbor node, supply the name of a neighbor node
262 * which is the next hop. If it *is* a neighbor node, we also fill in the
265 int is_valid_node(char *nexthop, char *secret, char *node) {
266 char *ignetcfg = NULL;
271 struct NetMap *nmptr;
278 * First try the neighbor nodes
280 ignetcfg = CtdlGetSysConfig(IGNETCFG);
281 if (ignetcfg == NULL) {
282 if (nexthop != NULL) {
289 if (nexthop != NULL) {
293 /* Use the string tokenizer to grab one line at a time */
294 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
295 extract_token(linebuf, ignetcfg, i, '\n');
296 extract(buf, linebuf, 0);
297 if (!strcasecmp(buf, node)) {
298 if (nexthop != NULL) {
301 if (secret != NULL) {
302 extract(secret, linebuf, 1);
310 return(retval); /* yup, it's a direct neighbor */
314 * If we get to this point we have to see if we know the next hop
316 if (the_netmap != NULL) {
317 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
318 if (!strcasecmp(nmptr->nodename, node)) {
319 if (nexthop != NULL) {
320 strcpy(nexthop, nmptr->nexthop);
328 * If we get to this point, the supplied node name is bogus.
330 lprintf(5, "Invalid node name <%s>\n", node);
338 void cmd_gnet(char *argbuf) {
343 if (CtdlAccessCheck(ac_room_aide)) return;
344 assoc_file_name(filename, &CC->quickroom, "netconfigs");
345 cprintf("%d Network settings for room #%ld <%s>\n",
347 CC->quickroom.QRnumber, CC->quickroom.QRname);
349 fp = fopen(filename, "r");
351 while (fgets(buf, sizeof buf, fp) != NULL) {
352 buf[strlen(buf)-1] = 0;
353 cprintf("%s\n", buf);
362 void cmd_snet(char *argbuf) {
363 char tempfilename[SIZ];
368 if (CtdlAccessCheck(ac_room_aide)) return;
369 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
370 assoc_file_name(filename, &CC->quickroom, "netconfigs");
372 fp = fopen(tempfilename, "w");
374 cprintf("%d Cannot open %s: %s\n",
375 ERROR+INTERNAL_ERROR,
380 cprintf("%d %s\n", SEND_LISTING, tempfilename);
381 while (client_gets(buf), strcmp(buf, "000")) {
382 fprintf(fp, "%s\n", buf);
386 /* Now copy the temp file to its permanent location
387 * (We use /bin/mv instead of link() because they may be on
388 * different filesystems)
391 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
398 * Spools out one message from the list.
400 void network_spool_msg(long msgnum, void *userdata) {
401 struct SpoolControl *sc;
402 struct namelist *nptr;
406 char *newpath = NULL;
407 size_t instr_len = SIZ;
408 struct CtdlMessage *msg = NULL;
409 struct CtdlMessage *imsg;
410 struct ser_ret sermsg;
416 int delete_after_send = 0; /* Set to 1 to delete after spooling */
418 sc = (struct SpoolControl *)userdata;
421 * Process mailing list recipients
423 if (sc->listrecps != NULL) {
425 /* First, copy it to the spoolout room */
426 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
427 if (err != 0) return;
430 * Figure out how big a buffer we need to allocate
432 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
433 instr_len = instr_len + strlen(nptr->name);
439 lprintf(9, "Generating delivery instructions\n");
440 instr = mallok(instr_len);
442 lprintf(1, "Cannot allocate %d bytes for instr...\n",
447 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
448 "bounceto|postmaster@%s\n" ,
449 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
451 /* Generate delivery instructions for each recipient */
452 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
453 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
458 * Generate a message from the instructions
460 imsg = mallok(sizeof(struct CtdlMessage));
461 memset(imsg, 0, sizeof(struct CtdlMessage));
462 imsg->cm_magic = CTDLMESSAGE_MAGIC;
463 imsg->cm_anon_type = MES_NORMAL;
464 imsg->cm_format_type = FMT_RFC822;
465 imsg->cm_fields['A'] = strdoop("Citadel");
466 imsg->cm_fields['M'] = instr;
468 /* Save delivery instructions in spoolout room */
469 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
470 CtdlFreeMessage(imsg);
474 * Process IGnet push shares
476 if (sc->ignet_push_shares != NULL) {
478 msg = CtdlFetchMessage(msgnum);
481 /* Prepend our node name to the Path field whenever
482 * sending a message to another IGnet node
484 if (msg->cm_fields['P'] == NULL) {
485 msg->cm_fields['P'] = strdoop("username");
487 newpath = mallok(strlen(msg->cm_fields['P']) +
488 strlen(config.c_nodename) + 2);
489 sprintf(newpath, "%s!%s", config.c_nodename,
490 msg->cm_fields['P']);
491 phree(msg->cm_fields['P']);
492 msg->cm_fields['P'] = newpath;
495 * Force the message to appear in the correct room
496 * on the far end by setting the C field correctly
498 if (msg->cm_fields['C'] != NULL) {
499 phree(msg->cm_fields['C']);
501 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
504 * Determine if this message is set to be deleted
505 * after sending out on the network
507 if (msg->cm_fields['S'] != NULL) {
508 if (!strcasecmp(msg->cm_fields['S'],
510 delete_after_send = 1;
515 * Now serialize it for transmission
517 serialize_message(&sermsg, msg);
519 /* Now send it to every node */
520 for (nptr = sc->ignet_push_shares; nptr != NULL;
525 /* Check for valid node name */
526 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
527 lprintf(3, "Invalid node <%s>\n",
532 /* Check for split horizon */
533 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
534 bang = num_tokens(msg->cm_fields['P'], '!');
535 if (bang > 1) for (i=0; i<(bang-1); ++i) {
536 extract_token(buf, msg->cm_fields['P'],
538 if (!strcasecmp(buf, nptr->name)) {
543 /* Send the message */
546 "./network/spoolout/%s",
548 fp = fopen(filename, "ab");
557 CtdlFreeMessage(msg);
561 /* update lastsent */
562 sc->lastsent = msgnum;
564 /* Delete this message if delete-after-send is set */
565 if (delete_after_send) {
566 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
575 * Batch up and send all outbound traffic from the current room
577 void network_spoolout_room(char *room_to_spool) {
582 struct SpoolControl sc;
583 /* struct namelist *digestrecps = NULL; */
584 struct namelist *nptr;
586 lprintf(7, "Spooling <%s>\n", room_to_spool);
587 if (getroom(&CC->quickroom, room_to_spool) != 0) {
588 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
592 memset(&sc, 0, sizeof(struct SpoolControl));
593 assoc_file_name(filename, &CC->quickroom, "netconfigs");
595 fp = fopen(filename, "r");
597 lprintf(7, "Outbound batch processing skipped for <%s>\n",
598 CC->quickroom.QRname);
602 lprintf(5, "Outbound batch processing started for <%s>\n",
603 CC->quickroom.QRname);
605 while (fgets(buf, sizeof buf, fp) != NULL) {
606 buf[strlen(buf)-1] = 0;
608 extract(instr, buf, 0);
609 if (!strcasecmp(instr, "lastsent")) {
610 sc.lastsent = extract_long(buf, 1);
612 else if (!strcasecmp(instr, "listrecp")) {
613 nptr = (struct namelist *)
614 mallok(sizeof(struct namelist));
615 nptr->next = sc.listrecps;
616 extract(nptr->name, buf, 1);
619 else if (!strcasecmp(instr, "ignet_push_share")) {
620 nptr = (struct namelist *)
621 mallok(sizeof(struct namelist));
622 nptr->next = sc.ignet_push_shares;
623 extract(nptr->name, buf, 1);
624 sc.ignet_push_shares = nptr;
632 /* Do something useful */
633 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
634 network_spool_msg, &sc);
637 /* Now rewrite the config file */
638 fp = fopen(filename, "w");
640 lprintf(1, "ERROR: cannot open %s: %s\n",
641 filename, strerror(errno));
644 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
646 /* Write out the listrecps while freeing from memory at the
647 * same time. Am I clever or what? :)
649 while (sc.listrecps != NULL) {
650 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
651 nptr = sc.listrecps->next;
655 while (sc.ignet_push_shares != NULL) {
656 fprintf(fp, "ignet_push_share|%s\n",
657 sc.ignet_push_shares->name);
658 nptr = sc.ignet_push_shares->next;
659 phree(sc.ignet_push_shares);
660 sc.ignet_push_shares = nptr;
666 lprintf(5, "Outbound batch processing finished for <%s>\n",
667 CC->quickroom.QRname);
672 * Batch up and send all outbound traffic from the current room
674 void network_queue_room(struct quickroom *qrbuf, void *data) {
675 struct RoomProcList *ptr;
677 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
678 if (ptr == NULL) return;
680 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
687 * Learn topology from path fields
689 void network_learn_topology(char *node, char *path) {
691 struct NetMap *nmptr;
695 if (num_tokens(path, '!') < 3) return;
696 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
697 if (!strcasecmp(nmptr->nodename, node)) {
698 extract_token(nmptr->nexthop, path, 0, '!');
699 nmptr->lastcontact = time(NULL);
704 /* If we got here then it's not in the map, so add it. */
705 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
706 strcpy(nmptr->nodename, node);
707 nmptr->lastcontact = time(NULL);
708 extract_token(nmptr->nexthop, path, 0, '!');
709 nmptr->next = the_netmap;
717 * Bounce a message back to the sender
719 void network_bounce(struct CtdlMessage *msg, char *reason) {
720 static int serialnum = 0;
723 struct ser_ret sermsg;
724 char *oldpath = NULL;
727 lprintf(9, "entering network_bounce()\n");
729 if (msg == NULL) return;
732 * Give it a fresh message ID
734 if (msg->cm_fields['I'] != NULL) {
735 phree(msg->cm_fields['I']);
737 sprintf(buf, "%ld.%04x.%04x@%s",
738 (long)time(NULL), getpid(), ++serialnum, config.c_fqdn);
739 msg->cm_fields['I'] = strdoop(buf);
742 * FIXME ... right now we're just sending a bounce; we really want to
743 * include the text of the bounced message.
745 if (msg->cm_fields['M'] != NULL) {
746 phree(msg->cm_fields['M']);
748 msg->cm_fields['M'] = strdoop(reason);
749 msg->cm_format_type = 0;
752 * Turn the message around
754 if (msg->cm_fields['R'] == NULL) {
755 phree(msg->cm_fields['R']);
758 if (msg->cm_fields['D'] == NULL) {
759 phree(msg->cm_fields['D']);
762 msg->cm_fields['R'] = msg->cm_fields['A'];
763 msg->cm_fields['D'] = msg->cm_fields['N'];
764 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
765 msg->cm_fields['N'] = strdoop(config.c_nodename);
767 if (!strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
768 phree(msg->cm_fields['D']);
772 * If this is a bounce of a bounce, send it to the Aide> room
773 * instead of looping around forever
775 if (msg->cm_fields['D'] == NULL) if (msg->cm_fields['R'] != NULL)
776 if (!strcasecmp(msg->cm_fields['R'], BOUNCESOURCE)) {
777 phree(msg->cm_fields['R']);
778 if (msg->cm_fields['C'] != NULL) {
779 phree(msg->cm_fields['C']);
781 msg->cm_fields['C'] = strdoop(AIDEROOM);
784 /* prepend our node to the path */
785 if (msg->cm_fields['P'] != NULL) {
786 oldpath = msg->cm_fields['P'];
787 msg->cm_fields['P'] = NULL;
790 oldpath = strdoop("unknown_user");
792 msg->cm_fields['P'] = mallok(strlen(oldpath) + SIZ);
793 sprintf(msg->cm_fields['P'], "%s!%s", config.c_nodename, oldpath);
796 /* serialize the message */
797 serialize_message(&sermsg, msg);
800 sprintf(filename, "./network/spoolin/bounce.%04x.%04x",
801 getpid(), serialnum);
803 fp = fopen(filename, "ab");
810 CtdlFreeMessage(msg);
811 lprintf(9, "leaving network_bounce()\n");
818 * Process a buffer containing a single message from a single file
819 * from the inbound queue
821 void network_process_buffer(char *buffer, long size) {
822 struct CtdlMessage *msg;
827 struct usersupp tempUS;
829 char target_room[ROOMNAMELEN];
830 struct ser_ret sermsg;
831 char *oldpath = NULL;
836 /* Set default target room to trash */
837 strcpy(target_room, TWITROOM);
839 /* Load the message into memory */
840 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
841 memset(msg, 0, sizeof(struct CtdlMessage));
842 msg->cm_magic = CTDLMESSAGE_MAGIC;
843 msg->cm_anon_type = buffer[1];
844 msg->cm_format_type = buffer[2];
846 for (pos = 3; pos < size; ++pos) {
848 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
849 pos = pos + strlen(&buffer[(int)pos]);
852 /* Check for message routing */
853 if (msg->cm_fields['D'] != NULL) {
854 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
856 /* route the message */
857 if (is_valid_node(NULL, NULL,
858 msg->cm_fields['D']) == 0) {
860 /* prepend our node to the path */
861 if (msg->cm_fields['P'] != NULL) {
862 oldpath = msg->cm_fields['P'];
863 msg->cm_fields['P'] = NULL;
866 oldpath = strdoop("unknown_user");
868 msg->cm_fields['P'] =
869 mallok(strlen(oldpath) + SIZ);
870 sprintf(msg->cm_fields['P'], "%s!%s",
871 config.c_nodename, oldpath);
874 /* serialize the message */
875 serialize_message(&sermsg, msg);
879 "./network/spoolout/%s",
880 msg->cm_fields['D']);
881 fp = fopen(filename, "ab");
888 CtdlFreeMessage(msg);
892 else { /* invalid destination node name */
895 "A message you sent could not be delivered due to an invalid destination node"
896 " name. Please check the address and try sending the message again.\n");
905 * Check to see if we already have a copy of this message
907 if (network_usetable(UT_INSERT, msg) != 0) {
908 sprintf(buf, "Loopzapper rejected message <%s>\n",
909 msg->cm_fields['I']);
911 CtdlFreeMessage(msg);
916 /* Learn network topology from the path */
917 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
918 network_learn_topology(msg->cm_fields['N'],
919 msg->cm_fields['P']);
922 /* Does it have a recipient? If so, validate it... */
923 if (msg->cm_fields['R'] != NULL) {
925 safestrncpy(recp, msg->cm_fields['R'], sizeof(recp));
927 e = alias(recp); /* alias and mail type */
928 if ((recp[0] == 0) || (e == MES_ERROR)) {
931 "A message you sent could not be delivered due to an invalid address.\n"
932 "Please check the address and try sending the message again.\n");
937 else if (e == MES_LOCAL) {
938 a = getuser(&tempUS, recp);
942 "A message you sent could not be delivered because the user does not exist\n"
943 "on this system. Please check the address and try again.\n");
949 MailboxName(target_room, &tempUS, MAILROOM);
954 else if (msg->cm_fields['C'] != NULL) {
955 safestrncpy(target_room,
960 else if (msg->cm_fields['O'] != NULL) {
961 safestrncpy(target_room,
966 /* save the message into a room */
967 msg->cm_flags = CM_SKIP_HOOKS;
968 CtdlSaveMsg(msg, "", target_room, 0);
969 CtdlFreeMessage(msg);
974 * Process a single message from a single file from the inbound queue
976 void network_process_message(FILE *fp, long msgstart, long msgend) {
981 hold_pos = ftell(fp);
982 size = msgend - msgstart + 1;
983 buffer = mallok(size);
984 if (buffer != NULL) {
985 fseek(fp, msgstart, SEEK_SET);
986 fread(buffer, size, 1, fp);
987 network_process_buffer(buffer, size);
991 fseek(fp, hold_pos, SEEK_SET);
996 * Process a single file from the inbound queue
998 void network_process_file(char *filename) {
1000 long msgstart = (-1L);
1001 long msgend = (-1L);
1005 lprintf(7, "network: processing <%s>\n", filename);
1007 fp = fopen(filename, "rb");
1009 lprintf(5, "Error opening %s: %s\n",
1010 filename, strerror(errno));
1014 /* Look for messages in the data stream and break them out */
1015 while (ch = getc(fp), ch >= 0) {
1018 if (msgstart >= 0L) {
1019 msgend = msgcur - 1;
1020 network_process_message(fp, msgstart, msgend);
1028 msgend = msgcur - 1;
1029 if (msgstart >= 0L) {
1030 network_process_message(fp, msgstart, msgend);
1039 * Process anything in the inbound queue
1041 void network_do_spoolin(void) {
1046 dp = opendir("./network/spoolin");
1047 if (dp == NULL) return;
1049 while (d = readdir(dp), d != NULL) {
1050 sprintf(filename, "./network/spoolin/%s", d->d_name);
1051 network_process_file(filename);
1063 * receive network spool from the remote system
1065 void receive_spool(int sock, char *remote_nodename) {
1067 long bytes_received;
1069 static char pbuf[IGNET_PACKET_SIZE];
1070 char tempfilename[PATH_MAX];
1074 strcpy(tempfilename, tmpnam(NULL));
1075 if (sock_puts(sock, "NDOP") < 0) return;
1076 if (sock_gets(sock, buf) < 0) return;
1077 lprintf(9, "<%s\n", buf);
1078 if (buf[0] != '2') {
1081 download_len = extract_long(&buf[4], 0);
1083 bytes_received = 0L;
1084 fp = fopen(tempfilename, "w");
1086 lprintf(9, "cannot open download file locally: %s\n",
1091 while (bytes_received < download_len) {
1092 sprintf(buf, "READ %ld|%ld",
1094 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1095 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1096 if (sock_puts(sock, buf) < 0) {
1098 unlink(tempfilename);
1101 if (sock_gets(sock, buf) < 0) {
1103 unlink(tempfilename);
1106 if (buf[0] == '6') {
1107 plen = extract_long(&buf[4], 0);
1108 if (sock_read(sock, pbuf, plen) < 0) {
1110 unlink(tempfilename);
1113 fwrite((char *) pbuf, plen, 1, fp);
1114 bytes_received = bytes_received + plen;
1119 if (sock_puts(sock, "CLOS") < 0) {
1120 unlink(tempfilename);
1123 if (sock_gets(sock, buf) < 0) {
1124 unlink(tempfilename);
1127 lprintf(9, "%s\n", buf);
1128 sprintf(buf, "mv %s ./network/spoolin/%s.%ld",
1129 tempfilename, remote_nodename, (long) getpid());
1136 * transmit network spool to the remote system
1138 void transmit_spool(int sock, char *remote_nodename)
1143 long bytes_to_write, thisblock;
1147 if (sock_puts(sock, "NUOP") < 0) return;
1148 if (sock_gets(sock, buf) < 0) return;
1149 lprintf(9, "<%s\n", buf);
1150 if (buf[0] != '2') {
1154 sprintf(sfname, "./network/spoolout/%s", remote_nodename);
1155 fd = open(sfname, O_RDONLY);
1157 if (errno == ENOENT) {
1158 lprintf(9, "Nothing to send.\n");
1160 lprintf(5, "cannot open upload file locally: %s\n",
1165 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1166 bytes_to_write = plen;
1167 while (bytes_to_write > 0L) {
1168 sprintf(buf, "WRIT %ld", bytes_to_write);
1169 if (sock_puts(sock, buf) < 0) {
1173 if (sock_gets(sock, buf) < 0) {
1177 thisblock = atol(&buf[4]);
1178 if (buf[0] == '7') {
1179 if (sock_write(sock, pbuf,
1180 (int) thisblock) < 0) {
1184 bytes_to_write = bytes_to_write - thisblock;
1193 if (sock_puts(sock, "UCLS 1") < 0) return;
1194 if (sock_gets(sock, buf) < 0) return;
1195 lprintf(9, "<%s\n", buf);
1196 if (buf[0] == '2') {
1204 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1206 void network_poll_node(char *node, char *secret, char *host, char *port) {
1210 if (network_talking_to(node, NTT_CHECK)) return;
1211 network_talking_to(node, NTT_ADD);
1212 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1214 sock = sock_connect(host, port, "tcp");
1216 lprintf(7, "Could not connect: %s\n", strerror(errno));
1217 network_talking_to(node, NTT_REMOVE);
1221 lprintf(9, "Connected!\n");
1223 /* Read the server greeting */
1224 if (sock_gets(sock, buf) < 0) goto bail;
1225 lprintf(9, ">%s\n", buf);
1227 /* Identify ourselves */
1228 sprintf(buf, "NETP %s|%s", config.c_nodename, secret);
1229 lprintf(9, "<%s\n", buf);
1230 if (sock_puts(sock, buf) <0) goto bail;
1231 if (sock_gets(sock, buf) < 0) goto bail;
1232 lprintf(9, ">%s\n", buf);
1233 if (buf[0] != '2') goto bail;
1235 /* At this point we are authenticated. */
1236 receive_spool(sock, node);
1237 transmit_spool(sock, node);
1239 sock_puts(sock, "QUIT");
1240 bail: sock_close(sock);
1241 network_talking_to(node, NTT_REMOVE);
1247 * Poll other Citadel nodes and transfer inbound/outbound network data.
1249 void network_poll_other_citadel_nodes(void) {
1250 char *ignetcfg = NULL;
1258 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1259 if (ignetcfg == NULL) return; /* no nodes defined */
1261 /* Use the string tokenizer to grab one line at a time */
1262 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1263 extract_token(linebuf, ignetcfg, i, '\n');
1264 extract(node, linebuf, 0);
1265 extract(secret, linebuf, 1);
1266 extract(host, linebuf, 2);
1267 extract(port, linebuf, 3);
1268 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1269 && (strlen(host) > 0) && strlen(port) > 0) {
1270 network_poll_node(node, secret, host, port);
1284 * network_do_queue()
1286 * Run through the rooms doing various types of network stuff.
1288 void network_do_queue(void) {
1289 static int doing_queue = 0;
1290 static time_t last_run = 0L;
1291 struct RoomProcList *ptr;
1294 * Run no more frequently than once every n seconds
1296 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
1299 * This is a simple concurrency check to make sure only one queue run
1300 * is done at a time. We could do this with a mutex, but since we
1301 * don't really require extremely fine granularity here, we'll do it
1302 * with a static variable instead.
1304 if (doing_queue) return;
1306 last_run = time(NULL);
1309 * Poll other Citadel nodes.
1311 network_poll_other_citadel_nodes();
1314 * Load the network map and use table into memory.
1317 network_usetable(UT_LOAD, NULL);
1320 * Go ahead and run the queue
1322 lprintf(7, "network: loading outbound queue\n");
1323 ForEachRoom(network_queue_room, NULL);
1325 lprintf(7, "network: running outbound queue\n");
1326 while (rplist != NULL) {
1327 network_spoolout_room(rplist->name);
1329 rplist = rplist->next;
1333 lprintf(7, "network: processing inbound queue\n");
1334 network_do_spoolin();
1336 /* Save the usetable and network map back to disk */
1337 network_usetable(UT_SAVE, NULL);
1338 write_network_map();
1340 lprintf(7, "network: queue run completed\n");
1347 * cmd_netp() - authenticate to the server as another Citadel node polling
1348 * for network traffic
1350 void cmd_netp(char *cmdbuf)
1358 extract(node, cmdbuf, 0);
1359 extract(pass, cmdbuf, 1);
1361 if (is_valid_node(nexthop, secret, node) != 0) {
1362 cprintf("%d authentication failed\n", ERROR);
1366 if (strcasecmp(pass, secret)) {
1367 cprintf("%d authentication failed\n", ERROR);
1371 if (network_talking_to(node, NTT_CHECK)) {
1372 cprintf("%d Already talking to %s right now\n", ERROR);
1376 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1377 network_talking_to(node, NTT_ADD);
1378 cprintf("%d authenticated as network node '%s'\n", OK,
1385 * Module entry point
1387 char *Dynamic_Module_Init(void)
1389 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1390 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
1391 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1392 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);