2 This file is part of GNUnet.
3 (C) 2012 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 2, 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 lockmanager/gnunet-service-lockmanager.c
23 * @brief implementation of the LOCKMANAGER service
24 * @author Sree Harsha Totakura
28 #include "gnunet_common.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_service_lib.h"
32 #include "gnunet_server_lib.h"
34 #include "lockmanager.h"
37 #define LOG(kind,...) \
38 GNUNET_log (kind, __VA_ARGS__)
40 #define TIME_REL_MINS(min) \
41 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, min)
43 #define TIMEOUT TIME_REL_MINS(3)
47 * Doubly linked list of clients having connections to us
53 * Doubly linked list of clients waiting for a lock
58 * The next client structure
60 struct WaitList *next;
63 * The prev client structure
65 struct WaitList *prev;
68 * Pointer to the client
70 struct ClientList *cl_entry;
75 * Structure representing a Lock
80 * List head of clients waiting for this lock
82 struct WaitList *wl_head;
85 * List tail of clients waiting for this lock
87 struct WaitList *wl_tail;
90 * The client which is currently holding this lock
92 struct ClientList *cl_entry;
95 * The name of the locking domain this lock belongs to
100 * The number of this lock
107 * A Lock element for a doubly linked list
112 * The next element pointer
114 struct LockList *next;
117 * Pointer to the previous element
119 struct LockList *prev;
122 * Pointer to the Lock
129 * Doubly linked list of clients having connections to us
135 * The next client structure
137 struct ClientList *next;
140 * The previous client structure
142 struct ClientList *prev;
145 * Head of the doubly linked list of the currently held locks by this client
147 struct LockList *ll_head;
150 * Tail of the doubly linked list of the currently held locks by this client
152 struct LockList *ll_tail;
155 * Pointer to the client
157 struct GNUNET_SERVER_Client *client;
162 * Structure for matching a lock
167 * The matched LockingRequest entry; Should be NULL if no entry is found
169 struct Lock *matched_entry;
172 * The locking domain name of the lock
174 const char *domain_name;
184 * Map of lock-keys to the 'struct LockList' entry for the key.
186 static struct GNUNET_CONTAINER_MultiHashMap *lock_map;
189 * Head of the doubly linked list of clients currently connected
191 static struct ClientList *cl_head;
194 * Tail of the doubly linked list of clients currently connected
196 static struct ClientList *cl_tail;
200 * Get the key for the given lock in the 'lock_map'.
204 * @param key set to the key
207 get_key (const char *domain_name,
208 uint32_t lock_number,
209 struct GNUNET_HashCode *key)
213 GNUNET_CRYPTO_hash (domain_name,
214 strlen (domain_name),
216 last_32 = (uint32_t *) key;
217 *last_32 ^= lock_number;
222 * Hashmap iterator for matching a lock
224 * @param cls the LockMatch structure
225 * @param key current key code
226 * @param value value in the hash map (struct Lock)
227 * @return GNUNET_YES if we should continue to
232 match_iterator (void *cls, const GNUNET_HashCode *key, void *value)
234 struct LockMatch *match = cls;
235 struct Lock *lock = value;
237 if ( (match->lock_num == lock->lock_num)
238 && (0 == strcmp (match->domain_name, lock->domain_name)) )
240 match->matched_entry = lock;
248 * Function to search for a lock in the global lock hashmap
250 * @param domain_name the name of the locking domain
251 * @param lock_num the number of the lock
252 * @return the lock if found; NULL if not
255 find_lock (const char *domain_name,
256 const uint32_t lock_num)
259 struct LockMatch match;
260 struct GNUNET_HashCode key;
262 match.lock_num = lock_num;
263 match.domain_name = domain_name;
264 match.matched_entry = NULL;
265 get_key (domain_name, lock_num, &key);
266 GNUNET_CONTAINER_multihashmap_get_multiple (lock_map,
270 return match.matched_entry;
275 * Adds a lock to the global lock hashmap
277 * @param domain_name the name of the lock's locking domain
278 * @param lock_num the lock number
279 * @return pointer to the lock structure which is added to lock map
282 add_lock (const char *domain_name,
286 struct GNUNET_HashCode key;
287 size_t domain_name_len;
289 lock = GNUNET_malloc (sizeof (struct Lock));
290 domain_name_len = strlen (domain_name) + 1;
291 lock->domain_name = GNUNET_malloc (domain_name_len);
292 strncpy (lock->domain_name, domain_name, domain_name_len);
293 lock->lock_num = lock_num;
294 get_key (domain_name, lock_num, &key);
295 LOG (GNUNET_ERROR_TYPE_DEBUG,
296 "Adding a lock with num: %d and domain: %s to the lock map\n",
297 lock->lock_num, lock->domain_name);
298 GNUNET_CONTAINER_multihashmap_put (lock_map,
301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
307 * Removes a lock from the lock map. The WaitList of the lock should be empty
309 * @param lock the lock to remove
312 remove_lock (struct Lock *lock)
314 struct GNUNET_HashCode key;
316 GNUNET_assert (NULL == lock->wl_head);
317 get_key (lock->domain_name,
320 LOG (GNUNET_ERROR_TYPE_DEBUG,
321 "Removing lock with num: %u, domain: %s from lock map\n",
322 lock->lock_num, lock->domain_name);
323 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove
324 (lock_map, &key, lock));
325 GNUNET_free (lock->domain_name);
331 * Find the LockList entry corresponding to the given Lock in a ClientList
334 * @param cl_entry the ClientList entry whose lock list has to be searched
335 * @param lock the lock which has to be matched
336 * @return the matching LockList entry; NULL if no match is found
338 static struct LockList *
339 cl_ll_find_lock (struct ClientList *cl_entry,
340 const struct Lock *lock)
342 struct LockList *ll_entry;
344 for (ll_entry = cl_entry->ll_head;
345 NULL != ll_entry; ll_entry = ll_entry->next)
347 if (lock == ll_entry->lock)
355 * Function to append a lock to the lock list of a ClientList entry
357 * @param cl_entry the client which currently owns this lock
358 * @param lock the lock to be added to the cl_entry's lock list
361 cl_ll_add_lock (struct ClientList *cl_entry,
364 struct LockList *ll_entry;
366 ll_entry = GNUNET_malloc (sizeof (struct LockList));
367 ll_entry->lock = lock;
368 LOG (GNUNET_ERROR_TYPE_DEBUG,
369 "Adding a lock with num: %u and domain: %s to lock list\n",
370 lock->lock_num, lock->domain_name);
371 GNUNET_CONTAINER_DLL_insert_tail (cl_entry->ll_head,
378 * Function to delete a lock from the lock list of the given ClientList entry
380 * @param cl_entry the ClientList entry
381 * @param ll_entry the LockList entry to be deleted
384 cl_ll_remove_lock (struct ClientList *cl_entry,
385 struct LockList *ll_entry)
387 LOG (GNUNET_ERROR_TYPE_DEBUG,
388 "Removing lock with num: %u, domain: %s from lock list of a client\n",
389 ll_entry->lock->lock_num,
390 ll_entry->lock->domain_name);
391 GNUNET_assert (NULL != cl_entry->ll_head);
392 GNUNET_CONTAINER_DLL_remove (cl_entry->ll_head,
395 GNUNET_free (ll_entry);
400 * Find a WaitList entry in the waiting list of a lock
402 * @param lock the lock whose wait list has to be searched
403 * @param cl_entry the ClientList entry to be searched
404 * @return the WaitList entry matching the given cl_entry; NULL if not match
407 static struct WaitList *
408 lock_wl_find (const struct Lock *lock,
409 const struct ClientList *cl_entry)
411 struct WaitList *wl_entry;
413 for (wl_entry = lock->wl_head;
415 wl_entry = wl_entry->next)
417 if (cl_entry == wl_entry->cl_entry)
425 * Add a client to the wait list of given lock
427 * @param lock the lock list entry of a lock
428 * @param cl_entry the client to queue for the lock's wait list
431 lock_wl_add_client (struct Lock *lock,
432 struct ClientList *cl_entry)
434 struct WaitList *wl_entry;
436 LOG (GNUNET_ERROR_TYPE_DEBUG,
437 "Adding a client to lock's wait list (lock num: %u, domain: %s)\n",
440 wl_entry = GNUNET_malloc (sizeof (struct WaitList));
441 wl_entry->cl_entry = cl_entry;
442 GNUNET_CONTAINER_DLL_insert_tail (lock->wl_head,
449 * Remove an entry from the wait list of the given lock
451 * @param lock the lock
452 * @param wl_entry the wait list entry to be removed
455 lock_wl_remove (struct Lock *lock,
456 struct WaitList *wl_entry)
458 LOG (GNUNET_ERROR_TYPE_DEBUG,
459 "Removing client from wait list of lock with num: %u, domain: %s\n",
460 lock->lock_num, lock->domain_name);
461 GNUNET_CONTAINER_DLL_remove (lock->wl_head,
464 GNUNET_free (wl_entry);
469 * Search for a client in the client list
471 * @param client the client to be searched for
472 * @return the ClientList entry; NULL if the client is not found
474 static struct ClientList *
475 cl_find_client (const struct GNUNET_SERVER_Client *client)
477 struct ClientList *current;
479 for (current = cl_head; NULL != current; current = current->next)
480 if (client == current->client)
487 * Append a client to the client list
489 * @param client the client to be appended to the list
490 * @return the client list entry which is added to the client list
492 static struct ClientList *
493 cl_add_client (struct GNUNET_SERVER_Client *client)
495 struct ClientList *new_client;
497 LOG (GNUNET_ERROR_TYPE_DEBUG,
498 "Adding a client to the client list\n");
499 new_client = GNUNET_malloc (sizeof (struct ClientList));
500 GNUNET_SERVER_client_keep (client);
501 new_client->client = client;
502 GNUNET_CONTAINER_DLL_insert_tail (cl_head,
510 * Delete the given client from the client list. The LockList should be empty
512 * @param cl_entry the client list entry to delete
515 cl_remove_client (struct ClientList *cl_entry)
517 GNUNET_assert (NULL == cl_entry->ll_head);
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Removing a client from the client list\n");
520 GNUNET_SERVER_client_drop (cl_entry->client);
521 GNUNET_CONTAINER_DLL_remove (cl_head,
524 GNUNET_free (cl_entry);
529 * Transmit notify for sending message to client
531 * @param cls the message to send
532 * @param size number of bytes available in buf
533 * @param buf where the callee should write the message
534 * @return number of bytes written to buf
537 transmit_notify (void *cls, size_t size, void *buf)
539 struct GNUNET_LOCKMANAGER_Message *msg = cls;
542 if ((0 == size) || (NULL == buf))
544 /* FIXME: Timed out -- requeue? */
547 msg_size = ntohs (msg->header.size);
548 GNUNET_assert (size >= msg_size);
549 memcpy (buf, msg, msg_size);
551 LOG (GNUNET_ERROR_TYPE_DEBUG,
552 "Message of size %u sent\n", msg_size);
558 * Send SUCCESS message to the client
560 * @param client the client to which the message has to be sent
561 * @param domain_name the locking domain of the successfully acquried lock
562 * @param lock_num the number of the successfully acquired lock
565 send_success_msg (struct GNUNET_SERVER_Client *client,
566 const char *domain_name,
569 struct GNUNET_LOCKMANAGER_Message *reply;
570 size_t domain_name_len;
573 domain_name_len = strlen (domain_name) + 1;
574 reply_size = sizeof (struct GNUNET_LOCKMANAGER_Message) + domain_name_len;
575 reply = GNUNET_malloc (reply_size);
576 reply->header.size = htons (reply_size);
577 reply->header.type = htons (GNUNET_MESSAGE_TYPE_LOCKMANAGER_SUCCESS);
578 reply->lock = htonl (lock_num);
579 strncpy ((char *) &reply[1], domain_name, domain_name_len);
580 LOG (GNUNET_ERROR_TYPE_DEBUG,
581 "Sending SUCCESS message for lock with num: %u, domain: %s\n",
582 lock_num, domain_name);
583 GNUNET_SERVER_notify_transmit_ready (client,
592 * Handler for GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE
595 * @param client the client sending this message
596 * @param message GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE message
599 handle_acquire (void *cls,
600 struct GNUNET_SERVER_Client *client,
601 const struct GNUNET_MessageHeader *message)
603 const struct GNUNET_LOCKMANAGER_Message *request;
604 const char *domain_name;
606 struct ClientList *cl_entry;
610 msize = htons (message->size);
611 if (msize <= sizeof (struct GNUNET_LOCKMANAGER_Message))
614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
617 request = (struct GNUNET_LOCKMANAGER_Message *) message;
618 domain_name = (const char *) &request[1];
619 msize -= sizeof (struct GNUNET_LOCKMANAGER_Message);
620 if ('\0' != domain_name[msize])
623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
626 lock_num = ntohl (request->lock);
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 "Received an ACQUIRE message for lock num: %u domain: %s\n",
629 lock_num, domain_name);
630 if (NULL == (cl_entry = cl_find_client (client)))
631 cl_entry = cl_add_client (client); /* Add client if not in client list */
632 if (NULL != (lock = find_lock (domain_name,lock_num)))
634 if (lock->cl_entry == cl_entry)
635 { /* Client is requesting a lock it already owns */
637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
640 lock_wl_add_client (lock, cl_entry);
641 cl_ll_add_lock (cl_entry, lock);
643 else /* Lock not present */
645 lock = add_lock (domain_name, lock_num);
646 lock->cl_entry = cl_entry;
647 cl_ll_add_lock (cl_entry, lock);
648 send_success_msg (cl_entry->client, domain_name, lock_num);
650 GNUNET_SERVER_receive_done (client, GNUNET_OK);
655 * This function gives the lock to the first client in the wait list of the
656 * lock. If no clients are currently waiting for this lock, the lock is then
659 * @param lock the lock which has to be processed for release
662 process_lock_release (struct Lock *lock)
664 struct WaitList *wl_entry;
666 LOG (GNUNET_ERROR_TYPE_DEBUG,
667 "Processing lock release for lock with num: %u, domain: %s\n",
668 lock->lock_num, lock->domain_name);
669 wl_entry = lock->wl_head;
670 if (NULL == wl_entry)
672 remove_lock (lock); /* No clients waiting for this lock - delete */
675 LOG (GNUNET_ERROR_TYPE_DEBUG,
676 "Giving lock to a client from wait list\n");
677 lock->cl_entry = wl_entry->cl_entry;
678 lock_wl_remove(lock, wl_entry);
679 send_success_msg (lock->cl_entry->client,
687 * Handle for GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE
690 * @param client the client sending this message
691 * @param message the LOCKMANAGER_RELEASE message
694 handle_release (void *cls,
695 struct GNUNET_SERVER_Client *client,
696 const struct GNUNET_MessageHeader *message)
698 const struct GNUNET_LOCKMANAGER_Message *request;
699 struct ClientList *cl_entry;
700 struct WaitList *wl_entry;
701 struct LockList *ll_entry;
702 const char *domain_name;
707 msize = ntohs (message->size);
708 if (msize <= sizeof (struct GNUNET_LOCKMANAGER_Message))
711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
714 request = (const struct GNUNET_LOCKMANAGER_Message *) message;
715 domain_name = (const char *) &request[1];
716 msize -= sizeof (struct GNUNET_LOCKMANAGER_Message);
717 if ('\0' != domain_name[msize-1])
720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
725 lock_num = ntohl (request->lock);
726 LOG (GNUNET_ERROR_TYPE_DEBUG,
727 "Received RELEASE message for lock with num: %d, domain: %s\n",
728 lock_num, domain_name);
729 if (NULL == (cl_entry = cl_find_client (client)))
732 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
735 lock = find_lock (domain_name, lock_num);
739 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
742 if (NULL == (ll_entry = cl_ll_find_lock (cl_entry, lock)))
745 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
748 cl_ll_remove_lock (cl_entry, ll_entry);
749 if (cl_entry == lock->cl_entry)
751 process_lock_release (lock);
752 GNUNET_SERVER_receive_done (client, GNUNET_OK);
755 /* remove 'client' from wait list (check that it is not there...) */
756 if (NULL != (wl_entry = lock_wl_find (lock, cl_entry)))
758 lock_wl_remove (lock, wl_entry);
760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
765 * Callback for client disconnect
768 * @param client the client which has disconnected
771 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
773 struct ClientList *cl_entry;
774 struct LockList *ll_entry;
778 LOG (GNUNET_ERROR_TYPE_DEBUG,
779 "A client has been disconnected -- freeing its locks and resources\n");
780 cl_entry = cl_find_client (client);
781 if (NULL == cl_entry)
783 while (NULL != (ll_entry = cl_entry->ll_head))
785 process_lock_release (ll_entry->lock);
786 cl_ll_remove_lock (cl_entry, ll_entry);
788 cl_remove_client (cl_entry);
793 * Hashmap Iterator to delete lock entries in hash map
796 * @param key current key code
797 * @param value value in the hash map
798 * @return GNUNET_YES if we should continue to
803 lock_delete_iterator (void *cls,
804 const GNUNET_HashCode * key,
807 struct Lock *lock = value;
809 GNUNET_assert (NULL != lock);
810 while (NULL != lock->wl_head)
812 lock_wl_remove (lock, lock->wl_head);
814 GNUNET_assert (GNUNET_YES ==
815 GNUNET_CONTAINER_multihashmap_remove(lock_map,
818 GNUNET_free (lock->domain_name);
825 * Task to clean up and shutdown nicely
831 shutdown_task (void *cls,
832 const struct GNUNET_SCHEDULER_TaskContext *tc)
834 LOG (GNUNET_ERROR_TYPE_DEBUG,
835 "Shutting down lock manager\n");
836 /* Clean the global ClientList */
837 while (NULL != cl_head)
839 while (NULL != cl_head->ll_head) /* Clear the LockList */
841 cl_ll_remove_lock (cl_head, cl_head->ll_head);
843 cl_remove_client (cl_head);
845 /* Clean the global hash table */
846 GNUNET_CONTAINER_multihashmap_iterate (lock_map,
847 &lock_delete_iterator,
849 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (lock_map));
850 GNUNET_CONTAINER_multihashmap_destroy (lock_map);
858 * @param server the initialized server
859 * @param cfg configuration to use
862 lockmanager_run (void *cls,
863 struct GNUNET_SERVER_Handle * server,
864 const struct GNUNET_CONFIGURATION_Handle *cfg)
866 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
868 {&handle_acquire, NULL, GNUNET_MESSAGE_TYPE_LOCKMANAGER_ACQUIRE, 0},
869 {&handle_release, NULL, GNUNET_MESSAGE_TYPE_LOCKMANAGER_RELEASE, 0},
872 GNUNET_SERVER_add_handlers (server,
874 GNUNET_SERVER_disconnect_notify (server,
875 &client_disconnect_cb,
877 lock_map = GNUNET_CONTAINER_multihashmap_create (30);
878 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
885 * The starting point of execution
887 int main (int argc, char *const *argv)
891 GNUNET_SERVICE_run (argc,
894 GNUNET_SERVICE_OPTION_NONE,