diff options
author | Nathan S. Evans <evans@in.tum.de> | 2010-08-03 08:24:45 +0000 |
---|---|---|
committer | Nathan S. Evans <evans@in.tum.de> | 2010-08-03 08:24:45 +0000 |
commit | b38f97fd35c05c1279da542a99e4577b2a019096 (patch) | |
tree | 0b63b091c7b27d7bd2e40708def5237cea2d6b18 /src | |
parent | 7d86288c8819afc624d08cb88e3ed77ab05f541d (diff) | |
download | gnunet-b38f97fd35c05c1279da542a99e4577b2a019096.tar.gz gnunet-b38f97fd35c05c1279da542a99e4577b2a019096.zip |
core changes for topology iteration (get current connections)
Diffstat (limited to 'src')
-rw-r--r-- | src/core/Makefile.am | 3 | ||||
-rw-r--r-- | src/core/core.h | 2 | ||||
-rw-r--r-- | src/core/core_api_iterate_peers.c | 171 | ||||
-rw-r--r-- | src/core/gnunet-service-core.c | 48 |
4 files changed, 221 insertions, 3 deletions
diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 83a8d1d35..ded3d476c 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am | |||
@@ -16,7 +16,8 @@ lib_LTLIBRARIES = \ | |||
16 | libgnunetcore_la_SOURCES = \ | 16 | libgnunetcore_la_SOURCES = \ |
17 | core_api.c core.h \ | 17 | core_api.c core.h \ |
18 | core_api_peer_get_info.c \ | 18 | core_api_peer_get_info.c \ |
19 | core_api_peer_request.c | 19 | core_api_peer_request.c \ |
20 | core_api_iterate_peers.c | ||
20 | libgnunetcore_la_LIBADD = \ | 21 | libgnunetcore_la_LIBADD = \ |
21 | $(top_builddir)/src/util/libgnunetutil.la \ | 22 | $(top_builddir)/src/util/libgnunetutil.la \ |
22 | $(GN_LIBINTL) $(XLIB) | 23 | $(GN_LIBINTL) $(XLIB) |
diff --git a/src/core/core.h b/src/core/core.h index cd3b5d805..021aa4184 100644 --- a/src/core/core.h +++ b/src/core/core.h | |||
@@ -30,7 +30,7 @@ | |||
30 | /** | 30 | /** |
31 | * General core debugging. | 31 | * General core debugging. |
32 | */ | 32 | */ |
33 | #define DEBUG_CORE GNUNET_YES | 33 | #define DEBUG_CORE GNUNET_NO |
34 | 34 | ||
35 | /** | 35 | /** |
36 | * Debugging interaction core-clients. | 36 | * Debugging interaction core-clients. |
diff --git a/src/core/core_api_iterate_peers.c b/src/core/core_api_iterate_peers.c new file mode 100644 index 000000000..08cb9797c --- /dev/null +++ b/src/core/core_api_iterate_peers.c | |||
@@ -0,0 +1,171 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2009, 2010 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file core/core_api_iterate_peers.c | ||
23 | * @brief implementation of the peer_iterate function | ||
24 | * @author Christian Grothoff | ||
25 | * @author Nathan Evans | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_core_service.h" | ||
29 | #include "core.h" | ||
30 | |||
31 | |||
32 | struct GNUNET_CORE_RequestContext | ||
33 | { | ||
34 | |||
35 | /** | ||
36 | * Our connection to the service. | ||
37 | */ | ||
38 | struct GNUNET_CLIENT_Connection *client; | ||
39 | |||
40 | /** | ||
41 | * Handle for transmitting a request. | ||
42 | */ | ||
43 | struct GNUNET_CLIENT_TransmitHandle *th; | ||
44 | |||
45 | /** | ||
46 | * Function called with the peer. | ||
47 | */ | ||
48 | GNUNET_CORE_ConnectEventHandler peer_cb; | ||
49 | |||
50 | /** | ||
51 | * Closure for peer_cb. | ||
52 | */ | ||
53 | void *cb_cls; | ||
54 | |||
55 | }; | ||
56 | |||
57 | |||
58 | /** | ||
59 | * Receive reply from core service with information about a peer. | ||
60 | * | ||
61 | * @param cls our 'struct GNUNET_CORE_RequestContext *' | ||
62 | * @param msg NULL on error or last entry | ||
63 | */ | ||
64 | static void | ||
65 | receive_info (void *cls, | ||
66 | const struct GNUNET_MessageHeader *msg) | ||
67 | { | ||
68 | struct GNUNET_CORE_RequestContext *request_context = cls; | ||
69 | const struct ConnectNotifyMessage *connect_message; | ||
70 | |||
71 | |||
72 | /* Handle last message or error case, disconnect and clean up */ | ||
73 | if ( (msg == NULL) || | ||
74 | ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) && | ||
75 | (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))) ) | ||
76 | { | ||
77 | if (request_context->peer_cb != NULL) | ||
78 | request_context->peer_cb (request_context->cb_cls, | ||
79 | NULL, GNUNET_TIME_relative_get_zero(), 0); | ||
80 | GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO); | ||
81 | GNUNET_free (request_context); | ||
82 | return; | ||
83 | } | ||
84 | |||
85 | /* Handle incorrect message type or size, disconnect and clean up */ | ||
86 | if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) || | ||
87 | (ntohs (msg->size) != sizeof (struct ConnectNotifyMessage)) ) | ||
88 | { | ||
89 | GNUNET_break (0); | ||
90 | if (request_context->peer_cb != NULL) | ||
91 | request_context->peer_cb (request_context->cb_cls, | ||
92 | NULL, GNUNET_TIME_relative_get_zero(), 0); | ||
93 | GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO); | ||
94 | GNUNET_free (request_context); | ||
95 | return; | ||
96 | } | ||
97 | |||
98 | /* Normal case */ | ||
99 | connect_message = (const struct ConnectNotifyMessage *) msg; | ||
100 | if (request_context->peer_cb != NULL) | ||
101 | request_context->peer_cb (request_context->cb_cls, | ||
102 | &connect_message->peer, | ||
103 | GNUNET_TIME_relative_ntoh(connect_message->latency), | ||
104 | ntohl (connect_message->distance)); | ||
105 | |||
106 | GNUNET_CLIENT_receive(request_context->client, &receive_info, request_context, GNUNET_TIME_relative_get_forever()); | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * Function called to notify a client about the socket | ||
111 | * begin ready to queue more data. "buf" will be | ||
112 | * NULL and "size" zero if the socket was closed for | ||
113 | * writing in the meantime. | ||
114 | * | ||
115 | * @param cls closure | ||
116 | * @param size number of bytes available in buf | ||
117 | * @param buf where the callee should write the message | ||
118 | * @return number of bytes written to buf | ||
119 | */ | ||
120 | static size_t | ||
121 | transmit_request(void *cls, | ||
122 | size_t size, void *buf) | ||
123 | { | ||
124 | struct GNUNET_MessageHeader *msg; | ||
125 | if ((size < sizeof(struct GNUNET_MessageHeader)) || (buf == NULL)) | ||
126 | return 0; | ||
127 | |||
128 | msg = (struct GNUNET_MessageHeader *)buf; | ||
129 | msg->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
130 | msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS); | ||
131 | return sizeof(struct GNUNET_MessageHeader); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Obtain statistics and/or change preferences for the given peer. | ||
136 | * | ||
137 | * @param sched scheduler to use | ||
138 | * @param cfg configuration to use | ||
139 | * @param peer_cb function to call with the peer information | ||
140 | * @param cb_cls closure for peer_cb | ||
141 | * @return GNUNET_OK if iterating, GNUNET_SYSERR on error | ||
142 | */ | ||
143 | int | ||
144 | GNUNET_CORE_iterate_peers (struct GNUNET_SCHEDULER_Handle *sched, | ||
145 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
146 | GNUNET_CORE_ConnectEventHandler peer_cb, | ||
147 | void *cb_cls) | ||
148 | { | ||
149 | struct GNUNET_CORE_RequestContext *request_context; | ||
150 | struct GNUNET_CLIENT_Connection *client; | ||
151 | |||
152 | client = GNUNET_CLIENT_connect (sched, "core", cfg); | ||
153 | if (client == NULL) | ||
154 | return GNUNET_SYSERR; | ||
155 | request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext)); | ||
156 | request_context->client = client; | ||
157 | request_context->peer_cb = peer_cb; | ||
158 | request_context->cb_cls = cb_cls; | ||
159 | |||
160 | /*GNUNET_assert (GNUNET_OK == GNUNET_CLIENT_transmit_and_get_response (client, | ||
161 | &request_message, | ||
162 | GNUNET_TIME_relative_get_forever(), | ||
163 | GNUNET_YES, | ||
164 | &receive_info, | ||
165 | request_context));*/ | ||
166 | request_context->th = GNUNET_CLIENT_notify_transmit_ready(client, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_relative_get_forever(), GNUNET_YES, &transmit_request, NULL); | ||
167 | GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever()); | ||
168 | return GNUNET_OK; | ||
169 | } | ||
170 | |||
171 | /* end of core_api_iterate_peers.c */ | ||
diff --git a/src/core/gnunet-service-core.c b/src/core/gnunet-service-core.c index 6921d758f..0fcc13015 100644 --- a/src/core/gnunet-service-core.c +++ b/src/core/gnunet-service-core.c | |||
@@ -865,6 +865,48 @@ handle_peer_status_change (struct Neighbour *n) | |||
865 | GNUNET_NO); | 865 | GNUNET_NO); |
866 | } | 866 | } |
867 | 867 | ||
868 | /** | ||
869 | * Handle CORE_ITERATE_PEERS request. | ||
870 | */ | ||
871 | static void | ||
872 | handle_client_iterate_peers (void *cls, | ||
873 | struct GNUNET_SERVER_Client *client, | ||
874 | const struct GNUNET_MessageHeader *message) | ||
875 | { | ||
876 | struct Neighbour *n; | ||
877 | struct ConnectNotifyMessage cnm; | ||
878 | struct GNUNET_MessageHeader done_msg; | ||
879 | struct GNUNET_SERVER_TransmitContext *tc; | ||
880 | |||
881 | /* notify new client about existing neighbours */ | ||
882 | cnm.header.size = htons (sizeof (struct ConnectNotifyMessage)); | ||
883 | cnm.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT); | ||
884 | done_msg.size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
885 | done_msg.type = htons (GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT); | ||
886 | tc = GNUNET_SERVER_transmit_context_create (client); | ||
887 | n = neighbours; | ||
888 | while (n != NULL) | ||
889 | { | ||
890 | if (n->status == PEER_STATE_KEY_CONFIRMED) | ||
891 | { | ||
892 | #if DEBUG_CORE_CLIENT | ||
893 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
894 | "Sending `%s' message to client.\n", "NOTIFY_CONNECT"); | ||
895 | #endif | ||
896 | cnm.distance = htonl (n->last_distance); | ||
897 | cnm.latency = GNUNET_TIME_relative_hton (n->last_latency); | ||
898 | cnm.peer = n->peer; | ||
899 | GNUNET_SERVER_transmit_context_append_message (tc, &cnm.header); | ||
900 | /*send_to_client (c, &cnm.header, GNUNET_NO);*/ | ||
901 | } | ||
902 | n = n->next; | ||
903 | } | ||
904 | |||
905 | GNUNET_SERVER_transmit_context_append_message (tc, &done_msg); | ||
906 | GNUNET_SERVER_transmit_context_run (tc, | ||
907 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
908 | } | ||
909 | |||
868 | 910 | ||
869 | /** | 911 | /** |
870 | * Handle CORE_INIT request. | 912 | * Handle CORE_INIT request. |
@@ -2201,6 +2243,7 @@ handle_client_send (void *cls, | |||
2201 | if (msize < | 2243 | if (msize < |
2202 | sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader)) | 2244 | sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader)) |
2203 | { | 2245 | { |
2246 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "about to assert fail, msize is %d, should be less than %d\n", msize, sizeof (struct SendMessage) + sizeof (struct GNUNET_MessageHeader)); | ||
2204 | GNUNET_break (0); | 2247 | GNUNET_break (0); |
2205 | if (client != NULL) | 2248 | if (client != NULL) |
2206 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); | 2249 | GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); |
@@ -2730,7 +2773,7 @@ process_hello_retry_handle_set_key (void *cls, | |||
2730 | } | 2773 | } |
2731 | else | 2774 | else |
2732 | { | 2775 | { |
2733 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 2776 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
2734 | _("Ignoring `%s' message due to lack of public key for peer `%4s' (failed to obtain one).\n"), | 2777 | _("Ignoring `%s' message due to lack of public key for peer `%4s' (failed to obtain one).\n"), |
2735 | "SET_KEY", | 2778 | "SET_KEY", |
2736 | GNUNET_i2s (&n->peer)); | 2779 | GNUNET_i2s (&n->peer)); |
@@ -3815,6 +3858,9 @@ run (void *cls, | |||
3815 | {&handle_client_request_info, NULL, | 3858 | {&handle_client_request_info, NULL, |
3816 | GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO, | 3859 | GNUNET_MESSAGE_TYPE_CORE_REQUEST_INFO, |
3817 | sizeof (struct RequestInfoMessage)}, | 3860 | sizeof (struct RequestInfoMessage)}, |
3861 | {&handle_client_iterate_peers, NULL, | ||
3862 | GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS, | ||
3863 | sizeof (struct GNUNET_MessageHeader)}, | ||
3818 | {&handle_client_send, NULL, | 3864 | {&handle_client_send, NULL, |
3819 | GNUNET_MESSAGE_TYPE_CORE_SEND, 0}, | 3865 | GNUNET_MESSAGE_TYPE_CORE_SEND, 0}, |
3820 | {&handle_client_request_connect, NULL, | 3866 | {&handle_client_request_connect, NULL, |