4 * This module will eventually replace netproc and some of its utilities. In
5 * the meantime, it serves as a mailing list manager.
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 * Read the network map from its configuration file into memory.
92 void read_network_map(void) {
93 char *serialized_map = NULL;
98 serialized_map = CtdlGetSysConfig(IGNETMAP);
99 if (serialized_map == NULL) return; /* if null, no entries */
101 /* Use the string tokenizer to grab one line at a time */
102 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
103 extract_token(buf, serialized_map, i, '\n');
104 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
105 extract(nmptr->nodename, buf, 0);
106 nmptr->lastcontact = extract_long(buf, 1);
107 extract(nmptr->nexthop, buf, 2);
108 nmptr->next = the_netmap;
112 phree(serialized_map);
117 * Write the network map from memory back to the configuration file.
119 void write_network_map(void) {
120 char *serialized_map = NULL;
121 struct NetMap *nmptr;
123 serialized_map = strdoop("");
125 if (the_netmap != NULL) {
126 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
127 serialized_map = reallok(serialized_map,
128 (strlen(serialized_map)+SIZ) );
129 if (strlen(nmptr->nodename) > 0) {
130 sprintf(&serialized_map[strlen(serialized_map)],
139 CtdlPutSysConfig(IGNETMAP, serialized_map);
140 phree(serialized_map);
142 /* Now free the list */
143 while (the_netmap != NULL) {
144 nmptr = the_netmap->next;
153 * Check the network map and determine whether the supplied node name is
154 * valid. If it is not a neighbor node, supply the name of a neighbor node
155 * which is the next hop. If it *is* a neighbor node, we also fill in the
158 int is_valid_node(char *nexthop, char *secret, char *node) {
159 char *ignetcfg = NULL;
164 struct NetMap *nmptr;
171 * First try the neighbor nodes
173 ignetcfg = CtdlGetSysConfig(IGNETCFG);
174 if (ignetcfg == NULL) {
175 if (nexthop != NULL) {
182 if (nexthop != NULL) {
186 /* Use the string tokenizer to grab one line at a time */
187 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
188 extract_token(linebuf, ignetcfg, i, '\n');
189 extract(buf, linebuf, 0);
190 if (!strcasecmp(buf, node)) {
191 if (nexthop != NULL) {
194 if (secret != NULL) {
195 extract(secret, linebuf, 1);
203 return(retval); /* yup, it's a direct neighbor */
207 * If we get to this point we have to see if we know the next hop
209 if (the_netmap != NULL) {
210 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
211 if (!strcasecmp(nmptr->nodename, node)) {
212 if (nexthop != NULL) {
213 strcpy(nexthop, nmptr->nexthop);
221 * If we get to this point, the supplied node name is bogus.
223 lprintf(5, "Invalid node name <%s>\n", node);
231 void cmd_gnet(char *argbuf) {
236 if (CtdlAccessCheck(ac_room_aide)) return;
237 assoc_file_name(filename, &CC->quickroom, "netconfigs");
238 cprintf("%d Network settings for room #%ld <%s>\n",
240 CC->quickroom.QRnumber, CC->quickroom.QRname);
242 fp = fopen(filename, "r");
244 while (fgets(buf, sizeof buf, fp) != NULL) {
245 buf[strlen(buf)-1] = 0;
246 cprintf("%s\n", buf);
255 void cmd_snet(char *argbuf) {
256 char tempfilename[SIZ];
261 if (CtdlAccessCheck(ac_room_aide)) return;
262 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
263 assoc_file_name(filename, &CC->quickroom, "netconfigs");
265 fp = fopen(tempfilename, "w");
267 cprintf("%d Cannot open %s: %s\n",
268 ERROR+INTERNAL_ERROR,
273 cprintf("%d %s\n", SEND_LISTING, tempfilename);
274 while (client_gets(buf), strcmp(buf, "000")) {
275 fprintf(fp, "%s\n", buf);
279 /* Now copy the temp file to its permanent location
280 * (We use /bin/mv instead of link() because they may be on
281 * different filesystems)
284 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
291 * Spools out one message from the list.
293 void network_spool_msg(long msgnum, void *userdata) {
294 struct SpoolControl *sc;
295 struct namelist *nptr;
299 char *newpath = NULL;
300 size_t instr_len = SIZ;
301 struct CtdlMessage *msg = NULL;
302 struct CtdlMessage *imsg;
303 struct ser_ret sermsg;
309 int delete_after_send = 0; /* Set to 1 to delete after spooling */
311 sc = (struct SpoolControl *)userdata;
314 * Process mailing list recipients
316 if (sc->listrecps != NULL) {
318 /* First, copy it to the spoolout room */
319 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
320 if (err != 0) return;
323 * Figure out how big a buffer we need to allocate
325 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
326 instr_len = instr_len + strlen(nptr->name);
332 lprintf(9, "Generating delivery instructions\n");
333 instr = mallok(instr_len);
335 lprintf(1, "Cannot allocate %d bytes for instr...\n",
340 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
341 "bounceto|postmaster@%s\n" ,
342 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
344 /* Generate delivery instructions for each recipient */
345 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
346 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
351 * Generate a message from the instructions
353 imsg = mallok(sizeof(struct CtdlMessage));
354 memset(imsg, 0, sizeof(struct CtdlMessage));
355 imsg->cm_magic = CTDLMESSAGE_MAGIC;
356 imsg->cm_anon_type = MES_NORMAL;
357 imsg->cm_format_type = FMT_RFC822;
358 imsg->cm_fields['A'] = strdoop("Citadel");
359 imsg->cm_fields['M'] = instr;
361 /* Save delivery instructions in spoolout room */
362 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
363 CtdlFreeMessage(imsg);
367 * Process IGnet push shares
369 if (sc->ignet_push_shares != NULL) {
371 msg = CtdlFetchMessage(msgnum);
374 /* Prepend our node name to the Path field whenever
375 * sending a message to another IGnet node
377 if (msg->cm_fields['P'] == NULL) {
378 msg->cm_fields['P'] = strdoop("username");
380 newpath = mallok(strlen(msg->cm_fields['P']) +
381 strlen(config.c_nodename) + 2);
382 sprintf(newpath, "%s!%s", config.c_nodename,
383 msg->cm_fields['P']);
384 phree(msg->cm_fields['P']);
385 msg->cm_fields['P'] = newpath;
388 * Force the message to appear in the correct room
389 * on the far end by setting the C field correctly
391 if (msg->cm_fields['C'] != NULL) {
392 phree(msg->cm_fields['C']);
394 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
397 * Determine if this message is set to be deleted
398 * after sending out on the network
400 if (msg->cm_fields['S'] != NULL) {
401 if (!strcasecmp(msg->cm_fields['S'],
403 delete_after_send = 1;
408 * Now serialize it for transmission
410 serialize_message(&sermsg, msg);
412 /* Now send it to every node */
413 for (nptr = sc->ignet_push_shares; nptr != NULL;
418 /* Check for valid node name */
419 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
420 lprintf(3, "Invalid node <%s>\n",
425 /* Check for split horizon */
426 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
427 bang = num_tokens(msg->cm_fields['P'], '!');
428 if (bang > 1) for (i=0; i<(bang-1); ++i) {
429 extract_token(buf, msg->cm_fields['P'],
431 if (!strcasecmp(buf, nptr->name)) {
436 /* Send the message */
439 "./network/spoolout/%s",
441 fp = fopen(filename, "ab");
450 CtdlFreeMessage(msg);
454 /* update lastsent */
455 sc->lastsent = msgnum;
457 /* Delete this message if delete-after-send is set */
458 if (delete_after_send) {
459 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
468 * Batch up and send all outbound traffic from the current room
470 void network_spoolout_room(char *room_to_spool) {
475 struct SpoolControl sc;
476 /* struct namelist *digestrecps = NULL; */
477 struct namelist *nptr;
479 lprintf(7, "Spooling <%s>\n", room_to_spool);
480 if (getroom(&CC->quickroom, room_to_spool) != 0) {
481 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
485 memset(&sc, 0, sizeof(struct SpoolControl));
486 assoc_file_name(filename, &CC->quickroom, "netconfigs");
488 fp = fopen(filename, "r");
490 lprintf(7, "Outbound batch processing skipped for <%s>\n",
491 CC->quickroom.QRname);
495 lprintf(5, "Outbound batch processing started for <%s>\n",
496 CC->quickroom.QRname);
498 while (fgets(buf, sizeof buf, fp) != NULL) {
499 buf[strlen(buf)-1] = 0;
501 extract(instr, buf, 0);
502 if (!strcasecmp(instr, "lastsent")) {
503 sc.lastsent = extract_long(buf, 1);
505 else if (!strcasecmp(instr, "listrecp")) {
506 nptr = (struct namelist *)
507 mallok(sizeof(struct namelist));
508 nptr->next = sc.listrecps;
509 extract(nptr->name, buf, 1);
512 else if (!strcasecmp(instr, "ignet_push_share")) {
513 nptr = (struct namelist *)
514 mallok(sizeof(struct namelist));
515 nptr->next = sc.ignet_push_shares;
516 extract(nptr->name, buf, 1);
517 sc.ignet_push_shares = nptr;
525 /* Do something useful */
526 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
527 network_spool_msg, &sc);
530 /* Now rewrite the config file */
531 fp = fopen(filename, "w");
533 lprintf(1, "ERROR: cannot open %s: %s\n",
534 filename, strerror(errno));
537 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
539 /* Write out the listrecps while freeing from memory at the
540 * same time. Am I clever or what? :)
542 while (sc.listrecps != NULL) {
543 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
544 nptr = sc.listrecps->next;
548 while (sc.ignet_push_shares != NULL) {
549 fprintf(fp, "ignet_push_share|%s\n",
550 sc.ignet_push_shares->name);
551 nptr = sc.ignet_push_shares->next;
552 phree(sc.ignet_push_shares);
553 sc.ignet_push_shares = nptr;
559 lprintf(5, "Outbound batch processing finished for <%s>\n",
560 CC->quickroom.QRname);
565 * Batch up and send all outbound traffic from the current room
567 void network_queue_room(struct quickroom *qrbuf, void *data) {
568 struct RoomProcList *ptr;
570 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
571 if (ptr == NULL) return;
573 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
580 * Learn topology from path fields
582 void network_learn_topology(char *node, char *path) {
584 struct NetMap *nmptr;
588 if (num_tokens(path, '!') < 3) return;
589 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
590 if (!strcasecmp(nmptr->nodename, node)) {
591 extract_token(nmptr->nexthop, path, 0, '!');
592 nmptr->lastcontact = time(NULL);
597 /* If we got here then it's not in the map, so add it. */
598 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
599 strcpy(nmptr->nodename, node);
600 nmptr->lastcontact = time(NULL);
601 extract_token(nmptr->nexthop, path, 0, '!');
602 nmptr->next = the_netmap;
610 * Bounce a message back to the sender
612 void network_bounce(struct CtdlMessage *msg, char *reason) {
613 static int serialnum = 0;
616 struct ser_ret sermsg;
617 char *oldpath = NULL;
620 lprintf(9, "entering network_bounce()\n");
622 if (msg == NULL) return;
625 * Give it a fresh message ID
627 if (msg->cm_fields['I'] != NULL) {
628 phree(msg->cm_fields['I']);
630 sprintf(buf, "%ld.%04x.%04x@%s",
631 time(NULL), getpid(), ++serialnum, config.c_fqdn);
632 msg->cm_fields['I'] = strdoop(buf);
635 * FIXME ... right now we're just sending a bounce; we really want to
636 * include the text of the bounced message.
638 if (msg->cm_fields['M'] != NULL) {
639 phree(msg->cm_fields['M']);
641 msg->cm_fields['M'] = strdoop(reason);
642 msg->cm_format_type = 0;
645 * Turn the message around
647 if (msg->cm_fields['R'] == NULL) {
648 phree(msg->cm_fields['R']);
651 if (msg->cm_fields['D'] == NULL) {
652 phree(msg->cm_fields['D']);
655 msg->cm_fields['R'] = msg->cm_fields['A'];
656 msg->cm_fields['D'] = msg->cm_fields['N'];
657 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
658 msg->cm_fields['N'] = strdoop(config.c_nodename);
660 if (!strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
661 phree(msg->cm_fields['D']);
665 * If this is a bounce of a bounce, send it to the Aide> room
666 * instead of looping around forever
668 if (msg->cm_fields['D'] == NULL) if (msg->cm_fields['R'] != NULL)
669 if (!strcasecmp(msg->cm_fields['R'], BOUNCESOURCE)) {
670 phree(msg->cm_fields['R']);
671 if (msg->cm_fields['C'] != NULL) {
672 phree(msg->cm_fields['C']);
674 msg->cm_fields['C'] = strdoop(AIDEROOM);
677 /* prepend our node to the path */
678 if (msg->cm_fields['P'] != NULL) {
679 oldpath = msg->cm_fields['P'];
680 msg->cm_fields['P'] = NULL;
683 oldpath = strdoop("unknown_user");
685 msg->cm_fields['P'] = mallok(strlen(oldpath) + SIZ);
686 sprintf(msg->cm_fields['P'], "%s!%s", config.c_nodename, oldpath);
689 /* serialize the message */
690 serialize_message(&sermsg, msg);
693 sprintf(filename, "./network/spoolin/bounce.%04x.%04x",
694 getpid(), serialnum);
696 fp = fopen(filename, "ab");
703 CtdlFreeMessage(msg);
704 lprintf(9, "leaving network_bounce()\n");
711 * Process a buffer containing a single message from a single file
712 * from the inbound queue
714 void network_process_buffer(char *buffer, long size) {
715 struct CtdlMessage *msg;
720 struct usersupp tempUS;
722 char target_room[ROOMNAMELEN];
723 struct ser_ret sermsg;
724 char *oldpath = NULL;
728 /* Set default target room to trash */
729 strcpy(target_room, TWITROOM);
731 /* Load the message into memory */
732 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
733 memset(msg, 0, sizeof(struct CtdlMessage));
734 msg->cm_magic = CTDLMESSAGE_MAGIC;
735 msg->cm_anon_type = buffer[1];
736 msg->cm_format_type = buffer[2];
738 for (pos = 3; pos < size; ++pos) {
740 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
741 pos = pos + strlen(&buffer[(int)pos]);
744 /* Check for message routing */
745 if (msg->cm_fields['D'] != NULL) {
746 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
748 /* route the message */
749 if (is_valid_node(NULL, NULL,
750 msg->cm_fields['D']) == 0) {
752 /* prepend our node to the path */
753 if (msg->cm_fields['P'] != NULL) {
754 oldpath = msg->cm_fields['P'];
755 msg->cm_fields['P'] = NULL;
758 oldpath = strdoop("unknown_user");
760 msg->cm_fields['P'] =
761 mallok(strlen(oldpath) + SIZ);
762 sprintf(msg->cm_fields['P'], "%s!%s",
763 config.c_nodename, oldpath);
766 /* serialize the message */
767 serialize_message(&sermsg, msg);
771 "./network/spoolout/%s",
772 msg->cm_fields['D']);
773 fp = fopen(filename, "ab");
780 CtdlFreeMessage(msg);
784 else { /* invalid destination node name */
787 "A message you sent could not be delivered due to an invalid destination node"
788 " name. Please check the address and try sending the message again.\n");
796 /* FIXME check to see if we already have this message */
798 /* Learn network topology from the path */
799 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
800 network_learn_topology(msg->cm_fields['N'],
801 msg->cm_fields['P']);
804 /* Does it have a recipient? If so, validate it... */
805 if (msg->cm_fields['R'] != NULL) {
807 safestrncpy(recp, msg->cm_fields['R'], sizeof(recp));
809 e = alias(recp); /* alias and mail type */
810 if ((recp[0] == 0) || (e == MES_ERROR)) {
813 "A message you sent could not be delivered due to an invalid address.\n"
814 "Please check the address and try sending the message again.\n");
819 else if (e == MES_LOCAL) {
820 a = getuser(&tempUS, recp);
824 "A message you sent could not be delivered because the user does not exist\n"
825 "on this system. Please check the address and try again.\n");
831 MailboxName(target_room, &tempUS, MAILROOM);
836 else if (msg->cm_fields['C'] != NULL) {
837 safestrncpy(target_room,
842 else if (msg->cm_fields['O'] != NULL) {
843 safestrncpy(target_room,
848 /* save the message into a room */
849 msg->cm_flags = CM_SKIP_HOOKS;
850 CtdlSaveMsg(msg, "", target_room, 0);
851 CtdlFreeMessage(msg);
856 * Process a single message from a single file from the inbound queue
858 void network_process_message(FILE *fp, long msgstart, long msgend) {
863 hold_pos = ftell(fp);
864 size = msgend - msgstart + 1;
865 buffer = mallok(size);
866 if (buffer != NULL) {
867 fseek(fp, msgstart, SEEK_SET);
868 fread(buffer, size, 1, fp);
869 network_process_buffer(buffer, size);
873 fseek(fp, hold_pos, SEEK_SET);
878 * Process a single file from the inbound queue
880 void network_process_file(char *filename) {
882 long msgstart = (-1L);
887 lprintf(7, "network: processing <%s>\n", filename);
889 fp = fopen(filename, "rb");
891 lprintf(5, "Error opening %s: %s\n",
892 filename, strerror(errno));
896 /* Look for messages in the data stream and break them out */
897 while (ch = getc(fp), ch >= 0) {
900 if (msgstart >= 0L) {
902 network_process_message(fp, msgstart, msgend);
911 if (msgstart >= 0L) {
912 network_process_message(fp, msgstart, msgend);
921 * Process anything in the inbound queue
923 void network_do_spoolin(void) {
928 dp = opendir("./network/spoolin");
929 if (dp == NULL) return;
931 while (d = readdir(dp), d != NULL) {
932 sprintf(filename, "./network/spoolin/%s", d->d_name);
933 network_process_file(filename);
945 * receive network spool from the remote system
947 void receive_spool(int sock, char *remote_nodename) {
951 static char pbuf[IGNET_PACKET_SIZE];
952 char tempfilename[PATH_MAX];
956 strcpy(tempfilename, tmpnam(NULL));
957 if (sock_puts(sock, "NDOP") < 0) return;
958 if (sock_gets(sock, buf) < 0) return;
959 lprintf(9, "<%s\n", buf);
963 download_len = extract_long(&buf[4], 0);
966 fp = fopen(tempfilename, "w");
968 lprintf(9, "cannot open download file locally: %s\n",
973 while (bytes_received < download_len) {
974 sprintf(buf, "READ %ld|%ld",
976 ((download_len - bytes_received > IGNET_PACKET_SIZE)
977 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
978 if (sock_puts(sock, buf) < 0) {
980 unlink(tempfilename);
983 if (sock_gets(sock, buf) < 0) {
985 unlink(tempfilename);
989 plen = extract_long(&buf[4], 0);
990 if (sock_read(sock, pbuf, plen) < 0) {
992 unlink(tempfilename);
995 fwrite((char *) pbuf, plen, 1, fp);
996 bytes_received = bytes_received + plen;
1001 if (sock_puts(sock, "CLOS") < 0) {
1002 unlink(tempfilename);
1005 if (sock_gets(sock, buf) < 0) {
1006 unlink(tempfilename);
1009 lprintf(9, "%s\n", buf);
1010 sprintf(buf, "mv %s ./network/spoolin/%s.%ld",
1011 tempfilename, remote_nodename, (long) getpid());
1018 * transmit network spool to the remote system
1020 void transmit_spool(int sock, char *remote_nodename)
1025 long bytes_to_write, thisblock;
1029 if (sock_puts(sock, "NUOP") < 0) return;
1030 if (sock_gets(sock, buf) < 0) return;
1031 lprintf(9, "<%s\n", buf);
1032 if (buf[0] != '2') {
1036 sprintf(sfname, "./network/spoolout/%s", remote_nodename);
1037 fd = open(sfname, O_RDONLY);
1039 if (errno == ENOENT) {
1040 lprintf(9, "Nothing to send.\n");
1042 lprintf(5, "cannot open upload file locally: %s\n",
1047 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1048 bytes_to_write = plen;
1049 while (bytes_to_write > 0L) {
1050 sprintf(buf, "WRIT %ld", bytes_to_write);
1051 if (sock_puts(sock, buf) < 0) {
1055 if (sock_gets(sock, buf) < 0) {
1059 thisblock = atol(&buf[4]);
1060 if (buf[0] == '7') {
1061 if (sock_write(sock, pbuf,
1062 (int) thisblock) < 0) {
1066 bytes_to_write = bytes_to_write - thisblock;
1075 if (sock_puts(sock, "UCLS 1") < 0) return;
1076 if (sock_gets(sock, buf) < 0) return;
1077 lprintf(9, "<%s\n", buf);
1078 if (buf[0] == '2') {
1086 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1088 void network_poll_node(char *node, char *secret, char *host, char *port) {
1092 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1094 sock = sock_connect(host, port, "tcp");
1096 lprintf(7, "Could not connect: %s\n", strerror(errno));
1100 lprintf(9, "Connected!\n");
1102 /* Read the server greeting */
1103 if (sock_gets(sock, buf) < 0) goto bail;
1104 lprintf(9, ">%s\n", buf);
1106 /* Identify ourselves */
1107 sprintf(buf, "NETP %s|%s", config.c_nodename, secret);
1108 lprintf(9, "<%s\n", buf);
1109 if (sock_puts(sock, buf) <0) goto bail;
1110 if (sock_gets(sock, buf) < 0) goto bail;
1111 lprintf(9, ">%s\n", buf);
1112 if (buf[0] != '2') goto bail;
1114 /* At this point we are authenticated. */
1115 receive_spool(sock, node);
1116 transmit_spool(sock, node);
1118 sock_puts(sock, "QUIT");
1119 bail: sock_close(sock);
1125 * Poll other Citadel nodes and transfer inbound/outbound network data.
1127 void network_poll_other_citadel_nodes(void) {
1128 char *ignetcfg = NULL;
1136 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1137 if (ignetcfg == NULL) return; /* no nodes defined */
1139 /* Use the string tokenizer to grab one line at a time */
1140 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1141 extract_token(linebuf, ignetcfg, i, '\n');
1142 extract(node, linebuf, 0);
1143 extract(secret, linebuf, 1);
1144 extract(host, linebuf, 2);
1145 extract(port, linebuf, 3);
1146 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1147 && (strlen(host) > 0) && strlen(port) > 0) {
1148 network_poll_node(node, secret, host, port);
1162 * network_do_queue()
1164 * Run through the rooms doing various types of network stuff.
1166 void network_do_queue(void) {
1167 static int doing_queue = 0;
1168 static time_t last_run = 0L;
1169 struct RoomProcList *ptr;
1172 * Run no more frequently than once every n seconds
1174 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
1177 * This is a simple concurrency check to make sure only one queue run
1178 * is done at a time. We could do this with a mutex, but since we
1179 * don't really require extremely fine granularity here, we'll do it
1180 * with a static variable instead.
1182 if (doing_queue) return;
1184 last_run = time(NULL);
1187 * Poll other Citadel nodes.
1189 network_poll_other_citadel_nodes();
1192 * Load the network map into memory.
1197 * Go ahead and run the queue
1199 lprintf(7, "network: loading outbound queue\n");
1200 ForEachRoom(network_queue_room, NULL);
1202 lprintf(7, "network: running outbound queue\n");
1203 while (rplist != NULL) {
1204 network_spoolout_room(rplist->name);
1206 rplist = rplist->next;
1210 lprintf(7, "network: processing inbound queue\n");
1211 network_do_spoolin();
1213 write_network_map();
1215 lprintf(7, "network: queue run completed\n");
1222 * cmd_netp() - authenticate to the server as another Citadel node polling
1223 * for network traffic
1225 void cmd_netp(char *cmdbuf)
1233 extract(node, cmdbuf, 0);
1234 extract(pass, cmdbuf, 1);
1236 if (is_valid_node(nexthop, secret, node) != 0) {
1237 cprintf("%d authentication failed\n", ERROR);
1241 if (strcasecmp(pass, secret)) {
1242 cprintf("%d authentication failed\n", ERROR);
1246 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1247 cprintf("%d authenticated as network node '%s'\n", OK,
1254 * Module entry point
1256 char *Dynamic_Module_Init(void)
1258 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1259 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
1260 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1261 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);