Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested

Dependents:   STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers udp.c Source File

udp.c

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * User Datagram Protocol module
00004  *
00005  */
00006 
00007 /*
00008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00009  * All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without modification,
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  *
00035  * Author: Adam Dunkels <adam@sics.se>
00036  *
00037  */
00038 
00039 
00040 /* udp.c
00041  *
00042  * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828).
00043  *
00044  */
00045 
00046 /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'!
00047  */
00048 
00049 #include "lwip/opt.h"
00050 
00051 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
00052 
00053 #include "lwip/udp.h"
00054 #include "lwip/def.h"
00055 #include "lwip/memp.h"
00056 #include "lwip/inet_chksum.h"
00057 #include "lwip/ip_addr.h"
00058 #include "lwip/netif.h"
00059 #include "lwip/icmp.h"
00060 #include "lwip/stats.h"
00061 #include "lwip/snmp.h"
00062 #include "arch/perf.h"
00063 #include "lwip/dhcp.h "
00064 
00065 #include <string.h>
00066 
00067 #ifndef UDP_LOCAL_PORT_RANGE_START
00068 /* From http://www.iana.org/assignments/port-numbers:
00069    "The Dynamic and/or Private Ports are those from 49152 through 65535" */
00070 #define UDP_LOCAL_PORT_RANGE_START  0xc000
00071 #define UDP_LOCAL_PORT_RANGE_END    0xffff
00072 #define UDP_ENSURE_LOCAL_PORT_RANGE(port) (((port) & ~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START)
00073 #endif
00074 
00075 /* last local UDP port */
00076 static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
00077 
00078 /* The list of UDP PCBs */
00079 /* exported in udp.h (was static) */
00080 struct udp_pcb *udp_pcbs;
00081 
00082 /**
00083  * Initialize this module.
00084  */
00085 void
00086 udp_init(void)
00087 {
00088 #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
00089   udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
00090 #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */
00091 }
00092 
00093 /**
00094  * Allocate a new local UDP port.
00095  *
00096  * @return a new (free) local UDP port number
00097  */
00098 static u16_t
00099 udp_new_port(void)
00100 {
00101   u16_t n = 0;
00102   struct udp_pcb *pcb;
00103   
00104 again:
00105   if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) {
00106     udp_port = UDP_LOCAL_PORT_RANGE_START;
00107   }
00108   /* Check all PCBs. */
00109   for(pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
00110     if (pcb->local_port == udp_port) {
00111       if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) {
00112         return 0;
00113       }
00114       goto again;
00115     }
00116   }
00117   return udp_port;
00118 #if 0
00119   struct udp_pcb *ipcb = udp_pcbs;
00120   while ((ipcb != NULL) && (udp_port != UDP_LOCAL_PORT_RANGE_END)) {
00121     if (ipcb->local_port == udp_port) {
00122       /* port is already used by another udp_pcb */
00123       udp_port++;
00124       /* restart scanning all udp pcbs */
00125       ipcb = udp_pcbs;
00126     } else {
00127       /* go on with next udp pcb */
00128       ipcb = ipcb->next;
00129     }
00130   }
00131   if (ipcb != NULL) {
00132     return 0;
00133   }
00134   return udp_port;
00135 #endif
00136 }
00137 
00138 /**
00139  * Process an incoming UDP datagram.
00140  *
00141  * Given an incoming UDP datagram (as a chain of pbufs) this function
00142  * finds a corresponding UDP PCB and hands over the pbuf to the pcbs
00143  * recv function. If no pcb is found or the datagram is incorrect, the
00144  * pbuf is freed.
00145  *
00146  * @param p pbuf to be demultiplexed to a UDP PCB.
00147  * @param inp network interface on which the datagram was received.
00148  *
00149  */
00150 void
00151 udp_input(struct pbuf *p, struct netif *inp)
00152 {
00153   struct udp_hdr *udphdr;
00154   struct udp_pcb *pcb, *prev;
00155   struct udp_pcb *uncon_pcb;
00156   struct ip_hdr *iphdr;
00157   u16_t src, dest;
00158   u8_t local_match;
00159   u8_t broadcast;
00160 
00161   PERF_START;
00162 
00163   UDP_STATS_INC(udp.recv);
00164 
00165   iphdr = (struct ip_hdr *)p->payload;
00166 
00167   /* Check minimum length (IP header + UDP header)
00168    * and move payload pointer to UDP header */
00169   if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN) || pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4))) {
00170     /* drop short packets */
00171     LWIP_DEBUGF(UDP_DEBUG,
00172                 ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
00173     UDP_STATS_INC(udp.lenerr);
00174     UDP_STATS_INC(udp.drop);
00175     snmp_inc_udpinerrors();
00176     pbuf_free(p);
00177     goto end;
00178   }
00179 
00180   udphdr = (struct udp_hdr *)p->payload;
00181 
00182   /* is broadcast packet ? */
00183   broadcast = ip_addr_isbroadcast(&current_iphdr_dest, inp);
00184 
00185   LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
00186 
00187   /* convert src and dest ports to host byte order */
00188   src = ntohs(udphdr->src);
00189   dest = ntohs(udphdr->dest);
00190 
00191   udp_debug_print(udphdr);
00192 
00193   /* print the UDP source and destination */
00194   LWIP_DEBUGF(UDP_DEBUG,
00195               ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
00196                "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
00197                ip4_addr1_16(&iphdr->dest), ip4_addr2_16(&iphdr->dest),
00198                ip4_addr3_16(&iphdr->dest), ip4_addr4_16(&iphdr->dest), ntohs(udphdr->dest),
00199                ip4_addr1_16(&iphdr->src), ip4_addr2_16(&iphdr->src),
00200                ip4_addr3_16(&iphdr->src), ip4_addr4_16(&iphdr->src), ntohs(udphdr->src)));
00201 
00202 #if LWIP_DHCP
00203   pcb = NULL;
00204   /* when LWIP_DHCP is active, packets to DHCP_CLIENT_PORT may only be processed by
00205      the dhcp module, no other UDP pcb may use the local UDP port DHCP_CLIENT_PORT */
00206   if (dest == DHCP_CLIENT_PORT) {
00207     /* all packets for DHCP_CLIENT_PORT not coming from DHCP_SERVER_PORT are dropped! */
00208     if (src == DHCP_SERVER_PORT) {
00209       if ((inp->dhcp != NULL) && (inp->dhcp->pcb != NULL)) {
00210         /* accept the packe if 
00211            (- broadcast or directed to us) -> DHCP is link-layer-addressed, local ip is always ANY!
00212            - inp->dhcp->pcb->remote == ANY or iphdr->src */
00213         if ((ip_addr_isany(&inp->dhcp->pcb->remote_ip) ||
00214            ip_addr_cmp(&(inp->dhcp->pcb->remote_ip), &current_iphdr_src))) {
00215           pcb = inp->dhcp->pcb;
00216         }
00217       }
00218     }
00219   } else
00220 #endif /* LWIP_DHCP */
00221   {
00222     prev = NULL;
00223     local_match = 0;
00224     uncon_pcb = NULL;
00225     /* Iterate through the UDP pcb list for a matching pcb.
00226      * 'Perfect match' pcbs (connected to the remote port & ip address) are
00227      * preferred. If no perfect match is found, the first unconnected pcb that
00228      * matches the local port and ip address gets the datagram. */
00229     for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
00230       local_match = 0;
00231       /* print the PCB local and remote address */
00232       LWIP_DEBUGF(UDP_DEBUG,
00233                   ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "
00234                    "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
00235                    ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),
00236                    ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip), pcb->local_port,
00237                    ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
00238                    ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip), pcb->remote_port));
00239 
00240       /* compare PCB local addr+port to UDP destination addr+port */
00241       if (pcb->local_port == dest) {
00242         if (
00243            (!broadcast && ip_addr_isany(&pcb->local_ip)) ||
00244            ip_addr_cmp(&(pcb->local_ip), &current_iphdr_dest) ||
00245 #if LWIP_IGMP
00246            ip_addr_ismulticast(&current_iphdr_dest) ||
00247 #endif /* LWIP_IGMP */
00248 #if IP_SOF_BROADCAST_RECV
00249             (broadcast && ip_get_option(pcb, SOF_BROADCAST) &&
00250              (ip_addr_isany(&pcb->local_ip) ||
00251               ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), &inp->netmask)))) {
00252 #else /* IP_SOF_BROADCAST_RECV */
00253             (broadcast &&
00254              (ip_addr_isany(&pcb->local_ip) ||
00255               ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), &inp->netmask)))) {
00256 #endif /* IP_SOF_BROADCAST_RECV */ 
00257           local_match = 1;
00258           if ((uncon_pcb == NULL) && 
00259               ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
00260             /* the first unconnected matching PCB */
00261             uncon_pcb = pcb;
00262           }
00263         }
00264       }
00265       /* compare PCB remote addr+port to UDP source addr+port */
00266       if ((local_match != 0) &&
00267           (pcb->remote_port == src) &&
00268           (ip_addr_isany(&pcb->remote_ip) ||
00269            ip_addr_cmp(&(pcb->remote_ip), &current_iphdr_src))) {
00270         /* the first fully matching PCB */
00271         if (prev != NULL) {
00272           /* move the pcb to the front of udp_pcbs so that is
00273              found faster next time */
00274           prev->next = pcb->next;
00275           pcb->next = udp_pcbs;
00276           udp_pcbs = pcb;
00277         } else {
00278           UDP_STATS_INC(udp.cachehit);
00279         }
00280         break;
00281       }
00282       prev = pcb;
00283     }
00284     /* no fully matching pcb found? then look for an unconnected pcb */
00285     if (pcb == NULL) {
00286       pcb = uncon_pcb;
00287     }
00288   }
00289 
00290   /* Check checksum if this is a match or if it was directed at us. */
00291   if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &current_iphdr_dest)) {
00292     LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
00293 #if LWIP_UDPLITE
00294     if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
00295       /* Do the UDP Lite checksum */
00296 #if CHECKSUM_CHECK_UDP
00297       u16_t chklen = ntohs(udphdr->len);
00298       if (chklen < sizeof(struct udp_hdr)) {
00299         if (chklen == 0) {
00300           /* For UDP-Lite, checksum length of 0 means checksum
00301              over the complete packet (See RFC 3828 chap. 3.1) */
00302           chklen = p->tot_len;
00303         } else {
00304           /* At least the UDP-Lite header must be covered by the
00305              checksum! (Again, see RFC 3828 chap. 3.1) */
00306           UDP_STATS_INC(udp.chkerr);
00307           UDP_STATS_INC(udp.drop);
00308           snmp_inc_udpinerrors();
00309           pbuf_free(p);
00310           goto end;
00311         }
00312       }
00313       if (inet_chksum_pseudo_partial(p, &current_iphdr_src, &current_iphdr_dest,
00314                              IP_PROTO_UDPLITE, p->tot_len, chklen) != 0) {
00315        LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00316                    ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
00317         UDP_STATS_INC(udp.chkerr);
00318         UDP_STATS_INC(udp.drop);
00319         snmp_inc_udpinerrors();
00320         pbuf_free(p);
00321         goto end;
00322       }
00323 #endif /* CHECKSUM_CHECK_UDP */
00324     } else
00325 #endif /* LWIP_UDPLITE */
00326     {
00327 #if CHECKSUM_CHECK_UDP
00328       if (udphdr->chksum != 0) {
00329         if (inet_chksum_pseudo(p, ip_current_src_addr(), ip_current_dest_addr(),
00330                                IP_PROTO_UDP, p->tot_len) != 0) {
00331           LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00332                       ("udp_input: UDP datagram discarded due to failing checksum\n"));
00333           UDP_STATS_INC(udp.chkerr);
00334           UDP_STATS_INC(udp.drop);
00335           snmp_inc_udpinerrors();
00336           pbuf_free(p);
00337           goto end;
00338         }
00339       }
00340 #endif /* CHECKSUM_CHECK_UDP */
00341     }
00342     if(pbuf_header(p, -UDP_HLEN)) {
00343       /* Can we cope with this failing? Just assert for now */
00344       LWIP_ASSERT("pbuf_header failed\n", 0);
00345       UDP_STATS_INC(udp.drop);
00346       snmp_inc_udpinerrors();
00347       pbuf_free(p);
00348       goto end;
00349     }
00350     if (pcb != NULL) {
00351       snmp_inc_udpindatagrams();
00352 #if SO_REUSE && SO_REUSE_RXTOALL
00353       if ((broadcast || ip_addr_ismulticast(&current_iphdr_dest)) &&
00354           ip_get_option(pcb, SOF_REUSEADDR)) {
00355         /* pass broadcast- or multicast packets to all multicast pcbs
00356            if SOF_REUSEADDR is set on the first match */
00357         struct udp_pcb *mpcb;
00358         u8_t p_header_changed = 0;
00359         for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
00360           if (mpcb != pcb) {
00361             /* compare PCB local addr+port to UDP destination addr+port */
00362             if ((mpcb->local_port == dest) &&
00363                 ((!broadcast && ip_addr_isany(&mpcb->local_ip)) ||
00364                  ip_addr_cmp(&(mpcb->local_ip), &current_iphdr_dest) ||
00365 #if LWIP_IGMP
00366                  ip_addr_ismulticast(&current_iphdr_dest) ||
00367 #endif /* LWIP_IGMP */
00368 #if IP_SOF_BROADCAST_RECV
00369                  (broadcast && ip_get_option(mpcb, SOF_BROADCAST)))) {
00370 #else  /* IP_SOF_BROADCAST_RECV */
00371                  (broadcast))) {
00372 #endif /* IP_SOF_BROADCAST_RECV */
00373               /* pass a copy of the packet to all local matches */
00374               if (mpcb->recv != NULL) {
00375                 struct pbuf *q;
00376                 /* for that, move payload to IP header again */
00377                 if (p_header_changed == 0) {
00378                   pbuf_header(p, (s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN));
00379                   p_header_changed = 1;
00380                 }
00381                 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
00382                 if (q != NULL) {
00383                   err_t err = pbuf_copy(q, p);
00384                   if (err == ERR_OK) {
00385                     /* move payload to UDP data */
00386                     pbuf_header(q, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN));
00387                     mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src);
00388                   }
00389                 }
00390               }
00391             }
00392           }
00393         }
00394         if (p_header_changed) {
00395           /* and move payload to UDP data again */
00396           pbuf_header(p, -(s16_t)((IPH_HL(iphdr) * 4) + UDP_HLEN));
00397         }
00398       }
00399 #endif /* SO_REUSE && SO_REUSE_RXTOALL */
00400       /* callback */
00401       if (pcb->recv != NULL) {
00402         /* now the recv function is responsible for freeing p */
00403         pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src);
00404       } else {
00405         /* no recv function registered? then we have to free the pbuf! */
00406         pbuf_free(p);
00407         goto end;
00408       }
00409     } else {
00410       LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
00411 
00412 #if LWIP_ICMP
00413       /* No match was found, send ICMP destination port unreachable unless
00414          destination address was broadcast/multicast. */
00415       if (!broadcast &&
00416           !ip_addr_ismulticast(&current_iphdr_dest)) {
00417         /* move payload pointer back to ip header */
00418         pbuf_header(p, (IPH_HL(iphdr) * 4) + UDP_HLEN);
00419         LWIP_ASSERT("p->payload == iphdr", (p->payload == iphdr));
00420         icmp_dest_unreach(p, ICMP_DUR_PORT);
00421       }
00422 #endif /* LWIP_ICMP */
00423       UDP_STATS_INC(udp.proterr);
00424       UDP_STATS_INC(udp.drop);
00425       snmp_inc_udpnoports();
00426       pbuf_free(p);
00427     }
00428   } else {
00429     pbuf_free(p);
00430   }
00431 end:
00432   PERF_STOP("udp_input");
00433 }
00434 
00435 /**
00436  * Send data using UDP.
00437  *
00438  * @param pcb UDP PCB used to send the data.
00439  * @param p chain of pbuf's to be sent.
00440  *
00441  * The datagram will be sent to the current remote_ip & remote_port
00442  * stored in pcb. If the pcb is not bound to a port, it will
00443  * automatically be bound to a random port.
00444  *
00445  * @return lwIP error code.
00446  * - ERR_OK. Successful. No error occured.
00447  * - ERR_MEM. Out of memory.
00448  * - ERR_RTE. Could not find route to destination address.
00449  * - More errors could be returned by lower protocol layers.
00450  *
00451  * @see udp_disconnect() udp_sendto()
00452  */
00453 err_t
00454 udp_send(struct udp_pcb *pcb, struct pbuf *p)
00455 {
00456   /* send to the packet using remote ip and port stored in the pcb */
00457   return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
00458 }
00459 
00460 #if LWIP_CHECKSUM_ON_COPY
00461 /** Same as udp_send() but with checksum
00462  */
00463 err_t
00464 udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
00465                 u8_t have_chksum, u16_t chksum)
00466 {
00467   /* send to the packet using remote ip and port stored in the pcb */
00468   return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
00469     have_chksum, chksum);
00470 }
00471 #endif /* LWIP_CHECKSUM_ON_COPY */
00472 
00473 /**
00474  * Send data to a specified address using UDP.
00475  *
00476  * @param pcb UDP PCB used to send the data.
00477  * @param p chain of pbuf's to be sent.
00478  * @param dst_ip Destination IP address.
00479  * @param dst_port Destination UDP port.
00480  *
00481  * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
00482  *
00483  * If the PCB already has a remote address association, it will
00484  * be restored after the data is sent.
00485  * 
00486  * @return lwIP error code (@see udp_send for possible error codes)
00487  *
00488  * @see udp_disconnect() udp_send()
00489  */
00490 err_t
00491 udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
00492   ip_addr_t *dst_ip, u16_t dst_port)
00493 {
00494 #if LWIP_CHECKSUM_ON_COPY
00495   return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
00496 }
00497 
00498 /** Same as udp_sendto(), but with checksum */
00499 err_t
00500 udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
00501                   u16_t dst_port, u8_t have_chksum, u16_t chksum)
00502 {
00503 #endif /* LWIP_CHECKSUM_ON_COPY */
00504   struct netif *netif;
00505 
00506   LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
00507 
00508   /* find the outgoing network interface for this packet */
00509 #if LWIP_IGMP
00510   netif = ip_route((ip_addr_ismulticast(dst_ip))?(&(pcb->multicast_ip)):(dst_ip));
00511 #else
00512   netif = ip_route(dst_ip);
00513 #endif /* LWIP_IGMP */
00514 
00515   /* no outgoing network interface could be found? */
00516   if (netif == NULL) {
00517     LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
00518       ip4_addr1_16(dst_ip), ip4_addr2_16(dst_ip), ip4_addr3_16(dst_ip), ip4_addr4_16(dst_ip)));
00519     UDP_STATS_INC(udp.rterr);
00520     return ERR_RTE;
00521   }
00522 #if LWIP_CHECKSUM_ON_COPY
00523   return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
00524 #else /* LWIP_CHECKSUM_ON_COPY */
00525   return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
00526 #endif /* LWIP_CHECKSUM_ON_COPY */
00527 }
00528 
00529 /**
00530  * Send data to a specified address using UDP.
00531  * The netif used for sending can be specified.
00532  *
00533  * This function exists mainly for DHCP, to be able to send UDP packets
00534  * on a netif that is still down.
00535  *
00536  * @param pcb UDP PCB used to send the data.
00537  * @param p chain of pbuf's to be sent.
00538  * @param dst_ip Destination IP address.
00539  * @param dst_port Destination UDP port.
00540  * @param netif the netif used for sending.
00541  *
00542  * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
00543  *
00544  * @return lwIP error code (@see udp_send for possible error codes)
00545  *
00546  * @see udp_disconnect() udp_send()
00547  */
00548 err_t
00549 udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
00550   ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
00551 {
00552 #if LWIP_CHECKSUM_ON_COPY
00553   return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
00554 }
00555 
00556 /** Same as udp_sendto_if(), but with checksum */
00557 err_t
00558 udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *dst_ip,
00559                      u16_t dst_port, struct netif *netif, u8_t have_chksum,
00560                      u16_t chksum)
00561 {
00562 #endif /* LWIP_CHECKSUM_ON_COPY */
00563   struct udp_hdr *udphdr;
00564   ip_addr_t *src_ip;
00565   err_t err;
00566   struct pbuf *q; /* q will be sent down the stack */
00567 
00568 #if IP_SOF_BROADCAST
00569   /* broadcast filter? */
00570   if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) {
00571     LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
00572       ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
00573     return ERR_VAL;
00574   }
00575 #endif /* IP_SOF_BROADCAST */
00576 
00577   /* if the PCB is not yet bound to a port, bind it here */
00578   if (pcb->local_port == 0) {
00579     LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
00580     err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
00581     if (err != ERR_OK) {
00582       LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
00583       return err;
00584     }
00585   }
00586 
00587   /* not enough space to add an UDP header to first pbuf in given p chain? */
00588   if (pbuf_header(p, UDP_HLEN)) {
00589     /* allocate header in a separate new pbuf */
00590     q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
00591     /* new header pbuf could not be allocated? */
00592     if (q == NULL) {
00593       LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
00594       return ERR_MEM;
00595     }
00596     if (p->tot_len != 0) {
00597       /* chain header q in front of given pbuf p (only if p contains data) */
00598       pbuf_chain(q, p);
00599     }
00600     /* first pbuf q points to header pbuf */
00601     LWIP_DEBUGF(UDP_DEBUG,
00602                 ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
00603   } else {
00604     /* adding space for header within p succeeded */
00605     /* first pbuf q equals given pbuf */
00606     q = p;
00607     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
00608   }
00609   LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
00610               (q->len >= sizeof(struct udp_hdr)));
00611   /* q now represents the packet to be sent */
00612   udphdr = (struct udp_hdr *)q->payload;
00613   udphdr->src = htons(pcb->local_port);
00614   udphdr->dest = htons(dst_port);
00615   /* in UDP, 0 checksum means 'no checksum' */
00616   udphdr->chksum = 0x0000; 
00617 
00618   /* Multicast Loop? */
00619 #if LWIP_IGMP
00620   if (ip_addr_ismulticast(dst_ip) && ((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0)) {
00621     q->flags |= PBUF_FLAG_MCASTLOOP;
00622   }
00623 #endif /* LWIP_IGMP */
00624 
00625 
00626   /* PCB local address is IP_ANY_ADDR? */
00627   if (ip_addr_isany(&pcb->local_ip)) {
00628     /* use outgoing network interface IP address as source address */
00629     src_ip = &(netif->ip_addr);
00630   } else {
00631     /* check if UDP PCB local IP address is correct
00632      * this could be an old address if netif->ip_addr has changed */
00633     if (!ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
00634       /* local_ip doesn't match, drop the packet */
00635       if (q != p) {
00636         /* free the header pbuf */
00637         pbuf_free(q);
00638         q = NULL;
00639         /* p is still referenced by the caller, and will live on */
00640       }
00641       return ERR_VAL;
00642     }
00643     /* use UDP PCB local IP address as source address */
00644     src_ip = &(pcb->local_ip);
00645   }
00646 
00647   LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
00648 
00649 #if LWIP_UDPLITE
00650   /* UDP Lite protocol? */
00651   if (pcb->flags & UDP_FLAGS_UDPLITE) {
00652     u16_t chklen, chklen_hdr;
00653     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
00654     /* set UDP message length in UDP header */
00655     chklen_hdr = chklen = pcb->chksum_len_tx;
00656     if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
00657       if (chklen != 0) {
00658         LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
00659       }
00660       /* For UDP-Lite, checksum length of 0 means checksum
00661          over the complete packet. (See RFC 3828 chap. 3.1)
00662          At least the UDP-Lite header must be covered by the
00663          checksum, therefore, if chksum_len has an illegal
00664          value, we generate the checksum over the complete
00665          packet to be safe. */
00666       chklen_hdr = 0;
00667       chklen = q->tot_len;
00668     }
00669     udphdr->len = htons(chklen_hdr);
00670     /* calculate checksum */
00671 #if CHECKSUM_GEN_UDP
00672     udphdr->chksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip,
00673       IP_PROTO_UDPLITE, q->tot_len,
00674 #if !LWIP_CHECKSUM_ON_COPY
00675       chklen);
00676 #else /* !LWIP_CHECKSUM_ON_COPY */
00677       (have_chksum ? UDP_HLEN : chklen));
00678     if (have_chksum) {
00679       u32_t acc;
00680       acc = udphdr->chksum + (u16_t)~(chksum);
00681       udphdr->chksum = FOLD_U32T(acc);
00682     }
00683 #endif /* !LWIP_CHECKSUM_ON_COPY */
00684 
00685     /* chksum zero must become 0xffff, as zero means 'no checksum' */
00686     if (udphdr->chksum == 0x0000) {
00687       udphdr->chksum = 0xffff;
00688     }
00689 #endif /* CHECKSUM_GEN_UDP */
00690     /* output to IP */
00691     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
00692     NETIF_SET_HWADDRHINT(netif, &pcb->addr_hint);
00693     err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);
00694     NETIF_SET_HWADDRHINT(netif, NULL);
00695   } else
00696 #endif /* LWIP_UDPLITE */
00697   {      /* UDP */
00698     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
00699     udphdr->len = htons(q->tot_len);
00700     /* calculate checksum */
00701 #if CHECKSUM_GEN_UDP
00702     if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
00703       u16_t udpchksum;
00704 #if LWIP_CHECKSUM_ON_COPY
00705       if (have_chksum) {
00706         u32_t acc;
00707         udpchksum = inet_chksum_pseudo_partial(q, src_ip, dst_ip, IP_PROTO_UDP,
00708           q->tot_len, UDP_HLEN);
00709         acc = udpchksum + (u16_t)~(chksum);
00710         udpchksum = FOLD_U32T(acc);
00711       } else
00712 #endif /* LWIP_CHECKSUM_ON_COPY */
00713       {
00714         udpchksum = inet_chksum_pseudo(q, src_ip, dst_ip, IP_PROTO_UDP, q->tot_len);
00715       }
00716 
00717       /* chksum zero must become 0xffff, as zero means 'no checksum' */
00718       if (udpchksum == 0x0000) {
00719         udpchksum = 0xffff;
00720       }
00721       udphdr->chksum = udpchksum;
00722     }
00723 #endif /* CHECKSUM_GEN_UDP */
00724     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
00725     LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
00726     /* output to IP */
00727     NETIF_SET_HWADDRHINT(netif, &pcb->addr_hint);
00728     err = ip_output_if(q, src_ip, dst_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);
00729     NETIF_SET_HWADDRHINT(netif, NULL);
00730   }
00731   /* TODO: must this be increased even if error occured? */
00732   snmp_inc_udpoutdatagrams();
00733 
00734   /* did we chain a separate header pbuf earlier? */
00735   if (q != p) {
00736     /* free the header pbuf */
00737     pbuf_free(q);
00738     q = NULL;
00739     /* p is still referenced by the caller, and will live on */
00740   }
00741 
00742   UDP_STATS_INC(udp.xmit);
00743   return err;
00744 }
00745 
00746 /**
00747  * Bind an UDP PCB.
00748  *
00749  * @param pcb UDP PCB to be bound with a local address ipaddr and port.
00750  * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
00751  * bind to all local interfaces.
00752  * @param port local UDP port to bind with. Use 0 to automatically bind
00753  * to a random port between UDP_LOCAL_PORT_RANGE_START and
00754  * UDP_LOCAL_PORT_RANGE_END.
00755  *
00756  * ipaddr & port are expected to be in the same byte order as in the pcb.
00757  *
00758  * @return lwIP error code.
00759  * - ERR_OK. Successful. No error occured.
00760  * - ERR_USE. The specified ipaddr and port are already bound to by
00761  * another UDP PCB.
00762  *
00763  * @see udp_disconnect()
00764  */
00765 err_t
00766 udp_bind(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
00767 {
00768   struct udp_pcb *ipcb;
00769   u8_t rebind;
00770 
00771   LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
00772   ip_addr_debug_print(UDP_DEBUG, ipaddr);
00773   LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
00774 
00775   rebind = 0;
00776   /* Check for double bind and rebind of the same pcb */
00777   for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
00778     /* is this UDP PCB already on active list? */
00779     if (pcb == ipcb) {
00780       /* pcb may occur at most once in active list */
00781       LWIP_ASSERT("rebind == 0", rebind == 0);
00782       /* pcb already in list, just rebind */
00783       rebind = 1;
00784     }
00785 
00786     /* By default, we don't allow to bind to a port that any other udp
00787        PCB is alread bound to, unless *all* PCBs with that port have tha
00788        REUSEADDR flag set. */
00789 #if SO_REUSE
00790     else if (!ip_get_option(pcb, SOF_REUSEADDR) &&
00791              !ip_get_option(ipcb, SOF_REUSEADDR)) {
00792 #else /* SO_REUSE */
00793     /* port matches that of PCB in list and REUSEADDR not set -> reject */
00794     else {
00795 #endif /* SO_REUSE */
00796       if ((ipcb->local_port == port) &&
00797           /* IP address matches, or one is IP_ADDR_ANY? */
00798           (ip_addr_isany(&(ipcb->local_ip)) ||
00799            ip_addr_isany(ipaddr) ||
00800            ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
00801         /* other PCB already binds to this local IP and port */
00802         LWIP_DEBUGF(UDP_DEBUG,
00803                     ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
00804         return ERR_USE;
00805       }
00806     }
00807   }
00808 
00809   ip_addr_set(&pcb->local_ip, ipaddr);
00810 
00811   /* no port specified? */
00812   if (port == 0) {
00813     port = udp_new_port();
00814     if (port == 0) {
00815       /* no more ports available in local range */
00816       LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
00817       return ERR_USE;
00818     }
00819   }
00820   pcb->local_port = port;
00821   snmp_insert_udpidx_tree(pcb);
00822   /* pcb not active yet? */
00823   if (rebind == 0) {
00824     /* place the PCB on the active list if not already there */
00825     pcb->next = udp_pcbs;
00826     udp_pcbs = pcb;
00827   }
00828   LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
00829               ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",
00830                ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),
00831                ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip),
00832                pcb->local_port));
00833   return ERR_OK;
00834 }
00835 /**
00836  * Connect an UDP PCB.
00837  *
00838  * This will associate the UDP PCB with the remote address.
00839  *
00840  * @param pcb UDP PCB to be connected with remote address ipaddr and port.
00841  * @param ipaddr remote IP address to connect with.
00842  * @param port remote UDP port to connect with.
00843  *
00844  * @return lwIP error code
00845  *
00846  * ipaddr & port are expected to be in the same byte order as in the pcb.
00847  *
00848  * The udp pcb is bound to a random local port if not already bound.
00849  *
00850  * @see udp_disconnect()
00851  */
00852 err_t
00853 udp_connect(struct udp_pcb *pcb, ip_addr_t *ipaddr, u16_t port)
00854 {
00855   struct udp_pcb *ipcb;
00856 
00857   if (pcb->local_port == 0) {
00858     err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
00859     if (err != ERR_OK) {
00860       return err;
00861     }
00862   }
00863 
00864   ip_addr_set(&pcb->remote_ip, ipaddr);
00865   pcb->remote_port = port;
00866   pcb->flags |= UDP_FLAGS_CONNECTED;
00867 /** TODO: this functionality belongs in upper layers */
00868 #ifdef LWIP_UDP_TODO
00869   /* Nail down local IP for netconn_addr()/getsockname() */
00870   if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
00871     struct netif *netif;
00872 
00873     if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
00874       LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
00875       UDP_STATS_INC(udp.rterr);
00876       return ERR_RTE;
00877     }
00878     /** TODO: this will bind the udp pcb locally, to the interface which
00879         is used to route output packets to the remote address. However, we
00880         might want to accept incoming packets on any interface! */
00881     pcb->local_ip = netif->ip_addr;
00882   } else if (ip_addr_isany(&pcb->remote_ip)) {
00883     pcb->local_ip.addr = 0;
00884   }
00885 #endif
00886   LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
00887               ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",
00888                ip4_addr1_16(&pcb->local_ip), ip4_addr2_16(&pcb->local_ip),
00889                ip4_addr3_16(&pcb->local_ip), ip4_addr4_16(&pcb->local_ip),
00890                pcb->local_port));
00891 
00892   /* Insert UDP PCB into the list of active UDP PCBs. */
00893   for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
00894     if (pcb == ipcb) {
00895       /* already on the list, just return */
00896       return ERR_OK;
00897     }
00898   }
00899   /* PCB not yet on the list, add PCB now */
00900   pcb->next = udp_pcbs;
00901   udp_pcbs = pcb;
00902   return ERR_OK;
00903 }
00904 
00905 /**
00906  * Disconnect a UDP PCB
00907  *
00908  * @param pcb the udp pcb to disconnect.
00909  */
00910 void
00911 udp_disconnect(struct udp_pcb *pcb)
00912 {
00913   /* reset remote address association */
00914   ip_addr_set_any(&pcb->remote_ip);
00915   pcb->remote_port = 0;
00916   /* mark PCB as unconnected */
00917   pcb->flags &= ~UDP_FLAGS_CONNECTED;
00918 }
00919 
00920 /**
00921  * Set a receive callback for a UDP PCB
00922  *
00923  * This callback will be called when receiving a datagram for the pcb.
00924  *
00925  * @param pcb the pcb for wich to set the recv callback
00926  * @param recv function pointer of the callback function
00927  * @param recv_arg additional argument to pass to the callback function
00928  */
00929 void
00930 udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
00931 {
00932   /* remember recv() callback and user data */
00933   pcb->recv = recv;
00934   pcb->recv_arg = recv_arg;
00935 }
00936 
00937 /**
00938  * Remove an UDP PCB.
00939  *
00940  * @param pcb UDP PCB to be removed. The PCB is removed from the list of
00941  * UDP PCB's and the data structure is freed from memory.
00942  *
00943  * @see udp_new()
00944  */
00945 void
00946 udp_remove(struct udp_pcb *pcb)
00947 {
00948   struct udp_pcb *pcb2;
00949 
00950   snmp_delete_udpidx_tree(pcb);
00951   /* pcb to be removed is first in list? */
00952   if (udp_pcbs == pcb) {
00953     /* make list start at 2nd pcb */
00954     udp_pcbs = udp_pcbs->next;
00955     /* pcb not 1st in list */
00956   } else {
00957     for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
00958       /* find pcb in udp_pcbs list */
00959       if (pcb2->next != NULL && pcb2->next == pcb) {
00960         /* remove pcb from list */
00961         pcb2->next = pcb->next;
00962       }
00963     }
00964   }
00965   memp_free(MEMP_UDP_PCB, pcb);
00966 }
00967 
00968 /**
00969  * Create a UDP PCB.
00970  *
00971  * @return The UDP PCB which was created. NULL if the PCB data structure
00972  * could not be allocated.
00973  *
00974  * @see udp_remove()
00975  */
00976 struct udp_pcb *
00977 udp_new(void)
00978 {
00979   struct udp_pcb *pcb;
00980   pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);
00981   /* could allocate UDP PCB? */
00982   if (pcb != NULL) {
00983     /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
00984      * which means checksum is generated over the whole datagram per default
00985      * (recommended as default by RFC 3828). */
00986     /* initialize PCB to all zeroes */
00987     memset(pcb, 0, sizeof(struct udp_pcb));
00988     pcb->ttl = UDP_TTL;
00989   }
00990   return pcb;
00991 }
00992 
00993 #if UDP_DEBUG
00994 /**
00995  * Print UDP header information for debug purposes.
00996  *
00997  * @param udphdr pointer to the udp header in memory.
00998  */
00999 void
01000 udp_debug_print(struct udp_hdr *udphdr)
01001 {
01002   LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
01003   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
01004   LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     %5"U16_F"     | (src port, dest port)\n",
01005                           ntohs(udphdr->src), ntohs(udphdr->dest)));
01006   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
01007   LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     0x%04"X16_F"    | (len, chksum)\n",
01008                           ntohs(udphdr->len), ntohs(udphdr->chksum)));
01009   LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
01010 }
01011 #endif /* UDP_DEBUG */
01012 
01013 #endif /* LWIP_UDP */