api_msg.c 62 KB

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