gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r6442 - GNUnet/src/applications/chat


From: gnunet
Subject: [GNUnet-SVN] r6442 - GNUnet/src/applications/chat
Date: Sun, 24 Feb 2008 15:07:32 -0700 (MST)

Author: nevans
Date: 2008-02-24 15:07:32 -0700 (Sun, 24 Feb 2008)
New Revision: 6442

Modified:
   GNUnet/src/applications/chat/chat.c
   GNUnet/src/applications/chat/clientapi.c
Log:


Modified: GNUnet/src/applications/chat/chat.c
===================================================================
--- GNUnet/src/applications/chat/chat.c 2008-02-24 20:18:05 UTC (rev 6441)
+++ GNUnet/src/applications/chat/chat.c 2008-02-24 22:07:32 UTC (rev 6442)
@@ -35,14 +35,10 @@
 
 static GNUNET_CoreAPIForPlugins *coreAPI;
 
-#define MAX_LAST_MESSAGES 12
-
 static struct GNUNET_Mutex *chatMutex;
 
 static struct GNUNET_GE_Context *ectx;
 
-static struct GNUNET_GC_Configuration *cfg;
-
 struct GNUNET_CS_chat_client
 {
   struct GNUNET_ClientHandle *client;
@@ -52,7 +48,7 @@
 
 };
 
