api_msg.c 44 KB

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