api_lib.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /**
  2. * @file
  3. * Sequential API External 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. /* This is the part of the API that is linked with
  38. the application */
  39. #include "lwip/opt.h"
  40. #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
  41. #include "lwip/api.h"
  42. #include "lwip/memp.h"
  43. #include "lwip/ip.h"
  44. #include "lwip/raw.h"
  45. #include "lwip/udp.h"
  46. #include "lwip/priv/api_msg.h"
  47. #include "lwip/priv/tcp_priv.h"
  48. #include "lwip/priv/tcpip_priv.h"
  49. #include <string.h>
  50. #define API_MSG_VAR_REF(name) API_VAR_REF(name)
  51. #define API_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct api_msg, name)
  52. #define API_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct api_msg, MEMP_API_MSG, name)
  53. #define API_MSG_VAR_ALLOC_DONTFAIL(name) API_VAR_ALLOC_DONTFAIL(struct api_msg, MEMP_API_MSG, name)
  54. #define API_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_API_MSG, name)
  55. static err_t netconn_close_shutdown(struct netconn *conn, u8_t how);
  56. #if !LWIP_TCPIP_CORE_LOCKING
  57. /**
  58. * Call the lower part of a netconn_* function
  59. * This function is then running in the thread context
  60. * of tcpip_thread and has exclusive access to lwIP core code.
  61. *
  62. * @param apimsg a struct containing the function to call and its parameters
  63. * @return ERR_OK if the function was called, another err_t if not
  64. */
  65. static err_t
  66. tcpip_apimsg(struct api_msg *apimsg)
  67. {
  68. #ifdef LWIP_DEBUG
  69. /* catch functions that don't set err */
  70. apimsg->msg.err = ERR_VAL;
  71. #endif
  72. #if LWIP_NETCONN_SEM_PER_THREAD
  73. apimsg->msg.op_completed_sem = LWIP_NETCONN_THREAD_SEM_GET();
  74. LWIP_ASSERT("netconn semaphore not initialized",
  75. sys_sem_valid(apimsg->msg.op_completed_sem));
  76. #endif
  77. if (tcpip_send_api_msg(apimsg->function, &apimsg->msg, LWIP_API_MSG_SEM(&apimsg->msg)) == ERR_OK) {
  78. return apimsg->msg.err;
  79. }
  80. return ERR_VAL;
  81. }
  82. #endif /* !LWIP_TCPIP_CORE_LOCKING */
  83. /**
  84. * Create a new netconn (of a specific type) that has a callback function.
  85. * The corresponding pcb is also created.
  86. *
  87. * @param t the type of 'connection' to create (@see enum netconn_type)
  88. * @param proto the IP protocol for RAW IP pcbs
  89. * @param callback a function to call on status changes (RX available, TX'ed)
  90. * @return a newly allocated struct netconn or
  91. * NULL on memory error
  92. */
  93. struct netconn*
  94. netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback)
  95. {
  96. struct netconn *conn;
  97. API_MSG_VAR_DECLARE(msg);
  98. conn = netconn_alloc(t, callback);
  99. if (conn != NULL) {
  100. err_t err;
  101. API_MSG_VAR_ALLOC_DONTFAIL(msg);
  102. API_MSG_VAR_REF(msg).msg.msg.n.proto = proto;
  103. API_MSG_VAR_REF(msg).msg.conn = conn;
  104. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_newconn, err);
  105. API_MSG_VAR_FREE(msg);
  106. if (err != ERR_OK) {
  107. LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL);
  108. LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox));
  109. #if LWIP_TCP
  110. LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox));
  111. #endif /* LWIP_TCP */
  112. #if !LWIP_NETCONN_SEM_PER_THREAD
  113. LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed));
  114. sys_sem_free(&conn->op_completed);
  115. #endif /* !LWIP_NETCONN_SEM_PER_THREAD */
  116. sys_mbox_free(&conn->recvmbox);
  117. memp_free(MEMP_NETCONN, conn);
  118. return NULL;
  119. }
  120. }
  121. return conn;
  122. }
  123. /**
  124. * Close a netconn 'connection' and free its resources.
  125. * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate
  126. * after this returns.
  127. *
  128. * @param conn the netconn to delete
  129. * @return ERR_OK if the connection was deleted
  130. */
  131. err_t
  132. netconn_delete(struct netconn *conn)
  133. {
  134. err_t err;
  135. API_MSG_VAR_DECLARE(msg);
  136. /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */
  137. if (conn == NULL) {
  138. return ERR_OK;
  139. }
  140. API_MSG_VAR_ALLOC(msg);
  141. API_MSG_VAR_REF(msg).msg.conn = conn;
  142. #if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER
  143. /* get the time we started, which is later compared to
  144. sys_now() + conn->send_timeout */
  145. API_MSG_VAR_REF(msg).msg.msg.sd.time_started = sys_now();
  146. #else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
  147. #if LWIP_TCP
  148. API_MSG_VAR_REF(msg).msg.msg.sd.polls_left =
  149. ((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1;
  150. #endif /* LWIP_TCP */
  151. #endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
  152. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_delconn, err);
  153. API_MSG_VAR_FREE(msg);
  154. if (err != ERR_OK) {
  155. return err;
  156. }
  157. #if !ESP_THREAD_SAFE
  158. LWIP_DEBUGF(ESP_THREAD_SAFE_DEBUG, ("netconn_delete - free conn\n"));
  159. netconn_free(conn);
  160. #endif
  161. return ERR_OK;
  162. }
  163. /**
  164. * Get the local or remote IP address and port of a netconn.
  165. * For RAW netconns, this returns the protocol instead of a port!
  166. *
  167. * @param conn the netconn to query
  168. * @param addr a pointer to which to save the IP address
  169. * @param port a pointer to which to save the port (or protocol for RAW)
  170. * @param local 1 to get the local IP address, 0 to get the remote one
  171. * @return ERR_CONN for invalid connections
  172. * ERR_OK if the information was retrieved
  173. */
  174. err_t
  175. netconn_getaddr(struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local)
  176. {
  177. API_MSG_VAR_DECLARE(msg);
  178. err_t err;
  179. LWIP_ERROR("netconn_getaddr: invalid conn", (conn != NULL), return ERR_ARG;);
  180. LWIP_ERROR("netconn_getaddr: invalid addr", (addr != NULL), return ERR_ARG;);
  181. LWIP_ERROR("netconn_getaddr: invalid port", (port != NULL), return ERR_ARG;);
  182. API_MSG_VAR_ALLOC(msg);
  183. API_MSG_VAR_REF(msg).msg.conn = conn;
  184. API_MSG_VAR_REF(msg).msg.msg.ad.local = local;
  185. #if LWIP_MPU_COMPATIBLE
  186. TCPIP_APIMSG(msg, lwip_netconn_do_getaddr, err);
  187. *addr = msg->msg.msg.ad.ipaddr;
  188. *port = msg->msg.msg.ad.port;
  189. #else /* LWIP_MPU_COMPATIBLE */
  190. msg.msg.msg.ad.ipaddr = addr;
  191. msg.msg.msg.ad.port = port;
  192. TCPIP_APIMSG(&msg, lwip_netconn_do_getaddr, err);
  193. #endif /* LWIP_MPU_COMPATIBLE */
  194. API_MSG_VAR_FREE(msg);
  195. return err;
  196. }
  197. /**
  198. * Bind a netconn to a specific local IP address and port.
  199. * Binding one netconn twice might not always be checked correctly!
  200. *
  201. * @param conn the netconn to bind
  202. * @param addr the local IP address to bind the netconn to (use IP_ADDR_ANY
  203. * to bind to all addresses)
  204. * @param port the local port to bind the netconn to (not used for RAW)
  205. * @return ERR_OK if bound, any other err_t on failure
  206. */
  207. err_t
  208. netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port)
  209. {
  210. API_MSG_VAR_DECLARE(msg);
  211. err_t err;
  212. LWIP_ERROR("netconn_bind: invalid conn", (conn != NULL), return ERR_ARG;);
  213. /* Don't propagate NULL pointer (IP_ADDR_ANY alias) to subsequent functions */
  214. if (addr == NULL) {
  215. addr = IP_ADDR_ANY;
  216. }
  217. API_MSG_VAR_ALLOC(msg);
  218. API_MSG_VAR_REF(msg).msg.conn = conn;
  219. API_MSG_VAR_REF(msg).msg.msg.bc.ipaddr = API_MSG_VAR_REF(addr);
  220. API_MSG_VAR_REF(msg).msg.msg.bc.port = port;
  221. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_bind, err);
  222. API_MSG_VAR_FREE(msg);
  223. return err;
  224. }
  225. /**
  226. * Connect a netconn to a specific remote IP address and port.
  227. *
  228. * @param conn the netconn to connect
  229. * @param addr the remote IP address to connect to
  230. * @param port the remote port to connect to (no used for RAW)
  231. * @return ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise
  232. */
  233. err_t
  234. netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port)
  235. {
  236. API_MSG_VAR_DECLARE(msg);
  237. err_t err;
  238. LWIP_ERROR("netconn_connect: invalid conn", (conn != NULL), return ERR_ARG;);
  239. /* Don't propagate NULL pointer (IP_ADDR_ANY alias) to subsequent functions */
  240. if (addr == NULL) {
  241. addr = IP_ADDR_ANY;
  242. }
  243. API_MSG_VAR_ALLOC(msg);
  244. API_MSG_VAR_REF(msg).msg.conn = conn;
  245. API_MSG_VAR_REF(msg).msg.msg.bc.ipaddr = API_MSG_VAR_REF(addr);
  246. API_MSG_VAR_REF(msg).msg.msg.bc.port = port;
  247. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_connect, err);
  248. API_MSG_VAR_FREE(msg);
  249. return err;
  250. }
  251. /**
  252. * Disconnect a netconn from its current peer (only valid for UDP netconns).
  253. *
  254. * @param conn the netconn to disconnect
  255. * @return TODO: return value is not set here...
  256. */
  257. err_t
  258. netconn_disconnect(struct netconn *conn)
  259. {
  260. API_MSG_VAR_DECLARE(msg);
  261. err_t err;
  262. LWIP_ERROR("netconn_disconnect: invalid conn", (conn != NULL), return ERR_ARG;);
  263. API_MSG_VAR_ALLOC(msg);
  264. API_MSG_VAR_REF(msg).msg.conn = conn;
  265. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_disconnect, err);
  266. API_MSG_VAR_FREE(msg);
  267. return err;
  268. }
  269. /**
  270. * Set a TCP netconn into listen mode
  271. *
  272. * @param conn the tcp netconn to set to listen mode
  273. * @param backlog the listen backlog, only used if TCP_LISTEN_BACKLOG==1
  274. * @return ERR_OK if the netconn was set to listen (UDP and RAW netconns
  275. * don't return any error (yet?))
  276. */
  277. err_t
  278. netconn_listen_with_backlog(struct netconn *conn, u8_t backlog)
  279. {
  280. #if LWIP_TCP
  281. API_MSG_VAR_DECLARE(msg);
  282. err_t err;
  283. /* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */
  284. LWIP_UNUSED_ARG(backlog);
  285. LWIP_ERROR("netconn_listen: invalid conn", (conn != NULL), return ERR_ARG;);
  286. API_MSG_VAR_ALLOC(msg);
  287. API_MSG_VAR_REF(msg).msg.conn = conn;
  288. #if TCP_LISTEN_BACKLOG
  289. API_MSG_VAR_REF(msg).msg.msg.lb.backlog = backlog;
  290. #endif /* TCP_LISTEN_BACKLOG */
  291. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_listen, err);
  292. API_MSG_VAR_FREE(msg);
  293. return err;
  294. #else /* LWIP_TCP */
  295. LWIP_UNUSED_ARG(conn);
  296. LWIP_UNUSED_ARG(backlog);
  297. return ERR_ARG;
  298. #endif /* LWIP_TCP */
  299. }
  300. /**
  301. * Accept a new connection on a TCP listening netconn.
  302. *
  303. * @param conn the TCP listen netconn
  304. * @param new_conn pointer where the new connection is stored
  305. * @return ERR_OK if a new connection has been received or an error
  306. * code otherwise
  307. */
  308. err_t
  309. netconn_accept(struct netconn *conn, struct netconn **new_conn)
  310. {
  311. #if LWIP_TCP
  312. struct netconn *newconn;
  313. err_t err;
  314. #if TCP_LISTEN_BACKLOG
  315. API_MSG_VAR_DECLARE(msg);
  316. #endif /* TCP_LISTEN_BACKLOG */
  317. LWIP_ERROR("netconn_accept: invalid pointer", (new_conn != NULL), return ERR_ARG;);
  318. *new_conn = NULL;
  319. LWIP_ERROR("netconn_accept: invalid conn", (conn != NULL), return ERR_ARG;);
  320. LWIP_ERROR("netconn_accept: invalid acceptmbox", sys_mbox_valid(&conn->acceptmbox), return ERR_ARG;);
  321. err = conn->last_err;
  322. if (ERR_IS_FATAL(err)) {
  323. /* don't recv on fatal errors: this might block the application task
  324. waiting on acceptmbox forever! */
  325. return err;
  326. }
  327. #if LWIP_SO_RCVTIMEO
  328. if (sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
  329. return ERR_TIMEOUT;
  330. }
  331. #else
  332. sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, 0);
  333. #endif /* LWIP_SO_RCVTIMEO*/
  334. /* Register event with callback */
  335. API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
  336. if (newconn == NULL) {
  337. /* connection has been aborted */
  338. /* in this special case, we set the netconn error from application thread, as
  339. on a ready-to-accept listening netconn, there should not be anything running
  340. in tcpip_thread */
  341. NETCONN_SET_SAFE_ERR(conn, ERR_ABRT);
  342. return ERR_ABRT;
  343. }
  344. #if TCP_LISTEN_BACKLOG
  345. /* Let the stack know that we have accepted the connection. */
  346. API_MSG_VAR_ALLOC_DONTFAIL(msg);
  347. API_MSG_VAR_REF(msg).msg.conn = conn;
  348. /* don't care for the return value of lwip_netconn_do_recv */
  349. TCPIP_APIMSG_NOERR(&API_MSG_VAR_REF(msg), lwip_netconn_do_recv);
  350. API_MSG_VAR_FREE(msg);
  351. #endif /* TCP_LISTEN_BACKLOG */
  352. *new_conn = newconn;
  353. /* don't set conn->last_err: it's only ERR_OK, anyway */
  354. return ERR_OK;
  355. #else /* LWIP_TCP */
  356. LWIP_UNUSED_ARG(conn);
  357. LWIP_UNUSED_ARG(new_conn);
  358. return ERR_ARG;
  359. #endif /* LWIP_TCP */
  360. }
  361. /**
  362. * Receive data: actual implementation that doesn't care whether pbuf or netbuf
  363. * is received
  364. *
  365. * @param conn the netconn from which to receive data
  366. * @param new_buf pointer where a new pbuf/netbuf is stored when received data
  367. * @return ERR_OK if data has been received, an error code otherwise (timeout,
  368. * memory error or another error)
  369. */
  370. static err_t
  371. netconn_recv_data(struct netconn *conn, void **new_buf)
  372. {
  373. void *buf = NULL;
  374. u16_t len;
  375. err_t err;
  376. #if LWIP_TCP
  377. API_MSG_VAR_DECLARE(msg);
  378. #endif /* LWIP_TCP */
  379. LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
  380. *new_buf = NULL;
  381. LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
  382. #if LWIP_TCP
  383. #if (LWIP_UDP || LWIP_RAW)
  384. if (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP)
  385. #endif /* (LWIP_UDP || LWIP_RAW) */
  386. {
  387. if (!sys_mbox_valid(&conn->recvmbox)) {
  388. /* This happens when calling this function after receiving FIN */
  389. return sys_mbox_valid(&conn->acceptmbox) ? ERR_CONN : ERR_CLSD;
  390. }
  391. }
  392. #endif /* LWIP_TCP */
  393. LWIP_ERROR("netconn_recv: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
  394. err = conn->last_err;
  395. if (ERR_IS_FATAL(err)) {
  396. /* don't recv on fatal errors: this might block the application task
  397. waiting on recvmbox forever! */
  398. /* @todo: this does not allow us to fetch data that has been put into recvmbox
  399. before the fatal error occurred - is that a problem? */
  400. return err;
  401. }
  402. #if LWIP_SO_RCVTIMEO
  403. if (sys_arch_mbox_fetch(&conn->recvmbox, &buf, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
  404. return ERR_TIMEOUT;
  405. }
  406. #else
  407. sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
  408. #endif /* LWIP_SO_RCVTIMEO*/
  409. #if LWIP_TCP
  410. #if (LWIP_UDP || LWIP_RAW)
  411. if (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP)
  412. #endif /* (LWIP_UDP || LWIP_RAW) */
  413. {
  414. if (!netconn_get_noautorecved(conn) || (buf == NULL)) {
  415. /* Let the stack know that we have taken the data. */
  416. /* TODO: Speedup: Don't block and wait for the answer here
  417. (to prevent multiple thread-switches). */
  418. API_MSG_VAR_ALLOC_DONTFAIL(msg);
  419. API_MSG_VAR_REF(msg).msg.conn = conn;
  420. if (buf != NULL) {
  421. API_MSG_VAR_REF(msg).msg.msg.r.len = ((struct pbuf *)buf)->tot_len;
  422. } else {
  423. API_MSG_VAR_REF(msg).msg.msg.r.len = 1;
  424. }
  425. /* don't care for the return value of lwip_netconn_do_recv */
  426. TCPIP_APIMSG_NOERR(&API_MSG_VAR_REF(msg), lwip_netconn_do_recv);
  427. API_MSG_VAR_FREE(msg);
  428. }
  429. /* If we are closed, we indicate that we no longer wish to use the socket */
  430. if (buf == NULL) {
  431. API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
  432. /* RX side is closed, so deallocate the recvmbox */
  433. netconn_close_shutdown(conn, NETCONN_SHUT_RD);
  434. /* Don' store ERR_CLSD as conn->err since we are only half-closed */
  435. return ERR_CLSD;
  436. }
  437. len = ((struct pbuf *)buf)->tot_len;
  438. }
  439. #endif /* LWIP_TCP */
  440. #if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
  441. else
  442. #endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
  443. #if (LWIP_UDP || LWIP_RAW)
  444. {
  445. #if ESP_THREAD_SAFE
  446. if (buf == NULL){
  447. API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
  448. return ERR_CLSD;
  449. }
  450. #endif
  451. LWIP_ASSERT("buf != NULL", buf != NULL);
  452. len = netbuf_len((struct netbuf *)buf);
  453. }
  454. #endif /* (LWIP_UDP || LWIP_RAW) */
  455. #if ESP_PERF
  456. if (len > DBG_PERF_FILTER_LEN) {
  457. DBG_PERF_PATH_SET(DBG_PERF_DIR_RX, DBG_PERF_POINT_SOC_IN);
  458. }
  459. #endif
  460. #if LWIP_SO_RCVBUF
  461. SYS_ARCH_DEC(conn->recv_avail, len);
  462. #endif /* LWIP_SO_RCVBUF */
  463. /* Register event with callback */
  464. API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
  465. LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len));
  466. *new_buf = buf;
  467. /* don't set conn->last_err: it's only ERR_OK, anyway */
  468. return ERR_OK;
  469. }
  470. /**
  471. * Receive data (in form of a pbuf) from a TCP netconn
  472. *
  473. * @param conn the netconn from which to receive data
  474. * @param new_buf pointer where a new pbuf is stored when received data
  475. * @return ERR_OK if data has been received, an error code otherwise (timeout,
  476. * memory error or another error)
  477. * ERR_ARG if conn is not a TCP netconn
  478. */
  479. err_t
  480. netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf)
  481. {
  482. LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL) &&
  483. NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;);
  484. return netconn_recv_data(conn, (void **)new_buf);
  485. }
  486. /**
  487. * Receive data (in form of a netbuf containing a packet buffer) from a netconn
  488. *
  489. * @param conn the netconn from which to receive data
  490. * @param new_buf pointer where a new netbuf is stored when received data
  491. * @return ERR_OK if data has been received, an error code otherwise (timeout,
  492. * memory error or another error)
  493. */
  494. err_t
  495. netconn_recv(struct netconn *conn, struct netbuf **new_buf)
  496. {
  497. #if LWIP_TCP
  498. struct netbuf *buf = NULL;
  499. err_t err;
  500. #endif /* LWIP_TCP */
  501. LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
  502. *new_buf = NULL;
  503. LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
  504. #if LWIP_TCP
  505. #if (LWIP_UDP || LWIP_RAW)
  506. if (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP)
  507. #endif /* (LWIP_UDP || LWIP_RAW) */
  508. {
  509. struct pbuf *p = NULL;
  510. /* This is not a listening netconn, since recvmbox is set */
  511. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  512. if (buf == NULL) {
  513. return ERR_MEM;
  514. }
  515. err = netconn_recv_data(conn, (void **)&p);
  516. if (err != ERR_OK) {
  517. memp_free(MEMP_NETBUF, buf);
  518. return err;
  519. }
  520. LWIP_ASSERT("p != NULL", p != NULL);
  521. buf->p = p;
  522. buf->ptr = p;
  523. buf->port = 0;
  524. ip_addr_set_zero(&buf->addr);
  525. *new_buf = buf;
  526. /* don't set conn->last_err: it's only ERR_OK, anyway */
  527. return ERR_OK;
  528. }
  529. #endif /* LWIP_TCP */
  530. #if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
  531. else
  532. #endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
  533. {
  534. #if (LWIP_UDP || LWIP_RAW)
  535. return netconn_recv_data(conn, (void **)new_buf);
  536. #endif /* (LWIP_UDP || LWIP_RAW) */
  537. }
  538. }
  539. /**
  540. * TCP: update the receive window: by calling this, the application
  541. * tells the stack that it has processed data and is able to accept
  542. * new data.
  543. * ATTENTION: use with care, this is mainly used for sockets!
  544. * Can only be used when calling netconn_set_noautorecved(conn, 1) before.
  545. *
  546. * @param conn the netconn for which to update the receive window
  547. * @param length amount of data processed (ATTENTION: this must be accurate!)
  548. */
  549. void
  550. netconn_recved(struct netconn *conn, u32_t length)
  551. {
  552. #if LWIP_TCP
  553. if ((conn != NULL) && (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP) &&
  554. (netconn_get_noautorecved(conn))) {
  555. API_MSG_VAR_DECLARE(msg);
  556. /* Let the stack know that we have taken the data. */
  557. /* TODO: Speedup: Don't block and wait for the answer here
  558. (to prevent multiple thread-switches). */
  559. API_MSG_VAR_ALLOC_DONTFAIL(msg);
  560. API_MSG_VAR_REF(msg).msg.conn = conn;
  561. API_MSG_VAR_REF(msg).msg.msg.r.len = length;
  562. /* don't care for the return value of lwip_netconn_do_recv */
  563. TCPIP_APIMSG_NOERR(&API_MSG_VAR_REF(msg), lwip_netconn_do_recv);
  564. API_MSG_VAR_FREE(msg);
  565. }
  566. #else /* LWIP_TCP */
  567. LWIP_UNUSED_ARG(conn);
  568. LWIP_UNUSED_ARG(length);
  569. #endif /* LWIP_TCP */
  570. }
  571. /**
  572. * Send data (in form of a netbuf) to a specific remote IP address and port.
  573. * Only to be used for UDP and RAW netconns (not TCP).
  574. *
  575. * @param conn the netconn over which to send data
  576. * @param buf a netbuf containing the data to send
  577. * @param addr the remote IP address to which to send the data
  578. * @param port the remote port to which to send the data
  579. * @return ERR_OK if data was sent, any other err_t on error
  580. */
  581. err_t
  582. netconn_sendto(struct netconn *conn, struct netbuf *buf, const ip_addr_t *addr, u16_t port)
  583. {
  584. if (buf != NULL) {
  585. ip_addr_set(&buf->addr, addr);
  586. buf->port = port;
  587. return netconn_send(conn, buf);
  588. }
  589. return ERR_VAL;
  590. }
  591. /**
  592. * Send data over a UDP or RAW netconn (that is already connected).
  593. *
  594. * @param conn the UDP or RAW netconn over which to send data
  595. * @param buf a netbuf containing the data to send
  596. * @return ERR_OK if data was sent, any other err_t on error
  597. */
  598. err_t
  599. netconn_send(struct netconn *conn, struct netbuf *buf)
  600. {
  601. API_MSG_VAR_DECLARE(msg);
  602. err_t err;
  603. LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;);
  604. LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len));
  605. API_MSG_VAR_ALLOC(msg);
  606. API_MSG_VAR_REF(msg).msg.conn = conn;
  607. API_MSG_VAR_REF(msg).msg.msg.b = buf;
  608. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_send, err);
  609. API_MSG_VAR_FREE(msg);
  610. return err;
  611. }
  612. /**
  613. * Send data over a TCP netconn.
  614. *
  615. * @param conn the TCP netconn over which to send data
  616. * @param dataptr pointer to the application buffer that contains the data to send
  617. * @param size size of the application data to send
  618. * @param apiflags combination of following flags :
  619. * - NETCONN_COPY: data will be copied into memory belonging to the stack
  620. * - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
  621. * - NETCONN_DONTBLOCK: only write the data if all data can be written at once
  622. * @param bytes_written pointer to a location that receives the number of written bytes
  623. * @return ERR_OK if data was sent, any other err_t on error
  624. */
  625. err_t
  626. netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
  627. u8_t apiflags, size_t *bytes_written)
  628. {
  629. API_MSG_VAR_DECLARE(msg);
  630. err_t err;
  631. u8_t dontblock;
  632. LWIP_ERROR("netconn_write: invalid conn", (conn != NULL), return ERR_ARG;);
  633. LWIP_ERROR("netconn_write: invalid conn->type", (NETCONNTYPE_GROUP(conn->type)== NETCONN_TCP), return ERR_VAL;);
  634. if (size == 0) {
  635. return ERR_OK;
  636. }
  637. dontblock = netconn_is_nonblocking(conn) || (apiflags & NETCONN_DONTBLOCK);
  638. if (dontblock && !bytes_written) {
  639. /* This implies netconn_write() cannot be used for non-blocking send, since
  640. it has no way to return the number of bytes written. */
  641. return ERR_VAL;
  642. }
  643. API_MSG_VAR_ALLOC(msg);
  644. /* non-blocking write sends as much */
  645. API_MSG_VAR_REF(msg).msg.conn = conn;
  646. API_MSG_VAR_REF(msg).msg.msg.w.dataptr = dataptr;
  647. API_MSG_VAR_REF(msg).msg.msg.w.apiflags = apiflags;
  648. API_MSG_VAR_REF(msg).msg.msg.w.len = size;
  649. #if LWIP_SO_SNDTIMEO
  650. if (conn->send_timeout != 0) {
  651. /* get the time we started, which is later compared to
  652. sys_now() + conn->send_timeout */
  653. API_MSG_VAR_REF(msg).msg.msg.w.time_started = sys_now();
  654. } else {
  655. API_MSG_VAR_REF(msg).msg.msg.w.time_started = 0;
  656. }
  657. #endif /* LWIP_SO_SNDTIMEO */
  658. /* For locking the core: this _can_ be delayed on low memory/low send buffer,
  659. but if it is, this is done inside api_msg.c:do_write(), so we can use the
  660. non-blocking version here. */
  661. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_write, err);
  662. if ((err == ERR_OK) && (bytes_written != NULL)) {
  663. if (dontblock
  664. #if LWIP_SO_SNDTIMEO
  665. || (conn->send_timeout != 0)
  666. #endif /* LWIP_SO_SNDTIMEO */
  667. ) {
  668. /* nonblocking write: maybe the data has been sent partly */
  669. *bytes_written = API_MSG_VAR_REF(msg).msg.msg.w.len;
  670. } else {
  671. /* blocking call succeeded: all data has been sent if it */
  672. *bytes_written = size;
  673. }
  674. }
  675. API_MSG_VAR_FREE(msg);
  676. return err;
  677. }
  678. /**
  679. * Close or shutdown a TCP netconn (doesn't delete it).
  680. *
  681. * @param conn the TCP netconn to close or shutdown
  682. * @param how fully close or only shutdown one side?
  683. * @return ERR_OK if the netconn was closed, any other err_t on error
  684. */
  685. static err_t
  686. netconn_close_shutdown(struct netconn *conn, u8_t how)
  687. {
  688. API_MSG_VAR_DECLARE(msg);
  689. err_t err;
  690. LWIP_UNUSED_ARG(how);
  691. LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;);
  692. API_MSG_VAR_ALLOC(msg);
  693. API_MSG_VAR_REF(msg).msg.conn = conn;
  694. #if LWIP_TCP
  695. /* shutting down both ends is the same as closing */
  696. API_MSG_VAR_REF(msg).msg.msg.sd.shut = how;
  697. #if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER
  698. /* get the time we started, which is later compared to
  699. sys_now() + conn->send_timeout */
  700. API_MSG_VAR_REF(msg).msg.msg.sd.time_started = sys_now();
  701. #else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
  702. API_MSG_VAR_REF(msg).msg.msg.sd.polls_left =
  703. ((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1;
  704. #endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
  705. #endif /* LWIP_TCP */
  706. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_close, err);
  707. API_MSG_VAR_FREE(msg);
  708. return err;
  709. }
  710. /**
  711. * Close a TCP netconn (doesn't delete it).
  712. *
  713. * @param conn the TCP netconn to close
  714. * @return ERR_OK if the netconn was closed, any other err_t on error
  715. */
  716. err_t
  717. netconn_close(struct netconn *conn)
  718. {
  719. /* shutting down both ends is the same as closing */
  720. return netconn_close_shutdown(conn, NETCONN_SHUT_RDWR);
  721. }
  722. /**
  723. * Shut down one or both sides of a TCP netconn (doesn't delete it).
  724. *
  725. * @param conn the TCP netconn to shut down
  726. * @return ERR_OK if the netconn was closed, any other err_t on error
  727. */
  728. err_t
  729. netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx)
  730. {
  731. return netconn_close_shutdown(conn, (shut_rx ? NETCONN_SHUT_RD : 0) | (shut_tx ? NETCONN_SHUT_WR : 0));
  732. }
  733. #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
  734. /**
  735. * Join multicast groups for UDP netconns.
  736. *
  737. * @param conn the UDP netconn for which to change multicast addresses
  738. * @param multiaddr IP address of the multicast group to join or leave
  739. * @param netif_addr the IP address of the network interface on which to send
  740. * the igmp message
  741. * @param join_or_leave flag whether to send a join- or leave-message
  742. * @return ERR_OK if the action was taken, any err_t on error
  743. */
  744. err_t
  745. netconn_join_leave_group(struct netconn *conn,
  746. const ip_addr_t *multiaddr,
  747. const ip_addr_t *netif_addr,
  748. enum netconn_igmp join_or_leave)
  749. {
  750. API_MSG_VAR_DECLARE(msg);
  751. err_t err;
  752. LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;);
  753. API_MSG_VAR_ALLOC(msg);
  754. /* Don't propagate NULL pointer (IP_ADDR_ANY alias) to subsequent functions */
  755. if (multiaddr == NULL) {
  756. multiaddr = IP_ADDR_ANY;
  757. }
  758. if (netif_addr == NULL) {
  759. netif_addr = IP_ADDR_ANY;
  760. }
  761. API_MSG_VAR_REF(msg).msg.conn = conn;
  762. API_MSG_VAR_REF(msg).msg.msg.jl.multiaddr = API_MSG_VAR_REF(multiaddr);
  763. API_MSG_VAR_REF(msg).msg.msg.jl.netif_addr = API_MSG_VAR_REF(netif_addr);
  764. API_MSG_VAR_REF(msg).msg.msg.jl.join_or_leave = join_or_leave;
  765. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_join_leave_group, err);
  766. API_MSG_VAR_FREE(msg);
  767. return err;
  768. }
  769. #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
  770. #if LWIP_DNS
  771. /**
  772. * Execute a DNS query, only one IP address is returned
  773. *
  774. * @param name a string representation of the DNS host name to query
  775. * @param addr a preallocated ip_addr_t where to store the resolved IP address
  776. * @return ERR_OK: resolving succeeded
  777. * ERR_MEM: memory error, try again later
  778. * ERR_ARG: dns client not initialized or invalid hostname
  779. * ERR_VAL: dns server response was invalid
  780. */
  781. #if LWIP_IPV4 && LWIP_IPV6
  782. err_t
  783. netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype)
  784. #else
  785. err_t
  786. netconn_gethostbyname(const char *name, ip_addr_t *addr)
  787. #endif
  788. {
  789. API_VAR_DECLARE(struct dns_api_msg, msg);
  790. err_t localerr;
  791. #if !LWIP_MPU_COMPATIBLE
  792. sys_sem_t sem;
  793. #endif /* LWIP_MPU_COMPATIBLE */
  794. err_t err = ERR_OK;
  795. LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
  796. LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
  797. #if LWIP_MPU_COMPATIBLE
  798. if (strlen(name) >= DNS_MAX_NAME_LENGTH) {
  799. return ERR_ARG;
  800. }
  801. #endif
  802. API_VAR_ALLOC(struct dns_api_msg, MEMP_DNS_API_MSG, msg);
  803. #if LWIP_MPU_COMPATIBLE
  804. strncpy(API_VAR_REF(msg).name, name, DNS_MAX_NAME_LENGTH-1);
  805. API_VAR_REF(msg).name[DNS_MAX_NAME_LENGTH-1] = 0;
  806. #else /* LWIP_MPU_COMPATIBLE */
  807. msg.err = &err;
  808. msg.sem = &sem;
  809. API_VAR_REF(msg).addr = API_VAR_REF(addr);
  810. API_VAR_REF(msg).name = name;
  811. #endif /* LWIP_MPU_COMPATIBLE */
  812. #if LWIP_IPV4 && LWIP_IPV6
  813. API_VAR_REF(msg).dns_addrtype = dns_addrtype;
  814. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  815. #if LWIP_NETCONN_SEM_PER_THREAD
  816. API_VAR_REF(msg).sem = LWIP_NETCONN_THREAD_SEM_GET();
  817. #else /* LWIP_NETCONN_SEM_PER_THREAD*/
  818. err = sys_sem_new(API_EXPR_REF(API_VAR_REF(msg).sem), 0);
  819. if (err != ERR_OK) {
  820. API_VAR_FREE(MEMP_DNS_API_MSG, msg);
  821. return err;
  822. }
  823. #endif /* LWIP_NETCONN_SEM_PER_THREAD */
  824. localerr = tcpip_callback(lwip_netconn_do_gethostbyname, &API_VAR_REF(msg));
  825. if (localerr != ERR_OK) {
  826. #if !LWIP_NETCONN_SEM_PER_THREAD
  827. sys_sem_free(API_EXPR_REF(API_VAR_REF(msg).sem));
  828. #endif /* !LWIP_NETCONN_SEM_PER_THREAD */
  829. API_VAR_FREE(MEMP_DNS_API_MSG, msg);
  830. return localerr;
  831. }
  832. sys_sem_wait(API_EXPR_REF_SEM(API_VAR_REF(msg).sem));
  833. #if !LWIP_NETCONN_SEM_PER_THREAD
  834. sys_sem_free(API_EXPR_REF(API_VAR_REF(msg).sem));
  835. #endif /* !LWIP_NETCONN_SEM_PER_THREAD */
  836. #if LWIP_MPU_COMPATIBLE
  837. *addr = msg->addr;
  838. err = msg->err;
  839. #endif /* LWIP_MPU_COMPATIBLE */
  840. API_VAR_FREE(MEMP_DNS_API_MSG, msg);
  841. return err;
  842. }
  843. #endif /* LWIP_DNS*/
  844. #if LWIP_NETCONN_SEM_PER_THREAD
  845. void
  846. netconn_thread_init(void)
  847. {
  848. sys_sem_t *sem = LWIP_NETCONN_THREAD_SEM_GET();
  849. if ((sem == NULL) || !sys_sem_valid(sem)) {
  850. /* call alloc only once */
  851. LWIP_NETCONN_THREAD_SEM_ALLOC();
  852. LWIP_ASSERT("LWIP_NETCONN_THREAD_SEM_ALLOC() failed", sys_sem_valid(LWIP_NETCONN_THREAD_SEM_GET()));
  853. }
  854. }
  855. void
  856. netconn_thread_cleanup(void)
  857. {
  858. sys_sem_t *sem = LWIP_NETCONN_THREAD_SEM_GET();
  859. if ((sem != NULL) && sys_sem_valid(sem)) {
  860. /* call free only once */
  861. LWIP_NETCONN_THREAD_SEM_FREE();
  862. }
  863. }
  864. #endif /* LWIP_NETCONN_SEM_PER_THREAD */
  865. #endif /* LWIP_NETCONN */