api_msg.c 59 KB

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