more checks
authorChristian Grothoff <christian@grothoff.org>
Sun, 14 Nov 2010 13:26:50 +0000 (13:26 +0000)
committerChristian Grothoff <christian@grothoff.org>
Sun, 14 Nov 2010 13:26:50 +0000 (13:26 +0000)
src/core/core_api.c
src/core/test_core_api.c

index 1d7a32327c94e1489fa4d1900f9647d9a02811d6..2211028fb65e3cd12a13e8f80b9f767251ed4890 100644 (file)
@@ -236,6 +236,11 @@ struct GNUNET_CORE_Handle
    */
   struct GNUNET_CONTAINER_MultiHashMap *peers;
 
+  /**
+   * Identity of this peer.
+   */
+  struct GNUNET_PeerIdentity me;
+
   /**
    * ID of reconnect task (if any).
    */
@@ -781,7 +786,6 @@ main_notify_handler (void *cls,
   const struct GNUNET_CORE_MessageHandler *mh;
   GNUNET_CORE_StartupCallback init;
   GNUNET_CORE_PeerConfigurationInfoCallback pcic;
-  struct GNUNET_PeerIdentity my_identity;
   struct PeerRecord *pr;
   struct GNUNET_CORE_TransmitHandle *th;
   unsigned int hpos;
@@ -832,8 +836,8 @@ main_notify_handler (void *cls,
          GNUNET_CRYPTO_hash (&m->publicKey,
                              sizeof (struct
                                      GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                             &my_identity.hashPubKey);
-         init (h->cls, h, &my_identity, &m->publicKey);
+                             &h->me.hashPubKey);
+         init (h->cls, h, &h->me, &m->publicKey);
        }
       break;
     case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
@@ -849,6 +853,14 @@ main_notify_handler (void *cls,
                  "Received notification about connection from `%s'.\n",
                  GNUNET_i2s (&cnm->peer));
 #endif
+      if (0 == memcmp (&h->me,
+                      &cnm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* disconnect from self!? */
+         GNUNET_break (0);
+         return;
+       }
       pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
                                              &cnm->peer.hashPubKey);
       if (pr != NULL)
@@ -878,6 +890,14 @@ main_notify_handler (void *cls,
          return;
         }
       dnm = (const struct DisconnectNotifyMessage *) msg;
+      if (0 == memcmp (&h->me,
+                      &dnm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* connection to self!? */
+         GNUNET_break (0);
+         return;
+       }
 #if DEBUG_CORE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Received notification about disconnect from `%s'.\n",
@@ -911,6 +931,14 @@ main_notify_handler (void *cls,
          return;
         }
       psnm = (const struct PeerStatusNotifyMessage *) msg;
+      if (0 == memcmp (&h->me,
+                      &psnm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
 #if DEBUG_CORE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Received notification about status change by `%s'.\n",
@@ -941,6 +969,14 @@ main_notify_handler (void *cls,
          return;
         }
       ntm = (const struct NotifyTrafficMessage *) msg;
+      if (0 == memcmp (&h->me,
+                      &ntm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
       em = (const struct GNUNET_MessageHeader *) &ntm[1];
 #if DEBUG_CORE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -998,6 +1034,14 @@ main_notify_handler (void *cls,
          return;
         }
       ntm = (const struct NotifyTrafficMessage *) msg;
+      if (0 == memcmp (&h->me,
+                      &ntm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
       em = (const struct GNUNET_MessageHeader *) &ntm[1];
       pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
                                              &ntm->peer.hashPubKey);
@@ -1035,6 +1079,14 @@ main_notify_handler (void *cls,
          return;
         }
       smr = (const struct SendMessageReady *) msg;
+      if (0 == memcmp (&h->me,
+                      &smr->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
       pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
                                              &smr->peer.hashPubKey);
       if (pr == NULL)
@@ -1077,6 +1129,14 @@ main_notify_handler (void *cls,
          return;
        }
       cim = (const struct ConfigurationInfoMessage*) msg;
+      if (0 == memcmp (&h->me,
+                      &cim->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
 #if DEBUG_CORE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Received notification about configuration update for `%s'.\n",
index b41d304506736069f6a1852b8476b2c5d6d8c780..004486525049130f995ff37ce9a5aae6bbe67b74 100644 (file)
@@ -57,6 +57,7 @@ static struct PeerContext p1;
 
 static struct PeerContext p2;
 
+static GNUNET_SCHEDULER_TaskIdentifier err_task;
 
 static int ok;
 
@@ -95,6 +96,27 @@ terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+static size_t
+transmit_ready (void *cls, size_t size, void *buf)
+{
+  struct PeerContext *p = cls;
+  struct GNUNET_MessageHeader *m;
+
+  GNUNET_assert (ok == 4);
+  OKPP;
+  GNUNET_assert (p == &p1);
+  GNUNET_assert (buf != NULL);
+  m = (struct GNUNET_MessageHeader *) buf;
+  m->type = htons (MTYPE);
+  m->size = htons (sizeof (struct GNUNET_MessageHeader));
+  err_task = 
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120), 
+                                 &terminate_task_error, NULL);
+
+  return sizeof (struct GNUNET_MessageHeader);
+}
+
+
 static void
 connect_notify (void *cls,
                 const struct GNUNET_PeerIdentity *peer,
@@ -106,6 +128,20 @@ connect_notify (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Encrypted connection established to peer `%4s'\n",
               GNUNET_i2s (peer));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Asking core (1) for transmission to peer `%4s'\n",
+             GNUNET_i2s (&p2.id));
+  if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
+                                                0,
+                                                GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
+                                                &p2.id,
+                                                sizeof (struct GNUNET_MessageHeader),
+                                                &transmit_ready, &p1))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
+                 GNUNET_i2s (&p2.id));
+    }  
 }
 
 
@@ -145,8 +181,6 @@ outbound_notify (void *cls,
 }
 
 
-static GNUNET_SCHEDULER_TaskIdentifier err_task;
-
 
 static int
 process_mtype (void *cls,
@@ -170,27 +204,6 @@ static struct GNUNET_CORE_MessageHandler handlers[] = {
 };
 
 
-static size_t
-transmit_ready (void *cls, size_t size, void *buf)
-{
-  struct PeerContext *p = cls;
-  struct GNUNET_MessageHeader *m;
-
-  GNUNET_assert (ok == 4);
-  OKPP;
-  GNUNET_assert (p == &p1);
-  GNUNET_assert (buf != NULL);
-  m = (struct GNUNET_MessageHeader *) buf;
-  m->type = htons (MTYPE);
-  m->size = htons (sizeof (struct GNUNET_MessageHeader));
-  err_task = 
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120), &terminate_task_error, NULL);
-
-  return sizeof (struct GNUNET_MessageHeader);
-}
-
-
-
 static void
 init_notify (void *cls,
              struct GNUNET_CORE_Handle *server,
@@ -226,21 +239,12 @@ init_notify (void *cls,
       OKPP;
       GNUNET_assert (cls == &p2);
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Asking core (1) for transmission to peer `%4s'\n",
+                  "Asking core (1) to connect to peer `%4s'\n",
                   GNUNET_i2s (&p2.id));
-
-      if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
-                                         0,
-                                         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
-                                         &p2.id,
-                                         sizeof (struct GNUNET_MessageHeader),
-                                         &transmit_ready, &p1))
-        {
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
-                      GNUNET_i2s (&p2.id));
-        }
-
+      GNUNET_CORE_peer_request_connect (p1.ch,
+                                       GNUNET_TIME_UNIT_SECONDS,
+                                       &p2.id,
+                                       NULL, NULL);
     }
 }