4 * This module handles shared rooms, inter-Citadel mail, and outbound
5 * mailing list processing.
7 * Copyright (C) 2000-2001 by Art Cancro and others.
8 * This code is released under the terms of the GNU General Public License.
14 * Don't allow polls during network processing
25 #include <sys/types.h>
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
32 # include <sys/time.h>
43 #include "sysdep_decls.h"
44 #include "citserver.h"
47 #include "dynloader.h"
54 #include "internet_addressing.h"
55 #include "serv_network.h"
56 #include "clientsocket.h"
61 * When we do network processing, it's accomplished in two passes; one to
62 * gather a list of rooms and one to actually do them. It's ok that rplist
63 * is global; this process *only* runs as part of the housekeeping loop and
64 * therefore only one will run at a time.
67 struct RoomProcList *next;
68 char name[ROOMNAMELEN];
71 struct RoomProcList *rplist = NULL;
75 * We build a map of the Citadel network during network runs.
84 struct NetMap *the_netmap = NULL;
89 * Read the network map from its configuration file into memory.
91 void read_network_map(void) {
92 char *serialized_map = NULL;
97 serialized_map = CtdlGetSysConfig(IGNETMAP);
98 if (serialized_map == NULL) return; /* if null, no entries */
100 /* Use the string tokenizer to grab one line at a time */
101 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
102 extract_token(buf, serialized_map, i, '\n');
103 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
104 extract(nmptr->nodename, buf, 0);
105 nmptr->lastcontact = extract_long(buf, 1);
106 extract(nmptr->nexthop, buf, 2);
107 nmptr->next = the_netmap;
111 phree(serialized_map);
116 * Write the network map from memory back to the configuration file.
118 void write_network_map(void) {
119 char *serialized_map = NULL;
120 struct NetMap *nmptr;
122 serialized_map = strdoop("");
124 if (the_netmap != NULL) {
125 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
126 serialized_map = reallok(serialized_map,
127 (strlen(serialized_map)+SIZ) );
128 if (strlen(nmptr->nodename) > 0) {
129 sprintf(&serialized_map[strlen(serialized_map)],
138 CtdlPutSysConfig(IGNETMAP, serialized_map);
139 phree(serialized_map);
141 /* Now free the list */
142 while (the_netmap != NULL) {
143 nmptr = the_netmap->next;
152 * Check the network map and determine whether the supplied node name is
153 * valid. If it is not a neighbor node, supply the name of a neighbor node
154 * which is the next hop. If it *is* a neighbor node, we also fill in the
157 int is_valid_node(char *nexthop, char *secret, char *node) {
158 char *ignetcfg = NULL;
163 struct NetMap *nmptr;
170 * First try the neighbor nodes
172 ignetcfg = CtdlGetSysConfig(IGNETCFG);
173 if (ignetcfg == NULL) {
174 if (nexthop != NULL) {
181 if (nexthop != NULL) {
185 /* Use the string tokenizer to grab one line at a time */
186 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
187 extract_token(linebuf, ignetcfg, i, '\n');
188 extract(buf, linebuf, 0);
189 if (!strcasecmp(buf, node)) {
190 if (nexthop != NULL) {
193 if (secret != NULL) {
194 extract(secret, linebuf, 1);
202 return(retval); /* yup, it's a direct neighbor */
206 * If we get to this point we have to see if we know the next hop
208 if (the_netmap != NULL) {
209 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
210 if (!strcasecmp(nmptr->nodename, node)) {
211 if (nexthop != NULL) {
212 strcpy(nexthop, nmptr->nexthop);
220 * If we get to this point, the supplied node name is bogus.
222 lprintf(5, "Invalid node name <%s>\n", node);
230 void cmd_gnet(char *argbuf) {
235 if (CtdlAccessCheck(ac_room_aide)) return;
236 assoc_file_name(filename, &CC->quickroom, "netconfigs");
237 cprintf("%d Network settings for room #%ld <%s>\n",
239 CC->quickroom.QRnumber, CC->quickroom.QRname);
241 fp = fopen(filename, "r");
243 while (fgets(buf, sizeof buf, fp) != NULL) {
244 buf[strlen(buf)-1] = 0;
245 cprintf("%s\n", buf);
254 void cmd_snet(char *argbuf) {
255 char tempfilename[SIZ];
260 if (CtdlAccessCheck(ac_room_aide)) return;
261 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
262 assoc_file_name(filename, &CC->quickroom, "netconfigs");
264 fp = fopen(tempfilename, "w");
266 cprintf("%d Cannot open %s: %s\n",
267 ERROR+INTERNAL_ERROR,
272 cprintf("%d %s\n", SEND_LISTING, tempfilename);
273 while (client_gets(buf), strcmp(buf, "000")) {
274 fprintf(fp, "%s\n", buf);
278 /* Now copy the temp file to its permanent location
279 * (We use /bin/mv instead of link() because they may be on
280 * different filesystems)
283 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
290 * Spools out one message from the list.
292 void network_spool_msg(long msgnum, void *userdata) {
293 struct SpoolControl *sc;
294 struct namelist *nptr;
298 char *newpath = NULL;
299 size_t instr_len = SIZ;
300 struct CtdlMessage *msg = NULL;
301 struct CtdlMessage *imsg;
302 struct ser_ret sermsg;
308 int delete_after_send = 0; /* Set to 1 to delete after spooling */
310 sc = (struct SpoolControl *)userdata;
313 * Process mailing list recipients
315 if (sc->listrecps != NULL) {
317 /* First, copy it to the spoolout room */
318 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
319 if (err != 0) return;
322 * Figure out how big a buffer we need to allocate
324 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
325 instr_len = instr_len + strlen(nptr->name);
331 lprintf(9, "Generating delivery instructions\n");
332 instr = mallok(instr_len);
334 lprintf(1, "Cannot allocate %d bytes for instr...\n",
339 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
340 "bounceto|postmaster@%s\n" ,
341 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
343 /* Generate delivery instructions for each recipient */
344 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
345 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
350 * Generate a message from the instructions
352 imsg = mallok(sizeof(struct CtdlMessage));
353 memset(imsg, 0, sizeof(struct CtdlMessage));
354 imsg->cm_magic = CTDLMESSAGE_MAGIC;
355 imsg->cm_anon_type = MES_NORMAL;
356 imsg->cm_format_type = FMT_RFC822;
357 imsg->cm_fields['A'] = strdoop("Citadel");
358 imsg->cm_fields['M'] = instr;
360 /* Save delivery instructions in spoolout room */
361 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
362 CtdlFreeMessage(imsg);
366 * Process IGnet push shares
368 if (sc->ignet_push_shares != NULL) {
370 msg = CtdlFetchMessage(msgnum);
373 /* Prepend our node name to the Path field whenever
374 * sending a message to another IGnet node
376 if (msg->cm_fields['P'] == NULL) {
377 msg->cm_fields['P'] = strdoop("username");
379 newpath = mallok(strlen(msg->cm_fields['P']) +
380 strlen(config.c_nodename) + 2);
381 sprintf(newpath, "%s!%s", config.c_nodename,
382 msg->cm_fields['P']);
383 phree(msg->cm_fields['P']);
384 msg->cm_fields['P'] = newpath;
387 * Force the message to appear in the correct room
388 * on the far end by setting the C field correctly
390 if (msg->cm_fields['C'] != NULL) {
391 phree(msg->cm_fields['C']);
393 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
396 * Determine if this message is set to be deleted
397 * after sending out on the network
399 if (msg->cm_fields['S'] != NULL) {
400 if (!strcasecmp(msg->cm_fields['S'],
402 delete_after_send = 1;
407 * Now serialize it for transmission
409 serialize_message(&sermsg, msg);
411 /* Now send it to every node */
412 for (nptr = sc->ignet_push_shares; nptr != NULL;
417 /* Check for valid node name */
418 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
419 lprintf(3, "Invalid node <%s>\n",
424 /* Check for split horizon */
425 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
426 bang = num_tokens(msg->cm_fields['P'], '!');
427 if (bang > 1) for (i=0; i<(bang-1); ++i) {
428 extract_token(buf, msg->cm_fields['P'],
430 if (!strcasecmp(buf, nptr->name)) {
435 /* Send the message */
438 "./network/spoolout/%s",
440 fp = fopen(filename, "ab");
449 CtdlFreeMessage(msg);
453 /* update lastsent */
454 sc->lastsent = msgnum;
456 /* Delete this message if delete-after-send is set */
457 if (delete_after_send) {
458 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
467 * Batch up and send all outbound traffic from the current room
469 void network_spoolout_room(char *room_to_spool) {
474 struct SpoolControl sc;
475 /* struct namelist *digestrecps = NULL; */
476 struct namelist *nptr;
478 lprintf(7, "Spooling <%s>\n", room_to_spool);
479 if (getroom(&CC->quickroom, room_to_spool) != 0) {
480 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
484 memset(&sc, 0, sizeof(struct SpoolControl));
485 assoc_file_name(filename, &CC->quickroom, "netconfigs");
487 fp = fopen(filename, "r");
489 lprintf(7, "Outbound batch processing skipped for <%s>\n",
490 CC->quickroom.QRname);
494 lprintf(5, "Outbound batch processing started for <%s>\n",
495 CC->quickroom.QRname);
497 while (fgets(buf, sizeof buf, fp) != NULL) {
498 buf[strlen(buf)-1] = 0;
500 extract(instr, buf, 0);
501 if (!strcasecmp(instr, "lastsent")) {
502 sc.lastsent = extract_long(buf, 1);
504 else if (!strcasecmp(instr, "listrecp")) {
505 nptr = (struct namelist *)
506 mallok(sizeof(struct namelist));
507 nptr->next = sc.listrecps;
508 extract(nptr->name, buf, 1);
511 else if (!strcasecmp(instr, "ignet_push_share")) {
512 nptr = (struct namelist *)
513 mallok(sizeof(struct namelist));
514 nptr->next = sc.ignet_push_shares;
515 extract(nptr->name, buf, 1);
516 sc.ignet_push_shares = nptr;
524 /* Do something useful */
525 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
526 network_spool_msg, &sc);
529 /* Now rewrite the config file */
530 fp = fopen(filename, "w");
532 lprintf(1, "ERROR: cannot open %s: %s\n",
533 filename, strerror(errno));
536 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
538 /* Write out the listrecps while freeing from memory at the
539 * same time. Am I clever or what? :)
541 while (sc.listrecps != NULL) {
542 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
543 nptr = sc.listrecps->next;
547 while (sc.ignet_push_shares != NULL) {
548 fprintf(fp, "ignet_push_share|%s\n",
549 sc.ignet_push_shares->name);
550 nptr = sc.ignet_push_shares->next;
551 phree(sc.ignet_push_shares);
552 sc.ignet_push_shares = nptr;
558 lprintf(5, "Outbound batch processing finished for <%s>\n",
559 CC->quickroom.QRname);
564 * Batch up and send all outbound traffic from the current room
566 void network_queue_room(struct quickroom *qrbuf, void *data) {
567 struct RoomProcList *ptr;
569 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
570 if (ptr == NULL) return;
572 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
579 * Learn topology from path fields
581 void network_learn_topology(char *node, char *path) {
583 struct NetMap *nmptr;
587 if (num_tokens(path, '!') < 3) return;
588 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
589 if (!strcasecmp(nmptr->nodename, node)) {
590 extract_token(nmptr->nexthop, path, 0, '!');
591 nmptr->lastcontact = time(NULL);
596 /* If we got here then it's not in the map, so add it. */
597 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
598 strcpy(nmptr->nodename, node);
599 nmptr->lastcontact = time(NULL);
600 extract_token(nmptr->nexthop, path, 0, '!');
601 nmptr->next = the_netmap;
609 * Bounce a message back to the sender
611 void network_bounce(struct CtdlMessage *msg, char *reason) {
612 static int serialnum = 0;
615 struct ser_ret sermsg;
616 char *oldpath = NULL;
619 lprintf(9, "entering network_bounce()\n");
621 if (msg == NULL) return;
624 * Give it a fresh message ID
626 if (msg->cm_fields['I'] != NULL) {
627 phree(msg->cm_fields['I']);
629 sprintf(buf, "%ld.%04x.%04x@%s",
630 time(NULL), getpid(), ++serialnum, config.c_fqdn);
631 msg->cm_fields['I'] = strdoop(buf);
634 * FIXME ... right now we're just sending a bounce; we really want to
635 * include the text of the bounced message.
637 if (msg->cm_fields['M'] != NULL) {
638 phree(msg->cm_fields['M']);
640 msg->cm_fields['M'] = strdoop(reason);
641 msg->cm_format_type = 0;
644 * Turn the message around
646 if (msg->cm_fields['R'] == NULL) {
647 phree(msg->cm_fields['R']);
650 if (msg->cm_fields['D'] == NULL) {
651 phree(msg->cm_fields['D']);
654 msg->cm_fields['R'] = msg->cm_fields['A'];
655 msg->cm_fields['D'] = msg->cm_fields['N'];
656 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
657 msg->cm_fields['N'] = strdoop(config.c_nodename);
659 if (!strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
660 phree(msg->cm_fields['D']);
664 * If this is a bounce of a bounce, send it to the Aide> room
665 * instead of looping around forever
667 if (msg->cm_fields['D'] == NULL) if (msg->cm_fields['R'] != NULL)
668 if (!strcasecmp(msg->cm_fields['R'], BOUNCESOURCE)) {
669 phree(msg->cm_fields['R']);
670 if (msg->cm_fields['C'] != NULL) {
671 phree(msg->cm_fields['C']);
673 msg->cm_fields['C'] = strdoop(AIDEROOM);
676 /* prepend our node to the path */
677 if (msg->cm_fields['P'] != NULL) {
678 oldpath = msg->cm_fields['P'];
679 msg->cm_fields['P'] = NULL;
682 oldpath = strdoop("unknown_user");
684 msg->cm_fields['P'] = mallok(strlen(oldpath) + SIZ);
685 sprintf(msg->cm_fields['P'], "%s!%s", config.c_nodename, oldpath);
688 /* serialize the message */
689 serialize_message(&sermsg, msg);
692 sprintf(filename, "./network/spoolin/bounce.%04x.%04x",
693 getpid(), serialnum);
695 fp = fopen(filename, "ab");
702 CtdlFreeMessage(msg);
703 lprintf(9, "leaving network_bounce()\n");
710 * Process a buffer containing a single message from a single file
711 * from the inbound queue
713 void network_process_buffer(char *buffer, long size) {
714 struct CtdlMessage *msg;
719 struct usersupp tempUS;
721 char target_room[ROOMNAMELEN];
722 struct ser_ret sermsg;
723 char *oldpath = NULL;
727 /* Set default target room to trash */
728 strcpy(target_room, TWITROOM);
730 /* Load the message into memory */
731 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
732 memset(msg, 0, sizeof(struct CtdlMessage));
733 msg->cm_magic = CTDLMESSAGE_MAGIC;
734 msg->cm_anon_type = buffer[1];
735 msg->cm_format_type = buffer[2];
737 for (pos = 3; pos < size; ++pos) {
739 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
740 pos = pos + strlen(&buffer[(int)pos]);
743 /* Check for message routing */
744 if (msg->cm_fields['D'] != NULL) {
745 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
747 /* route the message */
748 if (is_valid_node(NULL, NULL,
749 msg->cm_fields['D']) == 0) {
751 /* prepend our node to the path */
752 if (msg->cm_fields['P'] != NULL) {
753 oldpath = msg->cm_fields['P'];
754 msg->cm_fields['P'] = NULL;
757 oldpath = strdoop("unknown_user");
759 msg->cm_fields['P'] =
760 mallok(strlen(oldpath) + SIZ);
761 sprintf(msg->cm_fields['P'], "%s!%s",
762 config.c_nodename, oldpath);
765 /* serialize the message */
766 serialize_message(&sermsg, msg);
770 "./network/spoolout/%s",
771 msg->cm_fields['D']);
772 fp = fopen(filename, "ab");
779 CtdlFreeMessage(msg);
783 else { /* invalid destination node name */
786 "A message you sent could not be delivered due to an invalid destination node"
787 " name. Please check the address and try sending the message again.\n");
795 /* FIXME check to see if we already have this message */
797 /* Learn network topology from the path */
798 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
799 network_learn_topology(msg->cm_fields['N'],
800 msg->cm_fields['P']);
803 /* Does it have a recipient? If so, validate it... */
804 if (msg->cm_fields['R'] != NULL) {
806 safestrncpy(recp, msg->cm_fields['R'], sizeof(recp));
808 e = alias(recp); /* alias and mail type */
809 if ((recp[0] == 0) || (e == MES_ERROR)) {
812 "A message you sent could not be delivered due to an invalid address.\n"
813 "Please check the address and try sending the message again.\n");
818 else if (e == MES_LOCAL) {
819 a = getuser(&tempUS, recp);
823 "A message you sent could not be delivered because the user does not exist\n"
824 "on this system. Please check the address and try again.\n");
830 MailboxName(target_room, &tempUS, MAILROOM);
835 else if (msg->cm_fields['C'] != NULL) {
836 safestrncpy(target_room,
841 else if (msg->cm_fields['O'] != NULL) {
842 safestrncpy(target_room,
847 /* save the message into a room */
848 msg->cm_flags = CM_SKIP_HOOKS;
849 CtdlSaveMsg(msg, "", target_room, 0);
850 CtdlFreeMessage(msg);
855 * Process a single message from a single file from the inbound queue
857 void network_process_message(FILE *fp, long msgstart, long msgend) {
862 hold_pos = ftell(fp);
863 size = msgend - msgstart + 1;
864 buffer = mallok(size);
865 if (buffer != NULL) {
866 fseek(fp, msgstart, SEEK_SET);
867 fread(buffer, size, 1, fp);
868 network_process_buffer(buffer, size);
872 fseek(fp, hold_pos, SEEK_SET);
877 * Process a single file from the inbound queue
879 void network_process_file(char *filename) {
881 long msgstart = (-1L);
886 lprintf(7, "network: processing <%s>\n", filename);
888 fp = fopen(filename, "rb");
890 lprintf(5, "Error opening %s: %s\n",
891 filename, strerror(errno));
895 /* Look for messages in the data stream and break them out */
896 while (ch = getc(fp), ch >= 0) {
899 if (msgstart >= 0L) {
901 network_process_message(fp, msgstart, msgend);
910 if (msgstart >= 0L) {
911 network_process_message(fp, msgstart, msgend);
920 * Process anything in the inbound queue
922 void network_do_spoolin(void) {
927 dp = opendir("./network/spoolin");
928 if (dp == NULL) return;
930 while (d = readdir(dp), d != NULL) {
931 sprintf(filename, "./network/spoolin/%s", d->d_name);
932 network_process_file(filename);
944 * receive network spool from the remote system
946 void receive_spool(int sock, char *remote_nodename) {
950 static char pbuf[IGNET_PACKET_SIZE];
951 char tempfilename[PATH_MAX];
955 strcpy(tempfilename, tmpnam(NULL));
956 if (sock_puts(sock, "NDOP") < 0) return;
957 if (sock_gets(sock, buf) < 0) return;
958 lprintf(9, "<%s\n", buf);
962 download_len = extract_long(&buf[4], 0);
965 fp = fopen(tempfilename, "w");
967 lprintf(9, "cannot open download file locally: %s\n",
972 while (bytes_received < download_len) {
973 sprintf(buf, "READ %ld|%ld",
975 ((download_len - bytes_received > IGNET_PACKET_SIZE)
976 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
977 if (sock_puts(sock, buf) < 0) {
979 unlink(tempfilename);
982 if (sock_gets(sock, buf) < 0) {
984 unlink(tempfilename);
988 plen = extract_long(&buf[4], 0);
989 if (sock_read(sock, pbuf, plen) < 0) {
991 unlink(tempfilename);
994 fwrite((char *) pbuf, plen, 1, fp);
995 bytes_received = bytes_received + plen;
1000 if (sock_puts(sock, "CLOS") < 0) {
1001 unlink(tempfilename);
1004 if (sock_gets(sock, buf) < 0) {
1005 unlink(tempfilename);
1008 lprintf(9, "%s\n", buf);
1009 sprintf(buf, "mv %s ./network/spoolin/%s.%ld",
1010 tempfilename, remote_nodename, (long) getpid());
1017 * transmit network spool to the remote system
1019 void transmit_spool(int sock, char *remote_nodename)
1024 long bytes_to_write, thisblock;
1028 if (sock_puts(sock, "NUOP") < 0) return;
1029 if (sock_gets(sock, buf) < 0) return;
1030 lprintf(9, "<%s\n", buf);
1031 if (buf[0] != '2') {
1035 sprintf(sfname, "./network/spoolout/%s", remote_nodename);
1036 fd = open(sfname, O_RDONLY);
1038 if (errno == ENOENT) {
1039 lprintf(9, "Nothing to send.\n");
1041 lprintf(5, "cannot open upload file locally: %s\n",
1046 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1047 bytes_to_write = plen;
1048 while (bytes_to_write > 0L) {
1049 sprintf(buf, "WRIT %ld", bytes_to_write);
1050 if (sock_puts(sock, buf) < 0) {
1054 if (sock_gets(sock, buf) < 0) {
1058 thisblock = atol(&buf[4]);
1059 if (buf[0] == '7') {
1060 if (sock_write(sock, pbuf,
1061 (int) thisblock) < 0) {
1065 bytes_to_write = bytes_to_write - thisblock;
1074 if (sock_puts(sock, "UCLS 1") < 0) return;
1075 if (sock_gets(sock, buf) < 0) return;
1076 lprintf(9, "<%s\n", buf);
1077 if (buf[0] == '2') {
1085 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1087 void network_poll_node(char *node, char *secret, char *host, char *port) {
1091 if (network_talking_to(node, NTT_CHECK)) return;
1092 network_talking_to(node, NTT_ADD);
1093 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1095 sock = sock_connect(host, port, "tcp");
1097 lprintf(7, "Could not connect: %s\n", strerror(errno));
1098 network_talking_to(node, NTT_REMOVE);
1102 lprintf(9, "Connected!\n");
1104 /* Read the server greeting */
1105 if (sock_gets(sock, buf) < 0) goto bail;
1106 lprintf(9, ">%s\n", buf);
1108 /* Identify ourselves */
1109 sprintf(buf, "NETP %s|%s", config.c_nodename, secret);
1110 lprintf(9, "<%s\n", buf);
1111 if (sock_puts(sock, buf) <0) goto bail;
1112 if (sock_gets(sock, buf) < 0) goto bail;
1113 lprintf(9, ">%s\n", buf);
1114 if (buf[0] != '2') goto bail;
1116 /* At this point we are authenticated. */
1117 receive_spool(sock, node);
1118 transmit_spool(sock, node);
1120 sock_puts(sock, "QUIT");
1121 bail: sock_close(sock);
1122 network_talking_to(node, NTT_REMOVE);
1128 * Poll other Citadel nodes and transfer inbound/outbound network data.
1130 void network_poll_other_citadel_nodes(void) {
1131 char *ignetcfg = NULL;
1139 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1140 if (ignetcfg == NULL) return; /* no nodes defined */
1142 /* Use the string tokenizer to grab one line at a time */
1143 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1144 extract_token(linebuf, ignetcfg, i, '\n');
1145 extract(node, linebuf, 0);
1146 extract(secret, linebuf, 1);
1147 extract(host, linebuf, 2);
1148 extract(port, linebuf, 3);
1149 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1150 && (strlen(host) > 0) && strlen(port) > 0) {
1151 network_poll_node(node, secret, host, port);
1165 * network_do_queue()
1167 * Run through the rooms doing various types of network stuff.
1169 void network_do_queue(void) {
1170 static int doing_queue = 0;
1171 static time_t last_run = 0L;
1172 struct RoomProcList *ptr;
1175 * Run no more frequently than once every n seconds
1177 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
1180 * This is a simple concurrency check to make sure only one queue run
1181 * is done at a time. We could do this with a mutex, but since we
1182 * don't really require extremely fine granularity here, we'll do it
1183 * with a static variable instead.
1185 if (doing_queue) return;
1187 last_run = time(NULL);
1190 * Poll other Citadel nodes.
1192 network_poll_other_citadel_nodes();
1195 * Load the network map into memory.
1200 * Go ahead and run the queue
1202 lprintf(7, "network: loading outbound queue\n");
1203 ForEachRoom(network_queue_room, NULL);
1205 lprintf(7, "network: running outbound queue\n");
1206 while (rplist != NULL) {
1207 network_spoolout_room(rplist->name);
1209 rplist = rplist->next;
1213 lprintf(7, "network: processing inbound queue\n");
1214 network_do_spoolin();
1216 write_network_map();
1218 lprintf(7, "network: queue run completed\n");
1225 * cmd_netp() - authenticate to the server as another Citadel node polling
1226 * for network traffic
1228 void cmd_netp(char *cmdbuf)
1236 extract(node, cmdbuf, 0);
1237 extract(pass, cmdbuf, 1);
1239 if (is_valid_node(nexthop, secret, node) != 0) {
1240 cprintf("%d authentication failed\n", ERROR);
1244 if (strcasecmp(pass, secret)) {
1245 cprintf("%d authentication failed\n", ERROR);
1249 if (network_talking_to(node, NTT_CHECK)) {
1250 cprintf("%d Already talking to %s right now\n", ERROR);
1254 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1255 network_talking_to(node, NTT_ADD);
1256 cprintf("%d authenticated as network node '%s'\n", OK,
1263 * Module entry point
1265 char *Dynamic_Module_Init(void)
1267 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1268 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
1269 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1270 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);