2015-01-27 18:51:24 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/local/local_recvpacket.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-10-11 20:16:00 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
#include "local/local.h"
|
|
|
|
|
2017-10-11 20:16:00 +02:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL)
|
|
|
|
|
2015-01-27 18:51:24 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_fifo_read
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read a data from the read-only FIFO.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-11-02 15:23:38 +01:00
|
|
|
* filep - File structure of write-only FIFO.
|
|
|
|
* buf - Local to store the received data
|
|
|
|
* len - Length of data to receive [in]
|
|
|
|
* Length of data actually received [out]
|
2015-01-27 18:51:24 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-01-27 18:51:24 +01:00
|
|
|
* Zero is returned on success; a negated errno value is returned on any
|
|
|
|
* failure. If -ECONNRESET is received, then the sending side has closed
|
|
|
|
* the FIFO. In this case, the returned data may still be valid (if the
|
|
|
|
* returned len > 0).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf, size_t *len)
|
2015-01-27 18:51:24 +01:00
|
|
|
{
|
2015-01-28 15:39:48 +01:00
|
|
|
ssize_t remaining;
|
2015-01-27 18:51:24 +01:00
|
|
|
ssize_t nread;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(buf && len);
|
|
|
|
|
2015-01-28 15:39:48 +01:00
|
|
|
remaining = *len;
|
|
|
|
while (remaining > 0)
|
2015-01-27 18:51:24 +01:00
|
|
|
{
|
2017-11-02 15:23:38 +01:00
|
|
|
nread = file_read(filep, buf, remaining);
|
2015-01-27 18:51:24 +01:00
|
|
|
if (nread < 0)
|
|
|
|
{
|
2017-10-11 20:16:00 +02:00
|
|
|
if (nread != -EINTR)
|
2015-01-27 18:51:24 +01:00
|
|
|
{
|
2017-10-11 20:16:00 +02:00
|
|
|
ret = (int)nread;
|
|
|
|
nerr("ERROR: nx_read() failed: %d\n", ret);
|
2015-01-27 18:51:24 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
ninfo("Ignoring signal\n");
|
2015-01-27 18:51:24 +01:00
|
|
|
}
|
|
|
|
else if (nread == 0)
|
|
|
|
{
|
|
|
|
/* The FIFO returns zero if the sending side of the connection
|
|
|
|
* has closed the FIFO.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = -ECONNRESET;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-30 20:29:56 +01:00
|
|
|
DEBUGASSERT(nread <= remaining);
|
2015-01-28 15:39:48 +01:00
|
|
|
remaining -= nread;
|
|
|
|
buf += nread;
|
2015-01-27 18:51:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout:
|
2015-01-28 15:39:48 +01:00
|
|
|
*len -= remaining;
|
2015-01-27 18:51:24 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_sync
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read a sync bytes until the start of the packet is found.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-11-02 15:23:38 +01:00
|
|
|
* filep - File structure of write-only FIFO.
|
2015-01-27 18:51:24 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-01-27 18:51:24 +01:00
|
|
|
* The non-zero size of the following packet is returned on success; a
|
|
|
|
* negated errno value is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
int local_sync(FAR struct file *filep)
|
2015-01-27 18:51:24 +01:00
|
|
|
{
|
|
|
|
size_t readlen;
|
|
|
|
uint16_t pktlen;
|
|
|
|
uint8_t sync;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Loop until a valid pre-amble is encountered: SYNC bytes followed
|
|
|
|
* by one END byte.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Read until we encounter a sync byte */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
readlen = sizeof(uint8_t);
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = local_fifo_read(filep, &sync, &readlen);
|
2015-01-27 18:51:24 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to read sync bytes: %d\n", ret);
|
2015-01-27 18:51:24 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (sync != LOCAL_SYNC_BYTE);
|
|
|
|
|
|
|
|
/* Then read to the end of the SYNC sequence */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
readlen = sizeof(uint8_t);
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = local_fifo_read(filep, &sync, &readlen);
|
2015-01-27 18:51:24 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
nerr("ERROR: Failed to read sync bytes: %d\n", ret);
|
2015-01-27 18:51:24 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (sync == LOCAL_SYNC_BYTE);
|
|
|
|
}
|
|
|
|
while (sync != LOCAL_END_BYTE);
|
|
|
|
|
|
|
|
/* Then read the packet length */
|
|
|
|
|
|
|
|
readlen = sizeof(uint16_t);
|
2017-11-02 15:23:38 +01:00
|
|
|
ret = local_fifo_read(filep, (FAR uint8_t *)&pktlen, &readlen);
|
2015-01-27 18:51:24 +01:00
|
|
|
return ret < 0 ? ret : pktlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_getaddr
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the Unix domain address of a connection.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2015-01-27 18:51:24 +01:00
|
|
|
* conn - The connection
|
|
|
|
* addr - The location to return the address
|
|
|
|
* addrlen - The size of the memory allocated by the caller to receive the
|
|
|
|
* address.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2015-01-27 18:51:24 +01:00
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int local_getaddr(FAR struct local_conn_s *conn, FAR struct sockaddr *addr,
|
|
|
|
FAR socklen_t *addrlen)
|
|
|
|
{
|
|
|
|
FAR struct sockaddr_un *unaddr;
|
|
|
|
int totlen;
|
|
|
|
int pathlen;
|
|
|
|
|
|
|
|
DEBUGASSERT(conn && addr && addrlen && *addrlen >= sizeof(sa_family_t));
|
|
|
|
|
|
|
|
/* Get the length of the path (minus the NUL terminator) and the length
|
|
|
|
* of the whole Unix domain address.
|
|
|
|
*/
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
pathlen = strnlen(conn->lc_path, UNIX_PATH_MAX - 1);
|
2015-01-27 18:51:24 +01:00
|
|
|
totlen = sizeof(sa_family_t) + pathlen + 1;
|
|
|
|
|
|
|
|
/* If the length of the whole Unix domain address is larger than the
|
|
|
|
* buffer provided by the caller, then truncate the address to fit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (totlen > *addrlen)
|
|
|
|
{
|
|
|
|
pathlen -= (totlen - *addrlen);
|
|
|
|
totlen = *addrlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the Unix domain address */
|
|
|
|
|
|
|
|
unaddr = (FAR struct sockaddr_un *)addr;
|
|
|
|
unaddr->sun_family = AF_LOCAL;
|
|
|
|
memcpy(unaddr->sun_path, conn->lc_path, pathlen);
|
|
|
|
unaddr->sun_path[pathlen] = '\0';
|
|
|
|
|
|
|
|
/* Return the Unix domain address size */
|
|
|
|
|
|
|
|
*addrlen = totlen;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_LOCAL */
|