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.
13 * FIXME do something about concurrency issues:
14 * 1. Don't allow the two nodes to poll each other at the same time
15 * 2. Don't allow polls during network processing
16 * 3. Kill Bill Gates using either a chainsaw or a wood chipper
27 #include <sys/types.h>
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
45 #include "sysdep_decls.h"
46 #include "citserver.h"
49 #include "dynloader.h"
56 #include "internet_addressing.h"
57 #include "serv_network.h"
58 #include "clientsocket.h"
62 * When we do network processing, it's accomplished in two passes; one to
63 * gather a list of rooms and one to actually do them. It's ok that rplist
64 * is global; this process *only* runs as part of the housekeeping loop and
65 * therefore only one will run at a time.
68 struct RoomProcList *next;
69 char name[ROOMNAMELEN];
72 struct RoomProcList *rplist = NULL;
76 * We build a map of the Citadel network during network runs.
85 struct NetMap *the_netmap = NULL;
90 * network_talking_to() -- concurrency checker
92 int network_talking_to(char *nodename, int operation) {
94 static char *nttlist = NULL;
100 begin_critical_section(S_NTTLIST);
105 if (nttlist == NULL) nttlist = strdoop("");
106 if (nttlist == NULL) break;
107 nttlist = (char *)reallok(nttlist,
108 (strlen(nttlist) + strlen(nodename) + 3) );
109 strcat(nttlist, "|");
110 strcat(nttlist, nodename);
114 if (nttlist == NULL) break;
115 if (strlen(nttlist) == 0) break;
116 ptr = mallok(strlen(nttlist));
117 if (ptr == NULL) break;
119 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
120 extract(buf, nttlist, i);
121 if ( (strlen(buf) > 0)
122 && (strcasecmp(buf, nodename)) ) {
132 if (nttlist == NULL) break;
133 if (strlen(nttlist) == 0) break;
134 for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
135 extract(buf, nttlist, i);
136 if (!strcasecmp(buf, nodename)) ++retval;
141 if (nttlist != NULL) lprintf(9, "nttlist=<%s>\n", nttlist);
142 end_critical_section(S_NTTLIST);
153 * Read the network map from its configuration file into memory.
155 void read_network_map(void) {
156 char *serialized_map = NULL;
159 struct NetMap *nmptr;
161 serialized_map = CtdlGetSysConfig(IGNETMAP);
162 if (serialized_map == NULL) return; /* if null, no entries */
164 /* Use the string tokenizer to grab one line at a time */
165 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
166 extract_token(buf, serialized_map, i, '\n');
167 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
168 extract(nmptr->nodename, buf, 0);
169 nmptr->lastcontact = extract_long(buf, 1);
170 extract(nmptr->nexthop, buf, 2);
171 nmptr->next = the_netmap;
175 phree(serialized_map);
180 * Write the network map from memory back to the configuration file.
182 void write_network_map(void) {
183 char *serialized_map = NULL;
184 struct NetMap *nmptr;
186 serialized_map = strdoop("");
188 if (the_netmap != NULL) {
189 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
190 serialized_map = reallok(serialized_map,
191 (strlen(serialized_map)+SIZ) );
192 if (strlen(nmptr->nodename) > 0) {
193 sprintf(&serialized_map[strlen(serialized_map)],
202 CtdlPutSysConfig(IGNETMAP, serialized_map);
203 phree(serialized_map);
205 /* Now free the list */
206 while (the_netmap != NULL) {
207 nmptr = the_netmap->next;
216 * Check the network map and determine whether the supplied node name is
217 * valid. If it is not a neighbor node, supply the name of a neighbor node
218 * which is the next hop. If it *is* a neighbor node, we also fill in the
221 int is_valid_node(char *nexthop, char *secret, char *node) {
222 char *ignetcfg = NULL;
227 struct NetMap *nmptr;
234 * First try the neighbor nodes
236 ignetcfg = CtdlGetSysConfig(IGNETCFG);
237 if (ignetcfg == NULL) {
238 if (nexthop != NULL) {
245 if (nexthop != NULL) {
249 /* Use the string tokenizer to grab one line at a time */
250 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
251 extract_token(linebuf, ignetcfg, i, '\n');
252 extract(buf, linebuf, 0);
253 if (!strcasecmp(buf, node)) {
254 if (nexthop != NULL) {
257 if (secret != NULL) {
258 extract(secret, linebuf, 1);
266 return(retval); /* yup, it's a direct neighbor */
270 * If we get to this point we have to see if we know the next hop
272 if (the_netmap != NULL) {
273 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
274 if (!strcasecmp(nmptr->nodename, node)) {
275 if (nexthop != NULL) {
276 strcpy(nexthop, nmptr->nexthop);
284 * If we get to this point, the supplied node name is bogus.
286 lprintf(5, "Invalid node name <%s>\n", node);
294 void cmd_gnet(char *argbuf) {
299 if (CtdlAccessCheck(ac_room_aide)) return;
300 assoc_file_name(filename, &CC->quickroom, "netconfigs");
301 cprintf("%d Network settings for room #%ld <%s>\n",
303 CC->quickroom.QRnumber, CC->quickroom.QRname);
305 fp = fopen(filename, "r");
307 while (fgets(buf, sizeof buf, fp) != NULL) {
308 buf[strlen(buf)-1] = 0;
309 cprintf("%s\n", buf);
318 void cmd_snet(char *argbuf) {
319 char tempfilename[SIZ];
324 if (CtdlAccessCheck(ac_room_aide)) return;
325 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
326 assoc_file_name(filename, &CC->quickroom, "netconfigs");
328 fp = fopen(tempfilename, "w");
330 cprintf("%d Cannot open %s: %s\n",
331 ERROR+INTERNAL_ERROR,
336 cprintf("%d %s\n", SEND_LISTING, tempfilename);
337 while (client_gets(buf), strcmp(buf, "000")) {
338 fprintf(fp, "%s\n", buf);
342 /* Now copy the temp file to its permanent location
343 * (We use /bin/mv instead of link() because they may be on
344 * different filesystems)
347 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
354 * Spools out one message from the list.
356 void network_spool_msg(long msgnum, void *userdata) {
357 struct SpoolControl *sc;
358 struct namelist *nptr;
362 char *newpath = NULL;
363 size_t instr_len = SIZ;
364 struct CtdlMessage *msg = NULL;
365 struct CtdlMessage *imsg;
366 struct ser_ret sermsg;
372 int delete_after_send = 0; /* Set to 1 to delete after spooling */
374 sc = (struct SpoolControl *)userdata;
377 * Process mailing list recipients
379 if (sc->listrecps != NULL) {
381 /* First, copy it to the spoolout room */
382 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
383 if (err != 0) return;
386 * Figure out how big a buffer we need to allocate
388 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
389 instr_len = instr_len + strlen(nptr->name);
395 lprintf(9, "Generating delivery instructions\n");
396 instr = mallok(instr_len);
398 lprintf(1, "Cannot allocate %d bytes for instr...\n",
403 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
404 "bounceto|postmaster@%s\n" ,
405 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
407 /* Generate delivery instructions for each recipient */
408 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
409 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
414 * Generate a message from the instructions
416 imsg = mallok(sizeof(struct CtdlMessage));
417 memset(imsg, 0, sizeof(struct CtdlMessage));
418 imsg->cm_magic = CTDLMESSAGE_MAGIC;
419 imsg->cm_anon_type = MES_NORMAL;
420 imsg->cm_format_type = FMT_RFC822;
421 imsg->cm_fields['A'] = strdoop("Citadel");
422 imsg->cm_fields['M'] = instr;
424 /* Save delivery instructions in spoolout room */
425 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
426 CtdlFreeMessage(imsg);
430 * Process IGnet push shares
432 if (sc->ignet_push_shares != NULL) {
434 msg = CtdlFetchMessage(msgnum);
437 /* Prepend our node name to the Path field whenever
438 * sending a message to another IGnet node
440 if (msg->cm_fields['P'] == NULL) {
441 msg->cm_fields['P'] = strdoop("username");
443 newpath = mallok(strlen(msg->cm_fields['P']) +
444 strlen(config.c_nodename) + 2);
445 sprintf(newpath, "%s!%s", config.c_nodename,
446 msg->cm_fields['P']);
447 phree(msg->cm_fields['P']);
448 msg->cm_fields['P'] = newpath;
451 * Force the message to appear in the correct room
452 * on the far end by setting the C field correctly
454 if (msg->cm_fields['C'] != NULL) {
455 phree(msg->cm_fields['C']);
457 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
460 * Determine if this message is set to be deleted
461 * after sending out on the network
463 if (msg->cm_fields['S'] != NULL) {
464 if (!strcasecmp(msg->cm_fields['S'],
466 delete_after_send = 1;
471 * Now serialize it for transmission
473 serialize_message(&sermsg, msg);
475 /* Now send it to every node */
476 for (nptr = sc->ignet_push_shares; nptr != NULL;
481 /* Check for valid node name */
482 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
483 lprintf(3, "Invalid node <%s>\n",
488 /* Check for split horizon */
489 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
490 bang = num_tokens(msg->cm_fields['P'], '!');
491 if (bang > 1) for (i=0; i<(bang-1); ++i) {
492 extract_token(buf, msg->cm_fields['P'],
494 if (!strcasecmp(buf, nptr->name)) {
499 /* Send the message */
502 "./network/spoolout/%s",
504 fp = fopen(filename, "ab");
513 CtdlFreeMessage(msg);
517 /* update lastsent */
518 sc->lastsent = msgnum;
520 /* Delete this message if delete-after-send is set */
521 if (delete_after_send) {
522 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
531 * Batch up and send all outbound traffic from the current room
533 void network_spoolout_room(char *room_to_spool) {
538 struct SpoolControl sc;
539 /* struct namelist *digestrecps = NULL; */
540 struct namelist *nptr;
542 lprintf(7, "Spooling <%s>\n", room_to_spool);
543 if (getroom(&CC->quickroom, room_to_spool) != 0) {
544 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
548 memset(&sc, 0, sizeof(struct SpoolControl));
549 assoc_file_name(filename, &CC->quickroom, "netconfigs");
551 fp = fopen(filename, "r");
553 lprintf(7, "Outbound batch processing skipped for <%s>\n",
554 CC->quickroom.QRname);
558 lprintf(5, "Outbound batch processing started for <%s>\n",
559 CC->quickroom.QRname);
561 while (fgets(buf, sizeof buf, fp) != NULL) {
562 buf[strlen(buf)-1] = 0;
564 extract(instr, buf, 0);
565 if (!strcasecmp(instr, "lastsent")) {
566 sc.lastsent = extract_long(buf, 1);
568 else if (!strcasecmp(instr, "listrecp")) {
569 nptr = (struct namelist *)
570 mallok(sizeof(struct namelist));
571 nptr->next = sc.listrecps;
572 extract(nptr->name, buf, 1);
575 else if (!strcasecmp(instr, "ignet_push_share")) {
576 nptr = (struct namelist *)
577 mallok(sizeof(struct namelist));
578 nptr->next = sc.ignet_push_shares;
579 extract(nptr->name, buf, 1);
580 sc.ignet_push_shares = nptr;
588 /* Do something useful */
589 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
590 network_spool_msg, &sc);
593 /* Now rewrite the config file */
594 fp = fopen(filename, "w");
596 lprintf(1, "ERROR: cannot open %s: %s\n",
597 filename, strerror(errno));
600 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
602 /* Write out the listrecps while freeing from memory at the
603 * same time. Am I clever or what? :)
605 while (sc.listrecps != NULL) {
606 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
607 nptr = sc.listrecps->next;
611 while (sc.ignet_push_shares != NULL) {
612 fprintf(fp, "ignet_push_share|%s\n",
613 sc.ignet_push_shares->name);
614 nptr = sc.ignet_push_shares->next;
615 phree(sc.ignet_push_shares);
616 sc.ignet_push_shares = nptr;
622 lprintf(5, "Outbound batch processing finished for <%s>\n",
623 CC->quickroom.QRname);
628 * Batch up and send all outbound traffic from the current room
630 void network_queue_room(struct quickroom *qrbuf, void *data) {
631 struct RoomProcList *ptr;
633 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
634 if (ptr == NULL) return;
636 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
643 * Learn topology from path fields
645 void network_learn_topology(char *node, char *path) {
647 struct NetMap *nmptr;
651 if (num_tokens(path, '!') < 3) return;
652 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
653 if (!strcasecmp(nmptr->nodename, node)) {
654 extract_token(nmptr->nexthop, path, 0, '!');
655 nmptr->lastcontact = time(NULL);
660 /* If we got here then it's not in the map, so add it. */
661 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
662 strcpy(nmptr->nodename, node);
663 nmptr->lastcontact = time(NULL);
664 extract_token(nmptr->nexthop, path, 0, '!');
665 nmptr->next = the_netmap;
673 * Bounce a message back to the sender
675 void network_bounce(struct CtdlMessage *msg, char *reason) {
676 static int serialnum = 0;
679 struct ser_ret sermsg;
680 char *oldpath = NULL;
683 lprintf(9, "entering network_bounce()\n");
685 if (msg == NULL) return;
688 * Give it a fresh message ID
690 if (msg->cm_fields['I'] != NULL) {
691 phree(msg->cm_fields['I']);
693 sprintf(buf, "%ld.%04x.%04x@%s",
694 time(NULL), getpid(), ++serialnum, config.c_fqdn);
695 msg->cm_fields['I'] = strdoop(buf);
698 * FIXME ... right now we're just sending a bounce; we really want to
699 * include the text of the bounced message.
701 if (msg->cm_fields['M'] != NULL) {
702 phree(msg->cm_fields['M']);
704 msg->cm_fields['M'] = strdoop(reason);
705 msg->cm_format_type = 0;
708 * Turn the message around
710 if (msg->cm_fields['R'] == NULL) {
711 phree(msg->cm_fields['R']);
714 if (msg->cm_fields['D'] == NULL) {
715 phree(msg->cm_fields['D']);
718 msg->cm_fields['R'] = msg->cm_fields['A'];
719 msg->cm_fields['D'] = msg->cm_fields['N'];
720 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
721 msg->cm_fields['N'] = strdoop(config.c_nodename);
723 if (!strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
724 phree(msg->cm_fields['D']);
728 * If this is a bounce of a bounce, send it to the Aide> room
729 * instead of looping around forever
731 if (msg->cm_fields['D'] == NULL) if (msg->cm_fields['R'] != NULL)
732 if (!strcasecmp(msg->cm_fields['R'], BOUNCESOURCE)) {
733 phree(msg->cm_fields['R']);
734 if (msg->cm_fields['C'] != NULL) {
735 phree(msg->cm_fields['C']);
737 msg->cm_fields['C'] = strdoop(AIDEROOM);
740 /* prepend our node to the path */
741 if (msg->cm_fields['P'] != NULL) {
742 oldpath = msg->cm_fields['P'];
743 msg->cm_fields['P'] = NULL;
746 oldpath = strdoop("unknown_user");
748 msg->cm_fields['P'] = mallok(strlen(oldpath) + SIZ);
749 sprintf(msg->cm_fields['P'], "%s!%s", config.c_nodename, oldpath);
752 /* serialize the message */
753 serialize_message(&sermsg, msg);
756 sprintf(filename, "./network/spoolin/bounce.%04x.%04x",
757 getpid(), serialnum);
759 fp = fopen(filename, "ab");
766 CtdlFreeMessage(msg);
767 lprintf(9, "leaving network_bounce()\n");
774 * Process a buffer containing a single message from a single file
775 * from the inbound queue
777 void network_process_buffer(char *buffer, long size) {
778 struct CtdlMessage *msg;
783 struct usersupp tempUS;
785 char target_room[ROOMNAMELEN];
786 struct ser_ret sermsg;
787 char *oldpath = NULL;
791 /* Set default target room to trash */
792 strcpy(target_room, TWITROOM);
794 /* Load the message into memory */
795 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
796 memset(msg, 0, sizeof(struct CtdlMessage));
797 msg->cm_magic = CTDLMESSAGE_MAGIC;
798 msg->cm_anon_type = buffer[1];
799 msg->cm_format_type = buffer[2];
801 for (pos = 3; pos < size; ++pos) {
803 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
804 pos = pos + strlen(&buffer[(int)pos]);
807 /* Check for message routing */
808 if (msg->cm_fields['D'] != NULL) {
809 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
811 /* route the message */
812 if (is_valid_node(NULL, NULL,
813 msg->cm_fields['D']) == 0) {
815 /* prepend our node to the path */
816 if (msg->cm_fields['P'] != NULL) {
817 oldpath = msg->cm_fields['P'];
818 msg->cm_fields['P'] = NULL;
821 oldpath = strdoop("unknown_user");
823 msg->cm_fields['P'] =
824 mallok(strlen(oldpath) + SIZ);
825 sprintf(msg->cm_fields['P'], "%s!%s",
826 config.c_nodename, oldpath);
829 /* serialize the message */
830 serialize_message(&sermsg, msg);
834 "./network/spoolout/%s",
835 msg->cm_fields['D']);
836 fp = fopen(filename, "ab");
843 CtdlFreeMessage(msg);
847 else { /* invalid destination node name */
850 "A message you sent could not be delivered due to an invalid destination node"
851 " name. Please check the address and try sending the message again.\n");
859 /* FIXME check to see if we already have this message */
861 /* Learn network topology from the path */
862 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
863 network_learn_topology(msg->cm_fields['N'],
864 msg->cm_fields['P']);
867 /* Does it have a recipient? If so, validate it... */
868 if (msg->cm_fields['R'] != NULL) {
870 safestrncpy(recp, msg->cm_fields['R'], sizeof(recp));
872 e = alias(recp); /* alias and mail type */
873 if ((recp[0] == 0) || (e == MES_ERROR)) {
876 "A message you sent could not be delivered due to an invalid address.\n"
877 "Please check the address and try sending the message again.\n");
882 else if (e == MES_LOCAL) {
883 a = getuser(&tempUS, recp);
887 "A message you sent could not be delivered because the user does not exist\n"
888 "on this system. Please check the address and try again.\n");
894 MailboxName(target_room, &tempUS, MAILROOM);
899 else if (msg->cm_fields['C'] != NULL) {
900 safestrncpy(target_room,
905 else if (msg->cm_fields['O'] != NULL) {
906 safestrncpy(target_room,
911 /* save the message into a room */
912 msg->cm_flags = CM_SKIP_HOOKS;
913 CtdlSaveMsg(msg, "", target_room, 0);
914 CtdlFreeMessage(msg);
919 * Process a single message from a single file from the inbound queue
921 void network_process_message(FILE *fp, long msgstart, long msgend) {
926 hold_pos = ftell(fp);
927 size = msgend - msgstart + 1;
928 buffer = mallok(size);
929 if (buffer != NULL) {
930 fseek(fp, msgstart, SEEK_SET);
931 fread(buffer, size, 1, fp);
932 network_process_buffer(buffer, size);
936 fseek(fp, hold_pos, SEEK_SET);
941 * Process a single file from the inbound queue
943 void network_process_file(char *filename) {
945 long msgstart = (-1L);
950 lprintf(7, "network: processing <%s>\n", filename);
952 fp = fopen(filename, "rb");
954 lprintf(5, "Error opening %s: %s\n",
955 filename, strerror(errno));
959 /* Look for messages in the data stream and break them out */
960 while (ch = getc(fp), ch >= 0) {
963 if (msgstart >= 0L) {
965 network_process_message(fp, msgstart, msgend);
974 if (msgstart >= 0L) {
975 network_process_message(fp, msgstart, msgend);
984 * Process anything in the inbound queue
986 void network_do_spoolin(void) {
991 dp = opendir("./network/spoolin");
992 if (dp == NULL) return;
994 while (d = readdir(dp), d != NULL) {
995 sprintf(filename, "./network/spoolin/%s", d->d_name);
996 network_process_file(filename);
1008 * receive network spool from the remote system
1010 void receive_spool(int sock, char *remote_nodename) {
1012 long bytes_received;
1014 static char pbuf[IGNET_PACKET_SIZE];
1015 char tempfilename[PATH_MAX];
1019 strcpy(tempfilename, tmpnam(NULL));
1020 if (sock_puts(sock, "NDOP") < 0) return;
1021 if (sock_gets(sock, buf) < 0) return;
1022 lprintf(9, "<%s\n", buf);
1023 if (buf[0] != '2') {
1026 download_len = extract_long(&buf[4], 0);
1028 bytes_received = 0L;
1029 fp = fopen(tempfilename, "w");
1031 lprintf(9, "cannot open download file locally: %s\n",
1036 while (bytes_received < download_len) {
1037 sprintf(buf, "READ %ld|%ld",
1039 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1040 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1041 if (sock_puts(sock, buf) < 0) {
1043 unlink(tempfilename);
1046 if (sock_gets(sock, buf) < 0) {
1048 unlink(tempfilename);
1051 if (buf[0] == '6') {
1052 plen = extract_long(&buf[4], 0);
1053 if (sock_read(sock, pbuf, plen) < 0) {
1055 unlink(tempfilename);
1058 fwrite((char *) pbuf, plen, 1, fp);
1059 bytes_received = bytes_received + plen;
1064 if (sock_puts(sock, "CLOS") < 0) {
1065 unlink(tempfilename);
1068 if (sock_gets(sock, buf) < 0) {
1069 unlink(tempfilename);
1072 lprintf(9, "%s\n", buf);
1073 sprintf(buf, "mv %s ./network/spoolin/%s.%ld",
1074 tempfilename, remote_nodename, (long) getpid());
1081 * transmit network spool to the remote system
1083 void transmit_spool(int sock, char *remote_nodename)
1088 long bytes_to_write, thisblock;
1092 if (sock_puts(sock, "NUOP") < 0) return;
1093 if (sock_gets(sock, buf) < 0) return;
1094 lprintf(9, "<%s\n", buf);
1095 if (buf[0] != '2') {
1099 sprintf(sfname, "./network/spoolout/%s", remote_nodename);
1100 fd = open(sfname, O_RDONLY);
1102 if (errno == ENOENT) {
1103 lprintf(9, "Nothing to send.\n");
1105 lprintf(5, "cannot open upload file locally: %s\n",
1110 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1111 bytes_to_write = plen;
1112 while (bytes_to_write > 0L) {
1113 sprintf(buf, "WRIT %ld", bytes_to_write);
1114 if (sock_puts(sock, buf) < 0) {
1118 if (sock_gets(sock, buf) < 0) {
1122 thisblock = atol(&buf[4]);
1123 if (buf[0] == '7') {
1124 if (sock_write(sock, pbuf,
1125 (int) thisblock) < 0) {
1129 bytes_to_write = bytes_to_write - thisblock;
1138 if (sock_puts(sock, "UCLS 1") < 0) return;
1139 if (sock_gets(sock, buf) < 0) return;
1140 lprintf(9, "<%s\n", buf);
1141 if (buf[0] == '2') {
1149 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1151 void network_poll_node(char *node, char *secret, char *host, char *port) {
1155 if (network_talking_to(node, NTT_CHECK)) return;
1156 network_talking_to(node, NTT_ADD);
1157 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1159 sock = sock_connect(host, port, "tcp");
1161 lprintf(7, "Could not connect: %s\n", strerror(errno));
1162 network_talking_to(node, NTT_REMOVE);
1166 lprintf(9, "Connected!\n");
1168 /* Read the server greeting */
1169 if (sock_gets(sock, buf) < 0) goto bail;
1170 lprintf(9, ">%s\n", buf);
1172 /* Identify ourselves */
1173 sprintf(buf, "NETP %s|%s", config.c_nodename, secret);
1174 lprintf(9, "<%s\n", buf);
1175 if (sock_puts(sock, buf) <0) goto bail;
1176 if (sock_gets(sock, buf) < 0) goto bail;
1177 lprintf(9, ">%s\n", buf);
1178 if (buf[0] != '2') goto bail;
1180 /* At this point we are authenticated. */
1181 receive_spool(sock, node);
1182 transmit_spool(sock, node);
1184 sock_puts(sock, "QUIT");
1185 bail: sock_close(sock);
1186 network_talking_to(node, NTT_REMOVE);
1192 * Poll other Citadel nodes and transfer inbound/outbound network data.
1194 void network_poll_other_citadel_nodes(void) {
1195 char *ignetcfg = NULL;
1203 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1204 if (ignetcfg == NULL) return; /* no nodes defined */
1206 /* Use the string tokenizer to grab one line at a time */
1207 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1208 extract_token(linebuf, ignetcfg, i, '\n');
1209 extract(node, linebuf, 0);
1210 extract(secret, linebuf, 1);
1211 extract(host, linebuf, 2);
1212 extract(port, linebuf, 3);
1213 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1214 && (strlen(host) > 0) && strlen(port) > 0) {
1215 network_poll_node(node, secret, host, port);
1229 * network_do_queue()
1231 * Run through the rooms doing various types of network stuff.
1233 void network_do_queue(void) {
1234 static int doing_queue = 0;
1235 static time_t last_run = 0L;
1236 struct RoomProcList *ptr;
1239 * Run no more frequently than once every n seconds
1241 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
1244 * This is a simple concurrency check to make sure only one queue run
1245 * is done at a time. We could do this with a mutex, but since we
1246 * don't really require extremely fine granularity here, we'll do it
1247 * with a static variable instead.
1249 if (doing_queue) return;
1251 last_run = time(NULL);
1254 * Poll other Citadel nodes.
1256 network_poll_other_citadel_nodes();
1259 * Load the network map into memory.
1264 * Go ahead and run the queue
1266 lprintf(7, "network: loading outbound queue\n");
1267 ForEachRoom(network_queue_room, NULL);
1269 lprintf(7, "network: running outbound queue\n");
1270 while (rplist != NULL) {
1271 network_spoolout_room(rplist->name);
1273 rplist = rplist->next;
1277 lprintf(7, "network: processing inbound queue\n");
1278 network_do_spoolin();
1280 write_network_map();
1282 lprintf(7, "network: queue run completed\n");
1289 * cmd_netp() - authenticate to the server as another Citadel node polling
1290 * for network traffic
1292 void cmd_netp(char *cmdbuf)
1300 extract(node, cmdbuf, 0);
1301 extract(pass, cmdbuf, 1);
1303 if (is_valid_node(nexthop, secret, node) != 0) {
1304 cprintf("%d authentication failed\n", ERROR);
1308 if (strcasecmp(pass, secret)) {
1309 cprintf("%d authentication failed\n", ERROR);
1313 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1314 cprintf("%d authenticated as network node '%s'\n", OK,
1321 * Module entry point
1323 char *Dynamic_Module_Init(void)
1325 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1326 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
1327 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1328 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);