api_msg.c 59 KB

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