gnunet-svn
[Top][All Lists]
Advanced

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

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


From: gnunet
Subject: [GNUnet-SVN] r6927 - GNUnet/src/applications/chat
Date: Thu, 29 May 2008 22:36:38 -0600 (MDT)

Author: grothoff
Date: 2008-05-29 22:36:38 -0600 (Thu, 29 May 2008)
New Revision: 6927

Modified:
   GNUnet/src/applications/chat/chat.h
   GNUnet/src/applications/chat/clientapi.c
Log:
fixing client API

Modified: GNUnet/src/applications/chat/chat.h
===================================================================
--- GNUnet/src/applications/chat/chat.h 2008-05-30 03:59:29 UTC (rev 6926)
+++ GNUnet/src/applications/chat/chat.h 2008-05-30 04:36:38 UTC (rev 6927)
@@ -120,6 +120,8 @@
 
 /**
  * Message send from client to daemon to join a chat room.
+ * This struct is followed by the room name and then
+ * the serialized ECRS meta data describing the new member.
  */
 typedef struct
 {
@@ -139,12 +141,11 @@
    */
   GNUNET_RSA_PrivateKeyEncoded private_key;
 
-  unsigned short nick_len;
+  /**
+   * Length of the room name.
+   */
+  unsigned short room_name_len;
 
-  /* followed by nick_len bytes of
-     the nickname; then followed
-     by the name of the chat room */
-
 } CS_chat_MESSAGE_JoinRequest;
 
 /**
@@ -190,7 +191,7 @@
   /**
    * Who is leaving?
    */
-  GNUNET_HashCode user;
+  GNUNET_RSA_PublicKey user;
 
 } CS_chat_MESSAGE_LeaveNotification;
 

Modified: GNUnet/src/applications/chat/clientapi.c
===================================================================
--- GNUnet/src/applications/chat/clientapi.c    2008-05-30 03:59:29 UTC (rev 
6926)
+++ GNUnet/src/applications/chat/clientapi.c    2008-05-30 04:36:38 UTC (rev 
6927)
@@ -53,7 +53,7 @@
 
   char *room_name;
 
-  struct GNUNET_RSA_PrivateKeyEncoded *my_private_key;
+  GNUNET_RSA_PrivateKeyEncoded *my_private_key;
 
   GNUNET_CHAT_MessageCallback message_callback;
 
@@ -69,32 +69,46 @@
 
   int shutdown_flag;
 
