api_msg.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  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. /* check whether acceptmbox is valid */
  329. if (conn->acceptmbox == SYS_MBOX_NULL)
  330. {
  331. LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: acceptmbox already deleted\n"));
  332. return ERR_VAL;
  333. }
  334. /* We have to set the callback here even though
  335. * the new socket is unknown. conn->socket is marked as -1. */
  336. newconn = netconn_alloc(conn->type, conn->callback);
  337. if (newconn == NULL) {
  338. return ERR_MEM;
  339. }
  340. newconn->pcb.tcp = newpcb;
  341. setup_tcp(newconn);
  342. newconn->err = err;
  343. if (sys_mbox_trypost(conn->acceptmbox, newconn) != ERR_OK) {
  344. /* When returning != ERR_OK, the connection is aborted in tcp_process(),
  345. so do nothing here! */
  346. newconn->pcb.tcp = NULL;
  347. netconn_free(newconn);
  348. return ERR_MEM;
  349. } else {
  350. /* Register event with callback */
  351. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  352. }
  353. return ERR_OK;
  354. }
  355. #endif /* LWIP_TCP */
  356. /**
  357. * Create a new pcb of a specific type.
  358. * Called from do_newconn().
  359. *
  360. * @param msg the api_msg_msg describing the connection type
  361. * @return msg->conn->err, but the return value is currently ignored
  362. */
  363. static err_t
  364. pcb_new(struct api_msg_msg *msg)
  365. {
  366. msg->conn->err = ERR_OK;
  367. LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
  368. /* Allocate a PCB for this connection */
  369. switch(NETCONNTYPE_GROUP(msg->conn->type)) {
  370. #if LWIP_RAW
  371. case NETCONN_RAW:
  372. msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
  373. if(msg->conn->pcb.raw == NULL) {
  374. msg->conn->err = ERR_MEM;
  375. break;
  376. }
  377. raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
  378. break;
  379. #endif /* LWIP_RAW */
  380. #if LWIP_UDP
  381. case NETCONN_UDP:
  382. msg->conn->pcb.udp = udp_new();
  383. if(msg->conn->pcb.udp == NULL) {
  384. msg->conn->err = ERR_MEM;
  385. break;
  386. }
  387. #if LWIP_UDPLITE
  388. if (msg->conn->type==NETCONN_UDPLITE) {
  389. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
  390. }
  391. #endif /* LWIP_UDPLITE */
  392. if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
  393. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
  394. }
  395. udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
  396. break;
  397. #endif /* LWIP_UDP */
  398. #if LWIP_TCP
  399. case NETCONN_TCP:
  400. msg->conn->pcb.tcp = tcp_new();
  401. if(msg->conn->pcb.tcp == NULL) {
  402. msg->conn->err = ERR_MEM;
  403. break;
  404. }
  405. setup_tcp(msg->conn);
  406. break;
  407. #endif /* LWIP_TCP */
  408. default:
  409. /* Unsupported netconn type, e.g. protocol disabled */
  410. msg->conn->err = ERR_VAL;
  411. break;
  412. }
  413. return msg->conn->err;
  414. }
  415. /**
  416. * Create a new pcb of a specific type inside a netconn.
  417. * Called from netconn_new_with_proto_and_callback.
  418. *
  419. * @param msg the api_msg_msg describing the connection type
  420. */
  421. void
  422. do_newconn(struct api_msg_msg *msg)
  423. {
  424. if(msg->conn->pcb.tcp == NULL) {
  425. pcb_new(msg);
  426. }
  427. /* Else? This "new" connection already has a PCB allocated. */
  428. /* Is this an error condition? Should it be deleted? */
  429. /* We currently just are happy and return. */
  430. TCPIP_APIMSG_ACK(msg);
  431. }
  432. /**
  433. * Create a new netconn (of a specific type) that has a callback function.
  434. * The corresponding pcb is NOT created!
  435. *
  436. * @param t the type of 'connection' to create (@see enum netconn_type)
  437. * @param proto the IP protocol for RAW IP pcbs
  438. * @param callback a function to call on status changes (RX available, TX'ed)
  439. * @return a newly allocated struct netconn or
  440. * NULL on memory error
  441. */
  442. struct netconn*
  443. netconn_alloc(enum netconn_type t, netconn_callback callback)
  444. {
  445. struct netconn *conn;
  446. int size;
  447. conn = memp_malloc(MEMP_NETCONN);
  448. if (conn == NULL) {
  449. return NULL;
  450. }
  451. conn->err = ERR_OK;
  452. conn->type = t;
  453. conn->pcb.tcp = NULL;
  454. #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
  455. (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
  456. size = DEFAULT_RAW_RECVMBOX_SIZE;
  457. #else
  458. size = 0; /* skip warning */
  459. switch(NETCONNTYPE_GROUP(t)) {
  460. #if LWIP_RAW
  461. case NETCONN_RAW:
  462. size = DEFAULT_RAW_RECVMBOX_SIZE;
  463. break;
  464. #endif /* LWIP_RAW */
  465. #if LWIP_UDP
  466. case NETCONN_UDP:
  467. size = DEFAULT_UDP_RECVMBOX_SIZE;
  468. break;
  469. #endif /* LWIP_UDP */
  470. #if LWIP_TCP
  471. case NETCONN_TCP:
  472. size = DEFAULT_TCP_RECVMBOX_SIZE;
  473. break;
  474. #endif /* LWIP_TCP */
  475. default:
  476. LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
  477. break;
  478. }
  479. #endif
  480. if ((conn->op_completed = sys_sem_new(0)) == SYS_SEM_NULL) {
  481. memp_free(MEMP_NETCONN, conn);
  482. return NULL;
  483. }
  484. if ((conn->recvmbox = sys_mbox_new(size)) == SYS_MBOX_NULL) {
  485. sys_sem_free(conn->op_completed);
  486. memp_free(MEMP_NETCONN, conn);
  487. return NULL;
  488. }
  489. conn->acceptmbox = SYS_MBOX_NULL;
  490. conn->state = NETCONN_NONE;
  491. /* initialize socket to -1 since 0 is a valid socket */
  492. conn->socket = -1;
  493. conn->callback = callback;
  494. conn->recv_avail = 0;
  495. #if LWIP_TCP
  496. conn->write_msg = NULL;
  497. conn->write_offset = 0;
  498. #if LWIP_TCPIP_CORE_LOCKING
  499. conn->write_delayed = 0;
  500. #endif /* LWIP_TCPIP_CORE_LOCKING */
  501. #endif /* LWIP_TCP */
  502. #if LWIP_SO_RCVTIMEO
  503. conn->recv_timeout = 0;
  504. #endif /* LWIP_SO_RCVTIMEO */
  505. #if LWIP_SO_RCVBUF
  506. conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
  507. #endif /* LWIP_SO_RCVBUF */
  508. return conn;
  509. }
  510. /**
  511. * Delete a netconn and all its resources.
  512. * The pcb is NOT freed (since we might not be in the right thread context do this).
  513. *
  514. * @param conn the netconn to free
  515. */
  516. void
  517. netconn_free(struct netconn *conn)
  518. {
  519. void *mem;
  520. LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
  521. /* Drain the recvmbox. */
  522. if (conn->recvmbox != SYS_MBOX_NULL) {
  523. while (sys_mbox_tryfetch(conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
  524. if (conn->type == NETCONN_TCP) {
  525. if(mem != NULL) {
  526. pbuf_free((struct pbuf *)mem);
  527. }
  528. } else {
  529. netbuf_delete((struct netbuf *)mem);
  530. }
  531. }
  532. sys_mbox_free(conn->recvmbox);
  533. conn->recvmbox = SYS_MBOX_NULL;
  534. }
  535. /* Drain the acceptmbox. */
  536. if (conn->acceptmbox != SYS_MBOX_NULL) {
  537. while (sys_mbox_tryfetch(conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
  538. netconn_delete((struct netconn *)mem);
  539. }
  540. sys_mbox_free(conn->acceptmbox);
  541. conn->acceptmbox = SYS_MBOX_NULL;
  542. }
  543. sys_sem_free(conn->op_completed);
  544. conn->op_completed = SYS_SEM_NULL;
  545. memp_free(MEMP_NETCONN, conn);
  546. }
  547. #if LWIP_TCP
  548. /**
  549. * Internal helper function to close a TCP netconn: since this sometimes
  550. * doesn't work at the first attempt, this function is called from multiple
  551. * places.
  552. *
  553. * @param conn the TCP netconn to close
  554. */
  555. static void
  556. do_close_internal(struct netconn *conn)
  557. {
  558. err_t err;
  559. LWIP_ASSERT("invalid conn", (conn != NULL));
  560. LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
  561. LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
  562. LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
  563. /* Set back some callback pointers */
  564. tcp_arg(conn->pcb.tcp, NULL);
  565. if (conn->pcb.tcp->state == LISTEN) {
  566. tcp_accept(conn->pcb.tcp, NULL);
  567. } else {
  568. tcp_recv(conn->pcb.tcp, NULL);
  569. tcp_accept(conn->pcb.tcp, NULL);
  570. /* some callbacks have to be reset if tcp_close is not successful */
  571. tcp_sent(conn->pcb.tcp, NULL);
  572. tcp_poll(conn->pcb.tcp, NULL, 4);
  573. tcp_err(conn->pcb.tcp, NULL);
  574. }
  575. /* Try to close the connection */
  576. err = tcp_close(conn->pcb.tcp);
  577. if (err == ERR_OK) {
  578. /* Closing succeeded */
  579. conn->state = NETCONN_NONE;
  580. /* Set back some callback pointers as conn is going away */
  581. conn->pcb.tcp = NULL;
  582. conn->err = ERR_OK;
  583. /* Trigger select() in socket layer. This send should something else so the
  584. errorfd is set, not the read and write fd! */
  585. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  586. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  587. /* wake up the application task */
  588. sys_sem_signal(conn->op_completed);
  589. } else {
  590. /* Closing failed, restore some of the callbacks */
  591. /* Closing of listen pcb will never fail! */
  592. LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
  593. tcp_sent(conn->pcb.tcp, sent_tcp);
  594. tcp_poll(conn->pcb.tcp, poll_tcp, 4);
  595. tcp_err(conn->pcb.tcp, err_tcp);
  596. tcp_arg(conn->pcb.tcp, conn);
  597. }
  598. /* If closing didn't succeed, we get called again either
  599. from poll_tcp or from sent_tcp */
  600. }
  601. #endif /* LWIP_TCP */
  602. /**
  603. * Delete the pcb inside a netconn.
  604. * Called from netconn_delete.
  605. *
  606. * @param msg the api_msg_msg pointing to the connection
  607. */
  608. void
  609. do_delconn(struct api_msg_msg *msg)
  610. {
  611. if (msg->conn->pcb.tcp != NULL) {
  612. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  613. #if LWIP_RAW
  614. case NETCONN_RAW:
  615. raw_remove(msg->conn->pcb.raw);
  616. break;
  617. #endif /* LWIP_RAW */
  618. #if LWIP_UDP
  619. case NETCONN_UDP:
  620. msg->conn->pcb.udp->recv_arg = NULL;
  621. udp_remove(msg->conn->pcb.udp);
  622. break;
  623. #endif /* LWIP_UDP */
  624. #if LWIP_TCP
  625. case NETCONN_TCP:
  626. msg->conn->state = NETCONN_CLOSE;
  627. do_close_internal(msg->conn);
  628. /* API_EVENT is called inside do_close_internal, before releasing
  629. the application thread, so we can return at this point! */
  630. return;
  631. #endif /* LWIP_TCP */
  632. default:
  633. break;
  634. }
  635. }
  636. /* tcp netconns don't come here! */
  637. /* Trigger select() in socket layer. This send should something else so the
  638. errorfd is set, not the read and write fd! */
  639. API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
  640. API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
  641. if (msg->conn->op_completed != SYS_SEM_NULL) {
  642. sys_sem_signal(msg->conn->op_completed);
  643. }
  644. }
  645. /**
  646. * Bind a pcb contained in a netconn
  647. * Called from netconn_bind.
  648. *
  649. * @param msg the api_msg_msg pointing to the connection and containing
  650. * the IP address and port to bind to
  651. */
  652. void
  653. do_bind(struct api_msg_msg *msg)
  654. {
  655. if (!ERR_IS_FATAL(msg->conn->err)) {
  656. if (msg->conn->pcb.tcp != NULL) {
  657. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  658. #if LWIP_RAW
  659. case NETCONN_RAW:
  660. msg->conn->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  661. break;
  662. #endif /* LWIP_RAW */
  663. #if LWIP_UDP
  664. case NETCONN_UDP:
  665. msg->conn->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  666. break;
  667. #endif /* LWIP_UDP */
  668. #if LWIP_TCP
  669. case NETCONN_TCP:
  670. msg->conn->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  671. break;
  672. #endif /* LWIP_TCP */
  673. default:
  674. break;
  675. }
  676. } else {
  677. /* msg->conn->pcb is NULL */
  678. msg->conn->err = ERR_VAL;
  679. }
  680. }
  681. TCPIP_APIMSG_ACK(msg);
  682. }
  683. #if LWIP_TCP
  684. /**
  685. * TCP callback function if a connection (opened by tcp_connect/do_connect) has
  686. * been established (or reset by the remote host).
  687. *
  688. * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
  689. */
  690. static err_t
  691. do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
  692. {
  693. struct netconn *conn;
  694. LWIP_UNUSED_ARG(pcb);
  695. conn = arg;
  696. if (conn == NULL) {
  697. return ERR_VAL;
  698. }
  699. conn->err = err;
  700. if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
  701. setup_tcp(conn);
  702. }
  703. conn->state = NETCONN_NONE;
  704. sys_sem_signal(conn->op_completed);
  705. return ERR_OK;
  706. }
  707. #endif /* LWIP_TCP */
  708. /**
  709. * Connect a pcb contained inside a netconn
  710. * Called from netconn_connect.
  711. *
  712. * @param msg the api_msg_msg pointing to the connection and containing
  713. * the IP address and port to connect to
  714. */
  715. void
  716. do_connect(struct api_msg_msg *msg)
  717. {
  718. if (msg->conn->pcb.tcp == NULL) {
  719. sys_sem_signal(msg->conn->op_completed);
  720. return;
  721. }
  722. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  723. #if LWIP_RAW
  724. case NETCONN_RAW:
  725. msg->conn->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  726. sys_sem_signal(msg->conn->op_completed);
  727. break;
  728. #endif /* LWIP_RAW */
  729. #if LWIP_UDP
  730. case NETCONN_UDP:
  731. msg->conn->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  732. sys_sem_signal(msg->conn->op_completed);
  733. break;
  734. #endif /* LWIP_UDP */
  735. #if LWIP_TCP
  736. case NETCONN_TCP:
  737. msg->conn->state = NETCONN_CONNECT;
  738. setup_tcp(msg->conn);
  739. msg->conn->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port,
  740. do_connected);
  741. /* sys_sem_signal() is called from do_connected (or err_tcp()),
  742. * when the connection is established! */
  743. break;
  744. #endif /* LWIP_TCP */
  745. default:
  746. LWIP_ERROR("Invalid netconn type", 0, do{ msg->conn->err = ERR_VAL;
  747. sys_sem_signal(msg->conn->op_completed); }while(0));
  748. break;
  749. }
  750. }
  751. /**
  752. * Connect a pcb contained inside a netconn
  753. * Only used for UDP netconns.
  754. * Called from netconn_disconnect.
  755. *
  756. * @param msg the api_msg_msg pointing to the connection to disconnect
  757. */
  758. void
  759. do_disconnect(struct api_msg_msg *msg)
  760. {
  761. #if LWIP_UDP
  762. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  763. udp_disconnect(msg->conn->pcb.udp);
  764. }
  765. #endif /* LWIP_UDP */
  766. TCPIP_APIMSG_ACK(msg);
  767. }
  768. /**
  769. * Set a TCP pcb contained in a netconn into listen mode
  770. * Called from netconn_listen.
  771. *
  772. * @param msg the api_msg_msg pointing to the connection
  773. */
  774. void
  775. do_listen(struct api_msg_msg *msg)
  776. {
  777. #if LWIP_TCP
  778. if (!ERR_IS_FATAL(msg->conn->err)) {
  779. if (msg->conn->pcb.tcp != NULL) {
  780. if (msg->conn->type == NETCONN_TCP) {
  781. if (msg->conn->pcb.tcp->state == CLOSED) {
  782. #if TCP_LISTEN_BACKLOG
  783. struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
  784. #else /* TCP_LISTEN_BACKLOG */
  785. struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
  786. #endif /* TCP_LISTEN_BACKLOG */
  787. if (lpcb == NULL) {
  788. msg->conn->err = ERR_MEM;
  789. } else {
  790. /* delete the recvmbox and allocate the acceptmbox */
  791. if (msg->conn->recvmbox != SYS_MBOX_NULL) {
  792. /** @todo: should we drain the recvmbox here? */
  793. sys_mbox_free(msg->conn->recvmbox);
  794. msg->conn->recvmbox = SYS_MBOX_NULL;
  795. }
  796. if (msg->conn->acceptmbox == SYS_MBOX_NULL) {
  797. if ((msg->conn->acceptmbox = sys_mbox_new(DEFAULT_ACCEPTMBOX_SIZE)) == SYS_MBOX_NULL) {
  798. msg->conn->err = ERR_MEM;
  799. }
  800. }
  801. if (msg->conn->err == ERR_OK) {
  802. msg->conn->state = NETCONN_LISTEN;
  803. msg->conn->pcb.tcp = lpcb;
  804. tcp_arg(msg->conn->pcb.tcp, msg->conn);
  805. tcp_accept(msg->conn->pcb.tcp, accept_function);
  806. }
  807. }
  808. } else {
  809. msg->conn->err = ERR_CONN;
  810. }
  811. }
  812. }
  813. }
  814. #endif /* LWIP_TCP */
  815. TCPIP_APIMSG_ACK(msg);
  816. }
  817. /**
  818. * Send some data on a RAW or UDP pcb contained in a netconn
  819. * Called from netconn_send
  820. *
  821. * @param msg the api_msg_msg pointing to the connection
  822. */
  823. void
  824. do_send(struct api_msg_msg *msg)
  825. {
  826. if (!ERR_IS_FATAL(msg->conn->err)) {
  827. if (msg->conn->pcb.tcp != NULL) {
  828. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  829. #if LWIP_RAW
  830. case NETCONN_RAW:
  831. if (msg->msg.b->addr == NULL) {
  832. msg->conn->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
  833. } else {
  834. msg->conn->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, msg->msg.b->addr);
  835. }
  836. break;
  837. #endif
  838. #if LWIP_UDP
  839. case NETCONN_UDP:
  840. if (msg->msg.b->addr == NULL) {
  841. msg->conn->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
  842. } else {
  843. msg->conn->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, msg->msg.b->addr, msg->msg.b->port);
  844. }
  845. break;
  846. #endif /* LWIP_UDP */
  847. default:
  848. break;
  849. }
  850. }
  851. }
  852. TCPIP_APIMSG_ACK(msg);
  853. }
  854. /**
  855. * Indicate data has been received from a TCP pcb contained in a netconn
  856. * Called from netconn_recv
  857. *
  858. * @param msg the api_msg_msg pointing to the connection
  859. */
  860. void
  861. do_recv(struct api_msg_msg *msg)
  862. {
  863. #if LWIP_TCP
  864. if (!ERR_IS_FATAL(msg->conn->err)) {
  865. if (msg->conn->pcb.tcp != NULL) {
  866. if (msg->conn->type == NETCONN_TCP) {
  867. #if TCP_LISTEN_BACKLOG
  868. if (msg->conn->pcb.tcp->state == LISTEN) {
  869. tcp_accepted(msg->conn->pcb.tcp);
  870. } else
  871. #endif /* TCP_LISTEN_BACKLOG */
  872. {
  873. tcp_recved(msg->conn->pcb.tcp, msg->msg.r.len);
  874. }
  875. }
  876. }
  877. }
  878. #endif /* LWIP_TCP */
  879. TCPIP_APIMSG_ACK(msg);
  880. }
  881. #if LWIP_TCP
  882. /**
  883. * See if more data needs to be written from a previous call to netconn_write.
  884. * Called initially from do_write. If the first call can't send all data
  885. * (because of low memory or empty send-buffer), this function is called again
  886. * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
  887. * blocking application thread (waiting in netconn_write) is released.
  888. *
  889. * @param conn netconn (that is currently in state NETCONN_WRITE) to process
  890. * @return ERR_OK
  891. * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
  892. */
  893. static err_t
  894. do_writemore(struct netconn *conn)
  895. {
  896. err_t err;
  897. void *dataptr;
  898. u16_t len, available;
  899. u8_t write_finished = 0;
  900. size_t diff;
  901. LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
  902. dataptr = (u8_t*)conn->write_msg->msg.w.dataptr + conn->write_offset;
  903. diff = conn->write_msg->msg.w.len - conn->write_offset;
  904. if (diff > 0xffffUL) { /* max_u16_t */
  905. len = 0xffff;
  906. #if LWIP_TCPIP_CORE_LOCKING
  907. conn->write_delayed = 1;
  908. #endif
  909. } else {
  910. len = (u16_t)diff;
  911. }
  912. available = tcp_sndbuf(conn->pcb.tcp);
  913. if (available < len) {
  914. /* don't try to write more than sendbuf */
  915. len = available;
  916. #if LWIP_TCPIP_CORE_LOCKING
  917. conn->write_delayed = 1;
  918. #endif
  919. }
  920. err = tcp_write(conn->pcb.tcp, dataptr, len, conn->write_msg->msg.w.apiflags);
  921. LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->write_msg->msg.w.len));
  922. if (err == ERR_OK) {
  923. conn->write_offset += len;
  924. if (conn->write_offset == conn->write_msg->msg.w.len) {
  925. /* everything was written */
  926. write_finished = 1;
  927. conn->write_msg = NULL;
  928. conn->write_offset = 0;
  929. /* API_EVENT might call tcp_tmr, so reset conn->state now */
  930. conn->state = NETCONN_NONE;
  931. }
  932. err = tcp_output_nagle(conn->pcb.tcp);
  933. conn->err = err;
  934. if ((err == ERR_OK) && (tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT)) {
  935. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  936. }
  937. } else if (err == ERR_MEM) {
  938. /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
  939. we do NOT return to the application thread, since ERR_MEM is
  940. only a temporary error! */
  941. /* tcp_enqueue returned ERR_MEM, try tcp_output anyway */
  942. err = tcp_output(conn->pcb.tcp);
  943. #if LWIP_TCPIP_CORE_LOCKING
  944. conn->write_delayed = 1;
  945. #endif
  946. } else {
  947. /* On errors != ERR_MEM, we don't try writing any more but return
  948. the error to the application thread. */
  949. conn->err = err;
  950. write_finished = 1;
  951. }
  952. if (write_finished) {
  953. /* everything was written: set back connection state
  954. and back to application task */
  955. conn->state = NETCONN_NONE;
  956. #if LWIP_TCPIP_CORE_LOCKING
  957. if (conn->write_delayed != 0)
  958. #endif
  959. {
  960. sys_sem_signal(conn->op_completed);
  961. }
  962. }
  963. #if LWIP_TCPIP_CORE_LOCKING
  964. else
  965. return ERR_MEM;
  966. #endif
  967. return ERR_OK;
  968. }
  969. #endif /* LWIP_TCP */
  970. /**
  971. * Send some data on a TCP pcb contained in a netconn
  972. * Called from netconn_write
  973. *
  974. * @param msg the api_msg_msg pointing to the connection
  975. */
  976. void
  977. do_write(struct api_msg_msg *msg)
  978. {
  979. if (!ERR_IS_FATAL(msg->conn->err)) {
  980. if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
  981. #if LWIP_TCP
  982. msg->conn->state = NETCONN_WRITE;
  983. /* set all the variables used by do_writemore */
  984. LWIP_ASSERT("already writing", msg->conn->write_msg == NULL &&
  985. msg->conn->write_offset == 0);
  986. msg->conn->write_msg = msg;
  987. msg->conn->write_offset = 0;
  988. #if LWIP_TCPIP_CORE_LOCKING
  989. msg->conn->write_delayed = 0;
  990. if (do_writemore(msg->conn) != ERR_OK) {
  991. LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
  992. UNLOCK_TCPIP_CORE();
  993. sys_arch_sem_wait(msg->conn->op_completed, 0);
  994. LOCK_TCPIP_CORE();
  995. LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
  996. }
  997. #else
  998. do_writemore(msg->conn);
  999. #endif
  1000. /* for both cases: if do_writemore was called, don't ACK the APIMSG! */
  1001. return;
  1002. #endif /* LWIP_TCP */
  1003. #if (LWIP_UDP || LWIP_RAW)
  1004. } else {
  1005. msg->conn->err = ERR_VAL;
  1006. #endif /* (LWIP_UDP || LWIP_RAW) */
  1007. }
  1008. }
  1009. TCPIP_APIMSG_ACK(msg);
  1010. }
  1011. /**
  1012. * Return a connection's local or remote address
  1013. * Called from netconn_getaddr
  1014. *
  1015. * @param msg the api_msg_msg pointing to the connection
  1016. */
  1017. void
  1018. do_getaddr(struct api_msg_msg *msg)
  1019. {
  1020. if (msg->conn->pcb.ip != NULL) {
  1021. *(msg->msg.ad.ipaddr) = (msg->msg.ad.local?msg->conn->pcb.ip->local_ip:msg->conn->pcb.ip->remote_ip);
  1022. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1023. #if LWIP_RAW
  1024. case NETCONN_RAW:
  1025. if (msg->msg.ad.local) {
  1026. *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
  1027. } else {
  1028. /* return an error as connecting is only a helper for upper layers */
  1029. msg->conn->err = ERR_CONN;
  1030. }
  1031. break;
  1032. #endif /* LWIP_RAW */
  1033. #if LWIP_UDP
  1034. case NETCONN_UDP:
  1035. if (msg->msg.ad.local) {
  1036. *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
  1037. } else {
  1038. if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
  1039. msg->conn->err = ERR_CONN;
  1040. } else {
  1041. *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
  1042. }
  1043. }
  1044. break;
  1045. #endif /* LWIP_UDP */
  1046. #if LWIP_TCP
  1047. case NETCONN_TCP:
  1048. *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
  1049. break;
  1050. #endif /* LWIP_TCP */
  1051. }
  1052. } else {
  1053. msg->conn->err = ERR_CONN;
  1054. }
  1055. TCPIP_APIMSG_ACK(msg);
  1056. }
  1057. /**
  1058. * Close a TCP pcb contained in a netconn
  1059. * Called from netconn_close
  1060. *
  1061. * @param msg the api_msg_msg pointing to the connection
  1062. */
  1063. void
  1064. do_close(struct api_msg_msg *msg)
  1065. {
  1066. #if LWIP_TCP
  1067. if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
  1068. msg->conn->state = NETCONN_CLOSE;
  1069. do_close_internal(msg->conn);
  1070. /* for tcp netconns, do_close_internal ACKs the message */
  1071. } else
  1072. #endif /* LWIP_TCP */
  1073. {
  1074. msg->conn->err = ERR_VAL;
  1075. sys_sem_signal(msg->conn->op_completed);
  1076. }
  1077. }
  1078. #if LWIP_IGMP
  1079. /**
  1080. * Join multicast groups for UDP netconns.
  1081. * Called from netconn_join_leave_group
  1082. *
  1083. * @param msg the api_msg_msg pointing to the connection
  1084. */
  1085. void
  1086. do_join_leave_group(struct api_msg_msg *msg)
  1087. {
  1088. if (!ERR_IS_FATAL(msg->conn->err)) {
  1089. if (msg->conn->pcb.tcp != NULL) {
  1090. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  1091. #if LWIP_UDP
  1092. if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
  1093. msg->conn->err = igmp_joingroup(msg->msg.jl.interface, msg->msg.jl.multiaddr);
  1094. } else {
  1095. msg->conn->err = igmp_leavegroup(msg->msg.jl.interface, msg->msg.jl.multiaddr);
  1096. }
  1097. #endif /* LWIP_UDP */
  1098. #if (LWIP_TCP || LWIP_RAW)
  1099. } else {
  1100. msg->conn->err = ERR_VAL;
  1101. #endif /* (LWIP_TCP || LWIP_RAW) */
  1102. }
  1103. }
  1104. }
  1105. TCPIP_APIMSG_ACK(msg);
  1106. }
  1107. #endif /* LWIP_IGMP */
  1108. #if LWIP_DNS
  1109. /**
  1110. * Callback function that is called when DNS name is resolved
  1111. * (or on timeout). A waiting application thread is waked up by
  1112. * signaling the semaphore.
  1113. */
  1114. static void
  1115. do_dns_found(const char *name, struct ip_addr *ipaddr, void *arg)
  1116. {
  1117. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1118. LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
  1119. if (ipaddr == NULL) {
  1120. /* timeout or memory error */
  1121. *msg->err = ERR_VAL;
  1122. } else {
  1123. /* address was resolved */
  1124. *msg->err = ERR_OK;
  1125. *msg->addr = *ipaddr;
  1126. }
  1127. /* wake up the application task waiting in netconn_gethostbyname */
  1128. sys_sem_signal(msg->sem);
  1129. }
  1130. /**
  1131. * Execute a DNS query
  1132. * Called from netconn_gethostbyname
  1133. *
  1134. * @param arg the dns_api_msg pointing to the query
  1135. */
  1136. void
  1137. do_gethostbyname(void *arg)
  1138. {
  1139. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1140. *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
  1141. if (*msg->err != ERR_INPROGRESS) {
  1142. /* on error or immediate success, wake up the application
  1143. * task waiting in netconn_gethostbyname */
  1144. sys_sem_signal(msg->sem);
  1145. }
  1146. }
  1147. #endif /* LWIP_DNS */
  1148. #endif /* LWIP_NETCONN */