aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_udp_broadcasting.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/plugin_transport_udp_broadcasting.c')
-rw-r--r--src/transport/plugin_transport_udp_broadcasting.c199
1 files changed, 69 insertions, 130 deletions
diff --git a/src/transport/plugin_transport_udp_broadcasting.c b/src/transport/plugin_transport_udp_broadcasting.c
index 814328f48..767bddff4 100644
--- a/src/transport/plugin_transport_udp_broadcasting.c
+++ b/src/transport/plugin_transport_udp_broadcasting.c
@@ -88,7 +88,7 @@ struct BroadcastAddress
88 88
89 struct Plugin *plugin; 89 struct Plugin *plugin;
90 90
91 void *addr; 91 struct sockaddr *addr;
92 92
93 socklen_t addrlen; 93 socklen_t addrlen;
94 94
@@ -106,24 +106,16 @@ struct BroadcastAddress
106}; 106};
107 107
108 108
109struct Mstv4Context 109/**
110 * Client-specific context for #broadcast_mst_cb().
111 */
112struct MstContext
110{ 113{
111 struct Plugin *plugin; 114 struct Plugin *plugin;
112 115
113 struct IPv4UdpAddress addr; 116 const union UdpAddress *udp_addr;
114
115 /**
116 * ATS network type.
117 */
118 enum GNUNET_ATS_Network_Type ats_address_network_type;
119};
120
121
122struct Mstv6Context
123{
124 struct Plugin *plugin;
125 117
126 struct IPv6UdpAddress addr; 118 size_t udp_addr_len;
127 119
128 /** 120 /**
129 * ATS network type. 121 * ATS network type.
@@ -132,56 +124,21 @@ struct Mstv6Context
132}; 124};
133 125
134 126
127/**
128 * Parse broadcast message received.
129 *
130 * @param cls the `struct Plugin`
131 * @param client the `struct MstContext` with sender address
132 * @param message the message we received
133 * @return #GNUNET_OK (always)
134 */
135static int 135static int
136broadcast_ipv6_mst_cb (void *cls, void *client, 136broadcast_mst_cb (void *cls,
137 const struct GNUNET_MessageHeader *message) 137 void *client,
138{ 138 const struct GNUNET_MessageHeader *message)
139 struct Plugin *plugin = cls;
140 struct Mstv6Context *mc = client;
141 struct GNUNET_HELLO_Address *address;
142 const struct GNUNET_MessageHeader *hello;
143 const struct UDP_Beacon_Message *msg;
144 struct GNUNET_ATS_Information atsi;
145
146 msg = (const struct UDP_Beacon_Message *) message;
147
148 if (GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON !=
149 ntohs (msg->header.type))
150 return GNUNET_OK;
151 LOG (GNUNET_ERROR_TYPE_DEBUG,
152 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
153 ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
154 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr)));
155
156 /* setup ATS */
157 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
158 atsi.value = htonl (mc->ats_address_network_type);
159 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
160
161 hello = (struct GNUNET_MessageHeader *) &msg[1];
162 address = GNUNET_HELLO_address_allocate (&msg->sender, PLUGIN_NAME,
163 (const char *) &mc->addr,
164 sizeof (mc->addr),
165 GNUNET_HELLO_ADDRESS_INFO_NONE);
166 plugin->env->receive (plugin->env->cls, address, NULL, hello);
167 plugin->env->update_address_metrics (plugin->env->cls, address,
168 NULL, &atsi, 1);
169 GNUNET_HELLO_address_free (address);
170 GNUNET_STATISTICS_update (plugin->env->stats,
171 _
172 ("# IPv6 multicast HELLO beacons received via udp"),
173 1, GNUNET_NO);
174 GNUNET_free (mc);
175 return GNUNET_OK;
176}
177
178
179static int
180broadcast_ipv4_mst_cb (void *cls, void *client,
181 const struct GNUNET_MessageHeader *message)
182{ 139{
183 struct Plugin *plugin = cls; 140 struct Plugin *plugin = cls;
184 struct Mstv4Context *mc = client; 141 struct MstContext *mc = client;
185 struct GNUNET_HELLO_Address *address; 142 struct GNUNET_HELLO_Address *address;
186 const struct GNUNET_MessageHeader *hello; 143 const struct GNUNET_MessageHeader *hello;
187 const struct UDP_Beacon_Message *msg; 144 const struct UDP_Beacon_Message *msg;
@@ -194,87 +151,76 @@ broadcast_ipv4_mst_cb (void *cls, void *client,
194 return GNUNET_OK; 151 return GNUNET_OK;
195 LOG (GNUNET_ERROR_TYPE_DEBUG, 152 LOG (GNUNET_ERROR_TYPE_DEBUG,
196 "Received beacon with %u bytes from peer `%s' via address `%s'\n", 153 "Received beacon with %u bytes from peer `%s' via address `%s'\n",
197 ntohs (msg->header.size), GNUNET_i2s (&msg->sender), 154 ntohs (msg->header.size),
198 udp_address_to_string (NULL, &mc->addr, sizeof (mc->addr))); 155 GNUNET_i2s (&msg->sender),
199 156 udp_address_to_string (NULL,
157 mc->udp_addr,
158 mc->udp_addr_len));
200 159
201 /* setup ATS */ 160 /* setup ATS */
202 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE); 161 atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
203 atsi.value = htonl (mc->ats_address_network_type); 162 atsi.value = htonl (mc->ats_address_network_type);
204 GNUNET_break (ntohl(mc->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED); 163 GNUNET_break (ntohl(mc->ats_address_network_type) !=
164 GNUNET_ATS_NET_UNSPECIFIED);
205 165
206 hello = (struct GNUNET_MessageHeader *) &msg[1]; 166 hello = (struct GNUNET_MessageHeader *) &msg[1];
207 address = GNUNET_HELLO_address_allocate (&msg->sender, 167 address = GNUNET_HELLO_address_allocate (&msg->sender,
208 PLUGIN_NAME, 168 PLUGIN_NAME,
209 (const char *) &mc->addr, 169 mc->udp_addr,
210 sizeof (mc->addr), 170 mc->udp_addr_len,
211 GNUNET_HELLO_ADDRESS_INFO_NONE); 171 GNUNET_HELLO_ADDRESS_INFO_NONE);
212 plugin->env->receive (plugin->env->cls, address, NULL, hello); 172 plugin->env->receive (plugin->env->cls,
213 plugin->env->update_address_metrics (plugin->env->cls, address, 173 address,
214 NULL, &atsi, 1); 174 NULL,
175 hello);
176 plugin->env->update_address_metrics (plugin->env->cls,
177 address,
178 NULL,
179 &atsi,
180 1);
215 GNUNET_HELLO_address_free (address); 181 GNUNET_HELLO_address_free (address);
216
217 GNUNET_STATISTICS_update (plugin->env->stats, 182 GNUNET_STATISTICS_update (plugin->env->stats,
218 _("# IPv4 broadcast HELLO beacons received via udp"), 183 _("# Multicast HELLO beacons received via UDP"),
219 1, GNUNET_NO); 184 1, GNUNET_NO);
220 GNUNET_free (mc);
221 return GNUNET_OK; 185 return GNUNET_OK;
222} 186}
223 187
224 188
189/**
190 * We received a broadcast message. Process it and all subsequent
191 * messages in the same packet.
192 *
193 * @param plugin the UDP plugin
194 * @param buf the buffer with the message(s)
195 * @param size number of bytes in @a buf
196 * @param udp_addr address of the sender
197 * @param udp_addr_len number of bytes in @a udp_addr
198 * @param network_type network type of the sender's address
199 */
225void 200void
226udp_broadcast_receive (struct Plugin *plugin, 201udp_broadcast_receive (struct Plugin *plugin,
227 const char *buf, 202 const char *buf,
228 ssize_t size, 203 ssize_t size,
229 const struct sockaddr *addr, 204 const union UdpAddress *udp_addr,
230 size_t addrlen) 205 size_t udp_addr_len,
206 enum GNUNET_ATS_Network_Type network_type)
231{ 207{
232 if (addrlen == sizeof (struct sockaddr_in)) 208 struct MstContext mc;
233 { 209
234 LOG (GNUNET_ERROR_TYPE_DEBUG, 210 mc.udp_addr = udp_addr;
235 "Received IPv4 HELLO beacon broadcast with %i bytes from address %s\n", 211 mc.udp_addr_len = udp_addr_len;
236 size, GNUNET_a2s ((const struct sockaddr *) addr, addrlen)); 212 mc.ats_address_network_type = network_type;
237 struct Mstv4Context *mc; 213 GNUNET_SERVER_mst_receive (plugin->broadcast_mst,
238 214 &mc,
239 mc = GNUNET_new (struct Mstv4Context); 215 buf, size,
240 struct sockaddr_in *av4 = (struct sockaddr_in *) addr; 216 GNUNET_NO,
241 217 GNUNET_NO);
242 mc->addr.ipv4_addr = av4->sin_addr.s_addr;
243 mc->addr.u4_port = av4->sin_port;
244 mc->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls,
245 (const struct sockaddr *) addr,
246 addrlen);
247
248 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
249 if (GNUNET_OK !=
250 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv4_mst, mc, buf, size,
251 GNUNET_NO, GNUNET_NO))
252 GNUNET_free (mc);
253 }
254 if (addrlen == sizeof (struct sockaddr_in6))
255 {
256 LOG (GNUNET_ERROR_TYPE_DEBUG,
257 "Received IPv6 HELLO beacon broadcast with %i bytes from address %s\n",
258 size, GNUNET_a2s ((const struct sockaddr *) &addr, addrlen));
259 struct Mstv6Context *mc;
260
261 mc = GNUNET_new (struct Mstv6Context);
262 struct sockaddr_in6 *av6 = (struct sockaddr_in6 *) addr;
263
264 mc->addr.ipv6_addr = av6->sin6_addr;
265 mc->addr.u6_port = av6->sin6_port;
266 mc->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) addr, addrlen);
267 GNUNET_assert (NULL != plugin->broadcast_ipv4_mst);
268 if (GNUNET_OK !=
269 GNUNET_SERVER_mst_receive (plugin->broadcast_ipv6_mst, mc, buf, size,
270 GNUNET_NO, GNUNET_NO))
271 GNUNET_free (mc);
272 }
273} 218}
274 219
275 220
276static unsigned int 221static unsigned int
277prepare_beacon (struct Plugin *plugin, struct UDP_Beacon_Message *msg) 222prepare_beacon (struct Plugin *plugin,
223 struct UDP_Beacon_Message *msg)
278{ 224{
279 uint16_t hello_size; 225 uint16_t hello_size;
280 uint16_t msg_size; 226 uint16_t msg_size;
@@ -611,10 +557,8 @@ setup_broadcast (struct Plugin *plugin,
611 } 557 }
612 558
613 /* always create tokenizers */ 559 /* always create tokenizers */
614 plugin->broadcast_ipv4_mst = 560 plugin->broadcast_mst =
615 GNUNET_SERVER_mst_create (&broadcast_ipv4_mst_cb, plugin); 561 GNUNET_SERVER_mst_create (&broadcast_mst_cb, plugin);
616 plugin->broadcast_ipv6_mst =
617 GNUNET_SERVER_mst_create (&broadcast_ipv6_mst_cb, plugin);
618 562
619 if (GNUNET_YES != plugin->enable_broadcasting) 563 if (GNUNET_YES != plugin->enable_broadcasting)
620 return; /* We do not send, just receive */ 564 return; /* We do not send, just receive */
@@ -699,15 +643,10 @@ stop_broadcast (struct Plugin *plugin)
699 } 643 }
700 644
701 /* Destroy MSTs */ 645 /* Destroy MSTs */
702 if (NULL != plugin->broadcast_ipv4_mst) 646 if (NULL != plugin->broadcast_mst)
703 {
704 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv4_mst);
705 plugin->broadcast_ipv4_mst = NULL;
706 }
707 if (NULL != plugin->broadcast_ipv6_mst)
708 { 647 {
709 GNUNET_SERVER_mst_destroy (plugin->broadcast_ipv6_mst); 648 GNUNET_SERVER_mst_destroy (plugin->broadcast_mst);
710 plugin->broadcast_ipv6_mst = NULL; 649 plugin->broadcast_mst = NULL;
711 } 650 }
712} 651}
713 652