2007-09-01 20:06:15 +02:00
|
|
|
/****************************************************************************
|
2014-06-29 01:25:18 +02:00
|
|
|
* net/socket/net_sockets.c
|
2007-09-01 20:06:15 +02:00
|
|
|
*
|
2016-02-25 02:02:51 +01:00
|
|
|
* Copyright (C) 2007-2009, 2011-2014, 2016 Gregory Nutt. All rights reserved.
|
2012-01-30 22:29:59 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-01 20:06:15 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2008-11-18 18:24:50 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-09-01 20:06:15 +02:00
|
|
|
* 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>
|
2007-09-09 13:58:50 +02:00
|
|
|
|
2007-09-01 20:06:15 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
2007-11-22 00:29:14 +01:00
|
|
|
#include <debug.h>
|
2007-09-09 13:58:50 +02:00
|
|
|
|
2012-03-04 00:18:34 +01:00
|
|
|
#include <nuttx/net/net.h>
|
2007-09-01 20:06:15 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2020-01-31 10:43:41 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2007-09-09 13:58:50 +02:00
|
|
|
|
2014-06-29 01:25:18 +02:00
|
|
|
#include "socket/socket.h"
|
2007-09-01 20:06:15 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void _net_semtake(FAR struct socketlist *list)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
net_lockedwait_uninterruptible(&list->sl_sem);
|
2007-09-01 20:06:15 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
#define _net_semgive(list) nxsem_post(&list->sl_sem)
|
2014-08-16 20:52:28 +02:00
|
|
|
|
2007-09-01 20:06:15 +02:00
|
|
|
/****************************************************************************
|
2007-09-16 19:46:25 +02:00
|
|
|
* Public Functions
|
2007-09-01 20:06:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-16 18:24:06 +02:00
|
|
|
/****************************************************************************
|
2014-10-11 16:15:23 +02:00
|
|
|
* Name: net_initlist
|
2014-08-16 18:24:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize a list of sockets for a new task
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* list -- A reference to the pre-allocated socket list to be initialized.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-09-01 20:06:15 +02:00
|
|
|
|
2013-01-27 00:49:02 +01:00
|
|
|
void net_initlist(FAR struct socketlist *list)
|
2007-09-01 20:06:15 +02:00
|
|
|
{
|
2013-01-27 00:49:02 +01:00
|
|
|
/* Initialize the list access mutex */
|
2007-09-01 20:06:15 +02:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&list->sl_sem, 0, 1);
|
2007-09-01 20:06:15 +02:00
|
|
|
}
|
|
|
|
|
2014-08-16 18:24:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_releaselist
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Release resources held by the socket list
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* list -- A reference to the pre-allocated socket list to be un-initialized.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-09-01 20:06:15 +02:00
|
|
|
|
2013-01-27 00:49:02 +01:00
|
|
|
void net_releaselist(FAR struct socketlist *list)
|
2007-09-01 20:06:15 +02:00
|
|
|
{
|
2013-01-27 00:49:02 +01:00
|
|
|
int ndx;
|
2007-09-01 20:06:15 +02:00
|
|
|
|
2013-01-27 00:49:02 +01:00
|
|
|
DEBUGASSERT(list);
|
2007-09-01 20:06:15 +02:00
|
|
|
|
2013-01-27 00:49:02 +01:00
|
|
|
/* Close each open socket in the list. */
|
2009-09-16 22:29:00 +02:00
|
|
|
|
2013-01-27 00:49:02 +01:00
|
|
|
for (ndx = 0; ndx < CONFIG_NSOCKET_DESCRIPTORS; ndx++)
|
2007-09-01 20:06:15 +02:00
|
|
|
{
|
2013-01-27 00:49:02 +01:00
|
|
|
FAR struct socket *psock = &list->sl_sockets[ndx];
|
|
|
|
if (psock->s_crefs > 0)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
psock_close(psock);
|
2013-01-27 00:49:02 +01:00
|
|
|
}
|
2007-09-01 20:06:15 +02:00
|
|
|
}
|
2013-01-27 00:49:02 +01:00
|
|
|
|
|
|
|
/* Destroy the semaphore */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_destroy(&list->sl_sem);
|
2007-09-01 20:06:15 +02:00
|
|
|
}
|
|
|
|
|
2014-08-16 18:24:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: sockfd_allocate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Allocate a socket descriptor
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-05-28 15:26:03 +02:00
|
|
|
* Lowest socket descriptor index to be used.
|
2014-08-16 18:24:06 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2015-05-28 15:26:03 +02:00
|
|
|
* On success, a socket descriptor >= minsd is returned. A negated errno
|
2014-08-16 18:24:06 +02:00
|
|
|
* value is returned on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-07-19 02:14:46 +02:00
|
|
|
int sockfd_allocate(int minsd)
|
2007-09-01 22:56:19 +02:00
|
|
|
{
|
|
|
|
FAR struct socketlist *list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Get the socket list for this task/thread */
|
|
|
|
|
|
|
|
list = sched_getsockets();
|
|
|
|
if (list)
|
|
|
|
{
|
|
|
|
/* Search for a socket structure with no references */
|
|
|
|
|
|
|
|
_net_semtake(list);
|
2009-07-19 02:14:46 +02:00
|
|
|
for (i = minsd; i < CONFIG_NSOCKET_DESCRIPTORS; i++)
|
2007-09-01 22:56:19 +02:00
|
|
|
{
|
|
|
|
/* Are there references on this socket? */
|
2009-07-19 02:14:46 +02:00
|
|
|
|
2007-09-01 22:56:19 +02:00
|
|
|
if (!list->sl_sockets[i].s_crefs)
|
|
|
|
{
|
|
|
|
/* No take the reference and return the index + an offset
|
|
|
|
* as the socket descriptor.
|
|
|
|
*/
|
2009-07-19 02:14:46 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
memset(&list->sl_sockets[i], 0, sizeof(struct socket));
|
|
|
|
list->sl_sockets[i].s_crefs = 1;
|
|
|
|
_net_semgive(list);
|
|
|
|
return i + __SOCKFD_OFFSET;
|
2007-09-01 22:56:19 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-16 18:24:06 +02:00
|
|
|
|
2007-09-01 22:56:19 +02:00
|
|
|
_net_semgive(list);
|
|
|
|
}
|
2014-04-13 22:32:20 +02:00
|
|
|
|
2007-09-01 22:56:19 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:24:06 +02:00
|
|
|
/****************************************************************************
|
2018-11-27 14:50:09 +01:00
|
|
|
* Name: psock_release
|
2014-08-16 18:24:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a socket.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2015-05-28 15:26:03 +02:00
|
|
|
* psock - A reference to the socket instance to be freed.
|
2014-08-16 18:24:06 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2015-05-28 15:26:03 +02:00
|
|
|
* None
|
2014-08-16 18:24:06 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-11-27 14:50:09 +01:00
|
|
|
void psock_release(FAR struct socket *psock)
|
2007-09-01 22:56:19 +02:00
|
|
|
{
|
2017-09-25 15:17:09 +02:00
|
|
|
if (psock != NULL)
|
2007-09-01 22:56:19 +02:00
|
|
|
{
|
2020-01-31 10:43:41 +01:00
|
|
|
/* Decrement the count if there the socket will persist
|
|
|
|
* after this.
|
2007-09-01 22:56:19 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-31 10:43:41 +01:00
|
|
|
if (psock->s_crefs > 1)
|
2007-09-01 22:56:19 +02:00
|
|
|
{
|
2020-01-31 10:43:41 +01:00
|
|
|
psock->s_crefs--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The socket will not persist... reset it */
|
2014-08-16 18:24:06 +02:00
|
|
|
|
2020-01-31 10:43:41 +01:00
|
|
|
memset(psock, 0, sizeof(struct socket));
|
2007-09-01 22:56:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:24:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: sockfd_release
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free the socket by its socket descriptor.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* sockfd - Socket descriptor identifies the socket to be released.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-09-16 22:29:00 +02:00
|
|
|
void sockfd_release(int sockfd)
|
|
|
|
{
|
|
|
|
/* Get the socket structure for this sockfd */
|
|
|
|
|
|
|
|
FAR struct socket *psock = sockfd_socket(sockfd);
|
|
|
|
|
|
|
|
if (psock)
|
|
|
|
{
|
2020-01-31 10:43:41 +01:00
|
|
|
/* Take the list semaphore so that there will be no accesses
|
|
|
|
* to this socket structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
FAR struct socketlist *list = sched_getsockets();
|
|
|
|
if (list)
|
|
|
|
{
|
|
|
|
_net_semtake(list);
|
|
|
|
psock_release(psock);
|
|
|
|
_net_semgive(list);
|
|
|
|
}
|
2009-09-16 22:29:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:24:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: sockfd_socket
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Given a socket descriptor, return the underlying socket structure.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-01-09 08:46:32 +01:00
|
|
|
* sockfd - The socket descriptor index to use.
|
2014-08-16 18:24:06 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, a reference to the socket structure associated with the
|
|
|
|
* the socket descriptor is returned. NULL is returned on any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-01 22:56:19 +02:00
|
|
|
FAR struct socket *sockfd_socket(int sockfd)
|
|
|
|
{
|
|
|
|
FAR struct socketlist *list;
|
|
|
|
int ndx = sockfd - __SOCKFD_OFFSET;
|
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
if (ndx >= 0 && ndx < CONFIG_NSOCKET_DESCRIPTORS)
|
2007-09-01 22:56:19 +02:00
|
|
|
{
|
|
|
|
list = sched_getsockets();
|
|
|
|
if (list)
|
|
|
|
{
|
|
|
|
return &list->sl_sockets[ndx];
|
|
|
|
}
|
|
|
|
}
|
2014-08-16 18:24:06 +02:00
|
|
|
|
2007-09-01 22:56:19 +02:00
|
|
|
return NULL;
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|