diff options
Diffstat (limited to 'src/core/core_api_iterate_peers.c')
-rw-r--r-- | src/core/core_api_iterate_peers.c | 135 |
1 files changed, 70 insertions, 65 deletions
diff --git a/src/core/core_api_iterate_peers.c b/src/core/core_api_iterate_peers.c index 967580f83..1bad9a768 100644 --- a/src/core/core_api_iterate_peers.c +++ b/src/core/core_api_iterate_peers.c | |||
@@ -66,8 +66,7 @@ struct GNUNET_CORE_RequestContext | |||
66 | * @param msg NULL on error or last entry | 66 | * @param msg NULL on error or last entry |
67 | */ | 67 | */ |
68 | static void | 68 | static void |
69 | receive_info (void *cls, | 69 | receive_info (void *cls, const struct GNUNET_MessageHeader *msg) |
70 | const struct GNUNET_MessageHeader *msg) | ||
71 | { | 70 | { |
72 | struct GNUNET_CORE_RequestContext *request_context = cls; | 71 | struct GNUNET_CORE_RequestContext *request_context = cls; |
73 | const struct ConnectNotifyMessage *connect_message; | 72 | const struct ConnectNotifyMessage *connect_message; |
@@ -75,53 +74,51 @@ receive_info (void *cls, | |||
75 | uint16_t msize; | 74 | uint16_t msize; |
76 | 75 | ||
77 | /* Handle last message or error case, disconnect and clean up */ | 76 | /* Handle last message or error case, disconnect and clean up */ |
78 | if ( (msg == NULL) || | 77 | if ((msg == NULL) || |
79 | ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END) && | 78 | ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS_END) && |
80 | (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader))) ) | 79 | (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader)))) |
81 | { | 80 | { |
82 | if (request_context->peer_cb != NULL) | 81 | if (request_context->peer_cb != NULL) |
83 | request_context->peer_cb (request_context->cb_cls, | 82 | request_context->peer_cb (request_context->cb_cls, NULL, NULL); |
84 | NULL, NULL); | 83 | GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO); |
85 | GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO); | 84 | GNUNET_free (request_context); |
86 | GNUNET_free (request_context); | 85 | return; |
87 | return; | 86 | } |
88 | } | ||
89 | 87 | ||
90 | msize = ntohs (msg->size); | 88 | msize = ntohs (msg->size); |
91 | /* Handle incorrect message type or size, disconnect and clean up */ | 89 | /* Handle incorrect message type or size, disconnect and clean up */ |
92 | if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) || | 90 | if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT) || |
93 | (msize < sizeof (struct ConnectNotifyMessage)) ) | 91 | (msize < sizeof (struct ConnectNotifyMessage))) |
94 | { | 92 | { |
95 | GNUNET_break (0); | 93 | GNUNET_break (0); |
96 | if (request_context->peer_cb != NULL) | 94 | if (request_context->peer_cb != NULL) |
97 | request_context->peer_cb (request_context->cb_cls, | 95 | request_context->peer_cb (request_context->cb_cls, NULL, NULL); |
98 | NULL, NULL); | 96 | GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO); |
99 | GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO); | 97 | GNUNET_free (request_context); |
100 | GNUNET_free (request_context); | 98 | return; |
101 | return; | 99 | } |
102 | } | ||
103 | connect_message = (const struct ConnectNotifyMessage *) msg; | 100 | connect_message = (const struct ConnectNotifyMessage *) msg; |
104 | ats_count = ntohl (connect_message->ats_count); | 101 | ats_count = ntohl (connect_message->ats_count); |
105 | if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) || | 102 | if ((msize != |
106 | (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&connect_message->ats)[ats_count].type)) ) | 103 | sizeof (struct ConnectNotifyMessage) + |
107 | { | 104 | ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) || |
108 | GNUNET_break (0); | 105 | (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != |
109 | if (request_context->peer_cb != NULL) | 106 | ntohl ((&connect_message->ats)[ats_count].type))) |
110 | request_context->peer_cb (request_context->cb_cls, | 107 | { |
111 | NULL, NULL); | 108 | GNUNET_break (0); |
112 | GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO); | 109 | if (request_context->peer_cb != NULL) |
113 | GNUNET_free (request_context); | 110 | request_context->peer_cb (request_context->cb_cls, NULL, NULL); |
114 | return; | 111 | GNUNET_CLIENT_disconnect (request_context->client, GNUNET_NO); |
115 | } | 112 | GNUNET_free (request_context); |
113 | return; | ||
114 | } | ||
116 | /* Normal case */ | 115 | /* Normal case */ |
117 | if (request_context->peer_cb != NULL) | 116 | if (request_context->peer_cb != NULL) |
118 | request_context->peer_cb (request_context->cb_cls, | 117 | request_context->peer_cb (request_context->cb_cls, |
119 | &connect_message->peer, | 118 | &connect_message->peer, &connect_message->ats); |
120 | &connect_message->ats); | 119 | GNUNET_CLIENT_receive (request_context->client, |
121 | GNUNET_CLIENT_receive(request_context->client, | 120 | &receive_info, |
122 | &receive_info, | 121 | request_context, GNUNET_TIME_UNIT_FOREVER_REL); |
123 | request_context, | ||
124 | GNUNET_TIME_UNIT_FOREVER_REL); | ||
125 | } | 122 | } |
126 | 123 | ||
127 | /** | 124 | /** |
@@ -136,28 +133,29 @@ receive_info (void *cls, | |||
136 | * @return number of bytes written to buf | 133 | * @return number of bytes written to buf |
137 | */ | 134 | */ |
138 | static size_t | 135 | static size_t |
139 | transmit_request(void *cls, | 136 | transmit_request (void *cls, size_t size, void *buf) |
140 | size_t size, void *buf) | ||
141 | { | 137 | { |
142 | struct GNUNET_MessageHeader *msg; | 138 | struct GNUNET_MessageHeader *msg; |
143 | struct GNUNET_PeerIdentity *peer = cls; | 139 | struct GNUNET_PeerIdentity *peer = cls; |
144 | int msize; | 140 | int msize; |
145 | 141 | ||
146 | if (peer == NULL) | 142 | if (peer == NULL) |
147 | msize = sizeof(struct GNUNET_MessageHeader); | 143 | msize = sizeof (struct GNUNET_MessageHeader); |
148 | else | 144 | else |
149 | msize = sizeof(struct GNUNET_MessageHeader) + sizeof(struct GNUNET_PeerIdentity); | 145 | msize = |
146 | sizeof (struct GNUNET_MessageHeader) + | ||
147 | sizeof (struct GNUNET_PeerIdentity); | ||
150 | 148 | ||
151 | if ((size < msize) || (buf == NULL)) | 149 | if ((size < msize) || (buf == NULL)) |
152 | return 0; | 150 | return 0; |
153 | 151 | ||
154 | msg = (struct GNUNET_MessageHeader *)buf; | 152 | msg = (struct GNUNET_MessageHeader *) buf; |
155 | msg->size = htons (msize); | 153 | msg->size = htons (msize); |
156 | if (peer != NULL) | 154 | if (peer != NULL) |
157 | { | 155 | { |
158 | msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED); | 156 | msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_PEER_CONNECTED); |
159 | memcpy(&msg[1], peer, sizeof(struct GNUNET_PeerIdentity)); | 157 | memcpy (&msg[1], peer, sizeof (struct GNUNET_PeerIdentity)); |
160 | } | 158 | } |
161 | else | 159 | else |
162 | msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS); | 160 | msg->type = htons (GNUNET_MESSAGE_TYPE_CORE_ITERATE_PEERS); |
163 | 161 | ||
@@ -189,21 +187,26 @@ GNUNET_CORE_is_peer_connected (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
189 | client = GNUNET_CLIENT_connect ("core", cfg); | 187 | client = GNUNET_CLIENT_connect ("core", cfg); |
190 | if (client == NULL) | 188 | if (client == NULL) |
191 | return GNUNET_SYSERR; | 189 | return GNUNET_SYSERR; |
192 | GNUNET_assert(peer != NULL); | 190 | GNUNET_assert (peer != NULL); |
193 | request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext)); | 191 | request_context = GNUNET_malloc (sizeof (struct GNUNET_CORE_RequestContext)); |
194 | request_context->client = client; | 192 | request_context->client = client; |
195 | request_context->peer_cb = peer_cb; | 193 | request_context->peer_cb = peer_cb; |
196 | request_context->cb_cls = cb_cls; | 194 | request_context->cb_cls = cb_cls; |
197 | request_context->peer = peer; | 195 | request_context->peer = peer; |
198 | 196 | ||
199 | request_context->th = GNUNET_CLIENT_notify_transmit_ready(client, | 197 | request_context->th = GNUNET_CLIENT_notify_transmit_ready (client, |
200 | sizeof(struct GNUNET_MessageHeader) + sizeof(struct GNUNET_PeerIdentity), | 198 | sizeof (struct |
201 | GNUNET_TIME_relative_get_forever(), | 199 | GNUNET_MessageHeader) |
202 | GNUNET_YES, | 200 | + |
203 | &transmit_request, | 201 | sizeof (struct |
204 | peer); | 202 | GNUNET_PeerIdentity), |
205 | GNUNET_assert(request_context->th != NULL); | 203 | GNUNET_TIME_relative_get_forever |
206 | GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever()); | 204 | (), GNUNET_YES, |
205 | &transmit_request, | ||
206 | peer); | ||
207 | GNUNET_assert (request_context->th != NULL); | ||
208 | GNUNET_CLIENT_receive (client, &receive_info, request_context, | ||
209 | GNUNET_TIME_relative_get_forever ()); | ||
207 | return GNUNET_OK; | 210 | return GNUNET_OK; |
208 | } | 211 | } |
209 | 212 | ||
@@ -235,14 +238,16 @@ GNUNET_CORE_iterate_peers (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
235 | request_context->peer_cb = peer_cb; | 238 | request_context->peer_cb = peer_cb; |
236 | request_context->cb_cls = cb_cls; | 239 | request_context->cb_cls = cb_cls; |
237 | 240 | ||
238 | request_context->th = GNUNET_CLIENT_notify_transmit_ready(client, | 241 | request_context->th = GNUNET_CLIENT_notify_transmit_ready (client, |
239 | sizeof(struct GNUNET_MessageHeader), | 242 | sizeof (struct |
240 | GNUNET_TIME_relative_get_forever(), | 243 | GNUNET_MessageHeader), |
241 | GNUNET_YES, | 244 | GNUNET_TIME_relative_get_forever |
242 | &transmit_request, | 245 | (), GNUNET_YES, |
243 | NULL); | 246 | &transmit_request, |
247 | NULL); | ||
244 | 248 | ||
245 | GNUNET_CLIENT_receive(client, &receive_info, request_context, GNUNET_TIME_relative_get_forever()); | 249 | GNUNET_CLIENT_receive (client, &receive_info, request_context, |
250 | GNUNET_TIME_relative_get_forever ()); | ||
246 | return GNUNET_OK; | 251 | return GNUNET_OK; |
247 | } | 252 | } |
248 | 253 | ||