]> Joshua Wise's Git repositories - netwatch.git/blame - lwip/src/core/udp.c
Be more quiet about this whole transmit and receive business. This time, we actually...
[netwatch.git] / lwip / src / core / udp.c
CommitLineData
6e6d4a8b
JP
1/**
2 * @file
3 * User Datagram Protocol module
4 *
5 */
6
7/*
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
32 *
33 * This file is part of the lwIP TCP/IP stack.
34 *
35 * Author: Adam Dunkels <adam@sics.se>
36 *
37 */
38
39
40/* udp.c
41 *
42 * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828).
43 *
44 */
45
46/* @todo Check the use of '(struct udp_pcb).chksum_len_rx'!
47 */
48
49#include "lwip/opt.h"
50
51#if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
52
53#include "lwip/udp.h"
54#include "lwip/def.h"
55#include "lwip/memp.h"
56#include "lwip/inet.h"
57#include "lwip/inet_chksum.h"
58#include "lwip/ip_addr.h"
59#include "lwip/netif.h"
60#include "lwip/icmp.h"
61#include "lwip/stats.h"
62#include "lwip/snmp.h"
63#include "arch/perf.h"
64#include "lwip/dhcp.h"
65
66#include <string.h>
67
68/* The list of UDP PCBs */
69/* exported in udp.h (was static) */
70struct udp_pcb *udp_pcbs;
71
72/**
73 * Process an incoming UDP datagram.
74 *
75 * Given an incoming UDP datagram (as a chain of pbufs) this function
76 * finds a corresponding UDP PCB and hands over the pbuf to the pcbs
77 * recv function. If no pcb is found or the datagram is incorrect, the
78 * pbuf is freed.
79 *
80 * @param p pbuf to be demultiplexed to a UDP PCB.
81 * @param inp network interface on which the datagram was received.
82 *
83 */
84void
85udp_input(struct pbuf *p, struct netif *inp)
86{
87 struct udp_hdr *udphdr;
88 struct udp_pcb *pcb, *prev;
89 struct udp_pcb *uncon_pcb;
90 struct ip_hdr *iphdr;
91 u16_t src, dest;
92 u8_t local_match;
93
94 PERF_START;
95
96 UDP_STATS_INC(udp.recv);
97
98 iphdr = p->payload;
99
100 /* Check minimum length (IP header + UDP header)
101 * and move payload pointer to UDP header */
102 if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN) || pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4))) {
103 /* drop short packets */
104 LWIP_DEBUGF(UDP_DEBUG,
105 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
106 UDP_STATS_INC(udp.lenerr);
107 UDP_STATS_INC(udp.drop);
108 snmp_inc_udpinerrors();
109 pbuf_free(p);
110 goto end;
111 }
112
113 udphdr = (struct udp_hdr *)p->payload;
114
115 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
116
117 /* convert src and dest ports to host byte order */
118 src = ntohs(udphdr->src);
119 dest = ntohs(udphdr->dest);
120
121 udp_debug_print(udphdr);
122
123 /* print the UDP source and destination */
124 LWIP_DEBUGF(UDP_DEBUG,
125 ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
126 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
127 ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),
128 ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),
129 ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),
130 ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));
131
132#if LWIP_DHCP
133 pcb = NULL;
134 /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by
135 the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */
136 if (dest == DHCP_CLIENT_PORT) {
137 /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */
138 if (src == DHCP_SERVER_PORT) {
139 if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) {
140 /* accept the packe if
141 (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY!
142 - inp->dhcp->pcb->remote == ANY or iphdr->src */
143 if ((ip_addr_isany(&inp->dhcp->pcb->remote_ip) ||
144 ip_addr_cmp(&(inp->dhcp->pcb->remote_ip), &(iphdr->src)))) {
145 pcb = inp->dhcp->pcb;
146 }
147 }
148 }
149 } else
150#endif /* LWIP_DHCP */
151 {
152 prev = NULL;
153 local_match = 0;
154 uncon_pcb = NULL;
155 /* Iterate through the UDP pcb list for a matching pcb.
156 * 'Perfect match' pcbs (connected to the remote port & ip address) are
157 * preferred. If no perfect match is found, the first unconnected pcb that
158 * matches the local port and ip address gets the datagram. */
159 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
160 local_match = 0;
161 /* print the PCB local and remote address */
162 LWIP_DEBUGF(UDP_DEBUG,
163 ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "
164 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
165 ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
166 ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
167 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
168 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
169
170 /* compare PCB local addr+port to UDP destination addr+port */
171 if ((pcb->local_port == dest) &&
172 (ip_addr_isany(&pcb->local_ip) ||
173 ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)) ||
174#if LWIP_IGMP
175 ip_addr_ismulticast(&(iphdr->dest)) ||
176#endif /* LWIP_IGMP */
177 ip_addr_isbroadcast(&(iphdr->dest), inp))) {
178 local_match = 1;
179 if ((uncon_pcb == NULL) &&
180 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
181 /* the first unconnected matching PCB */
182 uncon_pcb = pcb;
183 }
184 }
185 /* compare PCB remote addr+port to UDP source addr+port */
186 if ((local_match != 0) &&
187 (pcb->remote_port == src) &&
188 (ip_addr_isany(&pcb->remote_ip) ||
189 ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)))) {
190 /* the first fully matching PCB */
191 if (prev != NULL) {
192 /* move the pcb to the front of udp_pcbs so that is
193 found faster next time */
194 prev->next = pcb->next;
195 pcb->next = udp_pcbs;
196 udp_pcbs = pcb;
197 } else {
198 UDP_STATS_INC(udp.cachehit);
199 }
200 break;
201 }
202 prev = pcb;
203 }
204 /* no fully matching pcb found? then look for an unconnected pcb */
205 if (pcb == NULL) {
206 pcb = uncon_pcb;
207 }
208 }
209
210 /* Check checksum if this is a match or if it was directed at us. */
211 if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &iphdr->dest)) {
212 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
213#if LWIP_UDPLITE
214 if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
215 /* Do the UDP Lite checksum */
216#if CHECKSUM_CHECK_UDP
217 u16_t chklen = ntohs(udphdr->len);
218 if (chklen < sizeof(struct udp_hdr)) {
219 if (chklen == 0) {
220 /* For UDP-Lite, checksum length of 0 means checksum
221 over the complete packet (See RFC 3828 chap. 3.1) */
222 chklen = p->tot_len;
223 } else {
224 /* At least the UDP-Lite header must be covered by the
225 checksum! (Again, see RFC 3828 chap. 3.1) */
226 UDP_STATS_INC(udp.chkerr);
227 UDP_STATS_INC(udp.drop);
228 snmp_inc_udpinerrors();
229 pbuf_free(p);
230 goto end;
231 }
232 }
233 if (inet_chksum_pseudo_partial(p, (struct ip_addr *)&(iphdr->src),
234 (struct ip_addr *)&(iphdr->dest),
235 IP_PROTO_UDPLITE, p->tot_len, chklen) != 0) {
236 LWIP_DEBUGF(UDP_DEBUG | 2,
237 ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
238 UDP_STATS_INC(udp.chkerr);
239 UDP_STATS_INC(udp.drop);
240 snmp_inc_udpinerrors();
241 pbuf_free(p);
242 goto end;
243 }
244#endif /* CHECKSUM_CHECK_UDP */
245 } else
246#endif /* LWIP_UDPLITE */
247 {
248#if CHECKSUM_CHECK_UDP
249 if (udphdr->chksum != 0) {
250 if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
251 (struct ip_addr *)&(iphdr->dest),
252 IP_PROTO_UDP, p->tot_len) != 0) {
253 LWIP_DEBUGF(UDP_DEBUG | 2,
254 ("udp_input: UDP datagram discarded due to failing checksum\n"));
255 UDP_STATS_INC(udp.chkerr);
256 UDP_STATS_INC(udp.drop);
257 snmp_inc_udpinerrors();
258 pbuf_free(p);
259 goto end;
260 }
261 }
262#endif /* CHECKSUM_CHECK_UDP */
263 }
264 if(pbuf_header(p, -UDP_HLEN)) {
265 /* Can we cope with this failing? Just assert for now */
266 LWIP_ASSERT("pbuf_header failed\n", 0);
267 UDP_STATS_INC(udp.drop);
268 snmp_inc_udpinerrors();
269 pbuf_free(p);
270 goto end;
271 }
272 if (pcb != NULL) {
273 snmp_inc_udpindatagrams();
274 /* callback */
275 if (pcb->recv != NULL) {
276 /* now the recv function is responsible for freeing p */
277 pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src), src);
278 } else {
279 /* no recv function registered? then we have to free the pbuf! */
280 pbuf_free(p);
281 goto end;
282 }
283 } else {
284 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
285
286#if LWIP_ICMP
287 /* No match was found, send ICMP destination port unreachable unless
288 destination address was broadcast/multicast. */
289 if (!ip_addr_isbroadcast(&iphdr->dest, inp) &&
290 !ip_addr_ismulticast(&iphdr->dest)) {
291 /* move payload pointer back to ip header */
292 pbuf_header(p, (IPH_HL(iphdr) * 4) + UDP_HLEN);
293 LWIP_ASSERT("p->payload == iphdr", (p->payload == iphdr));
294 icmp_dest_unreach(p, ICMP_DUR_PORT);
295 }
296#endif /* LWIP_ICMP */
297 UDP_STATS_INC(udp.proterr);
298 UDP_STATS_INC(udp.drop);
299 snmp_inc_udpnoports();
300 pbuf_free(p);
301 }
302 } else {
303 pbuf_free(p);
304 }
305end:
306 PERF_STOP("udp_input");
307}
308
309/**
310 * Send data using UDP.
311 *
312 * @param pcb UDP PCB used to send the data.
313 * @param p chain of pbuf's to be sent.
314 *
315 * The datagram will be sent to the current remote_ip & remote_port
316 * stored in pcb. If the pcb is not bound to a port, it will
317 * automatically be bound to a random port.
318 *
319 * @return lwIP error code.
320 * - ERR_OK. Successful. No error occured.
321 * - ERR_MEM. Out of memory.
322 * - ERR_RTE. Could not find route to destination address.
323 * - More errors could be returned by lower protocol layers.
324 *
325 * @see udp_disconnect() udp_sendto()
326 */
327err_t
328udp_send(struct udp_pcb *pcb, struct pbuf *p)
329{
330 /* send to the packet using remote ip and port stored in the pcb */
331 return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
332}
333
334/**
335 * Send data to a specified address using UDP.
336 *
337 * @param pcb UDP PCB used to send the data.
338 * @param p chain of pbuf's to be sent.
339 * @param dst_ip Destination IP address.
340 * @param dst_port Destination UDP port.
341 *
342 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
343 *
344 * If the PCB already has a remote address association, it will
345 * be restored after the data is sent.
346 *
347 * @return lwIP error code (@see udp_send for possible error codes)
348 *
349 * @see udp_disconnect() udp_send()
350 */
351err_t
352udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
353 struct ip_addr *dst_ip, u16_t dst_port)
354{
355 struct netif *netif;
356
357 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 3, ("udp_send\n"));
358
359 /* find the outgoing network interface for this packet */
360#if LWIP_IGMP
361 netif = ip_route((ip_addr_ismulticast(dst_ip))?(&(pcb->multicast_ip)):(dst_ip));
362#else
363 netif = ip_route(dst_ip);
364#endif /* LWIP_IGMP */
365
366 /* no outgoing network interface could be found? */
367 if (netif == NULL) {
368 LWIP_DEBUGF(UDP_DEBUG | 1, ("udp_send: No route to 0x%"X32_F"\n", dst_ip->addr));
369 UDP_STATS_INC(udp.rterr);
370 return ERR_RTE;
371 }
372 return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
373}
374
375/**
376 * Send data to a specified address using UDP.
377 * The netif used for sending can be specified.
378 *
379 * This function exists mainly for DHCP, to be able to send UDP packets
380 * on a netif that is still down.
381 *
382 * @param pcb UDP PCB used to send the data.
383 * @param p chain of pbuf's to be sent.
384 * @param dst_ip Destination IP address.
385 * @param dst_port Destination UDP port.
386 * @param netif the netif used for sending.
387 *
388 * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
389 *
390 * @return lwIP error code (@see udp_send for possible error codes)
391 *
392 * @see udp_disconnect() udp_send()
393 */
394err_t
395udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
396 struct ip_addr *dst_ip, u16_t dst_port, struct netif *netif)
397{
398 struct udp_hdr *udphdr;
399 struct ip_addr *src_ip;
400 err_t err;
401 struct pbuf *q; /* q will be sent down the stack */
402
403 /* if the PCB is not yet bound to a port, bind it here */
404 if (pcb->local_port == 0) {
405 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding now\n"));
406 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
407 if (err != ERR_OK) {
408 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 2, ("udp_send: forced port bind failed\n"));
409 return err;
410 }
411 }
412
413 /* not enough space to add an UDP header to first pbuf in given p chain? */
414 if (pbuf_header(p, UDP_HLEN)) {
415 /* allocate header in a separate new pbuf */
416 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
417 /* new header pbuf could not be allocated? */
418 if (q == NULL) {
419 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 2, ("udp_send: could not allocate header\n"));
420 return ERR_MEM;
421 }
422 /* chain header q in front of given pbuf p */
423 pbuf_chain(q, p);
424 /* first pbuf q points to header pbuf */
425 LWIP_DEBUGF(UDP_DEBUG,
426 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
427 } else {
428 /* adding space for header within p succeeded */
429 /* first pbuf q equals given pbuf */
430 q = p;
431 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
432 }
433 LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
434 (q->len >= sizeof(struct udp_hdr)));
435 /* q now represents the packet to be sent */
436 udphdr = q->payload;
437 udphdr->src = htons(pcb->local_port);
438 udphdr->dest = htons(dst_port);
439 /* in UDP, 0 checksum means 'no checksum' */
440 udphdr->chksum = 0x0000;
441
442 /* PCB local address is IP_ANY_ADDR? */
443 if (ip_addr_isany(&pcb->local_ip)) {
444 /* use outgoing network interface IP address as source address */
445 src_ip = &(netif->ip_addr);
446 } else {
447 /* check if UDP PCB local IP address is correct
448 * this could be an old address if netif->ip_addr has changed */
449 if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
450 /* local_ip doesn't match, drop the packet */
451 if (q != p) {
452 /* free the header pbuf */
453 pbuf_free(q);
454 q = NULL;
455 /* p is still referenced by the caller, and will live on */
456 }
457 return ERR_VAL;
458 }
459 /* use UDP PCB local IP address as source address */
460 src_ip = &(pcb->local_ip);
461 }
462
463 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
464
465#if LWIP_UDPLITE
466 /* UDP Lite protocol? */
467 if (pcb->flags & UDP_FLAGS_UDPLITE) {
468 u16_t chklen, chklen_hdr;
469 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
470 /* set UDP message length in UDP header */
471 chklen_hdr = chklen = pcb->chksum_len_tx;
472 if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
473 if (chklen != 0) {
474 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
475 }
476 /* For UDP-Lite, checksum length of 0 means checksum
477 over the complete packet. (See RFC 3828 chap. 3.1)
478 At least the UDP-Lite header must be covered by the
479 checksum, therefore, if chksum_len has an illegal
480 value, we generate the checksum over the complete
481 packet to be safe. */
482 chklen_hdr = 0;
483 chklen = q->tot_len;
484 }
485 udphdr->len = htons(chklen_hdr);
486 /* calculate checksum */
487#if CHECKSUM_GEN_UDP
488 udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip,
489 IP_PROTO_UDPLITE, q->tot_len, chklen);
490 /* chksum zero must become 0xffff, as zero means 'no checksum' */
491 if (udphdr->chksum == 0x0000)
492 udphdr->chksum = 0xffff;
493#endif /* CHECKSUM_CHECK_UDP */
494 /* output to IP */
495 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
496#if LWIP_NETIF_HWADDRHINT
497 netif->addr_hint = &(pcb->addr_hint);
498#endif /* LWIP_NETIF_HWADDRHINT*/
499 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);
500#if LWIP_NETIF_HWADDRHINT
501 netif->addr_hint = NULL;
502#endif /* LWIP_NETIF_HWADDRHINT*/
503 } else
504#endif /* LWIP_UDPLITE */
505 { /* UDP */
506 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
507 udphdr->len = htons(q->tot_len);
508 /* calculate checksum */
509#if CHECKSUM_GEN_UDP
510 if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
511 udphdr->chksum = inet_chksum_pseudo(q, src_ip, dst_ip, IP_PROTO_UDP, q->tot_len);
512 /* chksum zero must become 0xffff, as zero means 'no checksum' */
513 if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
514 }
515#endif /* CHECKSUM_CHECK_UDP */
516 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
517 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
518 /* output to IP */
519#if LWIP_NETIF_HWADDRHINT
520 netif->addr_hint = &(pcb->addr_hint);
521#endif /* LWIP_NETIF_HWADDRHINT*/
522 err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);
523#if LWIP_NETIF_HWADDRHINT
524 netif->addr_hint = NULL;
525#endif /* LWIP_NETIF_HWADDRHINT*/
526 }
527 /* TODO: must this be increased even if error occured? */
528 snmp_inc_udpoutdatagrams();
529
530 /* did we chain a separate header pbuf earlier? */
531 if (q != p) {
532 /* free the header pbuf */
533 pbuf_free(q);
534 q = NULL;
535 /* p is still referenced by the caller, and will live on */
536 }
537
538 UDP_STATS_INC(udp.xmit);
539 return err;
540}
541
542/**
543 * Bind an UDP PCB.
544 *
545 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
546 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
547 * bind to all local interfaces.
548 * @param port local UDP port to bind with. Use 0 to automatically bind
549 * to a random port between UDP_LOCAL_PORT_RANGE_START and
550 * UDP_LOCAL_PORT_RANGE_END.
551 *
552 * ipaddr & port are expected to be in the same byte order as in the pcb.
553 *
554 * @return lwIP error code.
555 * - ERR_OK. Successful. No error occured.
556 * - ERR_USE. The specified ipaddr and port are already bound to by
557 * another UDP PCB.
558 *
559 * @see udp_disconnect()
560 */
561err_t
562udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
563{
564 struct udp_pcb *ipcb;
565 u8_t rebind;
566
567 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 3, ("udp_bind(ipaddr = "));
568 ip_addr_debug_print(UDP_DEBUG, ipaddr);
569 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | 3, (", port = %"U16_F")\n", port));
570
571 rebind = 0;
572 /* Check for double bind and rebind of the same pcb */
573 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
574 /* is this UDP PCB already on active list? */
575 if (pcb == ipcb) {
576 /* pcb may occur at most once in active list */
577 LWIP_ASSERT("rebind == 0", rebind == 0);
578 /* pcb already in list, just rebind */
579 rebind = 1;
580 }
581
582 /* this code does not allow upper layer to share a UDP port for
583 listening to broadcast or multicast traffic (See SO_REUSE_ADDR and
584 SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR
585 combine with implementation of UDP PCB flags. Leon Woestenberg. */
586#ifdef LWIP_UDP_TODO
587 /* port matches that of PCB in list? */
588 else
589 if ((ipcb->local_port == port) &&
590 /* IP address matches, or one is IP_ADDR_ANY? */
591 (ip_addr_isany(&(ipcb->local_ip)) ||
592 ip_addr_isany(ipaddr) ||
593 ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
594 /* other PCB already binds to this local IP and port */
595 LWIP_DEBUGF(UDP_DEBUG,
596 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
597 return ERR_USE;
598 }
599#endif
600 }
601
602 ip_addr_set(&pcb->local_ip, ipaddr);
603
604 /* no port specified? */
605 if (port == 0) {
606#ifndef UDP_LOCAL_PORT_RANGE_START
607#define UDP_LOCAL_PORT_RANGE_START 4096
608#define UDP_LOCAL_PORT_RANGE_END 0x7fff
609#endif
610 port = UDP_LOCAL_PORT_RANGE_START;
611 ipcb = udp_pcbs;
612 while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
613 if (ipcb->local_port == port) {
614 /* port is already used by another udp_pcb */
615 port++;
616 /* restart scanning all udp pcbs */
617 ipcb = udp_pcbs;
618 } else
619 /* go on with next udp pcb */
620 ipcb = ipcb->next;
621 }
622 if (ipcb != NULL) {
623 /* no more ports available in local range */
624 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
625 return ERR_USE;
626 }
627 }
628 pcb->local_port = port;
629 snmp_insert_udpidx_tree(pcb);
630 /* pcb not active yet? */
631 if (rebind == 0) {
632 /* place the PCB on the active list if not already there */
633 pcb->next = udp_pcbs;
634 udp_pcbs = pcb;
635 }
636 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
637 ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",
638 (u16_t)(ntohl(pcb->local_ip.addr) >> 24 & 0xff),
639 (u16_t)(ntohl(pcb->local_ip.addr) >> 16 & 0xff),
640 (u16_t)(ntohl(pcb->local_ip.addr) >> 8 & 0xff),
641 (u16_t)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));
642 return ERR_OK;
643}
644/**
645 * Connect an UDP PCB.
646 *
647 * This will associate the UDP PCB with the remote address.
648 *
649 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
650 * @param ipaddr remote IP address to connect with.
651 * @param port remote UDP port to connect with.
652 *
653 * @return lwIP error code
654 *
655 * ipaddr & port are expected to be in the same byte order as in the pcb.
656 *
657 * The udp pcb is bound to a random local port if not already bound.
658 *
659 * @see udp_disconnect()
660 */
661err_t
662udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
663{
664 struct udp_pcb *ipcb;
665
666 if (pcb->local_port == 0) {
667 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
668 if (err != ERR_OK)
669 return err;
670 }
671
672 ip_addr_set(&pcb->remote_ip, ipaddr);
673 pcb->remote_port = port;
674 pcb->flags |= UDP_FLAGS_CONNECTED;
675/** TODO: this functionality belongs in upper layers */
676#ifdef LWIP_UDP_TODO
677 /* Nail down local IP for netconn_addr()/getsockname() */
678 if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
679 struct netif *netif;
680
681 if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
682 LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
683 UDP_STATS_INC(udp.rterr);
684 return ERR_RTE;
685 }
686 /** TODO: this will bind the udp pcb locally, to the interface which
687 is used to route output packets to the remote address. However, we
688 might want to accept incoming packets on any interface! */
689 pcb->local_ip = netif->ip_addr;
690 } else if (ip_addr_isany(&pcb->remote_ip)) {
691 pcb->local_ip.addr = 0;
692 }
693#endif
694 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
695 ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",
696 (u16_t)(ntohl(pcb->remote_ip.addr) >> 24 & 0xff),
697 (u16_t)(ntohl(pcb->remote_ip.addr) >> 16 & 0xff),
698 (u16_t)(ntohl(pcb->remote_ip.addr) >> 8 & 0xff),
699 (u16_t)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));
700
701 /* Insert UDP PCB into the list of active UDP PCBs. */
702 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
703 if (pcb == ipcb) {
704 /* already on the list, just return */
705 return ERR_OK;
706 }
707 }
708 /* PCB not yet on the list, add PCB now */
709 pcb->next = udp_pcbs;
710 udp_pcbs = pcb;
711 return ERR_OK;
712}
713
714/**
715 * Disconnect a UDP PCB
716 *
717 * @param pcb the udp pcb to disconnect.
718 */
719void
720udp_disconnect(struct udp_pcb *pcb)
721{
722 /* reset remote address association */
723 ip_addr_set(&pcb->remote_ip, IP_ADDR_ANY);
724 pcb->remote_port = 0;
725 /* mark PCB as unconnected */
726 pcb->flags &= ~UDP_FLAGS_CONNECTED;
727}
728
729/**
730 * Set a receive callback for a UDP PCB
731 *
732 * This callback will be called when receiving a datagram for the pcb.
733 *
734 * @param pcb the pcb for wich to set the recv callback
735 * @param recv function pointer of the callback function
736 * @param recv_arg additional argument to pass to the callback function
737 */
738void
739udp_recv(struct udp_pcb *pcb,
740 void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,
741 struct ip_addr *addr, u16_t port),
742 void *recv_arg)
743{
744 /* remember recv() callback and user data */
745 pcb->recv = recv;
746 pcb->recv_arg = recv_arg;
747}
748
749/**
750 * Remove an UDP PCB.
751 *
752 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
753 * UDP PCB's and the data structure is freed from memory.
754 *
755 * @see udp_new()
756 */
757void
758udp_remove(struct udp_pcb *pcb)
759{
760 struct udp_pcb *pcb2;
761
762 snmp_delete_udpidx_tree(pcb);
763 /* pcb to be removed is first in list? */
764 if (udp_pcbs == pcb) {
765 /* make list start at 2nd pcb */
766 udp_pcbs = udp_pcbs->next;
767 /* pcb not 1st in list */
768 } else
769 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
770 /* find pcb in udp_pcbs list */
771 if (pcb2->next != NULL && pcb2->next == pcb) {
772 /* remove pcb from list */
773 pcb2->next = pcb->next;
774 }
775 }
776 memp_free(MEMP_UDP_PCB, pcb);
777}
778
779/**
780 * Create a UDP PCB.
781 *
782 * @return The UDP PCB which was created. NULL if the PCB data structure
783 * could not be allocated.
784 *
785 * @see udp_remove()
786 */
787struct udp_pcb *
788udp_new(void)
789{
790 struct udp_pcb *pcb;
791 pcb = memp_malloc(MEMP_UDP_PCB);
792 /* could allocate UDP PCB? */
793 if (pcb != NULL) {
794 /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
795 * which means checksum is generated over the whole datagram per default
796 * (recommended as default by RFC 3828). */
797 /* initialize PCB to all zeroes */
798 memset(pcb, 0, sizeof(struct udp_pcb));
799 pcb->ttl = UDP_TTL;
800 }
801 return pcb;
802}
803
804#if UDP_DEBUG
805/**
806 * Print UDP header information for debug purposes.
807 *
808 * @param udphdr pointer to the udp header in memory.
809 */
810void
811udp_debug_print(struct udp_hdr *udphdr)
812{
813 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
814 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
815 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
816 ntohs(udphdr->src), ntohs(udphdr->dest)));
817 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
818 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
819 ntohs(udphdr->len), ntohs(udphdr->chksum)));
820 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
821}
822#endif /* UDP_DEBUG */
823
824#endif /* LWIP_UDP */
This page took 0.090577 seconds and 4 git commands to generate.