api_msg.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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 <[email protected]>
  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/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/tcpip.h"
  46. #include "lwip/igmp.h"
  47. #include "lwip/dns.h"
  48. #include <string.h>
  49. #define SET_NONBLOCKING_CONNECT(conn, val) do { if(val) { \
  50. (conn)->flags |= NETCONN_FLAG_IN_NONBLOCKING_CONNECT; \
  51. } else { \
  52. (conn)->flags &= ~ NETCONN_FLAG_IN_NONBLOCKING_CONNECT; }} while(0)
  53. #define IN_NONBLOCKING_CONNECT(conn) (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0)
  54. /* forward declarations */
  55. #if LWIP_TCP
  56. static err_t do_writemore(struct netconn *conn);
  57. static void do_close_internal(struct netconn *conn);
  58. #endif
  59. #if LWIP_RAW
  60. /**
  61. * Receive callback function for RAW netconns.
  62. * Doesn't 'eat' the packet, only references it and sends it to
  63. * conn->recvmbox
  64. *
  65. * @see raw.h (struct raw_pcb.recv) for parameters and return value
  66. */
  67. static u8_t
  68. recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
  69. ip_addr_t *addr)
  70. {
  71. struct pbuf *q;
  72. struct netbuf *buf;
  73. struct netconn *conn;
  74. LWIP_UNUSED_ARG(addr);
  75. conn = (struct netconn *)arg;
  76. if ((conn != NULL) && sys_mbox_valid(&conn->recvmbox)) {
  77. #if LWIP_SO_RCVBUF
  78. int recv_avail;
  79. SYS_ARCH_GET(conn->recv_avail, recv_avail);
  80. if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) {
  81. return 0;
  82. }
  83. #endif /* LWIP_SO_RCVBUF */
  84. /* copy the whole packet into new pbufs */
  85. q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
  86. if(q != NULL) {
  87. if (pbuf_copy(q, p) != ERR_OK) {
  88. pbuf_free(q);
  89. q = NULL;
  90. }
  91. }
  92. if (q != NULL) {
  93. u16_t len;
  94. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  95. if (buf == NULL) {
  96. pbuf_free(q);
  97. return 0;
  98. }
  99. buf->p = q;
  100. buf->ptr = q;
  101. ip_addr_copy(buf->addr, *ip_current_src_addr());
  102. buf->port = pcb->protocol;
  103. len = q->tot_len;
  104. if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
  105. netbuf_delete(buf);
  106. return 0;
  107. } else {
  108. #if LWIP_SO_RCVBUF
  109. SYS_ARCH_INC(conn->recv_avail, len);
  110. #endif /* LWIP_SO_RCVBUF */
  111. /* Register event with callback */
  112. API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
  113. }
  114. }
  115. }
  116. return 0; /* do not eat the packet */
  117. }
  118. #endif /* LWIP_RAW*/
  119. #if LWIP_UDP
  120. /**
  121. * Receive callback function for UDP netconns.
  122. * Posts the packet to conn->recvmbox or deletes it on memory error.
  123. *
  124. * @see udp.h (struct udp_pcb.recv) for parameters
  125. */
  126. static void
  127. recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  128. ip_addr_t *addr, u16_t port)
  129. {
  130. struct netbuf *buf;
  131. struct netconn *conn;
  132. u16_t len;
  133. #if LWIP_SO_RCVBUF
  134. int recv_avail;
  135. #endif /* LWIP_SO_RCVBUF */
  136. LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
  137. LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
  138. LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
  139. conn = (struct netconn *)arg;
  140. LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
  141. #if LWIP_SO_RCVBUF
  142. SYS_ARCH_GET(conn->recv_avail, recv_avail);
  143. if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox) ||
  144. ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
  145. #else /* LWIP_SO_RCVBUF */
  146. if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox)) {
  147. #endif /* LWIP_SO_RCVBUF */
  148. pbuf_free(p);
  149. return;
  150. }
  151. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  152. if (buf == NULL) {
  153. pbuf_free(p);
  154. return;
  155. } else {
  156. buf->p = p;
  157. buf->ptr = p;
  158. ip_addr_set(&buf->addr, addr);
  159. buf->port = port;
  160. #if LWIP_NETBUF_RECVINFO
  161. {
  162. const struct ip_hdr* iphdr = ip_current_header();
  163. /* get the UDP header - always in the first pbuf, ensured by udp_input */
  164. const struct udp_hdr* udphdr = (void*)(((char*)iphdr) + IPH_LEN(iphdr));
  165. #if LWIP_CHECKSUM_ON_COPY
  166. buf->flags = NETBUF_FLAG_DESTADDR;
  167. #endif /* LWIP_CHECKSUM_ON_COPY */
  168. ip_addr_set(&buf->toaddr, ip_current_dest_addr());
  169. buf->toport_chksum = udphdr->dest;
  170. }
  171. #endif /* LWIP_NETBUF_RECVINFO */
  172. }
  173. len = p->tot_len;
  174. if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
  175. netbuf_delete(buf);
  176. return;
  177. } else {
  178. #if LWIP_SO_RCVBUF
  179. SYS_ARCH_INC(conn->recv_avail, len);
  180. #endif /* LWIP_SO_RCVBUF */
  181. /* Register event with callback */
  182. API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
  183. }
  184. }
  185. #endif /* LWIP_UDP */
  186. #if LWIP_TCP
  187. /**
  188. * Receive callback function for TCP netconns.
  189. * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
  190. *
  191. * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
  192. */
  193. static err_t
  194. recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  195. {
  196. struct netconn *conn;
  197. u16_t len;
  198. LWIP_UNUSED_ARG(pcb);
  199. LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
  200. LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
  201. conn = (struct netconn *)arg;
  202. LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
  203. if (conn == NULL) {
  204. return ERR_VAL;
  205. }
  206. if (!sys_mbox_valid(&conn->recvmbox)) {
  207. /* recvmbox already deleted */
  208. if (p != NULL) {
  209. tcp_recved(pcb, p->tot_len);
  210. pbuf_free(p);
  211. }
  212. return ERR_OK;
  213. }
  214. /* Unlike for UDP or RAW pcbs, don't check for available space
  215. using recv_avail since that could break the connection
  216. (data is already ACKed) */
  217. /* don't overwrite fatal errors! */
  218. NETCONN_SET_SAFE_ERR(conn, err);
  219. if (p != NULL) {
  220. len = p->tot_len;
  221. } else {
  222. len = 0;
  223. }
  224. if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) {
  225. /* don't deallocate p: it is presented to us later again from tcp_fasttmr! */
  226. return ERR_MEM;
  227. } else {
  228. #if LWIP_SO_RCVBUF
  229. SYS_ARCH_INC(conn->recv_avail, len);
  230. #endif /* LWIP_SO_RCVBUF */
  231. /* Register event with callback */
  232. API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
  233. }
  234. return ERR_OK;
  235. }
  236. /**
  237. * Poll callback function for TCP netconns.
  238. * Wakes up an application thread that waits for a connection to close
  239. * or data to be sent. The application thread then takes the
  240. * appropriate action to go on.
  241. *
  242. * Signals the conn->sem.
  243. * netconn_close waits for conn->sem if closing failed.
  244. *
  245. * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
  246. */
  247. static err_t
  248. poll_tcp(void *arg, struct tcp_pcb *pcb)
  249. {
  250. struct netconn *conn = (struct netconn *)arg;
  251. LWIP_UNUSED_ARG(pcb);
  252. LWIP_ASSERT("conn != NULL", (conn != NULL));
  253. if (conn->state == NETCONN_WRITE) {
  254. do_writemore(conn);
  255. } else if (conn->state == NETCONN_CLOSE) {
  256. do_close_internal(conn);
  257. }
  258. /* @todo: implement connect timeout here? */
  259. /* Did a nonblocking write fail before? Then check available write-space. */
  260. if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) {
  261. /* If the queued byte- or pbuf-count drops below the configured low-water limit,
  262. let select mark this pcb as writable again. */
  263. if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
  264. (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
  265. conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
  266. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  267. }
  268. }
  269. return ERR_OK;
  270. }
  271. /**
  272. * Sent callback function for TCP netconns.
  273. * Signals the conn->sem and calls API_EVENT.
  274. * netconn_write waits for conn->sem if send buffer is low.
  275. *
  276. * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
  277. */
  278. static err_t
  279. sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
  280. {
  281. struct netconn *conn = (struct netconn *)arg;
  282. LWIP_UNUSED_ARG(pcb);
  283. LWIP_ASSERT("conn != NULL", (conn != NULL));
  284. if (conn->state == NETCONN_WRITE) {
  285. do_writemore(conn);
  286. } else if (conn->state == NETCONN_CLOSE) {
  287. do_close_internal(conn);
  288. }
  289. if (conn) {
  290. /* If the queued byte- or pbuf-count drops below the configured low-water limit,
  291. let select mark this pcb as writable again. */
  292. if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
  293. (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
  294. conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
  295. API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
  296. }
  297. }
  298. return ERR_OK;
  299. }
  300. /**
  301. * Error callback function for TCP netconns.
  302. * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
  303. * The application thread has then to decide what to do.
  304. *
  305. * @see tcp.h (struct tcp_pcb.err) for parameters
  306. */
  307. static void
  308. err_tcp(void *arg, err_t err)
  309. {
  310. struct netconn *conn;
  311. enum netconn_state old_state;
  312. SYS_ARCH_DECL_PROTECT(lev);
  313. conn = (struct netconn *)arg;
  314. LWIP_ASSERT("conn != NULL", (conn != NULL));
  315. conn->pcb.tcp = NULL;
  316. /* no check since this is always fatal! */
  317. SYS_ARCH_PROTECT(lev);
  318. conn->last_err = err;
  319. SYS_ARCH_UNPROTECT(lev);
  320. /* reset conn->state now before waking up other threads */
  321. old_state = conn->state;
  322. conn->state = NETCONN_NONE;
  323. /* Notify the user layer about a connection error. Used to signal
  324. select. */
  325. API_EVENT(conn, NETCONN_EVT_ERROR, 0);
  326. /* Try to release selects pending on 'read' or 'write', too.
  327. They will get an error if they actually try to read or write. */
  328. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  329. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  330. /* pass NULL-message to recvmbox to wake up pending recv */
  331. if (sys_mbox_valid(&conn->recvmbox)) {
  332. /* use trypost to prevent deadlock */
  333. sys_mbox_trypost(&conn->recvmbox, NULL);
  334. }
  335. /* pass NULL-message to acceptmbox to wake up pending accept */
  336. if (sys_mbox_valid(&conn->acceptmbox)) {
  337. /* use trypost to preven deadlock */
  338. sys_mbox_trypost(&conn->acceptmbox, NULL);
  339. }
  340. if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) ||
  341. (old_state == NETCONN_CONNECT)) {
  342. /* calling do_writemore/do_close_internal is not necessary
  343. since the pcb has already been deleted! */
  344. int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn);
  345. SET_NONBLOCKING_CONNECT(conn, 0);
  346. if (!was_nonblocking_connect) {
  347. /* set error return code */
  348. LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
  349. conn->current_msg->err = err;
  350. conn->current_msg = NULL;
  351. /* wake up the waiting task */
  352. sys_sem_signal(&conn->op_completed);
  353. }
  354. } else {
  355. LWIP_ASSERT("conn->current_msg == NULL", conn->current_msg == NULL);
  356. }
  357. }
  358. /**
  359. * Setup a tcp_pcb with the correct callback function pointers
  360. * and their arguments.
  361. *
  362. * @param conn the TCP netconn to setup
  363. */
  364. static void
  365. setup_tcp(struct netconn *conn)
  366. {
  367. struct tcp_pcb *pcb;
  368. pcb = conn->pcb.tcp;
  369. tcp_arg(pcb, conn);
  370. tcp_recv(pcb, recv_tcp);
  371. tcp_sent(pcb, sent_tcp);
  372. tcp_poll(pcb, poll_tcp, 4);
  373. tcp_err(pcb, err_tcp);
  374. }
  375. /**
  376. * Accept callback function for TCP netconns.
  377. * Allocates a new netconn and posts that to conn->acceptmbox.
  378. *
  379. * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
  380. */
  381. static err_t
  382. accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
  383. {
  384. struct netconn *newconn;
  385. struct netconn *conn = (struct netconn *)arg;
  386. LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: newpcb->tate: %s\n", tcp_debug_state_str(newpcb->state)));
  387. if (!sys_mbox_valid(&conn->acceptmbox)) {
  388. LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: acceptmbox already deleted\n"));
  389. return ERR_VAL;
  390. }
  391. /* We have to set the callback here even though
  392. * the new socket is unknown. conn->socket is marked as -1. */
  393. newconn = netconn_alloc(conn->type, conn->callback);
  394. if (newconn == NULL) {
  395. return ERR_MEM;
  396. }
  397. newconn->pcb.tcp = newpcb;
  398. setup_tcp(newconn);
  399. /* no protection: when creating the pcb, the netconn is not yet known
  400. to the application thread */
  401. newconn->last_err = err;
  402. if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) {
  403. /* When returning != ERR_OK, the pcb is aborted in tcp_process(),
  404. so do nothing here! */
  405. /* remove all references to this netconn from the pcb */
  406. struct tcp_pcb* pcb = newconn->pcb.tcp;
  407. tcp_arg(pcb, NULL);
  408. tcp_recv(pcb, NULL);
  409. tcp_sent(pcb, NULL);
  410. tcp_poll(pcb, NULL, 4);
  411. tcp_err(pcb, NULL);
  412. /* remove reference from to the pcb from this netconn */
  413. newconn->pcb.tcp = NULL;
  414. /* no need to drain since we know the recvmbox is empty. */
  415. sys_mbox_free(&newconn->recvmbox);
  416. sys_mbox_set_invalid(&newconn->recvmbox);
  417. netconn_free(newconn);
  418. return ERR_MEM;
  419. } else {
  420. /* Register event with callback */
  421. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  422. }
  423. return ERR_OK;
  424. }
  425. #endif /* LWIP_TCP */
  426. /**
  427. * Create a new pcb of a specific type.
  428. * Called from do_newconn().
  429. *
  430. * @param msg the api_msg_msg describing the connection type
  431. * @return msg->conn->err, but the return value is currently ignored
  432. */
  433. static void
  434. pcb_new(struct api_msg_msg *msg)
  435. {
  436. LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
  437. /* Allocate a PCB for this connection */
  438. switch(NETCONNTYPE_GROUP(msg->conn->type)) {
  439. #if LWIP_RAW
  440. case NETCONN_RAW:
  441. msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
  442. if(msg->conn->pcb.raw == NULL) {
  443. msg->err = ERR_MEM;
  444. break;
  445. }
  446. raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
  447. break;
  448. #endif /* LWIP_RAW */
  449. #if LWIP_UDP
  450. case NETCONN_UDP:
  451. msg->conn->pcb.udp = udp_new();
  452. if(msg->conn->pcb.udp == NULL) {
  453. msg->err = ERR_MEM;
  454. break;
  455. }
  456. #if LWIP_UDPLITE
  457. if (msg->conn->type==NETCONN_UDPLITE) {
  458. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
  459. }
  460. #endif /* LWIP_UDPLITE */
  461. if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
  462. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
  463. }
  464. udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
  465. break;
  466. #endif /* LWIP_UDP */
  467. #if LWIP_TCP
  468. case NETCONN_TCP:
  469. msg->conn->pcb.tcp = tcp_new();
  470. if(msg->conn->pcb.tcp == NULL) {
  471. msg->err = ERR_MEM;
  472. break;
  473. }
  474. setup_tcp(msg->conn);
  475. break;
  476. #endif /* LWIP_TCP */
  477. default:
  478. /* Unsupported netconn type, e.g. protocol disabled */
  479. msg->err = ERR_VAL;
  480. break;
  481. }
  482. }
  483. /**
  484. * Create a new pcb of a specific type inside a netconn.
  485. * Called from netconn_new_with_proto_and_callback.
  486. *
  487. * @param msg the api_msg_msg describing the connection type
  488. */
  489. void
  490. do_newconn(struct api_msg_msg *msg)
  491. {
  492. msg->err = ERR_OK;
  493. if(msg->conn->pcb.tcp == NULL) {
  494. pcb_new(msg);
  495. }
  496. /* Else? This "new" connection already has a PCB allocated. */
  497. /* Is this an error condition? Should it be deleted? */
  498. /* We currently just are happy and return. */
  499. TCPIP_APIMSG_ACK(msg);
  500. }
  501. /**
  502. * Create a new netconn (of a specific type) that has a callback function.
  503. * The corresponding pcb is NOT created!
  504. *
  505. * @param t the type of 'connection' to create (@see enum netconn_type)
  506. * @param proto the IP protocol for RAW IP pcbs
  507. * @param callback a function to call on status changes (RX available, TX'ed)
  508. * @return a newly allocated struct netconn or
  509. * NULL on memory error
  510. */
  511. struct netconn*
  512. netconn_alloc(enum netconn_type t, netconn_callback callback)
  513. {
  514. struct netconn *conn;
  515. int size;
  516. conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
  517. if (conn == NULL) {
  518. return NULL;
  519. }
  520. conn->last_err = ERR_OK;
  521. conn->type = t;
  522. conn->pcb.tcp = NULL;
  523. #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
  524. (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
  525. size = DEFAULT_RAW_RECVMBOX_SIZE;
  526. #else
  527. switch(NETCONNTYPE_GROUP(t)) {
  528. #if LWIP_RAW
  529. case NETCONN_RAW:
  530. size = DEFAULT_RAW_RECVMBOX_SIZE;
  531. break;
  532. #endif /* LWIP_RAW */
  533. #if LWIP_UDP
  534. case NETCONN_UDP:
  535. size = DEFAULT_UDP_RECVMBOX_SIZE;
  536. break;
  537. #endif /* LWIP_UDP */
  538. #if LWIP_TCP
  539. case NETCONN_TCP:
  540. size = DEFAULT_TCP_RECVMBOX_SIZE;
  541. break;
  542. #endif /* LWIP_TCP */
  543. default:
  544. LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
  545. goto free_and_return;
  546. }
  547. #endif
  548. if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) {
  549. goto free_and_return;
  550. }
  551. if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) {
  552. sys_sem_free(&conn->op_completed);
  553. goto free_and_return;
  554. }
  555. #if LWIP_TCP
  556. sys_mbox_set_invalid(&conn->acceptmbox);
  557. #endif
  558. conn->state = NETCONN_NONE;
  559. #if LWIP_SOCKET
  560. /* initialize socket to -1 since 0 is a valid socket */
  561. conn->socket = -1;
  562. #endif /* LWIP_SOCKET */
  563. conn->callback = callback;
  564. #if LWIP_TCP
  565. conn->current_msg = NULL;
  566. conn->write_offset = 0;
  567. #endif /* LWIP_TCP */
  568. #if LWIP_SO_SNDTIMEO
  569. conn->send_timeout = 0;
  570. #endif /* LWIP_SO_SNDTIMEO */
  571. #if LWIP_SO_RCVTIMEO
  572. conn->recv_timeout = 0;
  573. #endif /* LWIP_SO_RCVTIMEO */
  574. #if LWIP_SO_RCVBUF
  575. conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
  576. conn->recv_avail = 0;
  577. #endif /* LWIP_SO_RCVBUF */
  578. conn->flags = 0;
  579. return conn;
  580. free_and_return:
  581. memp_free(MEMP_NETCONN, conn);
  582. return NULL;
  583. }
  584. /**
  585. * Delete a netconn and all its resources.
  586. * The pcb is NOT freed (since we might not be in the right thread context do this).
  587. *
  588. * @param conn the netconn to free
  589. */
  590. void
  591. netconn_free(struct netconn *conn)
  592. {
  593. LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
  594. LWIP_ASSERT("recvmbox must be deallocated before calling this function",
  595. !sys_mbox_valid(&conn->recvmbox));
  596. #if LWIP_TCP
  597. LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
  598. !sys_mbox_valid(&conn->acceptmbox));
  599. #endif /* LWIP_TCP */
  600. sys_sem_free(&conn->op_completed);
  601. sys_sem_set_invalid(&conn->op_completed);
  602. memp_free(MEMP_NETCONN, conn);
  603. }
  604. /**
  605. * Delete rcvmbox and acceptmbox of a netconn and free the left-over data in
  606. * these mboxes
  607. *
  608. * @param conn the netconn to free
  609. * @bytes_drained bytes drained from recvmbox
  610. * @accepts_drained pending connections drained from acceptmbox
  611. */
  612. static void
  613. netconn_drain(struct netconn *conn)
  614. {
  615. void *mem;
  616. #if LWIP_TCP
  617. struct pbuf *p;
  618. #endif /* LWIP_TCP */
  619. /* This runs in tcpip_thread, so we don't need to lock against rx packets */
  620. /* Delete and drain the recvmbox. */
  621. if (sys_mbox_valid(&conn->recvmbox)) {
  622. while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
  623. #if LWIP_TCP
  624. if (conn->type == NETCONN_TCP) {
  625. if(mem != NULL) {
  626. p = (struct pbuf*)mem;
  627. /* pcb might be set to NULL already by err_tcp() */
  628. if (conn->pcb.tcp != NULL) {
  629. tcp_recved(conn->pcb.tcp, p->tot_len);
  630. }
  631. pbuf_free(p);
  632. }
  633. } else
  634. #endif /* LWIP_TCP */
  635. {
  636. netbuf_delete((struct netbuf *)mem);
  637. }
  638. }
  639. sys_mbox_free(&conn->recvmbox);
  640. sys_mbox_set_invalid(&conn->recvmbox);
  641. }
  642. /* Delete and drain the acceptmbox. */
  643. #if LWIP_TCP
  644. if (sys_mbox_valid(&conn->acceptmbox)) {
  645. while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
  646. struct netconn *newconn = (struct netconn *)mem;
  647. /* Only tcp pcbs have an acceptmbox, so no need to check conn->type */
  648. /* pcb might be set to NULL already by err_tcp() */
  649. if (conn->pcb.tcp != NULL) {
  650. tcp_accepted(conn->pcb.tcp);
  651. }
  652. /* drain recvmbox */
  653. netconn_drain(newconn);
  654. if (newconn->pcb.tcp != NULL) {
  655. tcp_abort(newconn->pcb.tcp);
  656. newconn->pcb.tcp = NULL;
  657. }
  658. netconn_free(newconn);
  659. }
  660. sys_mbox_free(&conn->acceptmbox);
  661. sys_mbox_set_invalid(&conn->acceptmbox);
  662. }
  663. #endif /* LWIP_TCP */
  664. }
  665. #if LWIP_TCP
  666. /**
  667. * Internal helper function to close a TCP netconn: since this sometimes
  668. * doesn't work at the first attempt, this function is called from multiple
  669. * places.
  670. *
  671. * @param conn the TCP netconn to close
  672. */
  673. static void
  674. do_close_internal(struct netconn *conn)
  675. {
  676. err_t err;
  677. u8_t shut, shut_rx, shut_tx, close;
  678. LWIP_ASSERT("invalid conn", (conn != NULL));
  679. LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
  680. LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
  681. LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
  682. LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
  683. shut = conn->current_msg->msg.sd.shut;
  684. shut_rx = shut & NETCONN_SHUT_RD;
  685. shut_tx = shut & NETCONN_SHUT_WR;
  686. /* shutting down both ends is the same as closing */
  687. close = shut == NETCONN_SHUT_RDWR;
  688. /* Set back some callback pointers */
  689. if (close) {
  690. tcp_arg(conn->pcb.tcp, NULL);
  691. }
  692. if (conn->pcb.tcp->state == LISTEN) {
  693. tcp_accept(conn->pcb.tcp, NULL);
  694. } else {
  695. /* some callbacks have to be reset if tcp_close is not successful */
  696. if (shut_rx) {
  697. tcp_recv(conn->pcb.tcp, NULL);
  698. tcp_accept(conn->pcb.tcp, NULL);
  699. }
  700. if (shut_tx) {
  701. tcp_sent(conn->pcb.tcp, NULL);
  702. }
  703. if (close) {
  704. tcp_poll(conn->pcb.tcp, NULL, 4);
  705. tcp_err(conn->pcb.tcp, NULL);
  706. }
  707. }
  708. /* Try to close the connection */
  709. if (close) {
  710. err = tcp_close(conn->pcb.tcp);
  711. } else {
  712. err = tcp_shutdown(conn->pcb.tcp, shut_rx, shut_tx);
  713. }
  714. if (err == ERR_OK) {
  715. /* Closing succeeded */
  716. conn->current_msg->err = ERR_OK;
  717. conn->current_msg = NULL;
  718. conn->state = NETCONN_NONE;
  719. if (close) {
  720. /* Set back some callback pointers as conn is going away */
  721. conn->pcb.tcp = NULL;
  722. /* Trigger select() in socket layer. Make sure everybody notices activity
  723. on the connection, error first! */
  724. API_EVENT(conn, NETCONN_EVT_ERROR, 0);
  725. }
  726. if (shut_rx) {
  727. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  728. }
  729. if (shut_tx) {
  730. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  731. }
  732. /* wake up the application task */
  733. sys_sem_signal(&conn->op_completed);
  734. } else {
  735. /* Closing failed, restore some of the callbacks */
  736. /* Closing of listen pcb will never fail! */
  737. LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
  738. tcp_sent(conn->pcb.tcp, sent_tcp);
  739. tcp_poll(conn->pcb.tcp, poll_tcp, 4);
  740. tcp_err(conn->pcb.tcp, err_tcp);
  741. tcp_arg(conn->pcb.tcp, conn);
  742. /* don't restore recv callback: we don't want to receive any more data */
  743. }
  744. /* If closing didn't succeed, we get called again either
  745. from poll_tcp or from sent_tcp */
  746. }
  747. #endif /* LWIP_TCP */
  748. /**
  749. * Delete the pcb inside a netconn.
  750. * Called from netconn_delete.
  751. *
  752. * @param msg the api_msg_msg pointing to the connection
  753. */
  754. void
  755. do_delconn(struct api_msg_msg *msg)
  756. {
  757. /* @todo TCP: abort running write/connect? */
  758. if ((msg->conn->state != NETCONN_NONE) &&
  759. (msg->conn->state != NETCONN_LISTEN) &&
  760. (msg->conn->state != NETCONN_CONNECT)) {
  761. /* this only happens for TCP netconns */
  762. LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
  763. msg->err = ERR_INPROGRESS;
  764. } else {
  765. LWIP_ASSERT("blocking connect in progress",
  766. (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
  767. /* Drain and delete mboxes */
  768. netconn_drain(msg->conn);
  769. if (msg->conn->pcb.tcp != NULL) {
  770. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  771. #if LWIP_RAW
  772. case NETCONN_RAW:
  773. raw_remove(msg->conn->pcb.raw);
  774. break;
  775. #endif /* LWIP_RAW */
  776. #if LWIP_UDP
  777. case NETCONN_UDP:
  778. msg->conn->pcb.udp->recv_arg = NULL;
  779. udp_remove(msg->conn->pcb.udp);
  780. break;
  781. #endif /* LWIP_UDP */
  782. #if LWIP_TCP
  783. case NETCONN_TCP:
  784. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  785. msg->conn->write_offset == 0);
  786. msg->conn->state = NETCONN_CLOSE;
  787. msg->msg.sd.shut = NETCONN_SHUT_RDWR;
  788. msg->conn->current_msg = msg;
  789. do_close_internal(msg->conn);
  790. /* API_EVENT is called inside do_close_internal, before releasing
  791. the application thread, so we can return at this point! */
  792. return;
  793. #endif /* LWIP_TCP */
  794. default:
  795. break;
  796. }
  797. msg->conn->pcb.tcp = NULL;
  798. }
  799. /* tcp netconns don't come here! */
  800. /* @todo: this lets select make the socket readable and writable,
  801. which is wrong! errfd instead? */
  802. API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
  803. API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
  804. }
  805. if (sys_sem_valid(&msg->conn->op_completed)) {
  806. sys_sem_signal(&msg->conn->op_completed);
  807. }
  808. }
  809. /**
  810. * Bind a pcb contained in a netconn
  811. * Called from netconn_bind.
  812. *
  813. * @param msg the api_msg_msg pointing to the connection and containing
  814. * the IP address and port to bind to
  815. */
  816. void
  817. do_bind(struct api_msg_msg *msg)
  818. {
  819. if (ERR_IS_FATAL(msg->conn->last_err)) {
  820. msg->err = msg->conn->last_err;
  821. } else {
  822. msg->err = ERR_VAL;
  823. if (msg->conn->pcb.tcp != NULL) {
  824. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  825. #if LWIP_RAW
  826. case NETCONN_RAW:
  827. msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  828. break;
  829. #endif /* LWIP_RAW */
  830. #if LWIP_UDP
  831. case NETCONN_UDP:
  832. msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  833. break;
  834. #endif /* LWIP_UDP */
  835. #if LWIP_TCP
  836. case NETCONN_TCP:
  837. msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  838. break;
  839. #endif /* LWIP_TCP */
  840. default:
  841. break;
  842. }
  843. }
  844. }
  845. TCPIP_APIMSG_ACK(msg);
  846. }
  847. #if LWIP_TCP
  848. /**
  849. * TCP callback function if a connection (opened by tcp_connect/do_connect) has
  850. * been established (or reset by the remote host).
  851. *
  852. * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
  853. */
  854. static err_t
  855. do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
  856. {
  857. struct netconn *conn;
  858. int was_blocking;
  859. LWIP_UNUSED_ARG(pcb);
  860. conn = (struct netconn *)arg;
  861. if (conn == NULL) {
  862. return ERR_VAL;
  863. }
  864. LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
  865. LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
  866. (conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
  867. if (conn->current_msg != NULL) {
  868. conn->current_msg->err = err;
  869. }
  870. if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
  871. setup_tcp(conn);
  872. }
  873. was_blocking = !IN_NONBLOCKING_CONNECT(conn);
  874. SET_NONBLOCKING_CONNECT(conn, 0);
  875. conn->current_msg = NULL;
  876. conn->state = NETCONN_NONE;
  877. if (!was_blocking) {
  878. NETCONN_SET_SAFE_ERR(conn, ERR_OK);
  879. }
  880. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  881. if (was_blocking) {
  882. sys_sem_signal(&conn->op_completed);
  883. }
  884. return ERR_OK;
  885. }
  886. #endif /* LWIP_TCP */
  887. /**
  888. * Connect a pcb contained inside a netconn
  889. * Called from netconn_connect.
  890. *
  891. * @param msg the api_msg_msg pointing to the connection and containing
  892. * the IP address and port to connect to
  893. */
  894. void
  895. do_connect(struct api_msg_msg *msg)
  896. {
  897. if (msg->conn->pcb.tcp == NULL) {
  898. /* This may happen when calling netconn_connect() a second time */
  899. msg->err = ERR_CLSD;
  900. } else {
  901. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  902. #if LWIP_RAW
  903. case NETCONN_RAW:
  904. msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  905. break;
  906. #endif /* LWIP_RAW */
  907. #if LWIP_UDP
  908. case NETCONN_UDP:
  909. msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  910. break;
  911. #endif /* LWIP_UDP */
  912. #if LWIP_TCP
  913. case NETCONN_TCP:
  914. /* Prevent connect while doing any other action. */
  915. if (msg->conn->state != NETCONN_NONE) {
  916. msg->err = ERR_ISCONN;
  917. } else {
  918. setup_tcp(msg->conn);
  919. msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr,
  920. msg->msg.bc.port, do_connected);
  921. if (msg->err == ERR_OK) {
  922. u8_t non_blocking = netconn_is_nonblocking(msg->conn);
  923. msg->conn->state = NETCONN_CONNECT;
  924. SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
  925. if (non_blocking) {
  926. msg->err = ERR_INPROGRESS;
  927. } else {
  928. msg->conn->current_msg = msg;
  929. /* sys_sem_signal() is called from do_connected (or err_tcp()),
  930. * when the connection is established! */
  931. return;
  932. }
  933. }
  934. }
  935. break;
  936. #endif /* LWIP_TCP */
  937. default:
  938. LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0));
  939. break;
  940. }
  941. }
  942. sys_sem_signal(&msg->conn->op_completed);
  943. }
  944. /**
  945. * Connect a pcb contained inside a netconn
  946. * Only used for UDP netconns.
  947. * Called from netconn_disconnect.
  948. *
  949. * @param msg the api_msg_msg pointing to the connection to disconnect
  950. */
  951. void
  952. do_disconnect(struct api_msg_msg *msg)
  953. {
  954. #if LWIP_UDP
  955. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  956. udp_disconnect(msg->conn->pcb.udp);
  957. msg->err = ERR_OK;
  958. } else
  959. #endif /* LWIP_UDP */
  960. {
  961. msg->err = ERR_VAL;
  962. }
  963. TCPIP_APIMSG_ACK(msg);
  964. }
  965. #if LWIP_TCP
  966. /**
  967. * Set a TCP pcb contained in a netconn into listen mode
  968. * Called from netconn_listen.
  969. *
  970. * @param msg the api_msg_msg pointing to the connection
  971. */
  972. void
  973. do_listen(struct api_msg_msg *msg)
  974. {
  975. if (ERR_IS_FATAL(msg->conn->last_err)) {
  976. msg->err = msg->conn->last_err;
  977. } else {
  978. msg->err = ERR_CONN;
  979. if (msg->conn->pcb.tcp != NULL) {
  980. if (msg->conn->type == NETCONN_TCP) {
  981. if (msg->conn->state == NETCONN_NONE) {
  982. #if TCP_LISTEN_BACKLOG
  983. struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
  984. #else /* TCP_LISTEN_BACKLOG */
  985. struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
  986. #endif /* TCP_LISTEN_BACKLOG */
  987. if (lpcb == NULL) {
  988. /* in this case, the old pcb is still allocated */
  989. msg->err = ERR_MEM;
  990. } else {
  991. /* delete the recvmbox and allocate the acceptmbox */
  992. if (sys_mbox_valid(&msg->conn->recvmbox)) {
  993. /** @todo: should we drain the recvmbox here? */
  994. sys_mbox_free(&msg->conn->recvmbox);
  995. sys_mbox_set_invalid(&msg->conn->recvmbox);
  996. }
  997. msg->err = ERR_OK;
  998. if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
  999. msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
  1000. }
  1001. if (msg->err == ERR_OK) {
  1002. msg->conn->state = NETCONN_LISTEN;
  1003. msg->conn->pcb.tcp = lpcb;
  1004. tcp_arg(msg->conn->pcb.tcp, msg->conn);
  1005. tcp_accept(msg->conn->pcb.tcp, accept_function);
  1006. } else {
  1007. /* since the old pcb is already deallocated, free lpcb now */
  1008. tcp_close(lpcb);
  1009. msg->conn->pcb.tcp = NULL;
  1010. }
  1011. }
  1012. }
  1013. } else {
  1014. msg->err = ERR_ARG;
  1015. }
  1016. }
  1017. }
  1018. TCPIP_APIMSG_ACK(msg);
  1019. }
  1020. #endif /* LWIP_TCP */
  1021. /**
  1022. * Send some data on a RAW or UDP pcb contained in a netconn
  1023. * Called from netconn_send
  1024. *
  1025. * @param msg the api_msg_msg pointing to the connection
  1026. */
  1027. void
  1028. do_send(struct api_msg_msg *msg)
  1029. {
  1030. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1031. msg->err = msg->conn->last_err;
  1032. } else {
  1033. msg->err = ERR_CONN;
  1034. if (msg->conn->pcb.tcp != NULL) {
  1035. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1036. #if LWIP_RAW
  1037. case NETCONN_RAW:
  1038. if (ip_addr_isany(&msg->msg.b->addr)) {
  1039. msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
  1040. } else {
  1041. msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, &msg->msg.b->addr);
  1042. }
  1043. break;
  1044. #endif
  1045. #if LWIP_UDP
  1046. case NETCONN_UDP:
  1047. #if LWIP_CHECKSUM_ON_COPY
  1048. if (ip_addr_isany(&msg->msg.b->addr)) {
  1049. msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
  1050. msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
  1051. } else {
  1052. msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
  1053. &msg->msg.b->addr, msg->msg.b->port,
  1054. msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
  1055. }
  1056. #else /* LWIP_CHECKSUM_ON_COPY */
  1057. if (ip_addr_isany(&msg->msg.b->addr)) {
  1058. msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
  1059. } else {
  1060. msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, &msg->msg.b->addr, msg->msg.b->port);
  1061. }
  1062. #endif /* LWIP_CHECKSUM_ON_COPY */
  1063. break;
  1064. #endif /* LWIP_UDP */
  1065. default:
  1066. break;
  1067. }
  1068. }
  1069. }
  1070. TCPIP_APIMSG_ACK(msg);
  1071. }
  1072. #if LWIP_TCP
  1073. /**
  1074. * Indicate data has been received from a TCP pcb contained in a netconn
  1075. * Called from netconn_recv
  1076. *
  1077. * @param msg the api_msg_msg pointing to the connection
  1078. */
  1079. void
  1080. do_recv(struct api_msg_msg *msg)
  1081. {
  1082. msg->err = ERR_OK;
  1083. if (msg->conn->pcb.tcp != NULL) {
  1084. if (msg->conn->type == NETCONN_TCP) {
  1085. #if TCP_LISTEN_BACKLOG
  1086. if (msg->conn->pcb.tcp->state == LISTEN) {
  1087. tcp_accepted(msg->conn->pcb.tcp);
  1088. } else
  1089. #endif /* TCP_LISTEN_BACKLOG */
  1090. {
  1091. u32_t remaining = msg->msg.r.len;
  1092. do {
  1093. u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining;
  1094. tcp_recved(msg->conn->pcb.tcp, recved);
  1095. remaining -= recved;
  1096. }while(remaining != 0);
  1097. }
  1098. }
  1099. }
  1100. TCPIP_APIMSG_ACK(msg);
  1101. }
  1102. /**
  1103. * See if more data needs to be written from a previous call to netconn_write.
  1104. * Called initially from do_write. If the first call can't send all data
  1105. * (because of low memory or empty send-buffer), this function is called again
  1106. * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
  1107. * blocking application thread (waiting in netconn_write) is released.
  1108. *
  1109. * @param conn netconn (that is currently in state NETCONN_WRITE) to process
  1110. * @return ERR_OK
  1111. * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
  1112. */
  1113. static err_t
  1114. do_writemore(struct netconn *conn)
  1115. {
  1116. err_t err;
  1117. void *dataptr;
  1118. u16_t len, available;
  1119. u8_t write_finished = 0;
  1120. size_t diff;
  1121. u8_t dontblock = netconn_is_nonblocking(conn) ||
  1122. (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
  1123. u8_t apiflags = conn->current_msg->msg.w.apiflags;
  1124. LWIP_ASSERT("conn != NULL", conn != NULL);
  1125. LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
  1126. LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
  1127. LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
  1128. LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
  1129. conn->write_offset < conn->current_msg->msg.w.len);
  1130. #if LWIP_SO_SNDTIMEO
  1131. if ((conn->send_timeout != 0) &&
  1132. ((s32_t)(sys_now() - conn->current_msg->msg.w.time_started) >= conn->send_timeout)) {
  1133. write_finished = 1;
  1134. if (conn->write_offset == 0) {
  1135. /* nothing has been written */
  1136. err = ERR_WOULDBLOCK;
  1137. conn->current_msg->msg.w.len = 0;
  1138. } else {
  1139. /* partial write */
  1140. err = ERR_OK;
  1141. conn->current_msg->msg.w.len = conn->write_offset;
  1142. }
  1143. } else
  1144. #endif /* LWIP_SO_SNDTIMEO */
  1145. {
  1146. dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
  1147. diff = conn->current_msg->msg.w.len - conn->write_offset;
  1148. if (diff > 0xffffUL) { /* max_u16_t */
  1149. len = 0xffff;
  1150. #if LWIP_TCPIP_CORE_LOCKING
  1151. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1152. #endif
  1153. apiflags |= TCP_WRITE_FLAG_MORE;
  1154. } else {
  1155. len = (u16_t)diff;
  1156. }
  1157. available = tcp_sndbuf(conn->pcb.tcp);
  1158. if (available < len) {
  1159. /* don't try to write more than sendbuf */
  1160. len = available;
  1161. if (dontblock){
  1162. if (!len) {
  1163. err = ERR_WOULDBLOCK;
  1164. goto err_mem;
  1165. }
  1166. } else {
  1167. #if LWIP_TCPIP_CORE_LOCKING
  1168. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1169. #endif
  1170. apiflags |= TCP_WRITE_FLAG_MORE;
  1171. }
  1172. }
  1173. LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
  1174. err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
  1175. /* if OK or memory error, check available space */
  1176. if ((err == ERR_OK) || (err == ERR_MEM)) {
  1177. err_mem:
  1178. if (dontblock && (len < conn->current_msg->msg.w.len)) {
  1179. /* non-blocking write did not write everything: mark the pcb non-writable
  1180. and let poll_tcp check writable space to mark the pcb writable again */
  1181. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  1182. conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
  1183. } else if ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
  1184. (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT)) {
  1185. /* The queued byte- or pbuf-count exceeds the configured low-water limit,
  1186. let select mark this pcb as non-writable. */
  1187. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  1188. }
  1189. }
  1190. if (err == ERR_OK) {
  1191. conn->write_offset += len;
  1192. if ((conn->write_offset == conn->current_msg->msg.w.len) || dontblock) {
  1193. /* return sent length */
  1194. conn->current_msg->msg.w.len = conn->write_offset;
  1195. /* everything was written */
  1196. write_finished = 1;
  1197. conn->write_offset = 0;
  1198. }
  1199. tcp_output(conn->pcb.tcp);
  1200. } else if ((err == ERR_MEM) && !dontblock) {
  1201. /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
  1202. we do NOT return to the application thread, since ERR_MEM is
  1203. only a temporary error! */
  1204. /* tcp_write returned ERR_MEM, try tcp_output anyway */
  1205. tcp_output(conn->pcb.tcp);
  1206. #if LWIP_TCPIP_CORE_LOCKING
  1207. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1208. #endif
  1209. } else {
  1210. /* On errors != ERR_MEM, we don't try writing any more but return
  1211. the error to the application thread. */
  1212. write_finished = 1;
  1213. conn->current_msg->msg.w.len = 0;
  1214. }
  1215. }
  1216. if (write_finished) {
  1217. /* everything was written: set back connection state
  1218. and back to application task */
  1219. conn->current_msg->err = err;
  1220. conn->current_msg = NULL;
  1221. conn->state = NETCONN_NONE;
  1222. #if LWIP_TCPIP_CORE_LOCKING
  1223. if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
  1224. #endif
  1225. {
  1226. sys_sem_signal(&conn->op_completed);
  1227. }
  1228. }
  1229. #if LWIP_TCPIP_CORE_LOCKING
  1230. else
  1231. return ERR_MEM;
  1232. #endif
  1233. return ERR_OK;
  1234. }
  1235. #endif /* LWIP_TCP */
  1236. /**
  1237. * Send some data on a TCP pcb contained in a netconn
  1238. * Called from netconn_write
  1239. *
  1240. * @param msg the api_msg_msg pointing to the connection
  1241. */
  1242. void
  1243. do_write(struct api_msg_msg *msg)
  1244. {
  1245. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1246. msg->err = msg->conn->last_err;
  1247. } else {
  1248. if (msg->conn->type == NETCONN_TCP) {
  1249. #if LWIP_TCP
  1250. if (msg->conn->state != NETCONN_NONE) {
  1251. /* netconn is connecting, closing or in blocking write */
  1252. msg->err = ERR_INPROGRESS;
  1253. } else if (msg->conn->pcb.tcp != NULL) {
  1254. msg->conn->state = NETCONN_WRITE;
  1255. /* set all the variables used by do_writemore */
  1256. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  1257. msg->conn->write_offset == 0);
  1258. LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
  1259. msg->conn->current_msg = msg;
  1260. msg->conn->write_offset = 0;
  1261. #if LWIP_TCPIP_CORE_LOCKING
  1262. msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
  1263. if (do_writemore(msg->conn) != ERR_OK) {
  1264. LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
  1265. UNLOCK_TCPIP_CORE();
  1266. sys_arch_sem_wait(&msg->conn->op_completed, 0);
  1267. LOCK_TCPIP_CORE();
  1268. LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
  1269. }
  1270. #else /* LWIP_TCPIP_CORE_LOCKING */
  1271. do_writemore(msg->conn);
  1272. #endif /* LWIP_TCPIP_CORE_LOCKING */
  1273. /* for both cases: if do_writemore was called, don't ACK the APIMSG
  1274. since do_writemore ACKs it! */
  1275. return;
  1276. } else {
  1277. msg->err = ERR_CONN;
  1278. }
  1279. #else /* LWIP_TCP */
  1280. msg->err = ERR_VAL;
  1281. #endif /* LWIP_TCP */
  1282. #if (LWIP_UDP || LWIP_RAW)
  1283. } else {
  1284. msg->err = ERR_VAL;
  1285. #endif /* (LWIP_UDP || LWIP_RAW) */
  1286. }
  1287. }
  1288. TCPIP_APIMSG_ACK(msg);
  1289. }
  1290. /**
  1291. * Return a connection's local or remote address
  1292. * Called from netconn_getaddr
  1293. *
  1294. * @param msg the api_msg_msg pointing to the connection
  1295. */
  1296. void
  1297. do_getaddr(struct api_msg_msg *msg)
  1298. {
  1299. if (msg->conn->pcb.ip != NULL) {
  1300. *(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip :
  1301. msg->conn->pcb.ip->remote_ip);
  1302. msg->err = ERR_OK;
  1303. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1304. #if LWIP_RAW
  1305. case NETCONN_RAW:
  1306. if (msg->msg.ad.local) {
  1307. *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
  1308. } else {
  1309. /* return an error as connecting is only a helper for upper layers */
  1310. msg->err = ERR_CONN;
  1311. }
  1312. break;
  1313. #endif /* LWIP_RAW */
  1314. #if LWIP_UDP
  1315. case NETCONN_UDP:
  1316. if (msg->msg.ad.local) {
  1317. *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
  1318. } else {
  1319. if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
  1320. msg->err = ERR_CONN;
  1321. } else {
  1322. *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
  1323. }
  1324. }
  1325. break;
  1326. #endif /* LWIP_UDP */
  1327. #if LWIP_TCP
  1328. case NETCONN_TCP:
  1329. *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
  1330. break;
  1331. #endif /* LWIP_TCP */
  1332. default:
  1333. LWIP_ASSERT("invalid netconn_type", 0);
  1334. break;
  1335. }
  1336. } else {
  1337. msg->err = ERR_CONN;
  1338. }
  1339. TCPIP_APIMSG_ACK(msg);
  1340. }
  1341. /**
  1342. * Close a TCP pcb contained in a netconn
  1343. * Called from netconn_close
  1344. *
  1345. * @param msg the api_msg_msg pointing to the connection
  1346. */
  1347. void
  1348. do_close(struct api_msg_msg *msg)
  1349. {
  1350. #if LWIP_TCP
  1351. /* @todo: abort running write/connect? */
  1352. if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
  1353. /* this only happens for TCP netconns */
  1354. LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
  1355. msg->err = ERR_INPROGRESS;
  1356. } else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
  1357. if ((msg->msg.sd.shut != NETCONN_SHUT_RDWR) && (msg->conn->state == NETCONN_LISTEN)) {
  1358. /* LISTEN doesn't support half shutdown */
  1359. msg->err = ERR_CONN;
  1360. } else {
  1361. if (msg->msg.sd.shut & NETCONN_SHUT_RD) {
  1362. /* Drain and delete mboxes */
  1363. netconn_drain(msg->conn);
  1364. }
  1365. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  1366. msg->conn->write_offset == 0);
  1367. msg->conn->state = NETCONN_CLOSE;
  1368. msg->conn->current_msg = msg;
  1369. do_close_internal(msg->conn);
  1370. /* for tcp netconns, do_close_internal ACKs the message */
  1371. return;
  1372. }
  1373. } else
  1374. #endif /* LWIP_TCP */
  1375. {
  1376. msg->err = ERR_VAL;
  1377. }
  1378. sys_sem_signal(&msg->conn->op_completed);
  1379. }
  1380. #if LWIP_IGMP
  1381. /**
  1382. * Join multicast groups for UDP netconns.
  1383. * Called from netconn_join_leave_group
  1384. *
  1385. * @param msg the api_msg_msg pointing to the connection
  1386. */
  1387. void
  1388. do_join_leave_group(struct api_msg_msg *msg)
  1389. {
  1390. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1391. msg->err = msg->conn->last_err;
  1392. } else {
  1393. if (msg->conn->pcb.tcp != NULL) {
  1394. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  1395. #if LWIP_UDP
  1396. if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
  1397. msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
  1398. } else {
  1399. msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
  1400. }
  1401. #endif /* LWIP_UDP */
  1402. #if (LWIP_TCP || LWIP_RAW)
  1403. } else {
  1404. msg->err = ERR_VAL;
  1405. #endif /* (LWIP_TCP || LWIP_RAW) */
  1406. }
  1407. } else {
  1408. msg->err = ERR_CONN;
  1409. }
  1410. }
  1411. TCPIP_APIMSG_ACK(msg);
  1412. }
  1413. #endif /* LWIP_IGMP */
  1414. #if LWIP_DNS
  1415. /**
  1416. * Callback function that is called when DNS name is resolved
  1417. * (or on timeout). A waiting application thread is waked up by
  1418. * signaling the semaphore.
  1419. */
  1420. static void
  1421. do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  1422. {
  1423. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1424. LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
  1425. LWIP_UNUSED_ARG(name);
  1426. if (ipaddr == NULL) {
  1427. /* timeout or memory error */
  1428. *msg->err = ERR_VAL;
  1429. } else {
  1430. /* address was resolved */
  1431. *msg->err = ERR_OK;
  1432. *msg->addr = *ipaddr;
  1433. }
  1434. /* wake up the application task waiting in netconn_gethostbyname */
  1435. sys_sem_signal(msg->sem);
  1436. }
  1437. /**
  1438. * Execute a DNS query
  1439. * Called from netconn_gethostbyname
  1440. *
  1441. * @param arg the dns_api_msg pointing to the query
  1442. */
  1443. void
  1444. do_gethostbyname(void *arg)
  1445. {
  1446. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1447. *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
  1448. if (*msg->err != ERR_INPROGRESS) {
  1449. /* on error or immediate success, wake up the application
  1450. * task waiting in netconn_gethostbyname */
  1451. sys_sem_signal(msg->sem);
  1452. }
  1453. }
  1454. #endif /* LWIP_DNS */
  1455. #endif /* LWIP_NETCONN */