api_msg.c 59 KB

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