api_lib.c 31 KB

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