api_msg.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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/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. newconn->pcb.tcp = NULL;
  406. /* no need to drain since we know the recvmbox is empty. */
  407. sys_mbox_free(&newconn->recvmbox);
  408. sys_mbox_set_invalid(&newconn->recvmbox);
  409. netconn_free(newconn);
  410. return ERR_MEM;
  411. } else {
  412. /* Register event with callback */
  413. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  414. }
  415. return ERR_OK;
  416. }
  417. #endif /* LWIP_TCP */
  418. /**
  419. * Create a new pcb of a specific type.
  420. * Called from do_newconn().
  421. *
  422. * @param msg the api_msg_msg describing the connection type
  423. * @return msg->conn->err, but the return value is currently ignored
  424. */
  425. static void
  426. pcb_new(struct api_msg_msg *msg)
  427. {
  428. LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
  429. /* Allocate a PCB for this connection */
  430. switch(NETCONNTYPE_GROUP(msg->conn->type)) {
  431. #if LWIP_RAW
  432. case NETCONN_RAW:
  433. msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
  434. if(msg->conn->pcb.raw == NULL) {
  435. msg->err = ERR_MEM;
  436. break;
  437. }
  438. raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
  439. break;
  440. #endif /* LWIP_RAW */
  441. #if LWIP_UDP
  442. case NETCONN_UDP:
  443. msg->conn->pcb.udp = udp_new();
  444. if(msg->conn->pcb.udp == NULL) {
  445. msg->err = ERR_MEM;
  446. break;
  447. }
  448. #if LWIP_UDPLITE
  449. if (msg->conn->type==NETCONN_UDPLITE) {
  450. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
  451. }
  452. #endif /* LWIP_UDPLITE */
  453. if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
  454. udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
  455. }
  456. udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
  457. break;
  458. #endif /* LWIP_UDP */
  459. #if LWIP_TCP
  460. case NETCONN_TCP:
  461. msg->conn->pcb.tcp = tcp_new();
  462. if(msg->conn->pcb.tcp == NULL) {
  463. msg->err = ERR_MEM;
  464. break;
  465. }
  466. setup_tcp(msg->conn);
  467. break;
  468. #endif /* LWIP_TCP */
  469. default:
  470. /* Unsupported netconn type, e.g. protocol disabled */
  471. msg->err = ERR_VAL;
  472. break;
  473. }
  474. }
  475. /**
  476. * Create a new pcb of a specific type inside a netconn.
  477. * Called from netconn_new_with_proto_and_callback.
  478. *
  479. * @param msg the api_msg_msg describing the connection type
  480. */
  481. void
  482. do_newconn(struct api_msg_msg *msg)
  483. {
  484. msg->err = ERR_OK;
  485. if(msg->conn->pcb.tcp == NULL) {
  486. pcb_new(msg);
  487. }
  488. /* Else? This "new" connection already has a PCB allocated. */
  489. /* Is this an error condition? Should it be deleted? */
  490. /* We currently just are happy and return. */
  491. TCPIP_APIMSG_ACK(msg);
  492. }
  493. /**
  494. * Create a new netconn (of a specific type) that has a callback function.
  495. * The corresponding pcb is NOT created!
  496. *
  497. * @param t the type of 'connection' to create (@see enum netconn_type)
  498. * @param proto the IP protocol for RAW IP pcbs
  499. * @param callback a function to call on status changes (RX available, TX'ed)
  500. * @return a newly allocated struct netconn or
  501. * NULL on memory error
  502. */
  503. struct netconn*
  504. netconn_alloc(enum netconn_type t, netconn_callback callback)
  505. {
  506. struct netconn *conn;
  507. int size;
  508. conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
  509. if (conn == NULL) {
  510. return NULL;
  511. }
  512. conn->last_err = ERR_OK;
  513. conn->type = t;
  514. conn->pcb.tcp = NULL;
  515. #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
  516. (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
  517. size = DEFAULT_RAW_RECVMBOX_SIZE;
  518. #else
  519. switch(NETCONNTYPE_GROUP(t)) {
  520. #if LWIP_RAW
  521. case NETCONN_RAW:
  522. size = DEFAULT_RAW_RECVMBOX_SIZE;
  523. break;
  524. #endif /* LWIP_RAW */
  525. #if LWIP_UDP
  526. case NETCONN_UDP:
  527. size = DEFAULT_UDP_RECVMBOX_SIZE;
  528. break;
  529. #endif /* LWIP_UDP */
  530. #if LWIP_TCP
  531. case NETCONN_TCP:
  532. size = DEFAULT_TCP_RECVMBOX_SIZE;
  533. break;
  534. #endif /* LWIP_TCP */
  535. default:
  536. LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
  537. break;
  538. }
  539. #endif
  540. if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) {
  541. memp_free(MEMP_NETCONN, conn);
  542. return NULL;
  543. }
  544. if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) {
  545. sys_sem_free(&conn->op_completed);
  546. memp_free(MEMP_NETCONN, conn);
  547. return NULL;
  548. }
  549. #if LWIP_TCP
  550. sys_mbox_set_invalid(&conn->acceptmbox);
  551. #endif
  552. conn->state = NETCONN_NONE;
  553. #if LWIP_SOCKET
  554. /* initialize socket to -1 since 0 is a valid socket */
  555. conn->socket = -1;
  556. #endif /* LWIP_SOCKET */
  557. conn->callback = callback;
  558. #if LWIP_TCP
  559. conn->current_msg = NULL;
  560. conn->write_offset = 0;
  561. #endif /* LWIP_TCP */
  562. #if LWIP_SO_RCVTIMEO
  563. conn->recv_timeout = 0;
  564. #endif /* LWIP_SO_RCVTIMEO */
  565. #if LWIP_SO_RCVBUF
  566. conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
  567. conn->recv_avail = 0;
  568. #endif /* LWIP_SO_RCVBUF */
  569. conn->flags = 0;
  570. return conn;
  571. }
  572. /**
  573. * Delete a netconn and all its resources.
  574. * The pcb is NOT freed (since we might not be in the right thread context do this).
  575. *
  576. * @param conn the netconn to free
  577. */
  578. void
  579. netconn_free(struct netconn *conn)
  580. {
  581. LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
  582. LWIP_ASSERT("recvmbox must be deallocated before calling this function",
  583. !sys_mbox_valid(&conn->recvmbox));
  584. #if LWIP_TCP
  585. LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
  586. !sys_mbox_valid(&conn->acceptmbox));
  587. #endif /* LWIP_TCP */
  588. sys_sem_free(&conn->op_completed);
  589. sys_sem_set_invalid(&conn->op_completed);
  590. memp_free(MEMP_NETCONN, conn);
  591. }
  592. /**
  593. * Delete rcvmbox and acceptmbox of a netconn and free the left-over data in
  594. * these mboxes
  595. *
  596. * @param conn the netconn to free
  597. * @bytes_drained bytes drained from recvmbox
  598. * @accepts_drained pending connections drained from acceptmbox
  599. */
  600. static void
  601. netconn_drain(struct netconn *conn)
  602. {
  603. void *mem;
  604. #if LWIP_TCP
  605. struct pbuf *p;
  606. #endif /* LWIP_TCP */
  607. /* This runs in tcpip_thread, so we don't need to lock against rx packets */
  608. /* Delete and drain the recvmbox. */
  609. if (sys_mbox_valid(&conn->recvmbox)) {
  610. while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
  611. #if LWIP_TCP
  612. if (conn->type == NETCONN_TCP) {
  613. if(mem != NULL) {
  614. p = (struct pbuf*)mem;
  615. /* pcb might be set to NULL already by err_tcp() */
  616. if (conn->pcb.tcp != NULL) {
  617. tcp_recved(conn->pcb.tcp, p->tot_len);
  618. }
  619. pbuf_free(p);
  620. }
  621. } else
  622. #endif /* LWIP_TCP */
  623. {
  624. netbuf_delete((struct netbuf *)mem);
  625. }
  626. }
  627. sys_mbox_free(&conn->recvmbox);
  628. sys_mbox_set_invalid(&conn->recvmbox);
  629. }
  630. /* Delete and drain the acceptmbox. */
  631. #if LWIP_TCP
  632. if (sys_mbox_valid(&conn->acceptmbox)) {
  633. while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
  634. struct netconn *newconn = (struct netconn *)mem;
  635. /* Only tcp pcbs have an acceptmbox, so no need to check conn->type */
  636. /* pcb might be set to NULL already by err_tcp() */
  637. if (conn->pcb.tcp != NULL) {
  638. tcp_accepted(conn->pcb.tcp);
  639. }
  640. /* drain recvmbox */
  641. netconn_drain(newconn);
  642. if (newconn->pcb.tcp != NULL) {
  643. tcp_abort(newconn->pcb.tcp);
  644. newconn->pcb.tcp = NULL;
  645. }
  646. netconn_free(newconn);
  647. }
  648. sys_mbox_free(&conn->acceptmbox);
  649. sys_mbox_set_invalid(&conn->acceptmbox);
  650. }
  651. #endif /* LWIP_TCP */
  652. }
  653. #if LWIP_TCP
  654. /**
  655. * Internal helper function to close a TCP netconn: since this sometimes
  656. * doesn't work at the first attempt, this function is called from multiple
  657. * places.
  658. *
  659. * @param conn the TCP netconn to close
  660. */
  661. static void
  662. do_close_internal(struct netconn *conn)
  663. {
  664. err_t err;
  665. u8_t shut, shut_rx, shut_tx, close;
  666. LWIP_ASSERT("invalid conn", (conn != NULL));
  667. LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
  668. LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
  669. LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
  670. LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
  671. shut = conn->current_msg->msg.sd.shut;
  672. shut_rx = shut & NETCONN_SHUT_RD;
  673. shut_tx = shut & NETCONN_SHUT_WR;
  674. /* shutting down both ends is the same as closing */
  675. close = shut == NETCONN_SHUT_RDWR;
  676. /* Set back some callback pointers */
  677. if (close) {
  678. tcp_arg(conn->pcb.tcp, NULL);
  679. }
  680. if (conn->pcb.tcp->state == LISTEN) {
  681. tcp_accept(conn->pcb.tcp, NULL);
  682. } else {
  683. /* some callbacks have to be reset if tcp_close is not successful */
  684. if (shut_rx) {
  685. tcp_recv(conn->pcb.tcp, NULL);
  686. tcp_accept(conn->pcb.tcp, NULL);
  687. }
  688. if (shut_tx) {
  689. tcp_sent(conn->pcb.tcp, NULL);
  690. }
  691. if (close) {
  692. tcp_poll(conn->pcb.tcp, NULL, 4);
  693. tcp_err(conn->pcb.tcp, NULL);
  694. }
  695. }
  696. /* Try to close the connection */
  697. if (shut == NETCONN_SHUT_RDWR) {
  698. err = tcp_close(conn->pcb.tcp);
  699. } else {
  700. err = tcp_shutdown(conn->pcb.tcp, shut & NETCONN_SHUT_RD, shut & NETCONN_SHUT_WR);
  701. }
  702. if (err == ERR_OK) {
  703. /* Closing succeeded */
  704. conn->current_msg->err = ERR_OK;
  705. conn->current_msg = NULL;
  706. conn->state = NETCONN_NONE;
  707. /* Set back some callback pointers as conn is going away */
  708. conn->pcb.tcp = NULL;
  709. /* Trigger select() in socket layer. Make sure everybody notices activity
  710. on the connection, error first! */
  711. if (close) {
  712. API_EVENT(conn, NETCONN_EVT_ERROR, 0);
  713. }
  714. if (shut_rx) {
  715. API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
  716. }
  717. if (shut_tx) {
  718. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  719. }
  720. /* wake up the application task */
  721. sys_sem_signal(&conn->op_completed);
  722. } else {
  723. /* Closing failed, restore some of the callbacks */
  724. /* Closing of listen pcb will never fail! */
  725. LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
  726. tcp_sent(conn->pcb.tcp, sent_tcp);
  727. tcp_poll(conn->pcb.tcp, poll_tcp, 4);
  728. tcp_err(conn->pcb.tcp, err_tcp);
  729. tcp_arg(conn->pcb.tcp, conn);
  730. /* don't restore recv callback: we don't want to receive any more data */
  731. }
  732. /* If closing didn't succeed, we get called again either
  733. from poll_tcp or from sent_tcp */
  734. }
  735. #endif /* LWIP_TCP */
  736. /**
  737. * Delete the pcb inside a netconn.
  738. * Called from netconn_delete.
  739. *
  740. * @param msg the api_msg_msg pointing to the connection
  741. */
  742. void
  743. do_delconn(struct api_msg_msg *msg)
  744. {
  745. /* @todo TCP: abort running write/connect? */
  746. if ((msg->conn->state != NETCONN_NONE) &&
  747. (msg->conn->state != NETCONN_LISTEN) &&
  748. (msg->conn->state != NETCONN_CONNECT)) {
  749. /* this only happens for TCP netconns */
  750. LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
  751. msg->err = ERR_INPROGRESS;
  752. } else {
  753. LWIP_ASSERT("blocking connect in progress",
  754. (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
  755. /* Drain and delete mboxes */
  756. netconn_drain(msg->conn);
  757. if (msg->conn->pcb.tcp != NULL) {
  758. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  759. #if LWIP_RAW
  760. case NETCONN_RAW:
  761. raw_remove(msg->conn->pcb.raw);
  762. break;
  763. #endif /* LWIP_RAW */
  764. #if LWIP_UDP
  765. case NETCONN_UDP:
  766. msg->conn->pcb.udp->recv_arg = NULL;
  767. udp_remove(msg->conn->pcb.udp);
  768. break;
  769. #endif /* LWIP_UDP */
  770. #if LWIP_TCP
  771. case NETCONN_TCP:
  772. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  773. msg->conn->write_offset == 0);
  774. msg->conn->state = NETCONN_CLOSE;
  775. msg->msg.sd.shut = NETCONN_SHUT_RDWR;
  776. msg->conn->current_msg = msg;
  777. do_close_internal(msg->conn);
  778. /* API_EVENT is called inside do_close_internal, before releasing
  779. the application thread, so we can return at this point! */
  780. return;
  781. #endif /* LWIP_TCP */
  782. default:
  783. break;
  784. }
  785. msg->conn->pcb.tcp = NULL;
  786. }
  787. /* tcp netconns don't come here! */
  788. /* @todo: this lets select make the socket readable and writable,
  789. which is wrong! errfd instead? */
  790. API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
  791. API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
  792. }
  793. if (sys_sem_valid(&msg->conn->op_completed)) {
  794. sys_sem_signal(&msg->conn->op_completed);
  795. }
  796. }
  797. /**
  798. * Bind a pcb contained in a netconn
  799. * Called from netconn_bind.
  800. *
  801. * @param msg the api_msg_msg pointing to the connection and containing
  802. * the IP address and port to bind to
  803. */
  804. void
  805. do_bind(struct api_msg_msg *msg)
  806. {
  807. if (ERR_IS_FATAL(msg->conn->last_err)) {
  808. msg->err = msg->conn->last_err;
  809. } else {
  810. msg->err = ERR_VAL;
  811. if (msg->conn->pcb.tcp != NULL) {
  812. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  813. #if LWIP_RAW
  814. case NETCONN_RAW:
  815. msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  816. break;
  817. #endif /* LWIP_RAW */
  818. #if LWIP_UDP
  819. case NETCONN_UDP:
  820. msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  821. break;
  822. #endif /* LWIP_UDP */
  823. #if LWIP_TCP
  824. case NETCONN_TCP:
  825. msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  826. break;
  827. #endif /* LWIP_TCP */
  828. default:
  829. break;
  830. }
  831. }
  832. }
  833. TCPIP_APIMSG_ACK(msg);
  834. }
  835. #if LWIP_TCP
  836. /**
  837. * TCP callback function if a connection (opened by tcp_connect/do_connect) has
  838. * been established (or reset by the remote host).
  839. *
  840. * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
  841. */
  842. static err_t
  843. do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
  844. {
  845. struct netconn *conn;
  846. int was_blocking;
  847. LWIP_UNUSED_ARG(pcb);
  848. conn = (struct netconn *)arg;
  849. if (conn == NULL) {
  850. return ERR_VAL;
  851. }
  852. LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
  853. LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
  854. (conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
  855. if (conn->current_msg != NULL) {
  856. conn->current_msg->err = err;
  857. }
  858. if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
  859. setup_tcp(conn);
  860. }
  861. was_blocking = !IN_NONBLOCKING_CONNECT(conn);
  862. SET_NONBLOCKING_CONNECT(conn, 0);
  863. conn->current_msg = NULL;
  864. conn->state = NETCONN_NONE;
  865. if (!was_blocking) {
  866. NETCONN_SET_SAFE_ERR(conn, ERR_OK);
  867. }
  868. API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
  869. if (was_blocking) {
  870. sys_sem_signal(&conn->op_completed);
  871. }
  872. return ERR_OK;
  873. }
  874. #endif /* LWIP_TCP */
  875. /**
  876. * Connect a pcb contained inside a netconn
  877. * Called from netconn_connect.
  878. *
  879. * @param msg the api_msg_msg pointing to the connection and containing
  880. * the IP address and port to connect to
  881. */
  882. void
  883. do_connect(struct api_msg_msg *msg)
  884. {
  885. if (msg->conn->pcb.tcp == NULL) {
  886. /* This may happen when calling netconn_connect() a second time */
  887. msg->err = ERR_CLSD;
  888. } else {
  889. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  890. #if LWIP_RAW
  891. case NETCONN_RAW:
  892. msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
  893. break;
  894. #endif /* LWIP_RAW */
  895. #if LWIP_UDP
  896. case NETCONN_UDP:
  897. msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
  898. break;
  899. #endif /* LWIP_UDP */
  900. #if LWIP_TCP
  901. case NETCONN_TCP:
  902. /* Prevent connect while doing any other action. */
  903. if (msg->conn->state != NETCONN_NONE) {
  904. msg->err = ERR_ISCONN;
  905. } else {
  906. setup_tcp(msg->conn);
  907. msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr,
  908. msg->msg.bc.port, do_connected);
  909. if (msg->err == ERR_OK) {
  910. u8_t non_blocking = netconn_is_nonblocking(msg->conn);
  911. msg->conn->state = NETCONN_CONNECT;
  912. SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
  913. if (non_blocking) {
  914. msg->err = ERR_INPROGRESS;
  915. } else {
  916. msg->conn->current_msg = msg;
  917. /* sys_sem_signal() is called from do_connected (or err_tcp()),
  918. * when the connection is established! */
  919. return;
  920. }
  921. }
  922. }
  923. break;
  924. #endif /* LWIP_TCP */
  925. default:
  926. LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0));
  927. break;
  928. }
  929. }
  930. sys_sem_signal(&msg->conn->op_completed);
  931. }
  932. /**
  933. * Connect a pcb contained inside a netconn
  934. * Only used for UDP netconns.
  935. * Called from netconn_disconnect.
  936. *
  937. * @param msg the api_msg_msg pointing to the connection to disconnect
  938. */
  939. void
  940. do_disconnect(struct api_msg_msg *msg)
  941. {
  942. #if LWIP_UDP
  943. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  944. udp_disconnect(msg->conn->pcb.udp);
  945. msg->err = ERR_OK;
  946. } else
  947. #endif /* LWIP_UDP */
  948. {
  949. msg->err = ERR_VAL;
  950. }
  951. TCPIP_APIMSG_ACK(msg);
  952. }
  953. #if LWIP_TCP
  954. /**
  955. * Set a TCP pcb contained in a netconn into listen mode
  956. * Called from netconn_listen.
  957. *
  958. * @param msg the api_msg_msg pointing to the connection
  959. */
  960. void
  961. do_listen(struct api_msg_msg *msg)
  962. {
  963. if (ERR_IS_FATAL(msg->conn->last_err)) {
  964. msg->err = msg->conn->last_err;
  965. } else {
  966. msg->err = ERR_CONN;
  967. if (msg->conn->pcb.tcp != NULL) {
  968. if (msg->conn->type == NETCONN_TCP) {
  969. if (msg->conn->state == NETCONN_NONE) {
  970. #if TCP_LISTEN_BACKLOG
  971. struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
  972. #else /* TCP_LISTEN_BACKLOG */
  973. struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
  974. #endif /* TCP_LISTEN_BACKLOG */
  975. if (lpcb == NULL) {
  976. /* in this case, the old pcb is still allocated */
  977. msg->err = ERR_MEM;
  978. } else {
  979. /* delete the recvmbox and allocate the acceptmbox */
  980. if (sys_mbox_valid(&msg->conn->recvmbox)) {
  981. /** @todo: should we drain the recvmbox here? */
  982. sys_mbox_free(&msg->conn->recvmbox);
  983. sys_mbox_set_invalid(&msg->conn->recvmbox);
  984. }
  985. msg->err = ERR_OK;
  986. if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
  987. msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
  988. }
  989. if (msg->err == ERR_OK) {
  990. msg->conn->state = NETCONN_LISTEN;
  991. msg->conn->pcb.tcp = lpcb;
  992. tcp_arg(msg->conn->pcb.tcp, msg->conn);
  993. tcp_accept(msg->conn->pcb.tcp, accept_function);
  994. } else {
  995. /* since the old pcb is already deallocated, free lpcb now */
  996. tcp_close(lpcb);
  997. msg->conn->pcb.tcp = NULL;
  998. }
  999. }
  1000. }
  1001. }
  1002. }
  1003. }
  1004. TCPIP_APIMSG_ACK(msg);
  1005. }
  1006. #endif /* LWIP_TCP */
  1007. /**
  1008. * Send some data on a RAW or UDP pcb contained in a netconn
  1009. * Called from netconn_send
  1010. *
  1011. * @param msg the api_msg_msg pointing to the connection
  1012. */
  1013. void
  1014. do_send(struct api_msg_msg *msg)
  1015. {
  1016. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1017. msg->err = msg->conn->last_err;
  1018. } else {
  1019. msg->err = ERR_CONN;
  1020. if (msg->conn->pcb.tcp != NULL) {
  1021. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1022. #if LWIP_RAW
  1023. case NETCONN_RAW:
  1024. if (ip_addr_isany(&msg->msg.b->addr)) {
  1025. msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
  1026. } else {
  1027. msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, &msg->msg.b->addr);
  1028. }
  1029. break;
  1030. #endif
  1031. #if LWIP_UDP
  1032. case NETCONN_UDP:
  1033. #if LWIP_CHECKSUM_ON_COPY
  1034. if (ip_addr_isany(&msg->msg.b->addr)) {
  1035. msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
  1036. msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
  1037. } else {
  1038. msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
  1039. &msg->msg.b->addr, msg->msg.b->port,
  1040. msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
  1041. }
  1042. #else /* LWIP_CHECKSUM_ON_COPY */
  1043. if (ip_addr_isany(&msg->msg.b->addr)) {
  1044. msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
  1045. } else {
  1046. msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, &msg->msg.b->addr, msg->msg.b->port);
  1047. }
  1048. #endif /* LWIP_CHECKSUM_ON_COPY */
  1049. break;
  1050. #endif /* LWIP_UDP */
  1051. default:
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. TCPIP_APIMSG_ACK(msg);
  1057. }
  1058. #if LWIP_TCP
  1059. /**
  1060. * Indicate data has been received from a TCP pcb contained in a netconn
  1061. * Called from netconn_recv
  1062. *
  1063. * @param msg the api_msg_msg pointing to the connection
  1064. */
  1065. void
  1066. do_recv(struct api_msg_msg *msg)
  1067. {
  1068. msg->err = ERR_OK;
  1069. if (msg->conn->pcb.tcp != NULL) {
  1070. if (msg->conn->type == NETCONN_TCP) {
  1071. #if TCP_LISTEN_BACKLOG
  1072. if (msg->conn->pcb.tcp->state == LISTEN) {
  1073. tcp_accepted(msg->conn->pcb.tcp);
  1074. } else
  1075. #endif /* TCP_LISTEN_BACKLOG */
  1076. {
  1077. u32_t remaining = msg->msg.r.len;
  1078. do {
  1079. u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining;
  1080. tcp_recved(msg->conn->pcb.tcp, recved);
  1081. remaining -= recved;
  1082. }while(remaining != 0);
  1083. }
  1084. }
  1085. }
  1086. TCPIP_APIMSG_ACK(msg);
  1087. }
  1088. /**
  1089. * See if more data needs to be written from a previous call to netconn_write.
  1090. * Called initially from do_write. If the first call can't send all data
  1091. * (because of low memory or empty send-buffer), this function is called again
  1092. * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
  1093. * blocking application thread (waiting in netconn_write) is released.
  1094. *
  1095. * @param conn netconn (that is currently in state NETCONN_WRITE) to process
  1096. * @return ERR_OK
  1097. * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
  1098. */
  1099. static err_t
  1100. do_writemore(struct netconn *conn)
  1101. {
  1102. err_t err = ERR_OK;
  1103. void *dataptr;
  1104. u16_t len, available;
  1105. u8_t write_finished = 0;
  1106. size_t diff;
  1107. u8_t dontblock = netconn_is_nonblocking(conn) ||
  1108. (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
  1109. u8_t apiflags = conn->current_msg->msg.w.apiflags;
  1110. LWIP_ASSERT("conn != NULL", conn != NULL);
  1111. LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
  1112. LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
  1113. LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
  1114. LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
  1115. conn->write_offset < conn->current_msg->msg.w.len);
  1116. dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
  1117. diff = conn->current_msg->msg.w.len - conn->write_offset;
  1118. if (diff > 0xffffUL) { /* max_u16_t */
  1119. len = 0xffff;
  1120. #if LWIP_TCPIP_CORE_LOCKING
  1121. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1122. #endif
  1123. apiflags |= TCP_WRITE_FLAG_MORE;
  1124. } else {
  1125. len = (u16_t)diff;
  1126. }
  1127. available = tcp_sndbuf(conn->pcb.tcp);
  1128. if (available < len) {
  1129. /* don't try to write more than sendbuf */
  1130. len = available;
  1131. #if LWIP_TCPIP_CORE_LOCKING
  1132. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1133. #endif
  1134. apiflags |= TCP_WRITE_FLAG_MORE;
  1135. }
  1136. if (dontblock && (len < conn->current_msg->msg.w.len)) {
  1137. /* failed to send all data at once -> nonblocking write not possible */
  1138. err = ERR_MEM;
  1139. }
  1140. if (err == ERR_OK) {
  1141. LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
  1142. err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
  1143. }
  1144. if (dontblock && (err == ERR_MEM)) {
  1145. /* nonblocking write failed */
  1146. write_finished = 1;
  1147. err = ERR_WOULDBLOCK;
  1148. /* let poll_tcp check writable space to mark the pcb
  1149. writable again */
  1150. conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
  1151. /* let select mark this pcb as non-writable. */
  1152. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  1153. } else {
  1154. /* if OK or memory error, check available space */
  1155. if (((err == ERR_OK) || (err == ERR_MEM)) &&
  1156. ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
  1157. (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT))) {
  1158. /* The queued byte- or pbuf-count exceeds the configured low-water limit,
  1159. let select mark this pcb as non-writable. */
  1160. API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
  1161. }
  1162. if (err == ERR_OK) {
  1163. conn->write_offset += len;
  1164. if (conn->write_offset == conn->current_msg->msg.w.len) {
  1165. /* everything was written */
  1166. write_finished = 1;
  1167. conn->write_offset = 0;
  1168. }
  1169. tcp_output(conn->pcb.tcp);
  1170. } else if (err == ERR_MEM) {
  1171. /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
  1172. we do NOT return to the application thread, since ERR_MEM is
  1173. only a temporary error! */
  1174. /* tcp_write returned ERR_MEM, try tcp_output anyway */
  1175. tcp_output(conn->pcb.tcp);
  1176. #if LWIP_TCPIP_CORE_LOCKING
  1177. conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
  1178. #endif
  1179. } else {
  1180. /* On errors != ERR_MEM, we don't try writing any more but return
  1181. the error to the application thread. */
  1182. write_finished = 1;
  1183. }
  1184. }
  1185. if (write_finished) {
  1186. /* everything was written: set back connection state
  1187. and back to application task */
  1188. conn->current_msg->err = err;
  1189. conn->current_msg = NULL;
  1190. conn->state = NETCONN_NONE;
  1191. #if LWIP_TCPIP_CORE_LOCKING
  1192. if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
  1193. #endif
  1194. {
  1195. sys_sem_signal(&conn->op_completed);
  1196. }
  1197. }
  1198. #if LWIP_TCPIP_CORE_LOCKING
  1199. else
  1200. return ERR_MEM;
  1201. #endif
  1202. return ERR_OK;
  1203. }
  1204. #endif /* LWIP_TCP */
  1205. /**
  1206. * Send some data on a TCP pcb contained in a netconn
  1207. * Called from netconn_write
  1208. *
  1209. * @param msg the api_msg_msg pointing to the connection
  1210. */
  1211. void
  1212. do_write(struct api_msg_msg *msg)
  1213. {
  1214. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1215. msg->err = msg->conn->last_err;
  1216. } else {
  1217. if (msg->conn->type == NETCONN_TCP) {
  1218. #if LWIP_TCP
  1219. if (msg->conn->state != NETCONN_NONE) {
  1220. /* netconn is connecting, closing or in blocking write */
  1221. msg->err = ERR_INPROGRESS;
  1222. } else if (msg->conn->pcb.tcp != NULL) {
  1223. msg->conn->state = NETCONN_WRITE;
  1224. /* set all the variables used by do_writemore */
  1225. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  1226. msg->conn->write_offset == 0);
  1227. LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
  1228. msg->conn->current_msg = msg;
  1229. msg->conn->write_offset = 0;
  1230. #if LWIP_TCPIP_CORE_LOCKING
  1231. msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
  1232. if (do_writemore(msg->conn) != ERR_OK) {
  1233. LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
  1234. UNLOCK_TCPIP_CORE();
  1235. sys_arch_sem_wait(&msg->conn->op_completed, 0);
  1236. LOCK_TCPIP_CORE();
  1237. LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
  1238. }
  1239. #else /* LWIP_TCPIP_CORE_LOCKING */
  1240. do_writemore(msg->conn);
  1241. #endif /* LWIP_TCPIP_CORE_LOCKING */
  1242. /* for both cases: if do_writemore was called, don't ACK the APIMSG
  1243. since do_writemore ACKs it! */
  1244. return;
  1245. } else {
  1246. msg->err = ERR_CONN;
  1247. }
  1248. #else /* LWIP_TCP */
  1249. msg->err = ERR_VAL;
  1250. #endif /* LWIP_TCP */
  1251. #if (LWIP_UDP || LWIP_RAW)
  1252. } else {
  1253. msg->err = ERR_VAL;
  1254. #endif /* (LWIP_UDP || LWIP_RAW) */
  1255. }
  1256. }
  1257. TCPIP_APIMSG_ACK(msg);
  1258. }
  1259. /**
  1260. * Return a connection's local or remote address
  1261. * Called from netconn_getaddr
  1262. *
  1263. * @param msg the api_msg_msg pointing to the connection
  1264. */
  1265. void
  1266. do_getaddr(struct api_msg_msg *msg)
  1267. {
  1268. if (msg->conn->pcb.ip != NULL) {
  1269. *(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip :
  1270. msg->conn->pcb.ip->remote_ip);
  1271. msg->err = ERR_OK;
  1272. switch (NETCONNTYPE_GROUP(msg->conn->type)) {
  1273. #if LWIP_RAW
  1274. case NETCONN_RAW:
  1275. if (msg->msg.ad.local) {
  1276. *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
  1277. } else {
  1278. /* return an error as connecting is only a helper for upper layers */
  1279. msg->err = ERR_CONN;
  1280. }
  1281. break;
  1282. #endif /* LWIP_RAW */
  1283. #if LWIP_UDP
  1284. case NETCONN_UDP:
  1285. if (msg->msg.ad.local) {
  1286. *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
  1287. } else {
  1288. if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
  1289. msg->err = ERR_CONN;
  1290. } else {
  1291. *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
  1292. }
  1293. }
  1294. break;
  1295. #endif /* LWIP_UDP */
  1296. #if LWIP_TCP
  1297. case NETCONN_TCP:
  1298. *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
  1299. break;
  1300. #endif /* LWIP_TCP */
  1301. default:
  1302. LWIP_ASSERT("invalid netconn_type", 0);
  1303. break;
  1304. }
  1305. } else {
  1306. msg->err = ERR_CONN;
  1307. }
  1308. TCPIP_APIMSG_ACK(msg);
  1309. }
  1310. /**
  1311. * Close a TCP pcb contained in a netconn
  1312. * Called from netconn_close
  1313. *
  1314. * @param msg the api_msg_msg pointing to the connection
  1315. */
  1316. void
  1317. do_close(struct api_msg_msg *msg)
  1318. {
  1319. #if LWIP_TCP
  1320. /* @todo: abort running write/connect? */
  1321. if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
  1322. /* this only happens for TCP netconns */
  1323. LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
  1324. msg->err = ERR_INPROGRESS;
  1325. } else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
  1326. if ((msg->msg.sd.shut != NETCONN_SHUT_RDWR) && (msg->conn->state == NETCONN_LISTEN)) {
  1327. /* LISTEN doesn't support half shutdown */
  1328. msg->err = ERR_CONN;
  1329. } else {
  1330. if (msg->msg.sd.shut & NETCONN_SHUT_RD) {
  1331. /* Drain and delete mboxes */
  1332. netconn_drain(msg->conn);
  1333. }
  1334. LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
  1335. msg->conn->write_offset == 0);
  1336. msg->conn->state = NETCONN_CLOSE;
  1337. msg->conn->current_msg = msg;
  1338. do_close_internal(msg->conn);
  1339. /* for tcp netconns, do_close_internal ACKs the message */
  1340. return;
  1341. }
  1342. } else
  1343. #endif /* LWIP_TCP */
  1344. {
  1345. msg->err = ERR_VAL;
  1346. }
  1347. sys_sem_signal(&msg->conn->op_completed);
  1348. }
  1349. #if LWIP_IGMP
  1350. /**
  1351. * Join multicast groups for UDP netconns.
  1352. * Called from netconn_join_leave_group
  1353. *
  1354. * @param msg the api_msg_msg pointing to the connection
  1355. */
  1356. void
  1357. do_join_leave_group(struct api_msg_msg *msg)
  1358. {
  1359. if (ERR_IS_FATAL(msg->conn->last_err)) {
  1360. msg->err = msg->conn->last_err;
  1361. } else {
  1362. if (msg->conn->pcb.tcp != NULL) {
  1363. if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
  1364. #if LWIP_UDP
  1365. if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
  1366. msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
  1367. } else {
  1368. msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
  1369. }
  1370. #endif /* LWIP_UDP */
  1371. #if (LWIP_TCP || LWIP_RAW)
  1372. } else {
  1373. msg->err = ERR_VAL;
  1374. #endif /* (LWIP_TCP || LWIP_RAW) */
  1375. }
  1376. } else {
  1377. msg->err = ERR_CONN;
  1378. }
  1379. }
  1380. TCPIP_APIMSG_ACK(msg);
  1381. }
  1382. #endif /* LWIP_IGMP */
  1383. #if LWIP_DNS
  1384. /**
  1385. * Callback function that is called when DNS name is resolved
  1386. * (or on timeout). A waiting application thread is waked up by
  1387. * signaling the semaphore.
  1388. */
  1389. static void
  1390. do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  1391. {
  1392. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1393. LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
  1394. LWIP_UNUSED_ARG(name);
  1395. if (ipaddr == NULL) {
  1396. /* timeout or memory error */
  1397. *msg->err = ERR_VAL;
  1398. } else {
  1399. /* address was resolved */
  1400. *msg->err = ERR_OK;
  1401. *msg->addr = *ipaddr;
  1402. }
  1403. /* wake up the application task waiting in netconn_gethostbyname */
  1404. sys_sem_signal(msg->sem);
  1405. }
  1406. /**
  1407. * Execute a DNS query
  1408. * Called from netconn_gethostbyname
  1409. *
  1410. * @param arg the dns_api_msg pointing to the query
  1411. */
  1412. void
  1413. do_gethostbyname(void *arg)
  1414. {
  1415. struct dns_api_msg *msg = (struct dns_api_msg*)arg;
  1416. *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
  1417. if (*msg->err != ERR_INPROGRESS) {
  1418. /* on error or immediate success, wake up the application
  1419. * task waiting in netconn_gethostbyname */
  1420. sys_sem_signal(msg->sem);
  1421. }
  1422. }
  1423. #endif /* LWIP_DNS */
  1424. #endif /* LWIP_NETCONN */