-
+  unsigned int sequence_number;
+  
 };
 
 static int
 GNUNET_CHAT_rejoin_room (struct GNUNET_CHAT_Room *chat_room)
 {
-  CS_chat_JOIN_MESSAGE *join_msg;
+  CS_chat_MESSAGE_JoinRequest *join_msg;
   unsigned int size_of_join;
-  unsigned int nick_len;
-  char * nick;
+  unsigned int room_len;
+  unsigned int meta_len;
+  char * room;
 
-  nick_len = strlen(chat_room->nickname);
+  meta_len = GNUNET_ECRS_meta_data_get_serialized_size(chat_room->member_info,
+                                                      GNUNET_YES);  
+  room_len = strlen(chat_room->room_name);
   size_of_join =
-    sizeof (CS_chat_JOIN_MESSAGE) + nick_len +
-    strlen (chat_room->room_name);
+    sizeof (CS_chat_MESSAGE_JoinRequest) + meta_len +
+    room_len;
   if (size_of_join >= GNUNET_MAX_BUFFER_SIZE - 8)
     return GNUNET_SYSERR;
   join_msg = GNUNET_malloc (size_of_join);
   join_msg->header.size = htons (size_of_join);
-  join_msg->header.type = htons (GNUNET_CS_PROTO_CHAT_JOIN_MSG);
-  join_msg->nick_len = htons (nick_len);
-  nick = (char *) &join_msg[1];
-  memcpy (nick, chat_room->nickname, nick_len);
-  memcpy (&nick[nick_len],
-          chat_room->room_name, strlen (chat_room->room_name));
+  join_msg->header.type = htons (GNUNET_CS_PROTO_CHAT_JOIN_REQUEST);
+  join_msg->room_name_len = htons(room_len);
+  room = (char*)&join_msg[1];
+  memcpy (room,
+         chat_room->room_name,
+         room_len);
   if (GNUNET_SYSERR ==
+      GNUNET_ECRS_meta_data_serialize(chat_room->ectx,
+                                     chat_room->member_info,
+                                     &room[room_len],
+                                     meta_len,
+                                     GNUNET_YES))
+    {
+      GNUNET_free(join_msg);
+      return GNUNET_SYSERR;
+    }  
+  if (GNUNET_SYSERR ==
       GNUNET_client_connection_write (chat_room->sock, &join_msg->header))
     {
       GNUNET_free (join_msg);
@@ -115,22 +129,37 @@
 {
   struct GNUNET_CHAT_Room *room = rcls;
   GNUNET_MessageHeader *reply;
-  CS_chat_MESSAGE *received_msg;
-  CS_chat_ROOM_MEMBER_MESSAGE *received_room_member_msg;
+  CS_chat_MESSAGE_ConfirmationReceipt * receipt;
+  CS_chat_MESSAGE_LeaveNotification * leave_msg;
+  CS_chat_MESSAGE_JoinNotification * join_msg;
+  CS_chat_MESSAGE_ReceiveNotification *received_msg;
+  struct GNUNET_ECRS_MetaData * meta;
   unsigned int size;
-  unsigned int nick_len;
+  unsigned int meta_len;
   unsigned int msg_len;
-  unsigned int room_name_len;
-  char *nick;
   char *message_content;
   int disconnected;
+  int malformed;
   int ret;
 
   disconnected = GNUNET_NO;
+  malformed = GNUNET_NO;
   ret = GNUNET_OK;
+  reply = NULL;
   while ((ret == GNUNET_OK) && (room->shutdown_flag != GNUNET_YES))
     {
-
+      if (malformed) 
+       {
+          GNUNET_GE_BREAK (NULL, 0);
+          GNUNET_client_connection_close_temporarily (room->sock);
+          disconnected = GNUNET_YES;
+         malformed = GNUNET_NO;
+       }
+      if (reply != NULL)
+       {
+         GNUNET_free(reply);
+         reply = NULL;
+       }
       if (disconnected)
         {
           GNUNET_thread_sleep (15 * GNUNET_CRON_SECONDS);
@@ -145,105 +174,92 @@
           else
             break;
         }
-
       reply = NULL;
       if (GNUNET_OK != GNUNET_client_connection_read (room->sock, &reply))
         {
           disconnected = GNUNET_YES;
           continue;
         }
-
-      if (((reply->size < ntohs (sizeof (CS_chat_MESSAGE)))
-           || (reply->type != ntohs (GNUNET_CS_PROTO_CHAT_MSG)))
-          && ((reply->size < ntohs (sizeof (CS_chat_ROOM_MEMBER_MESSAGE)))
-              || (reply->type !=
-                  ntohs (GNUNET_CS_PROTO_CHAT_ROOM_MEMBER_MESSAGE))))
-        {
-          GNUNET_GE_BREAK (NULL, 0);
-          GNUNET_client_connection_close_temporarily (room->sock);
-          disconnected = GNUNET_YES;
-          continue;
-        }
+      size = ntohs (reply->size);
       switch (ntohs (reply->type))
         {
-        case GNUNET_CS_PROTO_CHAT_ROOM_MEMBER_MESSAGE:
-        case GNUNET_CS_PROTO_CHAT_ROOM_MEMBER_LEAVE_MESSAGE:
-          {
-            size = ntohs (reply->size);
-            received_room_member_msg = (CS_chat_ROOM_MEMBER_MESSAGE *) reply;
-            nick_len = ntohs (received_room_member_msg->nick_len);
-
-            if (size - sizeof (GNUNET_MessageHeader) < nick_len)
-              {
-                GNUNET_GE_BREAK (NULL, 0);
-                GNUNET_client_connection_close_temporarily (room->sock);
-                disconnected = GNUNET_YES;
-                continue;
-              }
-            nick = GNUNET_malloc (nick_len + 1);
-            memcpy (nick, &received_room_member_msg->nick[0], nick_len);
-            nick[nick_len] = '\0';
-
-            if (GNUNET_OK !=
-                room->member_list_callback (room->member_list_callback_cls,
-                                            nick,
-                                            ntohs (reply->type) ==
-                                            
GNUNET_CS_PROTO_CHAT_ROOM_MEMBER_MESSAGE,
-                                            GNUNET_get_time ()))
-              {
-                GNUNET_GE_BREAK (NULL, 0);
-                GNUNET_client_connection_close_temporarily (room->sock);
-                disconnected = GNUNET_YES;
-                continue;
-              }
-          }
-          GNUNET_free (nick);
+       case GNUNET_CS_PROTO_CHAT_JOIN_NOTIFICATION:
+         if (size < sizeof(CS_chat_MESSAGE_JoinNotification))
+           {
+             malformed = GNUNET_YES;
+             continue;
+           }
+         join_msg = (CS_chat_MESSAGE_JoinNotification *) reply;
+         meta_len = size - sizeof(CS_chat_MESSAGE_JoinNotification);
+         meta = GNUNET_ECRS_meta_data_deserialize(room->ectx,
+                                                  (const char*)&join_msg[1],
+                                                  meta_len);
+         if (meta == NULL)
+           {
+             malformed = GNUNET_YES;
+             continue;
+           }
+         room->member_list_callback (room->member_list_callback_cls,
+                                     meta,
+                                     &join_msg->public_key);
+         break;
+       case GNUNET_CS_PROTO_CHAT_LEAVE_NOTIFICATION:
+         if (size < sizeof(CS_chat_MESSAGE_LeaveNotification))
+           {
+             malformed = GNUNET_YES;
+             continue;
+           }
+         leave_msg = (CS_chat_MESSAGE_LeaveNotification *) reply;
+         room->member_list_callback (room->member_list_callback_cls,
+                                     NULL,
+                                     &leave_msg->user);
+         break;
+       case GNUNET_CS_PROTO_CHAT_MESSAGE_NOTIFICATION:
+         if (size < sizeof(CS_chat_MESSAGE_ReceiveNotification))
+           {
+             malformed = GNUNET_YES;
+             continue;
+           }
+         received_msg = (CS_chat_MESSAGE_ReceiveNotification *) reply;
+         msg_len = size - sizeof(CS_chat_MESSAGE_ReceiveNotification);
+         message_content = GNUNET_malloc (msg_len + 1);
+         memcpy (message_content, 
+                 &received_msg[1],
+                 msg_len);
+         message_content[msg_len] = '\0';
+         room->message_callback (room->message_callback_cls, 
+                                 room, 
+                                 &received_msg->sender,
+                                 message_content, 
+                                 ntohl(received_msg->msg_options));
+         GNUNET_free (message_content);
           break;
-        case GNUNET_CS_PROTO_CHAT_MSG:
-          {
-            size = ntohs (reply->size);
-            received_msg = (CS_chat_MESSAGE *) reply;
-            nick_len = ntohs (received_msg->nick_len);
-            msg_len = ntohs (received_msg->msg_len);
-            /* NO NEED TO SEND ROOM! */
-            room_name_len =
-              size - nick_len - msg_len - sizeof (CS_chat_MESSAGE);
-            if (size - sizeof (GNUNET_MessageHeader) <
-                (nick_len + msg_len + room_name_len))
-              {
-                GNUNET_GE_BREAK (NULL, 0);
-                GNUNET_client_connection_close_temporarily (room->sock);
-                disconnected = GNUNET_YES;
-                continue;
-              }
-            nick = GNUNET_malloc (nick_len + 1);
-            memcpy (nick, &received_msg->nick[0], nick_len);
-            nick[nick_len] = '\0';
-            message_content = GNUNET_malloc (msg_len + 1);
-            memcpy (message_content, &received_msg->nick[nick_len], msg_len);
-            message_content[msg_len] = '\0';
-            if (GNUNET_OK !=
-                room->callback (room->callback_cls, room, nick,
-                                message_content, GNUNET_get_time (), 0))
-              {
-                GNUNET_GE_BREAK (NULL, 0);
-                GNUNET_client_connection_close_temporarily (room->sock);
-                disconnected = GNUNET_YES;
-                continue;
-              }
-            GNUNET_free (message_content);
-          }
-          GNUNET_free (nick);
-          break;
+       case GNUNET_CS_PROTO_CHAT_CONFIRMATION_RECEIPT:   
+         if (size < sizeof(CS_chat_MESSAGE_ConfirmationReceipt))
+           {
+             malformed = GNUNET_YES;
+             continue;
+           }
+         receipt = (CS_chat_MESSAGE_ConfirmationReceipt *) reply;
+         if (room->confirmation_callback != NULL)
+           room->confirmation_callback (room->confirmation_cls, 
+                                        room, 
+                                        ntohl(receipt->sequence_number),
+                                        GNUNET_ntohll(receipt->timestamp),
+                                        &receipt->target,
+                                        &receipt->content,
+                                        &receipt->signature);
+         break;
         default:
           GNUNET_GE_BREAK (NULL, 0);
           break;
         }
     }
+  GNUNET_free_non_null(reply);
   return NULL;
 }
 
-
+#if 0
 /**
  * List all of the (publically visible) chat rooms.
  * @return number of rooms on success, GNUNET_SYSERR if iterator aborted
@@ -255,13 +271,13 @@
 {
   return GNUNET_SYSERR;
 }
+#endif
 
-
 /**
  * Returns the private key on success,
  * NULL on error.
  */
-static struct GNUNET_RSA_PrivateKeyEncoded *
+static GNUNET_RSA_PrivateKeyEncoded *
 GNUNET_CHAT_initPrivateKey (struct GNUNET_GE_Context *ectx,
                             struct GNUNET_GC_Configuration *cfg,
                             const char *nick_name)
@@ -269,6 +285,7 @@
   char *gnHome;
   char *keyfile;
   GNUNET_RSA_PrivateKeyEncoded *encPrivateKey;
+  struct GNUNET_RSA_PrivateKey *privKey;
   unsigned short len;
   int res;
 
@@ -340,13 +357,13 @@
                               keyfile,
                               encPrivateKey, ntohs (encPrivateKey->len),
                               "600");
-      GNUNET_RSA_free_key(key);
+      GNUNET_RSA_free_key(privKey);
       GNUNET_GE_LOG (ectx,
                      GNUNET_GE_INFO | GNUNET_GE_USER | GNUNET_GE_BULK,
                      _("Done creating key.\n"));
     }
   GNUNET_free (keyfile);
