2 This file is part of GNUnet.
3 (C) 2008, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file chat/chat_api.c
23 * @brief convenience API for sending and receiving chat messages
24 * @author Christian Grothoff
25 * @author Nathan Evans
26 * @author Vitaly Minko
30 #include "gnunet_constants.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_signatures.h"
35 #define DEBUG_CHAT GNUNET_YES
36 #define NICK_IDENTITY_PREFIX ".chat_identity_"
40 * Handle for a (joined) chat room.
42 struct GNUNET_CHAT_Room
44 struct GNUNET_CLIENT_Connection *client;
46 const struct GNUNET_CONFIGURATION_Handle *cfg;
48 struct GNUNET_CONTAINER_MetaData *member_info;
52 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
54 struct MemberList *members;
56 GNUNET_CHAT_MessageCallback message_callback;
58 void *message_callback_cls;
60 GNUNET_CHAT_MemberListCallback member_list_callback;
62 void *member_list_callback_cls;
64 GNUNET_CHAT_MessageConfirmation confirmation_callback;
66 void *confirmation_cls;
68 uint32_t sequence_number;
75 * Linked list of members in the chat room.
79 struct MemberList *next;
82 * Description of the member.
84 struct GNUNET_CONTAINER_MetaData *meta;
87 * Member ID (pseudonym).
94 * Context for transmitting a send-message request.
96 struct GNUNET_CHAT_SendMessageContext
99 * Handle for the chat room.
101 struct GNUNET_CHAT_Room *chat_room;
104 * Message that we're sending.
109 * Options for the message.
111 enum GNUNET_CHAT_MsgOptions options;
114 * Receiver of the message. NULL to send to everyone in the room.
116 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *receiver;
119 * Sequence id of the message.
121 uint32_t sequence_number;
126 * Context for transmitting a confirmation receipt.
128 struct GNUNET_CHAT_SendReceiptContext
131 * Handle for the chat room.
133 struct GNUNET_CHAT_Room *chat_room;
136 * The original message that we're going to acknowledge.
138 struct ReceiveNotificationMessage *received_msg;
143 * Ask client to send a join request.
146 GNUNET_CHAT_rejoin_room (struct GNUNET_CHAT_Room *chat_room);
150 * Transmit a confirmation receipt to the chat service.
152 * @param cls closure, pointer to the 'struct GNUNET_CHAT_SendReceiptContext'
153 * @param size number of bytes available in buf
154 * @param buf where the callee should write the message
155 * @return number of bytes written to buf
158 transmit_acknowledge_request (void *cls,
162 struct GNUNET_CHAT_SendReceiptContext *src = cls;
163 struct ConfirmationReceiptMessage *receipt;
164 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub_key;
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
171 _("Could not transmit confirmation receipt\n"));
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Transmitting confirmation receipt to the service\n");
178 msg_size = sizeof (struct ConfirmationReceiptMessage);
179 GNUNET_assert (size >= msg_size);
181 receipt->header.size = htons (msg_size);
182 receipt->header.type =
183 htons (GNUNET_MESSAGE_TYPE_CHAT_CONFIRMATION_RECEIPT);
184 receipt->sequence_number = src->received_msg->sequence_number;
185 receipt->reserved2 = 0;
186 receipt->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
187 GNUNET_CRYPTO_rsa_key_get_public (src->chat_room->my_private_key, &pub_key);
188 GNUNET_CRYPTO_hash (&pub_key,
189 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
191 receipt->author = src->received_msg->sender;
192 receipt->purpose.purpose =
193 htonl (GNUNET_SIGNATURE_PURPOSE_CHAT_RECEIPT);
194 receipt->purpose.size =
196 sizeof (struct GNUNET_MessageHeader) -
198 sizeof (struct GNUNET_CRYPTO_RsaSignature));
199 msg_len = ntohs (src->received_msg->header.size) -
200 sizeof (struct ReceiveNotificationMessage);
201 GNUNET_CRYPTO_hash (&src->received_msg[1], msg_len, &receipt->content);
202 GNUNET_assert (GNUNET_OK ==
203 GNUNET_CRYPTO_rsa_sign (src->chat_room->my_private_key,
205 &receipt->signature));
206 GNUNET_free (src->received_msg);
213 * Handles messages received from the service. Calls the proper client
217 process_result (struct GNUNET_CHAT_Room *room,
218 const struct GNUNET_MessageHeader *reply)
220 struct LeaveNotificationMessage *leave_msg;
221 struct JoinNotificationMessage *join_msg;
222 struct ReceiveNotificationMessage *received_msg;
223 struct ConfirmationReceiptMessage *receipt;
225 struct GNUNET_CONTAINER_MetaData *meta;
226 struct GNUNET_CHAT_SendReceiptContext *src;
227 struct MemberList *pos;
228 struct MemberList *prev;
229 struct GNUNET_CRYPTO_AesSessionKey key;
230 char decrypted_msg[MAX_MESSAGE_LENGTH];
234 char *message_content;
236 size = ntohs (reply->size);
237 switch (ntohs (reply->type))
239 case GNUNET_MESSAGE_TYPE_CHAT_JOIN_NOTIFICATION:
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a join notification\n");
243 if (size < sizeof (struct JoinNotificationMessage))
248 join_msg = (struct JoinNotificationMessage *) reply;
249 meta_len = size - sizeof (struct JoinNotificationMessage);
251 GNUNET_CONTAINER_meta_data_deserialize ((const char *) &join_msg[1],
258 pos = GNUNET_malloc (sizeof (struct MemberList));
260 GNUNET_CRYPTO_hash (&join_msg->public_key,
261 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
263 GNUNET_PSEUDONYM_add (room->cfg, &pos->id, meta);
264 room->member_list_callback (room->member_list_callback_cls,
265 meta, &join_msg->public_key,
266 ntohl (join_msg->msg_options));
267 pos->next = room->members;
270 case GNUNET_MESSAGE_TYPE_CHAT_LEAVE_NOTIFICATION:
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a leave notification\n");
274 if (size < sizeof (struct LeaveNotificationMessage))
279 leave_msg = (struct LeaveNotificationMessage *) reply;
280 room->member_list_callback (room->member_list_callback_cls,
281 NULL, &leave_msg->user,
282 GNUNET_CHAT_MSG_OPTION_NONE);
283 GNUNET_CRYPTO_hash (&leave_msg->user,
284 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
288 while ((NULL != pos) &&
289 (0 != memcmp (&pos->id, &id, sizeof (GNUNET_HashCode))))
294 GNUNET_assert (NULL != pos);
296 room->members = pos->next;
298 prev->next = pos->next;
299 GNUNET_CONTAINER_meta_data_destroy (pos->meta);
302 case GNUNET_MESSAGE_TYPE_CHAT_MESSAGE_NOTIFICATION:
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a message notification\n");
306 if (size <= sizeof (struct ReceiveNotificationMessage))
311 received_msg = (struct ReceiveNotificationMessage *) reply;
313 (ntohl (received_msg->msg_options) & GNUNET_CHAT_MSG_ACKNOWLEDGED))
315 src = GNUNET_malloc (sizeof (struct GNUNET_CHAT_SendReceiptContext));
316 src->chat_room = room;
317 src->received_msg = GNUNET_memdup (received_msg, size);
318 GNUNET_CLIENT_notify_transmit_ready (room->client,
319 sizeof (struct ConfirmationReceiptMessage),
320 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
322 &transmit_acknowledge_request,
325 msg_len = size - sizeof (struct ReceiveNotificationMessage);
327 (ntohl (received_msg->msg_options) & GNUNET_CHAT_MSG_PRIVATE))
329 if (-1 == GNUNET_CRYPTO_rsa_decrypt (room->my_private_key,
330 &received_msg->encrypted_key,
332 sizeof (struct GNUNET_CRYPTO_AesSessionKey)))
337 msg_len = GNUNET_CRYPTO_aes_decrypt (&received_msg[1],
340 (const struct GNUNET_CRYPTO_AesInitializationVector *) INITVALUE,
342 message_content = decrypted_msg;
346 message_content = GNUNET_malloc (msg_len + 1);
347 memcpy (message_content, &received_msg[1], msg_len);
349 message_content[msg_len] = '\0';
351 while ((NULL != pos) &&
352 (0 != memcmp (&pos->id,
353 &received_msg->sender,
354 sizeof (GNUNET_HashCode))))
356 GNUNET_assert (NULL != pos);
357 room->message_callback (room->message_callback_cls,
359 &received_msg->sender,
362 ntohl (received_msg->msg_options));
363 if (message_content != decrypted_msg)
364 GNUNET_free (message_content);
366 case GNUNET_MESSAGE_TYPE_CHAT_CONFIRMATION_NOTIFICATION:
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a confirmation receipt\n");
370 if (size < sizeof (struct ConfirmationReceiptMessage))
375 receipt = (struct ConfirmationReceiptMessage *) reply;
376 if (NULL != room->confirmation_callback)
377 room->confirmation_callback (room->confirmation_cls,
379 ntohl (receipt->sequence_number),
380 GNUNET_TIME_absolute_ntoh (receipt->timestamp),
383 &receipt->signature);
386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
387 _("Unknown message type: '%u'\n"), ntohs (reply->type));
395 * Listen for incoming messages on this chat room. Also, support servers going
396 * away/coming back (i.e. rejoin chat room to keep server state up to date).
398 * @param cls closure, pointer to the 'struct GNUNET_CHAT_Room'
399 * @param msg message received, NULL on timeout or fatal error
402 receive_results (void *cls,
403 const struct GNUNET_MessageHeader *msg)
405 struct GNUNET_CHAT_Room *chat_room = cls;
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a message from the service\n");
410 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & GNUNET_SCHEDULER_get_reason ()))
415 GNUNET_CHAT_rejoin_room (chat_room);
418 process_result (chat_room, msg);
419 if (NULL == chat_room->client)
420 return; /* fatal error */
421 /* continue receiving */
422 GNUNET_CLIENT_receive (chat_room->client,
425 GNUNET_TIME_UNIT_FOREVER_REL);
430 * Read existing private key from file or create a new one if it does not exist
432 * Returns the private key on success, NULL on error.
434 static struct GNUNET_CRYPTO_RsaPrivateKey *
435 GNUNET_CHAT_initPrivateKey (const struct GNUNET_CONFIGURATION_Handle *cfg,
436 const char *nick_name)
440 struct GNUNET_CRYPTO_RsaPrivateKey *privKey;
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Initializing private key\n");
446 GNUNET_CONFIGURATION_get_value_filename (cfg,
451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
452 _("Configuration option `%s' in section `%s' missing\n"),
457 GNUNET_DISK_directory_create (home);
458 if (GNUNET_OK != GNUNET_DISK_directory_test (home))
460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
461 _("Failed to access chat home directory `%s'\n"),
466 /* read or create private key */
468 GNUNET_malloc (strlen (home) + strlen (NICK_IDENTITY_PREFIX) +
469 strlen (nick_name) + 2);
470 strcpy (keyfile, home);
472 if (keyfile[strlen (keyfile) - 1] != DIR_SEPARATOR)
473 strcat (keyfile, DIR_SEPARATOR_STR);
474 strcat (keyfile, NICK_IDENTITY_PREFIX);
475 strcat (keyfile, nick_name);
476 privKey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
480 _("Failed to create/open key in file `%s'\n"),
483 GNUNET_free (keyfile);
489 * Transmit a join request to the chat service.
491 * @param cls closure, pointer to the 'struct GNUNET_CHAT_Room'
492 * @param size number of bytes available in buf
493 * @param buf where the callee should write the message
494 * @return number of bytes written to buf
497 transmit_join_request (void *cls,
501 struct GNUNET_CHAT_Room *chat_room = cls;
502 struct JoinRequestMessage *join_msg;
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "Could not transmit join request\n");
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Transmitting join request to the service\n");
521 room_len = strlen (chat_room->room_name);
522 meta_len = GNUNET_CONTAINER_meta_data_get_serialized_size (chat_room->member_info);
523 size_of_join = sizeof (struct JoinRequestMessage) + meta_len + room_len;
524 GNUNET_assert (size >= size_of_join);
526 join_msg->header.size = htons (size);
527 join_msg->header.type = htons (GNUNET_MESSAGE_TYPE_CHAT_JOIN_REQUEST);
528 join_msg->msg_options = htonl (chat_room->msg_options);
529 join_msg->room_name_len = htons (room_len);
530 join_msg->reserved = htons (0);
531 GNUNET_CRYPTO_rsa_key_get_public (chat_room->my_private_key, &join_msg->public_key);
532 room = (char *) &join_msg[1];
533 memcpy (room, chat_room->room_name, room_len);
534 meta = &room[room_len];
536 GNUNET_CONTAINER_meta_data_serialize (chat_room->member_info,
539 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL))
541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
542 _("Could not serialize metadata\n"));
550 * Ask to send a join request.
553 GNUNET_CHAT_rejoin_room (struct GNUNET_CHAT_Room *chat_room)
557 size_of_join = sizeof (struct JoinRequestMessage) +
558 GNUNET_CONTAINER_meta_data_get_serialized_size (chat_room->member_info) +
559 strlen (chat_room->room_name);
561 GNUNET_CLIENT_notify_transmit_ready (chat_room->client,
563 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
565 &transmit_join_request,
567 return GNUNET_SYSERR;
576 GNUNET_CHAT_leave_room (struct GNUNET_CHAT_Room *chat_room)
578 struct MemberList *pos;
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Leaving the room '%s'\n", chat_room->room_name);
584 GNUNET_CLIENT_disconnect (chat_room->client, GNUNET_NO);
585 GNUNET_free (chat_room->room_name);
586 GNUNET_CONTAINER_meta_data_destroy (chat_room->member_info);
587 GNUNET_CRYPTO_rsa_key_free (chat_room->my_private_key);
588 while (NULL != chat_room->members)
590 pos = chat_room->members;
591 chat_room->members = pos->next;
592 GNUNET_CONTAINER_meta_data_destroy (pos->meta);
595 GNUNET_free (chat_room);
602 * @param cfg configuration
603 * @param nick_name nickname of the user joining (used to
604 * determine which public key to use);
605 * the nickname should probably also
606 * be used in the member_info (as "EXTRACTOR_TITLE")
607 * @param member_info information about the joining member
608 * @param room_name name of the room
609 * @param msg_options message options of the joining user
610 * @param messageCallback which function to call if a message has
612 * @param message_cls argument to callback
613 * @param memberCallback which function to call for join/leave notifications
614 * @param member_cls argument to callback
615 * @param confirmationCallback which function to call for confirmations (maybe NULL)
616 * @param confirmation_cls argument to callback
617 * @param me member ID (pseudonym)
618 * @return NULL on error
620 struct GNUNET_CHAT_Room *
621 GNUNET_CHAT_join_room (const struct GNUNET_CONFIGURATION_Handle *cfg,
622 const char *nick_name,
623 struct GNUNET_CONTAINER_MetaData *member_info,
624 const char *room_name,
625 enum GNUNET_CHAT_MsgOptions msg_options,
626 GNUNET_CHAT_MessageCallback messageCallback,
628 GNUNET_CHAT_MemberListCallback memberCallback,
630 GNUNET_CHAT_MessageConfirmation confirmationCallback,
631 void *confirmation_cls,
634 struct GNUNET_CHAT_Room *chat_room;
635 struct GNUNET_CRYPTO_RsaPrivateKey *priv_key;
636 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub_key;
637 struct GNUNET_CLIENT_Connection *client;
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Joining the room '%s'\n", room_name);
642 priv_key = GNUNET_CHAT_initPrivateKey (cfg, nick_name);
643 if (NULL == priv_key)
645 GNUNET_CRYPTO_rsa_key_get_public (priv_key, &pub_key);
646 GNUNET_CRYPTO_hash (&pub_key,
647 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
649 GNUNET_PSEUDONYM_add (cfg, me, member_info);
650 client = GNUNET_CLIENT_connect ("chat", cfg);
653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
654 _("Failed to connect to the chat service\n"));
657 chat_room = GNUNET_malloc (sizeof (struct GNUNET_CHAT_Room));
658 chat_room->msg_options = msg_options;
659 chat_room->room_name = GNUNET_strdup (room_name);
660 chat_room->member_info = GNUNET_CONTAINER_meta_data_duplicate (member_info);
661 chat_room->my_private_key = priv_key;
662 chat_room->message_callback = messageCallback;
663 chat_room->message_callback_cls = message_cls;
664 chat_room->member_list_callback = memberCallback;
665 chat_room->member_list_callback_cls = member_cls;
666 chat_room->confirmation_callback = confirmationCallback;
667 chat_room->confirmation_cls = confirmation_cls;
668 chat_room->cfg = cfg;
669 chat_room->client = client;
670 chat_room->members = NULL;
671 GNUNET_CLIENT_receive (client,
674 GNUNET_TIME_UNIT_FOREVER_REL);
675 if (GNUNET_SYSERR == GNUNET_CHAT_rejoin_room (chat_room))
677 GNUNET_CHAT_leave_room (chat_room);
685 * Transmit a send-message request to the chat service.
687 * @param cls closure, pointer to the 'struct GNUNET_CHAT_SendMessageContext'
688 * @param size number of bytes available in buf
689 * @param buf where the callee should write the message
690 * @return number of bytes written to buf
693 transmit_send_request (void *cls,
697 struct GNUNET_CHAT_SendMessageContext *smc = cls;
698 struct TransmitRequestMessage *msg_to_send;
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Could not transmit a chat message\n");
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 "Transmitting a chat message to the service\n");
713 msg_size = strlen (smc->message) + sizeof (struct TransmitRequestMessage);
714 GNUNET_assert (size >= msg_size);
716 msg_to_send->header.size = htons (msg_size);
717 msg_to_send->header.type = htons (GNUNET_MESSAGE_TYPE_CHAT_TRANSMIT_REQUEST);
718 msg_to_send->msg_options = htonl (smc->options);
719 msg_to_send->sequence_number = htonl (smc->sequence_number);
720 msg_to_send->reserved = htonl (0);
721 if (NULL == smc->receiver)
722 memset (&msg_to_send->target, 0, sizeof (GNUNET_HashCode));
724 GNUNET_CRYPTO_hash (smc->receiver,
725 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
726 &msg_to_send->target);
727 memcpy (&msg_to_send[1], smc->message, strlen (smc->message));
729 * Client don't encode private messages since public keys of other members are
730 * stored on the service side.
732 if (smc->options & GNUNET_CHAT_MSG_AUTHENTICATED)
734 msg_to_send->purpose.purpose =
735 htonl (GNUNET_SIGNATURE_PURPOSE_CHAT_MESSAGE);
736 msg_to_send->purpose.size =
738 sizeof (struct GNUNET_MessageHeader) -
739 sizeof (struct GNUNET_CRYPTO_RsaSignature));
740 GNUNET_assert (GNUNET_OK ==
741 GNUNET_CRYPTO_rsa_sign (smc->chat_room->my_private_key,
742 &msg_to_send->purpose,
743 &msg_to_send->signature));
745 GNUNET_free (smc->message);
754 * @param room handle for the chat room
755 * @param message message to be sent
756 * @param options options for the message
757 * @param receiver use NULL to send to everyone in the room
758 * @param sequence_number where to write the sequence id of the message
761 GNUNET_CHAT_send_message (struct GNUNET_CHAT_Room *room,
763 enum GNUNET_CHAT_MsgOptions options,
764 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *receiver,
765 uint32_t *sequence_number)
768 struct GNUNET_CHAT_SendMessageContext *smc;
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending a message\n");
773 *sequence_number = ++room->sequence_number;
774 smc = GNUNET_malloc (sizeof (struct GNUNET_CHAT_SendMessageContext));
775 smc->chat_room = room;
776 smc->message = GNUNET_strdup (message);
777 smc->options = options;
778 smc->receiver = receiver;
779 smc->sequence_number = *sequence_number;
780 msg_size = strlen (message) + sizeof (struct TransmitRequestMessage);
781 GNUNET_CLIENT_notify_transmit_ready (room->client,
783 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
785 &transmit_send_request,