]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_network.c
* Finished the concurrency check for network polling. (Now works both for
[citadel.git] / citadel / serv_network.c
index 81dd1aed916875072535e48193a0f9f98693aab7..b0dc2e75a71c14e7114b394ce31c2db33b5c123c 100644 (file)
  */
 
 /*
- * FIXME do something about concurrency issues:
- * 1. Don't allow the two nodes to poll each other at the same time
- * 2. Don't allow polls during network processing
- * 3. Kill Bill Gates using either a chainsaw or a wood chipper
+ * FIXME
+ * Don't allow polls during network processing
  */
 
 #include "sysdep.h"
@@ -56,6 +54,7 @@
 #include "internet_addressing.h"
 #include "serv_network.h"
 #include "clientsocket.h"
+#include "file_ops.h"
 
 
 /*
@@ -86,68 +85,6 @@ struct NetMap *the_netmap = NULL;
 
 
 
-/*
- * network_talking_to()  --  concurrency checker
- */
-int network_talking_to(char *nodename, int oper) {
-
-       static char *nttlist = NULL;
-       char *ptr = NULL;
-       int i;
-       char buf[SIZ];
-       int retval = 0;
-
-       begin_critical_section(S_NTTLIST);
-
-       switch(oper) {
-
-               case NTT_ADD:
-                       if (nttlist == NULL) nttlist = stdroop("");
-                       if (nttlist == NULL) return;
-                       nttlist = (char *)reallok(nttlist,
-                               (strlen(nttlist) + strlen(nodename) + 3) );
-                       strcat(nttlist, "|");
-                       strcat(nttlist, nodename);
-                       break;
-
-               case NTT_REMOVE:
-                       if (nttlist == NULL) break;
-                       if (strlen(nttlist) == 0) break;
-                       ptr = mallok(strlen(nttlist));
-                       if (ptr == NULL) break;
-                       strcpy(ptr, "");
-                       for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
-                               extract(buf, nttlist, i);
-                               if ( (strlen(buf) > 0)
-                                    && (strcasecmp(buf, nodename)) ) {
-                                               strcat(ptr, buf);
-                                               strcat(ptr, "|");
-                               }
-                       }
-                       phree(nttlist);
-                       nttlist = ptr;
-                       break;
-
-               case NTT_CHECK:
-                       if (nttlist == NULL) break;
-                       if (strlen(nttlist) == 0) break;
-                       for (i = 0; i < num_tokens(nttlist, '|'); ++i) {
-                               extract(buf, nttlist, i);
-                               if (!strcasecmp(buf, nodename)) ++retval;
-                       break;
-       }
-
-       lprintf(9, "nttlist=<%s>\n", nttlist);
-       end_critical_section(S_NTTLIST);
-       return(retval);
-}
-
-
-
-
-
-
-
 /* 
  * Read the network map from its configuration file into memory.
  */
@@ -1151,11 +1088,14 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
        int sock;
        char buf[SIZ];
 
+       if (network_talking_to(node, NTT_CHECK)) return;
+       network_talking_to(node, NTT_ADD);
        lprintf(5, "Polling node <%s> at %s:%s\n", node, host, port);
 
        sock = sock_connect(host, port, "tcp");
        if (sock < 0) {
                lprintf(7, "Could not connect: %s\n", strerror(errno));
+               network_talking_to(node, NTT_REMOVE);
                return;
        }
        
@@ -1179,6 +1119,7 @@ void network_poll_node(char *node, char *secret, char *host, char *port) {
 
        sock_puts(sock, "QUIT");
 bail:  sock_close(sock);
+       network_talking_to(node, NTT_REMOVE);
 }
 
 
@@ -1305,7 +1246,13 @@ void cmd_netp(char *cmdbuf)
                return;
        }
 
+       if (network_talking_to(node, NTT_CHECK)) {
+               cprintf("%d Already talking to %s right now\n", ERROR);
+               return;
+       }
+
        safestrncpy(CC->net_node, node, sizeof CC->net_node);
+       network_talking_to(node, NTT_ADD);
        cprintf("%d authenticated as network node '%s'\n", OK,
                CC->net_node);
 }