diff options
author | Ji Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2009-12-10 20:59:46 +0000 |
---|---|---|
committer | Ji Lu <jilu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2009-12-10 20:59:46 +0000 |
commit | cf6288e9f0e48c3f838078b231256183a19ff8bf (patch) | |
tree | f454eb91fd81adfd1e83aa56a8a9b52090779228 /src | |
parent | 447e98232feed479ba91e3511bdd31da8dc1e446 (diff) | |
download | gnunet-cf6288e9f0e48c3f838078b231256183a19ff8bf.tar.gz gnunet-cf6288e9f0e48c3f838078b231256183a19ff8bf.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/transport/gnunet-service-transport.c | 73 | ||||
-rw-r--r-- | src/transport/transport.h | 5 | ||||
-rw-r--r-- | src/transport/transport_api_address_lookup.c | 116 |
3 files changed, 194 insertions, 0 deletions
diff --git a/src/transport/gnunet-service-transport.c b/src/transport/gnunet-service-transport.c index 5c5c64dbc..b93ced56f 100644 --- a/src/transport/gnunet-service-transport.c +++ b/src/transport/gnunet-service-transport.c | |||
@@ -2451,6 +2451,79 @@ handle_try_connect (void *cls, | |||
2451 | GNUNET_SERVER_receive_done (client, GNUNET_OK); | 2451 | GNUNET_SERVER_receive_done (client, GNUNET_OK); |
2452 | } | 2452 | } |
2453 | 2453 | ||
2454 | static void | ||
2455 | transmit_address_to_client (void *cls, const char *address) | ||
2456 | { | ||
2457 | struct GNUNET_SERVER_TransmitContext *tc = cls; | ||
2458 | size_t slen; | ||
2459 | |||
2460 | if (NULL == address) | ||
2461 | slen = 0; | ||
2462 | else | ||
2463 | slen = strlen (address) + 1; | ||
2464 | GNUNET_SERVER_transmit_context_append (tc, address, slen, | ||
2465 | GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); | ||
2466 | if (NULL == address) | ||
2467 | GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); | ||
2468 | } | ||
2469 | |||
2470 | /** | ||
2471 | * Handle AddressLookup-message. | ||
2472 | * | ||
2473 | * @param cls closure (always NULL) | ||
2474 | * @param client identification of the client | ||
2475 | * @param message the actual message | ||
2476 | */ | ||
2477 | static void | ||
2478 | handle_addressLookUp (void *cls, | ||
2479 | struct GNUNET_SERVER_Client *client, | ||
2480 | const struct GNUNET_MessageHeader *message) | ||
2481 | { | ||
2482 | const struct AddressLookupMessage *alum; | ||
2483 | struct TransportPlugin *lsPlugin; | ||
2484 | const char *nameTransport; | ||
2485 | const char *address; | ||
2486 | uint16_t size; | ||
2487 | struct GNUNET_MessageHeader reply; | ||
2488 | struct GNUNET_SERVER_TransmitContext *tc; | ||
2489 | |||
2490 | size = ntohs (message->size); | ||
2491 | if (size < sizeof(struct AddressLookupMessage)) | ||
2492 | { | ||
2493 | GNUNET_break_op (0); | ||
2494 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
2495 | return; | ||
2496 | } | ||
2497 | alum = (const struct AddressLookupMessage *) message; | ||
2498 | uint32_t addressLen = ntohl(alum->addrlen); | ||
2499 | if (size <= sizeof(struct AddressLookupMessage) + addressLen) | ||
2500 | { | ||
2501 | GNUNET_break_op (0); | ||
2502 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
2503 | return; | ||
2504 | } | ||
2505 | address = (const char *)&alum[1]; | ||
2506 | nameTransport = (const char*)&addr[addressLen]; | ||
2507 | if (nameTransport [size - sizeof (struct AddressLookupMessage) - addressLen -1] != '\0') | ||
2508 | { | ||
2509 | GNUNET_break_op (0); | ||
2510 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | ||
2511 | return; | ||
2512 | } | ||
2513 | struct GNUNET_TIME_Absolute timeout= GNUNET_TIME_absolute_ntoh(bbalum->timeout); | ||
2514 | struct GNUNET_TIME_Relative rtimeout = GNUNET_TIME_absolute_get_remaining(timeout); | ||
2515 | lsPlugin = find_transport(nameTransport); | ||
2516 | if (NULL == lsPlugin) | ||
2517 | { | ||
2518 | tc = GNUNET_SERVER_transmit_context_create (client); | ||
2519 | GNUNET_SERVER_transmit_context_append (tc, NULL, 0, GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); | ||
2520 | GNUNET_SERVER_transmit_context_run (tc, rtimeout); | ||
2521 | return; | ||
2522 | } | ||
2523 | tc = GNUNET_SERVER_transmit_context_create (client); | ||
2524 | lsPlugin->api->address_pretty_printer(cls, nameTransport, | ||
2525 | addr, alum->addrlen, GNUNET_YES, rtimeout, &transmit_address_to_client, tc); | ||
2526 | } | ||
2454 | 2527 | ||
2455 | /** | 2528 | /** |
2456 | * List of handlers for the messages understood by this | 2529 | * List of handlers for the messages understood by this |
diff --git a/src/transport/transport.h b/src/transport/transport.h index 5de46b38f..fed4bf162 100644 --- a/src/transport/transport.h +++ b/src/transport/transport.h | |||
@@ -247,6 +247,11 @@ struct AddressLookupMessage | |||
247 | struct GNUNET_MessageHeader header; | 247 | struct GNUNET_MessageHeader header; |
248 | 248 | ||
249 | /** | 249 | /** |
250 | * timeout to give up. | ||
251 | */ | ||
252 | struct GNUNET_TIME_AbosluteNBO tiemout; | ||
253 | |||
254 | /** | ||
250 | * Length of the (binary) address in bytes, in big-endian. | 255 | * Length of the (binary) address in bytes, in big-endian. |
251 | */ | 256 | */ |
252 | uint32_t addrlen GNUNET_PACKED; | 257 | uint32_t addrlen GNUNET_PACKED; |
diff --git a/src/transport/transport_api_address_lookup.c b/src/transport/transport_api_address_lookup.c new file mode 100644 index 000000000..1c0cf7340 --- /dev/null +++ b/src/transport/transport_api_address_lookup.c | |||
@@ -0,0 +1,116 @@ | |||
1 | #include "platform.h" | ||
2 | #include "gnunet_client_lib.h" | ||
3 | #include "gnunet_arm_service.h" | ||
4 | #include "gnunet_hello_lib.h" | ||
5 | #include "gnunet_protocols.h" | ||
6 | #include "gnunet_server_lib.h" | ||
7 | #include "gnunet_time_lib.h" | ||
8 | #include "gnunet_transport_service.h" | ||
9 | #include "transport.h" | ||
10 | |||
11 | // FIXME: document | ||
12 | struct AddressLookUpCB { | ||
13 | GNUNET_TRANSPORT_AddressLookUpCallback cb; | ||
14 | void *cls; | ||
15 | struct GNUNET_TIME_Absolute timeout; | ||
16 | struct GNUNET_CLIENT_Connection *client; | ||
17 | }; | ||
18 | |||
19 | |||
20 | // FIXME: document | ||
21 | static void | ||
22 | address_response_processor(void *cls, const struct | ||
23 | GNUNET_MessageHeader * msg) | ||
24 | { | ||
25 | struct AddressLookUpCB *alucb = cls; | ||
26 | const char *address; | ||
27 | uint16_t size; | ||
28 | |||
29 | if (msg == NULL) | ||
30 | { | ||
31 | /* timeout */ | ||
32 | alucb->cb (alucb->cls, NULL); | ||
33 | GNUNET_CLIENT_disconnect (alucb->client); | ||
34 | GNUNET_free (alucb); | ||
35 | return; | ||
36 | } | ||
37 | size = ntohs(msg->size); | ||
38 | if (size == sizeof(struct GNUNET_MessageHeader)) | ||
39 | { | ||
40 | /* last reply */ | ||
41 | address = NULL; | ||
42 | } | ||
43 | else | ||
44 | { | ||
45 | address = (const char*) &msg[1]; | ||
46 | if (address[size - sizeof(struct GNUNET_MessageHeader) - 1] != '\0') | ||
47 | { | ||
48 | /* invalid reply */ | ||
49 | GNUNET_break_op (0); | ||
50 | alucb->cb (alucb->cls, NULL); | ||
51 | GNUNET_CLIENT_disconnect (alucb->client); | ||
52 | GNUNET_free (alucb); | ||
53 | return; | ||
54 | } | ||
55 | else | ||
56 | { | ||
57 | /* expect more replies */ | ||
58 | GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb, | ||
59 | GNUNET_TIME_absolute_get_remaining (alucb->timeout)); | ||
60 | } | ||
61 | } | ||
62 | alucb->cb (alucb->cls, address); | ||
63 | if (address == NULL) | ||
64 | { | ||
65 | /* done! */ | ||
66 | GNUNET_CLIENT_disconnect (alucb->client); | ||
67 | GNUNET_free (alucb); | ||
68 | } | ||
69 | } | ||
70 | |||
71 | void | ||
72 | GNUNET_TRANSPORT_address_lookup (struct GNUNET_SCHEDULER_Handle *sched, | ||
73 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
74 | const char * address, | ||
75 | size_t addressLen, | ||
76 | const char * nameTrans, | ||
77 | struct GNUNET_TIME_Relative timeout, | ||
78 | GNUNET_TRANSPORT_AddressLookUpCallback aluc, | ||
79 | void *aluc_cls) | ||
80 | { | ||
81 | size_t len = sizeof (struct AddressLookupMessage) + addressLen + strlen (nameTrans) + 1; | ||
82 | struct AddressLookupMessage *msg; | ||
83 | struct GNUNET_TIME_Absolute abs_timeout; | ||
84 | struct AddressLookUpCB *aluCB; | ||
85 | struct GNUNET_CLIENT_Connection *client; | ||
86 | |||
87 | if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
88 | { | ||
89 | GNUNET_break (0); | ||
90 | aluc (aluc_cls, NULL); | ||
91 | return; | ||
92 | } | ||
93 | client = GNUNET_CLIENT_connect (sched, "transport", cfg); | ||
94 | if (client == NULL) | ||
95 | { | ||
96 | aluc (aluc_cls, NULL); | ||
97 | return; | ||
98 | } | ||
99 | abs_timeout = GNUNET_TIME_relative_to_absolute(timeout); | ||
100 | msg = GNUNET_malloc (len); | ||
101 | msg->header->size = htons (len); | ||
102 | msg->header->type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP); | ||
103 | msg->timeout = GNUNET_TIME_absolute_hton(abs_timeout); | ||
104 | msg->addrlen = htonl (addressLen); | ||
105 | char *addrbuf = (char*) &msg[1]; | ||
106 | memcpy (addrbuf, address, addressLen); | ||
107 | char *tbuf = &addrbuf[addressLen]; | ||
108 | memcpy (tbuf, nameTrans, strlen(nameTrans) + 1); | ||
109 | aluCB = GNUNET_malloc (sizeof (struct AddressLookUpCB)); | ||
110 | aluCB->cb = aluc; | ||
111 | aluCB->cb_cls = aluc_cls; | ||
112 | aluCB->timeout = abs_timeout; | ||
113 | aluCB->client = client; | ||
114 | GNUNET_CLIENT_transmit_and_get_response(client, msg->header, timeout, GNUNET_YES, &address_response_processor, aluCB); | ||
115 | GNUNET_free(msg); | ||
116 | } | ||