api_msg.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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. #define SET_NONBLOCKING_CONNECT(conn, val) do { if(val) { \
  50. (conn)->flags |= NETCONN_FLAG_IN_NONBLOCKING_CONNECT; \
  51. } else { \
  52. (conn)->flags &= ~ NETCONN_FLAG_IN_NONBLOCKING_CONNECT; }} while(0)
  53. #define IN_NONBLOCKING_CONNECT(conn) (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0)
  54. /* forward declarations */
  55. #if LWIP_TCP
  56. static err_t do_writemore(struct netconn *conn);
  57. static void do_close_internal(struct netconn *conn);
  58. #endif
  59. #if LWIP_RAW
  60. /**
  61. * Receive callback function for RAW netconns.
  62. * Doesn't 'eat' the packet, only references it and sends it to
  63. * conn->recvmbox
  64. *
  65. * @see raw.h (struct raw_pcb.recv) for parameters and return value
  66. */
  67. static u8_t
  68. recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
  69. ip_addr_t *addr)
  70. {
  71. struct pbuf *q;
  72. struct netbuf *buf;
  73. struct netconn *conn;
  74. LWIP_UNUSED_ARG(addr);
  75. conn = (struct netconn *)arg;
  76. if ((conn != NULL) && sys_mbox_valid(&conn->recvmbox)) {
  77. #if LWIP_SO_RCVBUF
  78. int recv_avail;
  79. SYS_ARCH_GET(conn->recv_avail, recv_avail);
  80. if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) {
  81. return 0;
  82. }
  83. #endif /* LWIP_SO_RCVBUF */
  84. /* copy the whole packet into new pbufs */
  85. q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
  86. if(q != NULL) {
  87. if (pbuf_copy(q, p) != ERR_OK) {
  88. pbuf_free(q);
  89. q = NULL;
  90. }
  91. }
  92. if (q != NULL) {
  93. u16_t len;
  94. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  95. if (buf == NULL) {
  96. pbuf_free(q);
  97. return 0;
  98. }
  99. buf->p = q;
  100. buf->ptr = q;
  101. ip_addr_copy(buf->addr, *ip_current_src_addr());
  102. buf->port = pcb->protocol;
  103. len = q->tot_len;
  104. if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
  105. netbuf_delete(buf);
  106. return 0;
  107. } else {
  108. #if LWIP_SO_RCVBUF
  109. SYS_ARCH_INC(conn->recv_avail, len);
  110. #endif /* LWIP_SO_RCVBUF */
  111. /* Register event with callback */
  112. API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
  113. }
  114. }
  115. }
  116. return 0; /* do not eat the packet */
  117. }
  118. #endif /* LWIP_RAW*/
  119. #if LWIP_UDP
  120. /**
  121. * Receive callback function for UDP netconns.
  122. * Posts the packet to conn->recvmbox or deletes it on memory error.
  123. *
  124. * @see udp.h (struct udp_pcb.recv) for parameters
  125. */
  126. static void
  127. recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  128. ip_addr_t *addr, u16_t port)
  129. {
  130. struct netbuf *buf;
  131. struct netconn *conn;
  132. u16_t len;
  133. #if LWIP_SO_RCVBUF
  134. int recv_avail;
  135. #endif /* LWIP_SO_RCVBUF */
  136. LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
  137. LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
  138. LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
  139. conn = (struct netconn *)arg;
  140. LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
  141. #if LWIP_SO_RCVBUF
  142. SYS_ARCH_GET(conn->recv_avail, recv_avail);
  143. if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox) ||
  144. ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
  145. #else /* LWIP_SO_RCVBUF */
  146. if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox)) {
  147. #endif /* LWIP_SO_RCVBUF */
  148. pbuf_free(p);
  149. return;
  150. }
  151. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  152. if (buf == NULL) {
  153. pbuf_free(p);
  154. return;
  155. } else {
  156. buf->p = p;
  157. buf->ptr = p;
  158. ip_addr_set(&buf->addr, addr);
  159. buf->port = port;
  160. #if LWIP_NETBUF_RECVINFO
  161. {
  162. const struct ip_hdr* iphdr = ip_current_header();
  163. /* get the UDP header - always in the first pbuf, ensured by udp_input */
  164. const struct udp_hdr* udphdr = (void*)(((char*)iphdr) + IPH_LEN(iphdr));
  165. #if LWIP_CHECKSUM_ON_COPY
  166. buf->flags = NETBUF_FLAG_DESTADDR;
  167. #endif /* LWIP_CHECKSUM_ON_COPY */
  168. ip_addr_set(&buf->toaddr, ip_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. LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
  203. if (conn == NULL) {
  204. return ERR_VAL;
  205. }
  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. do_writemore(conn);
  255. } else if (conn->state == NETCONN_CLOSE) {
  256. 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. do_writemore(conn);
  286. } else if (conn->state == NETCONN_CLOSE) {
  287. 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 do_writemore/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 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. msg->err = ERR_MEM;
  444. break;
  445. }
  446. raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
  447. break;
  448. #endif /* LWIP_RAW */
  449. #if LWIP_UDP
  450. case NETCONN_UDP:
  451. msg->conn->pcb.udp = udp_new();
  452. if(msg->conn->pcb.udp == NULL) {
  453. msg->err = ERR_MEM;
  454. break;
  455. }
  456. #if LWIP_UDPLITE
  457. if (msg->conn->type==NETCONN_UDPLITE) {
  458. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
  459. }
  460. #endif /* LWIP_UDPLITE */
  461. if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
  462. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
  463. }
  464. udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
  465. break;
  466. #endif /* LWIP_UDP */
  467. #if LWIP_TCP
  468. case NETCONN_TCP:
  469. msg->conn->pcb.tcp = tcp_new();
  470. if(msg->conn->pcb.tcp == NULL) {
  471. msg->err = ERR_MEM;
  472. break;
  473. }
  474. setup_tcp(msg->conn);
  475. break;
  476. #endif /* LWIP_TCP */
  477. default:
  478. /* Unsupported netconn type, e.g. protocol disabled */
  479. msg->err = ERR_VAL;
  480. break;
  481. }
  482. }
  483. /**
  484. * Create a new pcb of a specific type inside a netconn.
  485. * Called from netconn_new_with_proto_and_callback.
  486. *
  487. * @param msg the api_msg_msg describing the connection type
  488. */
  489. void
  490. do_newconn(struct api_msg_msg *msg)
  491. {
  492. msg->err = ERR_OK;
  493. if(msg->conn->pcb.tcp == NULL) {
  494. pcb_new(msg);
  495. }
  496. /* Else? This "new" connection already has a PCB allocated. */
  497. /* Is this an error condition? Should it be deleted? */
  498. /* We currently just are happy and return. */
  499. TCPIP_APIMSG_ACK(msg);
  500. }
  501. /**
  502. * Create a new netconn (of a specific type) that has a callback function.
  503. * The corresponding pcb is NOT created!
  504. *
  505. * @param t the type of 'connection' to create (@see enum netconn_type)
  506. * @param proto the IP protocol for RAW IP pcbs
  507. * @param callback a function to call on status changes (RX available, TX'ed)
  508. * @return a newly allocated struct netconn or
  509. * NULL on memory error
  510. */
  511. struct netconn*
  512. netconn_alloc(enum netconn_type t, netconn_callback callback)
  513. {
  514. struct netconn *conn;
  515. int size;
  516. conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
  517. if (conn == NULL) {
  518. return NULL;
  519. }
  520. conn->last_err = ERR_OK;
  521. conn->type = t;
  522. conn->pcb.tcp = NULL;
  523. #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
  524. (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
  525. size = DEFAULT_RAW_RECVMBOX_SIZE;
  526. #else
  527. switch(NETCONNTYPE_GROUP(t)) {
  528. #if LWIP_RAW
  529. case NETCONN_RAW:
  530. size = DEFAULT_RAW_RECVMBOX_SIZE;
  531. break;
  532. #endif /* LWIP_RAW */
  533. #if LWIP_UDP
  534. case NETCONN_UDP:
  535. size = DEFAULT_UDP_RECVMBOX_SIZE;
  536. break;
  537. #endif /* LWIP_UDP */
  538. #if LWIP_TCP
  539. case NETCONN_TCP:
  540. size = DEFAULT_TCP_RECVMBOX_SIZE;
  541. break;
  542. #endif /* LWIP_TCP */
  543. default:
  544. LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
  545. goto free_and_return;
  546. }
  547. #endif
  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 (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. 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", (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. 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("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
  763. msg->err = ERR_INPROGRESS;
  764. } else {
  765. LWIP_ASSERT("blocking connect in progress",
  766. (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
  767. /* Drain and delete mboxes */
  768. netconn_drain(msg->conn);
  769. if (msg->conn->pcb.tcp != NULL) {
  770. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  771. #if LWIP_RAW
  772. case NETCONN_RAW:
  773. raw_remove(msg->conn->pcb.raw);
  774. break;
  775. #endif /* LWIP_RAW */
  776. #if LWIP_UDP
  777. case NETCONN_UDP:
  778. msg->conn->pcb.udp->recv_arg = NULL;
  779. udp_remove(msg->conn->pcb.udp);
  780. break;
  781. #endif /* LWIP_UDP */
  782. #if LWIP_TCP
  783. case NETCONN_TCP:
  784. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  785. msg->conn->write_offset == 0);
  786. msg->conn->state = NETCONN_CLOSE;
  787. msg->msg.sd.shut = NETCONN_SHUT_RDWR;
  788. msg->conn->current_msg = msg;
  789. do_close_internal(msg->conn);
  790. /* API_EVENT is called inside do_close_internal, before releasing
  791. the application thread, so we can return at this point! */
  792. return;
  793. #endif /* LWIP_TCP */
  794. default:
  795. break;
  796. }
  797. msg->conn->pcb.tcp = NULL;
  798. }
  799. /* tcp netconns don't come here! */
  800. /* @todo: this lets select make the socket readable and writable,
  801. which is wrong! errfd instead? */
  802. API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
  803. API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
  804. }
  805. if (sys_sem_valid(&msg->conn->op_completed)) {
  806. sys_sem_signal(&msg->conn->op_completed);
  807. }
  808. }
  809. /**
  810. * Bind a pcb contained in a netconn
  811. * Called from netconn_bind.
  812. *
  813. * @param msg the api_msg_msg pointing to the connection and containing
  814. * the IP address and port to bind to
  815. */
  816. void
  817. do_bind(struct api_msg_msg *msg)
  818. {
  819. if (ERR_IS_FATAL(msg->conn->last_err)) {
  820. msg->err = msg->conn->last_err;
  821. } else {
  822. msg->err = ERR_VAL;
  823. if (msg->conn->pcb.tcp != NULL) {
  824. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  825. #if LWIP_RAW
  826. case NETCONN_RAW:
  827. msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  828. break;
  829. #endif /* LWIP_RAW */
  830. #if LWIP_UDP
  831. case NETCONN_UDP:
  832. msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  833. break;
  834. #endif /* LWIP_UDP */
  835. #if LWIP_TCP
  836. case NETCONN_TCP:
  837. msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  838. break;
  839. #endif /* LWIP_TCP */
  840. default:
  841. break;
  842. }
  843. }
  844. }
  845. TCPIP_APIMSG_ACK(msg);
  846. }
  847. #if LWIP_TCP
  848. /**
  849. * TCP callback function if a connection (opened by tcp_connect/do_connect) has
  850. * been established (or reset by the remote host).
  851. *
  852. * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
  853. */
  854. static err_t
  855. do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
  856. {
  857. struct netconn *conn;
  858. int was_blocking;
  859. LWIP_UNUSED_ARG(pcb);
  860. conn = (struct netconn *)arg;
  861. if (conn == NULL) {
  862. return ERR_VAL;
  863. }
  864. LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
  865. LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
  866. (conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
  867. if (conn->current_msg != NULL) {
  868. conn->current_msg->err = err;
  869. }
  870. if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
  871. setup_tcp(conn);
  872. }
  873. was_blocking = !IN_NONBLOCKING_CONNECT(conn);
  874. SET_NONBLOCKING_CONNECT(conn, 0);
  875. conn->current_msg = NULL;
  876. conn->state = NETCONN_NONE;
  877. if (!was_blocking) {
  878. NETCONN_SET_SAFE_ERR(conn, ERR_OK);
  879. }
  880. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  881. if (was_blocking) {
  882. sys_sem_signal(&conn->op_completed);
  883. }
  884. return ERR_OK;
  885. }
  886. #endif /* LWIP_TCP */
  887. /**
  888. * Connect a pcb contained inside a netconn
  889. * Called from netconn_connect.
  890. *
  891. * @param msg the api_msg_msg pointing to the connection and containing
  892. * the IP address and port to connect to
  893. */
  894. void
  895. do_connect(struct api_msg_msg *msg)
  896. {
  897. if (msg->conn->pcb.tcp == NULL) {
  898. /* This may happen when calling netconn_connect() a second time */
  899. msg->err = ERR_CLSD;
  900. } else {
  901. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  902. #if LWIP_RAW
  903. case NETCONN_RAW:
  904. msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  905. break;
  906. #endif /* LWIP_RAW */
  907. #if LWIP_UDP
  908. case NETCONN_UDP:
  909. msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  910. break;
  911. #endif /* LWIP_UDP */
  912. #if LWIP_TCP
  913. case NETCONN_TCP:
  914. /* Prevent connect while doing any other action. */
  915. if (msg->conn->state != NETCONN_NONE) {
  916. msg->err = ERR_ISCONN;
  917. } else {
  918. setup_tcp(msg->conn);
  919. msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr,
  920. msg->msg.bc.port, do_connected);
  921. if (msg->err == ERR_OK) {
  922. u8_t non_blocking = netconn_is_nonblocking(msg->conn);
  923. msg->conn->state = NETCONN_CONNECT;
  924. SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
  925. if (non_blocking) {
  926. msg->err = ERR_INPROGRESS;
  927. } else {
  928. msg->conn->current_msg = msg;
  929. /* sys_sem_signal() is called from do_connected (or err_tcp()),
  930. * when the connection is established! */
  931. return;
  932. }
  933. }
  934. }
  935. break;
  936. #endif /* LWIP_TCP */
  937. default:
  938. LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0));
  939. break;
  940. }
  941. }
  942. sys_sem_signal(&msg->conn->op_completed);
  943. }
  944. /**
  945. * Connect a pcb contained inside a netconn
  946. * Only used for UDP netconns.
  947. * Called from netconn_disconnect.
  948. *
  949. * @param msg the api_msg_msg pointing to the connection to disconnect
  950. */
  951. void
  952. do_disconnect(struct api_msg_msg *msg)
  953. {
  954. #if LWIP_UDP
  955. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  956. udp_disconnect(msg->conn->pcb.udp);
  957. msg->err = ERR_OK;
  958. } else
  959. #endif /* LWIP_UDP */
  960. {
  961. msg->err = ERR_VAL;
  962. }
  963. TCPIP_APIMSG_ACK(msg);
  964. }
  965. #if LWIP_TCP
  966. /**
  967. * Set a TCP pcb contained in a netconn into listen mode
  968. * Called from netconn_listen.
  969. *
  970. * @param msg the api_msg_msg pointing to the connection
  971. */
  972. void
  973. do_listen(struct api_msg_msg *msg)
  974. {
  975. if (ERR_IS_FATAL(msg->conn->last_err)) {
  976. msg->err = msg->conn->last_err;
  977. } else {
  978. msg->err = ERR_CONN;
  979. if (msg->conn->pcb.tcp != NULL) {
  980. if (msg->conn->type == NETCONN_TCP) {
  981. if (msg->conn->state == NETCONN_NONE) {
  982. #if TCP_LISTEN_BACKLOG
  983. struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
  984. #else /* TCP_LISTEN_BACKLOG */
  985. struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
  986. #endif /* TCP_LISTEN_BACKLOG */
  987. if (lpcb == NULL) {
  988. /* in this case, the old pcb is still allocated */
  989. msg->err = ERR_MEM;
  990. } else {
  991. /* delete the recvmbox and allocate the acceptmbox */
  992. if (sys_mbox_valid(&msg->conn->recvmbox)) {
  993. /** @todo: should we drain the recvmbox here? */
  994. sys_mbox_free(&msg->conn->recvmbox);
  995. sys_mbox_set_invalid(&msg->conn->recvmbox);
  996. }
  997. msg->err = ERR_OK;
  998. if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
  999. msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
  1000. }
  1001. if (msg->err == ERR_OK) {
  1002. msg->conn->state = NETCONN_LISTEN;
  1003. msg->conn->pcb.tcp = lpcb;
  1004. tcp_arg(msg->conn->pcb.tcp, msg->conn);
  1005. tcp_accept(msg->conn->pcb.tcp, accept_function);
  1006. } else {
  1007. /* since the old pcb is already deallocated, free lpcb now */
  1008. tcp_close(lpcb);
  1009. msg->conn->pcb.tcp = NULL;
  1010. }
  1011. }
  1012. }
  1013. } else {
  1014. msg->err = ERR_ARG;
  1015. }
  1016. }
  1017. }
  1018. TCPIP_APIMSG_ACK(msg);
  1019. }
  1020. #endif /* LWIP_TCP */
  1021. /**
  1022. * Send some data on a RAW or UDP pcb contained in a netconn
  1023. * Called from netconn_send
  1024. *
  1025. * @param msg the api_msg_msg pointing to the connection
  1026. */
  1027. void
  1028. do_send(struct api_msg_msg *msg)
  1029. {
  1030. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1031. msg->err = msg->conn->last_err;
  1032. } else {
  1033. msg->err = ERR_CONN;
  1034. if (msg->conn->pcb.tcp != NULL) {
  1035. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1036. #if LWIP_RAW
  1037. case NETCONN_RAW:
  1038. if (ip_addr_isany(&msg->msg.b->addr)) {
  1039. msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
  1040. } else {
  1041. msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, &msg->msg.b->addr);
  1042. }
  1043. break;
  1044. #endif
  1045. #if LWIP_UDP
  1046. case NETCONN_UDP:
  1047. #if LWIP_CHECKSUM_ON_COPY
  1048. if (ip_addr_isany(&msg->msg.b->addr)) {
  1049. msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
  1050. msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
  1051. } else {
  1052. msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
  1053. &msg->msg.b->addr, msg->msg.b->port,
  1054. msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
  1055. }
  1056. #else /* LWIP_CHECKSUM_ON_COPY */
  1057. if (ip_addr_isany(&msg->msg.b->addr)) {
  1058. msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
  1059. } else {
  1060. msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, &msg->msg.b->addr, msg->msg.b->port);
  1061. }
  1062. #endif /* LWIP_CHECKSUM_ON_COPY */
  1063. break;
  1064. #endif /* LWIP_UDP */
  1065. default:
  1066. break;
  1067. }
  1068. }
  1069. }
  1070. TCPIP_APIMSG_ACK(msg);
  1071. }
  1072. #if LWIP_TCP
  1073. /**
  1074. * Indicate data has been received from a TCP pcb contained in a netconn
  1075. * Called from netconn_recv
  1076. *
  1077. * @param msg the api_msg_msg pointing to the connection
  1078. */
  1079. void
  1080. do_recv(struct api_msg_msg *msg)
  1081. {
  1082. msg->err = ERR_OK;
  1083. if (msg->conn->pcb.tcp != NULL) {
  1084. if (msg->conn->type == NETCONN_TCP) {
  1085. #if TCP_LISTEN_BACKLOG
  1086. if (msg->conn->pcb.tcp->state == LISTEN) {
  1087. tcp_accepted(msg->conn->pcb.tcp);
  1088. } else
  1089. #endif /* TCP_LISTEN_BACKLOG */
  1090. {
  1091. u32_t remaining = msg->msg.r.len;
  1092. do {
  1093. u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining;
  1094. tcp_recved(msg->conn->pcb.tcp, recved);
  1095. remaining -= recved;
  1096. }while(remaining != 0);
  1097. }
  1098. }
  1099. }
  1100. TCPIP_APIMSG_ACK(msg);
  1101. }
  1102. /**
  1103. * See if more data needs to be written from a previous call to netconn_write.
  1104. * Called initially from do_write. If the first call can't send all data
  1105. * (because of low memory or empty send-buffer), this function is called again
  1106. * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
  1107. * blocking application thread (waiting in netconn_write) is released.
  1108. *
  1109. * @param conn netconn (that is currently in state NETCONN_WRITE) to process
  1110. * @return ERR_OK
  1111. * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
  1112. */
  1113. static err_t
  1114. do_writemore(struct netconn *conn)
  1115. {
  1116. err_t err;
  1117. void *dataptr;
  1118. u16_t len, available;
  1119. u8_t write_finished = 0;
  1120. size_t diff;
  1121. u8_t dontblock = netconn_is_nonblocking(conn) ||
  1122. (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
  1123. u8_t apiflags = conn->current_msg->msg.w.apiflags;
  1124. LWIP_ASSERT("conn != NULL", conn != NULL);
  1125. LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
  1126. LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
  1127. LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
  1128. LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
  1129. conn->write_offset < conn->current_msg->msg.w.len);
  1130. #if LWIP_SO_SNDTIMEO
  1131. if ((conn->send_timeout != 0) &&
  1132. ((s32_t)(sys_now() - conn->current_msg->msg.w.time_started) >= conn->send_timeout)) {
  1133. write_finished = 1;
  1134. if (conn->write_offset == 0) {
  1135. /* nothing has been written */
  1136. err = ERR_WOULDBLOCK;
  1137. conn->current_msg->msg.w.len = 0;
  1138. } else {
  1139. /* partial write */
  1140. err = ERR_OK;
  1141. conn->current_msg->msg.w.len = conn->write_offset;
  1142. conn->write_offset = 0;
  1143. }
  1144. } else
  1145. #endif /* LWIP_SO_SNDTIMEO */
  1146. {
  1147. dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
  1148. diff = conn->current_msg->msg.w.len - conn->write_offset;
  1149. if (diff > 0xffffUL) { /* max_u16_t */
  1150. len = 0xffff;
  1151. #if LWIP_TCPIP_CORE_LOCKING
  1152. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1153. #endif
  1154. apiflags |= TCP_WRITE_FLAG_MORE;
  1155. } else {
  1156. len = (u16_t)diff;
  1157. }
  1158. available = tcp_sndbuf(conn->pcb.tcp);
  1159. if (available < len) {
  1160. /* don't try to write more than sendbuf */
  1161. len = available;
  1162. if (dontblock){
  1163. if (!len) {
  1164. err = ERR_WOULDBLOCK;
  1165. goto err_mem;
  1166. }
  1167. } else {
  1168. #if LWIP_TCPIP_CORE_LOCKING
  1169. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1170. #endif
  1171. apiflags |= TCP_WRITE_FLAG_MORE;
  1172. }
  1173. }
  1174. LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
  1175. err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
  1176. /* if OK or memory error, check available space */
  1177. if ((err == ERR_OK) || (err == ERR_MEM)) {
  1178. err_mem:
  1179. if (dontblock && (len < conn->current_msg->msg.w.len)) {
  1180. /* non-blocking write did not write everything: mark the pcb non-writable
  1181. and let poll_tcp check writable space to mark the pcb writable again */
  1182. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  1183. conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
  1184. } else if ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
  1185. (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT)) {
  1186. /* The queued byte- or pbuf-count exceeds the configured low-water limit,
  1187. let select mark this pcb as non-writable. */
  1188. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  1189. }
  1190. }
  1191. if (err == ERR_OK) {
  1192. conn->write_offset += len;
  1193. if ((conn->write_offset == conn->current_msg->msg.w.len) || dontblock) {
  1194. /* return sent length */
  1195. conn->current_msg->msg.w.len = conn->write_offset;
  1196. /* everything was written */
  1197. write_finished = 1;
  1198. conn->write_offset = 0;
  1199. }
  1200. tcp_output(conn->pcb.tcp);
  1201. } else if ((err == ERR_MEM) && !dontblock) {
  1202. /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
  1203. we do NOT return to the application thread, since ERR_MEM is
  1204. only a temporary error! */
  1205. /* tcp_write returned ERR_MEM, try tcp_output anyway */
  1206. tcp_output(conn->pcb.tcp);
  1207. #if LWIP_TCPIP_CORE_LOCKING
  1208. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1209. #endif
  1210. } else {
  1211. /* On errors != ERR_MEM, we don't try writing any more but return
  1212. the error to the application thread. */
  1213. write_finished = 1;
  1214. conn->current_msg->msg.w.len = 0;
  1215. }
  1216. }
  1217. if (write_finished) {
  1218. /* everything was written: set back connection state
  1219. and back to application task */
  1220. conn->current_msg->err = err;
  1221. conn->current_msg = NULL;
  1222. conn->state = NETCONN_NONE;
  1223. #if LWIP_TCPIP_CORE_LOCKING
  1224. if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
  1225. #endif
  1226. {
  1227. sys_sem_signal(&conn->op_completed);
  1228. }
  1229. }
  1230. #if LWIP_TCPIP_CORE_LOCKING
  1231. else
  1232. return ERR_MEM;
  1233. #endif
  1234. return ERR_OK;
  1235. }
  1236. #endif /* LWIP_TCP */
  1237. /**
  1238. * Send some data on a TCP pcb contained in a netconn
  1239. * Called from netconn_write
  1240. *
  1241. * @param msg the api_msg_msg pointing to the connection
  1242. */
  1243. void
  1244. do_write(struct api_msg_msg *msg)
  1245. {
  1246. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1247. msg->err = msg->conn->last_err;
  1248. } else {
  1249. if (msg->conn->type == NETCONN_TCP) {
  1250. #if LWIP_TCP
  1251. if (msg->conn->state != NETCONN_NONE) {
  1252. /* netconn is connecting, closing or in blocking write */
  1253. msg->err = ERR_INPROGRESS;
  1254. } else if (msg->conn->pcb.tcp != NULL) {
  1255. msg->conn->state = NETCONN_WRITE;
  1256. /* set all the variables used by do_writemore */
  1257. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  1258. msg->conn->write_offset == 0);
  1259. LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
  1260. msg->conn->current_msg = msg;
  1261. msg->conn->write_offset = 0;
  1262. #if LWIP_TCPIP_CORE_LOCKING
  1263. msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
  1264. if (do_writemore(msg->conn) != ERR_OK) {
  1265. LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
  1266. UNLOCK_TCPIP_CORE();
  1267. sys_arch_sem_wait(&msg->conn->op_completed, 0);
  1268. LOCK_TCPIP_CORE();
  1269. LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
  1270. }
  1271. #else /* LWIP_TCPIP_CORE_LOCKING */
  1272. do_writemore(msg->conn);
  1273. #endif /* LWIP_TCPIP_CORE_LOCKING */
  1274. /* for both cases: if do_writemore was called, don't ACK the APIMSG
  1275. since do_writemore ACKs it! */
  1276. return;
  1277. } else {
  1278. msg->err = ERR_CONN;
  1279. }
  1280. #else /* LWIP_TCP */
  1281. msg->err = ERR_VAL;
  1282. #endif /* LWIP_TCP */
  1283. #if (LWIP_UDP || LWIP_RAW)
  1284. } else {
  1285. msg->err = ERR_VAL;
  1286. #endif /* (LWIP_UDP || LWIP_RAW) */
  1287. }
  1288. }
  1289. TCPIP_APIMSG_ACK(msg);
  1290. }
  1291. /**
  1292. * Return a connection's local or remote address
  1293. * Called from netconn_getaddr
  1294. *
  1295. * @param msg the api_msg_msg pointing to the connection
  1296. */
  1297. void
  1298. do_getaddr(struct api_msg_msg *msg)
  1299. {
  1300. if (msg->conn->pcb.ip != NULL) {
  1301. *(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip :
  1302. msg->conn->pcb.ip->remote_ip);
  1303. msg->err = ERR_OK;
  1304. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1305. #if LWIP_RAW
  1306. case NETCONN_RAW:
  1307. if (msg->msg.ad.local) {
  1308. *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
  1309. } else {
  1310. /* return an error as connecting is only a helper for upper layers */
  1311. msg->err = ERR_CONN;
  1312. }
  1313. break;
  1314. #endif /* LWIP_RAW */
  1315. #if LWIP_UDP
  1316. case NETCONN_UDP:
  1317. if (msg->msg.ad.local) {
  1318. *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
  1319. } else {
  1320. if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
  1321. msg->err = ERR_CONN;
  1322. } else {
  1323. *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
  1324. }
  1325. }
  1326. break;
  1327. #endif /* LWIP_UDP */
  1328. #if LWIP_TCP
  1329. case NETCONN_TCP:
  1330. *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
  1331. break;
  1332. #endif /* LWIP_TCP */
  1333. default:
  1334. LWIP_ASSERT("invalid netconn_type", 0);
  1335. break;
  1336. }
  1337. } else {
  1338. msg->err = ERR_CONN;
  1339. }
  1340. TCPIP_APIMSG_ACK(msg);
  1341. }
  1342. /**
  1343. * Close a TCP pcb contained in a netconn
  1344. * Called from netconn_close
  1345. *
  1346. * @param msg the api_msg_msg pointing to the connection
  1347. */
  1348. void
  1349. do_close(struct api_msg_msg *msg)
  1350. {
  1351. #if LWIP_TCP
  1352. /* @todo: abort running write/connect? */
  1353. if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
  1354. /* this only happens for TCP netconns */
  1355. LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
  1356. msg->err = ERR_INPROGRESS;
  1357. } else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
  1358. if ((msg->msg.sd.shut != NETCONN_SHUT_RDWR) && (msg->conn->state == NETCONN_LISTEN)) {
  1359. /* LISTEN doesn't support half shutdown */
  1360. msg->err = ERR_CONN;
  1361. } else {
  1362. if (msg->msg.sd.shut & NETCONN_SHUT_RD) {
  1363. /* Drain and delete mboxes */
  1364. netconn_drain(msg->conn);
  1365. }
  1366. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  1367. msg->conn->write_offset == 0);
  1368. msg->conn->state = NETCONN_CLOSE;
  1369. msg->conn->current_msg = msg;
  1370. do_close_internal(msg->conn);
  1371. /* for tcp netconns, do_close_internal ACKs the message */
  1372. return;
  1373. }
  1374. } else
  1375. #endif /* LWIP_TCP */
  1376. {
  1377. msg->err = ERR_VAL;
  1378. }
  1379. sys_sem_signal(&msg->conn->op_completed);
  1380. }
  1381. #if LWIP_IGMP
  1382. /**
  1383. * Join multicast groups for UDP netconns.
  1384. * Called from netconn_join_leave_group
  1385. *
  1386. * @param msg the api_msg_msg pointing to the connection
  1387. */
  1388. void
  1389. do_join_leave_group(struct api_msg_msg *msg)
  1390. {
  1391. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1392. msg->err = msg->conn->last_err;
  1393. } else {
  1394. if (msg->conn->pcb.tcp != NULL) {
  1395. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  1396. #if LWIP_UDP
  1397. if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
  1398. msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
  1399. } else {
  1400. msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
  1401. }
  1402. #endif /* LWIP_UDP */
  1403. #if (LWIP_TCP || LWIP_RAW)
  1404. } else {
  1405. msg->err = ERR_VAL;
  1406. #endif /* (LWIP_TCP || LWIP_RAW) */
  1407. }
  1408. } else {
  1409. msg->err = ERR_CONN;
  1410. }
  1411. }
  1412. TCPIP_APIMSG_ACK(msg);
  1413. }
  1414. #endif /* LWIP_IGMP */
  1415. #if LWIP_DNS
  1416. /**
  1417. * Callback function that is called when DNS name is resolved
  1418. * (or on timeout). A waiting application thread is waked up by
  1419. * signaling the semaphore.
  1420. */
  1421. static void
  1422. do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  1423. {
  1424. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1425. LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
  1426. LWIP_UNUSED_ARG(name);
  1427. if (ipaddr == NULL) {
  1428. /* timeout or memory error */
  1429. *msg->err = ERR_VAL;
  1430. } else {
  1431. /* address was resolved */
  1432. *msg->err = ERR_OK;
  1433. *msg->addr = *ipaddr;
  1434. }
  1435. /* wake up the application task waiting in netconn_gethostbyname */
  1436. sys_sem_signal(msg->sem);
  1437. }
  1438. /**
  1439. * Execute a DNS query
  1440. * Called from netconn_gethostbyname
  1441. *
  1442. * @param arg the dns_api_msg pointing to the query
  1443. */
  1444. void
  1445. do_gethostbyname(void *arg)
  1446. {
  1447. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1448. *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
  1449. if (*msg->err != ERR_INPROGRESS) {
  1450. /* on error or immediate success, wake up the application
  1451. * task waiting in netconn_gethostbyname */
  1452. sys_sem_signal(msg->sem);
  1453. }
  1454. }
  1455. #endif /* LWIP_DNS */
  1456. #endif /* LWIP_NETCONN */