4 * This module handles shared rooms, inter-Citadel mail, and outbound
5 * mailing list processing.
7 * Copyright (C) 2000-2002 by Art Cancro and others.
8 * This code is released under the terms of the GNU General Public License.
14 * Don't allow polls during network processing
25 #include <sys/types.h>
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
32 # include <sys/time.h>
43 #include "sysdep_decls.h"
44 #include "citserver.h"
47 #include "dynloader.h"
54 #include "internet_addressing.h"
55 #include "serv_network.h"
56 #include "clientsocket.h"
65 * When we do network processing, it's accomplished in two passes; one to
66 * gather a list of rooms and one to actually do them. It's ok that rplist
67 * is global; this process *only* runs as part of the housekeeping loop and
68 * therefore only one will run at a time.
70 struct RoomProcList *rplist = NULL;
73 * We build a map of network nodes during processing.
75 struct NetMap *the_netmap = NULL;
79 * Check the use table. This is a list of messages which have recently
80 * arrived on the system. It is maintained and queried to prevent the same
81 * message from being entered into the database multiple times if it happens
82 * to arrive multiple times by accident.
84 int network_usetable(struct CtdlMessage *msg) {
87 struct cdbdata *cdbut;
90 /* Bail out if we can't generate a message ID */
94 if (msg->cm_fields['I'] == NULL) {
97 if (strlen(msg->cm_fields['I']) == 0) {
101 /* Generate the message ID */
102 strcpy(msgid, msg->cm_fields['I']);
103 if (haschar(msgid, '@') == 0) {
105 if (msg->cm_fields['N'] != NULL) {
106 strcat(msgid, msg->cm_fields['N']);
113 cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
119 /* If we got to this point, it's unique: add it. */
120 timestamp = time(NULL);
121 cdb_store(CDB_USETABLE, msgid, strlen(msgid),
122 ×tamp, sizeof(timestamp) );
128 * Read the network map from its configuration file into memory.
130 void read_network_map(void) {
131 char *serialized_map = NULL;
134 struct NetMap *nmptr;
136 serialized_map = CtdlGetSysConfig(IGNETMAP);
137 if (serialized_map == NULL) return; /* if null, no entries */
139 /* Use the string tokenizer to grab one line at a time */
140 for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
141 extract_token(buf, serialized_map, i, '\n');
142 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
143 extract(nmptr->nodename, buf, 0);
144 nmptr->lastcontact = extract_long(buf, 1);
145 extract(nmptr->nexthop, buf, 2);
146 nmptr->next = the_netmap;
150 phree(serialized_map);
155 * Write the network map from memory back to the configuration file.
157 void write_network_map(void) {
158 char *serialized_map = NULL;
159 struct NetMap *nmptr;
161 serialized_map = strdoop("");
163 if (the_netmap != NULL) {
164 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
165 serialized_map = reallok(serialized_map,
166 (strlen(serialized_map)+SIZ) );
167 if (strlen(nmptr->nodename) > 0) {
168 sprintf(&serialized_map[strlen(serialized_map)],
171 (long)nmptr->lastcontact,
177 CtdlPutSysConfig(IGNETMAP, serialized_map);
178 phree(serialized_map);
180 /* Now free the list */
181 while (the_netmap != NULL) {
182 nmptr = the_netmap->next;
191 * Check the network map and determine whether the supplied node name is
192 * valid. If it is not a neighbor node, supply the name of a neighbor node
193 * which is the next hop. If it *is* a neighbor node, we also fill in the
196 int is_valid_node(char *nexthop, char *secret, char *node) {
197 char *ignetcfg = NULL;
202 struct NetMap *nmptr;
209 * First try the neighbor nodes
211 ignetcfg = CtdlGetSysConfig(IGNETCFG);
212 if (ignetcfg == NULL) {
213 if (nexthop != NULL) {
220 if (nexthop != NULL) {
224 /* Use the string tokenizer to grab one line at a time */
225 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
226 extract_token(linebuf, ignetcfg, i, '\n');
227 extract(buf, linebuf, 0);
228 if (!strcasecmp(buf, node)) {
229 if (nexthop != NULL) {
232 if (secret != NULL) {
233 extract(secret, linebuf, 1);
241 return(retval); /* yup, it's a direct neighbor */
245 * If we get to this point we have to see if we know the next hop
247 if (the_netmap != NULL) {
248 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
249 if (!strcasecmp(nmptr->nodename, node)) {
250 if (nexthop != NULL) {
251 strcpy(nexthop, nmptr->nexthop);
259 * If we get to this point, the supplied node name is bogus.
261 lprintf(5, "Invalid node name <%s>\n", node);
269 void cmd_gnet(char *argbuf) {
274 if (CtdlAccessCheck(ac_room_aide)) return;
275 assoc_file_name(filename, &CC->quickroom, "netconfigs");
276 cprintf("%d Network settings for room #%ld <%s>\n",
278 CC->quickroom.QRnumber, CC->quickroom.QRname);
280 fp = fopen(filename, "r");
282 while (fgets(buf, sizeof buf, fp) != NULL) {
283 buf[strlen(buf)-1] = 0;
284 cprintf("%s\n", buf);
293 void cmd_snet(char *argbuf) {
294 char tempfilename[SIZ];
299 if (CtdlAccessCheck(ac_room_aide)) return;
300 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
301 assoc_file_name(filename, &CC->quickroom, "netconfigs");
303 fp = fopen(tempfilename, "w");
305 cprintf("%d Cannot open %s: %s\n",
306 ERROR+INTERNAL_ERROR,
311 cprintf("%d %s\n", SEND_LISTING, tempfilename);
312 while (client_gets(buf), strcmp(buf, "000")) {
313 fprintf(fp, "%s\n", buf);
317 /* Now copy the temp file to its permanent location
318 * (We use /bin/mv instead of link() because they may be on
319 * different filesystems)
322 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
329 * Spools out one message from the list.
331 void network_spool_msg(long msgnum, void *userdata) {
332 struct SpoolControl *sc;
333 struct namelist *nptr;
337 char *newpath = NULL;
338 size_t instr_len = SIZ;
339 struct CtdlMessage *msg = NULL;
340 struct CtdlMessage *imsg;
341 struct ser_ret sermsg;
347 int delete_after_send = 0; /* Set to 1 to delete after spooling */
349 sc = (struct SpoolControl *)userdata;
352 * Process mailing list recipients
354 if (sc->listrecps != NULL) {
356 /* First, copy it to the spoolout room */
357 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
358 if (err != 0) return;
361 * Figure out how big a buffer we need to allocate
363 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
364 instr_len = instr_len + strlen(nptr->name);
370 lprintf(9, "Generating delivery instructions\n");
371 instr = mallok(instr_len);
373 lprintf(1, "Cannot allocate %ld bytes for instr...\n",
378 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
379 "bounceto|postmaster@%s\n" ,
380 SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
382 /* Generate delivery instructions for each recipient */
383 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
384 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
389 * Generate a message from the instructions
391 imsg = mallok(sizeof(struct CtdlMessage));
392 memset(imsg, 0, sizeof(struct CtdlMessage));
393 imsg->cm_magic = CTDLMESSAGE_MAGIC;
394 imsg->cm_anon_type = MES_NORMAL;
395 imsg->cm_format_type = FMT_RFC822;
396 imsg->cm_fields['A'] = strdoop("Citadel");
397 imsg->cm_fields['M'] = instr;
399 /* Save delivery instructions in spoolout room */
400 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
401 CtdlFreeMessage(imsg);
405 * Process IGnet push shares
407 if (sc->ignet_push_shares != NULL) {
409 msg = CtdlFetchMessage(msgnum);
412 /* Prepend our node name to the Path field whenever
413 * sending a message to another IGnet node
415 if (msg->cm_fields['P'] == NULL) {
416 msg->cm_fields['P'] = strdoop("username");
418 newpath = mallok(strlen(msg->cm_fields['P']) +
419 strlen(config.c_nodename) + 2);
420 sprintf(newpath, "%s!%s", config.c_nodename,
421 msg->cm_fields['P']);
422 phree(msg->cm_fields['P']);
423 msg->cm_fields['P'] = newpath;
426 * Force the message to appear in the correct room
427 * on the far end by setting the C field correctly
429 if (msg->cm_fields['C'] != NULL) {
430 phree(msg->cm_fields['C']);
432 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
435 * Determine if this message is set to be deleted
436 * after sending out on the network
438 if (msg->cm_fields['S'] != NULL) {
439 if (!strcasecmp(msg->cm_fields['S'],
441 delete_after_send = 1;
446 * Now serialize it for transmission
448 serialize_message(&sermsg, msg);
450 /* Now send it to every node */
451 for (nptr = sc->ignet_push_shares; nptr != NULL;
456 /* Check for valid node name */
457 if (is_valid_node(NULL,NULL,nptr->name) != 0) {
458 lprintf(3, "Invalid node <%s>\n",
463 /* Check for split horizon */
464 lprintf(9, "Path is %s\n", msg->cm_fields['P']);
465 bang = num_tokens(msg->cm_fields['P'], '!');
466 if (bang > 1) for (i=0; i<(bang-1); ++i) {
467 extract_token(buf, msg->cm_fields['P'],
469 if (!strcasecmp(buf, nptr->name)) {
474 /* Send the message */
477 "./network/spoolout/%s",
479 fp = fopen(filename, "ab");
488 CtdlFreeMessage(msg);
492 /* update lastsent */
493 sc->lastsent = msgnum;
495 /* Delete this message if delete-after-send is set */
496 if (delete_after_send) {
497 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
506 * Batch up and send all outbound traffic from the current room
508 void network_spoolout_room(char *room_to_spool) {
513 struct SpoolControl sc;
514 /* struct namelist *digestrecps = NULL; */
515 struct namelist *nptr;
517 lprintf(7, "Spooling <%s>\n", room_to_spool);
518 if (getroom(&CC->quickroom, room_to_spool) != 0) {
519 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
523 memset(&sc, 0, sizeof(struct SpoolControl));
524 assoc_file_name(filename, &CC->quickroom, "netconfigs");
526 fp = fopen(filename, "r");
528 lprintf(7, "Outbound batch processing skipped for <%s>\n",
529 CC->quickroom.QRname);
533 lprintf(5, "Outbound batch processing started for <%s>\n",
534 CC->quickroom.QRname);
536 while (fgets(buf, sizeof buf, fp) != NULL) {
537 buf[strlen(buf)-1] = 0;
539 extract(instr, buf, 0);
540 if (!strcasecmp(instr, "lastsent")) {
541 sc.lastsent = extract_long(buf, 1);
543 else if (!strcasecmp(instr, "listrecp")) {
544 nptr = (struct namelist *)
545 mallok(sizeof(struct namelist));
546 nptr->next = sc.listrecps;
547 extract(nptr->name, buf, 1);
550 else if (!strcasecmp(instr, "ignet_push_share")) {
551 nptr = (struct namelist *)
552 mallok(sizeof(struct namelist));
553 nptr->next = sc.ignet_push_shares;
554 extract(nptr->name, buf, 1);
555 sc.ignet_push_shares = nptr;
563 /* Do something useful */
564 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
565 network_spool_msg, &sc);
568 /* Now rewrite the config file */
569 fp = fopen(filename, "w");
571 lprintf(1, "ERROR: cannot open %s: %s\n",
572 filename, strerror(errno));
575 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
577 /* Write out the listrecps while freeing from memory at the
578 * same time. Am I clever or what? :)
580 while (sc.listrecps != NULL) {
581 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
582 nptr = sc.listrecps->next;
586 while (sc.ignet_push_shares != NULL) {
587 fprintf(fp, "ignet_push_share|%s\n",
588 sc.ignet_push_shares->name);
589 nptr = sc.ignet_push_shares->next;
590 phree(sc.ignet_push_shares);
591 sc.ignet_push_shares = nptr;
597 lprintf(5, "Outbound batch processing finished for <%s>\n",
598 CC->quickroom.QRname);
603 * Batch up and send all outbound traffic from the current room
605 void network_queue_room(struct quickroom *qrbuf, void *data) {
606 struct RoomProcList *ptr;
608 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
609 if (ptr == NULL) return;
611 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
618 * Learn topology from path fields
620 void network_learn_topology(char *node, char *path) {
622 struct NetMap *nmptr;
626 if (num_tokens(path, '!') < 3) return;
627 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
628 if (!strcasecmp(nmptr->nodename, node)) {
629 extract_token(nmptr->nexthop, path, 0, '!');
630 nmptr->lastcontact = time(NULL);
635 /* If we got here then it's not in the map, so add it. */
636 nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
637 strcpy(nmptr->nodename, node);
638 nmptr->lastcontact = time(NULL);
639 extract_token(nmptr->nexthop, path, 0, '!');
640 nmptr->next = the_netmap;
648 * Bounce a message back to the sender
650 void network_bounce(struct CtdlMessage *msg, char *reason) {
651 char *oldpath = NULL;
653 char bouncesource[SIZ];
655 struct recptypes *valid = NULL;
656 char force_room[ROOMNAMELEN];
657 static int serialnum = 0;
659 lprintf(9, "entering network_bounce()\n");
661 if (msg == NULL) return;
663 sprintf(bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
666 * Give it a fresh message ID
668 if (msg->cm_fields['I'] != NULL) {
669 phree(msg->cm_fields['I']);
671 sprintf(buf, "%ld.%04x.%04x@%s",
672 (long)time(NULL), getpid(), ++serialnum, config.c_fqdn);
673 msg->cm_fields['I'] = strdoop(buf);
676 * FIXME ... right now we're just sending a bounce; we really want to
677 * include the text of the bounced message.
679 if (msg->cm_fields['M'] != NULL) {
680 phree(msg->cm_fields['M']);
682 msg->cm_fields['M'] = strdoop(reason);
683 msg->cm_format_type = 0;
686 * Turn the message around
688 if (msg->cm_fields['R'] == NULL) {
689 phree(msg->cm_fields['R']);
692 if (msg->cm_fields['D'] == NULL) {
693 phree(msg->cm_fields['D']);
696 snprintf(recipient, sizeof recipient, "%s@%s",
697 msg->cm_fields['A'], msg->cm_fields['N']);
699 if (msg->cm_fields['A'] == NULL) {
700 phree(msg->cm_fields['A']);
703 if (msg->cm_fields['N'] == NULL) {
704 phree(msg->cm_fields['N']);
707 msg->cm_fields['A'] = strdoop(BOUNCESOURCE);
708 msg->cm_fields['N'] = strdoop(config.c_nodename);
711 /* prepend our node to the path */
712 if (msg->cm_fields['P'] != NULL) {
713 oldpath = msg->cm_fields['P'];
714 msg->cm_fields['P'] = NULL;
717 oldpath = strdoop("unknown_user");
719 msg->cm_fields['P'] = mallok(strlen(oldpath) + SIZ);
720 sprintf(msg->cm_fields['P'], "%s!%s", config.c_nodename, oldpath);
723 /* Now submit the message */
724 valid = validate_recipients(recipient);
725 if (valid != NULL) if (valid->num_error > 0) {
729 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
730 strcpy(force_room, AIDEROOM);
733 strcpy(force_room, "");
735 if ( (valid == NULL) && (strlen(force_room) == 0) ) {
736 strcpy(force_room, AIDEROOM);
738 CtdlSubmitMsg(msg, valid, force_room);
741 if (valid != NULL) phree(valid);
742 CtdlFreeMessage(msg);
743 lprintf(9, "leaving network_bounce()\n");
750 * Process a buffer containing a single message from a single file
751 * from the inbound queue
753 void network_process_buffer(char *buffer, long size) {
754 struct CtdlMessage *msg;
757 struct recptypes *recp = NULL;
758 char target_room[ROOMNAMELEN];
759 struct ser_ret sermsg;
760 char *oldpath = NULL;
765 /* Set default target room to trash */
766 strcpy(target_room, TWITROOM);
768 /* Load the message into memory */
769 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
770 memset(msg, 0, sizeof(struct CtdlMessage));
771 msg->cm_magic = CTDLMESSAGE_MAGIC;
772 msg->cm_anon_type = buffer[1];
773 msg->cm_format_type = buffer[2];
775 for (pos = 3; pos < size; ++pos) {
777 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
778 pos = pos + strlen(&buffer[(int)pos]);
781 /* Check for message routing */
782 if (msg->cm_fields['D'] != NULL) {
783 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
785 /* route the message */
786 if (is_valid_node(NULL, NULL,
787 msg->cm_fields['D']) == 0) {
789 /* prepend our node to the path */
790 if (msg->cm_fields['P'] != NULL) {
791 oldpath = msg->cm_fields['P'];
792 msg->cm_fields['P'] = NULL;
795 oldpath = strdoop("unknown_user");
797 msg->cm_fields['P'] =
798 mallok(strlen(oldpath) + SIZ);
799 sprintf(msg->cm_fields['P'], "%s!%s",
800 config.c_nodename, oldpath);
803 /* serialize the message */
804 serialize_message(&sermsg, msg);
808 "./network/spoolout/%s",
809 msg->cm_fields['D']);
810 fp = fopen(filename, "ab");
817 CtdlFreeMessage(msg);
821 else { /* invalid destination node name */
824 "A message you sent could not be delivered due to an invalid destination node"
825 " name. Please check the address and try sending the message again.\n");
834 * Check to see if we already have a copy of this message
836 if (network_usetable(msg) != 0) {
838 "Loopzapper rejected message <%s> "
839 "from <%s> in <%s> @ <%s>\n",
840 ((msg->cm_fields['I']!=NULL)?(msg->cm_fields['I']):""),
841 ((msg->cm_fields['A']!=NULL)?(msg->cm_fields['A']):""),
842 ((msg->cm_fields['O']!=NULL)?(msg->cm_fields['O']):""),
843 ((msg->cm_fields['N']!=NULL)?(msg->cm_fields['N']):"")
846 CtdlFreeMessage(msg);
851 /* Learn network topology from the path */
852 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
853 network_learn_topology(msg->cm_fields['N'],
854 msg->cm_fields['P']);
857 /* Does it have a recipient? If so, validate it... */
858 if (msg->cm_fields['R'] != NULL) {
859 recp = validate_recipients(msg->cm_fields['R']);
860 if (recp != NULL) if (recp->num_error > 0) {
862 "A message you sent could not be delivered due to an invalid address.\n"
863 "Please check the address and try sending the message again.\n");
868 strcpy(target_room, ""); /* no target room if mail */
871 else if (msg->cm_fields['C'] != NULL) {
872 safestrncpy(target_room,
877 else if (msg->cm_fields['O'] != NULL) {
878 safestrncpy(target_room,
883 /* Strip out fields that are only relevant during transit */
884 if (msg->cm_fields['D'] != NULL) {
885 phree(msg->cm_fields['D']);
886 msg->cm_fields['D'] = NULL;
888 if (msg->cm_fields['C'] != NULL) {
889 phree(msg->cm_fields['C']);
890 msg->cm_fields['C'] = NULL;
893 /* save the message into a room */
894 if (PerformNetprocHooks(msg, target_room) == 0) {
895 msg->cm_flags = CM_SKIP_HOOKS;
896 CtdlSubmitMsg(msg, recp, target_room);
898 CtdlFreeMessage(msg);
904 * Process a single message from a single file from the inbound queue
906 void network_process_message(FILE *fp, long msgstart, long msgend) {
911 hold_pos = ftell(fp);
912 size = msgend - msgstart + 1;
913 buffer = mallok(size);
914 if (buffer != NULL) {
915 fseek(fp, msgstart, SEEK_SET);
916 fread(buffer, size, 1, fp);
917 network_process_buffer(buffer, size);
921 fseek(fp, hold_pos, SEEK_SET);
926 * Process a single file from the inbound queue
928 void network_process_file(char *filename) {
930 long msgstart = (-1L);
935 lprintf(7, "network: processing <%s>\n", filename);
937 fp = fopen(filename, "rb");
939 lprintf(5, "Error opening %s: %s\n",
940 filename, strerror(errno));
944 /* Look for messages in the data stream and break them out */
945 while (ch = getc(fp), ch >= 0) {
948 if (msgstart >= 0L) {
950 network_process_message(fp, msgstart, msgend);
959 if (msgstart >= 0L) {
960 network_process_message(fp, msgstart, msgend);
969 * Process anything in the inbound queue
971 void network_do_spoolin(void) {
976 dp = opendir("./network/spoolin");
977 if (dp == NULL) return;
979 while (d = readdir(dp), d != NULL) {
980 sprintf(filename, "./network/spoolin/%s", d->d_name);
981 network_process_file(filename);
993 * receive network spool from the remote system
995 void receive_spool(int sock, char *remote_nodename) {
999 static char pbuf[IGNET_PACKET_SIZE];
1000 char tempfilename[PATH_MAX];
1004 strcpy(tempfilename, tmpnam(NULL));
1005 if (sock_puts(sock, "NDOP") < 0) return;
1006 if (sock_gets(sock, buf) < 0) return;
1007 lprintf(9, "<%s\n", buf);
1008 if (buf[0] != '2') {
1011 download_len = extract_long(&buf[4], 0);
1013 bytes_received = 0L;
1014 fp = fopen(tempfilename, "w");
1016 lprintf(9, "cannot open download file locally: %s\n",
1021 while (bytes_received < download_len) {
1022 sprintf(buf, "READ %ld|%ld",
1024 ((download_len - bytes_received > IGNET_PACKET_SIZE)
1025 ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1026 if (sock_puts(sock, buf) < 0) {
1028 unlink(tempfilename);
1031 if (sock_gets(sock, buf) < 0) {
1033 unlink(tempfilename);
1036 if (buf[0] == '6') {
1037 plen = extract_long(&buf[4], 0);
1038 if (sock_read(sock, pbuf, plen) < 0) {
1040 unlink(tempfilename);
1043 fwrite((char *) pbuf, plen, 1, fp);
1044 bytes_received = bytes_received + plen;
1049 if (sock_puts(sock, "CLOS") < 0) {
1050 unlink(tempfilename);
1053 if (sock_gets(sock, buf) < 0) {
1054 unlink(tempfilename);
1057 lprintf(9, "%s\n", buf);
1058 sprintf(buf, "mv %s ./network/spoolin/%s.%ld",
1059 tempfilename, remote_nodename, (long) getpid());
1066 * transmit network spool to the remote system
1068 void transmit_spool(int sock, char *remote_nodename)
1073 long bytes_to_write, thisblock;
1077 if (sock_puts(sock, "NUOP") < 0) return;
1078 if (sock_gets(sock, buf) < 0) return;
1079 lprintf(9, "<%s\n", buf);
1080 if (buf[0] != '2') {
1084 sprintf(sfname, "./network/spoolout/%s", remote_nodename);
1085 fd = open(sfname, O_RDONLY);
1087 if (errno == ENOENT) {
1088 lprintf(9, "Nothing to send.\n");
1090 lprintf(5, "cannot open upload file locally: %s\n",
1095 while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1096 bytes_to_write = plen;
1097 while (bytes_to_write > 0L) {
1098 sprintf(buf, "WRIT %ld", bytes_to_write);
1099 if (sock_puts(sock, buf) < 0) {
1103 if (sock_gets(sock, buf) < 0) {
1107 thisblock = atol(&buf[4]);
1108 if (buf[0] == '7') {
1109 if (sock_write(sock, pbuf,
1110 (int) thisblock) < 0) {
1114 bytes_to_write = bytes_to_write - thisblock;
1123 if (sock_puts(sock, "UCLS 1") < 0) return;
1124 if (sock_gets(sock, buf) < 0) return;
1125 lprintf(9, "<%s\n", buf);
1126 if (buf[0] == '2') {
1134 * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1136 void network_poll_node(char *node, char *secret, char *host, char *port) {
1140 if (network_talking_to(node, NTT_CHECK)) return;
1141 network_talking_to(node, NTT_ADD);
1142 lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
1144 sock = sock_connect(host, port, "tcp");
1146 lprintf(7, "Could not connect: %s\n", strerror(errno));
1147 network_talking_to(node, NTT_REMOVE);
1151 lprintf(9, "Connected!\n");
1153 /* Read the server greeting */
1154 if (sock_gets(sock, buf) < 0) goto bail;
1155 lprintf(9, ">%s\n", buf);
1157 /* Identify ourselves */
1158 sprintf(buf, "NETP %s|%s", config.c_nodename, secret);
1159 lprintf(9, "<%s\n", buf);
1160 if (sock_puts(sock, buf) <0) goto bail;
1161 if (sock_gets(sock, buf) < 0) goto bail;
1162 lprintf(9, ">%s\n", buf);
1163 if (buf[0] != '2') goto bail;
1165 /* At this point we are authenticated. */
1166 receive_spool(sock, node);
1167 transmit_spool(sock, node);
1169 sock_puts(sock, "QUIT");
1170 bail: sock_close(sock);
1171 network_talking_to(node, NTT_REMOVE);
1177 * Poll other Citadel nodes and transfer inbound/outbound network data.
1179 void network_poll_other_citadel_nodes(void) {
1180 char *ignetcfg = NULL;
1188 ignetcfg = CtdlGetSysConfig(IGNETCFG);
1189 if (ignetcfg == NULL) return; /* no nodes defined */
1191 /* Use the string tokenizer to grab one line at a time */
1192 for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
1193 extract_token(linebuf, ignetcfg, i, '\n');
1194 extract(node, linebuf, 0);
1195 extract(secret, linebuf, 1);
1196 extract(host, linebuf, 2);
1197 extract(port, linebuf, 3);
1198 if ( (strlen(node) > 0) && (strlen(secret) > 0)
1199 && (strlen(host) > 0) && strlen(port) > 0) {
1200 network_poll_node(node, secret, host, port);
1214 * network_do_queue()
1216 * Run through the rooms doing various types of network stuff.
1218 void network_do_queue(void) {
1219 static int doing_queue = 0;
1220 static time_t last_run = 0L;
1221 struct RoomProcList *ptr;
1224 * Run no more frequently than once every n seconds
1226 if ( (time(NULL) - last_run) < config.c_net_freq ) return;
1229 * This is a simple concurrency check to make sure only one queue run
1230 * is done at a time. We could do this with a mutex, but since we
1231 * don't really require extremely fine granularity here, we'll do it
1232 * with a static variable instead.
1234 if (doing_queue) return;
1236 last_run = time(NULL);
1239 * Poll other Citadel nodes.
1241 network_poll_other_citadel_nodes();
1244 * Load the network map into memory.
1249 * Go ahead and run the queue
1251 lprintf(7, "network: loading outbound queue\n");
1252 ForEachRoom(network_queue_room, NULL);
1254 lprintf(7, "network: running outbound queue\n");
1255 while (rplist != NULL) {
1256 network_spoolout_room(rplist->name);
1258 rplist = rplist->next;
1262 lprintf(7, "network: processing inbound queue\n");
1263 network_do_spoolin();
1265 /* Save the network map back to disk */
1266 write_network_map();
1268 lprintf(7, "network: queue run completed\n");
1275 * cmd_netp() - authenticate to the server as another Citadel node polling
1276 * for network traffic
1278 void cmd_netp(char *cmdbuf)
1286 extract(node, cmdbuf, 0);
1287 extract(pass, cmdbuf, 1);
1289 if (is_valid_node(nexthop, secret, node) != 0) {
1290 cprintf("%d authentication failed\n", ERROR);
1294 if (strcasecmp(pass, secret)) {
1295 cprintf("%d authentication failed\n", ERROR);
1299 if (network_talking_to(node, NTT_CHECK)) {
1300 cprintf("%d Already talking to %s right now\n", ERROR, node);
1304 safestrncpy(CC->net_node, node, sizeof CC->net_node);
1305 network_talking_to(node, NTT_ADD);
1306 cprintf("%d authenticated as network node '%s'\n", OK,
1313 * Module entry point
1315 char *Dynamic_Module_Init(void)
1317 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
1318 CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
1319 CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
1320 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);