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;
78 * Keep track of what messages to reject
80 struct FilterList *load_filter_list(void) {
81 char *serialized_list = NULL;
84 struct FilterList *newlist = NULL;
85 struct FilterList *nptr;
87 serialized_list = CtdlGetSysConfig(FILTERLIST);
88 if (serialized_list == NULL) return(NULL); /* if null, no entries */
90 /* Use the string tokenizer to grab one line at a time */
91 for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
92 extract_token(buf, serialized_list, i, '\n');
93 nptr = (struct FilterList *) mallok(sizeof(struct FilterList));
94 extract(nptr->fl_user, buf, 0);
95 striplt(nptr->fl_user);
96 extract(nptr->fl_room, buf, 1);
97 striplt(nptr->fl_room);
98 extract(nptr->fl_node, buf, 2);
99 striplt(nptr->fl_node);
101 /* Cowardly refuse to add an any/any/any entry that would
102 * end up filtering every single message.
104 if (strlen(nptr->fl_user) + strlen(nptr->fl_room)
105 + strlen(nptr->fl_node) == 0) {
109 nptr->next = newlist;
114 phree(serialized_list);
119 void free_filter_list(struct FilterList *fl) {
120 if (fl == NULL) return;
121 free_filter_list(fl->next);
128 * Check the use table. This is a list of messages which have recently
129 * arrived on the system. It is maintained and queried to prevent the same
130 * message from being entered into the database multiple times if it happens
131 * to arrive multiple times by accident.
133 int network_usetable(struct CtdlMessage *msg) {
136 struct cdbdata *cdbut;
139 /* Bail out if we can't generate a message ID */
143 if (msg->cm_fields['I'] == NULL) {
146 if (strlen(msg->cm_fields['I']) == 0) {
150 /* Generate the message ID */
151 strcpy(msgid, msg->cm_fields['I']);
152 if (haschar(msgid, '@') == 0) {
154 if (msg->cm_fields['N'] != NULL) {
155 strcat(msgid, msg->cm_fields['N']);
162 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
168 /* If we got to this point, it's unique: add it. */
169 strcpy(ut.ut_msgid, msgid);
170 ut.ut_timestamp = time(NULL);
171 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
172 &ut, sizeof(struct UseTable) );
178 * Read the network map from its configuration file into memory.
180 void read_network_map(void) {
181 char *serialized_map = NULL;
184 struct NetMap *nmptr;
186 serialized_map = CtdlGetSysConfig(IGNETMAP);
187 if (serialized_map == NULL) return; /* if null, no entries */
189 /* Use the string tokenizer to grab one line at a time */
190 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
191 extract_token(buf, serialized_map, i, '\n');
192 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
193 extract(nmptr->nodename, buf, 0);
194 nmptr->lastcontact = extract_long(buf, 1);
195 extract(nmptr->nexthop, buf, 2);
196 nmptr->next = the_netmap;
200 phree(serialized_map);
205 * Write the network map from memory back to the configuration file.
207 void write_network_map(void) {
208 char *serialized_map = NULL;
209 struct NetMap *nmptr;
211 serialized_map = strdoop("");
213 if (the_netmap != NULL) {
214 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
215 serialized_map = reallok(serialized_map,
216 (strlen(serialized_map)+SIZ) );
217 if (strlen(nmptr->nodename) > 0) {
218 sprintf(&serialized_map[strlen(serialized_map)],
221 (long)nmptr->lastcontact,
227 CtdlPutSysConfig(IGNETMAP, serialized_map);
228 phree(serialized_map);
230 /* Now free the list */
231 while (the_netmap != NULL) {
232 nmptr = the_netmap->next;
241 * Check the network map and determine whether the supplied node name is
242 * valid. If it is not a neighbor node, supply the name of a neighbor node
243 * which is the next hop. If it *is* a neighbor node, we also fill in the
246 int is_valid_node(char *nexthop, char *secret, char *node) {
247 char *ignetcfg = NULL;
252 struct NetMap *nmptr;
259 * First try the neighbor nodes
261 ignetcfg = CtdlGetSysConfig(IGNETCFG);
262 if (ignetcfg == NULL) {
263 if (nexthop != NULL) {
270 if (nexthop != NULL) {
274 /* Use the string tokenizer to grab one line at a time */
275 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
276 extract_token(linebuf, ignetcfg, i, '\n');
277 extract(buf, linebuf, 0);
278 if (!strcasecmp(buf, node)) {
279 if (nexthop != NULL) {
282 if (secret != NULL) {
283 extract(secret, linebuf, 1);
291 return(retval); /* yup, it's a direct neighbor */
295 * If we get to this point we have to see if we know the next hop
297 if (the_netmap != NULL) {
298 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
299 if (!strcasecmp(nmptr->nodename, node)) {
300 if (nexthop != NULL) {
301 strcpy(nexthop, nmptr->nexthop);
309 * If we get to this point, the supplied node name is bogus.
311 lprintf(5, "Invalid node name <%s>\n", node);
319 void cmd_gnet(char *argbuf) {
324 if (CtdlAccessCheck(ac_room_aide)) return;
325 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
326 cprintf("%d Network settings for room #%ld <%s>\n",
328 CC->quickroom.QRnumber, CC->quickroom.QRname);
330 fp = fopen(filename, "r");
332 while (fgets(buf, sizeof buf, fp) != NULL) {
333 buf[strlen(buf)-1] = 0;
334 cprintf("%s\n", buf);
343 void cmd_snet(char *argbuf) {
344 char tempfilename[SIZ];
349 if (CtdlAccessCheck(ac_room_aide)) return;
350 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
351 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
353 fp = fopen(tempfilename, "w");
355 cprintf("%d Cannot open %s: %s\n",
356 ERROR+INTERNAL_ERROR,
361 cprintf("%d %s\n", SEND_LISTING, tempfilename);
362 while (client_gets(buf), strcmp(buf, "000")) {
363 fprintf(fp, "%s\n", buf);
367 /* Now copy the temp file to its permanent location
368 * (We use /bin/mv instead of link() because they may be on
369 * different filesystems)
372 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
379 * Spools out one message from the list.
381 void network_spool_msg(long msgnum, void *userdata) {
382 struct SpoolControl *sc;
383 struct namelist *nptr;
387 char *newpath = NULL;
388 size_t instr_len = SIZ;
389 struct CtdlMessage *msg = NULL;
390 struct CtdlMessage *imsg;
391 struct ser_ret sermsg;
397 int delete_after_send = 0; /* Set to 1 to delete after spooling */
399 sc = (struct SpoolControl *)userdata;
402 * Process mailing list recipients
404 if (sc->listrecps != NULL) {
406 /* First, copy it to the spoolout room */
407 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
408 if (err != 0) return;
411 * Figure out how big a buffer we need to allocate
413 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
414 instr_len = instr_len + strlen(nptr->name);
420 lprintf(9, "Generating delivery instructions\n");
421 instr = mallok(instr_len);
423 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
428 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
429 "bounceto|postmaster@%s\n" ,
430 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
432 /* Generate delivery instructions for each recipient */
433 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
434 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
439 * Generate a message from the instructions
441 imsg = mallok(sizeof(struct CtdlMessage));
442 memset(imsg, 0, sizeof(struct CtdlMessage));
443 imsg->cm_magic = CTDLMESSAGE_MAGIC;
444 imsg->cm_anon_type = MES_NORMAL;
445 imsg->cm_format_type = FMT_RFC822;
446 imsg->cm_fields['A'] = strdoop("Citadel");
447 imsg->cm_fields['M'] = instr;
449 /* Save delivery instructions in spoolout room */
450 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
451 CtdlFreeMessage(imsg);
455 * Process IGnet push shares
457 if (sc->ignet_push_shares != NULL) {
459 msg = CtdlFetchMessage(msgnum);
462 /* Prepend our node name to the Path field whenever
463 * sending a message to another IGnet node
465 if (msg->cm_fields['P'] == NULL) {
466 msg->cm_fields['P'] = strdoop("username");
468 newpath = mallok(strlen(msg->cm_fields['P']) +
469 strlen(config.c_nodename) + 2);
470 sprintf(newpath, "%s!%s", config.c_nodename,
471 msg->cm_fields['P']);
472 phree(msg->cm_fields['P']);
473 msg->cm_fields['P'] = newpath;
476 * Force the message to appear in the correct room
477 * on the far end by setting the C field correctly
479 if (msg->cm_fields['C'] != NULL) {
480 phree(msg->cm_fields['C']);
482 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
485 * Determine if this message is set to be deleted
486 * after sending out on the network
488 if (msg->cm_fields['S'] != NULL) {
489 if (!strcasecmp(msg->cm_fields['S'],
491 delete_after_send = 1;
496 * Now serialize it for transmission
498 serialize_message(&sermsg, msg);
500 /* Now send it to every node */
501 for (nptr = sc->ignet_push_shares; nptr != NULL;
506 /* Check for valid node name */
507 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
508 lprintf(3, "Invalid node <%s>\n",
513 /* Check for split horizon */
514 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
515 bang = num_tokens(msg->cm_fields['P'], '!');
516 if (bang > 1) for (i=0; i<(bang-1); ++i) {
517 extract_token(buf, msg->cm_fields['P'],
519 if (!strcasecmp(buf, nptr->name)) {
524 /* Send the message */
527 "./network/spoolout/%s",
529 fp = fopen(filename, "ab");
538 CtdlFreeMessage(msg);
542 /* update lastsent */
543 sc->lastsent = msgnum;
545 /* Delete this message if delete-after-send is set */
546 if (delete_after_send) {
547 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
556 * Batch up and send all outbound traffic from the current room
558 void network_spoolout_room(char *room_to_spool) {
563 struct SpoolControl sc;
564 /* struct namelist *digestrecps = NULL; */
565 struct namelist *nptr;
567 lprintf(7, "Spooling <%s>\n", room_to_spool);
568 if (getroom(&CC->quickroom, room_to_spool) != 0) {
569 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
573 memset(&sc, 0, sizeof(struct SpoolControl));
574 assoc_file_name(filename, sizeof filename, &CC->quickroom, "netconfigs");
576 fp = fopen(filename, "r");
578 lprintf(7, "Outbound batch processing skipped for <%s>\n",
579 CC->quickroom.QRname);
583 lprintf(5, "Outbound batch processing started for <%s>\n",
584 CC->quickroom.QRname);
586 while (fgets(buf, sizeof buf, fp) != NULL) {
587 buf[strlen(buf)-1] = 0;
589 extract(instr, buf, 0);
590 if (!strcasecmp(instr, "lastsent")) {
591 sc.lastsent = extract_long(buf, 1);
593 else if (!strcasecmp(instr, "listrecp")) {
594 nptr = (struct namelist *)
595 mallok(sizeof(struct namelist));
596 nptr->next = sc.listrecps;
597 extract(nptr->name, buf, 1);
600 else if (!strcasecmp(instr, "ignet_push_share")) {
601 nptr = (struct namelist *)
602 mallok(sizeof(struct namelist));
603 nptr->next = sc.ignet_push_shares;
604 extract(nptr->name, buf, 1);
605 sc.ignet_push_shares = nptr;
613 /* Do something useful */
614 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
615 network_spool_msg, &sc);
618 /* Now rewrite the config file */
619 fp = fopen(filename, "w");
621 lprintf(1, "ERROR: cannot open %s: %s\n",
622 filename, strerror(errno));
625 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
627 /* Write out the listrecps while freeing from memory at the
628 * same time. Am I clever or what? :)
630 while (sc.listrecps != NULL) {
631 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
632 nptr = sc.listrecps->next;
636 while (sc.ignet_push_shares != NULL) {
637 fprintf(fp, "ignet_push_share|%s\n",
638 sc.ignet_push_shares->name);
639 nptr = sc.ignet_push_shares->next;
640 phree(sc.ignet_push_shares);
641 sc.ignet_push_shares = nptr;
647 lprintf(5, "Outbound batch processing finished for <%s>\n",
648 CC->quickroom.QRname);
653 * Batch up and send all outbound traffic from the current room
655 void network_queue_room(struct quickroom *qrbuf, void *data) {
656 struct RoomProcList *ptr;
658 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
659 if (ptr == NULL) return;
661 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
668 * Learn topology from path fields
670 void network_learn_topology(char *node, char *path) {
672 struct NetMap *nmptr;
676 if (num_tokens(path, '!') < 3) return;
677 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
678 if (!strcasecmp(nmptr->nodename, node)) {
679 extract_token(nmptr->nexthop, path, 0, '!');
680 nmptr->lastcontact = time(NULL);
685 /* If we got here then it's not in the map, so add it. */
686 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
687 strcpy(nmptr->nodename, node);
688 nmptr->lastcontact = time(NULL);
689 extract_token(nmptr->nexthop, path, 0, '!');
690 nmptr->next = the_netmap;
698 * Bounce a message back to the sender
700 void network_bounce(struct CtdlMessage *msg, char *reason) {
701 char *oldpath = NULL;
703 char bouncesource[SIZ];
705 struct recptypes *valid = NULL;
706 char force_room[ROOMNAMELEN];
707 static int serialnum = 0;
709 lprintf(9, "entering network_bounce()\n");
711 if (msg == NULL) return;
713 sprintf(bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
716 * Give it a fresh message ID
718 if (msg->cm_fields['I'] != NULL) {
719 phree(msg->cm_fields['I']);
721 sprintf(buf, "%ld.%04lx.%04x@%s",
722 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
723 msg->cm_fields['I'] = strdoop(buf);
726 * FIXME ... right now we're just sending a bounce; we really want to
727 * include the text of the bounced message.
729 if (msg->cm_fields['M'] != NULL) {
730 phree(msg->cm_fields['M']);
732 msg->cm_fields['M'] = strdoop(reason);
733 msg->cm_format_type = 0;
736 * Turn the message around
738 if (msg->cm_fields['R'] == NULL) {
739 phree(msg->cm_fields['R']);
742 if (msg->cm_fields['D'] == NULL) {
743 phree(msg->cm_fields['D']);
746 snprintf(recipient, sizeof recipient, "%s@%s",
747 msg->cm_fields['A'], msg->cm_fields['N']);
749 if (msg->cm_fields['A'] == NULL) {
750 phree(msg->cm_fields['A']);
753 if (msg->cm_fields['N'] == NULL) {
754 phree(msg->cm_fields['N']);
757 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
758 msg->cm_fields['N'] = strdoop(config.c_nodename);
761 /* prepend our node to the path */
762 if (msg->cm_fields['P'] != NULL) {
763 oldpath = msg->cm_fields['P'];
764 msg->cm_fields['P'] = NULL;
767 oldpath = strdoop("unknown_user");
769 msg->cm_fields['P'] = mallok(strlen(oldpath) + SIZ);
770 sprintf(msg->cm_fields['P'], "%s!%s", config.c_nodename, oldpath);
773 /* Now submit the message */
774 valid = validate_recipients(recipient);
775 if (valid != NULL) if (valid->num_error > 0) {
779 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
780 strcpy(force_room, AIDEROOM);
783 strcpy(force_room, "");
785 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
786 strcpy(force_room, AIDEROOM);
788 CtdlSubmitMsg(msg, valid, force_room);
791 if (valid != NULL) phree(valid);
792 CtdlFreeMessage(msg);
793 lprintf(9, "leaving network_bounce()\n");
800 * Process a buffer containing a single message from a single file
801 * from the inbound queue
803 void network_process_buffer(char *buffer, long size) {
804 struct CtdlMessage *msg;
807 struct recptypes *recp = NULL;
808 char target_room[ROOMNAMELEN];
809 struct ser_ret sermsg;
810 char *oldpath = NULL;
815 /* Set default target room to trash */
816 strcpy(target_room, TWITROOM);
818 /* Load the message into memory */
819 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
820 memset(msg, 0, sizeof(struct CtdlMessage));
821 msg->cm_magic = CTDLMESSAGE_MAGIC;
822 msg->cm_anon_type = buffer[1];
823 msg->cm_format_type = buffer[2];
825 for (pos = 3; pos < size; ++pos) {
827 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
828 pos = pos + strlen(&buffer[(int)pos]);
831 /* Check for message routing */
832 if (msg->cm_fields['D'] != NULL) {
833 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
835 /* route the message */
836 if (is_valid_node(NULL, NULL,
837 msg->cm_fields['D']) == 0) {
839 /* prepend our node to the path */
840 if (msg->cm_fields['P'] != NULL) {
841 oldpath = msg->cm_fields['P'];
842 msg->cm_fields['P'] = NULL;
845 oldpath = strdoop("unknown_user");
847 msg->cm_fields['P'] =
848 mallok(strlen(oldpath) + SIZ);
849 sprintf(msg->cm_fields['P'], "%s!%s",
850 config.c_nodename, oldpath);
853 /* serialize the message */
854 serialize_message(&sermsg, msg);
858 "./network/spoolout/%s",
859 msg->cm_fields['D']);
860 fp = fopen(filename, "ab");
867 CtdlFreeMessage(msg);
871 else { /* invalid destination node name */
874 "A message you sent could not be delivered due to an invalid destination node"
875 " name. Please check the address and try sending the message again.\n");
884 * Check to see if we already have a copy of this message
886 if (network_usetable(msg) != 0) {
888 "Loopzapper rejected message <%s> "
889 "from <%s> in <%s> @ <%s>\n",
890 ((msg->cm_fields['I']!=NULL)?(msg->cm_fields['I']):""),
891 ((msg->cm_fields['A']!=NULL)?(msg->cm_fields['A']):""),
892 ((msg->cm_fields['O']!=NULL)?(msg->cm_fields['O']):""),
893 ((msg->cm_fields['N']!=NULL)?(msg->cm_fields['N']):"")
896 CtdlFreeMessage(msg);
901 /* Learn network topology from the path */
902 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
903 network_learn_topology(msg->cm_fields['N'],
904 msg->cm_fields['P']);
907 /* Does it have a recipient? If so, validate it... */
908 if (msg->cm_fields['R'] != NULL) {
909 recp = validate_recipients(msg->cm_fields['R']);
910 if (recp != NULL) if (recp->num_error > 0) {
912 "A message you sent could not be delivered due to an invalid address.\n"
913 "Please check the address and try sending the message again.\n");
918 strcpy(target_room, ""); /* no target room if mail */
921 else if (msg->cm_fields['C'] != NULL) {
922 safestrncpy(target_room,
927 else if (msg->cm_fields['O'] != NULL) {
928 safestrncpy(target_room,
933 /* Strip out fields that are only relevant during transit */
934 if (msg->cm_fields['D'] != NULL) {
935 phree(msg->cm_fields['D']);
936 msg->cm_fields['D'] = NULL;
938 if (msg->cm_fields['C'] != NULL) {
939 phree(msg->cm_fields['C']);
940 msg->cm_fields['C'] = NULL;
943 /* save the message into a room */
944 if (PerformNetprocHooks(msg, target_room) == 0) {
945 msg->cm_flags = CM_SKIP_HOOKS;
946 CtdlSubmitMsg(msg, recp, target_room);
948 CtdlFreeMessage(msg);
954 * Process a single message from a single file from the inbound queue
956 void network_process_message(FILE *fp, long msgstart, long msgend) {
961 hold_pos = ftell(fp);
962 size = msgend - msgstart + 1;
963 buffer = mallok(size);
964 if (buffer != NULL) {
965 fseek(fp, msgstart, SEEK_SET);
966 fread(buffer, size, 1, fp);
967 network_process_buffer(buffer, size);
971 fseek(fp, hold_pos, SEEK_SET);
976 * Process a single file from the inbound queue
978 void network_process_file(char *filename) {
980 long msgstart = (-1L);
985 lprintf(7, "network: processing <%s>\n", filename);
987 fp = fopen(filename, "rb");
989 lprintf(5, "Error opening %s: %s\n",
990 filename, strerror(errno));
994 /* Look for messages in the data stream and break them out */
995 while (ch = getc(fp), ch >= 0) {
998 if (msgstart >= 0L) {
1000 network_process_message(fp, msgstart, msgend);
1008 msgend = msgcur - 1;
1009 if (msgstart >= 0L) {
1010 network_process_message(fp, msgstart, msgend);
1019 * Process anything in the inbound queue
1021 void network_do_spoolin(void) {
1026 dp = opendir("./network/spoolin");
1027 if (dp == NULL) return;
1029 while (d = readdir(dp), d != NULL) {
1030 sprintf(filename, "./network/spoolin/%s", d->d_name);
1031 network_process_file(filename);
1043 * receive network spool from the remote system
1045 void receive_spool(int sock, char *remote_nodename) {
1047 long bytes_received;
1049 static char pbuf[IGNET_PACKET_SIZE];
1050 char tempfilename[PATH_MAX];
1054 strcpy(tempfilename, tmpnam(NULL));
1055 if (sock_puts(sock, "NDOP") < 0) return;
1056 if (sock_gets(sock, buf) < 0) return;
1057 lprintf(9, "<%s\n", buf);
1058 if (buf[0] != '2') {
1061 download_len = extract_long(&buf[4], 0);
1063 bytes_received = 0L;
1064 fp = fopen(tempfilename, "w");
1066 lprintf(9, "cannot open download file locally: %s\n",
1071 while (bytes_received < download_len) {
1072 sprintf(buf, "READ %ld|%ld",
1074 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1075 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1076 if (sock_puts(sock, buf) < 0) {
1078 unlink(tempfilename);
1081 if (sock_gets(sock, buf) < 0) {
1083 unlink(tempfilename);
1086 if (buf[0] == '6') {
1087 plen = extract_long(&buf[4], 0);
1088 if (sock_read(sock, pbuf, plen) < 0) {
1090 unlink(tempfilename);
1093 fwrite((char *) pbuf, plen, 1, fp);
1094 bytes_received = bytes_received + plen;
1099 if (sock_puts(sock, "CLOS") < 0) {
1100 unlink(tempfilename);
1103 if (sock_gets(sock, buf) < 0) {
1104 unlink(tempfilename);
1107 lprintf(9, "%s\n", buf);
1108 sprintf(buf, "mv %s ./network/spoolin/%s.%ld",
1109 tempfilename, remote_nodename, (long) getpid());
1116 * transmit network spool to the remote system
1118 void transmit_spool(int sock, char *remote_nodename)
1123 long bytes_to_write, thisblock;
1127 if (sock_puts(sock, "NUOP") < 0) return;
1128 if (sock_gets(sock, buf) < 0) return;
1129 lprintf(9, "<%s\n", buf);
1130 if (buf[0] != '2') {
1134 sprintf(sfname, "./network/spoolout/%s", remote_nodename);
1135 fd = open(sfname, O_RDONLY);
1137 if (errno == ENOENT) {
1138 lprintf(9, "Nothing to send.\n");
1140 lprintf(5, "cannot open upload file locally: %s\n",
1145 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1146 bytes_to_write = plen;
1147 while (bytes_to_write > 0L) {
1148 sprintf(buf, "WRIT %ld", bytes_to_write);
1149 if (sock_puts(sock, buf) < 0) {
1153 if (sock_gets(sock, buf) < 0) {
1157 thisblock = atol(&buf[4]);
1158 if (buf[0] == '7') {
1159 if (sock_write(sock, pbuf,
1160 (int) thisblock) < 0) {
1164 bytes_to_write = bytes_to_write - thisblock;
1173 if (sock_puts(sock, "UCLS 1") < 0) return;
1174 if (sock_gets(sock, buf) < 0) return;
1175 lprintf(9, "<%s\n", buf);
1176 if (buf[0] == '2') {
1184 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1186 void network_poll_node(char *node, char *secret, char *host, char *port) {
1190 if (network_talking_to(node, NTT_CHECK)) return;
1191 network_talking_to(node, NTT_ADD);
1192 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1194 sock = sock_connect(host, port, "tcp");
1196 lprintf(7, "Could not connect: %s\n", strerror(errno));
1197 network_talking_to(node, NTT_REMOVE);
1201 lprintf(9, "Connected!\n");
1203 /* Read the server greeting */
1204 if (sock_gets(sock, buf) < 0) goto bail;
1205 lprintf(9, ">%s\n", buf);
1207 /* Identify ourselves */
1208 sprintf(buf, "NETP %s|%s", config.c_nodename, secret);
1209 lprintf(9, "<%s\n", buf);
1210 if (sock_puts(sock, buf) <0) goto bail;
1211 if (sock_gets(sock, buf) < 0) goto bail;
1212 lprintf(9, ">%s\n", buf);
1213 if (buf[0] != '2') goto bail;
1215 /* At this point we are authenticated. */
1216 receive_spool(sock, node);
1217 transmit_spool(sock, node);
1219 sock_puts(sock, "QUIT");
1220 bail: sock_close(sock);
1221 network_talking_to(node, NTT_REMOVE);
1227 * Poll other Citadel nodes and transfer inbound/outbound network data.
1229 void network_poll_other_citadel_nodes(void) {
1230 char *ignetcfg = NULL;
1238 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1239 if (ignetcfg == NULL) return; /* no nodes defined */
1241 /* Use the string tokenizer to grab one line at a time */
1242 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1243 extract_token(linebuf, ignetcfg, i, '\n');
1244 extract(node, linebuf, 0);
1245 extract(secret, linebuf, 1);
1246 extract(host, linebuf, 2);
1247 extract(port, linebuf, 3);
1248 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1249 && (strlen(host) > 0) && strlen(port) > 0) {
1250 network_poll_node(node, secret, host, port);
1264 * network_do_queue()
1266 * Run through the rooms doing various types of network stuff.
1268 void network_do_queue(void) {
1269 static int doing_queue = 0;
1270 static time_t last_run = 0L;
1271 struct RoomProcList *ptr;
1274 * Run no more frequently than once every n seconds
1276 if ( (time(NULL) - last_run) < config.c_net_freq ) return;
1279 * This is a simple concurrency check to make sure only one queue run
1280 * is done at a time. We could do this with a mutex, but since we
1281 * don't really require extremely fine granularity here, we'll do it
1282 * with a static variable instead.
1284 if (doing_queue) return;
1286 last_run = time(NULL);
1289 * Poll other Citadel nodes.
1291 network_poll_other_citadel_nodes();
1294 * Load the network map and filter list into memory.
1297 filterlist = load_filter_list();
1300 * Go ahead and run the queue
1302 lprintf(7, "network: loading outbound queue\n");
1303 ForEachRoom(network_queue_room, NULL);
1305 lprintf(7, "network: running outbound queue\n");
1306 while (rplist != NULL) {
1307 network_spoolout_room(rplist->name);
1309 rplist = rplist->next;
1313 lprintf(7, "network: processing inbound queue\n");
1314 network_do_spoolin();
1316 /* Save the network map back to disk */
1317 write_network_map();
1319 /* Free the filter list in memory */
1320 free_filter_list(filterlist);
1323 lprintf(7, "network: queue run completed\n");
1329 * cmd_netp() - authenticate to the server as another Citadel node polling
1330 * for network traffic
1332 void cmd_netp(char *cmdbuf)
1340 extract(node, cmdbuf, 0);
1341 extract(pass, cmdbuf, 1);
1343 if (is_valid_node(nexthop, secret, node) != 0) {
1344 cprintf("%d authentication failed\n", ERROR);
1348 if (strcasecmp(pass, secret)) {
1349 cprintf("%d authentication failed\n", ERROR);
1353 if (network_talking_to(node, NTT_CHECK)) {
1354 cprintf("%d Already talking to %s right now\n", ERROR, node);
1358 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1359 network_talking_to(node, NTT_ADD);
1360 cprintf("%d authenticated as network node '%s'\n", OK,
1369 * Module entry point
1371 char *Dynamic_Module_Init(void)
1373 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1374 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1375 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1376 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);