api_lib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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/tcpip.h"
  43. #include "lwip/memp.h"
  44. #include "lwip/ip.h"
  45. #include "lwip/raw.h"
  46. #include "lwip/udp.h"
  47. #include "lwip/tcp.h"
  48. #include <string.h>
  49. /**
  50. * Create a new netconn (of a specific type) that has a callback function.
  51. * The corresponding pcb is also created.
  52. *
  53. * @param t the type of 'connection' to create (@see enum netconn_type)
  54. * @param proto the IP protocol for RAW IP pcbs
  55. * @param callback a function to call on status changes (RX available, TX'ed)
  56. * @return a newly allocated struct netconn or
  57. * NULL on memory error
  58. */
  59. struct netconn*
  60. netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback)
  61. {
  62. struct netconn *conn;
  63. struct api_msg msg;
  64. conn = netconn_alloc(t, callback);
  65. if (conn != NULL) {
  66. msg.function = do_newconn;
  67. msg.msg.msg.n.proto = proto;
  68. msg.msg.conn = conn;
  69. if (TCPIP_APIMSG(&msg) != ERR_OK) {
  70. LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL);
  71. LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed));
  72. LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox));
  73. #if LWIP_TCP
  74. LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox));
  75. #endif /* LWIP_TCP */
  76. sys_sem_free(&conn->op_completed);
  77. sys_mbox_free(&conn->recvmbox);
  78. memp_free(MEMP_NETCONN, conn);
  79. return NULL;
  80. }
  81. }
  82. return conn;
  83. }
  84. /**
  85. * Close a netconn 'connection' and free its resources.
  86. * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate
  87. * after this returns.
  88. *
  89. * @param conn the netconn to delete
  90. * @return ERR_OK if the connection was deleted
  91. */
  92. err_t
  93. netconn_delete(struct netconn *conn)
  94. {
  95. struct api_msg msg;
  96. /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */
  97. if (conn == NULL) {
  98. return ERR_OK;
  99. }
  100. msg.function = do_delconn;
  101. msg.msg.conn = conn;
  102. tcpip_apimsg(&msg);
  103. netconn_free(conn);
  104. /* don't care for return value of do_delconn since it only calls void functions */
  105. return ERR_OK;
  106. }
  107. /**
  108. * Get the local or remote IP address and port of a netconn.
  109. * For RAW netconns, this returns the protocol instead of a port!
  110. *
  111. * @param conn the netconn to query
  112. * @param addr a pointer to which to save the IP address
  113. * @param port a pointer to which to save the port (or protocol for RAW)
  114. * @param local 1 to get the local IP address, 0 to get the remote one
  115. * @return ERR_CONN for invalid connections
  116. * ERR_OK if the information was retrieved
  117. */
  118. err_t
  119. netconn_getaddr(struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local)
  120. {
  121. struct api_msg msg;
  122. err_t err;
  123. LWIP_ERROR("netconn_getaddr: invalid conn", (conn != NULL), return ERR_ARG;);
  124. LWIP_ERROR("netconn_getaddr: invalid addr", (addr != NULL), return ERR_ARG;);
  125. LWIP_ERROR("netconn_getaddr: invalid port", (port != NULL), return ERR_ARG;);
  126. msg.function = do_getaddr;
  127. msg.msg.conn = conn;
  128. msg.msg.msg.ad.ipaddr = addr;
  129. msg.msg.msg.ad.port = port;
  130. msg.msg.msg.ad.local = local;
  131. err = TCPIP_APIMSG(&msg);
  132. NETCONN_SET_SAFE_ERR(conn, err);
  133. return err;
  134. }
  135. /**
  136. * Bind a netconn to a specific local IP address and port.
  137. * Binding one netconn twice might not always be checked correctly!
  138. *
  139. * @param conn the netconn to bind
  140. * @param addr the local IP address to bind the netconn to (use IP_ADDR_ANY
  141. * to bind to all addresses)
  142. * @param port the local port to bind the netconn to (not used for RAW)
  143. * @return ERR_OK if bound, any other err_t on failure
  144. */
  145. err_t
  146. netconn_bind(struct netconn *conn, ip_addr_t *addr, u16_t port)
  147. {
  148. struct api_msg msg;
  149. err_t err;
  150. LWIP_ERROR("netconn_bind: invalid conn", (conn != NULL), return ERR_ARG;);
  151. msg.function = do_bind;
  152. msg.msg.conn = conn;
  153. msg.msg.msg.bc.ipaddr = addr;
  154. msg.msg.msg.bc.port = port;
  155. err = TCPIP_APIMSG(&msg);
  156. NETCONN_SET_SAFE_ERR(conn, err);
  157. return err;
  158. }
  159. /**
  160. * Connect a netconn to a specific remote IP address and port.
  161. *
  162. * @param conn the netconn to connect
  163. * @param addr the remote IP address to connect to
  164. * @param port the remote port to connect to (no used for RAW)
  165. * @return ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise
  166. */
  167. err_t
  168. netconn_connect(struct netconn *conn, ip_addr_t *addr, u16_t port)
  169. {
  170. struct api_msg msg;
  171. err_t err;
  172. LWIP_ERROR("netconn_connect: invalid conn", (conn != NULL), return ERR_ARG;);
  173. msg.function = do_connect;
  174. msg.msg.conn = conn;
  175. msg.msg.msg.bc.ipaddr = addr;
  176. msg.msg.msg.bc.port = port;
  177. /* This is the only function which need to not block tcpip_thread */
  178. err = tcpip_apimsg(&msg);
  179. NETCONN_SET_SAFE_ERR(conn, err);
  180. return err;
  181. }
  182. /**
  183. * Disconnect a netconn from its current peer (only valid for UDP netconns).
  184. *
  185. * @param conn the netconn to disconnect
  186. * @return TODO: return value is not set here...
  187. */
  188. err_t
  189. netconn_disconnect(struct netconn *conn)
  190. {
  191. struct api_msg msg;
  192. err_t err;
  193. LWIP_ERROR("netconn_disconnect: invalid conn", (conn != NULL), return ERR_ARG;);
  194. msg.function = do_disconnect;
  195. msg.msg.conn = conn;
  196. err = TCPIP_APIMSG(&msg);
  197. NETCONN_SET_SAFE_ERR(conn, err);
  198. return err;
  199. }
  200. /**
  201. * Set a TCP netconn into listen mode
  202. *
  203. * @param conn the tcp netconn to set to listen mode
  204. * @param backlog the listen backlog, only used if TCP_LISTEN_BACKLOG==1
  205. * @return ERR_OK if the netconn was set to listen (UDP and RAW netconns
  206. * don't return any error (yet?))
  207. */
  208. err_t
  209. netconn_listen_with_backlog(struct netconn *conn, u8_t backlog)
  210. {
  211. struct api_msg msg;
  212. err_t err;
  213. /* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */
  214. LWIP_UNUSED_ARG(backlog);
  215. LWIP_ERROR("netconn_listen: invalid conn", (conn != NULL), return ERR_ARG;);
  216. msg.function = do_listen;
  217. msg.msg.conn = conn;
  218. #if TCP_LISTEN_BACKLOG
  219. msg.msg.msg.lb.backlog = backlog;
  220. #endif /* TCP_LISTEN_BACKLOG */
  221. err = TCPIP_APIMSG(&msg);
  222. NETCONN_SET_SAFE_ERR(conn, err);
  223. return err;
  224. }
  225. /**
  226. * Accept a new connection on a TCP listening netconn.
  227. *
  228. * @param conn the TCP listen netconn
  229. * @param new_conn pointer where the new connection is stored
  230. * @return ERR_OK if a new connection has been received or an error
  231. * code otherwise
  232. */
  233. err_t
  234. netconn_accept(struct netconn *conn, struct netconn **new_conn)
  235. {
  236. #if LWIP_TCP
  237. struct netconn *newconn;
  238. err_t err;
  239. #if TCP_LISTEN_BACKLOG
  240. struct api_msg msg;
  241. #endif /* TCP_LISTEN_BACKLOG */
  242. LWIP_ERROR("netconn_accept: invalid pointer", (new_conn != NULL), return ERR_ARG;);
  243. *new_conn = NULL;
  244. LWIP_ERROR("netconn_accept: invalid conn", (conn != NULL), return ERR_ARG;);
  245. LWIP_ERROR("netconn_accept: invalid acceptmbox", sys_mbox_valid(&conn->acceptmbox), return ERR_ARG;);
  246. err = conn->last_err;
  247. if (ERR_IS_FATAL(err)) {
  248. /* don't recv on fatal errors: this might block the application task
  249. waiting on acceptmbox forever! */
  250. return err;
  251. }
  252. #if LWIP_SO_RCVTIMEO
  253. if (sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
  254. NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
  255. return ERR_TIMEOUT;
  256. }
  257. #else
  258. sys_arch_mbox_fetch(&conn->acceptmbox, (void **)&newconn, 0);
  259. #endif /* LWIP_SO_RCVTIMEO*/
  260. /* Register event with callback */
  261. API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
  262. if (newconn == NULL) {
  263. /* connection has been closed */
  264. NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
  265. return ERR_CLSD;
  266. }
  267. #if TCP_LISTEN_BACKLOG
  268. /* Let the stack know that we have accepted the connection. */
  269. msg.function = do_recv;
  270. msg.msg.conn = conn;
  271. /* don't care for the return value of do_recv */
  272. TCPIP_APIMSG(&msg);
  273. #endif /* TCP_LISTEN_BACKLOG */
  274. *new_conn = newconn;
  275. /* don't set conn->last_err: it's only ERR_OK, anyway */
  276. return ERR_OK;
  277. #else /* LWIP_TCP */
  278. LWIP_UNUSED_ARG(conn);
  279. LWIP_UNUSED_ARG(new_conn);
  280. return ERR_ARG;
  281. #endif /* LWIP_TCP */
  282. }
  283. /**
  284. * Receive data: actual implementation that doesn't care whether pbuf or netbuf
  285. * is received
  286. *
  287. * @param conn the netconn from which to receive data
  288. * @param new_buf pointer where a new pbuf/netbuf is stored when received data
  289. * @return ERR_OK if data has been received, an error code otherwise (timeout,
  290. * memory error or another error)
  291. */
  292. static err_t
  293. netconn_recv_data(struct netconn *conn, void **new_buf)
  294. {
  295. void *buf = NULL;
  296. u16_t len;
  297. err_t err;
  298. #if LWIP_TCP
  299. struct api_msg msg;
  300. #endif /* LWIP_TCP */
  301. LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
  302. *new_buf = NULL;
  303. LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
  304. LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
  305. err = conn->last_err;
  306. if (ERR_IS_FATAL(err)) {
  307. /* don't recv on fatal errors: this might block the application task
  308. waiting on recvmbox forever! */
  309. /* @todo: this does not allow us to fetch data that has been put into recvmbox
  310. before the fatal error occurred - is that a problem? */
  311. return err;
  312. }
  313. #if LWIP_SO_RCVTIMEO
  314. if (sys_arch_mbox_fetch(&conn->recvmbox, &buf, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
  315. NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
  316. return ERR_TIMEOUT;
  317. }
  318. #else
  319. sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
  320. #endif /* LWIP_SO_RCVTIMEO*/
  321. #if LWIP_TCP
  322. if (conn->type == NETCONN_TCP) {
  323. if (!netconn_get_noautorecved(conn) || (buf == NULL)) {
  324. /* Let the stack know that we have taken the data. */
  325. /* TODO: Speedup: Don't block and wait for the answer here
  326. (to prevent multiple thread-switches). */
  327. msg.function = do_recv;
  328. msg.msg.conn = conn;
  329. if (buf != NULL) {
  330. msg.msg.msg.r.len = ((struct pbuf *)buf)->tot_len;
  331. } else {
  332. msg.msg.msg.r.len = 1;
  333. }
  334. /* don't care for the return value of do_recv */
  335. TCPIP_APIMSG(&msg);
  336. }
  337. /* If we are closed, we indicate that we no longer wish to use the socket */
  338. if (buf == NULL) {
  339. API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
  340. /* Avoid to lose any previous error code */
  341. NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
  342. return ERR_CLSD;
  343. }
  344. len = ((struct pbuf *)buf)->tot_len;
  345. }
  346. #endif /* LWIP_TCP */
  347. #if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
  348. else
  349. #endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
  350. #if (LWIP_UDP || LWIP_RAW)
  351. {
  352. LWIP_ASSERT("buf != NULL", buf != NULL);
  353. len = netbuf_len((struct netbuf *)buf);
  354. }
  355. #endif /* (LWIP_UDP || LWIP_RAW) */
  356. SYS_ARCH_DEC(conn->recv_avail, len);
  357. /* Register event with callback */
  358. API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
  359. LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len));
  360. *new_buf = buf;
  361. /* don't set conn->last_err: it's only ERR_OK, anyway */
  362. return ERR_OK;
  363. }
  364. /**
  365. * Receive data (in form of a pbuf) from a TCP netconn
  366. *
  367. * @param conn the netconn from which to receive data
  368. * @param new_buf pointer where a new pbuf is stored when received data
  369. * @return ERR_OK if data has been received, an error code otherwise (timeout,
  370. * memory error or another error)
  371. * ERR_ARG if conn is not a TCP netconn
  372. */
  373. err_t
  374. netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf)
  375. {
  376. LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL) &&
  377. netconn_type(conn) == NETCONN_TCP, return ERR_ARG;);
  378. return netconn_recv_data(conn, (void **)new_buf);
  379. }
  380. /**
  381. * Receive data (in form of a netbuf containing a packet buffer) from a netconn
  382. *
  383. * @param conn the netconn from which to receive data
  384. * @param new_buf pointer where a new netbuf is stored when received data
  385. * @return ERR_OK if data has been received, an error code otherwise (timeout,
  386. * memory error or another error)
  387. */
  388. err_t
  389. netconn_recv(struct netconn *conn, struct netbuf **new_buf)
  390. {
  391. #if LWIP_TCP
  392. struct netbuf *buf = NULL;
  393. err_t err;
  394. #endif /* LWIP_TCP */
  395. LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
  396. *new_buf = NULL;
  397. LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
  398. LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
  399. #if LWIP_TCP
  400. if (conn->type == NETCONN_TCP) {
  401. struct pbuf *p = NULL;
  402. /* This is not a listening netconn, since recvmbox is set */
  403. buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
  404. if (buf == NULL) {
  405. NETCONN_SET_SAFE_ERR(conn, ERR_MEM);
  406. return ERR_MEM;
  407. }
  408. err = netconn_recv_data(conn, (void **)&p);
  409. if (err != ERR_OK) {
  410. memp_free(MEMP_NETBUF, buf);
  411. return err;
  412. }
  413. LWIP_ASSERT("p != NULL", p != NULL);
  414. buf->p = p;
  415. buf->ptr = p;
  416. buf->port = 0;
  417. ip_addr_set_any(&buf->addr);
  418. *new_buf = buf;
  419. /* don't set conn->last_err: it's only ERR_OK, anyway */
  420. return ERR_OK;
  421. } else
  422. #endif /* LWIP_TCP */
  423. {
  424. #if (LWIP_UDP || LWIP_RAW)
  425. return netconn_recv_data(conn, (void **)new_buf);
  426. #endif /* (LWIP_UDP || LWIP_RAW) */
  427. }
  428. }
  429. /**
  430. * TCP: update the receive window: by calling this, the application
  431. * tells the stack that it has processed data and is able to accept
  432. * new data.
  433. * ATTENTION: use with care, this is mainly used for sockets!
  434. * Can only be used when calling netconn_set_noautorecved(conn, 1) before.
  435. *
  436. * @param conn the netconn for which to update the receive window
  437. * @param length amount of data processed (ATTENTION: this must be accurate!)
  438. */
  439. void
  440. netconn_recved(struct netconn *conn, u32_t length)
  441. {
  442. if ((conn != NULL) && (conn->type == NETCONN_TCP) &&
  443. (netconn_get_noautorecved(conn))) {
  444. struct api_msg msg;
  445. /* Let the stack know that we have taken the data. */
  446. /* TODO: Speedup: Don't block and wait for the answer here
  447. (to prevent multiple thread-switches). */
  448. msg.function = do_recv;
  449. msg.msg.conn = conn;
  450. msg.msg.msg.r.len = length;
  451. /* don't care for the return value of do_recv */
  452. TCPIP_APIMSG(&msg);
  453. }
  454. }
  455. /**
  456. * Send data (in form of a netbuf) to a specific remote IP address and port.
  457. * Only to be used for UDP and RAW netconns (not TCP).
  458. *
  459. * @param conn the netconn over which to send data
  460. * @param buf a netbuf containing the data to send
  461. * @param addr the remote IP address to which to send the data
  462. * @param port the remote port to which to send the data
  463. * @return ERR_OK if data was sent, any other err_t on error
  464. */
  465. err_t
  466. netconn_sendto(struct netconn *conn, struct netbuf *buf, ip_addr_t *addr, u16_t port)
  467. {
  468. if (buf != NULL) {
  469. ip_addr_set(&buf->addr, addr);
  470. buf->port = port;
  471. return netconn_send(conn, buf);
  472. }
  473. return ERR_VAL;
  474. }
  475. /**
  476. * Send data over a UDP or RAW netconn (that is already connected).
  477. *
  478. * @param conn the UDP or RAW netconn over which to send data
  479. * @param buf a netbuf containing the data to send
  480. * @return ERR_OK if data was sent, any other err_t on error
  481. */
  482. err_t
  483. netconn_send(struct netconn *conn, struct netbuf *buf)
  484. {
  485. struct api_msg msg;
  486. err_t err;
  487. LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;);
  488. LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len));
  489. msg.function = do_send;
  490. msg.msg.conn = conn;
  491. msg.msg.msg.b = buf;
  492. err = TCPIP_APIMSG(&msg);
  493. NETCONN_SET_SAFE_ERR(conn, err);
  494. return err;
  495. }
  496. /**
  497. * Send data over a TCP netconn.
  498. *
  499. * @param conn the TCP netconn over which to send data
  500. * @param dataptr pointer to the application buffer that contains the data to send
  501. * @param size size of the application data to send
  502. * @param apiflags combination of following flags :
  503. * - NETCONN_COPY: data will be copied into memory belonging to the stack
  504. * - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
  505. * - NETCONN_DONTBLOCK: only write the data if all dat can be written at once
  506. * @return ERR_OK if data was sent, any other err_t on error
  507. */
  508. err_t
  509. netconn_write(struct netconn *conn, const void *dataptr, size_t size, u8_t apiflags)
  510. {
  511. struct api_msg msg;
  512. err_t err;
  513. LWIP_ERROR("netconn_write: invalid conn", (conn != NULL), return ERR_ARG;);
  514. LWIP_ERROR("netconn_write: invalid conn->type", (conn->type == NETCONN_TCP), return ERR_VAL;);
  515. if (size == 0) {
  516. return ERR_OK;
  517. }
  518. /* @todo: for non-blocking write, check if 'size' would ever fit into
  519. snd_queue or snd_buf */
  520. msg.function = do_write;
  521. msg.msg.conn = conn;
  522. msg.msg.msg.w.dataptr = dataptr;
  523. msg.msg.msg.w.apiflags = apiflags;
  524. msg.msg.msg.w.len = size;
  525. /* For locking the core: this _can_ be delayed on low memory/low send buffer,
  526. but if it is, this is done inside api_msg.c:do_write(), so we can use the
  527. non-blocking version here. */
  528. err = TCPIP_APIMSG(&msg);
  529. NETCONN_SET_SAFE_ERR(conn, err);
  530. return err;
  531. }
  532. /**
  533. * Close ot shutdown a TCP netconn (doesn't delete it).
  534. *
  535. * @param conn the TCP netconn to close or shutdown
  536. * @param how fully close or only shutdown one side?
  537. * @return ERR_OK if the netconn was closed, any other err_t on error
  538. */
  539. static err_t
  540. netconn_close_shutdown(struct netconn *conn, u8_t how)
  541. {
  542. struct api_msg msg;
  543. err_t err;
  544. LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;);
  545. msg.function = do_close;
  546. msg.msg.conn = conn;
  547. /* shutting down both ends is the same as closing */
  548. msg.msg.msg.sd.shut = how;
  549. /* because of the LWIP_TCPIP_CORE_LOCKING implementation of do_close,
  550. don't use TCPIP_APIMSG here */
  551. err = tcpip_apimsg(&msg);
  552. NETCONN_SET_SAFE_ERR(conn, err);
  553. return err;
  554. }
  555. /**
  556. * Close a TCP netconn (doesn't delete it).
  557. *
  558. * @param conn the TCP netconn to close
  559. * @return ERR_OK if the netconn was closed, any other err_t on error
  560. */
  561. err_t
  562. netconn_close(struct netconn *conn)
  563. {
  564. /* shutting down both ends is the same as closing */
  565. return netconn_close_shutdown(conn, NETCONN_SHUT_RDWR);
  566. }
  567. /**
  568. * Shut down one or both sides of a TCP netconn (doesn't delete it).
  569. *
  570. * @param conn the TCP netconn to shut down
  571. * @return ERR_OK if the netconn was closed, any other err_t on error
  572. */
  573. err_t
  574. netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx)
  575. {
  576. return netconn_close_shutdown(conn, (shut_rx ? NETCONN_SHUT_RD : 0) | (shut_tx ? NETCONN_SHUT_WR : 0));
  577. }
  578. #if LWIP_IGMP
  579. /**
  580. * Join multicast groups for UDP netconns.
  581. *
  582. * @param conn the UDP netconn for which to change multicast addresses
  583. * @param multiaddr IP address of the multicast group to join or leave
  584. * @param netif_addr the IP address of the network interface on which to send
  585. * the igmp message
  586. * @param join_or_leave flag whether to send a join- or leave-message
  587. * @return ERR_OK if the action was taken, any err_t on error
  588. */
  589. err_t
  590. netconn_join_leave_group(struct netconn *conn,
  591. ip_addr_t *multiaddr,
  592. ip_addr_t *netif_addr,
  593. enum netconn_igmp join_or_leave)
  594. {
  595. struct api_msg msg;
  596. err_t err;
  597. LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;);
  598. msg.function = do_join_leave_group;
  599. msg.msg.conn = conn;
  600. msg.msg.msg.jl.multiaddr = multiaddr;
  601. msg.msg.msg.jl.netif_addr = netif_addr;
  602. msg.msg.msg.jl.join_or_leave = join_or_leave;
  603. err = TCPIP_APIMSG(&msg);
  604. NETCONN_SET_SAFE_ERR(conn, err);
  605. return err;
  606. }
  607. #endif /* LWIP_IGMP */
  608. #if LWIP_DNS
  609. /**
  610. * Execute a DNS query, only one IP address is returned
  611. *
  612. * @param name a string representation of the DNS host name to query
  613. * @param addr a preallocated ip_addr_t where to store the resolved IP address
  614. * @return ERR_OK: resolving succeeded
  615. * ERR_MEM: memory error, try again later
  616. * ERR_ARG: dns client not initialized or invalid hostname
  617. * ERR_VAL: dns server response was invalid
  618. */
  619. err_t
  620. netconn_gethostbyname(const char *name, ip_addr_t *addr)
  621. {
  622. struct dns_api_msg msg;
  623. err_t err;
  624. sys_sem_t sem;
  625. LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
  626. LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
  627. err = sys_sem_new(&sem, 0);
  628. if (err != ERR_OK) {
  629. return err;
  630. }
  631. msg.name = name;
  632. msg.addr = addr;
  633. msg.err = &err;
  634. msg.sem = &sem;
  635. tcpip_callback(do_gethostbyname, &msg);
  636. sys_sem_wait(&sem);
  637. sys_sem_free(&sem);
  638. return err;
  639. }
  640. #endif /* LWIP_DNS*/
  641. #endif /* LWIP_NETCONN */