-  GNUNET_GE_ASSERT (ectx, privKey != NULL);
+  GNUNET_GE_ASSERT (ectx, encPrivateKey != NULL);
   return encPrivateKey;
 }
 
@@ -381,7 +398,7 @@
 {
   struct GNUNET_CHAT_Room *chat_room;
   struct GNUNET_ClientServerConnection *sock;
-  struct GNUNET_RSA_PrivateKeyEncoded *key;
+  GNUNET_RSA_PrivateKeyEncoded *key;
 
   key = GNUNET_CHAT_initPrivateKey(ectx, cfg, nick_name);
   if (key == NULL)
@@ -389,7 +406,7 @@
   sock = GNUNET_client_connection_create (ectx, cfg);
   if (sock == NULL)
     {
-      GNUNET_RSA_free_key(key);
+      GNUNET_free(key);
       return NULL;
     }
   chat_room = GNUNET_malloc (sizeof (struct GNUNET_CHAT_Room));
@@ -397,11 +414,11 @@
   chat_room->room_name = GNUNET_strdup (room_name);
   chat_room->member_info = GNUNET_ECRS_meta_data_duplicate(member_info);
   chat_room->my_private_key = key;
-  chat_room->message_callback = callback;
-  chat_room->message_callback_cls = cls;
+  chat_room->message_callback = messageCallback;
+  chat_room->message_callback_cls = message_cls;
   chat_room->member_list_callback = memberCallback;
-  chat_room->member_list_callback_cls = membercls;
-  chat_room->confirmation_callback = confirmation_callback;
+  chat_room->member_list_callback_cls = member_cls;
+  chat_room->confirmation_callback = confirmationCallback;
   chat_room->confirmation_cls = confirmation_cls;
   chat_room->ectx = ectx;
   chat_room->cfg = cfg;
@@ -443,7 +460,7 @@
   GNUNET_free (chat_room->nickname);
   GNUNET_ECRS_meta_data_destroy(chat_room->member_info);
   GNUNET_client_connection_destroy (chat_room->sock);
-  GNUNET_RSA_free_key(chat_room->my_private_key);
+  GNUNET_free(chat_room->my_private_key);
   GNUNET_free (chat_room);
 }
 
