2015-01-25 14:36:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/local/local_conn.c
|
|
|
|
*
|
2016-11-03 19:17:02 +01:00
|
|
|
* Copyright (C) 2015-2016 Gregory Nutt. All rights reserved.
|
2015-01-25 14:36:16 +01:00
|
|
|
* 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>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_LOCAL)
|
|
|
|
|
2015-01-25 19:37:40 +01:00
|
|
|
#include <semaphore.h>
|
2015-01-25 15:33:39 +01:00
|
|
|
#include <string.h>
|
2015-01-25 14:36:16 +01:00
|
|
|
#include <assert.h>
|
2015-01-25 19:37:40 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <queue.h>
|
2015-01-25 14:36:16 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2015-01-25 15:33:39 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2016-11-03 19:17:02 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2015-01-25 15:33:39 +01:00
|
|
|
|
2015-01-25 14:36:16 +01:00
|
|
|
#include "local/local.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the local, Unix domain connection structures. Called once
|
|
|
|
* and only from the common network initialization logic.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
void local_initialize(void)
|
2015-01-25 19:37:40 +01:00
|
|
|
{
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-25 19:37:40 +01:00
|
|
|
dq_init(&g_local_listeners);
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif
|
2015-01-25 19:37:40 +01:00
|
|
|
}
|
2015-01-25 14:36:16 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_alloc()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a new, uninitialized Unix domain socket connection structure.
|
|
|
|
* This is normally something done by the implementation of the socket()
|
|
|
|
* API
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct local_conn_s *local_alloc(void)
|
|
|
|
{
|
2015-01-25 15:33:39 +01:00
|
|
|
FAR struct local_conn_s *conn =
|
|
|
|
(FAR struct local_conn_s *)kmm_zalloc(sizeof(struct local_conn_s));
|
|
|
|
|
|
|
|
if (conn)
|
|
|
|
{
|
2015-01-25 19:37:40 +01:00
|
|
|
/* Initialize non-zero elements the new connection structure */
|
2015-01-25 15:33:39 +01:00
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
conn->lc_infile.f_inode = NULL;
|
|
|
|
conn->lc_outfile.f_inode = NULL;
|
2016-11-03 19:17:02 +01:00
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2016-11-03 19:17:02 +01:00
|
|
|
/* This semaphore is used for signaling and, hence, should not have
|
|
|
|
* priority inheritance enabled.
|
|
|
|
*/
|
|
|
|
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&conn->lc_waitsem, 0, 0);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_setprotocol(&conn->lc_waitsem, SEM_PRIO_NONE);
|
2016-11-03 19:17:02 +01:00
|
|
|
|
2015-05-12 15:41:12 +02:00
|
|
|
#ifdef HAVE_LOCAL_POLL
|
|
|
|
memset(conn->lc_accept_fds, 0, sizeof(conn->lc_accept_fds));
|
|
|
|
#endif
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif
|
2015-01-25 15:33:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn;
|
2015-01-25 14:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: local_free()
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a packet Unix domain connection structure that is no longer in use.
|
|
|
|
* This should be done by the implementation of close().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void local_free(FAR struct local_conn_s *conn)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(conn != NULL);
|
2015-01-25 19:37:40 +01:00
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Make sure that the read-only FIFO is closed */
|
2015-01-25 19:37:40 +01:00
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
if (conn->lc_infile.f_inode != NULL)
|
2015-01-25 19:37:40 +01:00
|
|
|
{
|
2018-09-15 21:04:56 +02:00
|
|
|
file_close(&conn->lc_infile);
|
2017-11-02 15:23:38 +01:00
|
|
|
conn->lc_infile.f_inode = NULL;
|
2015-01-25 19:37:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Make sure that the write-only FIFO is closed */
|
|
|
|
|
2017-11-02 15:23:38 +01:00
|
|
|
if (conn->lc_outfile.f_inode != NULL)
|
2015-01-26 20:30:27 +01:00
|
|
|
{
|
2018-09-15 21:04:56 +02:00
|
|
|
file_close(&conn->lc_outfile);
|
2017-11-02 15:23:38 +01:00
|
|
|
conn->lc_outfile.f_inode = NULL;
|
2015-01-26 20:30:27 +01:00
|
|
|
}
|
|
|
|
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-26 20:30:27 +01:00
|
|
|
/* Destroy all FIFOs associted with the connection */
|
|
|
|
|
2015-01-30 16:28:55 +01:00
|
|
|
local_release_fifos(conn);
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_destroy(&conn->lc_waitsem);
|
2015-01-31 14:58:51 +01:00
|
|
|
#endif
|
2015-01-25 19:37:40 +01:00
|
|
|
|
|
|
|
/* And free the connection structure */
|
|
|
|
|
|
|
|
kmm_free(conn);
|
2015-01-25 14:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_LOCAL */
|