api_msg.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. /**
  2. * @file
  3. * Sequential API Internal 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. #include "lwip/opt.h"
  38. #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
  39. #include "lwip/api_msg.h"
  40. #include "lwip/ip.h"
  41. #include "lwip/udp.h"
  42. #include "lwip/tcp.h"
  43. #include "lwip/raw.h"
  44. #include "lwip/memp.h"
  45. #include "lwip/tcpip.h"
  46. #include "lwip/igmp.h"
  47. #include "lwip/dns.h"
  48. #include "lwip/mld6.h"
  49. #include <string.h>
  50. #define SET_NONBLOCKING_CONNECT(conn, val) do { if(val) { \
  51. (conn)->flags |= NETCONN_FLAG_IN_NONBLOCKING_CONNECT; \
  52. } else { \
  53. (conn)->flags &= ~ NETCONN_FLAG_IN_NONBLOCKING_CONNECT; }} while(0)
  54. #define IN_NONBLOCKING_CONNECT(conn) (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0)
  55. /* forward declarations */
  56. #if LWIP_TCP
  57. static err_t lwip_netconn_do_writemore(struct netconn *conn);
  58. static void lwip_netconn_do_close_internal(struct netconn *conn);
  59. #endif
  60. #if LWIP_RAW
  61. /**
  62. * Receive callback function for RAW netconns.
  63. * Doesn't 'eat' the packet, only references it and sends it to
  64. * conn->recvmbox
  65. *
  66. * @see raw.h (struct raw_pcb.recv) for parameters and return value
  67. */
  68. static u8_t
  69. recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
  70. ip_addr_t *addr)
  71. {
  72. struct pbuf *q;
  73. struct netbuf *buf;
  74. struct netconn *conn;
  75. LWIP_UNUSED_ARG(addr);
  76. conn = (struct netconn *)arg;
  77. if ((conn != NULL) && sys_mbox_valid(&conn->recvmbox)) {
  78. #if LWIP_SO_RCVBUF
  79. int recv_avail;
  80. SYS_ARCH_GET(conn->recv_avail, recv_avail);
  81. if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) {
  82. return 0;
  83. }
  84. #endif /* LWIP_SO_RCVBUF */
  85. /* copy the whole packet into new pbufs */
  86. q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
  87. if(q != NULL) {
  88. if (pbuf_copy(q, p) != ERR_OK) {
  89. pbuf_free(q);
  90. q = NULL;
  91. }
  92. }
  93. if (q != NULL) {
  94. u16_t len;
  95. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  96. if (buf == NULL) {
  97. pbuf_free(q);
  98. return 0;
  99. }
  100. buf->p = q;
  101. buf->ptr = q;
  102. ipX_addr_copy(PCB_ISIPV6(pcb), buf->addr, *ipX_current_src_addr());
  103. buf->port = pcb->protocol;
  104. len = q->tot_len;
  105. if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
  106. netbuf_delete(buf);
  107. return 0;
  108. } else {
  109. #if LWIP_SO_RCVBUF
  110. SYS_ARCH_INC(conn->recv_avail, len);
  111. #endif /* LWIP_SO_RCVBUF */
  112. /* Register event with callback */
  113. API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
  114. }
  115. }
  116. }
  117. return 0; /* do not eat the packet */
  118. }
  119. #endif /* LWIP_RAW*/
  120. #if LWIP_UDP
  121. /**
  122. * Receive callback function for UDP netconns.
  123. * Posts the packet to conn->recvmbox or deletes it on memory error.
  124. *
  125. * @see udp.h (struct udp_pcb.recv) for parameters
  126. */
  127. static void
  128. recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  129. ip_addr_t *addr, u16_t port)
  130. {
  131. struct netbuf *buf;
  132. struct netconn *conn;
  133. u16_t len;
  134. #if LWIP_SO_RCVBUF
  135. int recv_avail;
  136. #endif /* LWIP_SO_RCVBUF */
  137. LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
  138. LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
  139. LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
  140. conn = (struct netconn *)arg;
  141. LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
  142. #if LWIP_SO_RCVBUF
  143. SYS_ARCH_GET(conn->recv_avail, recv_avail);
  144. if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox) ||
  145. ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
  146. #else /* LWIP_SO_RCVBUF */
  147. if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox)) {
  148. #endif /* LWIP_SO_RCVBUF */
  149. pbuf_free(p);
  150. return;
  151. }
  152. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  153. if (buf == NULL) {
  154. pbuf_free(p);
  155. return;
  156. } else {
  157. buf->p = p;
  158. buf->ptr = p;
  159. ipX_addr_set_ipaddr(ip_current_is_v6(), &buf->addr, addr);
  160. buf->port = port;
  161. #if LWIP_NETBUF_RECVINFO
  162. {
  163. /* get the UDP header - always in the first pbuf, ensured by udp_input */
  164. const struct udp_hdr* udphdr = ipX_next_header_ptr();
  165. #if LWIP_CHECKSUM_ON_COPY
  166. buf->flags = NETBUF_FLAG_DESTADDR;
  167. #endif /* LWIP_CHECKSUM_ON_COPY */
  168. ipX_addr_set(ip_current_is_v6(), &buf->toaddr, ipX_current_dest_addr());
  169. buf->toport_chksum = udphdr->dest;
  170. }
  171. #endif /* LWIP_NETBUF_RECVINFO */
  172. }
  173. len = p->tot_len;
  174. if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
  175. netbuf_delete(buf);
  176. return;
  177. } else {
  178. #if LWIP_SO_RCVBUF
  179. SYS_ARCH_INC(conn->recv_avail, len);
  180. #endif /* LWIP_SO_RCVBUF */
  181. /* Register event with callback */
  182. API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
  183. }
  184. }
  185. #endif /* LWIP_UDP */
  186. #if LWIP_TCP
  187. /**
  188. * Receive callback function for TCP netconns.
  189. * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
  190. *
  191. * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
  192. */
  193. static err_t
  194. recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  195. {
  196. struct netconn *conn;
  197. u16_t len;
  198. LWIP_UNUSED_ARG(pcb);
  199. LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
  200. LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
  201. conn = (struct netconn *)arg;
  202. if (conn == NULL) {
  203. return ERR_VAL;
  204. }
  205. LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
  206. if (!sys_mbox_valid(&conn->recvmbox)) {
  207. /* recvmbox already deleted */
  208. if (p != NULL) {
  209. tcp_recved(pcb, p->tot_len);
  210. pbuf_free(p);
  211. }
  212. return ERR_OK;
  213. }
  214. /* Unlike for UDP or RAW pcbs, don't check for available space
  215. using recv_avail since that could break the connection
  216. (data is already ACKed) */
  217. /* don't overwrite fatal errors! */
  218. NETCONN_SET_SAFE_ERR(conn, err);
  219. if (p != NULL) {
  220. len = p->tot_len;
  221. } else {
  222. len = 0;
  223. }
  224. if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) {
  225. /* don't deallocate p: it is presented to us later again from tcp_fasttmr! */
  226. return ERR_MEM;
  227. } else {
  228. #if LWIP_SO_RCVBUF
  229. SYS_ARCH_INC(conn->recv_avail, len);
  230. #endif /* LWIP_SO_RCVBUF */
  231. /* Register event with callback */
  232. API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
  233. }
  234. return ERR_OK;
  235. }
  236. /**
  237. * Poll callback function for TCP netconns.
  238. * Wakes up an application thread that waits for a connection to close
  239. * or data to be sent. The application thread then takes the
  240. * appropriate action to go on.
  241. *
  242. * Signals the conn->sem.
  243. * netconn_close waits for conn->sem if closing failed.
  244. *
  245. * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
  246. */
  247. static err_t
  248. poll_tcp(void *arg, struct tcp_pcb *pcb)
  249. {
  250. struct netconn *conn = (struct netconn *)arg;
  251. LWIP_UNUSED_ARG(pcb);
  252. LWIP_ASSERT("conn != NULL", (conn != NULL));
  253. if (conn->state == NETCONN_WRITE) {
  254. lwip_netconn_do_writemore(conn);
  255. } else if (conn->state == NETCONN_CLOSE) {
  256. lwip_netconn_do_close_internal(conn);
  257. }
  258. /* @todo: implement connect timeout here? */
  259. /* Did a nonblocking write fail before? Then check available write-space. */
  260. if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) {
  261. /* If the queued byte- or pbuf-count drops below the configured low-water limit,
  262. let select mark this pcb as writable again. */
  263. if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
  264. (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
  265. conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
  266. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  267. }
  268. }
  269. return ERR_OK;
  270. }
  271. /**
  272. * Sent callback function for TCP netconns.
  273. * Signals the conn->sem and calls API_EVENT.
  274. * netconn_write waits for conn->sem if send buffer is low.
  275. *
  276. * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
  277. */
  278. static err_t
  279. sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
  280. {
  281. struct netconn *conn = (struct netconn *)arg;
  282. LWIP_UNUSED_ARG(pcb);
  283. LWIP_ASSERT("conn != NULL", (conn != NULL));
  284. if (conn->state == NETCONN_WRITE) {
  285. lwip_netconn_do_writemore(conn);
  286. } else if (conn->state == NETCONN_CLOSE) {
  287. lwip_netconn_do_close_internal(conn);
  288. }
  289. if (conn) {
  290. /* If the queued byte- or pbuf-count drops below the configured low-water limit,
  291. let select mark this pcb as writable again. */
  292. if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
  293. (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
  294. conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
  295. API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
  296. }
  297. }
  298. return ERR_OK;
  299. }
  300. /**
  301. * Error callback function for TCP netconns.
  302. * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
  303. * The application thread has then to decide what to do.
  304. *
  305. * @see tcp.h (struct tcp_pcb.err) for parameters
  306. */
  307. static void
  308. err_tcp(void *arg, err_t err)
  309. {
  310. struct netconn *conn;
  311. enum netconn_state old_state;
  312. SYS_ARCH_DECL_PROTECT(lev);
  313. conn = (struct netconn *)arg;
  314. LWIP_ASSERT("conn != NULL", (conn != NULL));
  315. conn->pcb.tcp = NULL;
  316. /* no check since this is always fatal! */
  317. SYS_ARCH_PROTECT(lev);
  318. conn->last_err = err;
  319. SYS_ARCH_UNPROTECT(lev);
  320. /* reset conn->state now before waking up other threads */
  321. old_state = conn->state;
  322. conn->state = NETCONN_NONE;
  323. /* Notify the user layer about a connection error. Used to signal
  324. select. */
  325. API_EVENT(conn, NETCONN_EVT_ERROR, 0);
  326. /* Try to release selects pending on 'read' or 'write', too.
  327. They will get an error if they actually try to read or write. */
  328. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  329. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  330. /* pass NULL-message to recvmbox to wake up pending recv */
  331. if (sys_mbox_valid(&conn->recvmbox)) {
  332. /* use trypost to prevent deadlock */
  333. sys_mbox_trypost(&conn->recvmbox, NULL);
  334. }
  335. /* pass NULL-message to acceptmbox to wake up pending accept */
  336. if (sys_mbox_valid(&conn->acceptmbox)) {
  337. /* use trypost to preven deadlock */
  338. sys_mbox_trypost(&conn->acceptmbox, NULL);
  339. }
  340. if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) ||
  341. (old_state == NETCONN_CONNECT)) {
  342. /* calling lwip_netconn_do_writemore/lwip_netconn_do_close_internal is not necessary
  343. since the pcb has already been deleted! */
  344. int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn);
  345. SET_NONBLOCKING_CONNECT(conn, 0);
  346. if (!was_nonblocking_connect) {
  347. /* set error return code */
  348. LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
  349. conn->current_msg->err = err;
  350. conn->current_msg = NULL;
  351. /* wake up the waiting task */
  352. sys_sem_signal(&conn->op_completed);
  353. }
  354. } else {
  355. LWIP_ASSERT("conn->current_msg == NULL", conn->current_msg == NULL);
  356. }
  357. }
  358. /**
  359. * Setup a tcp_pcb with the correct callback function pointers
  360. * and their arguments.
  361. *
  362. * @param conn the TCP netconn to setup
  363. */
  364. static void
  365. setup_tcp(struct netconn *conn)
  366. {
  367. struct tcp_pcb *pcb;
  368. pcb = conn->pcb.tcp;
  369. tcp_arg(pcb, conn);
  370. tcp_recv(pcb, recv_tcp);
  371. tcp_sent(pcb, sent_tcp);
  372. tcp_poll(pcb, poll_tcp, 4);
  373. tcp_err(pcb, err_tcp);
  374. }
  375. /**
  376. * Accept callback function for TCP netconns.
  377. * Allocates a new netconn and posts that to conn->acceptmbox.
  378. *
  379. * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
  380. */
  381. static err_t
  382. accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
  383. {
  384. struct netconn *newconn;
  385. struct netconn *conn = (struct netconn *)arg;
  386. LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: newpcb->tate: %s\n", tcp_debug_state_str(newpcb->state)));
  387. if (!sys_mbox_valid(&conn->acceptmbox)) {
  388. LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: acceptmbox already deleted\n"));
  389. return ERR_VAL;
  390. }
  391. /* We have to set the callback here even though
  392. * the new socket is unknown. conn->socket is marked as -1. */
  393. newconn = netconn_alloc(conn->type, conn->callback);
  394. if (newconn == NULL) {
  395. return ERR_MEM;
  396. }
  397. newconn->pcb.tcp = newpcb;
  398. setup_tcp(newconn);
  399. /* no protection: when creating the pcb, the netconn is not yet known
  400. to the application thread */
  401. newconn->last_err = err;
  402. if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) {
  403. /* When returning != ERR_OK, the pcb is aborted in tcp_process(),
  404. so do nothing here! */
  405. /* remove all references to this netconn from the pcb */
  406. struct tcp_pcb* pcb = newconn->pcb.tcp;
  407. tcp_arg(pcb, NULL);
  408. tcp_recv(pcb, NULL);
  409. tcp_sent(pcb, NULL);
  410. tcp_poll(pcb, NULL, 4);
  411. tcp_err(pcb, NULL);
  412. /* remove reference from to the pcb from this netconn */
  413. newconn->pcb.tcp = NULL;
  414. /* no need to drain since we know the recvmbox is empty. */
  415. sys_mbox_free(&newconn->recvmbox);
  416. sys_mbox_set_invalid(&newconn->recvmbox);
  417. netconn_free(newconn);
  418. return ERR_MEM;
  419. } else {
  420. /* Register event with callback */
  421. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  422. }
  423. return ERR_OK;
  424. }
  425. #endif /* LWIP_TCP */
  426. /**
  427. * Create a new pcb of a specific type.
  428. * Called from lwip_netconn_do_newconn().
  429. *
  430. * @param msg the api_msg_msg describing the connection type
  431. * @return msg->conn->err, but the return value is currently ignored
  432. */
  433. static void
  434. pcb_new(struct api_msg_msg *msg)
  435. {
  436. LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
  437. /* Allocate a PCB for this connection */
  438. switch(NETCONNTYPE_GROUP(msg->conn->type)) {
  439. #if LWIP_RAW
  440. case NETCONN_RAW:
  441. msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
  442. if(msg->conn->pcb.raw != NULL) {
  443. raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
  444. }
  445. break;
  446. #endif /* LWIP_RAW */
  447. #if LWIP_UDP
  448. case NETCONN_UDP:
  449. msg->conn->pcb.udp = udp_new();
  450. if(msg->conn->pcb.udp != NULL) {
  451. #if LWIP_UDPLITE
  452. if (NETCONNTYPE_ISUDPLITE(msg->conn->type)) {
  453. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
  454. }
  455. #endif /* LWIP_UDPLITE */
  456. if (NETCONNTYPE_ISUDPNOCHKSUM(msg->conn->type)) {
  457. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
  458. }
  459. udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
  460. }
  461. break;
  462. #endif /* LWIP_UDP */
  463. #if LWIP_TCP
  464. case NETCONN_TCP:
  465. msg->conn->pcb.tcp = tcp_new();
  466. if(msg->conn->pcb.tcp != NULL) {
  467. setup_tcp(msg->conn);
  468. }
  469. break;
  470. #endif /* LWIP_TCP */
  471. default:
  472. /* Unsupported netconn type, e.g. protocol disabled */
  473. msg->err = ERR_VAL;
  474. return;
  475. }
  476. if (msg->conn->pcb.ip == NULL) {
  477. msg->err = ERR_MEM;
  478. }
  479. #if LWIP_IPV6
  480. else {
  481. if (NETCONNTYPE_ISIPV6(msg->conn->type)) {
  482. ip_set_v6(msg->conn->pcb.ip, 1);
  483. }
  484. }
  485. #endif /* LWIP_IPV6 */
  486. }
  487. /**
  488. * Create a new pcb of a specific type inside a netconn.
  489. * Called from netconn_new_with_proto_and_callback.
  490. *
  491. * @param msg the api_msg_msg describing the connection type
  492. */
  493. void
  494. lwip_netconn_do_newconn(struct api_msg_msg *msg)
  495. {
  496. msg->err = ERR_OK;
  497. if(msg->conn->pcb.tcp == NULL) {
  498. pcb_new(msg);
  499. }
  500. /* Else? This "new" connection already has a PCB allocated. */
  501. /* Is this an error condition? Should it be deleted? */
  502. /* We currently just are happy and return. */
  503. TCPIP_APIMSG_ACK(msg);
  504. }
  505. /**
  506. * Create a new netconn (of a specific type) that has a callback function.
  507. * The corresponding pcb is NOT created!
  508. *
  509. * @param t the type of 'connection' to create (@see enum netconn_type)
  510. * @param proto the IP protocol for RAW IP pcbs
  511. * @param callback a function to call on status changes (RX available, TX'ed)
  512. * @return a newly allocated struct netconn or
  513. * NULL on memory error
  514. */
  515. struct netconn*
  516. netconn_alloc(enum netconn_type t, netconn_callback callback)
  517. {
  518. struct netconn *conn;
  519. int size;
  520. conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
  521. if (conn == NULL) {
  522. return NULL;
  523. }
  524. conn->last_err = ERR_OK;
  525. conn->type = t;
  526. conn->pcb.tcp = NULL;
  527. /* If all sizes are the same, every compiler should optimize this switch to nothing, */
  528. switch(NETCONNTYPE_GROUP(t)) {
  529. #if LWIP_RAW
  530. case NETCONN_RAW:
  531. size = DEFAULT_RAW_RECVMBOX_SIZE;
  532. break;
  533. #endif /* LWIP_RAW */
  534. #if LWIP_UDP
  535. case NETCONN_UDP:
  536. size = DEFAULT_UDP_RECVMBOX_SIZE;
  537. break;
  538. #endif /* LWIP_UDP */
  539. #if LWIP_TCP
  540. case NETCONN_TCP:
  541. size = DEFAULT_TCP_RECVMBOX_SIZE;
  542. break;
  543. #endif /* LWIP_TCP */
  544. default:
  545. LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
  546. goto free_and_return;
  547. }
  548. if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) {
  549. goto free_and_return;
  550. }
  551. if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) {
  552. sys_sem_free(&conn->op_completed);
  553. goto free_and_return;
  554. }
  555. #if LWIP_TCP
  556. sys_mbox_set_invalid(&conn->acceptmbox);
  557. #endif
  558. conn->state = NETCONN_NONE;
  559. #if LWIP_SOCKET
  560. /* initialize socket to -1 since 0 is a valid socket */
  561. conn->socket = -1;
  562. #endif /* LWIP_SOCKET */
  563. conn->callback = callback;
  564. #if LWIP_TCP
  565. conn->current_msg = NULL;
  566. conn->write_offset = 0;
  567. #endif /* LWIP_TCP */
  568. #if LWIP_SO_SNDTIMEO
  569. conn->send_timeout = 0;
  570. #endif /* LWIP_SO_SNDTIMEO */
  571. #if LWIP_SO_RCVTIMEO
  572. conn->recv_timeout = 0;
  573. #endif /* LWIP_SO_RCVTIMEO */
  574. #if LWIP_SO_RCVBUF
  575. conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
  576. conn->recv_avail = 0;
  577. #endif /* LWIP_SO_RCVBUF */
  578. conn->flags = 0;
  579. return conn;
  580. free_and_return:
  581. memp_free(MEMP_NETCONN, conn);
  582. return NULL;
  583. }
  584. /**
  585. * Delete a netconn and all its resources.
  586. * The pcb is NOT freed (since we might not be in the right thread context do this).
  587. *
  588. * @param conn the netconn to free
  589. */
  590. void
  591. netconn_free(struct netconn *conn)
  592. {
  593. LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
  594. LWIP_ASSERT("recvmbox must be deallocated before calling this function",
  595. !sys_mbox_valid(&conn->recvmbox));
  596. #if LWIP_TCP
  597. LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
  598. !sys_mbox_valid(&conn->acceptmbox));
  599. #endif /* LWIP_TCP */
  600. sys_sem_free(&conn->op_completed);
  601. sys_sem_set_invalid(&conn->op_completed);
  602. memp_free(MEMP_NETCONN, conn);
  603. }
  604. /**
  605. * Delete rcvmbox and acceptmbox of a netconn and free the left-over data in
  606. * these mboxes
  607. *
  608. * @param conn the netconn to free
  609. * @bytes_drained bytes drained from recvmbox
  610. * @accepts_drained pending connections drained from acceptmbox
  611. */
  612. static void
  613. netconn_drain(struct netconn *conn)
  614. {
  615. void *mem;
  616. #if LWIP_TCP
  617. struct pbuf *p;
  618. #endif /* LWIP_TCP */
  619. /* This runs in tcpip_thread, so we don't need to lock against rx packets */
  620. /* Delete and drain the recvmbox. */
  621. if (sys_mbox_valid(&conn->recvmbox)) {
  622. while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
  623. #if LWIP_TCP
  624. if (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP) {
  625. if(mem != NULL) {
  626. p = (struct pbuf*)mem;
  627. /* pcb might be set to NULL already by err_tcp() */
  628. if (conn->pcb.tcp != NULL) {
  629. tcp_recved(conn->pcb.tcp, p->tot_len);
  630. }
  631. pbuf_free(p);
  632. }
  633. } else
  634. #endif /* LWIP_TCP */
  635. {
  636. netbuf_delete((struct netbuf *)mem);
  637. }
  638. }
  639. sys_mbox_free(&conn->recvmbox);
  640. sys_mbox_set_invalid(&conn->recvmbox);
  641. }
  642. /* Delete and drain the acceptmbox. */
  643. #if LWIP_TCP
  644. if (sys_mbox_valid(&conn->acceptmbox)) {
  645. while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
  646. struct netconn *newconn = (struct netconn *)mem;
  647. /* Only tcp pcbs have an acceptmbox, so no need to check conn->type */
  648. /* pcb might be set to NULL already by err_tcp() */
  649. if (conn->pcb.tcp != NULL) {
  650. tcp_accepted(conn->pcb.tcp);
  651. }
  652. /* drain recvmbox */
  653. netconn_drain(newconn);
  654. if (newconn->pcb.tcp != NULL) {
  655. tcp_abort(newconn->pcb.tcp);
  656. newconn->pcb.tcp = NULL;
  657. }
  658. netconn_free(newconn);
  659. }
  660. sys_mbox_free(&conn->acceptmbox);
  661. sys_mbox_set_invalid(&conn->acceptmbox);
  662. }
  663. #endif /* LWIP_TCP */
  664. }
  665. #if LWIP_TCP
  666. /**
  667. * Internal helper function to close a TCP netconn: since this sometimes
  668. * doesn't work at the first attempt, this function is called from multiple
  669. * places.
  670. *
  671. * @param conn the TCP netconn to close
  672. */
  673. static void
  674. lwip_netconn_do_close_internal(struct netconn *conn)
  675. {
  676. err_t err;
  677. u8_t shut, shut_rx, shut_tx, close;
  678. LWIP_ASSERT("invalid conn", (conn != NULL));
  679. LWIP_ASSERT("this is for tcp netconns only", (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP));
  680. LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
  681. LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
  682. LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
  683. shut = conn->current_msg->msg.sd.shut;
  684. shut_rx = shut & NETCONN_SHUT_RD;
  685. shut_tx = shut & NETCONN_SHUT_WR;
  686. /* shutting down both ends is the same as closing */
  687. close = shut == NETCONN_SHUT_RDWR;
  688. /* Set back some callback pointers */
  689. if (close) {
  690. tcp_arg(conn->pcb.tcp, NULL);
  691. }
  692. if (conn->pcb.tcp->state == LISTEN) {
  693. tcp_accept(conn->pcb.tcp, NULL);
  694. } else {
  695. /* some callbacks have to be reset if tcp_close is not successful */
  696. if (shut_rx) {
  697. tcp_recv(conn->pcb.tcp, NULL);
  698. tcp_accept(conn->pcb.tcp, NULL);
  699. }
  700. if (shut_tx) {
  701. tcp_sent(conn->pcb.tcp, NULL);
  702. }
  703. if (close) {
  704. tcp_poll(conn->pcb.tcp, NULL, 4);
  705. tcp_err(conn->pcb.tcp, NULL);
  706. }
  707. }
  708. /* Try to close the connection */
  709. if (close) {
  710. err = tcp_close(conn->pcb.tcp);
  711. } else {
  712. err = tcp_shutdown(conn->pcb.tcp, shut_rx, shut_tx);
  713. }
  714. if (err == ERR_OK) {
  715. /* Closing succeeded */
  716. conn->current_msg->err = ERR_OK;
  717. conn->current_msg = NULL;
  718. conn->state = NETCONN_NONE;
  719. if (close) {
  720. /* Set back some callback pointers as conn is going away */
  721. conn->pcb.tcp = NULL;
  722. /* Trigger select() in socket layer. Make sure everybody notices activity
  723. on the connection, error first! */
  724. API_EVENT(conn, NETCONN_EVT_ERROR, 0);
  725. }
  726. if (shut_rx) {
  727. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  728. }
  729. if (shut_tx) {
  730. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  731. }
  732. /* wake up the application task */
  733. sys_sem_signal(&conn->op_completed);
  734. } else {
  735. /* Closing failed, restore some of the callbacks */
  736. /* Closing of listen pcb will never fail! */
  737. LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
  738. tcp_sent(conn->pcb.tcp, sent_tcp);
  739. tcp_poll(conn->pcb.tcp, poll_tcp, 4);
  740. tcp_err(conn->pcb.tcp, err_tcp);
  741. tcp_arg(conn->pcb.tcp, conn);
  742. /* don't restore recv callback: we don't want to receive any more data */
  743. }
  744. /* If closing didn't succeed, we get called again either
  745. from poll_tcp or from sent_tcp */
  746. }
  747. #endif /* LWIP_TCP */
  748. /**
  749. * Delete the pcb inside a netconn.
  750. * Called from netconn_delete.
  751. *
  752. * @param msg the api_msg_msg pointing to the connection
  753. */
  754. void
  755. lwip_netconn_do_delconn(struct api_msg_msg *msg)
  756. {
  757. /* @todo TCP: abort running write/connect? */
  758. if ((msg->conn->state != NETCONN_NONE) &&
  759. (msg->conn->state != NETCONN_LISTEN) &&
  760. (msg->conn->state != NETCONN_CONNECT)) {
  761. /* this only happens for TCP netconns */
  762. LWIP_ASSERT("NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP",
  763. NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP);
  764. msg->err = ERR_INPROGRESS;
  765. } else {
  766. LWIP_ASSERT("blocking connect in progress",
  767. (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
  768. /* Drain and delete mboxes */
  769. netconn_drain(msg->conn);
  770. if (msg->conn->pcb.tcp != NULL) {
  771. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  772. #if LWIP_RAW
  773. case NETCONN_RAW:
  774. raw_remove(msg->conn->pcb.raw);
  775. break;
  776. #endif /* LWIP_RAW */
  777. #if LWIP_UDP
  778. case NETCONN_UDP:
  779. msg->conn->pcb.udp->recv_arg = NULL;
  780. udp_remove(msg->conn->pcb.udp);
  781. break;
  782. #endif /* LWIP_UDP */
  783. #if LWIP_TCP
  784. case NETCONN_TCP:
  785. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  786. msg->conn->write_offset == 0);
  787. msg->conn->state = NETCONN_CLOSE;
  788. msg->msg.sd.shut = NETCONN_SHUT_RDWR;
  789. msg->conn->current_msg = msg;
  790. lwip_netconn_do_close_internal(msg->conn);
  791. /* API_EVENT is called inside lwip_netconn_do_close_internal, before releasing
  792. the application thread, so we can return at this point! */
  793. return;
  794. #endif /* LWIP_TCP */
  795. default:
  796. break;
  797. }
  798. msg->conn->pcb.tcp = NULL;
  799. }
  800. /* tcp netconns don't come here! */
  801. /* @todo: this lets select make the socket readable and writable,
  802. which is wrong! errfd instead? */
  803. API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
  804. API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
  805. }
  806. if (sys_sem_valid(&msg->conn->op_completed)) {
  807. sys_sem_signal(&msg->conn->op_completed);
  808. }
  809. }
  810. /**
  811. * Bind a pcb contained in a netconn
  812. * Called from netconn_bind.
  813. *
  814. * @param msg the api_msg_msg pointing to the connection and containing
  815. * the IP address and port to bind to
  816. */
  817. void
  818. lwip_netconn_do_bind(struct api_msg_msg *msg)
  819. {
  820. if (ERR_IS_FATAL(msg->conn->last_err)) {
  821. msg->err = msg->conn->last_err;
  822. } else {
  823. msg->err = ERR_VAL;
  824. if (msg->conn->pcb.tcp != NULL) {
  825. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  826. #if LWIP_RAW
  827. case NETCONN_RAW:
  828. msg->err = raw_bind(msg->conn->pcb.raw, API_EXPR_REF(msg->msg.bc.ipaddr));
  829. break;
  830. #endif /* LWIP_RAW */
  831. #if LWIP_UDP
  832. case NETCONN_UDP:
  833. msg->err = udp_bind(msg->conn->pcb.udp, API_EXPR_REF(msg->msg.bc.ipaddr), msg->msg.bc.port);
  834. break;
  835. #endif /* LWIP_UDP */
  836. #if LWIP_TCP
  837. case NETCONN_TCP:
  838. msg->err = tcp_bind(msg->conn->pcb.tcp, API_EXPR_REF(msg->msg.bc.ipaddr), msg->msg.bc.port);
  839. break;
  840. #endif /* LWIP_TCP */
  841. default:
  842. break;
  843. }
  844. }
  845. }
  846. TCPIP_APIMSG_ACK(msg);
  847. }
  848. #if LWIP_TCP
  849. /**
  850. * TCP callback function if a connection (opened by tcp_connect/lwip_netconn_do_connect) has
  851. * been established (or reset by the remote host).
  852. *
  853. * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
  854. */
  855. static err_t
  856. lwip_netconn_do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
  857. {
  858. struct netconn *conn;
  859. int was_blocking;
  860. LWIP_UNUSED_ARG(pcb);
  861. conn = (struct netconn *)arg;
  862. if (conn == NULL) {
  863. return ERR_VAL;
  864. }
  865. LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
  866. LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
  867. (conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
  868. if (conn->current_msg != NULL) {
  869. conn->current_msg->err = err;
  870. }
  871. if ((NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP) && (err == ERR_OK)) {
  872. setup_tcp(conn);
  873. }
  874. was_blocking = !IN_NONBLOCKING_CONNECT(conn);
  875. SET_NONBLOCKING_CONNECT(conn, 0);
  876. conn->current_msg = NULL;
  877. conn->state = NETCONN_NONE;
  878. if (!was_blocking) {
  879. NETCONN_SET_SAFE_ERR(conn, ERR_OK);
  880. }
  881. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  882. if (was_blocking) {
  883. sys_sem_signal(&conn->op_completed);
  884. }
  885. return ERR_OK;
  886. }
  887. #endif /* LWIP_TCP */
  888. /**
  889. * Connect a pcb contained inside a netconn
  890. * Called from netconn_connect.
  891. *
  892. * @param msg the api_msg_msg pointing to the connection and containing
  893. * the IP address and port to connect to
  894. */
  895. void
  896. lwip_netconn_do_connect(struct api_msg_msg *msg)
  897. {
  898. if (msg->conn->pcb.tcp == NULL) {
  899. /* This may happen when calling netconn_connect() a second time */
  900. msg->err = ERR_CLSD;
  901. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) {
  902. /* For TCP, netconn_connect() calls tcpip_apimsg(), so signal op_completed here. */
  903. sys_sem_signal(&msg->conn->op_completed);
  904. return;
  905. }
  906. } else {
  907. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  908. #if LWIP_RAW
  909. case NETCONN_RAW:
  910. msg->err = raw_connect(msg->conn->pcb.raw, API_EXPR_REF(msg->msg.bc.ipaddr));
  911. break;
  912. #endif /* LWIP_RAW */
  913. #if LWIP_UDP
  914. case NETCONN_UDP:
  915. msg->err = udp_connect(msg->conn->pcb.udp, API_EXPR_REF(msg->msg.bc.ipaddr), msg->msg.bc.port);
  916. break;
  917. #endif /* LWIP_UDP */
  918. #if LWIP_TCP
  919. case NETCONN_TCP:
  920. /* Prevent connect while doing any other action. */
  921. if (msg->conn->state != NETCONN_NONE) {
  922. msg->err = ERR_ISCONN;
  923. } else {
  924. setup_tcp(msg->conn);
  925. msg->err = tcp_connect(msg->conn->pcb.tcp, API_EXPR_REF(msg->msg.bc.ipaddr),
  926. msg->msg.bc.port, lwip_netconn_do_connected);
  927. if (msg->err == ERR_OK) {
  928. u8_t non_blocking = netconn_is_nonblocking(msg->conn);
  929. msg->conn->state = NETCONN_CONNECT;
  930. SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
  931. if (non_blocking) {
  932. msg->err = ERR_INPROGRESS;
  933. } else {
  934. msg->conn->current_msg = msg;
  935. /* sys_sem_signal() is called from lwip_netconn_do_connected (or err_tcp()),
  936. * when the connection is established! */
  937. return;
  938. }
  939. }
  940. }
  941. /* For TCP, netconn_connect() calls tcpip_apimsg(), so signal op_completed here. */
  942. sys_sem_signal(&msg->conn->op_completed);
  943. return;
  944. #endif /* LWIP_TCP */
  945. default:
  946. LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0));
  947. break;
  948. }
  949. }
  950. /* For all other protocols, netconn_connect() calls TCPIP_APIMSG(),
  951. so use TCPIP_APIMSG_ACK() here. */
  952. TCPIP_APIMSG_ACK(msg);
  953. }
  954. /**
  955. * Connect a pcb contained inside a netconn
  956. * Only used for UDP netconns.
  957. * Called from netconn_disconnect.
  958. *
  959. * @param msg the api_msg_msg pointing to the connection to disconnect
  960. */
  961. void
  962. lwip_netconn_do_disconnect(struct api_msg_msg *msg)
  963. {
  964. #if LWIP_UDP
  965. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  966. udp_disconnect(msg->conn->pcb.udp);
  967. msg->err = ERR_OK;
  968. } else
  969. #endif /* LWIP_UDP */
  970. {
  971. msg->err = ERR_VAL;
  972. }
  973. TCPIP_APIMSG_ACK(msg);
  974. }
  975. #if LWIP_TCP
  976. /**
  977. * Set a TCP pcb contained in a netconn into listen mode
  978. * Called from netconn_listen.
  979. *
  980. * @param msg the api_msg_msg pointing to the connection
  981. */
  982. void
  983. lwip_netconn_do_listen(struct api_msg_msg *msg)
  984. {
  985. if (ERR_IS_FATAL(msg->conn->last_err)) {
  986. msg->err = msg->conn->last_err;
  987. } else {
  988. msg->err = ERR_CONN;
  989. if (msg->conn->pcb.tcp != NULL) {
  990. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) {
  991. if (msg->conn->state == NETCONN_NONE) {
  992. struct tcp_pcb* lpcb;
  993. #if LWIP_IPV6
  994. if ((msg->conn->flags & NETCONN_FLAG_IPV6_V6ONLY) == 0) {
  995. #if TCP_LISTEN_BACKLOG
  996. lpcb = tcp_listen_dual_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
  997. #else /* TCP_LISTEN_BACKLOG */
  998. lpcb = tcp_listen_dual(msg->conn->pcb.tcp);
  999. #endif /* TCP_LISTEN_BACKLOG */
  1000. } else
  1001. #endif /* LWIP_IPV6 */
  1002. {
  1003. #if TCP_LISTEN_BACKLOG
  1004. lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
  1005. #else /* TCP_LISTEN_BACKLOG */
  1006. lpcb = tcp_listen(msg->conn->pcb.tcp);
  1007. #endif /* TCP_LISTEN_BACKLOG */
  1008. }
  1009. if (lpcb == NULL) {
  1010. /* in this case, the old pcb is still allocated */
  1011. msg->err = ERR_MEM;
  1012. } else {
  1013. /* delete the recvmbox and allocate the acceptmbox */
  1014. if (sys_mbox_valid(&msg->conn->recvmbox)) {
  1015. /** @todo: should we drain the recvmbox here? */
  1016. sys_mbox_free(&msg->conn->recvmbox);
  1017. sys_mbox_set_invalid(&msg->conn->recvmbox);
  1018. }
  1019. msg->err = ERR_OK;
  1020. if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
  1021. msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
  1022. }
  1023. if (msg->err == ERR_OK) {
  1024. msg->conn->state = NETCONN_LISTEN;
  1025. msg->conn->pcb.tcp = lpcb;
  1026. tcp_arg(msg->conn->pcb.tcp, msg->conn);
  1027. tcp_accept(msg->conn->pcb.tcp, accept_function);
  1028. } else {
  1029. /* since the old pcb is already deallocated, free lpcb now */
  1030. tcp_close(lpcb);
  1031. msg->conn->pcb.tcp = NULL;
  1032. }
  1033. }
  1034. }
  1035. } else {
  1036. msg->err = ERR_ARG;
  1037. }
  1038. }
  1039. }
  1040. TCPIP_APIMSG_ACK(msg);
  1041. }
  1042. #endif /* LWIP_TCP */
  1043. /**
  1044. * Send some data on a RAW or UDP pcb contained in a netconn
  1045. * Called from netconn_send
  1046. *
  1047. * @param msg the api_msg_msg pointing to the connection
  1048. */
  1049. void
  1050. lwip_netconn_do_send(struct api_msg_msg *msg)
  1051. {
  1052. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1053. msg->err = msg->conn->last_err;
  1054. } else {
  1055. msg->err = ERR_CONN;
  1056. if (msg->conn->pcb.tcp != NULL) {
  1057. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1058. #if LWIP_RAW
  1059. case NETCONN_RAW:
  1060. if (ipX_addr_isany(PCB_ISIPV6(msg->conn->pcb.ip), &msg->msg.b->addr)) {
  1061. msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
  1062. } else {
  1063. msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, ipX_2_ip(&msg->msg.b->addr));
  1064. }
  1065. break;
  1066. #endif
  1067. #if LWIP_UDP
  1068. case NETCONN_UDP:
  1069. #if LWIP_CHECKSUM_ON_COPY
  1070. if (ipX_addr_isany(PCB_ISIPV6(msg->conn->pcb.ip), &msg->msg.b->addr)) {
  1071. msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
  1072. msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
  1073. } else {
  1074. msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
  1075. ipX_2_ip(&msg->msg.b->addr), msg->msg.b->port,
  1076. msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
  1077. }
  1078. #else /* LWIP_CHECKSUM_ON_COPY */
  1079. if (ipX_addr_isany(PCB_ISIPV6(msg->conn->pcb.ip), &msg->msg.b->addr)) {
  1080. msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
  1081. } else {
  1082. msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, ipX_2_ip(&msg->msg.b->addr), msg->msg.b->port);
  1083. }
  1084. #endif /* LWIP_CHECKSUM_ON_COPY */
  1085. break;
  1086. #endif /* LWIP_UDP */
  1087. default:
  1088. break;
  1089. }
  1090. }
  1091. }
  1092. TCPIP_APIMSG_ACK(msg);
  1093. }
  1094. #if LWIP_TCP
  1095. /**
  1096. * Indicate data has been received from a TCP pcb contained in a netconn
  1097. * Called from netconn_recv
  1098. *
  1099. * @param msg the api_msg_msg pointing to the connection
  1100. */
  1101. void
  1102. lwip_netconn_do_recv(struct api_msg_msg *msg)
  1103. {
  1104. msg->err = ERR_OK;
  1105. if (msg->conn->pcb.tcp != NULL) {
  1106. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) {
  1107. #if TCP_LISTEN_BACKLOG
  1108. if (msg->conn->pcb.tcp->state == LISTEN) {
  1109. tcp_accepted(msg->conn->pcb.tcp);
  1110. } else
  1111. #endif /* TCP_LISTEN_BACKLOG */
  1112. {
  1113. u32_t remaining = msg->msg.r.len;
  1114. do {
  1115. u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining;
  1116. tcp_recved(msg->conn->pcb.tcp, recved);
  1117. remaining -= recved;
  1118. }while(remaining != 0);
  1119. }
  1120. }
  1121. }
  1122. TCPIP_APIMSG_ACK(msg);
  1123. }
  1124. /**
  1125. * See if more data needs to be written from a previous call to netconn_write.
  1126. * Called initially from lwip_netconn_do_write. If the first call can't send all data
  1127. * (because of low memory or empty send-buffer), this function is called again
  1128. * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
  1129. * blocking application thread (waiting in netconn_write) is released.
  1130. *
  1131. * @param conn netconn (that is currently in state NETCONN_WRITE) to process
  1132. * @return ERR_OK
  1133. * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
  1134. */
  1135. static err_t
  1136. lwip_netconn_do_writemore(struct netconn *conn)
  1137. {
  1138. err_t err;
  1139. void *dataptr;
  1140. u16_t len, available;
  1141. u8_t write_finished = 0;
  1142. size_t diff;
  1143. u8_t dontblock;
  1144. u8_t apiflags;
  1145. LWIP_ASSERT("conn != NULL", conn != NULL);
  1146. LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
  1147. LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
  1148. LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
  1149. LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
  1150. conn->write_offset < conn->current_msg->msg.w.len);
  1151. dontblock = netconn_is_nonblocking(conn) ||
  1152. (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
  1153. apiflags = conn->current_msg->msg.w.apiflags;
  1154. #if LWIP_SO_SNDTIMEO
  1155. if ((conn->send_timeout != 0) &&
  1156. ((s32_t)(sys_now() - conn->current_msg->msg.w.time_started) >= conn->send_timeout)) {
  1157. write_finished = 1;
  1158. if (conn->write_offset == 0) {
  1159. /* nothing has been written */
  1160. err = ERR_WOULDBLOCK;
  1161. conn->current_msg->msg.w.len = 0;
  1162. } else {
  1163. /* partial write */
  1164. err = ERR_OK;
  1165. conn->current_msg->msg.w.len = conn->write_offset;
  1166. conn->write_offset = 0;
  1167. }
  1168. } else
  1169. #endif /* LWIP_SO_SNDTIMEO */
  1170. {
  1171. dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
  1172. diff = conn->current_msg->msg.w.len - conn->write_offset;
  1173. if (diff > 0xffffUL) { /* max_u16_t */
  1174. len = 0xffff;
  1175. #if LWIP_TCPIP_CORE_LOCKING
  1176. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1177. #endif
  1178. apiflags |= TCP_WRITE_FLAG_MORE;
  1179. } else {
  1180. len = (u16_t)diff;
  1181. }
  1182. available = tcp_sndbuf(conn->pcb.tcp);
  1183. if (available < len) {
  1184. /* don't try to write more than sendbuf */
  1185. len = available;
  1186. if (dontblock){
  1187. if (!len) {
  1188. err = ERR_WOULDBLOCK;
  1189. goto err_mem;
  1190. }
  1191. } else {
  1192. #if LWIP_TCPIP_CORE_LOCKING
  1193. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1194. #endif
  1195. apiflags |= TCP_WRITE_FLAG_MORE;
  1196. }
  1197. }
  1198. LWIP_ASSERT("lwip_netconn_do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
  1199. err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
  1200. /* if OK or memory error, check available space */
  1201. if ((err == ERR_OK) || (err == ERR_MEM)) {
  1202. err_mem:
  1203. if (dontblock && (len < conn->current_msg->msg.w.len)) {
  1204. /* non-blocking write did not write everything: mark the pcb non-writable
  1205. and let poll_tcp check writable space to mark the pcb writable again */
  1206. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  1207. conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
  1208. } else if ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
  1209. (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT)) {
  1210. /* The queued byte- or pbuf-count exceeds the configured low-water limit,
  1211. let select mark this pcb as non-writable. */
  1212. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  1213. }
  1214. }
  1215. if (err == ERR_OK) {
  1216. conn->write_offset += len;
  1217. if ((conn->write_offset == conn->current_msg->msg.w.len) || dontblock) {
  1218. /* return sent length */
  1219. conn->current_msg->msg.w.len = conn->write_offset;
  1220. /* everything was written */
  1221. write_finished = 1;
  1222. conn->write_offset = 0;
  1223. }
  1224. tcp_output(conn->pcb.tcp);
  1225. } else if ((err == ERR_MEM) && !dontblock) {
  1226. /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
  1227. we do NOT return to the application thread, since ERR_MEM is
  1228. only a temporary error! */
  1229. /* tcp_write returned ERR_MEM, try tcp_output anyway */
  1230. tcp_output(conn->pcb.tcp);
  1231. #if LWIP_TCPIP_CORE_LOCKING
  1232. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1233. #endif
  1234. } else {
  1235. /* On errors != ERR_MEM, we don't try writing any more but return
  1236. the error to the application thread. */
  1237. write_finished = 1;
  1238. conn->current_msg->msg.w.len = 0;
  1239. }
  1240. }
  1241. if (write_finished) {
  1242. /* everything was written: set back connection state
  1243. and back to application task */
  1244. conn->current_msg->err = err;
  1245. conn->current_msg = NULL;
  1246. conn->state = NETCONN_NONE;
  1247. #if LWIP_TCPIP_CORE_LOCKING
  1248. if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
  1249. #endif
  1250. {
  1251. sys_sem_signal(&conn->op_completed);
  1252. }
  1253. }
  1254. #if LWIP_TCPIP_CORE_LOCKING
  1255. else
  1256. return ERR_MEM;
  1257. #endif
  1258. return ERR_OK;
  1259. }
  1260. #endif /* LWIP_TCP */
  1261. /**
  1262. * Send some data on a TCP pcb contained in a netconn
  1263. * Called from netconn_write
  1264. *
  1265. * @param msg the api_msg_msg pointing to the connection
  1266. */
  1267. void
  1268. lwip_netconn_do_write(struct api_msg_msg *msg)
  1269. {
  1270. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1271. msg->err = msg->conn->last_err;
  1272. } else {
  1273. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP) {
  1274. #if LWIP_TCP
  1275. if (msg->conn->state != NETCONN_NONE) {
  1276. /* netconn is connecting, closing or in blocking write */
  1277. msg->err = ERR_INPROGRESS;
  1278. } else if (msg->conn->pcb.tcp != NULL) {
  1279. msg->conn->state = NETCONN_WRITE;
  1280. /* set all the variables used by lwip_netconn_do_writemore */
  1281. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  1282. msg->conn->write_offset == 0);
  1283. LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
  1284. msg->conn->current_msg = msg;
  1285. msg->conn->write_offset = 0;
  1286. #if LWIP_TCPIP_CORE_LOCKING
  1287. msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
  1288. if (lwip_netconn_do_writemore(msg->conn) != ERR_OK) {
  1289. LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
  1290. UNLOCK_TCPIP_CORE();
  1291. sys_arch_sem_wait(&msg->conn->op_completed, 0);
  1292. LOCK_TCPIP_CORE();
  1293. LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
  1294. }
  1295. #else /* LWIP_TCPIP_CORE_LOCKING */
  1296. lwip_netconn_do_writemore(msg->conn);
  1297. #endif /* LWIP_TCPIP_CORE_LOCKING */
  1298. /* for both cases: if lwip_netconn_do_writemore was called, don't ACK the APIMSG
  1299. since lwip_netconn_do_writemore ACKs it! */
  1300. return;
  1301. } else {
  1302. msg->err = ERR_CONN;
  1303. }
  1304. #else /* LWIP_TCP */
  1305. msg->err = ERR_VAL;
  1306. #endif /* LWIP_TCP */
  1307. #if (LWIP_UDP || LWIP_RAW)
  1308. } else {
  1309. msg->err = ERR_VAL;
  1310. #endif /* (LWIP_UDP || LWIP_RAW) */
  1311. }
  1312. }
  1313. TCPIP_APIMSG_ACK(msg);
  1314. }
  1315. /**
  1316. * Return a connection's local or remote address
  1317. * Called from netconn_getaddr
  1318. *
  1319. * @param msg the api_msg_msg pointing to the connection
  1320. */
  1321. void
  1322. lwip_netconn_do_getaddr(struct api_msg_msg *msg)
  1323. {
  1324. if (msg->conn->pcb.ip != NULL) {
  1325. if (msg->msg.ad.local) {
  1326. ipX_addr_copy(PCB_ISIPV6(msg->conn->pcb.ip), API_EXPR_DEREF(msg->msg.ad.ipaddr),
  1327. msg->conn->pcb.ip->local_ip);
  1328. } else {
  1329. ipX_addr_copy(PCB_ISIPV6(msg->conn->pcb.ip), API_EXPR_DEREF(msg->msg.ad.ipaddr),
  1330. msg->conn->pcb.ip->remote_ip);
  1331. }
  1332. msg->err = ERR_OK;
  1333. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1334. #if LWIP_RAW
  1335. case NETCONN_RAW:
  1336. if (msg->msg.ad.local) {
  1337. API_EXPR_DEREF(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
  1338. } else {
  1339. /* return an error as connecting is only a helper for upper layers */
  1340. msg->err = ERR_CONN;
  1341. }
  1342. break;
  1343. #endif /* LWIP_RAW */
  1344. #if LWIP_UDP
  1345. case NETCONN_UDP:
  1346. if (msg->msg.ad.local) {
  1347. API_EXPR_DEREF(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
  1348. } else {
  1349. if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
  1350. msg->err = ERR_CONN;
  1351. } else {
  1352. API_EXPR_DEREF(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
  1353. }
  1354. }
  1355. break;
  1356. #endif /* LWIP_UDP */
  1357. #if LWIP_TCP
  1358. case NETCONN_TCP:
  1359. if ((msg->msg.ad.local == 0) &&
  1360. ((msg->conn->pcb.tcp->state == CLOSED) || (msg->conn->pcb.tcp->state == LISTEN))) {
  1361. /* pcb is not connected and remote name is requested */
  1362. msg->err = ERR_CONN;
  1363. } else {
  1364. API_EXPR_DEREF(msg->msg.ad.port) = (msg->msg.ad.local ? msg->conn->pcb.tcp->local_port : msg->conn->pcb.tcp->remote_port);
  1365. }
  1366. break;
  1367. #endif /* LWIP_TCP */
  1368. default:
  1369. LWIP_ASSERT("invalid netconn_type", 0);
  1370. break;
  1371. }
  1372. } else {
  1373. msg->err = ERR_CONN;
  1374. }
  1375. TCPIP_APIMSG_ACK(msg);
  1376. }
  1377. /**
  1378. * Close a TCP pcb contained in a netconn
  1379. * Called from netconn_close
  1380. *
  1381. * @param msg the api_msg_msg pointing to the connection
  1382. */
  1383. void
  1384. lwip_netconn_do_close(struct api_msg_msg *msg)
  1385. {
  1386. #if LWIP_TCP
  1387. /* @todo: abort running write/connect? */
  1388. if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
  1389. /* this only happens for TCP netconns */
  1390. LWIP_ASSERT("NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP",
  1391. NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP);
  1392. msg->err = ERR_INPROGRESS;
  1393. } else if ((msg->conn->pcb.tcp != NULL) && (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_TCP)) {
  1394. if ((msg->msg.sd.shut != NETCONN_SHUT_RDWR) && (msg->conn->state == NETCONN_LISTEN)) {
  1395. /* LISTEN doesn't support half shutdown */
  1396. msg->err = ERR_CONN;
  1397. } else {
  1398. if (msg->msg.sd.shut & NETCONN_SHUT_RD) {
  1399. /* Drain and delete mboxes */
  1400. netconn_drain(msg->conn);
  1401. }
  1402. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  1403. msg->conn->write_offset == 0);
  1404. msg->conn->state = NETCONN_CLOSE;
  1405. msg->conn->current_msg = msg;
  1406. lwip_netconn_do_close_internal(msg->conn);
  1407. /* for tcp netconns, lwip_netconn_do_close_internal ACKs the message */
  1408. return;
  1409. }
  1410. } else
  1411. #endif /* LWIP_TCP */
  1412. {
  1413. msg->err = ERR_VAL;
  1414. }
  1415. sys_sem_signal(&msg->conn->op_completed);
  1416. }
  1417. #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
  1418. /**
  1419. * Join multicast groups for UDP netconns.
  1420. * Called from netconn_join_leave_group
  1421. *
  1422. * @param msg the api_msg_msg pointing to the connection
  1423. */
  1424. void
  1425. lwip_netconn_do_join_leave_group(struct api_msg_msg *msg)
  1426. {
  1427. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1428. msg->err = msg->conn->last_err;
  1429. } else {
  1430. if (msg->conn->pcb.tcp != NULL) {
  1431. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  1432. #if LWIP_UDP
  1433. #if LWIP_IPV6 && LWIP_IPV6_MLD
  1434. if (PCB_ISIPV6(msg->conn->pcb.udp)) {
  1435. if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
  1436. msg->err = mld6_joingroup(ipX_2_ip6(API_EXPR_REF(msg->msg.jl.netif_addr)),
  1437. ipX_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
  1438. } else {
  1439. msg->err = mld6_leavegroup(ipX_2_ip6(API_EXPR_REF(msg->msg.jl.netif_addr)),
  1440. ipX_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
  1441. }
  1442. }
  1443. else
  1444. #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
  1445. {
  1446. #if LWIP_IGMP
  1447. if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
  1448. msg->err = igmp_joingroup(ipX_2_ip(API_EXPR_REF(msg->msg.jl.netif_addr)),
  1449. ipX_2_ip(API_EXPR_REF(msg->msg.jl.multiaddr)));
  1450. } else {
  1451. msg->err = igmp_leavegroup(ipX_2_ip(API_EXPR_REF(msg->msg.jl.netif_addr)),
  1452. ipX_2_ip(API_EXPR_REF(msg->msg.jl.multiaddr)));
  1453. }
  1454. #endif /* LWIP_IGMP */
  1455. }
  1456. #endif /* LWIP_UDP */
  1457. #if (LWIP_TCP || LWIP_RAW)
  1458. } else {
  1459. msg->err = ERR_VAL;
  1460. #endif /* (LWIP_TCP || LWIP_RAW) */
  1461. }
  1462. } else {
  1463. msg->err = ERR_CONN;
  1464. }
  1465. }
  1466. TCPIP_APIMSG_ACK(msg);
  1467. }
  1468. #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
  1469. #if LWIP_DNS
  1470. /**
  1471. * Callback function that is called when DNS name is resolved
  1472. * (or on timeout). A waiting application thread is waked up by
  1473. * signaling the semaphore.
  1474. */
  1475. static void
  1476. lwip_netconn_do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  1477. {
  1478. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1479. LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
  1480. LWIP_UNUSED_ARG(name);
  1481. if (ipaddr == NULL) {
  1482. /* timeout or memory error */
  1483. API_EXPR_DEREF(msg->err) = ERR_VAL;
  1484. } else {
  1485. /* address was resolved */
  1486. API_EXPR_DEREF(msg->err) = ERR_OK;
  1487. API_EXPR_DEREF(msg->addr) = *ipaddr;
  1488. }
  1489. /* wake up the application task waiting in netconn_gethostbyname */
  1490. sys_sem_signal(API_EXPR_REF(msg->sem));
  1491. }
  1492. /**
  1493. * Execute a DNS query
  1494. * Called from netconn_gethostbyname
  1495. *
  1496. * @param arg the dns_api_msg pointing to the query
  1497. */
  1498. void
  1499. lwip_netconn_do_gethostbyname(void *arg)
  1500. {
  1501. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1502. API_EXPR_DEREF(msg->err) = dns_gethostbyname(msg->name, API_EXPR_REF(msg->addr), lwip_netconn_do_dns_found, msg);
  1503. if (API_EXPR_DEREF(msg->err) != ERR_INPROGRESS) {
  1504. /* on error or immediate success, wake up the application
  1505. * task waiting in netconn_gethostbyname */
  1506. sys_sem_signal(API_EXPR_REF(msg->sem));
  1507. }
  1508. }
  1509. #endif /* LWIP_DNS */
  1510. #endif /* LWIP_NETCONN */