2015-01-27 18:51:24 +01:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* net/local/local_recvutils.c
|
2015-01-27 18:51:24 +01:00
|
|
|
*
|
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
|
2015-01-27 18:51:24 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-01-27 18:51:24 +01: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.
|
2015-01-27 18:51:24 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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]
|
2021-10-15 12:02:21 +02:00
|
|
|
* Zero means *len[in] is zero,
|
|
|
|
* or the sending side has closed the FIFO
|
2021-07-21 09:41:49 +02:00
|
|
|
* once - Flag to indicate the buf may only be read once
|
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
|
2021-10-15 12:02:21 +02:00
|
|
|
* failure.
|
2015-01-27 18:51:24 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-07-21 09:41:49 +02:00
|
|
|
int local_fifo_read(FAR struct file *filep, FAR uint8_t *buf,
|
|
|
|
size_t *len, bool once)
|
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;
|
2021-02-15 09:08:51 +01:00
|
|
|
nerr("ERROR: file_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.
|
|
|
|
*/
|
|
|
|
|
2021-10-15 12:02:21 +02:00
|
|
|
break;
|
2015-01-27 18:51:24 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-30 20:29:56 +01:00
|
|
|
DEBUGASSERT(nread <= remaining);
|
2015-01-28 15:39:48 +01:00
|
|
|
remaining -= nread;
|
|
|
|
buf += nread;
|
2021-07-21 09:41:49 +02:00
|
|
|
|
|
|
|
if (once)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
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);
|
2021-07-21 09:41:49 +02:00
|
|
|
ret = local_fifo_read(filep, &sync, &readlen, false);
|
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);
|
2021-07-21 09:41:49 +02:00
|
|
|
ret = local_fifo_read(filep, &sync, &readlen, false);
|
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);
|
2021-07-21 09:41:49 +02:00
|
|
|
ret = local_fifo_read(filep, (FAR uint8_t *)&pktlen, &readlen, false);
|
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 */
|