Author: nevans Date: 2008-02-22 12:48:54 -0700 (Fri, 22 Feb 2008) New Revision: 6414
Modified: GNUnet/src/applications/chat/chat.c GNUnet/src/applications/chat/chat.h GNUnet/src/applications/chat/clientapi.c Log: Modified: GNUnet/src/applications/chat/chat.c =================================================================== --- GNUnet/src/applications/chat/chat.c 2008-02-22 15:24:15 UTC (rev 6413) +++ GNUnet/src/applications/chat/chat.c 2008-02-22 19:48:54 UTC (rev 6414) @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2001, 2002, 2005, 2006, 2007, 2008 Christian Grothoff (and other contributing authors) + (C) 2008 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -52,7 +52,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 +77,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 */ } @@ -116,7 +116,7 @@ /*TODO: p2p messages will need to be sent as well at some point */ tempClient = client_list_head; - while ((tempClient->next != NULL) && (tempClient->client != NULL)) + while ((tempClient != NULL) && (tempClient->client != NULL)) { if (memcmp (&room_name_hash, &tempClient->room_name_hash, @@ -139,9 +139,8 @@ return GNUNET_OK; } -/* FUNCTION NAME! */ static int -csHandleChatRequest (struct GNUNET_ClientHandle *client, +csHandleChatJoinRequest (struct GNUNET_ClientHandle *client, const GNUNET_MessageHeader * message) { const CS_chat_JOIN_MESSAGE *cmsg; @@ -165,7 +164,7 @@ 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 */ } @@ -197,20 +196,21 @@ /*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->client != client) && + (tempClient != NULL) ) 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; @@ -219,16 +219,16 @@ memcpy (&tempClient->room_name_hash, &room_name_hash, sizeof (GNUNET_HashCode)); tempCount = 0; -#if NATE - while ((tempClient->next != NULL) && (tempClient->client != NULL)) + + while (tempClient != NULL) { tempCount++; tempClient = tempClient->next; } fprintf (stderr, "Number of clients currently is... %d\n", tempCount); -#endif + /* forward to all other TCP chat clients */ /* marker to check if this is a new client */ /* @@ -274,8 +274,9 @@ { int tempCount; - 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); */ @@ -290,33 +291,33 @@ /*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 */ -#if NATE - tempClient = client_list; + + tempClient = client_list_head; tempCount = 0; - while ((tempClient->next != NULL) && (tempClient->client != NULL)) + while (tempClient != NULL) { tempCount++; tempClient = tempClient->next; } fprintf (stderr, "Number of clients currently is... %d\n", tempCount); -#endif + GNUNET_mutex_unlock (chatMutex); fprintf (stderr, "End of handleChatLeave\n"); return; @@ -331,7 +332,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"), @@ -346,7 +347,7 @@ if (GNUNET_SYSERR == capi->registerClientHandler (GNUNET_CS_PROTO_CHAT_JOIN_MSG, - &csHandleChatRequest)) + &csHandleChatJoinRequest)) ok = GNUNET_SYSERR; if (GNUNET_SYSERR == capi->registerClientHandler (GNUNET_CS_PROTO_CHAT_MSG, @@ -371,7 +372,7 @@ coreAPI->unregisterClientHandler (GNUNET_CS_PROTO_CHAT_MSG, &csHandleChatMSG); coreAPI->unregisterClientHandler (GNUNET_CS_PROTO_CHAT_JOIN_MSG, - &csHandleChatRequest); + &csHandleChatJoinRequest); GNUNET_mutex_destroy (chatMutex); coreAPI = NULL; Modified: GNUnet/src/applications/chat/chat.h =================================================================== --- GNUnet/src/applications/chat/chat.h 2008-02-22 15:24:15 UTC (rev 6413) +++ GNUnet/src/applications/chat/chat.h 2008-02-22 19:48:54 UTC (rev 6414) @@ -27,6 +27,7 @@ #define CHAT_CHAT_H #include "gnunet_core.h" +#include "gnunet_chat_lib.h" typedef struct { @@ -58,7 +59,43 @@ } CS_chat_JOIN_MESSAGE; +/** + * Handle for a (joined) chat room. + */ +struct GNUNET_CHAT_Room +{ + struct GNUNET_ClientServerConnection *sock; + struct GNUNET_ThreadHandle *listen_thread; + + struct GNUNET_GE_Context *ectx; + + struct GNUNET_GC_Configuration *cfg; + + char *nickname; + + char *room_name; + + GNUNET_HashCode room_name_hash; + + const GNUNET_RSA_PublicKey *my_public_key; + + GNUNET_HashCode my_public_key_hash; + + const struct GNUNET_RSA_PrivateKey *my_private_key; + + char *memberInfo; + + GNUNET_CHAT_MessageCallback callback; + + int shutdown_flag; + + void *callback_cls; + +}; + +int GNUNET_CHAT_rejoin_room (struct GNUNET_CHAT_Room *chat_room); + #endif /* end of chat.h */ Modified: GNUnet/src/applications/chat/clientapi.c =================================================================== --- GNUnet/src/applications/chat/clientapi.c 2008-02-22 15:24:15 UTC (rev 6413) +++ GNUnet/src/applications/chat/clientapi.c 2008-02-22 19:48:54 UTC (rev 6414) @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2007 Christian Grothoff (and other contributing authors) + (C) 2008 Christian Grothoff (and other contributing authors) GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -32,41 +32,6 @@ #include "chat.h" /** - * Handle for a (joined) chat room. - */ -struct GNUNET_CHAT_Room -{ - struct GNUNET_ClientServerConnection *sock; - - struct GNUNET_ThreadHandle *listen_thread; - - struct GNUNET_GE_Context *ectx; - - struct GNUNET_GC_Configuration *cfg; - - char *nickname; - - char *room_name; - - GNUNET_HashCode room_name_hash; - - const GNUNET_RSA_PublicKey *my_public_key; - - GNUNET_HashCode my_public_key_hash; - - const struct GNUNET_RSA_PrivateKey *my_private_key; - - char *memberInfo; - - GNUNET_CHAT_MessageCallback callback; - - int shutdown_flag; - - void *callback_cls; - -}; - -/** * Listen for incoming messages on this chat room. When received, call the client callback. * Also, support servers going away/coming back (i.e. rejoin chat room to keep server state up to date)... */ @@ -77,87 +42,86 @@ int ret; GNUNET_MessageHeader *reply; CS_chat_MESSAGE *received_msg; + unsigned int size; unsigned int nick_len; unsigned int msg_len; unsigned int room_name_len; - unsigned int retries; + char *nick; char *message_content; char *room_name; int disconnected; ret = GNUNET_OK; - + disconnected = GNUNET_NO; while (room->shutdown_flag != GNUNET_YES) - { - if (disconnected) - { - if (GNUNET_client_connection_ensure_connected (room->sock) != - GNUNET_OK) - { - disconnected = GNUNET_YES; - /* send join! */ - } - else - { - GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); - continue; - } - } + { + 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; - 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); + 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; } + /** * List all of the (publically visible) chat rooms. * @return number of rooms on success, GNUNET_SYSERR if iterator aborted @@ -240,7 +204,7 @@ { /* ALREADY LOGGED */ fprintf (stderr, _("Error writing to socket.\n")); - GNUNET_free (join_msg); + GNUNET_free(join_msg); return NULL; } @@ -273,17 +237,64 @@ return chat_room; } +int +GNUNET_CHAT_rejoin_room (struct GNUNET_CHAT_Room *chat_room) +{ + CS_chat_JOIN_MESSAGE *join_msg; + GNUNET_MessageHeader csHdr; + + GNUNET_HashCode hash_of_me; + GNUNET_HashCode hash_of_room_name; + + int size_of_join; + + 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); + + size_of_join = + sizeof (CS_chat_JOIN_MESSAGE) + strlen (chat_room->nickname) + + sizeof (GNUNET_RSA_PublicKey) + strlen (chat_room->room_name); + join_msg = GNUNET_malloc (size_of_join); + + join_msg->nick_len = htonl (strlen (chat_room->nickname)); + join_msg->pubkey_len = htonl (sizeof (GNUNET_RSA_PublicKey)); + 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)], + chat_room->room_name, strlen (chat_room->room_name)); + + join_msg->header = csHdr; + join_msg->header.size = htons (size_of_join); + + if (GNUNET_SYSERR == + GNUNET_client_connection_write (chat_room->sock, &join_msg->header)) + { + GNUNET_free(join_msg); + return GNUNET_SYSERR; + } + + GNUNET_free (join_msg); + return GNUNET_OK; +} + /** * Leave a chat room. */ 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); @@ -354,5 +365,4 @@ return GNUNET_SYSERR; } - /* end of clientapi.c */ _______________________________________________ GNUnet-SVN mailing list GNUnet-SVN@gnu.org http://lists.gnu.org/mailman/listinfo/gnunet-svn