2014-06-24 16:03:44 +02:00
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* net/pkt/pkt_sendmsg.c
|
2014-06-24 16:03:44 +02:00
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-06-24 16:03:44 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-24 16:03:44 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-06-24 16:03:44 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_PKT)
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
|
2016-11-03 19:17:02 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-07-05 00:38:51 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2014-06-24 16:03:44 +02:00
|
|
|
|
2014-06-27 17:56:45 +02:00
|
|
|
#include "netdev/netdev.h"
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-07-05 00:38:51 +02:00
|
|
|
#include "socket/socket.h"
|
2014-06-25 18:34:52 +02:00
|
|
|
#include "pkt/pkt.h"
|
2014-06-24 16:03:44 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This structure holds the state of the send operation until it can be
|
2018-06-23 22:09:06 +02:00
|
|
|
* operated upon by the event handler.
|
2014-06-24 16:03:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct send_s
|
|
|
|
{
|
2014-06-29 20:59:34 +02:00
|
|
|
FAR struct socket *snd_sock; /* Points to the parent socket structure */
|
|
|
|
FAR struct devif_callback_s *snd_cb; /* Reference to callback instance */
|
|
|
|
sem_t snd_sem; /* Used to wake up the waiting thread */
|
|
|
|
FAR const uint8_t *snd_buffer; /* Points to the buffer of data to send */
|
|
|
|
size_t snd_buflen; /* Number of bytes in the buffer to send */
|
|
|
|
ssize_t snd_sent; /* The number of bytes sent */
|
2014-06-24 16:03:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-08-29 22:08:04 +02:00
|
|
|
* Name: psock_send_eventhandler
|
2014-06-24 16:03:44 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-29 22:08:04 +02:00
|
|
|
static uint16_t psock_send_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *pvpriv, uint16_t flags)
|
2014-06-24 16:03:44 +02:00
|
|
|
{
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR struct send_s *pstate = pvpriv;
|
2014-06-24 16:03:44 +02:00
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
ninfo("flags: %04x sent: %zd\n", flags, pstate->snd_sent);
|
2014-06-24 16:03:44 +02:00
|
|
|
|
|
|
|
if (pstate)
|
|
|
|
{
|
2014-08-18 21:42:51 +02:00
|
|
|
/* Check if the outgoing packet is available. It may have been claimed
|
2018-06-23 22:09:06 +02:00
|
|
|
* by a send event handler serving a different thread -OR- if the
|
|
|
|
* output buffer currently contains unprocessed incoming data. In
|
|
|
|
* these cases we will just have to wait for the next polling cycle.
|
2014-06-24 16:03:44 +02:00
|
|
|
*/
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
if (dev->d_sndlen > 0 || (flags & PKT_NEWDATA) != 0)
|
2014-06-24 16:03:44 +02:00
|
|
|
{
|
|
|
|
/* Another thread has beat us sending data or the buffer is busy,
|
|
|
|
* Check for a timeout. If not timed out, wait for the next
|
|
|
|
* polling cycle and check again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* No timeout. Just wait for the next polling cycle */
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It looks like we are good to send the data */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Copy the packet data into the device packet buffer and send it */
|
|
|
|
|
2023-05-29 23:26:19 +02:00
|
|
|
int ret = devif_send(dev, pstate->snd_buffer,
|
2024-06-28 12:57:45 +02:00
|
|
|
pstate->snd_buflen, -NET_LL_HDRLEN(dev));
|
2023-05-29 23:26:19 +02:00
|
|
|
if (ret <= 0)
|
2022-12-07 10:44:09 +01:00
|
|
|
{
|
2023-05-29 23:26:19 +02:00
|
|
|
pstate->snd_sent = ret;
|
|
|
|
goto end_wait;
|
2022-12-07 10:44:09 +01:00
|
|
|
}
|
|
|
|
|
2024-08-19 12:52:20 +02:00
|
|
|
dev->d_len = dev->d_sndlen;
|
2014-06-24 16:03:44 +02:00
|
|
|
pstate->snd_sent = pstate->snd_buflen;
|
2014-08-18 21:42:51 +02:00
|
|
|
|
|
|
|
/* Make sure no ARP request overwrites this ARP request. This
|
|
|
|
* flag will be cleared in arp_out().
|
|
|
|
*/
|
|
|
|
|
2015-01-20 22:59:52 +01:00
|
|
|
IFF_SET_NOARP(dev->d_flags);
|
2014-06-24 16:03:44 +02:00
|
|
|
}
|
|
|
|
|
2023-05-29 23:26:19 +02:00
|
|
|
end_wait:
|
|
|
|
|
2014-06-24 16:03:44 +02:00
|
|
|
/* Don't allow any further call backs. */
|
|
|
|
|
|
|
|
pstate->snd_cb->flags = 0;
|
|
|
|
pstate->snd_cb->priv = NULL;
|
|
|
|
pstate->snd_cb->event = NULL;
|
|
|
|
|
|
|
|
/* Wake up the waiting thread */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&pstate->snd_sem);
|
2014-06-24 16:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-12-01 07:55:16 +01:00
|
|
|
* Name: pkt_sendmsg
|
2014-06-24 16:03:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-12-01 07:55:16 +01:00
|
|
|
* The pkt_sendmsg() call may be used only when the packet socket is in
|
2020-02-13 00:17:37 +01:00
|
|
|
* a connected state (so that the intended recipient is known).
|
2014-06-24 16:03:44 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-06-24 16:03:44 +02:00
|
|
|
* psock An instance of the internal socket structure.
|
2020-12-01 07:55:16 +01:00
|
|
|
* msg Message to send
|
|
|
|
* flags Send flags
|
2014-06-24 16:03:44 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-12-01 07:55:16 +01:00
|
|
|
* On success, returns the number of characters sent. On error, a negated
|
|
|
|
* errno value is returned (see sendmsg() for the complete list of return
|
|
|
|
* values.
|
2014-06-24 16:03:44 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
ssize_t pkt_sendmsg(FAR struct socket *psock, FAR struct msghdr *msg,
|
|
|
|
int flags)
|
2014-06-24 16:03:44 +02:00
|
|
|
{
|
2020-12-01 07:55:16 +01:00
|
|
|
FAR const void *buf = msg->msg_iov->iov_base;
|
|
|
|
size_t len = msg->msg_iov->iov_len;
|
2015-05-28 20:40:05 +02:00
|
|
|
FAR struct net_driver_s *dev;
|
2014-06-24 16:03:44 +02:00
|
|
|
struct send_s state;
|
|
|
|
int ret = OK;
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
/* Validity check, only single iov supported */
|
|
|
|
|
|
|
|
if (msg->msg_iovlen != 1)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg->msg_name != NULL)
|
|
|
|
{
|
|
|
|
/* pkt_sendto */
|
|
|
|
|
|
|
|
nerr("ERROR: sendto() not supported for raw packet sockets\n");
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
2014-06-24 16:03:44 +02:00
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
if (psock == NULL || psock->s_conn == NULL)
|
2014-06-24 16:03:44 +02:00
|
|
|
{
|
2017-09-30 14:41:56 +02:00
|
|
|
return -EBADF;
|
2014-06-24 16:03:44 +02:00
|
|
|
}
|
|
|
|
|
2020-12-01 07:55:16 +01:00
|
|
|
/* Only SOCK_RAW is supported */
|
|
|
|
|
2024-01-23 09:52:30 +01:00
|
|
|
if (psock->s_type != SOCK_RAW)
|
2020-12-01 07:55:16 +01:00
|
|
|
{
|
|
|
|
/* EDESTADDRREQ. Signifies that the socket is not connection-mode and
|
|
|
|
* no peer address is set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return -EDESTADDRREQ;
|
|
|
|
}
|
|
|
|
|
2015-05-28 20:40:05 +02:00
|
|
|
/* Get the device driver that will service this transfer */
|
|
|
|
|
2023-05-08 16:11:04 +02:00
|
|
|
dev = pkt_find_device(psock->s_conn);
|
2015-05-28 20:40:05 +02:00
|
|
|
if (dev == NULL)
|
|
|
|
{
|
2017-09-30 14:41:56 +02:00
|
|
|
return -ENODEV;
|
2015-05-28 20:40:05 +02:00
|
|
|
}
|
|
|
|
|
2014-06-24 16:03:44 +02:00
|
|
|
/* Perform the send operation */
|
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
/* Initialize the state structure. This is done with the network locked
|
2018-06-23 22:09:06 +02:00
|
|
|
* because we don't want anything to happen until we are ready.
|
2014-06-24 16:03:44 +02:00
|
|
|
*/
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2014-06-24 16:03:44 +02:00
|
|
|
memset(&state, 0, sizeof(struct send_s));
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&state.snd_sem, 0, 0); /* Doesn't really fail */
|
2016-11-03 19:17:02 +01:00
|
|
|
|
2014-06-24 16:03:44 +02:00
|
|
|
state.snd_sock = psock; /* Socket descriptor to use */
|
|
|
|
state.snd_buflen = len; /* Number of bytes to send */
|
|
|
|
state.snd_buffer = buf; /* Buffer to send from */
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
2023-05-08 16:11:04 +02:00
|
|
|
FAR struct pkt_conn_s *conn = psock->s_conn;
|
2014-06-24 16:03:44 +02:00
|
|
|
|
|
|
|
/* Allocate resource to receive a callback */
|
|
|
|
|
2015-05-28 20:40:05 +02:00
|
|
|
state.snd_cb = pkt_callback_alloc(dev, conn);
|
2014-06-24 16:03:44 +02:00
|
|
|
if (state.snd_cb)
|
|
|
|
{
|
|
|
|
/* Set up the callback in the connection */
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
state.snd_cb->flags = PKT_POLL;
|
2015-10-08 23:10:04 +02:00
|
|
|
state.snd_cb->priv = (FAR void *)&state;
|
2017-08-29 22:08:04 +02:00
|
|
|
state.snd_cb->event = psock_send_eventhandler;
|
2014-06-24 16:03:44 +02:00
|
|
|
|
2015-05-28 20:40:05 +02:00
|
|
|
/* Notify the device driver that new TX data is available. */
|
2014-06-24 16:03:44 +02:00
|
|
|
|
2015-05-28 20:40:05 +02:00
|
|
|
netdev_txnotify_dev(dev);
|
2014-06-24 16:03:44 +02:00
|
|
|
|
2018-06-23 22:09:06 +02:00
|
|
|
/* Wait for the send to complete or an error to occur.
|
2023-01-13 07:51:38 +01:00
|
|
|
* net_sem_wait will also terminate if a signal is received.
|
2014-06-24 16:03:44 +02:00
|
|
|
*/
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
ret = net_sem_wait(&state.snd_sem);
|
2014-06-24 16:03:44 +02:00
|
|
|
|
2018-06-23 22:09:06 +02:00
|
|
|
/* Make sure that no further events are processed */
|
2014-06-24 16:03:44 +02:00
|
|
|
|
2015-05-28 20:40:05 +02:00
|
|
|
pkt_callback_free(dev, conn, state.snd_cb);
|
2014-06-24 16:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&state.snd_sem);
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2014-06-24 16:03:44 +02:00
|
|
|
|
2021-01-15 10:01:00 +01:00
|
|
|
/* Check for errors. Errors are signalled by negative errno values
|
2014-06-24 16:03:44 +02:00
|
|
|
* for the send length
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (state.snd_sent < 0)
|
|
|
|
{
|
2017-09-30 14:41:56 +02:00
|
|
|
return state.snd_sent;
|
2014-06-24 16:03:44 +02:00
|
|
|
}
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
/* If net_sem_wait failed, then we were probably reawakened by a signal.
|
|
|
|
* In this case, net_sem_wait will have returned negated errno
|
2020-02-13 00:17:37 +01:00
|
|
|
* appropriately.
|
2014-06-24 16:03:44 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-09-30 14:41:56 +02:00
|
|
|
return ret;
|
2014-06-24 16:03:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the number of bytes actually sent */
|
|
|
|
|
|
|
|
return state.snd_sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_PKT */
|