tcp_send_unbuffered.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /****************************************************************************
  2. * net/tcp/tcp_send_unbuffered.c
  3. *
  4. * Copyright (C) 2007-2014, 2016-2017, 2019 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) && \
  41. !defined(CONFIG_NET_TCP_WRITE_BUFFERS)
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <stdint.h>
  45. #include <stdbool.h>
  46. #include <string.h>
  47. #include <errno.h>
  48. #include <assert.h>
  49. #include <debug.h>
  50. #include <arch/irq.h>
  51. #include <nuttx/semaphore.h>
  52. #include <nuttx/net/net.h>
  53. #include <nuttx/net/netdev.h>
  54. #include <nuttx/net/arp.h>
  55. #include <nuttx/net/tcp.h>
  56. #include "netdev/netdev.h"
  57. #include "devif/devif.h"
  58. #include "socket/socket.h"
  59. #include "inet/inet.h"
  60. #include "arp/arp.h"
  61. #include "icmpv6/icmpv6.h"
  62. #include "neighbor/neighbor.h"
  63. #include "route/route.h"
  64. #include "tcp/tcp.h"
  65. /****************************************************************************
  66. * Pre-processor Definitions
  67. ****************************************************************************/
  68. /* If both IPv4 and IPv6 support are both enabled, then we will need to build
  69. * in some additional domain selection support.
  70. */
  71. #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
  72. # define NEED_IPDOMAIN_SUPPORT 1
  73. #endif
  74. #if defined(CONFIG_NET_TCP_SPLIT) && !defined(CONFIG_NET_TCP_SPLIT_SIZE)
  75. # define CONFIG_NET_TCP_SPLIT_SIZE 40
  76. #endif
  77. #define TCPIPv4BUF ((struct tcp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv4_HDRLEN])
  78. #define TCPIPv6BUF ((struct tcp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN])
  79. /****************************************************************************
  80. * Private Types
  81. ****************************************************************************/
  82. /* This structure holds the state of the send operation until it can be
  83. * operated upon when the TX poll event occurs.
  84. */
  85. struct send_s
  86. {
  87. FAR struct socket *snd_sock; /* Points to the parent socket structure */
  88. FAR struct devif_callback_s *snd_cb; /* Reference to callback instance */
  89. sem_t snd_sem; /* Used to wake up the waiting thread */
  90. FAR const uint8_t *snd_buffer; /* Points to the buffer of data to send */
  91. size_t snd_buflen; /* Number of bytes in the buffer to send */
  92. ssize_t snd_sent; /* The number of bytes sent */
  93. uint32_t snd_isn; /* Initial sequence number */
  94. uint32_t snd_acked; /* The number of bytes acked */
  95. #if defined(CONFIG_NET_TCP_SPLIT)
  96. bool snd_odd; /* True: Odd packet in pair transaction */
  97. #endif
  98. };
  99. /****************************************************************************
  100. * Private Functions
  101. ****************************************************************************/
  102. /****************************************************************************
  103. * Name: tcpsend_ipselect
  104. *
  105. * Description:
  106. * If both IPv4 and IPv6 support are enabled, then we will need to select
  107. * which one to use when generating the outgoing packet. If only one
  108. * domain is selected, then the setup is already in place and we need do
  109. * nothing.
  110. *
  111. * Input Parameters:
  112. * dev - The structure of the network driver that caused the event
  113. * pstate - sendto state structure
  114. *
  115. * Returned Value:
  116. * None
  117. *
  118. * Assumptions:
  119. * The network is locked.
  120. *
  121. ****************************************************************************/
  122. #ifdef NEED_IPDOMAIN_SUPPORT
  123. static inline void tcpsend_ipselect(FAR struct net_driver_s *dev,
  124. FAR struct tcp_conn_s *conn)
  125. {
  126. /* Which domain does the socket support */
  127. if (conn->domain == PF_INET)
  128. {
  129. /* Select the IPv4 domain */
  130. tcp_ipv4_select(dev);
  131. }
  132. else /* if (conn->domain == PF_INET6) */
  133. {
  134. /* Select the IPv6 domain */
  135. DEBUGASSERT(conn->domain == PF_INET6);
  136. tcp_ipv6_select(dev);
  137. }
  138. }
  139. #endif
  140. /****************************************************************************
  141. * Name: tcpsend_eventhandler
  142. *
  143. * Description:
  144. * This function is called to perform the actual send operation when
  145. * polled by the lower, device interfacing layer.
  146. *
  147. * Input Parameters:
  148. * dev The structure of the network driver that caused the event
  149. * conn The connection structure associated with the socket
  150. * flags Set of events describing why the callback was invoked
  151. *
  152. * Returned Value:
  153. * None
  154. *
  155. * Assumptions:
  156. * The network is locked.
  157. *
  158. ****************************************************************************/
  159. static uint16_t tcpsend_eventhandler(FAR struct net_driver_s *dev,
  160. FAR void *pvconn,
  161. FAR void *pvpriv, uint16_t flags)
  162. {
  163. FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)pvconn;
  164. FAR struct send_s *pstate = (FAR struct send_s *)pvpriv;
  165. /* The TCP socket is connected and, hence, should be bound to a device.
  166. * Make sure that the polling device is the one that we are bound to.
  167. */
  168. DEBUGASSERT(conn->dev != NULL);
  169. if (dev != conn->dev)
  170. {
  171. return flags;
  172. }
  173. ninfo("flags: %04x acked: %d sent: %d\n",
  174. flags, pstate->snd_acked, pstate->snd_sent);
  175. /* If this packet contains an acknowledgement, then update the count of
  176. * acknowledged bytes.
  177. */
  178. if ((flags & TCP_ACKDATA) != 0)
  179. {
  180. FAR struct tcp_hdr_s *tcp;
  181. /* Get the offset address of the TCP header */
  182. #ifdef CONFIG_NET_IPv4
  183. #ifdef CONFIG_NET_IPv6
  184. if (conn->domain == PF_INET)
  185. #endif
  186. {
  187. DEBUGASSERT(IFF_IS_IPv4(dev->d_flags));
  188. tcp = TCPIPv4BUF;
  189. }
  190. #endif /* CONFIG_NET_IPv4 */
  191. #ifdef CONFIG_NET_IPv6
  192. #ifdef CONFIG_NET_IPv4
  193. else
  194. #endif
  195. {
  196. DEBUGASSERT(IFF_IS_IPv6(dev->d_flags));
  197. tcp = TCPIPv6BUF;
  198. }
  199. #endif /* CONFIG_NET_IPv6 */
  200. /* The current acknowledgement number is the (relative) offset of the
  201. * next byte needed by the receiver. The snd_isn is the offset of the
  202. * first byte to send to the receiver. The difference is the number
  203. * of bytes to be acknowledged.
  204. */
  205. pstate->snd_acked = tcp_getsequence(tcp->ackno) - pstate->snd_isn;
  206. ninfo("ACK: acked=%d sent=%d buflen=%d\n",
  207. pstate->snd_acked, pstate->snd_sent, pstate->snd_buflen);
  208. /* Have all of the bytes in the buffer been sent and acknowledged? */
  209. if (pstate->snd_acked >= pstate->snd_buflen)
  210. {
  211. /* Yes. Then pstate->snd_buflen should hold the number of bytes
  212. * actually sent.
  213. */
  214. goto end_wait;
  215. }
  216. /* No.. fall through to send more data if necessary */
  217. }
  218. /* Check if we are being asked to retransmit data */
  219. else if ((flags & TCP_REXMIT) != 0)
  220. {
  221. /* Yes.. in this case, reset the number of bytes that have been sent
  222. * to the number of bytes that have been ACKed.
  223. */
  224. pstate->snd_sent = pstate->snd_acked;
  225. #if defined(CONFIG_NET_TCP_SPLIT)
  226. /* Reset the even/odd indicator to even since we need to
  227. * retransmit.
  228. */
  229. pstate->snd_odd = false;
  230. #endif
  231. /* Fall through to re-send data from the last that was ACKed */
  232. }
  233. /* Check for a loss of connection */
  234. else if ((flags & TCP_DISCONN_EVENTS) != 0)
  235. {
  236. FAR struct socket *psock = pstate->snd_sock;
  237. ninfo("Lost connection\n");
  238. /* We could get here recursively through the callback actions of
  239. * tcp_lost_connection(). So don't repeat that action if we have
  240. * already been disconnected.
  241. */
  242. DEBUGASSERT(psock != NULL);
  243. if (_SS_ISCONNECTED(psock->s_flags))
  244. {
  245. /* Report not connected */
  246. tcp_lost_connection(psock, pstate->snd_cb, flags);
  247. }
  248. pstate->snd_sent = -ENOTCONN;
  249. goto end_wait;
  250. }
  251. /* Check if the outgoing packet is available (it may have been claimed
  252. * by a sendto event serving a different thread).
  253. */
  254. #if 0 /* We can't really support multiple senders on the same TCP socket */
  255. else if (dev->d_sndlen > 0)
  256. {
  257. /* Another thread has beat us sending data, wait for the next poll */
  258. return flags;
  259. }
  260. #endif
  261. /* We get here if (1) not all of the data has been ACKed, (2) we have been
  262. * asked to retransmit data, (3) the connection is still healthy, and (4)
  263. * the outgoing packet is available for our use. In this case, we are
  264. * now free to send more data to receiver -- UNLESS the buffer contains
  265. * unprocessed incoming data. In that event, we will have to wait for the
  266. * next polling cycle.
  267. */
  268. if ((flags & TCP_NEWDATA) == 0 && pstate->snd_sent < pstate->snd_buflen)
  269. {
  270. uint32_t seqno;
  271. /* Get the amount of data that we can send in the next packet */
  272. uint32_t sndlen = pstate->snd_buflen - pstate->snd_sent;
  273. #if defined(CONFIG_NET_TCP_SPLIT)
  274. /* RFC 1122 states that a host may delay ACKing for up to 500ms but
  275. * must respond to every second segment). This logic here will trick
  276. * the RFC 1122 recipient into responding sooner. This logic will be
  277. * activated if:
  278. *
  279. * 1. An even number of packets has been send (where zero is an even
  280. * number),
  281. * 2. There is more data be sent (more than or equal to
  282. * CONFIG_NET_TCP_SPLIT_SIZE), but
  283. * 3. Not enough data for two packets.
  284. *
  285. * Then we will split the remaining, single packet into two partial
  286. * packets. This will stimulate the RFC 1122 peer to ACK sooner.
  287. *
  288. * Don't try to split very small packets (less than
  289. * CONFIG_NET_TCP_SPLIT_SIZE). Only the first even packet and the
  290. * last odd packets could have sndlen less than
  291. * CONFIG_NET_TCP_SPLIT_SIZE. The value of sndlen on the last even
  292. * packet is guaranteed to be at least MSS / 2 by the logic below.
  293. */
  294. if (sndlen >= CONFIG_NET_TCP_SPLIT_SIZE)
  295. {
  296. /* sndlen is the number of bytes remaining to be sent.
  297. * conn->mss will provide the number of bytes that can sent
  298. * in one packet. The difference, then, is the number of bytes
  299. * that would be sent in the next packet after this one.
  300. */
  301. int32_t next_sndlen = sndlen - conn->mss;
  302. /* Is this the even packet in the packet pair transaction? */
  303. if (!pstate->snd_odd)
  304. {
  305. /* next_sndlen <= 0 means that the entire remaining data
  306. * could fit into this single packet. This is condition
  307. * in which we must do the split.
  308. */
  309. if (next_sndlen <= 0)
  310. {
  311. /* Split so that there will be an odd packet. Here
  312. * we know that 0 < sndlen <= MSS
  313. */
  314. sndlen = (sndlen / 2) + 1;
  315. }
  316. }
  317. /* No... this is the odd packet in the packet pair transaction */
  318. else
  319. {
  320. /* Will there be another (even) packet after this one?
  321. * (next_sndlen > 0) Will the split condition occur on that
  322. * next, even packet? ((next_sndlen - conn->mss) < 0) If
  323. * so, then perform the split now to avoid the case where the
  324. * byte count is less than CONFIG_NET_TCP_SPLIT_SIZE on the
  325. * next pair.
  326. */
  327. if (next_sndlen > 0 && (next_sndlen - conn->mss) < 0)
  328. {
  329. /* Here, we know that sndlen must be MSS < sndlen <= 2*MSS
  330. * and so (sndlen / 2) is <= MSS.
  331. */
  332. sndlen /= 2;
  333. }
  334. }
  335. }
  336. /* Toggle the even/odd indicator */
  337. pstate->snd_odd ^= true;
  338. #endif /* CONFIG_NET_TCP_SPLIT */
  339. if (sndlen > conn->mss)
  340. {
  341. sndlen = conn->mss;
  342. }
  343. /* Check if we have "space" in the window */
  344. if ((pstate->snd_sent - pstate->snd_acked + sndlen) < conn->winsize)
  345. {
  346. /* Set the sequence number for this packet. NOTE: The network
  347. * updates sndseq on receipt of ACK *before* this function is
  348. * called. In that case sndseq will point to the next
  349. * unacknowledged byte (which might have already been sent). We
  350. * will overwrite the value of sndseq here before the packet is
  351. * sent.
  352. */
  353. seqno = pstate->snd_sent + pstate->snd_isn;
  354. ninfo("SEND: sndseq %08x->%08x\n", conn->sndseq, seqno);
  355. tcp_setsequence(conn->sndseq, seqno);
  356. #ifdef NEED_IPDOMAIN_SUPPORT
  357. /* If both IPv4 and IPv6 support are enabled, then we will need to
  358. * select which one to use when generating the outgoing packet.
  359. * If only one domain is selected, then the setup is already in
  360. * place and we need do nothing.
  361. */
  362. tcpsend_ipselect(dev, conn);
  363. #endif
  364. /* Then set-up to send that amount of data. (this won't actually
  365. * happen until the polling cycle completes).
  366. */
  367. devif_send(dev, &pstate->snd_buffer[pstate->snd_sent], sndlen);
  368. /* Update the amount of data sent (but not necessarily ACKed) */
  369. pstate->snd_sent += sndlen;
  370. ninfo("SEND: acked=%d sent=%d buflen=%d\n",
  371. pstate->snd_acked, pstate->snd_sent, pstate->snd_buflen);
  372. }
  373. }
  374. /* Continue waiting */
  375. return flags;
  376. end_wait:
  377. /* Do not allow any further callbacks */
  378. pstate->snd_cb->flags = 0;
  379. pstate->snd_cb->priv = NULL;
  380. pstate->snd_cb->event = NULL;
  381. /* There are no outstanding, unacknowledged bytes */
  382. conn->tx_unacked = 0;
  383. /* Wake up the waiting thread */
  384. nxsem_post(&pstate->snd_sem);
  385. return flags;
  386. }
  387. /****************************************************************************
  388. * Name: send_txnotify
  389. *
  390. * Description:
  391. * Notify the appropriate device driver that we are have data ready to
  392. * be send (TCP)
  393. *
  394. * Input Parameters:
  395. * psock - Socket state structure
  396. * conn - The TCP connection structure
  397. *
  398. * Returned Value:
  399. * None
  400. *
  401. ****************************************************************************/
  402. static inline void send_txnotify(FAR struct socket *psock,
  403. FAR struct tcp_conn_s *conn)
  404. {
  405. #ifdef CONFIG_NET_IPv4
  406. #ifdef CONFIG_NET_IPv6
  407. /* If both IPv4 and IPv6 support are enabled, then we will need to select
  408. * the device driver using the appropriate IP domain.
  409. */
  410. if (psock->s_domain == PF_INET)
  411. #endif
  412. {
  413. /* Notify the device driver that send data is available */
  414. netdev_ipv4_txnotify(conn->u.ipv4.laddr, conn->u.ipv4.raddr);
  415. }
  416. #endif /* CONFIG_NET_IPv4 */
  417. #ifdef CONFIG_NET_IPv6
  418. #ifdef CONFIG_NET_IPv4
  419. else /* if (psock->s_domain == PF_INET6) */
  420. #endif /* CONFIG_NET_IPv4 */
  421. {
  422. /* Notify the device driver that send data is available */
  423. DEBUGASSERT(psock->s_domain == PF_INET6);
  424. netdev_ipv6_txnotify(conn->u.ipv6.laddr, conn->u.ipv6.raddr);
  425. }
  426. #endif /* CONFIG_NET_IPv6 */
  427. }
  428. /****************************************************************************
  429. * Public Functions
  430. ****************************************************************************/
  431. /****************************************************************************
  432. * Name: psock_tcp_send
  433. *
  434. * Description:
  435. * psock_tcp_send() call may be used only when the TCP socket is in a
  436. * connected state (so that the intended recipient is known).
  437. *
  438. * Input Parameters:
  439. * psock An instance of the internal socket structure.
  440. * buf Data to send
  441. * len Length of data to send
  442. * flags Send flags
  443. *
  444. * Returned Value:
  445. * On success, returns the number of characters sent. On error,
  446. * a negated errno value is returned.
  447. *
  448. * EAGAIN or EWOULDBLOCK
  449. * The socket is marked non-blocking and the requested operation
  450. * would block.
  451. * EBADF
  452. * An invalid descriptor was specified.
  453. * ECONNRESET
  454. * Connection reset by peer.
  455. * EDESTADDRREQ
  456. * The socket is not connection-mode, and no peer address is set.
  457. * EFAULT
  458. * An invalid user space address was specified for a parameter.
  459. * EINTR
  460. * A signal occurred before any data was transmitted.
  461. * EINVAL
  462. * Invalid argument passed.
  463. * EISCONN
  464. * The connection-mode socket was connected already but a recipient
  465. * was specified. (Now either this error is returned, or the recipient
  466. * specification is ignored.)
  467. * EMSGSIZE
  468. * The socket type requires that message be sent atomically, and the
  469. * size of the message to be sent made this impossible.
  470. * ENOBUFS
  471. * The output queue for a network interface was full. This generally
  472. * indicates that the interface has stopped sending, but may be
  473. * caused by transient congestion.
  474. * ENOMEM
  475. * No memory available.
  476. * ENOTCONN
  477. * The socket is not connected, and no target has been given.
  478. * ENOTSOCK
  479. * The argument s is not a socket.
  480. * EPIPE
  481. * The local end has been shut down on a connection oriented socket.
  482. * In this case the process will also receive a SIGPIPE unless
  483. * MSG_NOSIGNAL is set.
  484. *
  485. ****************************************************************************/
  486. ssize_t psock_tcp_send(FAR struct socket *psock,
  487. FAR const void *buf, size_t len, int flags)
  488. {
  489. FAR struct tcp_conn_s *conn;
  490. struct send_s state;
  491. int ret = OK;
  492. /* Verify that the sockfd corresponds to valid, allocated socket */
  493. if (psock == NULL || psock->s_crefs <= 0)
  494. {
  495. nerr("ERROR: Invalid socket\n");
  496. ret = -EBADF;
  497. goto errout;
  498. }
  499. /* If this is an un-connected socket, then return ENOTCONN */
  500. if (psock->s_type != SOCK_STREAM || !_SS_ISCONNECTED(psock->s_flags))
  501. {
  502. nerr("ERROR: Not connected\n");
  503. ret = -ENOTCONN;
  504. goto errout;
  505. }
  506. /* Make sure that we have the IP address mapping */
  507. conn = (FAR struct tcp_conn_s *)psock->s_conn;
  508. DEBUGASSERT(conn);
  509. #if defined(CONFIG_NET_ARP_SEND) || defined(CONFIG_NET_ICMPv6_NEIGHBOR)
  510. #ifdef CONFIG_NET_ARP_SEND
  511. #ifdef CONFIG_NET_ICMPv6_NEIGHBOR
  512. if (psock->s_domain == PF_INET)
  513. #endif
  514. {
  515. /* Make sure that the IP address mapping is in the ARP table */
  516. ret = arp_send(conn->u.ipv4.raddr);
  517. }
  518. #endif /* CONFIG_NET_ARP_SEND */
  519. #ifdef CONFIG_NET_ICMPv6_NEIGHBOR
  520. #ifdef CONFIG_NET_ARP_SEND
  521. else
  522. #endif
  523. {
  524. /* Make sure that the IP address mapping is in the Neighbor Table */
  525. ret = icmpv6_neighbor(conn->u.ipv6.raddr);
  526. }
  527. #endif /* CONFIG_NET_ICMPv6_NEIGHBOR */
  528. /* Did we successfully get the address mapping? */
  529. if (ret < 0)
  530. {
  531. nerr("ERROR: Not reachable\n");
  532. ret = -ENETUNREACH;
  533. goto errout;
  534. }
  535. #endif /* CONFIG_NET_ARP_SEND || CONFIG_NET_ICMPv6_NEIGHBOR */
  536. /* Perform the TCP send operation */
  537. /* Initialize the state structure. This is done with the network
  538. * locked because we don't want anything to happen until we are
  539. * ready.
  540. */
  541. net_lock();
  542. memset(&state, 0, sizeof(struct send_s));
  543. /* This semaphore is used for signaling and, hence, should not have
  544. * priority inheritance enabled.
  545. */
  546. nxsem_init(&state.snd_sem, 0, 0); /* Doesn't really fail */
  547. nxsem_set_protocol(&state.snd_sem, SEM_PRIO_NONE);
  548. state.snd_sock = psock; /* Socket descriptor to use */
  549. state.snd_buflen = len; /* Number of bytes to send */
  550. state.snd_buffer = buf; /* Buffer to send from */
  551. if (len > 0)
  552. {
  553. /* Allocate resources to receive a callback */
  554. ret = -ENOMEM; /* Assume allocation failure */
  555. state.snd_cb = tcp_callback_alloc(conn);
  556. if (state.snd_cb)
  557. {
  558. /* Get the initial sequence number that will be used */
  559. state.snd_isn = tcp_getsequence(conn->sndseq);
  560. /* There is no outstanding, unacknowledged data after this
  561. * initial sequence number.
  562. */
  563. conn->tx_unacked = 0;
  564. /* Set up the callback in the connection */
  565. state.snd_cb->flags = (TCP_ACKDATA | TCP_REXMIT | TCP_POLL |
  566. TCP_DISCONN_EVENTS);
  567. state.snd_cb->priv = (FAR void *)&state;
  568. state.snd_cb->event = tcpsend_eventhandler;
  569. /* Notify the device driver of the availability of TX data */
  570. send_txnotify(psock, conn);
  571. /* Wait for the send to complete or an error to occur: NOTES:
  572. * net_lockedwait will also terminate if a signal is received.
  573. */
  574. for (; ; )
  575. {
  576. uint32_t acked = state.snd_acked;
  577. ret = net_timedwait(&state.snd_sem,
  578. _SO_TIMEOUT(psock->s_sndtimeo));
  579. if (ret != -ETIMEDOUT || acked == state.snd_acked)
  580. {
  581. break; /* Timeout without any progress */
  582. }
  583. }
  584. /* Make sure that no further events are processed */
  585. tcp_callback_free(conn, state.snd_cb);
  586. }
  587. }
  588. nxsem_destroy(&state.snd_sem);
  589. net_unlock();
  590. /* Check for a errors. Errors are signalled by negative errno values
  591. * for the send length
  592. */
  593. if (state.snd_sent < 0)
  594. {
  595. ret = state.snd_sent;
  596. goto errout;
  597. }
  598. /* If net_timedwait failed, then we were probably reawakened by a signal.
  599. * In this case, net_timedwait will have returned negated errno
  600. * appropriately.
  601. */
  602. if (ret < 0)
  603. {
  604. goto errout;
  605. }
  606. /* Return the number of bytes actually sent */
  607. ret = state.snd_sent;
  608. errout:
  609. return ret;
  610. }
  611. /****************************************************************************
  612. * Name: psock_tcp_cansend
  613. *
  614. * Description:
  615. * psock_tcp_cansend() returns a value indicating if a write to the socket
  616. * would block. It is still possible that the write may block if another
  617. * write occurs first.
  618. *
  619. * Input Parameters:
  620. * psock An instance of the internal socket structure.
  621. *
  622. * Returned Value:
  623. * OK (Always can send).
  624. *
  625. * Assumptions:
  626. * None
  627. *
  628. ****************************************************************************/
  629. int psock_tcp_cansend(FAR struct socket *psock)
  630. {
  631. return OK;
  632. }
  633. #endif /* CONFIG_NET && CONFIG_NET_TCP && !CONFIG_NET_TCP_WRITE_BUFFERS */