4 * This module handles shared rooms, inter-Citadel mail, and outbound
5 * mailing list processing.
7 * Copyright (C) 2000-2002 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 * Check 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(struct CtdlMessage *msg) {
87 struct cdbdata *cdbut;
90 /* Bail out if we can't generate a message ID */
94 if (msg->cm_fields['I'] == NULL) {
97 if (strlen(msg->cm_fields['I']) == 0) {
101 /* Generate the message ID */
102 strcpy(msgid, msg->cm_fields['I']);
103 if (haschar(msgid, '@') == 0) {
105 if (msg->cm_fields['N'] != NULL) {
106 strcat(msgid, msg->cm_fields['N']);
113 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
119 /* If we got to this point, it's unique: add it. */
120 strcpy(ut.ut_msgid, msgid);
121 ut.ut_timestamp = time(NULL);
122 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
123 &ut, sizeof(struct UseTable) );
129 * Read the network map from its configuration file into memory.
131 void read_network_map(void) {
132 char *serialized_map = NULL;
135 struct NetMap *nmptr;
137 serialized_map = CtdlGetSysConfig(IGNETMAP);
138 if (serialized_map == NULL) return; /* if null, no entries */
140 /* Use the string tokenizer to grab one line at a time */
141 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
142 extract_token(buf, serialized_map, i, '\n');
143 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
144 extract(nmptr->nodename, buf, 0);
145 nmptr->lastcontact = extract_long(buf, 1);
146 extract(nmptr->nexthop, buf, 2);
147 nmptr->next = the_netmap;
151 phree(serialized_map);
156 * Write the network map from memory back to the configuration file.
158 void write_network_map(void) {
159 char *serialized_map = NULL;
160 struct NetMap *nmptr;
162 serialized_map = strdoop("");
164 if (the_netmap != NULL) {
165 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
166 serialized_map = reallok(serialized_map,
167 (strlen(serialized_map)+SIZ) );
168 if (strlen(nmptr->nodename) > 0) {
169 sprintf(&serialized_map[strlen(serialized_map)],
172 (long)nmptr->lastcontact,
178 CtdlPutSysConfig(IGNETMAP, serialized_map);
179 phree(serialized_map);
181 /* Now free the list */
182 while (the_netmap != NULL) {
183 nmptr = the_netmap->next;
192 * Check the network map and determine whether the supplied node name is
193 * valid. If it is not a neighbor node, supply the name of a neighbor node
194 * which is the next hop. If it *is* a neighbor node, we also fill in the
197 int is_valid_node(char *nexthop, char *secret, char *node) {
198 char *ignetcfg = NULL;
203 struct NetMap *nmptr;
210 * First try the neighbor nodes
212 ignetcfg = CtdlGetSysConfig(IGNETCFG);
213 if (ignetcfg == NULL) {
214 if (nexthop != NULL) {
221 if (nexthop != NULL) {
225 /* Use the string tokenizer to grab one line at a time */
226 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
227 extract_token(linebuf, ignetcfg, i, '\n');
228 extract(buf, linebuf, 0);
229 if (!strcasecmp(buf, node)) {
230 if (nexthop != NULL) {
233 if (secret != NULL) {
234 extract(secret, linebuf, 1);
242 return(retval); /* yup, it's a direct neighbor */
246 * If we get to this point we have to see if we know the next hop
248 if (the_netmap != NULL) {
249 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
250 if (!strcasecmp(nmptr->nodename, node)) {
251 if (nexthop != NULL) {
252 strcpy(nexthop, nmptr->nexthop);
260 * If we get to this point, the supplied node name is bogus.
262 lprintf(5, "Invalid node name <%s>\n", node);
270 void cmd_gnet(char *argbuf) {
275 if (CtdlAccessCheck(ac_room_aide)) return;
276 assoc_file_name(filename, &CC->quickroom, "netconfigs");
277 cprintf("%d Network settings for room #%ld <%s>\n",
279 CC->quickroom.QRnumber, CC->quickroom.QRname);
281 fp = fopen(filename, "r");
283 while (fgets(buf, sizeof buf, fp) != NULL) {
284 buf[strlen(buf)-1] = 0;
285 cprintf("%s\n", buf);
294 void cmd_snet(char *argbuf) {
295 char tempfilename[SIZ];
300 if (CtdlAccessCheck(ac_room_aide)) return;
301 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
302 assoc_file_name(filename, &CC->quickroom, "netconfigs");
304 fp = fopen(tempfilename, "w");
306 cprintf("%d Cannot open %s: %s\n",
307 ERROR+INTERNAL_ERROR,
312 cprintf("%d %s\n", SEND_LISTING, tempfilename);
313 while (client_gets(buf), strcmp(buf, "000")) {
314 fprintf(fp, "%s\n", buf);
318 /* Now copy the temp file to its permanent location
319 * (We use /bin/mv instead of link() because they may be on
320 * different filesystems)
323 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
330 * Spools out one message from the list.
332 void network_spool_msg(long msgnum, void *userdata) {
333 struct SpoolControl *sc;
334 struct namelist *nptr;
338 char *newpath = NULL;
339 size_t instr_len = SIZ;
340 struct CtdlMessage *msg = NULL;
341 struct CtdlMessage *imsg;
342 struct ser_ret sermsg;
348 int delete_after_send = 0; /* Set to 1 to delete after spooling */
350 sc = (struct SpoolControl *)userdata;
353 * Process mailing list recipients
355 if (sc->listrecps != NULL) {
357 /* First, copy it to the spoolout room */
358 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
359 if (err != 0) return;
362 * Figure out how big a buffer we need to allocate
364 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
365 instr_len = instr_len + strlen(nptr->name);
371 lprintf(9, "Generating delivery instructions\n");
372 instr = mallok(instr_len);
374 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
379 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
380 "bounceto|postmaster@%s\n" ,
381 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
383 /* Generate delivery instructions for each recipient */
384 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
385 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
390 * Generate a message from the instructions
392 imsg = mallok(sizeof(struct CtdlMessage));
393 memset(imsg, 0, sizeof(struct CtdlMessage));
394 imsg->cm_magic = CTDLMESSAGE_MAGIC;
395 imsg->cm_anon_type = MES_NORMAL;
396 imsg->cm_format_type = FMT_RFC822;
397 imsg->cm_fields['A'] = strdoop("Citadel");
398 imsg->cm_fields['M'] = instr;
400 /* Save delivery instructions in spoolout room */
401 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
402 CtdlFreeMessage(imsg);
406 * Process IGnet push shares
408 if (sc->ignet_push_shares != NULL) {
410 msg = CtdlFetchMessage(msgnum);
413 /* Prepend our node name to the Path field whenever
414 * sending a message to another IGnet node
416 if (msg->cm_fields['P'] == NULL) {
417 msg->cm_fields['P'] = strdoop("username");
419 newpath = mallok(strlen(msg->cm_fields['P']) +
420 strlen(config.c_nodename) + 2);
421 sprintf(newpath, "%s!%s", config.c_nodename,
422 msg->cm_fields['P']);
423 phree(msg->cm_fields['P']);
424 msg->cm_fields['P'] = newpath;
427 * Force the message to appear in the correct room
428 * on the far end by setting the C field correctly
430 if (msg->cm_fields['C'] != NULL) {
431 phree(msg->cm_fields['C']);
433 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
436 * Determine if this message is set to be deleted
437 * after sending out on the network
439 if (msg->cm_fields['S'] != NULL) {
440 if (!strcasecmp(msg->cm_fields['S'],
442 delete_after_send = 1;
447 * Now serialize it for transmission
449 serialize_message(&sermsg, msg);
451 /* Now send it to every node */
452 for (nptr = sc->ignet_push_shares; nptr != NULL;
457 /* Check for valid node name */
458 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
459 lprintf(3, "Invalid node <%s>\n",
464 /* Check for split horizon */
465 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
466 bang = num_tokens(msg->cm_fields['P'], '!');
467 if (bang > 1) for (i=0; i<(bang-1); ++i) {
468 extract_token(buf, msg->cm_fields['P'],
470 if (!strcasecmp(buf, nptr->name)) {
475 /* Send the message */
478 "./network/spoolout/%s",
480 fp = fopen(filename, "ab");
489 CtdlFreeMessage(msg);
493 /* update lastsent */
494 sc->lastsent = msgnum;
496 /* Delete this message if delete-after-send is set */
497 if (delete_after_send) {
498 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
507 * Batch up and send all outbound traffic from the current room
509 void network_spoolout_room(char *room_to_spool) {
514 struct SpoolControl sc;
515 /* struct namelist *digestrecps = NULL; */
516 struct namelist *nptr;
518 lprintf(7, "Spooling <%s>\n", room_to_spool);
519 if (getroom(&CC->quickroom, room_to_spool) != 0) {
520 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
524 memset(&sc, 0, sizeof(struct SpoolControl));
525 assoc_file_name(filename, &CC->quickroom, "netconfigs");
527 fp = fopen(filename, "r");
529 lprintf(7, "Outbound batch processing skipped for <%s>\n",
530 CC->quickroom.QRname);
534 lprintf(5, "Outbound batch processing started for <%s>\n",
535 CC->quickroom.QRname);
537 while (fgets(buf, sizeof buf, fp) != NULL) {
538 buf[strlen(buf)-1] = 0;
540 extract(instr, buf, 0);
541 if (!strcasecmp(instr, "lastsent")) {
542 sc.lastsent = extract_long(buf, 1);
544 else if (!strcasecmp(instr, "listrecp")) {
545 nptr = (struct namelist *)
546 mallok(sizeof(struct namelist));
547 nptr->next = sc.listrecps;
548 extract(nptr->name, buf, 1);
551 else if (!strcasecmp(instr, "ignet_push_share")) {
552 nptr = (struct namelist *)
553 mallok(sizeof(struct namelist));
554 nptr->next = sc.ignet_push_shares;
555 extract(nptr->name, buf, 1);
556 sc.ignet_push_shares = nptr;
564 /* Do something useful */
565 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
566 network_spool_msg, &sc);
569 /* Now rewrite the config file */
570 fp = fopen(filename, "w");
572 lprintf(1, "ERROR: cannot open %s: %s\n",
573 filename, strerror(errno));
576 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
578 /* Write out the listrecps while freeing from memory at the
579 * same time. Am I clever or what? :)
581 while (sc.listrecps != NULL) {
582 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
583 nptr = sc.listrecps->next;
587 while (sc.ignet_push_shares != NULL) {
588 fprintf(fp, "ignet_push_share|%s\n",
589 sc.ignet_push_shares->name);
590 nptr = sc.ignet_push_shares->next;
591 phree(sc.ignet_push_shares);
592 sc.ignet_push_shares = nptr;
598 lprintf(5, "Outbound batch processing finished for <%s>\n",
599 CC->quickroom.QRname);
604 * Batch up and send all outbound traffic from the current room
606 void network_queue_room(struct quickroom *qrbuf, void *data) {
607 struct RoomProcList *ptr;
609 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
610 if (ptr == NULL) return;
612 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
619 * Learn topology from path fields
621 void network_learn_topology(char *node, char *path) {
623 struct NetMap *nmptr;
627 if (num_tokens(path, '!') < 3) return;
628 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
629 if (!strcasecmp(nmptr->nodename, node)) {
630 extract_token(nmptr->nexthop, path, 0, '!');
631 nmptr->lastcontact = time(NULL);
636 /* If we got here then it's not in the map, so add it. */
637 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
638 strcpy(nmptr->nodename, node);
639 nmptr->lastcontact = time(NULL);
640 extract_token(nmptr->nexthop, path, 0, '!');
641 nmptr->next = the_netmap;
649 * Bounce a message back to the sender
651 void network_bounce(struct CtdlMessage *msg, char *reason) {
652 char *oldpath = NULL;
654 char bouncesource[SIZ];
656 struct recptypes *valid = NULL;
657 char force_room[ROOMNAMELEN];
658 static int serialnum = 0;
660 lprintf(9, "entering network_bounce()\n");
662 if (msg == NULL) return;
664 sprintf(bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
667 * Give it a fresh message ID
669 if (msg->cm_fields['I'] != NULL) {
670 phree(msg->cm_fields['I']);
672 sprintf(buf, "%ld.%04x.%04x@%s",
673 (long)time(NULL), getpid(), ++serialnum, config.c_fqdn);
674 msg->cm_fields['I'] = strdoop(buf);
677 * FIXME ... right now we're just sending a bounce; we really want to
678 * include the text of the bounced message.
680 if (msg->cm_fields['M'] != NULL) {
681 phree(msg->cm_fields['M']);
683 msg->cm_fields['M'] = strdoop(reason);
684 msg->cm_format_type = 0;
687 * Turn the message around
689 if (msg->cm_fields['R'] == NULL) {
690 phree(msg->cm_fields['R']);
693 if (msg->cm_fields['D'] == NULL) {
694 phree(msg->cm_fields['D']);
697 snprintf(recipient, sizeof recipient, "%s@%s",
698 msg->cm_fields['A'], msg->cm_fields['N']);
700 if (msg->cm_fields['A'] == NULL) {
701 phree(msg->cm_fields['A']);
704 if (msg->cm_fields['N'] == NULL) {
705 phree(msg->cm_fields['N']);
708 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
709 msg->cm_fields['N'] = strdoop(config.c_nodename);
712 /* prepend our node to the path */
713 if (msg->cm_fields['P'] != NULL) {
714 oldpath = msg->cm_fields['P'];
715 msg->cm_fields['P'] = NULL;
718 oldpath = strdoop("unknown_user");
720 msg->cm_fields['P'] = mallok(strlen(oldpath) + SIZ);
721 sprintf(msg->cm_fields['P'], "%s!%s", config.c_nodename, oldpath);
724 /* Now submit the message */
725 valid = validate_recipients(recipient);
726 if (valid != NULL) if (valid->num_error > 0) {
730 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
731 strcpy(force_room, AIDEROOM);
734 strcpy(force_room, "");
736 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
737 strcpy(force_room, AIDEROOM);
739 CtdlSubmitMsg(msg, valid, force_room);
742 if (valid != NULL) phree(valid);
743 CtdlFreeMessage(msg);
744 lprintf(9, "leaving network_bounce()\n");
751 * Process a buffer containing a single message from a single file
752 * from the inbound queue
754 void network_process_buffer(char *buffer, long size) {
755 struct CtdlMessage *msg;
758 struct recptypes *recp = NULL;
759 char target_room[ROOMNAMELEN];
760 struct ser_ret sermsg;
761 char *oldpath = NULL;
766 /* Set default target room to trash */
767 strcpy(target_room, TWITROOM);
769 /* Load the message into memory */
770 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
771 memset(msg, 0, sizeof(struct CtdlMessage));
772 msg->cm_magic = CTDLMESSAGE_MAGIC;
773 msg->cm_anon_type = buffer[1];
774 msg->cm_format_type = buffer[2];
776 for (pos = 3; pos < size; ++pos) {
778 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
779 pos = pos + strlen(&buffer[(int)pos]);
782 /* Check for message routing */
783 if (msg->cm_fields['D'] != NULL) {
784 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
786 /* route the message */
787 if (is_valid_node(NULL, NULL,
788 msg->cm_fields['D']) == 0) {
790 /* prepend our node to the path */
791 if (msg->cm_fields['P'] != NULL) {
792 oldpath = msg->cm_fields['P'];
793 msg->cm_fields['P'] = NULL;
796 oldpath = strdoop("unknown_user");
798 msg->cm_fields['P'] =
799 mallok(strlen(oldpath) + SIZ);
800 sprintf(msg->cm_fields['P'], "%s!%s",
801 config.c_nodename, oldpath);
804 /* serialize the message */
805 serialize_message(&sermsg, msg);
809 "./network/spoolout/%s",
810 msg->cm_fields['D']);
811 fp = fopen(filename, "ab");
818 CtdlFreeMessage(msg);
822 else { /* invalid destination node name */
825 "A message you sent could not be delivered due to an invalid destination node"
826 " name. Please check the address and try sending the message again.\n");
835 * Check to see if we already have a copy of this message
837 if (network_usetable(msg) != 0) {
839 "Loopzapper rejected message <%s> "
840 "from <%s> in <%s> @ <%s>\n",
841 ((msg->cm_fields['I']!=NULL)?(msg->cm_fields['I']):""),
842 ((msg->cm_fields['A']!=NULL)?(msg->cm_fields['A']):""),
843 ((msg->cm_fields['O']!=NULL)?(msg->cm_fields['O']):""),
844 ((msg->cm_fields['N']!=NULL)?(msg->cm_fields['N']):"")
847 CtdlFreeMessage(msg);
852 /* Learn network topology from the path */
853 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
854 network_learn_topology(msg->cm_fields['N'],
855 msg->cm_fields['P']);
858 /* Does it have a recipient? If so, validate it... */
859 if (msg->cm_fields['R'] != NULL) {
860 recp = validate_recipients(msg->cm_fields['R']);
861 if (recp != NULL) if (recp->num_error > 0) {
863 "A message you sent could not be delivered due to an invalid address.\n"
864 "Please check the address and try sending the message again.\n");
869 strcpy(target_room, ""); /* no target room if mail */
872 else if (msg->cm_fields['C'] != NULL) {
873 safestrncpy(target_room,
878 else if (msg->cm_fields['O'] != NULL) {
879 safestrncpy(target_room,
884 /* Strip out fields that are only relevant during transit */
885 if (msg->cm_fields['D'] != NULL) {
886 phree(msg->cm_fields['D']);
887 msg->cm_fields['D'] = NULL;
889 if (msg->cm_fields['C'] != NULL) {
890 phree(msg->cm_fields['C']);
891 msg->cm_fields['C'] = NULL;
894 /* save the message into a room */
895 if (PerformNetprocHooks(msg, target_room) == 0) {
896 msg->cm_flags = CM_SKIP_HOOKS;
897 CtdlSubmitMsg(msg, recp, target_room);
899 CtdlFreeMessage(msg);
905 * Process a single message from a single file from the inbound queue
907 void network_process_message(FILE *fp, long msgstart, long msgend) {
912 hold_pos = ftell(fp);
913 size = msgend - msgstart + 1;
914 buffer = mallok(size);
915 if (buffer != NULL) {
916 fseek(fp, msgstart, SEEK_SET);
917 fread(buffer, size, 1, fp);
918 network_process_buffer(buffer, size);
922 fseek(fp, hold_pos, SEEK_SET);
927 * Process a single file from the inbound queue
929 void network_process_file(char *filename) {
931 long msgstart = (-1L);
936 lprintf(7, "network: processing <%s>\n", filename);
938 fp = fopen(filename, "rb");
940 lprintf(5, "Error opening %s: %s\n",
941 filename, strerror(errno));
945 /* Look for messages in the data stream and break them out */
946 while (ch = getc(fp), ch >= 0) {
949 if (msgstart >= 0L) {
951 network_process_message(fp, msgstart, msgend);
960 if (msgstart >= 0L) {
961 network_process_message(fp, msgstart, msgend);
970 * Process anything in the inbound queue
972 void network_do_spoolin(void) {
977 dp = opendir("./network/spoolin");
978 if (dp == NULL) return;
980 while (d = readdir(dp), d != NULL) {
981 sprintf(filename, "./network/spoolin/%s", d->d_name);
982 network_process_file(filename);
994 * receive network spool from the remote system
996 void receive_spool(int sock, char *remote_nodename) {
1000 static char pbuf[IGNET_PACKET_SIZE];
1001 char tempfilename[PATH_MAX];
1005 strcpy(tempfilename, tmpnam(NULL));
1006 if (sock_puts(sock, "NDOP") < 0) return;
1007 if (sock_gets(sock, buf) < 0) return;
1008 lprintf(9, "<%s\n", buf);
1009 if (buf[0] != '2') {
1012 download_len = extract_long(&buf[4], 0);
1014 bytes_received = 0L;
1015 fp = fopen(tempfilename, "w");
1017 lprintf(9, "cannot open download file locally: %s\n",
1022 while (bytes_received < download_len) {
1023 sprintf(buf, "READ %ld|%ld",
1025 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1026 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1027 if (sock_puts(sock, buf) < 0) {
1029 unlink(tempfilename);
1032 if (sock_gets(sock, buf) < 0) {
1034 unlink(tempfilename);
1037 if (buf[0] == '6') {
1038 plen = extract_long(&buf[4], 0);
1039 if (sock_read(sock, pbuf, plen) < 0) {
1041 unlink(tempfilename);
1044 fwrite((char *) pbuf, plen, 1, fp);
1045 bytes_received = bytes_received + plen;
1050 if (sock_puts(sock, "CLOS") < 0) {
1051 unlink(tempfilename);
1054 if (sock_gets(sock, buf) < 0) {
1055 unlink(tempfilename);
1058 lprintf(9, "%s\n", buf);
1059 sprintf(buf, "mv %s ./network/spoolin/%s.%ld",
1060 tempfilename, remote_nodename, (long) getpid());
1067 * transmit network spool to the remote system
1069 void transmit_spool(int sock, char *remote_nodename)
1074 long bytes_to_write, thisblock;
1078 if (sock_puts(sock, "NUOP") < 0) return;
1079 if (sock_gets(sock, buf) < 0) return;
1080 lprintf(9, "<%s\n", buf);
1081 if (buf[0] != '2') {
1085 sprintf(sfname, "./network/spoolout/%s", remote_nodename);
1086 fd = open(sfname, O_RDONLY);
1088 if (errno == ENOENT) {
1089 lprintf(9, "Nothing to send.\n");
1091 lprintf(5, "cannot open upload file locally: %s\n",
1096 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1097 bytes_to_write = plen;
1098 while (bytes_to_write > 0L) {
1099 sprintf(buf, "WRIT %ld", bytes_to_write);
1100 if (sock_puts(sock, buf) < 0) {
1104 if (sock_gets(sock, buf) < 0) {
1108 thisblock = atol(&buf[4]);
1109 if (buf[0] == '7') {
1110 if (sock_write(sock, pbuf,
1111 (int) thisblock) < 0) {
1115 bytes_to_write = bytes_to_write - thisblock;
1124 if (sock_puts(sock, "UCLS 1") < 0) return;
1125 if (sock_gets(sock, buf) < 0) return;
1126 lprintf(9, "<%s\n", buf);
1127 if (buf[0] == '2') {
1135 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1137 void network_poll_node(char *node, char *secret, char *host, char *port) {
1141 if (network_talking_to(node, NTT_CHECK)) return;
1142 network_talking_to(node, NTT_ADD);
1143 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1145 sock = sock_connect(host, port, "tcp");
1147 lprintf(7, "Could not connect: %s\n", strerror(errno));
1148 network_talking_to(node, NTT_REMOVE);
1152 lprintf(9, "Connected!\n");
1154 /* Read the server greeting */
1155 if (sock_gets(sock, buf) < 0) goto bail;
1156 lprintf(9, ">%s\n", buf);
1158 /* Identify ourselves */
1159 sprintf(buf, "NETP %s|%s", config.c_nodename, secret);
1160 lprintf(9, "<%s\n", buf);
1161 if (sock_puts(sock, buf) <0) goto bail;
1162 if (sock_gets(sock, buf) < 0) goto bail;
1163 lprintf(9, ">%s\n", buf);
1164 if (buf[0] != '2') goto bail;
1166 /* At this point we are authenticated. */
1167 receive_spool(sock, node);
1168 transmit_spool(sock, node);
1170 sock_puts(sock, "QUIT");
1171 bail: sock_close(sock);
1172 network_talking_to(node, NTT_REMOVE);
1178 * Poll other Citadel nodes and transfer inbound/outbound network data.
1180 void network_poll_other_citadel_nodes(void) {
1181 char *ignetcfg = NULL;
1189 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1190 if (ignetcfg == NULL) return; /* no nodes defined */
1192 /* Use the string tokenizer to grab one line at a time */
1193 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1194 extract_token(linebuf, ignetcfg, i, '\n');
1195 extract(node, linebuf, 0);
1196 extract(secret, linebuf, 1);
1197 extract(host, linebuf, 2);
1198 extract(port, linebuf, 3);
1199 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1200 && (strlen(host) > 0) && strlen(port) > 0) {
1201 network_poll_node(node, secret, host, port);
1215 * network_do_queue()
1217 * Run through the rooms doing various types of network stuff.
1219 void network_do_queue(void) {
1220 static int doing_queue = 0;
1221 static time_t last_run = 0L;
1222 struct RoomProcList *ptr;
1225 * Run no more frequently than once every n seconds
1227 if ( (time(NULL) - last_run) < config.c_net_freq ) return;
1230 * This is a simple concurrency check to make sure only one queue run
1231 * is done at a time. We could do this with a mutex, but since we
1232 * don't really require extremely fine granularity here, we'll do it
1233 * with a static variable instead.
1235 if (doing_queue) return;
1237 last_run = time(NULL);
1240 * Poll other Citadel nodes.
1242 network_poll_other_citadel_nodes();
1245 * Load the network map into memory.
1250 * Go ahead and run the queue
1252 lprintf(7, "network: loading outbound queue\n");
1253 ForEachRoom(network_queue_room, NULL);
1255 lprintf(7, "network: running outbound queue\n");
1256 while (rplist != NULL) {
1257 network_spoolout_room(rplist->name);
1259 rplist = rplist->next;
1263 lprintf(7, "network: processing inbound queue\n");
1264 network_do_spoolin();
1266 /* Save the network map back to disk */
1267 write_network_map();
1269 lprintf(7, "network: queue run completed\n");
1276 * cmd_netp() - authenticate to the server as another Citadel node polling
1277 * for network traffic
1279 void cmd_netp(char *cmdbuf)
1287 extract(node, cmdbuf, 0);
1288 extract(pass, cmdbuf, 1);
1290 if (is_valid_node(nexthop, secret, node) != 0) {
1291 cprintf("%d authentication failed\n", ERROR);
1295 if (strcasecmp(pass, secret)) {
1296 cprintf("%d authentication failed\n", ERROR);
1300 if (network_talking_to(node, NTT_CHECK)) {
1301 cprintf("%d Already talking to %s right now\n", ERROR, node);
1305 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1306 network_talking_to(node, NTT_ADD);
1307 cprintf("%d authenticated as network node '%s'\n", OK,
1314 * Module entry point
1316 char *Dynamic_Module_Init(void)
1318 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1319 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1320 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1321 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);