udp.c
Upload User: tym7xi
Upload Date: 2018-01-17
Package Size: 6975k
Code Size: 21k
Development Platform:

C/C++

  1. /**
  2.  * @file
  3.  * User Datagram Protocol module
  4.  *
  5.  */
  6. /*
  7.  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without modification,
  11.  * are permitted provided that the following conditions are met:
  12.  *
  13.  * 1. Redistributions of source code must retain the above copyright notice,
  14.  *    this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright notice,
  16.  *    this list of conditions and the following disclaimer in the documentation
  17.  *    and/or other materials provided with the distribution.
  18.  * 3. The name of the author may not be used to endorse or promote products
  19.  *    derived from this software without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24.  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29.  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30.  * OF SUCH DAMAGE.
  31.  *
  32.  * This file is part of the lwIP TCP/IP stack.
  33.  *
  34.  * Author: Adam Dunkels <adam@sics.se>
  35.  *
  36.  */
  37. /* udp.c
  38.  *
  39.  * The code for the User Datagram Protocol UDP.
  40.  *
  41.  */
  42. #include <string.h>
  43. #include "lwip/opt.h"
  44. #include "lwip/def.h"
  45. #include "lwip/memp.h"
  46. #include "lwip/inet.h"
  47. #include "lwip/ip_addr.h"
  48. #include "lwip/netif.h"
  49. #include "lwip/udp.h"
  50. #include "lwip/icmp.h"
  51. #include "lwip/stats.h"
  52. #include "arch/perf.h"
  53. #include "lwip/snmp.h"
  54. /* The list of UDP PCBs */
  55. #if LWIP_UDP
  56. /* exported in udp.h (was static) */
  57. struct udp_pcb *udp_pcbs = NULL;
  58. static struct udp_pcb *pcb_cache = NULL;
  59. void
  60. udp_init(void)
  61. {
  62.   udp_pcbs = pcb_cache = NULL;
  63. }
  64. /**
  65.  * Process an incoming UDP datagram.
  66.  *
  67.  * Given an incoming UDP datagram (as a chain of pbufs) this function
  68.  * finds a corresponding UDP PCB and
  69.  *
  70.  * @param pbuf pbuf to be demultiplexed to a UDP PCB.
  71.  * @param netif network interface on which the datagram was received.
  72.  *
  73.  */
  74. void
  75. udp_input(struct pbuf *p, struct netif *inp)
  76. {
  77.   struct udp_hdr *udphdr;
  78.   struct udp_pcb *pcb;
  79.   struct udp_pcb *uncon_pcb;
  80.   struct ip_hdr *iphdr;
  81.   u16_t src, dest;
  82.   u8_t local_match;
  83.   PERF_START;
  84.   UDP_STATS_INC(udp.recv);
  85.   iphdr = p->payload;
  86.   if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN)) {
  87.     /* drop short packets */
  88.     LWIP_DEBUGF(UDP_DEBUG,
  89.                 ("udp_input: short UDP datagram (%"U16_F" bytes) discardedn", p->tot_len));
  90.     UDP_STATS_INC(udp.lenerr);
  91.     UDP_STATS_INC(udp.drop);
  92.     snmp_inc_udpinerrors();
  93.     pbuf_free(p);
  94.     goto end;
  95.   }
  96.   pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4));
  97.   udphdr = (struct udp_hdr *)p->payload;
  98.   LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"n", p->tot_len));
  99.   src = ntohs(udphdr->src);
  100.   dest = ntohs(udphdr->dest);
  101.   udp_debug_print(udphdr);
  102.   /* print the UDP source and destination */
  103.   LWIP_DEBUGF(UDP_DEBUG,
  104.               ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
  105.                "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")n",
  106.                ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),
  107.                ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),
  108.                ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),
  109.                ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));
  110.   local_match = 0;
  111.   uncon_pcb = NULL;
  112.   /* Iterate through the UDP pcb list for a matching pcb */
  113.   for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
  114.     /* print the PCB local and remote address */
  115.     LWIP_DEBUGF(UDP_DEBUG,
  116.                 ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "
  117.                  "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")n",
  118.                  ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
  119.                  ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
  120.                  ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
  121.                  ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
  122.     /* compare PCB local addr+port to UDP destination addr+port */
  123.     if ((pcb->local_port == dest) &&
  124.         (ip_addr_isany(&pcb->local_ip) ||
  125.          ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)) || 
  126.          ip_addr_isbroadcast(&(iphdr->dest), inp))) {
  127.       local_match = 1;
  128.       if ((uncon_pcb == NULL) && 
  129.           ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
  130.         /* the first unconnected matching PCB */     
  131.         uncon_pcb = pcb;
  132.       }
  133.     }
  134.     /* compare PCB remote addr+port to UDP source addr+port */
  135.     if ((local_match != 0) &&
  136.         (pcb->remote_port == src) &&
  137.         (ip_addr_isany(&pcb->remote_ip) ||
  138.          ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)))) {
  139.       /* the first fully matching PCB */
  140.       break;
  141.     }
  142.   }
  143.   /* no fully matching pcb found? then look for an unconnected pcb */
  144.   if (pcb == NULL) {
  145.     pcb = uncon_pcb;
  146.   }
  147.   /* Check checksum if this is a match or if it was directed at us. */
  148.   if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &iphdr->dest)) {
  149.     LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: calculating checksumn"));
  150. #ifdef IPv6
  151.     if (iphdr->nexthdr == IP_PROTO_UDPLITE) {
  152. #else
  153.     if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
  154. #endif /* IPv4 */
  155.       /* Do the UDP Lite checksum */
  156. #if CHECKSUM_CHECK_UDP
  157.       if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
  158.                              (struct ip_addr *)&(iphdr->dest),
  159.                              IP_PROTO_UDPLITE, ntohs(udphdr->len)) != 0) {
  160.         LWIP_DEBUGF(UDP_DEBUG | 2,
  161.                     ("udp_input: UDP Lite datagram discarded due to failing checksumn"));
  162.         UDP_STATS_INC(udp.chkerr);
  163.         UDP_STATS_INC(udp.drop);
  164.         snmp_inc_udpinerrors();
  165.         pbuf_free(p);
  166.         goto end;
  167.       }
  168. #endif
  169.     } else {
  170. #if CHECKSUM_CHECK_UDP
  171.       if (udphdr->chksum != 0) {
  172.         if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
  173.                                (struct ip_addr *)&(iphdr->dest),
  174.                                IP_PROTO_UDP, p->tot_len) != 0) {
  175.           LWIP_DEBUGF(UDP_DEBUG | 2,
  176.                       ("udp_input: UDP datagram discarded due to failing checksumn"));
  177.           UDP_STATS_INC(udp.chkerr);
  178.           UDP_STATS_INC(udp.drop);
  179.           snmp_inc_udpinerrors();
  180.           pbuf_free(p);
  181.           goto end;
  182.         }
  183.       }
  184. #endif
  185.     }
  186.     pbuf_header(p, -UDP_HLEN);
  187.     if (pcb != NULL) {
  188.       snmp_inc_udpindatagrams();
  189.       /* callback */
  190.       if (pcb->recv != NULL)
  191.         pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src), src);
  192.     } else {
  193.       LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: not for us.n"));
  194.       /* No match was found, send ICMP destination port unreachable unless
  195.          destination address was broadcast/multicast. */
  196.       if (!ip_addr_isbroadcast(&iphdr->dest, inp) &&
  197.           !ip_addr_ismulticast(&iphdr->dest)) {
  198.         /* restore pbuf pointer */
  199.         p->payload = iphdr;
  200.         icmp_dest_unreach(p, ICMP_DUR_PORT);
  201.       }
  202.       UDP_STATS_INC(udp.proterr);
  203.       UDP_STATS_INC(udp.drop);
  204.       snmp_inc_udpnoports();
  205.       pbuf_free(p);
  206.     }
  207.   } else {
  208.     pbuf_free(p);
  209.   }
  210. end:
  211.   PERF_STOP("udp_input");
  212. }
  213. /**
  214.  * Send data to a specified address using UDP.
  215.  *
  216.  * @param pcb UDP PCB used to send the data.
  217.  * @param pbuf chain of pbuf's to be sent.
  218.  * @param dst_ip Destination IP address.
  219.  * @param dst_port Destination UDP port.
  220.  *
  221.  * If the PCB already has a remote address association, it will
  222.  * be restored after the data is sent.
  223.  * 
  224.  * @return lwIP error code.
  225.  * - ERR_OK. Successful. No error occured.
  226.  * - ERR_MEM. Out of memory.
  227.  * - ERR_RTE. Could not find route to destination address.
  228.  *
  229.  * @see udp_disconnect() udp_send()
  230.  */
  231. err_t
  232. udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
  233.   struct ip_addr *dst_ip, u16_t dst_port)
  234. {
  235.   err_t err;
  236.   /* temporary space for current PCB remote address */
  237.   struct ip_addr pcb_remote_ip;
  238.   u16_t pcb_remote_port;
  239.   /* remember current remote peer address of PCB */
  240.   pcb_remote_ip.addr = pcb->remote_ip.addr;
  241.   pcb_remote_port = pcb->remote_port;
  242.   /* copy packet destination address to PCB remote peer address */
  243.   pcb->remote_ip.addr = dst_ip->addr;
  244.   pcb->remote_port = dst_port;
  245.   /* send to the packet destination address */
  246.   err = udp_send(pcb, p);
  247.   /* restore PCB remote peer address */
  248.   pcb->remote_ip.addr = pcb_remote_ip.addr;
  249.   pcb->remote_port = pcb_remote_port;
  250.   return err;
  251. }
  252. /**
  253.  * Send data using UDP.
  254.  *
  255.  * @param pcb UDP PCB used to send the data.
  256.  * @param pbuf chain of pbuf's to be sent.
  257.  *
  258.  * @return lwIP error code.
  259.  * - ERR_OK. Successful. No error occured.
  260.  * - ERR_MEM. Out of memory.
  261.  * - ERR_RTE. Could not find route to destination address.
  262.  *
  263.  * @see udp_disconnect() udp_sendto()
  264.  */
  265. err_t
  266. udp_send(struct udp_pcb *pcb, struct pbuf *p)
  267. {
  268.   struct udp_hdr *udphdr;
  269.   struct netif *netif;
  270.   struct ip_addr *src_ip;
  271.   err_t err;
  272.   struct pbuf *q; /* q will be sent down the stack */
  273.   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_sendn"));
  274.   /* if the PCB is not yet bound to a port, bind it here */
  275.   if (pcb->local_port == 0) {
  276.     LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding nown"));
  277.     err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
  278.     if (err != ERR_OK) {
  279.       LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: forced port bind failedn"));
  280.       return err;
  281.     }
  282.   }
  283.   /* find the outgoing network interface for this packet */
  284.   netif = ip_route(&(pcb->remote_ip));
  285.   /* no outgoing network interface could be found? */
  286.   if (netif == NULL) {
  287.     LWIP_DEBUGF(UDP_DEBUG | 1, ("udp_send: No route to 0x%"X32_F"n", pcb->remote_ip.addr));
  288.     UDP_STATS_INC(udp.rterr);
  289.     return ERR_RTE;
  290.   }
  291.   /* not enough space to add an UDP header to first pbuf in given p chain? */
  292.   if (pbuf_header(p, UDP_HLEN)) {
  293.     /* allocate header in a seperate new pbuf */
  294.     q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
  295.     /* new header pbuf could not be allocated? */
  296.     if (q == NULL) {
  297.       LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: could not allocate headern"));
  298.       return ERR_MEM;
  299.     }
  300.     /* chain header q in front of given pbuf p */
  301.     pbuf_chain(q, p);
  302.     /* first pbuf q points to header pbuf */
  303.     LWIP_DEBUGF(UDP_DEBUG,
  304.                 ("udp_send: added header pbuf %p before given pbuf %pn", (void *)q, (void *)p));
  305.     /* adding a header within p succeeded */
  306.   } else {
  307.     /* first pbuf q equals given pbuf */
  308.     q = p;
  309.     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %pn", (void *)p));
  310.   }
  311.   /* q now represents the packet to be sent */
  312.   udphdr = q->payload;
  313.   udphdr->src = htons(pcb->local_port);
  314.   udphdr->dest = htons(pcb->remote_port);
  315.   /* in UDP, 0 checksum means 'no checksum' */
  316.   udphdr->chksum = 0x0000; 
  317.   /* PCB local address is IP_ANY_ADDR? */
  318.   if (ip_addr_isany(&pcb->local_ip)) {
  319.     /* use outgoing network interface IP address as source address */
  320.     src_ip = &(netif->ip_addr);
  321.   } else {
  322.     /* use UDP PCB local IP address as source address */
  323.     src_ip = &(pcb->local_ip);
  324.   }
  325.   LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"n", q->tot_len));
  326.   /* UDP Lite protocol? */
  327.   if (pcb->flags & UDP_FLAGS_UDPLITE) {
  328.     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"n", q->tot_len));
  329.     /* set UDP message length in UDP header */
  330.     udphdr->len = htons(pcb->chksum_len);
  331.     /* calculate checksum */
  332. #if CHECKSUM_GEN_UDP
  333.     udphdr->chksum = inet_chksum_pseudo(q, src_ip, &(pcb->remote_ip),
  334.                                         IP_PROTO_UDP, pcb->chksum_len);
  335.     /* chksum zero must become 0xffff, as zero means 'no checksum' */
  336.     if (udphdr->chksum == 0x0000)
  337.       udphdr->chksum = 0xffff;
  338. #else
  339.     udphdr->chksum = 0x0000;
  340. #endif
  341.     /* output to IP */
  342.     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)n"));
  343.     err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);    
  344.   } else {      /* UDP */
  345.     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"n", q->tot_len));
  346.     udphdr->len = htons(q->tot_len);
  347.     /* calculate checksum */
  348. #if CHECKSUM_GEN_UDP
  349.     if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
  350.       udphdr->chksum = inet_chksum_pseudo(q, src_ip, &pcb->remote_ip, IP_PROTO_UDP, q->tot_len);
  351.       /* chksum zero must become 0xffff, as zero means 'no checksum' */
  352.       if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
  353.     }
  354. #else
  355.     udphdr->chksum = 0x0000;
  356. #endif
  357.     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"n", udphdr->chksum));
  358.     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)n"));
  359.     /* output to IP */
  360.     err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);    
  361.   }
  362.   /* TODO: must this be increased even if error occured? */
  363.   snmp_inc_udpoutdatagrams();
  364.   /* did we chain a seperate header pbuf earlier? */
  365.   if (q != p) {
  366.     /* free the header pbuf */
  367.     pbuf_free(q);
  368.     q = NULL;
  369.     /* p is still referenced by the caller, and will live on */
  370.   }
  371.   UDP_STATS_INC(udp.xmit);
  372.   return err;
  373. }
  374. /**
  375.  * Bind an UDP PCB.
  376.  *
  377.  * @param pcb UDP PCB to be bound with a local address ipaddr and port.
  378.  * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
  379.  * bind to all local interfaces.
  380.  * @param port local UDP port to bind with.
  381.  *
  382.  * @return lwIP error code.
  383.  * - ERR_OK. Successful. No error occured.
  384.  * - ERR_USE. The specified ipaddr and port are already bound to by
  385.  * another UDP PCB.
  386.  *
  387.  * @see udp_disconnect()
  388.  */
  389. err_t
  390. udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
  391. {
  392.   struct udp_pcb *ipcb;
  393.   u8_t rebind;
  394.   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_bind(ipaddr = "));
  395.   ip_addr_debug_print(UDP_DEBUG, ipaddr);
  396.   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, (", port = %"U16_F")n", port));
  397.   rebind = 0;
  398.   /* Check for double bind and rebind of the same pcb */
  399.   for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
  400.     /* is this UDP PCB already on active list? */
  401.     if (pcb == ipcb) {
  402.       /* pcb may occur at most once in active list */
  403.       LWIP_ASSERT("rebind == 0", rebind == 0);
  404.       /* pcb already in list, just rebind */
  405.       rebind = 1;
  406.     }
  407.     /* this code does not allow upper layer to share a UDP port for
  408.        listening to broadcast or multicast traffic (See SO_REUSE_ADDR and
  409.        SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR
  410.        combine with implementation of UDP PCB flags. Leon Woestenberg. */
  411. #ifdef LWIP_UDP_TODO
  412.     /* port matches that of PCB in list? */
  413.     else
  414.       if ((ipcb->local_port == port) &&
  415.           /* IP address matches, or one is IP_ADDR_ANY? */
  416.           (ip_addr_isany(&(ipcb->local_ip)) ||
  417.            ip_addr_isany(ipaddr) ||
  418.            ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
  419.         /* other PCB already binds to this local IP and port */
  420.         LWIP_DEBUGF(UDP_DEBUG,
  421.                     ("udp_bind: local port %"U16_F" already bound by another pcbn", port));
  422.         return ERR_USE;
  423.       }
  424. #endif
  425.   }
  426.   ip_addr_set(&pcb->local_ip, ipaddr);
  427.   /* no port specified? */
  428.   if (port == 0) {
  429. #ifndef UDP_LOCAL_PORT_RANGE_START
  430. #define UDP_LOCAL_PORT_RANGE_START 4096
  431. #define UDP_LOCAL_PORT_RANGE_END   0x7fff
  432. #endif
  433.     port = UDP_LOCAL_PORT_RANGE_START;
  434.     ipcb = udp_pcbs;
  435.     while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
  436.       if (ipcb->local_port == port) {
  437.         port++;
  438.         ipcb = udp_pcbs;
  439.       } else
  440.         ipcb = ipcb->next;
  441.     }
  442.     if (ipcb != NULL) {
  443.       /* no more ports available in local range */
  444.       LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP portsn"));
  445.       return ERR_USE;
  446.     }
  447.   }
  448.   pcb->local_port = port;
  449.   snmp_insert_udpidx_tree(pcb);
  450.   /* pcb not active yet? */
  451.   if (rebind == 0) {
  452.     /* place the PCB on the active list if not already there */
  453.     pcb->next = udp_pcbs;
  454.     udp_pcbs = pcb;
  455.   }
  456.   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE,
  457.               ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"n",
  458.                (u16_t)(ntohl(pcb->local_ip.addr) >> 24 & 0xff),
  459.                (u16_t)(ntohl(pcb->local_ip.addr) >> 16 & 0xff),
  460.                (u16_t)(ntohl(pcb->local_ip.addr) >> 8 & 0xff),
  461.                (u16_t)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));
  462.   return ERR_OK;
  463. }
  464. /**
  465.  * Connect an UDP PCB.
  466.  *
  467.  * This will associate the UDP PCB with the remote address.
  468.  *
  469.  * @param pcb UDP PCB to be connected with remote address ipaddr and port.
  470.  * @param ipaddr remote IP address to connect with.
  471.  * @param port remote UDP port to connect with.
  472.  *
  473.  * @return lwIP error code
  474.  *
  475.  * @see udp_disconnect()
  476.  */
  477. err_t
  478. udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
  479. {
  480.   struct udp_pcb *ipcb;
  481.   if (pcb->local_port == 0) {
  482.     err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
  483.     if (err != ERR_OK)
  484.       return err;
  485.   }
  486.   ip_addr_set(&pcb->remote_ip, ipaddr);
  487.   pcb->remote_port = port;
  488.   pcb->flags |= UDP_FLAGS_CONNECTED;
  489. /** TODO: this functionality belongs in upper layers */
  490. #ifdef LWIP_UDP_TODO
  491.   /* Nail down local IP for netconn_addr()/getsockname() */
  492.   if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
  493.     struct netif *netif;
  494.     if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
  495.       LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lxn", pcb->remote_ip.addr));
  496.       UDP_STATS_INC(udp.rterr);
  497.       return ERR_RTE;
  498.     }
  499.     /** TODO: this will bind the udp pcb locally, to the interface which
  500.         is used to route output packets to the remote address. However, we
  501.         might want to accept incoming packets on any interface! */
  502.     pcb->local_ip = netif->ip_addr;
  503.   } else if (ip_addr_isany(&pcb->remote_ip)) {
  504.     pcb->local_ip.addr = 0;
  505.   }
  506. #endif
  507.   LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE,
  508.               ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"n",
  509.                (u16_t)(ntohl(pcb->remote_ip.addr) >> 24 & 0xff),
  510.                (u16_t)(ntohl(pcb->remote_ip.addr) >> 16 & 0xff),
  511.                (u16_t)(ntohl(pcb->remote_ip.addr) >> 8 & 0xff),
  512.                (u16_t)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));
  513.   /* Insert UDP PCB into the list of active UDP PCBs. */
  514.   for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
  515.     if (pcb == ipcb) {
  516.       /* already on the list, just return */
  517.       return ERR_OK;
  518.     }
  519.   }
  520.   /* PCB not yet on the list, add PCB now */
  521.   pcb->next = udp_pcbs;
  522.   udp_pcbs = pcb;
  523.   return ERR_OK;
  524. }
  525. void
  526. udp_disconnect(struct udp_pcb *pcb)
  527. {
  528.   /* reset remote address association */
  529.   ip_addr_set(&pcb->remote_ip, IP_ADDR_ANY);
  530.   pcb->remote_port = 0;
  531.   /* mark PCB as unconnected */
  532.   pcb->flags &= ~UDP_FLAGS_CONNECTED;
  533. }
  534. void
  535. udp_recv(struct udp_pcb *pcb,
  536.          void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,
  537.                        struct ip_addr *addr, u16_t port),
  538.          void *recv_arg)
  539. {
  540.   /* remember recv() callback and user data */
  541.   pcb->recv = recv;
  542.   pcb->recv_arg = recv_arg;
  543. }
  544. /**
  545.  * Remove an UDP PCB.
  546.  *
  547.  * @param pcb UDP PCB to be removed. The PCB is removed from the list of
  548.  * UDP PCB's and the data structure is freed from memory.
  549.  *
  550.  * @see udp_new()
  551.  */
  552. void
  553. udp_remove(struct udp_pcb *pcb)
  554. {
  555.   struct udp_pcb *pcb2;
  556.   snmp_delete_udpidx_tree(pcb);
  557.   /* pcb to be removed is first in list? */
  558.   if (udp_pcbs == pcb) {
  559.     /* make list start at 2nd pcb */
  560.     udp_pcbs = udp_pcbs->next;
  561.     /* pcb not 1st in list */
  562.   } else
  563.     for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
  564.       /* find pcb in udp_pcbs list */
  565.       if (pcb2->next != NULL && pcb2->next == pcb) {
  566.         /* remove pcb from list */
  567.         pcb2->next = pcb->next;
  568.       }
  569.     }
  570.   memp_free(MEMP_UDP_PCB, pcb);
  571. }
  572. /**
  573.  * Create a UDP PCB.
  574.  *
  575.  * @return The UDP PCB which was created. NULL if the PCB data structure
  576.  * could not be allocated.
  577.  *
  578.  * @see udp_remove()
  579.  */
  580. struct udp_pcb *
  581. udp_new(void)
  582. {
  583.   struct udp_pcb *pcb;
  584.   pcb = memp_malloc(MEMP_UDP_PCB);
  585.   /* could allocate UDP PCB? */
  586.   if (pcb != NULL) {
  587.     /* initialize PCB to all zeroes */
  588.     memset(pcb, 0, sizeof(struct udp_pcb));
  589.     pcb->ttl = UDP_TTL;
  590.   }
  591.   return pcb;
  592. }
  593. #if UDP_DEBUG
  594. void
  595. udp_debug_print(struct udp_hdr *udphdr)
  596. {
  597.   LWIP_DEBUGF(UDP_DEBUG, ("UDP header:n"));
  598.   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+n"));
  599.   LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     %5"U16_F"     | (src port, dest port)n",
  600.                           ntohs(udphdr->src), ntohs(udphdr->dest)));
  601.   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+n"));
  602.   LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     0x%04"X16_F"    | (len, chksum)n",
  603.                           ntohs(udphdr->len), ntohs(udphdr->chksum)));
  604.   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+n"));
  605. }
  606. #endif /* UDP_DEBUG */
  607. #endif /* LWIP_UDP */