Implemented several options in set/getsockopts

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@334 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2007-09-08 19:50:59 +00:00
parent cdfb48dac6
commit a3d199f7ac
19 changed files with 692 additions and 117 deletions
+9 -1
View File
@@ -41,7 +41,15 @@ MKDEP = $(TOPDIR)/tools/mkdeps.sh
ifeq ($(CONFIG_NET),y)
STD_ASRCS =
STD_CSRCS = socket.c bind.c connect.c send.c sendto.c recv.c recvfrom.c \
setsockopt.c getsockopt.c net_sockets.c net-close.c
net_sockets.c net-close.c
ifeq ($(CONFIG_NET_SOCKOPTS),y)
STD_CSRCS += setsockopt.c getsockopt.c
ifneq ($(CONFIG_DISABLE_CLOCK),y)
STD_CSRCS += net_timeout.c net_dsec2timeval.c net_timeval2dsec.c
endif
endif
include uip/Make.defs
endif
+136 -3
View File
@@ -38,7 +38,7 @@
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_NET
#if defined(CONFIG_NET) && defined(CONFIG_NET_SOCKOPTS)
#include <sys/types.h>
#include <sys/socket.h>
@@ -95,8 +95,141 @@
int getsockopt(int sockfd, int level, int option, void *value, socklen_t *value_len)
{
*get_errno_ptr() = ENOPROTOOPT;
FAR struct socket *psock;
int err;
/* Get the underlying socket structure */
/* Verify that the sockfd corresponds to valid, allocated socket */
psock = sockfd_socket(sockfd);
if (!psock || psock->s_crefs <= 0)
{
err = EBADF;
goto errout;
}
/* Verify that the socket option if valid (but might not be supported ) */
if (!_SO_GETVALID(option) || !value)
{
err = EINVAL;
goto errout;
}
/* Process the option */
switch (option)
{
/* The following options take a point to an integer boolean value.
* We will blindly report the bit here although the implementation
* is outside of the scope of getsockopt.
*/
case SO_DEBUG: /* Enables recording of debugging information */
case SO_BROADCAST: /* Permits sending of broadcast messages */
case SO_REUSEADDR: /* Allow reuse of local addresses */
case SO_KEEPALIVE: /* Keeps connections active by enabling the
* periodic transmission */
case SO_OOBINLINE: /* Leaves received out-of-band data inline */
case SO_DONTROUTE: /* Requests outgoing messages bypass standard routing */
{
sockopt_t optionset;
/* Verify that option is the size of an 'int'. Should also check
* that 'value' is properly aligned for an 'int'
*/
if (value_len != sizeof(int))
{
err = EINVAL;
goto errout;
}
/* Sample the current options. This is atomic operation and so
* should not require any special steps for thread safety. We
* this outside of the macro because you can never be sure what
* a macro will do.
*/
optionset = psock->options;
*(int*)value = _SO_GETOPT(optionset, option);
}
break;
case SO_TYPE: /* Reports the socket type */
{
/* Verify that option is the size of an 'int'. Should also check
* that 'value' is properly aligned for an 'int'
*/
if (value_len != sizeof(int))
{
err = EINVAL;
goto errout;
}
/* Return the socket type */
*(int*)value = psock->s_type;
}
break;
/* The following are valid only if the OS CLOCK feature is enabled */
case SO_RCVTIMEO:
case SO_SNDTIMEO:
#ifndef CONFIG_DISABLE_CLOCK
{
socktimeo_t timeo;
/* Verify that option is the size of an 'int'. Should also check
* that 'value' is properly aligned for an 'int'
*/
if (value_len != sizeof(struct timeval))
{
err = EINVAL;
goto errout;
}
/* Get the timeout value. This is a atomic operation and should
* require no special operation.
*/
if (option == SO_RCVTIMEO)
{
timeo = psock->s_rcvtimeo;
}
else
{
timeo = psock->s_sndtimeo;
}
/* Then return the timeout value to the caller */
net_dsec2timeval(timeo, (struct timeval *)value);
}
break;
#endif
/* The following are not yet implemented */
case SO_ACCEPTCONN: /* Reports whether socket listening is enabled */
case SO_LINGER:
case SO_SNDBUF: /* Sets send buffer size */
case SO_RCVBUF: /* Sets receive buffer size */
case SO_ERROR: /* Reports and clears error status. */
case SO_RCVLOWAT: /* Sets the minimum number of bytes to input */
case SO_SNDLOWAT: /* Sets the minimum number of bytes to output */
default:
err = ENOPROTOOPT;
goto errout;
}
return OK;
errout:
*get_errno_ptr() = err;
return ERROR;
}
#endif /* CONFIG_NET */
#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS */
+79
View File
@@ -0,0 +1,79 @@
/****************************************************************************
* net/net-dsec2timeval.c
*
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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_SOCKOPTS) && !defined(CONFIG_DISABLE_CLOCK)
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include "net_internal.h"
/****************************************************************************
* Global Functions
****************************************************************************/
/****************************************************************************
* Function: net_dsec2timeval
*
* Description:
* Convert a decisecond timeout value to a struct timeval. Needed by
* getsockopt() to report timeout values.
*
* Parameters:
* dsec The decisecond value to convert
* tv The struct timeval to receive the converted value
*
* Returned Value:
* None
*
* Assumptions:
*
****************************************************************************/
void net_dsec2timeval(uint16 dsec, struct timeval *tv)
{
uint16 remainder;
tv->tv_sec = dsec / DSEC_PER_SEC;
remainder = dsec - tv->tv_sec * DSEC_PER_SEC;
tv->tv_usec = remainder * USEC_PER_DSEC;
}
#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS && !CONFIG_DISABLE_CLOCK */
+80
View File
@@ -0,0 +1,80 @@
/****************************************************************************
* net/net-timeo.c
*
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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_SOCKOPTS) && !defined(CONFIG_DISABLE_CLOCK)
#include <sys/types.h>
#include <nuttx/os_external.h>
#include "net_internal.h"
/****************************************************************************
* Global Functions
****************************************************************************/
/****************************************************************************
* Function: net_timeo
*
* Description:
* Check if a timeout has elapsed. This can be called from a socket poll
* function to determine if a timeout has occurred.
*
* Parameters:
* start_time Timeout start time in system clock ticks
* timeout Timeout value in deciseconds.
*
* Returned Value:
* 0 (FALSE) if not timeout; 1 (TRUE) if timeout
*
* Assumptions:
*
****************************************************************************/
int net_timeo(uint32 start_time, socktimeo_t timeo)
{
uint32 timeo_ticks = DSEC2TICK(timeo);
uint32 elapsed = g_system_timer - start_time;
if (elapsed >= timeo_ticks)
{
return TRUE;
}
return FALSE;
}
#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS && !CONFIG_DISABLE_CLOCK */
+75
View File
@@ -0,0 +1,75 @@
/****************************************************************************
* net/net-timeval2dsec.c
*
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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_SOCKOPTS) && !defined(CONFIG_DISABLE_CLOCK)
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include "net_internal.h"
/****************************************************************************
* Global Functions
****************************************************************************/
/****************************************************************************
* Function: net_timeval2dsec
*
* Description:
* Convert a struct timeval to deciseconds. Needed by setsockopt() to
* save new timeout values.
*
* Parameters:
* tv The struct timeval to convert
*
* Returned Value:
* the converted value
*
* Assumptions:
*
****************************************************************************/
socktimeo_t net_timeval2dsec(struct timeval *tv)
{
return (uint16)(tv->tv_sec* DSEC_PER_SEC + tv->tv_usec / USEC_PER_DSEC);
}
#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS && !CONFIG_DISABLE_CLOCK */
+44 -35
View File
@@ -51,46 +51,47 @@
* Definitions
****************************************************************************/
/* This macro converts a socket option value into a bit setting */
#define _SO_BIT(o) (1 << (o))
/* These define bit positions for each socket option (see sys/socket.h) */
#define _SO_DEBUG (1 << SO_DEBUG)
#define _SO_ACCEPTCONN (1 << SO_ACCEPTCONN)
#define _SO_BROADCAST (1 << SO_BROADCAST)
#define _SO_REUSEADDR (1 << SO_REUSEADDR)
#define _SO_KEEPALIVE (1 << SO_KEEPALIVE)
#define _SO_LINGER (1 << SO_LINGER)
#define _SO_OOBINLINE (1 << SO_OOBINLINE)
#define _SO_SNDBUF (1 << SO_SNDBUF)
#define _SO_RCVBUF (1 << SO_RCVBUF)
#define _SO_ERROR (1 << SO_ERROR)
#define _SO_TYPE (1 << SO_TYPE)
#define _SO_DONTROUTE (1 << SO_DONTROUTE)
#define _SO_RCVLOWAT (1 << SO_RCVLOWAT)
#define _SO_RCVTIMEO (1 << SO_RCVTIMEO)
#define _SO_SNDLOWAT (1 << SO_SNDLOWAT)
#define _SO_SNDTIMEO (1 << SO_SNDTIMEO)
#define _SO_DEBUG _SO_BIT(SO_DEBUG)
#define _SO_ACCEPTCONN _SO_BIT(SO_ACCEPTCONN)
#define _SO_BROADCAST _SO_BIT(SO_BROADCAST)
#define _SO_REUSEADDR _SO_BIT(SO_REUSEADDR)
#define _SO_KEEPALIVE _SO_BIT(SO_KEEPALIVE)
#define _SO_LINGER _SO_BIT(SO_LINGER)
#define _SO_OOBINLINE _SO_BIT(SO_OOBINLINE)
#define _SO_SNDBUF _SO_BIT(SO_SNDBUF)
#define _SO_RCVBUF _SO_BIT(SO_RCVBUF)
#define _SO_ERROR _SO_BIT(SO_ERROR)
#define _SO_TYPE _SO_BIT(SO_TYPE)
#define _SO_DONTROUTE _SO_BIT(SO_DONTROUTE)
#define _SO_RCVLOWAT _SO_BIT(SO_RCVLOWAT)
#define _SO_RCVTIMEO _SO_BIT(SO_RCVTIMEO)
#define _SO_SNDLOWAT _SO_BIT(SO_SNDLOWAT)
#define _SO_SNDTIMEO _SO_BIT(SO_SNDTIMEO)
/* This idenfies the options that have been implemented. Someday this
* should be 0xffff
/* This is the larget option value */
#define _SO_MAXOPT (15)
/* Macros to set, test, clear options */
#define _SO_SETOPT(s,o) ((s) |= _SO_BIT(o))
#define _SO_CLROPT(s,o) ((s) &= ~_SO_BIT(o))
#define _SO_GETOPT(s,o) (((s) & _SO_BIT(o)) != 0)
/* These are macros that can be used to determine if socket option code is
* valid (in range) and supported by API.
*/
#define _SO_IMPLEMENTED 0x0000
/* The set of all valid options is a subset of those that are implemented
* and those that can be supported within the kernel OS configuration.
*/
#ifdef CONFIG_DISABLE_CLOCK
# define _SO_ALLOPTIONS (_SO_IMPLEMENTED & ~(_SO_RCVTIMEO|_SO_SNDTIMEO)
#else
# define _SO_ALLOPTIONS (_SO_IMPLEMENTED)
#endif
/* This is the set of options valid for getsockopt and setsockopt */
#define _SO_GETONLY (_SO_ACCEPTCONN|_SO_ERROR|_SO_TYPE)
#define _SO_SETOPTS (_SO_ALLOPTIONS & ~_SO_GETONLY)
#define _SO_GETOTPS _SO_ALLOPTIONS
#define _SO_GETONLYSET (_SO_ACCEPTCONN|_SO_ERROR|_SO_TYPE)
#define _SO_GETONLY(o) ((_SO_BIT(o) & _SO_GETONLYSET) != 0)
#define _SO_GETVALID(o) (((unsigned int)(o)) <= _SO_MAXOPT)
#define _SO_SETVALID(o) ((((unsigned int)(o)) <= _SO_MAXOPT) && !_SO_GETONLY(o))
/****************************************************************************
* Public Types
@@ -119,6 +120,14 @@ EXTERN int sockfd_allocate(void);
EXTERN void sockfd_release(int sockfd);
EXTERN FAR struct socket *sockfd_socket(int sockfd);
/* sockopt support ***********************************************************/
#if defined(CONFIG_NET_SOCKOPTS) && !defined(CONFIG_DISABLE_CLOCK)
EXTERN int net_timeo(uint32 start_time, socktimeo_t timeo);
EXTERN socktimeo_t socktimeo_t net_timeval2dsec(struct timeval *tv);
EXTERN void net_dsec2timeval(uint16 dsec, struct timeval *tv);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
+142 -3
View File
@@ -38,7 +38,7 @@
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_NET
#if defined(CONFIG_NET) && defined(CONFIG_NET_SOCKOPTS)
#include <sys/types.h>
#include <sys/socket.h>
@@ -101,8 +101,147 @@
int setsockopt(int sockfd, int level, int option, const void *value, socklen_t value_len)
{
*get_errno_ptr() = ENOPROTOOPT;
FAR struct socket *psock;
irqstate_t flags;
int err;
/* Get the underlying socket structure */
/* Verify that the sockfd corresponds to valid, allocated socket */
psock = sockfd_socket(sockfd);
if (!psock || psock->s_crefs <= 0)
{
err = EBADF;
goto errout;
}
/* Verify that the socket option if valid (but might not be supported ) */
if (!_SO_SETVALID(option) || !value)
{
err = EINVAL;
goto errout;
}
/* Process the option */
switch (option)
{
/* The following options take a point to an integer boolean value.
* We will blindly set the bit here although the implementation
* is outside of the scope of setsockopt.
*/
case SO_DEBUG: /* Enables recording of debugging information */
case SO_BROADCAST: /* Permits sending of broadcast messages */
case SO_REUSEADDR: /* Allow reuse of local addresses */
case SO_KEEPALIVE: /* Keeps connections active by enabling the
* periodic transmission */
case SO_OOBINLINE: /* Leaves received out-of-band data inline */
case SO_DONTROUTE: /* Requests outgoing messages bypass standard routing */
{
int setting;
/* Verify that option is the size of an 'int'. Should also check
* that 'value' is properly aligned for an 'int'
*/
if (value_len != sizeof(int))
{
err = EINVAL;
goto errout;
}
/* Get the value. Is the option being set or cleared? */
setting = *(int*)value;
/* Disable interrupts so that there is no conflict with interrupt
* level access to options.
*/
flags = irqsave();
/* Set or clear the option bit */
if (setting)
{
_SO_SETOPT(psock->s_options, option);
}
else
{
_SO_CLROPT(psock->s_options, option);
}
}
break;
/* The following are valid only if the OS CLOCK feature is enabled */
case SO_RCVTIMEO:
case SO_SNDTIMEO:
#ifndef CONFIG_DISABLE_CLOCK
{
socktimeo_t timeo;
/* Verify that option is the size of an 'struct timeval'. */
if (value_len != sizeof(struct timeval))
{
err = EINVAL;
goto errout;
}
/* Get the timeout value */
timeo = net_timeval2dsec((struct timeval *)value);
/* Save the timeout value */
if (option == SO_RCVTIMEO)
{
psock->s_rcvtimeo = timeo;
}
else
{
psock->s_sndtimeo = timeo;
}
/* Set/clear the corresponding enable/disable bit */
if (timeo)
{
_SO_CLROPT(psock->s_options, option);
}
else
{
_SO_SETOPT(psock->s_options, option);
}
}
break;
#endif
/* The following are not yet implemented */
case SO_LINGER:
case SO_SNDBUF: /* Sets send buffer size */
case SO_RCVBUF: /* Sets receive buffer size */
case SO_RCVLOWAT: /* Sets the minimum number of bytes to input */
case SO_SNDLOWAT: /* Sets the minimum number of bytes to output */
/* There options are only valid when used with getopt */
case SO_ACCEPTCONN: /* Reports whether socket listening is enabled */
case SO_ERROR: /* Reports and clears error status. */
case SO_TYPE: /* Reports the socket type */
default:
err = ENOPROTOOPT;
goto errout;
}
return OK;
errout:
*get_errno_ptr() = err;
return ERROR;
}
#endif /* CONFIG_NET */
#endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS */