api_msg.c 59 KB

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