api_msg.c 61 KB

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