api_msg.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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 <string.h>
  49. /* forward declarations */
  50. #if LWIP_TCP
  51. static err_t do_writemore(struct netconn *conn);
  52. static void do_close_internal(struct netconn *conn);
  53. #endif
  54. #if LWIP_RAW
  55. /**
  56. * Receive callback function for RAW netconns.
  57. * Doesn't 'eat' the packet, only references it and sends it to
  58. * conn->recvmbox
  59. *
  60. * @see raw.h (struct raw_pcb.recv) for parameters and return value
  61. */
  62. static u8_t
  63. recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
  64. struct ip_addr *addr)
  65. {
  66. struct pbuf *q;
  67. struct netbuf *buf;
  68. struct netconn *conn;
  69. #if LWIP_SO_RCVBUF
  70. int recv_avail;
  71. #endif /* LWIP_SO_RCVBUF */
  72. LWIP_UNUSED_ARG(addr);
  73. conn = arg;
  74. #if LWIP_SO_RCVBUF
  75. SYS_ARCH_GET(conn->recv_avail, recv_avail);
  76. if ((conn != NULL) && (conn->recvmbox != SYS_MBOX_NULL) &&
  77. ((recv_avail + (int)(p->tot_len)) <= conn->recv_bufsize)) {
  78. #else /* LWIP_SO_RCVBUF */
  79. if ((conn != NULL) && (conn->recvmbox != SYS_MBOX_NULL)) {
  80. #endif /* LWIP_SO_RCVBUF */
  81. /* copy the whole packet into new pbufs */
  82. q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
  83. if(q != NULL) {
  84. if (pbuf_copy(q, p) != ERR_OK) {
  85. pbuf_free(q);
  86. q = NULL;
  87. }
  88. }
  89. if(q != NULL) {
  90. buf = memp_malloc(MEMP_NETBUF);
  91. if (buf == NULL) {
  92. pbuf_free(q);
  93. return 0;
  94. }
  95. buf->p = q;
  96. buf->ptr = q;
  97. buf->addr = &(((struct ip_hdr*)(q->payload))->src);
  98. buf->port = pcb->protocol;
  99. if (sys_mbox_trypost(conn->recvmbox, buf) != ERR_OK) {
  100. netbuf_delete(buf);
  101. return 0;
  102. } else {
  103. SYS_ARCH_INC(conn->recv_avail, q->tot_len);
  104. /* Register event with callback */
  105. API_EVENT(conn, NETCONN_EVT_RCVPLUS, q->tot_len);
  106. }
  107. }
  108. }
  109. return 0; /* do not eat the packet */
  110. }
  111. #endif /* LWIP_RAW*/
  112. #if LWIP_UDP
  113. /**
  114. * Receive callback function for UDP netconns.
  115. * Posts the packet to conn->recvmbox or deletes it on memory error.
  116. *
  117. * @see udp.h (struct udp_pcb.recv) for parameters
  118. */
  119. static void
  120. recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  121. struct ip_addr *addr, u16_t port)
  122. {
  123. struct netbuf *buf;
  124. struct netconn *conn;
  125. #if LWIP_SO_RCVBUF
  126. int recv_avail;
  127. #endif /* LWIP_SO_RCVBUF */
  128. LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
  129. LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
  130. LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
  131. conn = arg;
  132. LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
  133. #if LWIP_SO_RCVBUF
  134. SYS_ARCH_GET(conn->recv_avail, recv_avail);
  135. if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL) ||
  136. ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
  137. #else /* LWIP_SO_RCVBUF */
  138. if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL)) {
  139. #endif /* LWIP_SO_RCVBUF */
  140. pbuf_free(p);
  141. return;
  142. }
  143. buf = memp_malloc(MEMP_NETBUF);
  144. if (buf == NULL) {
  145. pbuf_free(p);
  146. return;
  147. } else {
  148. buf->p = p;
  149. buf->ptr = p;
  150. buf->addr = addr;
  151. buf->port = port;
  152. #if LWIP_NETBUF_RECVINFO
  153. {
  154. const struct ip_hdr* iphdr = ip_current_header();
  155. /* get the UDP header - always in the first pbuf, ensured by udp_input */
  156. const struct udp_hdr* udphdr = (void*)(((char*)iphdr) + IPH_LEN(iphdr));
  157. buf->toaddr = (struct ip_addr*)&iphdr->dest;
  158. buf->toport = udphdr->dest;
  159. }
  160. #endif /* LWIP_NETBUF_RECVINFO */
  161. }
  162. if (sys_mbox_trypost(conn->recvmbox, buf) != ERR_OK) {
  163. netbuf_delete(buf);
  164. return;
  165. } else {
  166. SYS_ARCH_INC(conn->recv_avail, p->tot_len);
  167. /* Register event with callback */
  168. API_EVENT(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
  169. }
  170. }
  171. #endif /* LWIP_UDP */
  172. #if LWIP_TCP
  173. /**
  174. * Receive callback function for TCP netconns.
  175. * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
  176. *
  177. * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
  178. */
  179. static err_t
  180. recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  181. {
  182. struct netconn *conn;
  183. u16_t len;
  184. LWIP_UNUSED_ARG(pcb);
  185. LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
  186. LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
  187. conn = arg;
  188. LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
  189. if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL)) {
  190. return ERR_VAL;
  191. }
  192. conn->err = err;
  193. if (p != NULL) {
  194. len = p->tot_len;
  195. SYS_ARCH_INC(conn->recv_avail, len);
  196. } else {
  197. len = 0;
  198. }
  199. if (sys_mbox_trypost(conn->recvmbox, p) != ERR_OK) {
  200. return ERR_MEM;
  201. } else {
  202. /* Register event with callback */
  203. API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
  204. }
  205. return ERR_OK;
  206. }
  207. /**
  208. * Poll callback function for TCP netconns.
  209. * Wakes up an application thread that waits for a connection to close
  210. * or data to be sent. The application thread then takes the
  211. * appropriate action to go on.
  212. *
  213. * Signals the conn->sem.
  214. * netconn_close waits for conn->sem if closing failed.
  215. *
  216. * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
  217. */
  218. static err_t
  219. poll_tcp(void *arg, struct tcp_pcb *pcb)
  220. {
  221. struct netconn *conn = arg;
  222. LWIP_UNUSED_ARG(pcb);
  223. LWIP_ASSERT("conn != NULL", (conn != NULL));
  224. if (conn->state == NETCONN_WRITE) {
  225. do_writemore(conn);
  226. } else if (conn->state == NETCONN_CLOSE) {
  227. do_close_internal(conn);
  228. }
  229. return ERR_OK;
  230. }
  231. /**
  232. * Sent callback function for TCP netconns.
  233. * Signals the conn->sem and calls API_EVENT.
  234. * netconn_write waits for conn->sem if send buffer is low.
  235. *
  236. * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
  237. */
  238. static err_t
  239. sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
  240. {
  241. struct netconn *conn = arg;
  242. LWIP_UNUSED_ARG(pcb);
  243. LWIP_ASSERT("conn != NULL", (conn != NULL));
  244. if (conn->state == NETCONN_WRITE) {
  245. LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
  246. do_writemore(conn);
  247. } else if (conn->state == NETCONN_CLOSE) {
  248. do_close_internal(conn);
  249. }
  250. if (conn) {
  251. if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT)) {
  252. API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
  253. }
  254. }
  255. return ERR_OK;
  256. }
  257. /**
  258. * Error callback function for TCP netconns.
  259. * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
  260. * The application thread has then to decide what to do.
  261. *
  262. * @see tcp.h (struct tcp_pcb.err) for parameters
  263. */
  264. static void
  265. err_tcp(void *arg, err_t err)
  266. {
  267. struct netconn *conn;
  268. conn = arg;
  269. LWIP_ASSERT("conn != NULL", (conn != NULL));
  270. conn->pcb.tcp = NULL;
  271. conn->err = err;
  272. if (conn->recvmbox != SYS_MBOX_NULL) {
  273. /* Register event with callback */
  274. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  275. sys_mbox_post(conn->recvmbox, NULL);
  276. }
  277. if (conn->op_completed != SYS_SEM_NULL && conn->state == NETCONN_CONNECT) {
  278. conn->state = NETCONN_NONE;
  279. sys_sem_signal(conn->op_completed);
  280. }
  281. if (conn->acceptmbox != SYS_MBOX_NULL) {
  282. /* Register event with callback */
  283. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  284. sys_mbox_post(conn->acceptmbox, NULL);
  285. }
  286. if ((conn->state == NETCONN_WRITE) || (conn->state == NETCONN_CLOSE)) {
  287. /* calling do_writemore/do_close_internal is not necessary
  288. since the pcb has already been deleted! */
  289. conn->state = NETCONN_NONE;
  290. /* wake up the waiting task */
  291. sys_sem_signal(conn->op_completed);
  292. }
  293. }
  294. /**
  295. * Setup a tcp_pcb with the correct callback function pointers
  296. * and their arguments.
  297. *
  298. * @param conn the TCP netconn to setup
  299. */
  300. static void
  301. setup_tcp(struct netconn *conn)
  302. {
  303. struct tcp_pcb *pcb;
  304. pcb = conn->pcb.tcp;
  305. tcp_arg(pcb, conn);
  306. tcp_recv(pcb, recv_tcp);
  307. tcp_sent(pcb, sent_tcp);
  308. tcp_poll(pcb, poll_tcp, 4);
  309. tcp_err(pcb, err_tcp);
  310. }
  311. /**
  312. * Accept callback function for TCP netconns.
  313. * Allocates a new netconn and posts that to conn->acceptmbox.
  314. *
  315. * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
  316. */
  317. static err_t
  318. accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
  319. {
  320. struct netconn *newconn;
  321. struct netconn *conn;
  322. #if API_MSG_DEBUG
  323. #if TCP_DEBUG
  324. tcp_debug_print_state(newpcb->state);
  325. #endif /* TCP_DEBUG */
  326. #endif /* API_MSG_DEBUG */
  327. conn = (struct netconn *)arg;
  328. LWIP_ERROR("accept_function: invalid conn->acceptmbox",
  329. conn->acceptmbox != SYS_MBOX_NULL, return ERR_VAL;);
  330. /* We have to set the callback here even though
  331. * the new socket is unknown. conn->socket is marked as -1. */
  332. newconn = netconn_alloc(conn->type, conn->callback);
  333. if (newconn == NULL) {
  334. return ERR_MEM;
  335. }
  336. newconn->pcb.tcp = newpcb;
  337. setup_tcp(newconn);
  338. newconn->err = err;
  339. if (sys_mbox_trypost(conn->acceptmbox, newconn) != ERR_OK) {
  340. /* When returning != ERR_OK, the connection is aborted in tcp_process(),
  341. so do nothing here! */
  342. newconn->pcb.tcp = NULL;
  343. netconn_free(newconn);
  344. return ERR_MEM;
  345. } else {
  346. /* Register event with callback */
  347. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  348. }
  349. return ERR_OK;
  350. }
  351. #endif /* LWIP_TCP */
  352. /**
  353. * Create a new pcb of a specific type.
  354. * Called from do_newconn().
  355. *
  356. * @param msg the api_msg_msg describing the connection type
  357. * @return msg->conn->err, but the return value is currently ignored
  358. */
  359. static err_t
  360. pcb_new(struct api_msg_msg *msg)
  361. {
  362. msg->conn->err = ERR_OK;
  363. LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
  364. /* Allocate a PCB for this connection */
  365. switch(NETCONNTYPE_GROUP(msg->conn->type)) {
  366. #if LWIP_RAW
  367. case NETCONN_RAW:
  368. msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
  369. if(msg->conn->pcb.raw == NULL) {
  370. msg->conn->err = ERR_MEM;
  371. break;
  372. }
  373. raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
  374. break;
  375. #endif /* LWIP_RAW */
  376. #if LWIP_UDP
  377. case NETCONN_UDP:
  378. msg->conn->pcb.udp = udp_new();
  379. if(msg->conn->pcb.udp == NULL) {
  380. msg->conn->err = ERR_MEM;
  381. break;
  382. }
  383. #if LWIP_UDPLITE
  384. if (msg->conn->type==NETCONN_UDPLITE) {
  385. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
  386. }
  387. #endif /* LWIP_UDPLITE */
  388. if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
  389. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
  390. }
  391. udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
  392. break;
  393. #endif /* LWIP_UDP */
  394. #if LWIP_TCP
  395. case NETCONN_TCP:
  396. msg->conn->pcb.tcp = tcp_new();
  397. if(msg->conn->pcb.tcp == NULL) {
  398. msg->conn->err = ERR_MEM;
  399. break;
  400. }
  401. setup_tcp(msg->conn);
  402. break;
  403. #endif /* LWIP_TCP */
  404. default:
  405. /* Unsupported netconn type, e.g. protocol disabled */
  406. msg->conn->err = ERR_VAL;
  407. break;
  408. }
  409. return msg->conn->err;
  410. }
  411. /**
  412. * Create a new pcb of a specific type inside a netconn.
  413. * Called from netconn_new_with_proto_and_callback.
  414. *
  415. * @param msg the api_msg_msg describing the connection type
  416. */
  417. void
  418. do_newconn(struct api_msg_msg *msg)
  419. {
  420. if(msg->conn->pcb.tcp == NULL) {
  421. pcb_new(msg);
  422. }
  423. /* Else? This "new" connection already has a PCB allocated. */
  424. /* Is this an error condition? Should it be deleted? */
  425. /* We currently just are happy and return. */
  426. TCPIP_APIMSG_ACK(msg);
  427. }
  428. /**
  429. * Create a new netconn (of a specific type) that has a callback function.
  430. * The corresponding pcb is NOT created!
  431. *
  432. * @param t the type of 'connection' to create (@see enum netconn_type)
  433. * @param proto the IP protocol for RAW IP pcbs
  434. * @param callback a function to call on status changes (RX available, TX'ed)
  435. * @return a newly allocated struct netconn or
  436. * NULL on memory error
  437. */
  438. struct netconn*
  439. netconn_alloc(enum netconn_type t, netconn_callback callback)
  440. {
  441. struct netconn *conn;
  442. int size;
  443. conn = memp_malloc(MEMP_NETCONN);
  444. if (conn == NULL) {
  445. return NULL;
  446. }
  447. conn->err = ERR_OK;
  448. conn->type = t;
  449. conn->pcb.tcp = NULL;
  450. #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
  451. (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
  452. size = DEFAULT_RAW_RECVMBOX_SIZE;
  453. #else
  454. switch(NETCONNTYPE_GROUP(t)) {
  455. #if LWIP_RAW
  456. case NETCONN_RAW:
  457. size = DEFAULT_RAW_RECVMBOX_SIZE;
  458. break;
  459. #endif /* LWIP_RAW */
  460. #if LWIP_UDP
  461. case NETCONN_UDP:
  462. size = DEFAULT_UDP_RECVMBOX_SIZE;
  463. break;
  464. #endif /* LWIP_UDP */
  465. #if LWIP_TCP
  466. case NETCONN_TCP:
  467. size = DEFAULT_TCP_RECVMBOX_SIZE;
  468. break;
  469. #endif /* LWIP_TCP */
  470. default:
  471. LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
  472. break;
  473. }
  474. #endif
  475. if ((conn->op_completed = sys_sem_new(0)) == SYS_SEM_NULL) {
  476. memp_free(MEMP_NETCONN, conn);
  477. return NULL;
  478. }
  479. if ((conn->recvmbox = sys_mbox_new(size)) == SYS_MBOX_NULL) {
  480. sys_sem_free(conn->op_completed);
  481. memp_free(MEMP_NETCONN, conn);
  482. return NULL;
  483. }
  484. conn->acceptmbox = SYS_MBOX_NULL;
  485. conn->state = NETCONN_NONE;
  486. /* initialize socket to -1 since 0 is a valid socket */
  487. conn->socket = -1;
  488. conn->callback = callback;
  489. conn->recv_avail = 0;
  490. #if LWIP_TCP
  491. conn->write_msg = NULL;
  492. conn->write_offset = 0;
  493. #if LWIP_TCPIP_CORE_LOCKING
  494. conn->write_delayed = 0;
  495. #endif /* LWIP_TCPIP_CORE_LOCKING */
  496. #endif /* LWIP_TCP */
  497. #if LWIP_SO_RCVTIMEO
  498. conn->recv_timeout = 0;
  499. #endif /* LWIP_SO_RCVTIMEO */
  500. #if LWIP_SO_RCVBUF
  501. conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
  502. #endif /* LWIP_SO_RCVBUF */
  503. return conn;
  504. }
  505. /**
  506. * Delete a netconn and all its resources.
  507. * The pcb is NOT freed (since we might not be in the right thread context do this).
  508. *
  509. * @param conn the netconn to free
  510. */
  511. void
  512. netconn_free(struct netconn *conn)
  513. {
  514. void *mem;
  515. LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
  516. /* Drain the recvmbox. */
  517. if (conn->recvmbox != SYS_MBOX_NULL) {
  518. while (sys_mbox_tryfetch(conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
  519. if (conn->type == NETCONN_TCP) {
  520. if(mem != NULL) {
  521. pbuf_free((struct pbuf *)mem);
  522. }
  523. } else {
  524. netbuf_delete((struct netbuf *)mem);
  525. }
  526. }
  527. sys_mbox_free(conn->recvmbox);
  528. conn->recvmbox = SYS_MBOX_NULL;
  529. }
  530. /* Drain the acceptmbox. */
  531. if (conn->acceptmbox != SYS_MBOX_NULL) {
  532. while (sys_mbox_tryfetch(conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
  533. netconn_delete((struct netconn *)mem);
  534. }
  535. sys_mbox_free(conn->acceptmbox);
  536. conn->acceptmbox = SYS_MBOX_NULL;
  537. }
  538. sys_sem_free(conn->op_completed);
  539. conn->op_completed = SYS_SEM_NULL;
  540. memp_free(MEMP_NETCONN, conn);
  541. }
  542. #if LWIP_TCP
  543. /**
  544. * Internal helper function to close a TCP netconn: since this sometimes
  545. * doesn't work at the first attempt, this function is called from multiple
  546. * places.
  547. *
  548. * @param conn the TCP netconn to close
  549. */
  550. static void
  551. do_close_internal(struct netconn *conn)
  552. {
  553. err_t err;
  554. LWIP_ASSERT("invalid conn", (conn != NULL));
  555. LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
  556. LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
  557. LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
  558. /* Set back some callback pointers */
  559. tcp_arg(conn->pcb.tcp, NULL);
  560. if (conn->pcb.tcp->state == LISTEN) {
  561. tcp_accept(conn->pcb.tcp, NULL);
  562. } else {
  563. tcp_recv(conn->pcb.tcp, NULL);
  564. tcp_accept(conn->pcb.tcp, NULL);
  565. /* some callbacks have to be reset if tcp_close is not successful */
  566. tcp_sent(conn->pcb.tcp, NULL);
  567. tcp_poll(conn->pcb.tcp, NULL, 4);
  568. tcp_err(conn->pcb.tcp, NULL);
  569. }
  570. /* Try to close the connection */
  571. err = tcp_close(conn->pcb.tcp);
  572. if (err == ERR_OK) {
  573. /* Closing succeeded */
  574. conn->state = NETCONN_NONE;
  575. /* Set back some callback pointers as conn is going away */
  576. conn->pcb.tcp = NULL;
  577. conn->err = ERR_OK;
  578. /* Trigger select() in socket layer. This send should something else so the
  579. errorfd is set, not the read and write fd! */
  580. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  581. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  582. /* wake up the application task */
  583. sys_sem_signal(conn->op_completed);
  584. } else {
  585. /* Closing failed, restore some of the callbacks */
  586. /* Closing of listen pcb will never fail! */
  587. LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
  588. tcp_sent(conn->pcb.tcp, sent_tcp);
  589. tcp_poll(conn->pcb.tcp, poll_tcp, 4);
  590. tcp_err(conn->pcb.tcp, err_tcp);
  591. tcp_arg(conn->pcb.tcp, conn);
  592. }
  593. /* If closing didn't succeed, we get called again either
  594. from poll_tcp or from sent_tcp */
  595. }
  596. #endif /* LWIP_TCP */
  597. /**
  598. * Delete the pcb inside a netconn.
  599. * Called from netconn_delete.
  600. *
  601. * @param msg the api_msg_msg pointing to the connection
  602. */
  603. void
  604. do_delconn(struct api_msg_msg *msg)
  605. {
  606. if (msg->conn->pcb.tcp != NULL) {
  607. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  608. #if LWIP_RAW
  609. case NETCONN_RAW:
  610. raw_remove(msg->conn->pcb.raw);
  611. break;
  612. #endif /* LWIP_RAW */
  613. #if LWIP_UDP
  614. case NETCONN_UDP:
  615. msg->conn->pcb.udp->recv_arg = NULL;
  616. udp_remove(msg->conn->pcb.udp);
  617. break;
  618. #endif /* LWIP_UDP */
  619. #if LWIP_TCP
  620. case NETCONN_TCP:
  621. msg->conn->state = NETCONN_CLOSE;
  622. do_close_internal(msg->conn);
  623. /* API_EVENT is called inside do_close_internal, before releasing
  624. the application thread, so we can return at this point! */
  625. return;
  626. #endif /* LWIP_TCP */
  627. default:
  628. break;
  629. }
  630. }
  631. /* tcp netconns don't come here! */
  632. /* Trigger select() in socket layer. This send should something else so the
  633. errorfd is set, not the read and write fd! */
  634. API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
  635. API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
  636. if (msg->conn->op_completed != SYS_SEM_NULL) {
  637. sys_sem_signal(msg->conn->op_completed);
  638. }
  639. }
  640. /**
  641. * Bind a pcb contained in a netconn
  642. * Called from netconn_bind.
  643. *
  644. * @param msg the api_msg_msg pointing to the connection and containing
  645. * the IP address and port to bind to
  646. */
  647. void
  648. do_bind(struct api_msg_msg *msg)
  649. {
  650. if (!ERR_IS_FATAL(msg->conn->err)) {
  651. if (msg->conn->pcb.tcp != NULL) {
  652. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  653. #if LWIP_RAW
  654. case NETCONN_RAW:
  655. msg->conn->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  656. break;
  657. #endif /* LWIP_RAW */
  658. #if LWIP_UDP
  659. case NETCONN_UDP:
  660. msg->conn->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  661. break;
  662. #endif /* LWIP_UDP */
  663. #if LWIP_TCP
  664. case NETCONN_TCP:
  665. msg->conn->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  666. break;
  667. #endif /* LWIP_TCP */
  668. default:
  669. break;
  670. }
  671. } else {
  672. /* msg->conn->pcb is NULL */
  673. msg->conn->err = ERR_VAL;
  674. }
  675. }
  676. TCPIP_APIMSG_ACK(msg);
  677. }
  678. #if LWIP_TCP
  679. /**
  680. * TCP callback function if a connection (opened by tcp_connect/do_connect) has
  681. * been established (or reset by the remote host).
  682. *
  683. * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
  684. */
  685. static err_t
  686. do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
  687. {
  688. struct netconn *conn;
  689. LWIP_UNUSED_ARG(pcb);
  690. conn = arg;
  691. if (conn == NULL) {
  692. return ERR_VAL;
  693. }
  694. conn->err = err;
  695. if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
  696. setup_tcp(conn);
  697. }
  698. conn->state = NETCONN_NONE;
  699. sys_sem_signal(conn->op_completed);
  700. return ERR_OK;
  701. }
  702. #endif /* LWIP_TCP */
  703. /**
  704. * Connect a pcb contained inside a netconn
  705. * Called from netconn_connect.
  706. *
  707. * @param msg the api_msg_msg pointing to the connection and containing
  708. * the IP address and port to connect to
  709. */
  710. void
  711. do_connect(struct api_msg_msg *msg)
  712. {
  713. if (msg->conn->pcb.tcp == NULL) {
  714. sys_sem_signal(msg->conn->op_completed);
  715. return;
  716. }
  717. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  718. #if LWIP_RAW
  719. case NETCONN_RAW:
  720. msg->conn->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  721. sys_sem_signal(msg->conn->op_completed);
  722. break;
  723. #endif /* LWIP_RAW */
  724. #if LWIP_UDP
  725. case NETCONN_UDP:
  726. msg->conn->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  727. sys_sem_signal(msg->conn->op_completed);
  728. break;
  729. #endif /* LWIP_UDP */
  730. #if LWIP_TCP
  731. case NETCONN_TCP:
  732. msg->conn->state = NETCONN_CONNECT;
  733. setup_tcp(msg->conn);
  734. msg->conn->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port,
  735. do_connected);
  736. /* sys_sem_signal() is called from do_connected (or err_tcp()),
  737. * when the connection is established! */
  738. break;
  739. #endif /* LWIP_TCP */
  740. default:
  741. LWIP_ERROR("Invalid netconn type", 0, do{ msg->conn->err = ERR_VAL;
  742. sys_sem_signal(msg->conn->op_completed); }while(0));
  743. break;
  744. }
  745. }
  746. /**
  747. * Connect a pcb contained inside a netconn
  748. * Only used for UDP netconns.
  749. * Called from netconn_disconnect.
  750. *
  751. * @param msg the api_msg_msg pointing to the connection to disconnect
  752. */
  753. void
  754. do_disconnect(struct api_msg_msg *msg)
  755. {
  756. #if LWIP_UDP
  757. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  758. udp_disconnect(msg->conn->pcb.udp);
  759. }
  760. #endif /* LWIP_UDP */
  761. TCPIP_APIMSG_ACK(msg);
  762. }
  763. /**
  764. * Set a TCP pcb contained in a netconn into listen mode
  765. * Called from netconn_listen.
  766. *
  767. * @param msg the api_msg_msg pointing to the connection
  768. */
  769. void
  770. do_listen(struct api_msg_msg *msg)
  771. {
  772. #if LWIP_TCP
  773. if (!ERR_IS_FATAL(msg->conn->err)) {
  774. if (msg->conn->pcb.tcp != NULL) {
  775. if (msg->conn->type == NETCONN_TCP) {
  776. if (msg->conn->pcb.tcp->state == CLOSED) {
  777. #if TCP_LISTEN_BACKLOG
  778. struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
  779. #else /* TCP_LISTEN_BACKLOG */
  780. struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
  781. #endif /* TCP_LISTEN_BACKLOG */
  782. if (lpcb == NULL) {
  783. msg->conn->err = ERR_MEM;
  784. } else {
  785. /* delete the recvmbox and allocate the acceptmbox */
  786. if (msg->conn->recvmbox != SYS_MBOX_NULL) {
  787. /** @todo: should we drain the recvmbox here? */
  788. sys_mbox_free(msg->conn->recvmbox);
  789. msg->conn->recvmbox = SYS_MBOX_NULL;
  790. }
  791. if (msg->conn->acceptmbox == SYS_MBOX_NULL) {
  792. if ((msg->conn->acceptmbox = sys_mbox_new(DEFAULT_ACCEPTMBOX_SIZE)) == SYS_MBOX_NULL) {
  793. msg->conn->err = ERR_MEM;
  794. }
  795. }
  796. if (msg->conn->err == ERR_OK) {
  797. msg->conn->state = NETCONN_LISTEN;
  798. msg->conn->pcb.tcp = lpcb;
  799. tcp_arg(msg->conn->pcb.tcp, msg->conn);
  800. tcp_accept(msg->conn->pcb.tcp, accept_function);
  801. }
  802. }
  803. } else {
  804. msg->conn->err = ERR_CONN;
  805. }
  806. }
  807. }
  808. }
  809. #endif /* LWIP_TCP */
  810. TCPIP_APIMSG_ACK(msg);
  811. }
  812. /**
  813. * Send some data on a RAW or UDP pcb contained in a netconn
  814. * Called from netconn_send
  815. *
  816. * @param msg the api_msg_msg pointing to the connection
  817. */
  818. void
  819. do_send(struct api_msg_msg *msg)
  820. {
  821. if (!ERR_IS_FATAL(msg->conn->err)) {
  822. if (msg->conn->pcb.tcp != NULL) {
  823. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  824. #if LWIP_RAW
  825. case NETCONN_RAW:
  826. if (msg->msg.b->addr == NULL) {
  827. msg->conn->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
  828. } else {
  829. msg->conn->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, msg->msg.b->addr);
  830. }
  831. break;
  832. #endif
  833. #if LWIP_UDP
  834. case NETCONN_UDP:
  835. if (msg->msg.b->addr == NULL) {
  836. msg->conn->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
  837. } else {
  838. msg->conn->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, msg->msg.b->addr, msg->msg.b->port);
  839. }
  840. break;
  841. #endif /* LWIP_UDP */
  842. default:
  843. break;
  844. }
  845. }
  846. }
  847. TCPIP_APIMSG_ACK(msg);
  848. }
  849. /**
  850. * Indicate data has been received from a TCP pcb contained in a netconn
  851. * Called from netconn_recv
  852. *
  853. * @param msg the api_msg_msg pointing to the connection
  854. */
  855. void
  856. do_recv(struct api_msg_msg *msg)
  857. {
  858. #if LWIP_TCP
  859. if (!ERR_IS_FATAL(msg->conn->err)) {
  860. if (msg->conn->pcb.tcp != NULL) {
  861. if (msg->conn->type == NETCONN_TCP) {
  862. #if TCP_LISTEN_BACKLOG
  863. if (msg->conn->pcb.tcp->state == LISTEN) {
  864. tcp_accepted(msg->conn->pcb.tcp);
  865. } else
  866. #endif /* TCP_LISTEN_BACKLOG */
  867. {
  868. tcp_recved(msg->conn->pcb.tcp, msg->msg.r.len);
  869. }
  870. }
  871. }
  872. }
  873. #endif /* LWIP_TCP */
  874. TCPIP_APIMSG_ACK(msg);
  875. }
  876. #if LWIP_TCP
  877. /**
  878. * See if more data needs to be written from a previous call to netconn_write.
  879. * Called initially from do_write. If the first call can't send all data
  880. * (because of low memory or empty send-buffer), this function is called again
  881. * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
  882. * blocking application thread (waiting in netconn_write) is released.
  883. *
  884. * @param conn netconn (that is currently in state NETCONN_WRITE) to process
  885. * @return ERR_OK
  886. * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
  887. */
  888. static err_t
  889. do_writemore(struct netconn *conn)
  890. {
  891. err_t err;
  892. void *dataptr;
  893. u16_t len, available;
  894. u8_t write_finished = 0;
  895. size_t diff;
  896. LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
  897. dataptr = (u8_t*)conn->write_msg->msg.w.dataptr + conn->write_offset;
  898. diff = conn->write_msg->msg.w.len - conn->write_offset;
  899. if (diff > 0xffffUL) { /* max_u16_t */
  900. len = 0xffff;
  901. #if LWIP_TCPIP_CORE_LOCKING
  902. conn->write_delayed = 1;
  903. #endif
  904. } else {
  905. len = (u16_t)diff;
  906. }
  907. available = tcp_sndbuf(conn->pcb.tcp);
  908. if (available < len) {
  909. /* don't try to write more than sendbuf */
  910. len = available;
  911. #if LWIP_TCPIP_CORE_LOCKING
  912. conn->write_delayed = 1;
  913. #endif
  914. }
  915. err = tcp_write(conn->pcb.tcp, dataptr, len, conn->write_msg->msg.w.apiflags);
  916. LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->write_msg->msg.w.len));
  917. if (err == ERR_OK) {
  918. conn->write_offset += len;
  919. if (conn->write_offset == conn->write_msg->msg.w.len) {
  920. /* everything was written */
  921. write_finished = 1;
  922. conn->write_msg = NULL;
  923. conn->write_offset = 0;
  924. /* API_EVENT might call tcp_tmr, so reset conn->state now */
  925. conn->state = NETCONN_NONE;
  926. }
  927. err = tcp_output_nagle(conn->pcb.tcp);
  928. conn->err = err;
  929. if ((err == ERR_OK) && (tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT)) {
  930. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  931. }
  932. } else if (err == ERR_MEM) {
  933. /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
  934. we do NOT return to the application thread, since ERR_MEM is
  935. only a temporary error! */
  936. /* tcp_enqueue returned ERR_MEM, try tcp_output anyway */
  937. err = tcp_output(conn->pcb.tcp);
  938. #if LWIP_TCPIP_CORE_LOCKING
  939. conn->write_delayed = 1;
  940. #endif
  941. } else {
  942. /* On errors != ERR_MEM, we don't try writing any more but return
  943. the error to the application thread. */
  944. conn->err = err;
  945. write_finished = 1;
  946. }
  947. if (write_finished) {
  948. /* everything was written: set back connection state
  949. and back to application task */
  950. conn->state = NETCONN_NONE;
  951. #if LWIP_TCPIP_CORE_LOCKING
  952. if (conn->write_delayed != 0)
  953. #endif
  954. {
  955. sys_sem_signal(conn->op_completed);
  956. }
  957. }
  958. #if LWIP_TCPIP_CORE_LOCKING
  959. else
  960. return ERR_MEM;
  961. #endif
  962. return ERR_OK;
  963. }
  964. #endif /* LWIP_TCP */
  965. /**
  966. * Send some data on a TCP pcb contained in a netconn
  967. * Called from netconn_write
  968. *
  969. * @param msg the api_msg_msg pointing to the connection
  970. */
  971. void
  972. do_write(struct api_msg_msg *msg)
  973. {
  974. if (!ERR_IS_FATAL(msg->conn->err)) {
  975. if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
  976. #if LWIP_TCP
  977. msg->conn->state = NETCONN_WRITE;
  978. /* set all the variables used by do_writemore */
  979. LWIP_ASSERT("already writing", msg->conn->write_msg == NULL &&
  980. msg->conn->write_offset == 0);
  981. msg->conn->write_msg = msg;
  982. msg->conn->write_offset = 0;
  983. #if LWIP_TCPIP_CORE_LOCKING
  984. msg->conn->write_delayed = 0;
  985. if (do_writemore(msg->conn) != ERR_OK) {
  986. LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
  987. UNLOCK_TCPIP_CORE();
  988. sys_arch_sem_wait(msg->conn->op_completed, 0);
  989. LOCK_TCPIP_CORE();
  990. LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
  991. }
  992. #else
  993. do_writemore(msg->conn);
  994. #endif
  995. /* for both cases: if do_writemore was called, don't ACK the APIMSG! */
  996. return;
  997. #endif /* LWIP_TCP */
  998. #if (LWIP_UDP || LWIP_RAW)
  999. } else {
  1000. msg->conn->err = ERR_VAL;
  1001. #endif /* (LWIP_UDP || LWIP_RAW) */
  1002. }
  1003. }
  1004. TCPIP_APIMSG_ACK(msg);
  1005. }
  1006. /**
  1007. * Return a connection's local or remote address
  1008. * Called from netconn_getaddr
  1009. *
  1010. * @param msg the api_msg_msg pointing to the connection
  1011. */
  1012. void
  1013. do_getaddr(struct api_msg_msg *msg)
  1014. {
  1015. if (msg->conn->pcb.ip != NULL) {
  1016. *(msg->msg.ad.ipaddr) = (msg->msg.ad.local?msg->conn->pcb.ip->local_ip:msg->conn->pcb.ip->remote_ip);
  1017. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1018. #if LWIP_RAW
  1019. case NETCONN_RAW:
  1020. if (msg->msg.ad.local) {
  1021. *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
  1022. } else {
  1023. /* return an error as connecting is only a helper for upper layers */
  1024. msg->conn->err = ERR_CONN;
  1025. }
  1026. break;
  1027. #endif /* LWIP_RAW */
  1028. #if LWIP_UDP
  1029. case NETCONN_UDP:
  1030. if (msg->msg.ad.local) {
  1031. *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
  1032. } else {
  1033. if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
  1034. msg->conn->err = ERR_CONN;
  1035. } else {
  1036. *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
  1037. }
  1038. }
  1039. break;
  1040. #endif /* LWIP_UDP */
  1041. #if LWIP_TCP
  1042. case NETCONN_TCP:
  1043. *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
  1044. break;
  1045. #endif /* LWIP_TCP */
  1046. }
  1047. } else {
  1048. msg->conn->err = ERR_CONN;
  1049. }
  1050. TCPIP_APIMSG_ACK(msg);
  1051. }
  1052. /**
  1053. * Close a TCP pcb contained in a netconn
  1054. * Called from netconn_close
  1055. *
  1056. * @param msg the api_msg_msg pointing to the connection
  1057. */
  1058. void
  1059. do_close(struct api_msg_msg *msg)
  1060. {
  1061. #if LWIP_TCP
  1062. if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
  1063. msg->conn->state = NETCONN_CLOSE;
  1064. do_close_internal(msg->conn);
  1065. /* for tcp netconns, do_close_internal ACKs the message */
  1066. } else
  1067. #endif /* LWIP_TCP */
  1068. {
  1069. msg->conn->err = ERR_VAL;
  1070. sys_sem_signal(msg->conn->op_completed);
  1071. }
  1072. }
  1073. #if LWIP_IGMP
  1074. /**
  1075. * Join multicast groups for UDP netconns.
  1076. * Called from netconn_join_leave_group
  1077. *
  1078. * @param msg the api_msg_msg pointing to the connection
  1079. */
  1080. void
  1081. do_join_leave_group(struct api_msg_msg *msg)
  1082. {
  1083. if (!ERR_IS_FATAL(msg->conn->err)) {
  1084. if (msg->conn->pcb.tcp != NULL) {
  1085. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  1086. #if LWIP_UDP
  1087. if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
  1088. msg->conn->err = igmp_joingroup(msg->msg.jl.interface, msg->msg.jl.multiaddr);
  1089. } else {
  1090. msg->conn->err = igmp_leavegroup(msg->msg.jl.interface, msg->msg.jl.multiaddr);
  1091. }
  1092. #endif /* LWIP_UDP */
  1093. #if (LWIP_TCP || LWIP_RAW)
  1094. } else {
  1095. msg->conn->err = ERR_VAL;
  1096. #endif /* (LWIP_TCP || LWIP_RAW) */
  1097. }
  1098. }
  1099. }
  1100. TCPIP_APIMSG_ACK(msg);
  1101. }
  1102. #endif /* LWIP_IGMP */
  1103. #if LWIP_DNS
  1104. /**
  1105. * Callback function that is called when DNS name is resolved
  1106. * (or on timeout). A waiting application thread is waked up by
  1107. * signaling the semaphore.
  1108. */
  1109. static void
  1110. do_dns_found(const char *name, struct ip_addr *ipaddr, void *arg)
  1111. {
  1112. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1113. LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
  1114. if (ipaddr == NULL) {
  1115. /* timeout or memory error */
  1116. *msg->err = ERR_VAL;
  1117. } else {
  1118. /* address was resolved */
  1119. *msg->err = ERR_OK;
  1120. *msg->addr = *ipaddr;
  1121. }
  1122. /* wake up the application task waiting in netconn_gethostbyname */
  1123. sys_sem_signal(msg->sem);
  1124. }
  1125. /**
  1126. * Execute a DNS query
  1127. * Called from netconn_gethostbyname
  1128. *
  1129. * @param arg the dns_api_msg pointing to the query
  1130. */
  1131. void
  1132. do_gethostbyname(void *arg)
  1133. {
  1134. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1135. *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
  1136. if (*msg->err != ERR_INPROGRESS) {
  1137. /* on error or immediate success, wake up the application
  1138. * task waiting in netconn_gethostbyname */
  1139. sys_sem_signal(msg->sem);
  1140. }
  1141. }
  1142. #endif /* LWIP_DNS */
  1143. #endif /* LWIP_NETCONN */