2015-01-25 14:36:16 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/local/local_conn.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>
|
|
|
|
#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>
|
|
|
|
|
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-01-25 19:37:40 +01:00
|
|
|
void local_initialize(void)
|
|
|
|
{
|
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
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
conn->lc_infd = -1;
|
|
|
|
conn->lc_outfd = -1;
|
2015-01-31 14:58:51 +01:00
|
|
|
#ifdef CONFIG_NET_LOCAL_STREAM
|
2015-01-25 19:37:40 +01:00
|
|
|
sem_init(&conn->lc_waitsem, 0, 0);
|
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
|
|
|
|
2015-01-26 20:30:27 +01:00
|
|
|
if (conn->lc_infd >= 0)
|
2015-01-25 19:37:40 +01:00
|
|
|
{
|
2015-01-26 20:30:27 +01:00
|
|
|
close(conn->lc_infd);
|
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 */
|
|
|
|
|
|
|
|
if (conn->lc_outfd >= 0)
|
|
|
|
{
|
|
|
|
close(conn->lc_outfd);
|
|
|
|
}
|
|
|
|
|
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);
|
2015-01-25 19:37:40 +01:00
|
|
|
sem_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 */
|