-static struct GNUNET_CS_chat_client *client_list_head;
+static struct GNUNET_CS_chat_client * client_list_head;
 
 static int
 csHandleChatMSG (struct GNUNET_ClientHandle *client,
@@ -77,7 +73,7 @@
   cmsg = (CS_chat_MESSAGE *) message;
   if (ntohs (cmsg->header.size) < sizeof (CS_chat_MESSAGE))
     {
-      GNUNET_GE_BREAK (NULL, 0);
+      GNUNET_GE_BREAK(NULL, 0);
       return GNUNET_SYSERR;     /* invalid message */
     }
 
@@ -86,12 +82,17 @@
   nick_len = ntohl (cmsg->nick_len);
   msg_len = ntohl (cmsg->msg_len);
   room_name_len = ntohl (cmsg->room_name_len);
+  
+  if (header_size < (nick_len + msg_len + room_name_len))
+    {
+      GNUNET_GE_BREAK(NULL, 0);
+      return GNUNET_SYSERR;     /* invalid message */
+    }
 
   nick = GNUNET_malloc (nick_len + 1);
   message_content = GNUNET_malloc (msg_len + 1);
   room_name = GNUNET_malloc (room_name_len + 1);
 
-  /* BUFFER OVERFLOWS! */
   memcpy (nick, &cmsg->nick[0], nick_len);
   memcpy (message_content, &cmsg->nick[nick_len], msg_len);
   memcpy (room_name, &cmsg->nick[nick_len + msg_len], room_name_len);
@@ -141,7 +142,7 @@
 
 static int
 csHandleChatJoinRequest (struct GNUNET_ClientHandle *client,
-                         const GNUNET_MessageHeader * message)
+                     const GNUNET_MessageHeader * message)
 {
   const CS_chat_JOIN_MESSAGE *cmsg;
   P2P_chat_MESSAGE *pmsg;
@@ -164,20 +165,26 @@
 
   if (ntohs (cmsg->header.size) < sizeof (CS_chat_JOIN_MESSAGE))
     {
-      GNUNET_GE_BREAK (NULL, 0);
+      GNUNET_GE_BREAK(NULL, 0);
       return GNUNET_SYSERR;     /* invalid message */
     }
+    
 
   header_size = ntohs (cmsg->header.size);
   nick_len = ntohl (cmsg->nick_len);
   pubkey_len = ntohl (cmsg->pubkey_len);
   room_name_len = ntohl (cmsg->room_name_len);
 
+  if (header_size < (nick_len + pubkey_len + room_name_len))
+    {
+      GNUNET_GE_BREAK(NULL, 0);
+      return GNUNET_SYSERR;     /* invalid message */
+    }
+
   nick = GNUNET_malloc (nick_len + 1);
   client_key = GNUNET_malloc (sizeof (GNUNET_RSA_PublicKey));
   room_name = GNUNET_malloc (room_name_len + 1);
 
-  /* BUFFER OVERFLOWS */
   memcpy (nick, &cmsg->nick[0], nick_len);
   memcpy (client_key, &cmsg->nick[nick_len], pubkey_len);
   memcpy (room_name, &cmsg->nick[nick_len + pubkey_len], room_name_len);
@@ -196,20 +203,20 @@
   /*TODO: create client context on the server, very simple as of now */
 #if EXTRA_CHECKS
   tempClient = client_list;
-  while ((tempClient->client != client) && (tempClient != NULL))
+  while ((tempClient != NULL) && (tempClient->client != client))
     tempClient = tempClient->next;
   if (tempClient != NULL)
     {
-      GNUNET_GE_BREAK (NULL, 0);
+      GNUNET_GE_BREAK(NULL, 0);
       GNUNET_free (nick);
       GNUNET_free (client_key);
       GNUNET_free (room_name);
       GNUNET_mutex_unlock (chatMutex);
-      return GNUNET_SYSERR;
+      return GNUNET_SYSERR;      
     }
 #endif
   tempClient = GNUNET_malloc (sizeof (struct GNUNET_CS_chat_client));
-  memset (tempClient, 0, sizeof (struct GNUNET_CS_chat_client));
+  memset(tempClient, 0, sizeof (struct GNUNET_CS_chat_client));
   tempClient->next = client_list_head;
   if (client_list_head != NULL)
     client_list_head->prev = tempClient;
@@ -228,36 +235,6 @@
   fprintf (stderr, "Number of clients currently is... %d\n", tempCount);
 
 
-  /* forward to all other TCP chat clients */
-  /* marker to check if this is a new client */
-  /*
-     j = -1;
-     for (i = 0; i < clientCount; i++)
-     if (clients[i] == client)
-     j = i;
-     else
-     coreAPI->cs_send_to_client (clients[i], message, GNUNET_YES);
-     if (j == -1)
-     {
-     if (clientCount == MAX_CLIENTS)
-     GNUNET_GE_LOG (ectx,
-     GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
-     _("Maximum number of chat clients reached.\n"));
-     else
-     {
-     GNUNET_array_grow (clients, clientCount, clientCount + 1);
-     clients[clientCount] = client;
-     ++clientCount;
-     GNUNET_GE_LOG (ectx,
-     GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
-     _("Now %d of %d chat clients at this node.\n"),
-     clientCount, MAX_CLIENTS);
-     }
-     }
-   */
-
-  /* forward to all other nodes in the network */
-
   GNUNET_free (nick);
   GNUNET_free (client_key);
   GNUNET_free (room_name);
@@ -272,38 +249,32 @@
 chatClientExitHandler (struct GNUNET_ClientHandle *client)
 {
   int tempCount;
+  struct GNUNET_CS_chat_client * tempClient;
+  struct GNUNET_CS_chat_client * pos;
+  struct GNUNET_CS_chat_client * prev;
 
-  struct GNUNET_CS_chat_client *tempClient;
-  struct GNUNET_CS_chat_client *pos;
-  struct GNUNET_CS_chat_client *prev;
-
-  /*client_key = GNUNET_malloc (sizeof (GNUNET_RSA_PublicKey));
-     memcpy (client_key, &cmsg->nick[nick_len], pubkey_len); */
-
   GNUNET_GE_LOG (ectx,
                  GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_DEVELOPER,
                  "Received leave chat room message from client.\n");
 
-
   GNUNET_mutex_lock (chatMutex);
 
-  /*TODO: delete client context on the server */
   pos = client_list_head;
   prev = NULL;
-  while ((pos != NULL) && (pos->client != client))
-    {
+  while ( (pos != NULL) && (pos->client != client) )
+       {
       prev = pos;
       pos = pos->next;
     }
   if (pos != NULL)
-    {
+       {
       if (prev == NULL)
-        client_list_head = pos->next;
+       client_list_head = pos->next;
       else
-        prev->next = pos->next;
+       prev->next = pos->next;
       if (pos->next != NULL)
-        pos->next->prev = pos->prev;
-      GNUNET_free (pos);
+       pos->next->prev = pos->prev;
+      GNUNET_free(pos);
     }
   /*Count the number of current clients, will be removed */
 
@@ -315,10 +286,9 @@
       tempClient = tempClient->next;
     }
   fprintf (stderr, "Number of clients currently is... %d\n", tempCount);
+  /*End of client count code*/
 
-
   GNUNET_mutex_unlock (chatMutex);
-  fprintf (stderr, "End of handleChatLeave\n");
   return;
 }
 
@@ -331,7 +301,7 @@
   GNUNET_GE_ASSERT (ectx,
                     sizeof (P2P_chat_MESSAGE) == sizeof (CS_chat_MESSAGE));
   chatMutex = GNUNET_mutex_create (GNUNET_NO);
-
+  
   coreAPI = capi;
   GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
                  _("`%s' registering handlers %d and %d\n"),

Modified: GNUnet/src/applications/chat/clientapi.c
===================================================================
--- GNUnet/src/applications/chat/clientapi.c    2008-02-24 20:18:05 UTC (rev 
6441)
+++ GNUnet/src/applications/chat/clientapi.c    2008-02-24 22:07:32 UTC (rev 
6442)
@@ -54,73 +54,76 @@
   int disconnected;
 
   ret = GNUNET_OK;
-  disconnected = GNUNET_NO;
+       disconnected = GNUNET_NO;
   while (room->shutdown_flag != GNUNET_YES)
-    {
-      if (disconnected)
-        {
-          GNUNET_thread_sleep (15 * GNUNET_CRON_SECONDS);
-          if (GNUNET_client_connection_ensure_connected (room->sock) ==
-              GNUNET_OK)
-            {
-              /* send join! */
-              disconnected = GNUNET_NO;
-              GNUNET_CHAT_rejoin_room (room);
-              continue;
-            }
-          else
-            break;
-        }
+  {
+       if (disconnected)
+               {
+                       GNUNET_thread_sleep(15 * GNUNET_CRON_SECONDS);
+                       if (GNUNET_client_connection_ensure_connected 
(room->sock) == GNUNET_OK)
+           {
+             /* send join! */
+             disconnected = GNUNET_NO;
+             GNUNET_CHAT_rejoin_room(room);
+             continue;
+           }
+           else
+             break;
+               }
+               
+               fprintf(stderr,"polling\n");
+    reply = NULL;
 
-      fprintf (stderr, "polling\n");
-      reply = NULL;
+    if (GNUNET_OK != GNUNET_client_connection_read (room->sock, &reply))
+               {
+                 disconnected = GNUNET_YES;
+                 continue;
+               }
 
-      if (GNUNET_OK != GNUNET_client_connection_read (room->sock, &reply))
-        {
-          disconnected = GNUNET_YES;
-          continue;
-        }
+    if ((reply->size <
+         ntohs (sizeof (GNUNET_MessageHeader) + sizeof (CS_chat_MESSAGE)))
+        || (reply->type != ntohs (GNUNET_CS_PROTO_CHAT_MSG)))
+               {
+                 GNUNET_GE_BREAK(NULL, 0);
+                 GNUNET_client_connection_close_temporarily(room->sock);
+                 disconnected = GNUNET_YES;
+                 continue;
+               }
 
-      if ((reply->size <
-           ntohs (sizeof (GNUNET_MessageHeader) + sizeof (CS_chat_MESSAGE)))
-          || (reply->type != ntohs (GNUNET_CS_PROTO_CHAT_MSG)))
-        {
-          GNUNET_GE_BREAK (NULL, 0);
-          GNUNET_client_connection_close_temporarily (room->sock);
-          disconnected = GNUNET_YES;
-          continue;
-        }
+    size = ntohs (reply->size);
 
-      size = ntohs (reply->size);
+    received_msg = (CS_chat_MESSAGE *) reply;
 
-      received_msg = (CS_chat_MESSAGE *) reply;
+    nick_len = ntohl (received_msg->nick_len);
+    msg_len = ntohl (received_msg->msg_len);
+    /* NO NEED TO SEND ROOM! */
+    room_name_len = ntohl (received_msg->room_name_len);
 
-      nick_len = ntohl (received_msg->nick_len);
-      msg_len = ntohl (received_msg->msg_len);
-      /* NO NEED TO SEND ROOM! */
-      room_name_len = ntohl (received_msg->room_name_len);
+    if (size < (nick_len + msg_len + room_name_len))
+    {
+      GNUNET_GE_BREAK(NULL, 0);
+      return GNUNET_SYSERR;     /* invalid message */
+    }
 
-      nick = GNUNET_malloc (nick_len + 1);
-      message_content = GNUNET_malloc (msg_len + 1);
-      room_name = GNUNET_malloc (room_name_len + 1);
+    nick = GNUNET_malloc (nick_len + 1);
+    message_content = GNUNET_malloc (msg_len + 1);
+    room_name = GNUNET_malloc (room_name_len + 1);
 
-      /* BUFFER OVERFLOWS! */
-      memcpy (nick, &received_msg->nick[0], nick_len);
-      memcpy (message_content, &received_msg->nick[nick_len], msg_len);
-      memcpy (room_name, &received_msg->nick[nick_len + msg_len],
-              room_name_len);
+    /* BUFFER OVERFLOWS! */
+    memcpy (nick, &received_msg->nick[0], nick_len);
+    memcpy (message_content, &received_msg->nick[nick_len], msg_len);
+    memcpy (room_name, &received_msg->nick[nick_len + msg_len],
+            room_name_len);
 
-      nick[nick_len] = '\0';
-      message_content[msg_len] = '\0';
-      room_name[room_name_len] = '\0';
+    nick[nick_len] = '\0';
+    message_content[msg_len] = '\0';
+    room_name[room_name_len] = '\0';
 
-      if (GNUNET_OK !=
-          room->callback (room->callback_cls, room, nick, message_content,
-                          GNUNET_get_time (), 0))
-        {
-          ret = GNUNET_SYSERR;
-        }
+    if (GNUNET_OK != room->callback (room->callback_cls, room, nick, 
message_content,GNUNET_get_time (), 0))
+    {
+      ret = GNUNET_SYSERR;
     }
+  }
   return NULL;
 }
 
@@ -207,7 +210,7 @@
     {
       /* ALREADY LOGGED */
       fprintf (stderr, _("Error writing to socket.\n"));
-      GNUNET_free (join_msg);
+      GNUNET_free(join_msg);
       return NULL;
     }
 
@@ -254,10 +257,8 @@
   csHdr.size = htons (sizeof (CS_chat_JOIN_MESSAGE));
   csHdr.type = htons (GNUNET_CS_PROTO_CHAT_JOIN_MSG);
 
-  GNUNET_hash (chat_room->my_public_key, sizeof (GNUNET_RSA_PublicKey),
-               &hash_of_me);
-  GNUNET_hash (chat_room->room_name, strlen (chat_room->room_name),
-               &hash_of_room_name);
+  GNUNET_hash (chat_room->my_public_key, sizeof (GNUNET_RSA_PublicKey), 
&hash_of_me);
+  GNUNET_hash (chat_room->room_name, strlen (chat_room->room_name), 
&hash_of_room_name);
 
   size_of_join =
     sizeof (CS_chat_JOIN_MESSAGE) + strlen (chat_room->nickname) +
@@ -269,12 +270,10 @@
   join_msg->room_name_len = htonl (strlen (chat_room->room_name));
 
 
-  memcpy (&join_msg->nick[0], chat_room->nickname,
-          strlen (chat_room->nickname));
-  memcpy (&join_msg->nick[strlen (chat_room->nickname)],
-          chat_room->my_public_key, sizeof (GNUNET_RSA_PublicKey));
-  memcpy (&join_msg->
-          nick[strlen (chat_room->nickname) + sizeof (GNUNET_RSA_PublicKey)],
+  memcpy (&join_msg->nick[0], chat_room->nickname, strlen 
(chat_room->nickname));
+  memcpy (&join_msg->nick[strlen (chat_room->nickname)], 
chat_room->my_public_key,
+          sizeof (GNUNET_RSA_PublicKey));
+  memcpy (&join_msg->nick[strlen (chat_room->nickname) + sizeof 
(GNUNET_RSA_PublicKey)],
           chat_room->room_name, strlen (chat_room->room_name));
 
   join_msg->header = csHdr;
@@ -283,11 +282,11 @@
   if (GNUNET_SYSERR ==
       GNUNET_client_connection_write (chat_room->sock, &join_msg->header))
     {
-      GNUNET_free (join_msg);
+      GNUNET_free(join_msg);
       return GNUNET_SYSERR;
     }
 
-  GNUNET_free (join_msg);
+  GNUNET_free (join_msg);  
   return GNUNET_OK;
 }
 
@@ -297,11 +296,11 @@
 void
 GNUNET_CHAT_leave_room (struct GNUNET_CHAT_Room *chat_room)
 {
-  void *unused;
+  void * unused;
   chat_room->shutdown_flag = GNUNET_YES;
   GNUNET_client_connection_close_forever (chat_room->sock);
-  GNUNET_thread_stop_sleep (chat_room->listen_thread);
-  GNUNET_thread_join (chat_room->listen_thread, &unused);
+  GNUNET_thread_stop_sleep(chat_room->listen_thread);
+  GNUNET_thread_join(chat_room->listen_thread, &unused);
   GNUNET_free (chat_room->nickname);
   GNUNET_free (chat_room->memberInfo);
   GNUNET_client_connection_destroy (chat_room->sock);





reply via email to

[Prev in Thread] Current Thread [Next in Thread]