@@ -456,13 +473,12 @@
 int
 GNUNET_CHAT_send_message (struct GNUNET_CHAT_Room *room,
                           const char *message,
-                          GNUNET_CHAT_MessageConfirmation callback,
-                          void *cls,
                           GNUNET_CHAT_MSG_OPTIONS options,
-                          const GNUNET_RSA_PublicKey * receiver)
+                          const GNUNET_RSA_PublicKey * receiver,
+                         unsigned int * sequence_number)
 {
   int ret = GNUNET_OK;
-  CS_chat_MESSAGE *msg_to_send;
+  CS_chat_MESSAGE_TransmitRequest *msg_to_send;
   unsigned int msg_size;
 
   msg_size = strlen(message) + sizeof(CS_chat_MESSAGE_TransmitRequest);
@@ -472,7 +488,8 @@
   msg_to_send->header.size = htons(msg_size);
   msg_to_send->header.type = htons(GNUNET_CS_PROTO_CHAT_TRANSMIT_REQUEST);
   msg_to_send->msg_options = htonl(options);
-  msg_to_send->sequence_number = room->sequence_number++;
+  *sequence_number = room->sequence_number++;
+  msg_to_send->sequence_number = htonl(*sequence_number);  
   msg_to_send->reserved = htonl(0);
   if (receiver == NULL)
     memset(&msg_to_send->target, 0, sizeof(GNUNET_HashCode));





reply via email to

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