udp.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /**
  2. * @file
  3. * User Datagram Protocol module\n
  4. * The code for the User Datagram Protocol UDP & UDPLite (RFC 3828).\n
  5. * See also @ref udp_raw
  6. *
  7. * @defgroup udp_raw UDP
  8. * @ingroup callbackstyle_api
  9. * User Datagram Protocol module\n
  10. * @see @ref raw_api and @ref netconn
  11. */
  12. /*
  13. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. *
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. The name of the author may not be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  29. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  30. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  32. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  35. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  36. * OF SUCH DAMAGE.
  37. *
  38. * This file is part of the lwIP TCP/IP stack.
  39. *
  40. * Author: Adam Dunkels <adam@sics.se>
  41. *
  42. */
  43. /* @todo Check the use of '(struct udp_pcb).chksum_len_rx'!
  44. */
  45. #include "lwip/opt.h"
  46. #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
  47. #include "lwip/udp.h"
  48. #include "lwip/def.h"
  49. #include "lwip/memp.h"
  50. #include "lwip/inet_chksum.h"
  51. #include "lwip/ip_addr.h"
  52. #include "lwip/ip6.h"
  53. #include "lwip/ip6_addr.h"
  54. #include "lwip/netif.h"
  55. #include "lwip/icmp.h"
  56. #include "lwip/icmp6.h"
  57. #include "lwip/stats.h"
  58. #include "lwip/snmp.h"
  59. #include "lwip/dhcp.h"
  60. #include <string.h>
  61. #ifndef UDP_LOCAL_PORT_RANGE_START
  62. /* From http://www.iana.org/assignments/port-numbers:
  63. "The Dynamic and/or Private Ports are those from 49152 through 65535" */
  64. #define UDP_LOCAL_PORT_RANGE_START 0xc000
  65. #define UDP_LOCAL_PORT_RANGE_END 0xffff
  66. #define UDP_ENSURE_LOCAL_PORT_RANGE(port) ((u16_t)(((port) & ~UDP_LOCAL_PORT_RANGE_START) + UDP_LOCAL_PORT_RANGE_START))
  67. #endif
  68. /* last local UDP port */
  69. static u16_t udp_port = UDP_LOCAL_PORT_RANGE_START;
  70. /* The list of UDP PCBs */
  71. /* exported in udp.h (was static) */
  72. struct udp_pcb *udp_pcbs;
  73. /**
  74. * Initialize this module.
  75. */
  76. void
  77. udp_init(void)
  78. {
  79. #if LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND)
  80. udp_port = UDP_ENSURE_LOCAL_PORT_RANGE(LWIP_RAND());
  81. #endif /* LWIP_RANDOMIZE_INITIAL_LOCAL_PORTS && defined(LWIP_RAND) */
  82. }
  83. /**
  84. * Allocate a new local UDP port.
  85. *
  86. * @return a new (free) local UDP port number
  87. */
  88. static u16_t
  89. udp_new_port(void)
  90. {
  91. u16_t n = 0;
  92. struct udp_pcb *pcb;
  93. again:
  94. if (udp_port++ == UDP_LOCAL_PORT_RANGE_END) {
  95. udp_port = UDP_LOCAL_PORT_RANGE_START;
  96. }
  97. /* Check all PCBs. */
  98. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
  99. if (pcb->local_port == udp_port) {
  100. if (++n > (UDP_LOCAL_PORT_RANGE_END - UDP_LOCAL_PORT_RANGE_START)) {
  101. return 0;
  102. }
  103. goto again;
  104. }
  105. }
  106. return udp_port;
  107. }
  108. /** Common code to see if the current input packet matches the pcb
  109. * (current input packet is accessed via ip(4/6)_current_* macros)
  110. *
  111. * @param pcb pcb to check
  112. * @param inp network interface on which the datagram was received (only used for IPv4)
  113. * @param broadcast 1 if his is an IPv4 broadcast (global or subnet-only), 0 otherwise (only used for IPv4)
  114. * @return 1 on match, 0 otherwise
  115. */
  116. static u8_t
  117. udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast)
  118. {
  119. LWIP_UNUSED_ARG(inp); /* in IPv6 only case */
  120. LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
  121. /* Dual-stack: PCBs listening to any IP type also listen to any IP address */
  122. if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
  123. #if LWIP_IPV4 && IP_SOF_BROADCAST_RECV
  124. if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
  125. return 0;
  126. }
  127. #endif /* LWIP_IPV4 && IP_SOF_BROADCAST_RECV */
  128. return 1;
  129. }
  130. /* Only need to check PCB if incoming IP version matches PCB IP version */
  131. if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
  132. #if LWIP_IPV4
  133. /* Special case: IPv4 broadcast: all or broadcasts in my subnet
  134. * Note: broadcast variable can only be 1 if it is an IPv4 broadcast */
  135. if (broadcast != 0) {
  136. #if IP_SOF_BROADCAST_RECV
  137. if (ip_get_option(pcb, SOF_BROADCAST))
  138. #endif /* IP_SOF_BROADCAST_RECV */
  139. {
  140. if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
  141. ((ip4_current_dest_addr()->addr == IPADDR_BROADCAST)) ||
  142. ip4_addr_netcmp(ip_2_ip4(&pcb->local_ip), ip4_current_dest_addr(), netif_ip4_netmask(inp))) {
  143. return 1;
  144. }
  145. }
  146. } else
  147. #endif /* LWIP_IPV4 */
  148. /* Handle IPv4 and IPv6: all or exact match */
  149. if (ip_addr_isany(&pcb->local_ip) || ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
  150. return 1;
  151. }
  152. }
  153. return 0;
  154. }
  155. /**
  156. * Process an incoming UDP datagram.
  157. *
  158. * Given an incoming UDP datagram (as a chain of pbufs) this function
  159. * finds a corresponding UDP PCB and hands over the pbuf to the pcbs
  160. * recv function. If no pcb is found or the datagram is incorrect, the
  161. * pbuf is freed.
  162. *
  163. * @param p pbuf to be demultiplexed to a UDP PCB (p->payload pointing to the UDP header)
  164. * @param inp network interface on which the datagram was received.
  165. *
  166. */
  167. void
  168. udp_input(struct pbuf *p, struct netif *inp)
  169. {
  170. struct udp_hdr *udphdr;
  171. struct udp_pcb *pcb, *prev;
  172. struct udp_pcb *uncon_pcb;
  173. u16_t src, dest;
  174. u8_t broadcast;
  175. u8_t for_us = 0;
  176. LWIP_UNUSED_ARG(inp);
  177. PERF_START;
  178. UDP_STATS_INC(udp.recv);
  179. /* Check minimum length (UDP header) */
  180. if (p->len < UDP_HLEN) {
  181. /* drop short packets */
  182. LWIP_DEBUGF(UDP_DEBUG,
  183. ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
  184. UDP_STATS_INC(udp.lenerr);
  185. UDP_STATS_INC(udp.drop);
  186. MIB2_STATS_INC(mib2.udpinerrors);
  187. pbuf_free(p);
  188. goto end;
  189. }
  190. udphdr = (struct udp_hdr *)p->payload;
  191. /* is broadcast packet ? */
  192. broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
  193. LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
  194. /* convert src and dest ports to host byte order */
  195. src = lwip_ntohs(udphdr->src);
  196. dest = lwip_ntohs(udphdr->dest);
  197. udp_debug_print(udphdr);
  198. /* print the UDP source and destination */
  199. LWIP_DEBUGF(UDP_DEBUG, ("udp ("));
  200. ip_addr_debug_print(UDP_DEBUG, ip_current_dest_addr());
  201. LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", lwip_ntohs(udphdr->dest)));
  202. ip_addr_debug_print(UDP_DEBUG, ip_current_src_addr());
  203. LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", lwip_ntohs(udphdr->src)));
  204. pcb = NULL;
  205. prev = NULL;
  206. uncon_pcb = NULL;
  207. /* Iterate through the UDP pcb list for a matching pcb.
  208. * 'Perfect match' pcbs (connected to the remote port & ip address) are
  209. * preferred. If no perfect match is found, the first unconnected pcb that
  210. * matches the local port and ip address gets the datagram. */
  211. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
  212. /* print the PCB local and remote address */
  213. LWIP_DEBUGF(UDP_DEBUG, ("pcb ("));
  214. ip_addr_debug_print(UDP_DEBUG, &pcb->local_ip);
  215. LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F") <-- (", pcb->local_port));
  216. ip_addr_debug_print(UDP_DEBUG, &pcb->remote_ip);
  217. LWIP_DEBUGF(UDP_DEBUG, (", %"U16_F")\n", pcb->remote_port));
  218. /* compare PCB local addr+port to UDP destination addr+port */
  219. if ((pcb->local_port == dest) &&
  220. (udp_input_local_match(pcb, inp, broadcast) != 0)) {
  221. if (((pcb->flags & UDP_FLAGS_CONNECTED) == 0) &&
  222. ((uncon_pcb == NULL)
  223. #if SO_REUSE
  224. /* prefer specific IPs over cath-all */
  225. || !ip_addr_isany(&pcb->local_ip)
  226. #endif /* SO_REUSE */
  227. )) {
  228. /* the first unconnected matching PCB */
  229. uncon_pcb = pcb;
  230. }
  231. /* compare PCB remote addr+port to UDP source addr+port */
  232. if ((pcb->remote_port == src) &&
  233. (ip_addr_isany_val(pcb->remote_ip) ||
  234. ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) {
  235. /* the first fully matching PCB */
  236. if (prev != NULL) {
  237. /* move the pcb to the front of udp_pcbs so that is
  238. found faster next time */
  239. prev->next = pcb->next;
  240. pcb->next = udp_pcbs;
  241. udp_pcbs = pcb;
  242. } else {
  243. UDP_STATS_INC(udp.cachehit);
  244. }
  245. break;
  246. }
  247. }
  248. prev = pcb;
  249. }
  250. /* no fully matching pcb found? then look for an unconnected pcb */
  251. if (pcb == NULL) {
  252. pcb = uncon_pcb;
  253. }
  254. /* Check checksum if this is a match or if it was directed at us. */
  255. if (pcb != NULL) {
  256. for_us = 1;
  257. } else {
  258. #if LWIP_IPV6
  259. if (ip_current_is_v6()) {
  260. for_us = netif_get_ip6_addr_match(inp, ip6_current_dest_addr()) >= 0;
  261. }
  262. #endif /* LWIP_IPV6 */
  263. #if LWIP_IPV4
  264. if (!ip_current_is_v6()) {
  265. for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr());
  266. }
  267. #endif /* LWIP_IPV4 */
  268. }
  269. if (for_us) {
  270. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: calculating checksum\n"));
  271. #if CHECKSUM_CHECK_UDP
  272. IF__NETIF_CHECKSUM_ENABLED(inp, CHECKSUM_CHECK_UDP) {
  273. #if LWIP_UDPLITE
  274. if (ip_current_header_proto() == IP_PROTO_UDPLITE) {
  275. /* Do the UDP Lite checksum */
  276. u16_t chklen = lwip_ntohs(udphdr->len);
  277. if (chklen < sizeof(struct udp_hdr)) {
  278. if (chklen == 0) {
  279. /* For UDP-Lite, checksum length of 0 means checksum
  280. over the complete packet (See RFC 3828 chap. 3.1) */
  281. chklen = p->tot_len;
  282. } else {
  283. /* At least the UDP-Lite header must be covered by the
  284. checksum! (Again, see RFC 3828 chap. 3.1) */
  285. goto chkerr;
  286. }
  287. }
  288. if (ip_chksum_pseudo_partial(p, IP_PROTO_UDPLITE,
  289. p->tot_len, chklen,
  290. ip_current_src_addr(), ip_current_dest_addr()) != 0) {
  291. goto chkerr;
  292. }
  293. } else
  294. #endif /* LWIP_UDPLITE */
  295. {
  296. if (udphdr->chksum != 0) {
  297. if (ip_chksum_pseudo(p, IP_PROTO_UDP, p->tot_len,
  298. ip_current_src_addr(),
  299. ip_current_dest_addr()) != 0) {
  300. goto chkerr;
  301. }
  302. }
  303. }
  304. }
  305. #endif /* CHECKSUM_CHECK_UDP */
  306. if (pbuf_header(p, -UDP_HLEN)) {
  307. /* Can we cope with this failing? Just assert for now */
  308. LWIP_ASSERT("pbuf_header failed\n", 0);
  309. UDP_STATS_INC(udp.drop);
  310. MIB2_STATS_INC(mib2.udpinerrors);
  311. pbuf_free(p);
  312. goto end;
  313. }
  314. if (pcb != NULL) {
  315. MIB2_STATS_INC(mib2.udpindatagrams);
  316. #if SO_REUSE && SO_REUSE_RXTOALL
  317. if (ip_get_option(pcb, SOF_REUSEADDR) &&
  318. (broadcast || ip_addr_ismulticast(ip_current_dest_addr()))) {
  319. /* pass broadcast- or multicast packets to all multicast pcbs
  320. if SOF_REUSEADDR is set on the first match */
  321. struct udp_pcb *mpcb;
  322. u8_t p_header_changed = 0;
  323. s16_t hdrs_len = (s16_t)(ip_current_header_tot_len() + UDP_HLEN);
  324. for (mpcb = udp_pcbs; mpcb != NULL; mpcb = mpcb->next) {
  325. if (mpcb != pcb) {
  326. /* compare PCB local addr+port to UDP destination addr+port */
  327. if ((mpcb->local_port == dest) &&
  328. (udp_input_local_match(mpcb, inp, broadcast) != 0)) {
  329. /* pass a copy of the packet to all local matches */
  330. if (mpcb->recv != NULL) {
  331. struct pbuf *q;
  332. /* for that, move payload to IP header again */
  333. if (p_header_changed == 0) {
  334. pbuf_header_force(p, hdrs_len);
  335. p_header_changed = 1;
  336. }
  337. q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
  338. if (q != NULL) {
  339. err_t err = pbuf_copy(q, p);
  340. if (err == ERR_OK) {
  341. /* move payload to UDP data */
  342. pbuf_header(q, -hdrs_len);
  343. mpcb->recv(mpcb->recv_arg, mpcb, q, ip_current_src_addr(), src);
  344. }
  345. }
  346. }
  347. }
  348. }
  349. }
  350. if (p_header_changed) {
  351. /* and move payload to UDP data again */
  352. pbuf_header(p, -hdrs_len);
  353. }
  354. }
  355. #endif /* SO_REUSE && SO_REUSE_RXTOALL */
  356. /* callback */
  357. if (pcb->recv != NULL) {
  358. /* now the recv function is responsible for freeing p */
  359. pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr(), src);
  360. } else {
  361. /* no recv function registered? then we have to free the pbuf! */
  362. pbuf_free(p);
  363. goto end;
  364. }
  365. } else {
  366. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_input: not for us.\n"));
  367. #if LWIP_ICMP || LWIP_ICMP6
  368. /* No match was found, send ICMP destination port unreachable unless
  369. destination address was broadcast/multicast. */
  370. if (!broadcast && !ip_addr_ismulticast(ip_current_dest_addr())) {
  371. /* move payload pointer back to ip header */
  372. pbuf_header_force(p, (s16_t)(ip_current_header_tot_len() + UDP_HLEN));
  373. icmp_port_unreach(ip_current_is_v6(), p);
  374. }
  375. #endif /* LWIP_ICMP || LWIP_ICMP6 */
  376. UDP_STATS_INC(udp.proterr);
  377. UDP_STATS_INC(udp.drop);
  378. MIB2_STATS_INC(mib2.udpnoports);
  379. pbuf_free(p);
  380. }
  381. } else {
  382. pbuf_free(p);
  383. }
  384. end:
  385. PERF_STOP("udp_input");
  386. return;
  387. #if CHECKSUM_CHECK_UDP
  388. chkerr:
  389. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  390. ("udp_input: UDP (or UDP Lite) datagram discarded due to failing checksum\n"));
  391. UDP_STATS_INC(udp.chkerr);
  392. UDP_STATS_INC(udp.drop);
  393. MIB2_STATS_INC(mib2.udpinerrors);
  394. pbuf_free(p);
  395. PERF_STOP("udp_input");
  396. #endif /* CHECKSUM_CHECK_UDP */
  397. }
  398. /**
  399. * @ingroup udp_raw
  400. * Send data using UDP.
  401. *
  402. * @param pcb UDP PCB used to send the data.
  403. * @param p chain of pbuf's to be sent.
  404. *
  405. * The datagram will be sent to the current remote_ip & remote_port
  406. * stored in pcb. If the pcb is not bound to a port, it will
  407. * automatically be bound to a random port.
  408. *
  409. * @return lwIP error code.
  410. * - ERR_OK. Successful. No error occurred.
  411. * - ERR_MEM. Out of memory.
  412. * - ERR_RTE. Could not find route to destination address.
  413. * - ERR_VAL. No PCB or PCB is dual-stack
  414. * - More errors could be returned by lower protocol layers.
  415. *
  416. * @see udp_disconnect() udp_sendto()
  417. */
  418. err_t
  419. udp_send(struct udp_pcb *pcb, struct pbuf *p)
  420. {
  421. if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
  422. return ERR_VAL;
  423. }
  424. /* send to the packet using remote ip and port stored in the pcb */
  425. return udp_sendto(pcb, p, &pcb->remote_ip, pcb->remote_port);
  426. }
  427. #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
  428. /** @ingroup udp_raw
  429. * Same as udp_send() but with checksum
  430. */
  431. err_t
  432. udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
  433. u8_t have_chksum, u16_t chksum)
  434. {
  435. if ((pcb == NULL) || IP_IS_ANY_TYPE_VAL(pcb->remote_ip)) {
  436. return ERR_VAL;
  437. }
  438. /* send to the packet using remote ip and port stored in the pcb */
  439. return udp_sendto_chksum(pcb, p, &pcb->remote_ip, pcb->remote_port,
  440. have_chksum, chksum);
  441. }
  442. #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
  443. /**
  444. * @ingroup udp_raw
  445. * Send data to a specified address using UDP.
  446. *
  447. * @param pcb UDP PCB used to send the data.
  448. * @param p chain of pbuf's to be sent.
  449. * @param dst_ip Destination IP address.
  450. * @param dst_port Destination UDP port.
  451. *
  452. * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
  453. *
  454. * If the PCB already has a remote address association, it will
  455. * be restored after the data is sent.
  456. *
  457. * @return lwIP error code (@see udp_send for possible error codes)
  458. *
  459. * @see udp_disconnect() udp_send()
  460. */
  461. err_t
  462. udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
  463. const ip_addr_t *dst_ip, u16_t dst_port)
  464. {
  465. #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
  466. return udp_sendto_chksum(pcb, p, dst_ip, dst_port, 0, 0);
  467. }
  468. /** @ingroup udp_raw
  469. * Same as udp_sendto(), but with checksum */
  470. err_t
  471. udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
  472. u16_t dst_port, u8_t have_chksum, u16_t chksum)
  473. {
  474. #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
  475. struct netif *netif;
  476. const ip_addr_t *dst_ip_route = dst_ip;
  477. if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
  478. return ERR_VAL;
  479. }
  480. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send\n"));
  481. #if LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS)
  482. if (ip_addr_ismulticast(dst_ip_route)) {
  483. #if LWIP_IPV6
  484. if (IP_IS_V6(dst_ip)) {
  485. /* For multicast, find a netif based on source address. */
  486. dst_ip_route = &pcb->local_ip;
  487. } else
  488. #endif /* LWIP_IPV6 */
  489. {
  490. #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS
  491. /* IPv4 does not use source-based routing by default, so we use an
  492. administratively selected interface for multicast by default.
  493. However, this can be overridden by setting an interface address
  494. in pcb->multicast_ip that is used for routing. */
  495. if (!ip_addr_isany_val(pcb->multicast_ip) &&
  496. !ip4_addr_cmp(ip_2_ip4(&pcb->multicast_ip), IP4_ADDR_BROADCAST)) {
  497. dst_ip_route = &pcb->multicast_ip;
  498. }
  499. #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS */
  500. }
  501. }
  502. #endif /* LWIP_IPV6 || (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) */
  503. /* find the outgoing network interface for this packet */
  504. if(IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
  505. /* Don't call ip_route() with IP_ANY_TYPE */
  506. netif = ip_route(IP46_ADDR_ANY(IP_GET_TYPE(dst_ip_route)), dst_ip_route);
  507. } else {
  508. netif = ip_route(&pcb->local_ip, dst_ip_route);
  509. }
  510. /* no outgoing network interface could be found? */
  511. if (netif == NULL) {
  512. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: No route to "));
  513. ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, dst_ip);
  514. LWIP_DEBUGF(UDP_DEBUG, ("\n"));
  515. UDP_STATS_INC(udp.rterr);
  516. return ERR_RTE;
  517. }
  518. #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
  519. return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum);
  520. #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
  521. return udp_sendto_if(pcb, p, dst_ip, dst_port, netif);
  522. #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
  523. }
  524. /**
  525. * @ingroup udp_raw
  526. * Send data to a specified address using UDP.
  527. * The netif used for sending can be specified.
  528. *
  529. * This function exists mainly for DHCP, to be able to send UDP packets
  530. * on a netif that is still down.
  531. *
  532. * @param pcb UDP PCB used to send the data.
  533. * @param p chain of pbuf's to be sent.
  534. * @param dst_ip Destination IP address.
  535. * @param dst_port Destination UDP port.
  536. * @param netif the netif used for sending.
  537. *
  538. * dst_ip & dst_port are expected to be in the same byte order as in the pcb.
  539. *
  540. * @return lwIP error code (@see udp_send for possible error codes)
  541. *
  542. * @see udp_disconnect() udp_send()
  543. */
  544. err_t
  545. udp_sendto_if(struct udp_pcb *pcb, struct pbuf *p,
  546. const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif)
  547. {
  548. #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
  549. return udp_sendto_if_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0);
  550. }
  551. /** Same as udp_sendto_if(), but with checksum */
  552. err_t
  553. udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
  554. u16_t dst_port, struct netif *netif, u8_t have_chksum,
  555. u16_t chksum)
  556. {
  557. #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
  558. const ip_addr_t *src_ip;
  559. if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
  560. return ERR_VAL;
  561. }
  562. /* PCB local address is IP_ANY_ADDR? */
  563. #if LWIP_IPV6
  564. if (IP_IS_V6(dst_ip)) {
  565. if (ip6_addr_isany(ip_2_ip6(&pcb->local_ip))) {
  566. src_ip = ip6_select_source_address(netif, ip_2_ip6(dst_ip));
  567. if (src_ip == NULL) {
  568. /* No suitable source address was found. */
  569. return ERR_RTE;
  570. }
  571. } else {
  572. /* use UDP PCB local IPv6 address as source address, if still valid. */
  573. if (netif_get_ip6_addr_match(netif, ip_2_ip6(&pcb->local_ip)) < 0) {
  574. /* Address isn't valid anymore. */
  575. return ERR_RTE;
  576. }
  577. src_ip = &pcb->local_ip;
  578. }
  579. }
  580. #endif /* LWIP_IPV6 */
  581. #if LWIP_IPV4 && LWIP_IPV6
  582. else
  583. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  584. #if LWIP_IPV4
  585. if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip)) ||
  586. ip4_addr_ismulticast(ip_2_ip4(&pcb->local_ip))) {
  587. /* if the local_ip is any or multicast
  588. * use the outgoing network interface IP address as source address */
  589. src_ip = netif_ip_addr4(netif);
  590. } else {
  591. /* check if UDP PCB local IP address is correct
  592. * this could be an old address if netif->ip_addr has changed */
  593. if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) {
  594. /* local_ip doesn't match, drop the packet */
  595. return ERR_RTE;
  596. }
  597. /* use UDP PCB local IP address as source address */
  598. src_ip = &pcb->local_ip;
  599. }
  600. #endif /* LWIP_IPV4 */
  601. #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
  602. return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, have_chksum, chksum, src_ip);
  603. #else /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
  604. return udp_sendto_if_src(pcb, p, dst_ip, dst_port, netif, src_ip);
  605. #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
  606. }
  607. /** @ingroup udp_raw
  608. * Same as @ref udp_sendto_if, but with source address */
  609. err_t
  610. udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p,
  611. const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, const ip_addr_t *src_ip)
  612. {
  613. #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
  614. return udp_sendto_if_src_chksum(pcb, p, dst_ip, dst_port, netif, 0, 0, src_ip);
  615. }
  616. /** Same as udp_sendto_if_src(), but with checksum */
  617. err_t
  618. udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
  619. u16_t dst_port, struct netif *netif, u8_t have_chksum,
  620. u16_t chksum, const ip_addr_t *src_ip)
  621. {
  622. #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
  623. struct udp_hdr *udphdr;
  624. err_t err;
  625. struct pbuf *q; /* q will be sent down the stack */
  626. u8_t ip_proto;
  627. u8_t ttl;
  628. if ((pcb == NULL) || (dst_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) ||
  629. !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
  630. return ERR_VAL;
  631. }
  632. #if LWIP_IPV4 && IP_SOF_BROADCAST
  633. /* broadcast filter? */
  634. if (!ip_get_option(pcb, SOF_BROADCAST) &&
  635. #if LWIP_IPV6
  636. IP_IS_V4(dst_ip) &&
  637. #endif /* LWIP_IPV6 */
  638. ip_addr_isbroadcast(dst_ip, netif)) {
  639. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS,
  640. ("udp_sendto_if: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
  641. return ERR_VAL;
  642. }
  643. #endif /* LWIP_IPV4 && IP_SOF_BROADCAST */
  644. /* if the PCB is not yet bound to a port, bind it here */
  645. if (pcb->local_port == 0) {
  646. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_send: not yet bound to a port, binding now\n"));
  647. err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
  648. if (err != ERR_OK) {
  649. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: forced port bind failed\n"));
  650. return err;
  651. }
  652. }
  653. /* not enough space to add an UDP header to first pbuf in given p chain? */
  654. if (pbuf_header(p, UDP_HLEN)) {
  655. /* allocate header in a separate new pbuf */
  656. q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
  657. /* new header pbuf could not be allocated? */
  658. if (q == NULL) {
  659. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("udp_send: could not allocate header\n"));
  660. return ERR_MEM;
  661. }
  662. if (p->tot_len != 0) {
  663. /* chain header q in front of given pbuf p (only if p contains data) */
  664. pbuf_chain(q, p);
  665. }
  666. /* first pbuf q points to header pbuf */
  667. LWIP_DEBUGF(UDP_DEBUG,
  668. ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
  669. } else {
  670. /* adding space for header within p succeeded */
  671. /* first pbuf q equals given pbuf */
  672. q = p;
  673. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
  674. }
  675. LWIP_ASSERT("check that first pbuf can hold struct udp_hdr",
  676. (q->len >= sizeof(struct udp_hdr)));
  677. /* q now represents the packet to be sent */
  678. udphdr = (struct udp_hdr *)q->payload;
  679. udphdr->src = lwip_htons(pcb->local_port);
  680. udphdr->dest = lwip_htons(dst_port);
  681. /* in UDP, 0 checksum means 'no checksum' */
  682. udphdr->chksum = 0x0000;
  683. /* Multicast Loop? */
  684. #if (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD)
  685. if (((pcb->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
  686. q->flags |= PBUF_FLAG_MCASTLOOP;
  687. }
  688. #endif /* (LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
  689. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
  690. #if LWIP_UDPLITE
  691. /* UDP Lite protocol? */
  692. if (pcb->flags & UDP_FLAGS_UDPLITE) {
  693. u16_t chklen, chklen_hdr;
  694. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
  695. /* set UDP message length in UDP header */
  696. chklen_hdr = chklen = pcb->chksum_len_tx;
  697. if ((chklen < sizeof(struct udp_hdr)) || (chklen > q->tot_len)) {
  698. if (chklen != 0) {
  699. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE pcb->chksum_len is illegal: %"U16_F"\n", chklen));
  700. }
  701. /* For UDP-Lite, checksum length of 0 means checksum
  702. over the complete packet. (See RFC 3828 chap. 3.1)
  703. At least the UDP-Lite header must be covered by the
  704. checksum, therefore, if chksum_len has an illegal
  705. value, we generate the checksum over the complete
  706. packet to be safe. */
  707. chklen_hdr = 0;
  708. chklen = q->tot_len;
  709. }
  710. udphdr->len = lwip_htons(chklen_hdr);
  711. /* calculate checksum */
  712. #if CHECKSUM_GEN_UDP
  713. IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
  714. #if LWIP_CHECKSUM_ON_COPY
  715. if (have_chksum) {
  716. chklen = UDP_HLEN;
  717. }
  718. #endif /* LWIP_CHECKSUM_ON_COPY */
  719. udphdr->chksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDPLITE,
  720. q->tot_len, chklen, src_ip, dst_ip);
  721. #if LWIP_CHECKSUM_ON_COPY
  722. if (have_chksum) {
  723. u32_t acc;
  724. acc = udphdr->chksum + (u16_t)~(chksum);
  725. udphdr->chksum = FOLD_U32T(acc);
  726. }
  727. #endif /* LWIP_CHECKSUM_ON_COPY */
  728. /* chksum zero must become 0xffff, as zero means 'no checksum' */
  729. if (udphdr->chksum == 0x0000) {
  730. udphdr->chksum = 0xffff;
  731. }
  732. }
  733. #endif /* CHECKSUM_GEN_UDP */
  734. ip_proto = IP_PROTO_UDPLITE;
  735. } else
  736. #endif /* LWIP_UDPLITE */
  737. { /* UDP */
  738. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
  739. udphdr->len = lwip_htons(q->tot_len);
  740. /* calculate checksum */
  741. #if CHECKSUM_GEN_UDP
  742. IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_UDP) {
  743. /* Checksum is mandatory over IPv6. */
  744. if (IP_IS_V6(dst_ip) || (pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
  745. u16_t udpchksum;
  746. #if LWIP_CHECKSUM_ON_COPY
  747. if (have_chksum) {
  748. u32_t acc;
  749. udpchksum = ip_chksum_pseudo_partial(q, IP_PROTO_UDP,
  750. q->tot_len, UDP_HLEN, src_ip, dst_ip);
  751. acc = udpchksum + (u16_t)~(chksum);
  752. udpchksum = FOLD_U32T(acc);
  753. } else
  754. #endif /* LWIP_CHECKSUM_ON_COPY */
  755. {
  756. udpchksum = ip_chksum_pseudo(q, IP_PROTO_UDP, q->tot_len,
  757. src_ip, dst_ip);
  758. }
  759. /* chksum zero must become 0xffff, as zero means 'no checksum' */
  760. if (udpchksum == 0x0000) {
  761. udpchksum = 0xffff;
  762. }
  763. udphdr->chksum = udpchksum;
  764. }
  765. }
  766. #endif /* CHECKSUM_GEN_UDP */
  767. ip_proto = IP_PROTO_UDP;
  768. }
  769. /* Determine TTL to use */
  770. #if LWIP_MULTICAST_TX_OPTIONS
  771. ttl = (ip_addr_ismulticast(dst_ip) ? udp_get_multicast_ttl(pcb) : pcb->ttl);
  772. #else /* LWIP_MULTICAST_TX_OPTIONS */
  773. ttl = pcb->ttl;
  774. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  775. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
  776. LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,0x%02"X16_F",)\n", (u16_t)ip_proto));
  777. /* output to IP */
  778. NETIF_SET_HWADDRHINT(netif, &(pcb->addr_hint));
  779. err = ip_output_if_src(q, src_ip, dst_ip, ttl, pcb->tos, ip_proto, netif);
  780. NETIF_SET_HWADDRHINT(netif, NULL);
  781. /* @todo: must this be increased even if error occurred? */
  782. MIB2_STATS_INC(mib2.udpoutdatagrams);
  783. /* did we chain a separate header pbuf earlier? */
  784. if (q != p) {
  785. /* free the header pbuf */
  786. pbuf_free(q);
  787. q = NULL;
  788. /* p is still referenced by the caller, and will live on */
  789. }
  790. UDP_STATS_INC(udp.xmit);
  791. return err;
  792. }
  793. /**
  794. * @ingroup udp_raw
  795. * Bind an UDP PCB.
  796. *
  797. * @param pcb UDP PCB to be bound with a local address ipaddr and port.
  798. * @param ipaddr local IP address to bind with. Use IP4_ADDR_ANY to
  799. * bind to all local interfaces.
  800. * @param port local UDP port to bind with. Use 0 to automatically bind
  801. * to a random port between UDP_LOCAL_PORT_RANGE_START and
  802. * UDP_LOCAL_PORT_RANGE_END.
  803. *
  804. * ipaddr & port are expected to be in the same byte order as in the pcb.
  805. *
  806. * @return lwIP error code.
  807. * - ERR_OK. Successful. No error occurred.
  808. * - ERR_USE. The specified ipaddr and port are already bound to by
  809. * another UDP PCB.
  810. *
  811. * @see udp_disconnect()
  812. */
  813. err_t
  814. udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
  815. {
  816. struct udp_pcb *ipcb;
  817. u8_t rebind;
  818. #if LWIP_IPV4
  819. /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
  820. if (ipaddr == NULL) {
  821. ipaddr = IP4_ADDR_ANY;
  822. }
  823. #endif /* LWIP_IPV4 */
  824. /* still need to check for ipaddr == NULL in IPv6 only case */
  825. if ((pcb == NULL) || (ipaddr == NULL)) {
  826. return ERR_VAL;
  827. }
  828. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, ("udp_bind(ipaddr = "));
  829. ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE, ipaddr);
  830. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE, (", port = %"U16_F")\n", port));
  831. rebind = 0;
  832. /* Check for double bind and rebind of the same pcb */
  833. for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
  834. /* is this UDP PCB already on active list? */
  835. if (pcb == ipcb) {
  836. rebind = 1;
  837. break;
  838. }
  839. }
  840. /* no port specified? */
  841. if (port == 0) {
  842. port = udp_new_port();
  843. if (port == 0) {
  844. /* no more ports available in local range */
  845. LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
  846. return ERR_USE;
  847. }
  848. } else {
  849. for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
  850. if (pcb != ipcb) {
  851. /* By default, we don't allow to bind to a port that any other udp
  852. PCB is already bound to, unless *all* PCBs with that port have tha
  853. REUSEADDR flag set. */
  854. #if SO_REUSE
  855. if (!ip_get_option(pcb, SOF_REUSEADDR) ||
  856. !ip_get_option(ipcb, SOF_REUSEADDR))
  857. #endif /* SO_REUSE */
  858. {
  859. /* port matches that of PCB in list and REUSEADDR not set -> reject */
  860. if ((ipcb->local_port == port) &&
  861. /* IP address matches? */
  862. ip_addr_cmp(&ipcb->local_ip, ipaddr)) {
  863. /* other PCB already binds to this local IP and port */
  864. LWIP_DEBUGF(UDP_DEBUG,
  865. ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
  866. return ERR_USE;
  867. }
  868. }
  869. }
  870. }
  871. }
  872. ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
  873. pcb->local_port = port;
  874. mib2_udp_bind(pcb);
  875. /* pcb not active yet? */
  876. if (rebind == 0) {
  877. /* place the PCB on the active list if not already there */
  878. pcb->next = udp_pcbs;
  879. udp_pcbs = pcb;
  880. }
  881. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_bind: bound to "));
  882. ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, &pcb->local_ip);
  883. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->local_port));
  884. return ERR_OK;
  885. }
  886. /**
  887. * @ingroup udp_raw
  888. * Connect an UDP PCB.
  889. *
  890. * This will associate the UDP PCB with the remote address.
  891. *
  892. * @param pcb UDP PCB to be connected with remote address ipaddr and port.
  893. * @param ipaddr remote IP address to connect with.
  894. * @param port remote UDP port to connect with.
  895. *
  896. * @return lwIP error code
  897. *
  898. * ipaddr & port are expected to be in the same byte order as in the pcb.
  899. *
  900. * The udp pcb is bound to a random local port if not already bound.
  901. *
  902. * @see udp_disconnect()
  903. */
  904. err_t
  905. udp_connect(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port)
  906. {
  907. struct udp_pcb *ipcb;
  908. if ((pcb == NULL) || (ipaddr == NULL)) {
  909. return ERR_VAL;
  910. }
  911. if (pcb->local_port == 0) {
  912. err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
  913. if (err != ERR_OK) {
  914. return err;
  915. }
  916. }
  917. ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
  918. pcb->remote_port = port;
  919. pcb->flags |= UDP_FLAGS_CONNECTED;
  920. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("udp_connect: connected to "));
  921. ip_addr_debug_print(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE,
  922. &pcb->remote_ip);
  923. LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, (", port %"U16_F")\n", pcb->remote_port));
  924. /* Insert UDP PCB into the list of active UDP PCBs. */
  925. for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
  926. if (pcb == ipcb) {
  927. /* already on the list, just return */
  928. return ERR_OK;
  929. }
  930. }
  931. /* PCB not yet on the list, add PCB now */
  932. pcb->next = udp_pcbs;
  933. udp_pcbs = pcb;
  934. return ERR_OK;
  935. }
  936. /**
  937. * @ingroup udp_raw
  938. * Disconnect a UDP PCB
  939. *
  940. * @param pcb the udp pcb to disconnect.
  941. */
  942. void
  943. udp_disconnect(struct udp_pcb *pcb)
  944. {
  945. /* reset remote address association */
  946. #if LWIP_IPV4 && LWIP_IPV6
  947. if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
  948. ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
  949. } else {
  950. #endif
  951. ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
  952. #if LWIP_IPV4 && LWIP_IPV6
  953. }
  954. #endif
  955. pcb->remote_port = 0;
  956. /* mark PCB as unconnected */
  957. pcb->flags &= ~UDP_FLAGS_CONNECTED;
  958. }
  959. /**
  960. * @ingroup udp_raw
  961. * Set a receive callback for a UDP PCB
  962. *
  963. * This callback will be called when receiving a datagram for the pcb.
  964. *
  965. * @param pcb the pcb for which to set the recv callback
  966. * @param recv function pointer of the callback function
  967. * @param recv_arg additional argument to pass to the callback function
  968. */
  969. void
  970. udp_recv(struct udp_pcb *pcb, udp_recv_fn recv, void *recv_arg)
  971. {
  972. /* remember recv() callback and user data */
  973. pcb->recv = recv;
  974. pcb->recv_arg = recv_arg;
  975. }
  976. /**
  977. * @ingroup udp_raw
  978. * Remove an UDP PCB.
  979. *
  980. * @param pcb UDP PCB to be removed. The PCB is removed from the list of
  981. * UDP PCB's and the data structure is freed from memory.
  982. *
  983. * @see udp_new()
  984. */
  985. void
  986. udp_remove(struct udp_pcb *pcb)
  987. {
  988. struct udp_pcb *pcb2;
  989. mib2_udp_unbind(pcb);
  990. /* pcb to be removed is first in list? */
  991. if (udp_pcbs == pcb) {
  992. /* make list start at 2nd pcb */
  993. udp_pcbs = udp_pcbs->next;
  994. /* pcb not 1st in list */
  995. } else {
  996. for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
  997. /* find pcb in udp_pcbs list */
  998. if (pcb2->next != NULL && pcb2->next == pcb) {
  999. /* remove pcb from list */
  1000. pcb2->next = pcb->next;
  1001. break;
  1002. }
  1003. }
  1004. }
  1005. memp_free(MEMP_UDP_PCB, pcb);
  1006. }
  1007. /**
  1008. * @ingroup udp_raw
  1009. * Create a UDP PCB.
  1010. *
  1011. * @return The UDP PCB which was created. NULL if the PCB data structure
  1012. * could not be allocated.
  1013. *
  1014. * @see udp_remove()
  1015. */
  1016. struct udp_pcb *
  1017. udp_new(void)
  1018. {
  1019. struct udp_pcb *pcb;
  1020. pcb = (struct udp_pcb *)memp_malloc(MEMP_UDP_PCB);
  1021. /* could allocate UDP PCB? */
  1022. if (pcb != NULL) {
  1023. /* UDP Lite: by initializing to all zeroes, chksum_len is set to 0
  1024. * which means checksum is generated over the whole datagram per default
  1025. * (recommended as default by RFC 3828). */
  1026. /* initialize PCB to all zeroes */
  1027. memset(pcb, 0, sizeof(struct udp_pcb));
  1028. pcb->ttl = UDP_TTL;
  1029. #if LWIP_MULTICAST_TX_OPTIONS
  1030. udp_set_multicast_ttl(pcb, UDP_TTL);
  1031. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  1032. }
  1033. return pcb;
  1034. }
  1035. /**
  1036. * @ingroup udp_raw
  1037. * Create a UDP PCB for specific IP type.
  1038. *
  1039. * @param type IP address type, see @ref lwip_ip_addr_type definitions.
  1040. * If you want to listen to IPv4 and IPv6 (dual-stack) packets,
  1041. * supply @ref IPADDR_TYPE_ANY as argument and bind to @ref IP_ANY_TYPE.
  1042. * @return The UDP PCB which was created. NULL if the PCB data structure
  1043. * could not be allocated.
  1044. *
  1045. * @see udp_remove()
  1046. */
  1047. struct udp_pcb *
  1048. udp_new_ip_type(u8_t type)
  1049. {
  1050. struct udp_pcb *pcb;
  1051. pcb = udp_new();
  1052. #if LWIP_IPV4 && LWIP_IPV6
  1053. if (pcb != NULL) {
  1054. IP_SET_TYPE_VAL(pcb->local_ip, type);
  1055. IP_SET_TYPE_VAL(pcb->remote_ip, type);
  1056. }
  1057. #else
  1058. LWIP_UNUSED_ARG(type);
  1059. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1060. return pcb;
  1061. }
  1062. /** This function is called from netif.c when address is changed
  1063. *
  1064. * @param old_addr IP address of the netif before change
  1065. * @param new_addr IP address of the netif after change
  1066. */
  1067. void udp_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr)
  1068. {
  1069. struct udp_pcb* upcb;
  1070. if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
  1071. for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) {
  1072. /* PCB bound to current local interface address? */
  1073. if (ip_addr_cmp(&upcb->local_ip, old_addr)) {
  1074. /* The PCB is bound to the old ipaddr and
  1075. * is set to bound to the new one instead */
  1076. ip_addr_copy(upcb->local_ip, *new_addr);
  1077. }
  1078. }
  1079. }
  1080. }
  1081. #if UDP_DEBUG
  1082. /**
  1083. * Print UDP header information for debug purposes.
  1084. *
  1085. * @param udphdr pointer to the udp header in memory.
  1086. */
  1087. void
  1088. udp_debug_print(struct udp_hdr *udphdr)
  1089. {
  1090. LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
  1091. LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
  1092. LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
  1093. lwip_ntohs(udphdr->src), lwip_ntohs(udphdr->dest)));
  1094. LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
  1095. LWIP_DEBUGF(UDP_DEBUG, ("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
  1096. lwip_ntohs(udphdr->len), lwip_ntohs(udphdr->chksum)));
  1097. LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
  1098. }
  1099. #endif /* UDP_DEBUG */
  1100. #endif /* LWIP_UDP */