api_lib.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 LWIP_SO_RCVBUF
  456. SYS_ARCH_DEC(conn->recv_avail, len);
  457. #endif /* LWIP_SO_RCVBUF */
  458. /* Register event with callback */
  459. API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
  460. LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len));
  461. *new_buf = buf;
  462. /* don't set conn->last_err: it's only ERR_OK, anyway */
  463. return ERR_OK;
  464. }
  465. /**
  466. * Receive data (in form of a pbuf) from a TCP netconn
  467. *
  468. * @param conn the netconn from which to receive data
  469. * @param new_buf pointer where a new pbuf is stored when received data
  470. * @return ERR_OK if data has been received, an error code otherwise (timeout,
  471. * memory error or another error)
  472. * ERR_ARG if conn is not a TCP netconn
  473. */
  474. err_t
  475. netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf)
  476. {
  477. LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL) &&
  478. NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;);
  479. return netconn_recv_data(conn, (void **)new_buf);
  480. }
  481. /**
  482. * Receive data (in form of a netbuf containing a packet buffer) from a netconn
  483. *
  484. * @param conn the netconn from which to receive data
  485. * @param new_buf pointer where a new netbuf is stored when received data
  486. * @return ERR_OK if data has been received, an error code otherwise (timeout,
  487. * memory error or another error)
  488. */
  489. err_t
  490. netconn_recv(struct netconn *conn, struct netbuf **new_buf)
  491. {
  492. #if LWIP_TCP
  493. struct netbuf *buf = NULL;
  494. err_t err;
  495. #endif /* LWIP_TCP */
  496. LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
  497. *new_buf = NULL;
  498. LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
  499. #if LWIP_TCP
  500. #if (LWIP_UDP || LWIP_RAW)
  501. if (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP)
  502. #endif /* (LWIP_UDP || LWIP_RAW) */
  503. {
  504. struct pbuf *p = NULL;
  505. /* This is not a listening netconn, since recvmbox is set */
  506. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  507. if (buf == NULL) {
  508. return ERR_MEM;
  509. }
  510. err = netconn_recv_data(conn, (void **)&p);
  511. if (err != ERR_OK) {
  512. memp_free(MEMP_NETBUF, buf);
  513. return err;
  514. }
  515. LWIP_ASSERT("p != NULL", p != NULL);
  516. buf->p = p;
  517. buf->ptr = p;
  518. buf->port = 0;
  519. ip_addr_set_zero(&buf->addr);
  520. *new_buf = buf;
  521. /* don't set conn->last_err: it's only ERR_OK, anyway */
  522. return ERR_OK;
  523. }
  524. #endif /* LWIP_TCP */
  525. #if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
  526. else
  527. #endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
  528. {
  529. #if (LWIP_UDP || LWIP_RAW)
  530. return netconn_recv_data(conn, (void **)new_buf);
  531. #endif /* (LWIP_UDP || LWIP_RAW) */
  532. }
  533. }
  534. /**
  535. * TCP: update the receive window: by calling this, the application
  536. * tells the stack that it has processed data and is able to accept
  537. * new data.
  538. * ATTENTION: use with care, this is mainly used for sockets!
  539. * Can only be used when calling netconn_set_noautorecved(conn, 1) before.
  540. *
  541. * @param conn the netconn for which to update the receive window
  542. * @param length amount of data processed (ATTENTION: this must be accurate!)
  543. */
  544. void
  545. netconn_recved(struct netconn *conn, u32_t length)
  546. {
  547. #if LWIP_TCP
  548. if ((conn != NULL) && (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP) &&
  549. (netconn_get_noautorecved(conn))) {
  550. API_MSG_VAR_DECLARE(msg);
  551. /* Let the stack know that we have taken the data. */
  552. /* TODO: Speedup: Don't block and wait for the answer here
  553. (to prevent multiple thread-switches). */
  554. API_MSG_VAR_ALLOC_DONTFAIL(msg);
  555. API_MSG_VAR_REF(msg).msg.conn = conn;
  556. API_MSG_VAR_REF(msg).msg.msg.r.len = length;
  557. /* don't care for the return value of lwip_netconn_do_recv */
  558. TCPIP_APIMSG_NOERR(&API_MSG_VAR_REF(msg), lwip_netconn_do_recv);
  559. API_MSG_VAR_FREE(msg);
  560. }
  561. #else /* LWIP_TCP */
  562. LWIP_UNUSED_ARG(conn);
  563. LWIP_UNUSED_ARG(length);
  564. #endif /* LWIP_TCP */
  565. }
  566. /**
  567. * Send data (in form of a netbuf) to a specific remote IP address and port.
  568. * Only to be used for UDP and RAW netconns (not TCP).
  569. *
  570. * @param conn the netconn over which to send data
  571. * @param buf a netbuf containing the data to send
  572. * @param addr the remote IP address to which to send the data
  573. * @param port the remote port to which to send the data
  574. * @return ERR_OK if data was sent, any other err_t on error
  575. */
  576. err_t
  577. netconn_sendto(struct netconn *conn, struct netbuf *buf, const ip_addr_t *addr, u16_t port)
  578. {
  579. if (buf != NULL) {
  580. ip_addr_set(&buf->addr, addr);
  581. buf->port = port;
  582. return netconn_send(conn, buf);
  583. }
  584. return ERR_VAL;
  585. }
  586. /**
  587. * Send data over a UDP or RAW netconn (that is already connected).
  588. *
  589. * @param conn the UDP or RAW netconn over which to send data
  590. * @param buf a netbuf containing the data to send
  591. * @return ERR_OK if data was sent, any other err_t on error
  592. */
  593. err_t
  594. netconn_send(struct netconn *conn, struct netbuf *buf)
  595. {
  596. API_MSG_VAR_DECLARE(msg);
  597. err_t err;
  598. LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;);
  599. LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len));
  600. API_MSG_VAR_ALLOC(msg);
  601. API_MSG_VAR_REF(msg).msg.conn = conn;
  602. API_MSG_VAR_REF(msg).msg.msg.b = buf;
  603. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_send, err);
  604. API_MSG_VAR_FREE(msg);
  605. return err;
  606. }
  607. /**
  608. * Send data over a TCP netconn.
  609. *
  610. * @param conn the TCP netconn over which to send data
  611. * @param dataptr pointer to the application buffer that contains the data to send
  612. * @param size size of the application data to send
  613. * @param apiflags combination of following flags :
  614. * - NETCONN_COPY: data will be copied into memory belonging to the stack
  615. * - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
  616. * - NETCONN_DONTBLOCK: only write the data if all data can be written at once
  617. * @param bytes_written pointer to a location that receives the number of written bytes
  618. * @return ERR_OK if data was sent, any other err_t on error
  619. */
  620. err_t
  621. netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
  622. u8_t apiflags, size_t *bytes_written)
  623. {
  624. API_MSG_VAR_DECLARE(msg);
  625. err_t err;
  626. u8_t dontblock;
  627. LWIP_ERROR("netconn_write: invalid conn", (conn != NULL), return ERR_ARG;);
  628. LWIP_ERROR("netconn_write: invalid conn->type", (NETCONNTYPE_GROUP(conn->type)== NETCONN_TCP), return ERR_VAL;);
  629. if (size == 0) {
  630. return ERR_OK;
  631. }
  632. dontblock = netconn_is_nonblocking(conn) || (apiflags & NETCONN_DONTBLOCK);
  633. if (dontblock && !bytes_written) {
  634. /* This implies netconn_write() cannot be used for non-blocking send, since
  635. it has no way to return the number of bytes written. */
  636. return ERR_VAL;
  637. }
  638. API_MSG_VAR_ALLOC(msg);
  639. /* non-blocking write sends as much */
  640. API_MSG_VAR_REF(msg).msg.conn = conn;
  641. API_MSG_VAR_REF(msg).msg.msg.w.dataptr = dataptr;
  642. API_MSG_VAR_REF(msg).msg.msg.w.apiflags = apiflags;
  643. API_MSG_VAR_REF(msg).msg.msg.w.len = size;
  644. #if LWIP_SO_SNDTIMEO
  645. if (conn->send_timeout != 0) {
  646. /* get the time we started, which is later compared to
  647. sys_now() + conn->send_timeout */
  648. API_MSG_VAR_REF(msg).msg.msg.w.time_started = sys_now();
  649. } else {
  650. API_MSG_VAR_REF(msg).msg.msg.w.time_started = 0;
  651. }
  652. #endif /* LWIP_SO_SNDTIMEO */
  653. /* For locking the core: this _can_ be delayed on low memory/low send buffer,
  654. but if it is, this is done inside api_msg.c:do_write(), so we can use the
  655. non-blocking version here. */
  656. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_write, err);
  657. if ((err == ERR_OK) && (bytes_written != NULL)) {
  658. if (dontblock
  659. #if LWIP_SO_SNDTIMEO
  660. || (conn->send_timeout != 0)
  661. #endif /* LWIP_SO_SNDTIMEO */
  662. ) {
  663. /* nonblocking write: maybe the data has been sent partly */
  664. *bytes_written = API_MSG_VAR_REF(msg).msg.msg.w.len;
  665. } else {
  666. /* blocking call succeeded: all data has been sent if it */
  667. *bytes_written = size;
  668. }
  669. }
  670. API_MSG_VAR_FREE(msg);
  671. return err;
  672. }
  673. /**
  674. * Close or shutdown a TCP netconn (doesn't delete it).
  675. *
  676. * @param conn the TCP netconn to close or shutdown
  677. * @param how fully close or only shutdown one side?
  678. * @return ERR_OK if the netconn was closed, any other err_t on error
  679. */
  680. static err_t
  681. netconn_close_shutdown(struct netconn *conn, u8_t how)
  682. {
  683. API_MSG_VAR_DECLARE(msg);
  684. err_t err;
  685. LWIP_UNUSED_ARG(how);
  686. LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;);
  687. API_MSG_VAR_ALLOC(msg);
  688. API_MSG_VAR_REF(msg).msg.conn = conn;
  689. #if LWIP_TCP
  690. /* shutting down both ends is the same as closing */
  691. API_MSG_VAR_REF(msg).msg.msg.sd.shut = how;
  692. #if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER
  693. /* get the time we started, which is later compared to
  694. sys_now() + conn->send_timeout */
  695. API_MSG_VAR_REF(msg).msg.msg.sd.time_started = sys_now();
  696. #else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
  697. API_MSG_VAR_REF(msg).msg.msg.sd.polls_left =
  698. ((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1;
  699. #endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
  700. #endif /* LWIP_TCP */
  701. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_close, err);
  702. API_MSG_VAR_FREE(msg);
  703. return err;
  704. }
  705. /**
  706. * Close a TCP netconn (doesn't delete it).
  707. *
  708. * @param conn the TCP netconn to close
  709. * @return ERR_OK if the netconn was closed, any other err_t on error
  710. */
  711. err_t
  712. netconn_close(struct netconn *conn)
  713. {
  714. /* shutting down both ends is the same as closing */
  715. return netconn_close_shutdown(conn, NETCONN_SHUT_RDWR);
  716. }
  717. /**
  718. * Shut down one or both sides of a TCP netconn (doesn't delete it).
  719. *
  720. * @param conn the TCP netconn to shut down
  721. * @return ERR_OK if the netconn was closed, any other err_t on error
  722. */
  723. err_t
  724. netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx)
  725. {
  726. return netconn_close_shutdown(conn, (shut_rx ? NETCONN_SHUT_RD : 0) | (shut_tx ? NETCONN_SHUT_WR : 0));
  727. }
  728. #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
  729. /**
  730. * Join multicast groups for UDP netconns.
  731. *
  732. * @param conn the UDP netconn for which to change multicast addresses
  733. * @param multiaddr IP address of the multicast group to join or leave
  734. * @param netif_addr the IP address of the network interface on which to send
  735. * the igmp message
  736. * @param join_or_leave flag whether to send a join- or leave-message
  737. * @return ERR_OK if the action was taken, any err_t on error
  738. */
  739. err_t
  740. netconn_join_leave_group(struct netconn *conn,
  741. const ip_addr_t *multiaddr,
  742. const ip_addr_t *netif_addr,
  743. enum netconn_igmp join_or_leave)
  744. {
  745. API_MSG_VAR_DECLARE(msg);
  746. err_t err;
  747. LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;);
  748. API_MSG_VAR_ALLOC(msg);
  749. /* Don't propagate NULL pointer (IP_ADDR_ANY alias) to subsequent functions */
  750. if (multiaddr == NULL) {
  751. multiaddr = IP_ADDR_ANY;
  752. }
  753. if (netif_addr == NULL) {
  754. netif_addr = IP_ADDR_ANY;
  755. }
  756. API_MSG_VAR_REF(msg).msg.conn = conn;
  757. API_MSG_VAR_REF(msg).msg.msg.jl.multiaddr = API_MSG_VAR_REF(multiaddr);
  758. API_MSG_VAR_REF(msg).msg.msg.jl.netif_addr = API_MSG_VAR_REF(netif_addr);
  759. API_MSG_VAR_REF(msg).msg.msg.jl.join_or_leave = join_or_leave;
  760. TCPIP_APIMSG(&API_MSG_VAR_REF(msg), lwip_netconn_do_join_leave_group, err);
  761. API_MSG_VAR_FREE(msg);
  762. return err;
  763. }
  764. #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
  765. #if LWIP_DNS
  766. /**
  767. * Execute a DNS query, only one IP address is returned
  768. *
  769. * @param name a string representation of the DNS host name to query
  770. * @param addr a preallocated ip_addr_t where to store the resolved IP address
  771. * @return ERR_OK: resolving succeeded
  772. * ERR_MEM: memory error, try again later
  773. * ERR_ARG: dns client not initialized or invalid hostname
  774. * ERR_VAL: dns server response was invalid
  775. */
  776. #if LWIP_IPV4 && LWIP_IPV6
  777. err_t
  778. netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype)
  779. #else
  780. err_t
  781. netconn_gethostbyname(const char *name, ip_addr_t *addr)
  782. #endif
  783. {
  784. API_VAR_DECLARE(struct dns_api_msg, msg);
  785. #if !LWIP_MPU_COMPATIBLE
  786. sys_sem_t sem;
  787. #endif /* LWIP_MPU_COMPATIBLE */
  788. err_t err;
  789. LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
  790. LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
  791. #if LWIP_MPU_COMPATIBLE
  792. if (strlen(name) >= DNS_MAX_NAME_LENGTH) {
  793. return ERR_ARG;
  794. }
  795. #endif
  796. API_VAR_ALLOC(struct dns_api_msg, MEMP_DNS_API_MSG, msg);
  797. #if LWIP_MPU_COMPATIBLE
  798. strncpy(API_VAR_REF(msg).name, name, DNS_MAX_NAME_LENGTH-1);
  799. API_VAR_REF(msg).name[DNS_MAX_NAME_LENGTH-1] = 0;
  800. #else /* LWIP_MPU_COMPATIBLE */
  801. msg.err = &err;
  802. msg.sem = &sem;
  803. API_VAR_REF(msg).addr = API_VAR_REF(addr);
  804. API_VAR_REF(msg).name = name;
  805. #endif /* LWIP_MPU_COMPATIBLE */
  806. #if LWIP_IPV4 && LWIP_IPV6
  807. API_VAR_REF(msg).dns_addrtype = dns_addrtype;
  808. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  809. #if LWIP_NETCONN_SEM_PER_THREAD
  810. API_VAR_REF(msg).sem = LWIP_NETCONN_THREAD_SEM_GET();
  811. #else /* LWIP_NETCONN_SEM_PER_THREAD*/
  812. err = sys_sem_new(API_EXPR_REF(API_VAR_REF(msg).sem), 0);
  813. if (err != ERR_OK) {
  814. API_VAR_FREE(MEMP_DNS_API_MSG, msg);
  815. return err;
  816. }
  817. #endif /* LWIP_NETCONN_SEM_PER_THREAD */
  818. err = tcpip_callback(lwip_netconn_do_gethostbyname, &API_VAR_REF(msg));
  819. if (err != ERR_OK) {
  820. #if !LWIP_NETCONN_SEM_PER_THREAD
  821. sys_sem_free(API_EXPR_REF(API_VAR_REF(msg).sem));
  822. #endif /* !LWIP_NETCONN_SEM_PER_THREAD */
  823. API_VAR_FREE(MEMP_DNS_API_MSG, msg);
  824. return err;
  825. }
  826. sys_sem_wait(API_EXPR_REF_SEM(API_VAR_REF(msg).sem));
  827. #if !LWIP_NETCONN_SEM_PER_THREAD
  828. sys_sem_free(API_EXPR_REF(API_VAR_REF(msg).sem));
  829. #endif /* !LWIP_NETCONN_SEM_PER_THREAD */
  830. #if LWIP_MPU_COMPATIBLE
  831. *addr = msg->addr;
  832. err = msg->err;
  833. #endif /* LWIP_MPU_COMPATIBLE */
  834. API_VAR_FREE(MEMP_DNS_API_MSG, msg);
  835. return err;
  836. }
  837. #endif /* LWIP_DNS*/
  838. #if LWIP_NETCONN_SEM_PER_THREAD
  839. void
  840. netconn_thread_init(void)
  841. {
  842. sys_sem_t *sem = LWIP_NETCONN_THREAD_SEM_GET();
  843. if ((sem == NULL) || !sys_sem_valid(sem)) {
  844. /* call alloc only once */
  845. LWIP_NETCONN_THREAD_SEM_ALLOC();
  846. LWIP_ASSERT("LWIP_NETCONN_THREAD_SEM_ALLOC() failed", sys_sem_valid(LWIP_NETCONN_THREAD_SEM_GET()));
  847. }
  848. }
  849. void
  850. netconn_thread_cleanup(void)
  851. {
  852. sys_sem_t *sem = LWIP_NETCONN_THREAD_SEM_GET();
  853. if ((sem != NULL) && sys_sem_valid(sem)) {
  854. /* call free only once */
  855. LWIP_NETCONN_THREAD_SEM_FREE();
  856. }
  857. }
  858. #endif /* LWIP_NETCONN_SEM_PER_THREAD */
  859. #endif /* LWIP_NETCONN */