aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gns_api.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-08-13 17:32:35 +0000
committerChristian Grothoff <christian@grothoff.org>2013-08-13 17:32:35 +0000
commit41d573de1a9bd510a15ec971762c0d7dc77f2901 (patch)
treed0e0249f5a7675d374562fd3ad129c5dac027b04 /src/gns/gns_api.c
parentf76eea97484af4869b1c3aa96459bc7860634444 (diff)
downloadgnunet-41d573de1a9bd510a15ec971762c0d7dc77f2901.tar.gz
gnunet-41d573de1a9bd510a15ec971762c0d7dc77f2901.zip
-simplifying GNS API
Diffstat (limited to 'src/gns/gns_api.c')
-rw-r--r--src/gns/gns_api.c489
1 files changed, 1 insertions, 488 deletions
diff --git a/src/gns/gns_api.c b/src/gns/gns_api.c
index 64f5144c6..9bc85efea 100644
--- a/src/gns/gns_api.c
+++ b/src/gns/gns_api.c
@@ -74,82 +74,6 @@ struct GNUNET_GNS_LookupRequest
74 74
75 75
76/** 76/**
77 * Handle to a shorten request
78 */
79struct GNUNET_GNS_ShortenRequest
80{
81 /**
82 * DLL
83 */
84 struct GNUNET_GNS_ShortenRequest *next;
85
86 /**
87 * DLL
88 */
89 struct GNUNET_GNS_ShortenRequest *prev;
90
91 /**
92 * handle to gns
93 */
94 struct GNUNET_GNS_Handle *gns_handle;
95
96 /**
97 * processor to call on shorten result
98 */
99 GNUNET_GNS_ShortenResultProcessor shorten_proc;
100
101 /**
102 * processor closure
103 */
104 void *proc_cls;
105
106 /**
107 * request id
108 */
109 uint32_t r_id;
110
111};
112
113
114/**
115 * Handle to GetAuthorityRequest
116 */
117struct GNUNET_GNS_GetAuthRequest
118{
119 /**
120 * DLL
121 */
122 struct GNUNET_GNS_GetAuthRequest *next;
123
124 /**
125 * DLL
126 */
127 struct GNUNET_GNS_GetAuthRequest *prev;
128
129 /**
130 * handle to gns
131 */
132 struct GNUNET_GNS_Handle *gns_handle;
133
134 /**
135 * processor to call on authority lookup result
136 */
137 GNUNET_GNS_GetAuthResultProcessor auth_proc;
138
139 /**
140 * processor closure
141 */
142 void *proc_cls;
143
144 /**
145 * request id
146 */
147 uint32_t r_id;
148
149};
150
151
152/**
153 * Entry in our list of messages to be (re-)transmitted. 77 * Entry in our list of messages to be (re-)transmitted.
154 */ 78 */
155struct PendingMessage 79struct PendingMessage
@@ -216,16 +140,6 @@ struct GNUNET_GNS_Handle
216 struct PendingMessage *pending_tail; 140 struct PendingMessage *pending_tail;
217 141
218 /** 142 /**
219 * Head of linked list of shorten messages we would like to transmit.
220 */
221 struct GNUNET_GNS_ShortenRequest *shorten_head;
222
223 /**
224 * Tail of linked list of shorten messages we would like to transmit.
225 */
226 struct GNUNET_GNS_ShortenRequest *shorten_tail;
227
228 /**
229 * Head of linked list of lookup messages we would like to transmit. 143 * Head of linked list of lookup messages we would like to transmit.
230 */ 144 */
231 struct GNUNET_GNS_LookupRequest *lookup_head; 145 struct GNUNET_GNS_LookupRequest *lookup_head;
@@ -234,16 +148,6 @@ struct GNUNET_GNS_Handle
234 * Tail of linked list of lookup messages we would like to transmit. 148 * Tail of linked list of lookup messages we would like to transmit.
235 */ 149 */
236 struct GNUNET_GNS_LookupRequest *lookup_tail; 150 struct GNUNET_GNS_LookupRequest *lookup_tail;
237
238 /**
239 * Head of linked list of authority lookup messages we would like to transmit.
240 */
241 struct GNUNET_GNS_GetAuthRequest *get_auth_head;
242
243 /**
244 * Tail of linked list of authority lookup messages we would like to transmit.
245 */
246 struct GNUNET_GNS_GetAuthRequest *get_auth_tail;
247 151
248 /** 152 /**
249 * Reconnect task 153 * Reconnect task
@@ -317,24 +221,12 @@ reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
317static void 221static void
318force_reconnect (struct GNUNET_GNS_Handle *handle) 222force_reconnect (struct GNUNET_GNS_Handle *handle)
319{ 223{
320 struct GNUNET_GNS_ShortenRequest *st;
321 struct GNUNET_GNS_LookupRequest *lh; 224 struct GNUNET_GNS_LookupRequest *lh;
322 struct GNUNET_GNS_GetAuthRequest *ga;
323 struct PendingMessage *p; 225 struct PendingMessage *p;
324 226
325 GNUNET_CLIENT_disconnect (handle->client); 227 GNUNET_CLIENT_disconnect (handle->client);
326 handle->client = NULL; 228 handle->client = NULL;
327 handle->in_receive = GNUNET_NO; 229 handle->in_receive = GNUNET_NO;
328 for (st = handle->shorten_head; NULL != st; st = st->next)
329 {
330 p = (struct PendingMessage *) &st[1];
331 if (GNUNET_NO == p->transmitted)
332 continue;
333 p->transmitted = GNUNET_NO;
334 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
335 handle->pending_tail,
336 p);
337 }
338 for (lh = handle->lookup_head; NULL != lh; lh = lh->next) 230 for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
339 { 231 {
340 p = (struct PendingMessage *) &lh[1]; 232 p = (struct PendingMessage *) &lh[1];
@@ -345,16 +237,6 @@ force_reconnect (struct GNUNET_GNS_Handle *handle)
345 handle->pending_tail, 237 handle->pending_tail,
346 p); 238 p);
347 } 239 }
348 for (ga = handle->get_auth_head; NULL != ga; ga = ga->next)
349 {
350 p = (struct PendingMessage *) &ga[1];
351 if (GNUNET_NO == p->transmitted)
352 continue;
353 p->transmitted = GNUNET_NO;
354 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
355 handle->pending_tail,
356 p);
357 }
358 handle->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff); 240 handle->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
359 handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff, 241 handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
360 &reconnect_task, 242 &reconnect_task,
@@ -470,101 +352,6 @@ transmit_pending (void *cls, size_t size, void *buf)
470 352
471 353
472/** 354/**
473 * Process a given reply that might match the given
474 * request.
475 *
476 * @param qe a queue entry
477 * @param msg the shorten msg received
478 */
479static void
480process_shorten_reply (struct GNUNET_GNS_ShortenRequest *qe,
481 const struct GNUNET_GNS_ClientShortenResultMessage *msg)
482{
483 struct GNUNET_GNS_Handle *handle = qe->gns_handle;
484 struct PendingMessage *p = (struct PendingMessage *)&qe[1];
485 const char *short_name;
486 size_t mlen;
487
488 if (GNUNET_YES != p->transmitted)
489 {
490 /* service send reply to query we never managed to send!? */
491 GNUNET_break (0);
492 force_reconnect (handle);
493 return;
494 }
495 mlen = ntohs (msg->header.size);
496 if (ntohs (msg->header.size) == sizeof (struct GNUNET_GNS_ClientShortenResultMessage))
497 {
498 /* service reports resolution failed */
499 short_name = NULL;
500 }
501 else
502 {
503 short_name = (const char *) &msg[1];
504 if ('\0' != short_name[mlen - sizeof (struct GNUNET_GNS_ClientShortenResultMessage) - 1])
505 {
506 GNUNET_break (0);
507 force_reconnect (handle);
508 return;
509 }
510 }
511 GNUNET_CONTAINER_DLL_remove (handle->shorten_head, handle->shorten_tail, qe);
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "Received shortened reply `%s' from GNS service\n",
514 short_name);
515 qe->shorten_proc (qe->proc_cls, short_name);
516 GNUNET_free (qe);
517}
518
519
520/**
521 * Process a given reply that might match the given
522 * request.
523 *
524 * @param qe the handle to the request
525 * @param msg the message to process
526 */
527static void
528process_get_auth_reply (struct GNUNET_GNS_GetAuthRequest *qe,
529 const struct GNUNET_GNS_ClientGetAuthResultMessage *msg)
530{
531 struct GNUNET_GNS_Handle *handle = qe->gns_handle;
532 struct PendingMessage *p = (struct PendingMessage *)&qe[1];
533 const char *auth_name;
534 size_t mlen;
535
536 if (GNUNET_YES != p->transmitted)
537 {
538 /* service send reply to query we never managed to send!? */
539 GNUNET_break (0);
540 force_reconnect (handle);
541 return;
542 }
543 mlen = ntohs (msg->header.size);
544 if (mlen == sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage))
545 {
546 auth_name = NULL;
547 }
548 else
549 {
550 auth_name = (const char*) &msg[1];
551 if ('\0' != auth_name[mlen - sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage) - 1])
552 {
553 GNUNET_break (0);
554 force_reconnect (handle);
555 return;
556 }
557 }
558 GNUNET_CONTAINER_DLL_remove (handle->get_auth_head, handle->get_auth_tail, qe);
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Received GET_AUTH reply `%s' from GNS service\n",
561 auth_name);
562 qe->auth_proc (qe->proc_cls, auth_name);
563 GNUNET_free (qe);
564}
565
566
567/**
568 * Process a given reply to the lookup request 355 * Process a given reply to the lookup request
569 * 356 *
570 * @param qe a queue entry 357 * @param qe a queue entry
@@ -621,11 +408,7 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
621{ 408{
622 struct GNUNET_GNS_Handle *handle = cls; 409 struct GNUNET_GNS_Handle *handle = cls;
623 struct GNUNET_GNS_LookupRequest *lr; 410 struct GNUNET_GNS_LookupRequest *lr;
624 struct GNUNET_GNS_ShortenRequest *sr;
625 struct GNUNET_GNS_GetAuthRequest *gar;
626 const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg; 411 const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
627 const struct GNUNET_GNS_ClientShortenResultMessage *shorten_msg;
628 const struct GNUNET_GNS_ClientGetAuthResultMessage *get_auth_msg;
629 uint32_t r_id; 412 uint32_t r_id;
630 413
631 if (NULL == msg) 414 if (NULL == msg)
@@ -653,42 +436,6 @@ process_message (void *cls, const struct GNUNET_MessageHeader *msg)
653 break; 436 break;
654 } 437 }
655 break; 438 break;
656 case GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT:
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
658 "Got SHORTEN_RESULT msg\n");
659 if (ntohs (msg->size) < sizeof (struct GNUNET_GNS_ClientShortenResultMessage))
660 {
661 GNUNET_break (0);
662 force_reconnect (handle);
663 return;
664 }
665 shorten_msg = (const struct GNUNET_GNS_ClientShortenResultMessage *) msg;
666 r_id = ntohl (shorten_msg->id);
667 for (sr = handle->shorten_head; NULL != sr; sr = sr->next)
668 if (sr->r_id == r_id)
669 {
670 process_shorten_reply (sr, shorten_msg);
671 break;
672 }
673 break;
674 case GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT:
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Got GET_AUTH_RESULT msg\n");
677 if (ntohs (msg->size) < sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage))
678 {
679 GNUNET_break (0);
680 force_reconnect (handle);
681 return;
682 }
683 get_auth_msg = (const struct GNUNET_GNS_ClientGetAuthResultMessage *) msg;
684 r_id = ntohl (get_auth_msg->id);
685 for (gar = handle->get_auth_head; NULL != gar; gar = gar->next)
686 if (gar->r_id == r_id)
687 {
688 process_get_auth_reply (gar, get_auth_msg);
689 break;
690 }
691 break;
692 default: 439 default:
693 GNUNET_break (0); 440 GNUNET_break (0);
694 force_reconnect (handle); 441 force_reconnect (handle);
@@ -710,7 +457,7 @@ GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
710{ 457{
711 struct GNUNET_GNS_Handle *handle; 458 struct GNUNET_GNS_Handle *handle;
712 459
713 handle = GNUNET_malloc (sizeof (struct GNUNET_GNS_Handle)); 460 handle = GNUNET_new (struct GNUNET_GNS_Handle);
714 handle->cfg = cfg; 461 handle->cfg = cfg;
715 reconnect (handle); 462 reconnect (handle);
716 return handle; 463 return handle;
@@ -732,8 +479,6 @@ GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
732 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK; 479 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
733 } 480 }
734 GNUNET_assert (NULL == handle->lookup_head); 481 GNUNET_assert (NULL == handle->lookup_head);
735 GNUNET_assert (NULL == handle->shorten_head);
736 GNUNET_assert (NULL == handle->get_auth_head);
737 GNUNET_free (handle); 482 GNUNET_free (handle);
738} 483}
739 484
@@ -761,50 +506,6 @@ GNUNET_GNS_cancel_lookup_request (struct GNUNET_GNS_LookupRequest *lr)
761 506
762 507
763/** 508/**
764 * Cancel pending shorten request
765 *
766 * @param sr the lookup request to cancel
767 */
768void
769GNUNET_GNS_cancel_shorten_request (struct GNUNET_GNS_ShortenRequest *sr)
770{
771 struct PendingMessage *p = (struct PendingMessage*) &sr[1];
772
773 GNUNET_assert (NULL != sr->gns_handle);
774 if (GNUNET_NO == p->transmitted)
775 GNUNET_CONTAINER_DLL_remove (sr->gns_handle->pending_head,
776 sr->gns_handle->pending_tail,
777 p);
778 GNUNET_CONTAINER_DLL_remove (sr->gns_handle->shorten_head,
779 sr->gns_handle->shorten_tail,
780 sr);
781 GNUNET_free (sr);
782}
783
784
785/**
786 * Cancel pending get auth request
787 *
788 * @param gar the lookup request to cancel
789 */
790void
791GNUNET_GNS_cancel_get_auth_request (struct GNUNET_GNS_GetAuthRequest *gar)
792{
793 struct PendingMessage *p = (struct PendingMessage*) &gar[1];
794
795 GNUNET_assert (NULL != gar->gns_handle);
796 if (GNUNET_NO == p->transmitted)
797 GNUNET_CONTAINER_DLL_remove (gar->gns_handle->pending_head,
798 gar->gns_handle->pending_tail,
799 p);
800 GNUNET_CONTAINER_DLL_remove (gar->gns_handle->get_auth_head,
801 gar->gns_handle->get_auth_tail,
802 gar);
803 GNUNET_free (gar);
804}
805
806
807/**
808 * Perform an asynchronous Lookup operation on the GNS. 509 * Perform an asynchronous Lookup operation on the GNS.
809 * 510 *
810 * @param handle handle to the GNS service 511 * @param handle handle to the GNS service
@@ -885,192 +586,4 @@ GNUNET_GNS_lookup_zone (struct GNUNET_GNS_Handle *handle,
885} 586}
886 587
887 588
888/**
889 * Perform an asynchronous Lookup operation on the GNS.
890 *
891 * @param handle handle to the GNS service
892 * @param name the name to look up
893 * @param type the record type to look up
894 * @param only_cached GNUNET_YES to only check locally not DHT for performance
895 * @param shorten_key the private key of the shorten zone (can be NULL)
896 * @param proc processor to call on result
897 * @param proc_cls closure for processor
898 * @return handle to the lookup request
899 */
900struct GNUNET_GNS_LookupRequest *
901GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
902 const char *name,
903 int type,
904 int only_cached,
905 struct GNUNET_CRYPTO_EccPrivateKey *shorten_key,
906 GNUNET_GNS_LookupResultProcessor proc,
907 void *proc_cls)
908{
909 return GNUNET_GNS_lookup_zone (handle, name,
910 NULL,
911 type, only_cached,
912 shorten_key,
913 proc, proc_cls);
914}
915
916
917/**
918 * Perform a name shortening operation on the GNS.
919 *
920 * @param handle handle to the GNS service
921 * @param name the name to look up
922 * @param private_zone the public zone of the private zone
923 * @param shorten_zone the public zone of the shorten zone
924 * @param zone the zone to start the resolution in
925 * @param proc function to call on result
926 * @param proc_cls closure for processor
927 * @return handle to the operation
928 */
929struct GNUNET_GNS_ShortenRequest*
930GNUNET_GNS_shorten_zone (struct GNUNET_GNS_Handle *handle,
931 const char *name,
932 struct GNUNET_CRYPTO_ShortHashCode *private_zone,
933 struct GNUNET_CRYPTO_ShortHashCode *shorten_zone,
934 struct GNUNET_CRYPTO_ShortHashCode *zone,
935 GNUNET_GNS_ShortenResultProcessor proc,
936 void *proc_cls)
937{
938 /* IPC to shorten gns names, return shorten_handle */
939 struct GNUNET_GNS_ClientShortenMessage *shorten_msg;
940 struct GNUNET_GNS_ShortenRequest *sr;
941 size_t msize;
942 struct PendingMessage *pending;
943
944 if (NULL == name)
945 {
946 GNUNET_break (0);
947 return NULL;
948 }
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to shorten `%s' in GNS\n", name);
950 msize = sizeof (struct GNUNET_GNS_ClientShortenMessage) + strlen (name) + 1;
951 if (msize > UINT16_MAX)
952 {
953 GNUNET_break (0);
954 return NULL;
955 }
956 sr = GNUNET_malloc (sizeof (struct GNUNET_GNS_ShortenRequest) +
957 sizeof (struct PendingMessage) + msize);
958 sr->gns_handle = handle;
959 sr->shorten_proc = proc;
960 sr->proc_cls = proc_cls;
961 sr->r_id = handle->r_id_gen++;
962 GNUNET_CONTAINER_DLL_insert_tail (handle->shorten_head,
963 handle->shorten_tail, sr);
964 pending = (struct PendingMessage *)&sr[1];
965 pending->size = msize;
966 pending->r_id = sr->r_id;
967
968 shorten_msg = (struct GNUNET_GNS_ClientShortenMessage *) &pending[1];
969 shorten_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_SHORTEN);
970 shorten_msg->header.size = htons ((uint16_t) msize);
971 shorten_msg->id = htonl (sr->r_id);
972 shorten_msg->private_zone = *private_zone;
973 shorten_msg->shorten_zone = *shorten_zone;
974 if (NULL != zone)
975 {
976 shorten_msg->use_default_zone = htonl (GNUNET_NO);
977 memcpy (&shorten_msg->zone, zone,
978 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
979 }
980 else
981 {
982 shorten_msg->use_default_zone = htonl (GNUNET_YES);
983 memset (&shorten_msg->zone, 0, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
984 }
985 memcpy (&shorten_msg[1], name, strlen (name) + 1);
986 GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head, handle->pending_tail,
987 pending);
988 process_pending_messages (handle);
989 return sr;
990}
991
992
993/**
994 * Perform a name shortening operation on the GNS.
995 *
996 * @param handle handle to the GNS service
997 * @param name the name to look up
998 * @param private_zone the public zone of the private zone
999 * @param shorten_zone the public zone of the shorten zone
1000 * @param proc function to call on result
1001 * @param proc_cls closure for processor
1002 * @return handle to the operation
1003 */
1004struct GNUNET_GNS_ShortenRequest*
1005GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle,
1006 const char *name,
1007 struct GNUNET_CRYPTO_ShortHashCode *private_zone,
1008 struct GNUNET_CRYPTO_ShortHashCode *shorten_zone,
1009 GNUNET_GNS_ShortenResultProcessor proc,
1010 void *proc_cls)
1011{
1012 return GNUNET_GNS_shorten_zone (handle, name,
1013 private_zone, shorten_zone,
1014 NULL, proc, proc_cls);
1015}
1016
1017
1018/**
1019 * Perform an authority lookup for a given name.
1020 *
1021 * @param handle handle to the GNS service
1022 * @param name the name to look up authority for
1023 * @param proc function to call on result
1024 * @param proc_cls closure for processor
1025 * @return handle to the operation
1026 */
1027struct GNUNET_GNS_GetAuthRequest*
1028GNUNET_GNS_get_authority (struct GNUNET_GNS_Handle *handle,
1029 const char *name,
1030 GNUNET_GNS_GetAuthResultProcessor proc,
1031 void *proc_cls)
1032{
1033 struct GNUNET_GNS_ClientGetAuthMessage *get_auth_msg;
1034 struct GNUNET_GNS_GetAuthRequest *gar;
1035 size_t msize;
1036 struct PendingMessage *pending;
1037
1038 if (NULL == name)
1039 {
1040 GNUNET_break (0);
1041 return NULL;
1042 }
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1044 "Trying to look up authority for %s in GNS\n", name);
1045 msize = sizeof (struct GNUNET_GNS_ClientGetAuthMessage) + strlen (name) + 1;
1046 if (msize > UINT16_MAX)
1047 {
1048 GNUNET_break (0);
1049 return NULL;
1050 }
1051 gar = GNUNET_malloc (sizeof (struct GNUNET_GNS_GetAuthRequest) +
1052 sizeof (struct PendingMessage) + msize);
1053 gar->gns_handle = handle;
1054 gar->auth_proc = proc;
1055 gar->proc_cls = proc_cls;
1056 gar->r_id = handle->r_id_gen++;
1057 GNUNET_CONTAINER_DLL_insert_tail (handle->get_auth_head,
1058 handle->get_auth_tail, gar);
1059
1060 pending = (struct PendingMessage *) &gar[1];
1061 pending->size = msize;
1062 pending->r_id = gar->r_id;
1063 get_auth_msg = (struct GNUNET_GNS_ClientGetAuthMessage *) &pending[1];
1064 get_auth_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_GET_AUTH);
1065 get_auth_msg->header.size = htons (msize);
1066 get_auth_msg->id = htonl (gar->r_id);
1067 memcpy (&get_auth_msg[1], name, strlen (name) + 1);
1068 GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head,
1069 handle->pending_tail,
1070 pending);
1071 process_pending_messages (handle);
1072 return gar;
1073}
1074
1075
1076/* end of gns_api.c */ 589/* end of gns_api.c */