3 * User Datagram Protocol module
\r
7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
\r
8 * All rights reserved.
\r
10 * Redistribution and use in source and binary forms, with or without modification,
\r
11 * are permitted provided that the following conditions are met:
\r
13 * 1. Redistributions of source code must retain the above copyright notice,
\r
14 * this list of conditions and the following disclaimer.
\r
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
16 * this list of conditions and the following disclaimer in the documentation
\r
17 * and/or other materials provided with the distribution.
\r
18 * 3. The name of the author may not be used to endorse or promote products
\r
19 * derived from this software without specific prior written permission.
\r
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
\r
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
\r
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
\r
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
\r
32 * This file is part of the lwIP TCP/IP stack.
\r
34 * Author: Adam Dunkels <adam@sics.se>
\r
41 * The code for the User Datagram Protocol UDP.
\r
47 #include "lwip/opt.h"
\r
49 #include "lwip/def.h"
\r
50 #include "lwip/memp.h"
\r
51 #include "lwip/inet.h"
\r
52 #include "lwip/ip_addr.h"
\r
53 #include "lwip/netif.h"
\r
54 #include "lwip/udp.h"
\r
55 #include "lwip/icmp.h"
\r
57 #include "lwip/stats.h"
\r
59 #include "arch/perf.h"
\r
60 #include "lwip/snmp.h"
\r
62 /* The list of UDP PCBs */
\r
64 /* exported in udp.h (was static) */
\r
65 struct udp_pcb *udp_pcbs = NULL;
\r
67 static struct udp_pcb *pcb_cache = NULL;
\r
72 udp_pcbs = pcb_cache = NULL;
\r
76 * Process an incoming UDP datagram.
\r
78 * Given an incoming UDP datagram (as a chain of pbufs) this function
\r
79 * finds a corresponding UDP PCB and
\r
81 * @param pbuf pbuf to be demultiplexed to a UDP PCB.
\r
82 * @param netif network interface on which the datagram was received.
\r
86 udp_input(struct pbuf *p, struct netif *inp)
\r
88 struct udp_hdr *udphdr;
\r
89 struct udp_pcb *pcb;
\r
90 struct udp_pcb *uncon_pcb;
\r
91 struct ip_hdr *iphdr;
\r
97 UDP_STATS_INC(udp.recv);
\r
101 if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN)) {
\r
102 /* drop short packets */
\r
103 // LWIP_DEBUGF(UDP_DEBUG,
\r
104 // ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
\r
106 LWIP_DEBUGF(UDP_DEBUG,
\r
107 ("udp_input: short UDP datagram (%u bytes) discarded\n", p->tot_len));
\r
109 UDP_STATS_INC(udp.lenerr);
\r
110 UDP_STATS_INC(udp.drop);
\r
111 snmp_inc_udpinerrors();
\r
116 pbuf_header(p, -((s16_t)(IPH_HL(iphdr) * 4)));
\r
118 udphdr = (struct udp_hdr *)p->payload;
\r
120 LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
\r
122 src = ntohs(udphdr->src);
\r
123 dest = ntohs(udphdr->dest);
\r
125 udp_debug_print(udphdr);
\r
127 /* print the UDP source and destination */
\r
128 LWIP_DEBUGF(UDP_DEBUG,
\r
129 ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
\r
130 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
\r
131 ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),
\r
132 ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),
\r
133 ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),
\r
134 ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));
\r
138 /* Iterate through the UDP pcb list for a matching pcb */
\r
139 for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
\r
140 /* print the PCB local and remote address */
\r
141 LWIP_DEBUGF(UDP_DEBUG,
\r
142 ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "
\r
143 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
\r
144 ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
\r
145 ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
\r
146 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
\r
147 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
\r
149 /* compare PCB local addr+port to UDP destination addr+port */
\r
150 if ((pcb->local_port == dest) &&
\r
151 (ip_addr_isany(&pcb->local_ip) ||
\r
152 ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)) ||
\r
153 ip_addr_isbroadcast(&(iphdr->dest), inp))) {
\r
155 if ((uncon_pcb == NULL) &&
\r
156 ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
\r
157 /* the first unconnected matching PCB */
\r
161 /* compare PCB remote addr+port to UDP source addr+port */
\r
162 if ((local_match != 0) &&
\r
163 (pcb->remote_port == src) &&
\r
164 (ip_addr_isany(&pcb->remote_ip) ||
\r
165 ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)))) {
\r
166 /* the first fully matching PCB */
\r
170 /* no fully matching pcb found? then look for an unconnected pcb */
\r
175 /* Check checksum if this is a match or if it was directed at us. */
\r
176 if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &iphdr->dest)) {
\r
177 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: calculating checksum\n"));
\r
179 if (iphdr->nexthdr == IP_PROTO_UDPLITE) {
\r
181 if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
\r
183 /* Do the UDP Lite checksum */
\r
184 #if CHECKSUM_CHECK_UDP
\r
185 if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
\r
186 (struct ip_addr *)&(iphdr->dest),
\r
187 IP_PROTO_UDPLITE, ntohs(udphdr->len)) != 0) {
\r
188 LWIP_DEBUGF(UDP_DEBUG | 2,
\r
189 ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
\r
190 UDP_STATS_INC(udp.chkerr);
\r
191 UDP_STATS_INC(udp.drop);
\r
192 snmp_inc_udpinerrors();
\r
198 #if CHECKSUM_CHECK_UDP
\r
199 if (udphdr->chksum != 0) {
\r
200 if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
\r
201 (struct ip_addr *)&(iphdr->dest),
\r
202 IP_PROTO_UDP, p->tot_len) != 0) {
\r
203 LWIP_DEBUGF(UDP_DEBUG | 2,
\r
204 ("udp_input: UDP datagram discarded due to failing checksum\n"));
\r
205 UDP_STATS_INC(udp.chkerr);
\r
206 UDP_STATS_INC(udp.drop);
\r
207 snmp_inc_udpinerrors();
\r
214 pbuf_header(p, -UDP_HLEN);
\r
216 snmp_inc_udpindatagrams();
\r
218 if (pcb->recv != NULL)
\r
219 pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src), src);
\r
221 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: not for us.\n"));
\r
223 /* No match was found, send ICMP destination port unreachable unless
\r
224 destination address was broadcast/multicast. */
\r
226 if (!ip_addr_isbroadcast(&iphdr->dest, inp) &&
\r
227 !ip_addr_ismulticast(&iphdr->dest)) {
\r
229 /* restore pbuf pointer */
\r
230 p->payload = iphdr;
\r
231 icmp_dest_unreach(p, ICMP_DUR_PORT);
\r
233 UDP_STATS_INC(udp.proterr);
\r
234 UDP_STATS_INC(udp.drop);
\r
235 snmp_inc_udpnoports();
\r
242 PERF_STOP("udp_input");
\r
246 * Send data to a specified address using UDP.
\r
248 * @param pcb UDP PCB used to send the data.
\r
249 * @param pbuf chain of pbuf's to be sent.
\r
250 * @param dst_ip Destination IP address.
\r
251 * @param dst_port Destination UDP port.
\r
253 * If the PCB already has a remote address association, it will
\r
254 * be restored after the data is sent.
\r
256 * @return lwIP error code.
\r
257 * - ERR_OK. Successful. No error occured.
\r
258 * - ERR_MEM. Out of memory.
\r
259 * - ERR_RTE. Could not find route to destination address.
\r
261 * @see udp_disconnect() udp_send()
\r
264 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
\r
265 struct ip_addr *dst_ip, u16_t dst_port)
\r
268 /* temporary space for current PCB remote address */
\r
269 struct ip_addr pcb_remote_ip;
\r
270 u16_t pcb_remote_port;
\r
271 /* remember current remote peer address of PCB */
\r
272 pcb_remote_ip.addr = pcb->remote_ip.addr;
\r
273 pcb_remote_port = pcb->remote_port;
\r
274 /* copy packet destination address to PCB remote peer address */
\r
275 pcb->remote_ip.addr = dst_ip->addr;
\r
276 pcb->remote_port = dst_port;
\r
277 /* send to the packet destination address */
\r
278 err = udp_send(pcb, p);
\r
279 /* restore PCB remote peer address */
\r
280 pcb->remote_ip.addr = pcb_remote_ip.addr;
\r
281 pcb->remote_port = pcb_remote_port;
\r
286 * Send data using UDP.
\r
288 * @param pcb UDP PCB used to send the data.
\r
289 * @param pbuf chain of pbuf's to be sent.
\r
291 * @return lwIP error code.
\r
292 * - ERR_OK. Successful. No error occured.
\r
293 * - ERR_MEM. Out of memory.
\r
294 * - ERR_RTE. Could not find route to destination address.
\r
296 * @see udp_disconnect() udp_sendto()
\r
299 udp_send(struct udp_pcb *pcb, struct pbuf *p)
\r
301 struct udp_hdr *udphdr;
\r
302 struct netif *netif;
\r
303 struct ip_addr *src_ip;
\r
305 struct pbuf *q; /* q will be sent down the stack */
\r
307 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_send\n"));
\r
309 /* if the PCB is not yet bound to a port, bind it here */
\r
310 if (pcb->local_port == 0) {
\r
311 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding now\n"));
\r
312 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
\r
313 if (err != ERR_OK) {
\r
314 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: forced port bind failed\n"));
\r
318 /* find the outgoing network interface for this packet */
\r
319 netif = ip_route(&(pcb->remote_ip));
\r
320 /* no outgoing network interface could be found? */
\r
321 if (netif == NULL) {
\r
322 LWIP_DEBUGF(UDP_DEBUG | 1, ("udp_send: No route to 0x%"X32_F"\n", pcb->remote_ip.addr));
\r
323 UDP_STATS_INC(udp.rterr);
\r
327 /* not enough space to add an UDP header to first pbuf in given p chain? */
\r
328 if (pbuf_header(p, UDP_HLEN)) {
\r
329 /* allocate header in a seperate new pbuf */
\r
330 q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
\r
331 /* new header pbuf could not be allocated? */
\r
333 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: could not allocate header\n"));
\r
336 /* chain header q in front of given pbuf p */
\r
338 /* first pbuf q points to header pbuf */
\r
339 LWIP_DEBUGF(UDP_DEBUG,
\r
340 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
\r
341 /* adding a header within p succeeded */
\r
343 /* first pbuf q equals given pbuf */
\r
345 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
\r
347 /* q now represents the packet to be sent */
\r
348 udphdr = q->payload;
\r
349 udphdr->src = htons(pcb->local_port);
\r
350 udphdr->dest = htons(pcb->remote_port);
\r
351 /* in UDP, 0 checksum means 'no checksum' */
\r
352 udphdr->chksum = 0x0000;
\r
354 /* PCB local address is IP_ANY_ADDR? */
\r
355 if (ip_addr_isany(&pcb->local_ip)) {
\r
356 /* use outgoing network interface IP address as source address */
\r
357 src_ip = &(netif->ip_addr);
\r
359 /* use UDP PCB local IP address as source address */
\r
360 src_ip = &(pcb->local_ip);
\r
363 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
\r
365 /* UDP Lite protocol? */
\r
366 if (pcb->flags & UDP_FLAGS_UDPLITE) {
\r
367 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
\r
368 /* set UDP message length in UDP header */
\r
369 udphdr->len = htons(pcb->chksum_len);
\r
370 /* calculate checksum */
\r
371 #if CHECKSUM_GEN_UDP
\r
372 udphdr->chksum = inet_chksum_pseudo(q, src_ip, &(pcb->remote_ip),
\r
373 IP_PROTO_UDP, pcb->chksum_len);
\r
374 /* chksum zero must become 0xffff, as zero means 'no checksum' */
\r
375 if (udphdr->chksum == 0x0000)
\r
376 udphdr->chksum = 0xffff;
\r
378 udphdr->chksum = 0x0000;
\r
381 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
\r
382 err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);
\r
384 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
\r
385 udphdr->len = htons(q->tot_len);
\r
386 /* calculate checksum */
\r
387 #if CHECKSUM_GEN_UDP
\r
388 if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
\r
389 udphdr->chksum = inet_chksum_pseudo(q, src_ip, &pcb->remote_ip, IP_PROTO_UDP, q->tot_len);
\r
390 /* chksum zero must become 0xffff, as zero means 'no checksum' */
\r
391 if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
\r
394 udphdr->chksum = 0x0000;
\r
396 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
\r
397 LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
\r
399 err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);
\r
401 /* TODO: must this be increased even if error occured? */
\r
402 snmp_inc_udpoutdatagrams();
\r
404 /* did we chain a seperate header pbuf earlier? */
\r
406 /* free the header pbuf */
\r
409 /* p is still referenced by the caller, and will live on */
\r
412 UDP_STATS_INC(udp.xmit);
\r
419 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
\r
420 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
\r
421 * bind to all local interfaces.
\r
422 * @param port local UDP port to bind with.
\r
424 * @return lwIP error code.
\r
425 * - ERR_OK. Successful. No error occured.
\r
426 * - ERR_USE. The specified ipaddr and port are already bound to by
\r
429 * @see udp_disconnect()
\r
432 udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
\r
434 struct udp_pcb *ipcb;
\r
437 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_bind(ipaddr = "));
\r
438 ip_addr_debug_print(UDP_DEBUG, ipaddr);
\r
439 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, (", port = %"U16_F")\n", port));
\r
442 /* Check for double bind and rebind of the same pcb */
\r
443 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
\r
444 /* is this UDP PCB already on active list? */
\r
446 /* pcb may occur at most once in active list */
\r
447 LWIP_ASSERT("rebind == 0", rebind == 0);
\r
448 /* pcb already in list, just rebind */
\r
452 /* this code does not allow upper layer to share a UDP port for
\r
453 listening to broadcast or multicast traffic (See SO_REUSE_ADDR and
\r
454 SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR
\r
455 combine with implementation of UDP PCB flags. Leon Woestenberg. */
\r
456 #ifdef LWIP_UDP_TODO
\r
457 /* port matches that of PCB in list? */
\r
459 if ((ipcb->local_port == port) &&
\r
460 /* IP address matches, or one is IP_ADDR_ANY? */
\r
461 (ip_addr_isany(&(ipcb->local_ip)) ||
\r
462 ip_addr_isany(ipaddr) ||
\r
463 ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
\r
464 /* other PCB already binds to this local IP and port */
\r
465 LWIP_DEBUGF(UDP_DEBUG,
\r
466 ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
\r
472 ip_addr_set(&pcb->local_ip, ipaddr);
\r
474 /* no port specified? */
\r
476 #ifndef UDP_LOCAL_PORT_RANGE_START
\r
477 #define UDP_LOCAL_PORT_RANGE_START 4096
\r
478 #define UDP_LOCAL_PORT_RANGE_END 0x7fff
\r
480 port = UDP_LOCAL_PORT_RANGE_START;
\r
482 while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
\r
483 if (ipcb->local_port == port) {
\r
489 if (ipcb != NULL) {
\r
490 /* no more ports available in local range */
\r
491 LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
\r
495 pcb->local_port = port;
\r
496 snmp_insert_udpidx_tree(pcb);
\r
497 /* pcb not active yet? */
\r
499 /* place the PCB on the active list if not already there */
\r
500 pcb->next = udp_pcbs;
\r
503 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE,
\r
504 ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",
\r
505 (u16_t)(ntohl(pcb->local_ip.addr) >> 24 & 0xff),
\r
506 (u16_t)(ntohl(pcb->local_ip.addr) >> 16 & 0xff),
\r
507 (u16_t)(ntohl(pcb->local_ip.addr) >> 8 & 0xff),
\r
508 (u16_t)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));
\r
512 * Connect an UDP PCB.
\r
514 * This will associate the UDP PCB with the remote address.
\r
516 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
\r
517 * @param ipaddr remote IP address to connect with.
\r
518 * @param port remote UDP port to connect with.
\r
520 * @return lwIP error code
\r
522 * @see udp_disconnect()
\r
525 udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
\r
527 struct udp_pcb *ipcb;
\r
529 if (pcb->local_port == 0) {
\r
530 err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
\r
535 ip_addr_set(&pcb->remote_ip, ipaddr);
\r
536 pcb->remote_port = port;
\r
537 pcb->flags |= UDP_FLAGS_CONNECTED;
\r
538 /** TODO: this functionality belongs in upper layers */
\r
539 #ifdef LWIP_UDP_TODO
\r
540 /* Nail down local IP for netconn_addr()/getsockname() */
\r
541 if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
\r
542 struct netif *netif;
\r
544 if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
\r
545 LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
\r
546 UDP_STATS_INC(udp.rterr);
\r
549 /** TODO: this will bind the udp pcb locally, to the interface which
\r
550 is used to route output packets to the remote address. However, we
\r
551 might want to accept incoming packets on any interface! */
\r
552 pcb->local_ip = netif->ip_addr;
\r
553 } else if (ip_addr_isany(&pcb->remote_ip)) {
\r
554 pcb->local_ip.addr = 0;
\r
557 LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE,
\r
558 ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",
\r
559 (u16_t)(ntohl(pcb->remote_ip.addr) >> 24 & 0xff),
\r
560 (u16_t)(ntohl(pcb->remote_ip.addr) >> 16 & 0xff),
\r
561 (u16_t)(ntohl(pcb->remote_ip.addr) >> 8 & 0xff),
\r
562 (u16_t)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));
\r
564 /* Insert UDP PCB into the list of active UDP PCBs. */
\r
565 for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
\r
567 /* already on the list, just return */
\r
571 /* PCB not yet on the list, add PCB now */
\r
572 pcb->next = udp_pcbs;
\r
578 udp_disconnect(struct udp_pcb *pcb)
\r
580 /* reset remote address association */
\r
581 ip_addr_set(&pcb->remote_ip, IP_ADDR_ANY);
\r
582 pcb->remote_port = 0;
\r
583 /* mark PCB as unconnected */
\r
584 pcb->flags &= ~UDP_FLAGS_CONNECTED;
\r
588 udp_recv(struct udp_pcb *pcb,
\r
589 void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,
\r
590 struct ip_addr *addr, u16_t port),
\r
593 /* remember recv() callback and user data */
\r
595 pcb->recv_arg = recv_arg;
\r
599 * Remove an UDP PCB.
\r
601 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
\r
602 * UDP PCB's and the data structure is freed from memory.
\r
607 udp_remove(struct udp_pcb *pcb)
\r
609 struct udp_pcb *pcb2;
\r
611 snmp_delete_udpidx_tree(pcb);
\r
612 /* pcb to be removed is first in list? */
\r
613 if (udp_pcbs == pcb) {
\r
614 /* make list start at 2nd pcb */
\r
615 udp_pcbs = udp_pcbs->next;
\r
616 /* pcb not 1st in list */
\r
618 for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
\r
619 /* find pcb in udp_pcbs list */
\r
620 if (pcb2->next != NULL && pcb2->next == pcb) {
\r
621 /* remove pcb from list */
\r
622 pcb2->next = pcb->next;
\r
625 memp_free(MEMP_UDP_PCB, pcb);
\r
629 * Create a UDP PCB.
\r
631 * @return The UDP PCB which was created. NULL if the PCB data structure
\r
632 * could not be allocated.
\r
634 * @see udp_remove()
\r
639 struct udp_pcb *pcb;
\r
640 pcb = memp_malloc(MEMP_UDP_PCB);
\r
641 /* could allocate UDP PCB? */
\r
643 /* initialize PCB to all zeroes */
\r
644 memset(pcb, 0, sizeof(struct udp_pcb));
\r
645 pcb->ttl = UDP_TTL;
\r
652 udp_debug_print(struct udp_hdr *udphdr)
\r
654 LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
\r
655 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
\r
656 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
\r
657 ntohs(udphdr->src), ntohs(udphdr->dest)));
\r
658 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
\r
659 LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
\r
660 ntohs(udphdr->len), ntohs(udphdr->chksum)));
\r
661 LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
\r
663 #endif /* UDP_DEBUG */
\r
665 #endif /* LWIP_UDP */
\r