api_msg.c 60 KB

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