Added framework for getsockopt() setsockopt()

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@332 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2007-09-07 00:10:10 +00:00
parent d265632c82
commit 535038c5cc
8 changed files with 292 additions and 25 deletions

View File

@ -207,3 +207,4 @@
* Added snprintf() * Added snprintf()
* Added send() and sendto(); integrate write() and close() with socket descriptors. * Added send() and sendto(); integrate write() and close() with socket descriptors.
* Added recv() and recvfrom(). * Added recv() and recvfrom().
* Added getsockopt() and setsockopt()

View File

@ -632,15 +632,15 @@ Other memory:
</table> </table>
<pre><ul> <pre><ul>
0.2.9 2007-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> 0.2.9 2007-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr>
* Imported uIP into the tree (see * Imported uIP into the tree (see
http://www.sics.se/~adam/uip/index.php/Main_Page) http://www.sics.se/~adam/uip/index.php/Main_Page)
* Adding socket(), bind(), connect() * Adding socket(), bind(), connect()
* Added snprintf() * Added snprintf()
* Added send() and sendto(); integrate write() and close() with socket descriptors. * Added send() and sendto(); integrate write() and close() with socket descriptors.
* Added recv() and recvfrom(). * Added recv() and recvfrom().
* Added getsockopt() and setsockopt()
</pre></ul> </pre></ul>
<table width ="100%"> <table width ="100%">

View File

@ -67,15 +67,22 @@
* Public Types * Public Types
****************************************************************************/ ****************************************************************************/
/* This defines a bitmap big enough for one bit for each socket option */
typedef uint16 sockopt_t;
/* This is the internal representation of a socket reference by a file /* This is the internal representation of a socket reference by a file
* descriptor. * descriptor.
*/ */
struct socket struct socket
{ {
int s_crefs; /* Reference count on the socket */ int s_crefs; /* Reference count on the socket */
uint8 s_type; /* Protocol type: Only SOCK_STREAM or SOCK_DGRAM */ uint8 s_type; /* Protocol type: Only SOCK_STREAM or SOCK_DGRAM */
void *s_conn; /* Connection: struct uip_conn * or uip_udp_conn * */ #ifdef CONFIG_NET_SOCKOPTS
sockopt_t s_options; /* Selected socket options */
#endif
void *s_conn; /* Connection: struct uip_conn * or uip_udp_conn * */
}; };
/* This defines a list of sockets indexed by the socket descriptor */ /* This defines a list of sockets indexed by the socket descriptor */
@ -89,6 +96,10 @@ struct socketlist
}; };
#endif #endif
/* This defines a bitmap big enough for one bit for each socket option */
typedef uint16 sockopt_t;
/**************************************************************************** /****************************************************************************
* Public Function Prototypes * Public Function Prototypes
****************************************************************************/ ****************************************************************************/

View File

@ -1,5 +1,5 @@
/**************************************************************************** /****************************************************************************
* socket.h * sys/socket.h
* *
* Copyright (C) 2007 Gregory Nutt. All rights reserved. * Copyright (C) 2007 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
@ -117,38 +117,42 @@
/* Socket options */ /* Socket options */
#define SO_DEBUG 1 /* Enables recording of debugging information (get/set). #define SO_DEBUG 0 /* Enables recording of debugging information (get/set).
* arg: integer contain boolean value */ * arg: integer contain boolean value */
#define SO_ACCEPTCONN 2 /* Reports whether socket listening is enabled (get only). #define SO_ACCEPTCONN 1 /* Reports whether socket listening is enabled (get only).
* arg: returns integer contain boolean value */ * arg: returns integer contain boolean value */
#define SO_BROADCAST 3 /* Permits sending of broadcast messages (get/set). #define SO_BROADCAST 2 /* Permits sending of broadcast messages (get/set).
* arg: integer contain boolean value */ * arg: integer contain boolean value */
#define SO_REUSEADDR 4 /* Allow reuse of local addresses (get/set) #define SO_REUSEADDR 3 /* Allow reuse of local addresses (get/set)
* arg: integer contain boolean value */ * arg: integer contain boolean value */
#define SO_KEEPALIVE 5 /* Keeps connections active by enabling the periodic transmission #define SO_KEEPALIVE 4 /* Keeps connections active by enabling the periodic transmission
* of messages (get/set). arg: int */ * of messages (get/set). arg: int */
#define SO_LINGER 6 /* Lingers on a close() if data is present (get/set) #define SO_LINGER 5 /* Lingers on a close() if data is present (get/set)
* arg: struct linger */ * arg: struct linger */
#define SO_OOBINLINE 7 /* Leaves received out-of-band data (data marked urgent) inline #define SO_OOBINLINE 6 /* Leaves received out-of-band data (data marked urgent) inline
* (get/set) arg: integer contain boolean value */ * (get/set) arg: integer contain boolean value */
#define SO_SNDBUF 8 /* Sets send buffer size. arg: integer value (get/set). */ #define SO_SNDBUF 7 /* Sets send buffer size. arg: integer value (get/set). */
#define SO_RCVBUF 9 /* Sets receive buffer size. arg: integer value (get/set). */ #define SO_RCVBUF 8 /* Sets receive buffer size. arg: integer value (get/set). */
#define SO_ERROR 10 /* Reports and clears error statust (get only). arg: returns #define SO_ERROR 9 /* Reports and clears error statust (get only). arg: returns
* an integer value * an integer value
#define SO_TYPE 11 /* Reports the socket type (get only). return: int */ #define SO_TYPE 10 /* Reports the socket type (get only). return: int */
#define SO_DONTROUTE 12 /* equests that outgoing messages bypass standard routing #define SO_DONTROUTE 11 /* equests that outgoing messages bypass standard routing (get/set)
* arg: integer contain boolean value */ * arg: integer contain boolean value */
#define SO_RCVLOWAT 13 /* Sets the minimum number of bytes to process for socket input #define SO_RCVLOWAT 12 /* Sets the minimum number of bytes to process for socket input
* (get/set). arg: integer value */ * (get/set). arg: integer value */
#define SO_RCVTIMEO 14 /* Sets the timeout value that specifies the maximum amount of time #define SO_RCVTIMEO 13 /* Sets the timeout value that specifies the maximum amount of time
* an input function waits until it completes (get/set). * an input function waits until it completes (get/set).
* arg: struct timeval */ * arg: struct timeval */
#define SO_SNDLOWAT 15 /* Sets the minimum number of bytes to process for socket output #define SO_SNDLOWAT 14 /* Sets the minimum number of bytes to process for socket output
* (get/set). arg: integer value */ * (get/set). arg: integer value */
#define SO_SNDTIMEO 16 /* Sets the timeout value specifying the amount of time that an #define SO_SNDTIMEO 15 /* Sets the timeout value specifying the amount of time that an
* output function blocks because flow control prevents data from * output function blocks because flow control prevents data from
* being sent(get/set). arg: struct timeval */ * being sent(get/set). arg: struct timeval */
/* Protocol levels supported by get/setsockopt(): */
#define SOL_SOCKET 0 /* Only socket-level options supported */
/**************************************************************************** /****************************************************************************
* Type Definitions * Type Definitions
****************************************************************************/ ****************************************************************************/
@ -185,8 +189,8 @@ EXTERN ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
EXTERN int setsockopt(int sockfd, int level, int option, EXTERN int setsockopt(int sockfd, int level, int option,
const void *value, socklen_t value_len); const void *value, socklen_t value_len);
EXTERN int setsockopt(int sockfd, int level, int option, EXTERN int getsockopt(int sockfd, int level, int option,
const void *value, socklen_t value_len); void *value, socklen_t *value_len);
#undef EXTERN #undef EXTERN
#if defined(__cplusplus) #if defined(__cplusplus)

View File

@ -41,7 +41,7 @@ MKDEP = $(TOPDIR)/tools/mkdeps.sh
ifeq ($(CONFIG_NET),y) ifeq ($(CONFIG_NET),y)
STD_ASRCS = STD_ASRCS =
STD_CSRCS = socket.c bind.c connect.c send.c sendto.c recv.c recvfrom.c \ STD_CSRCS = socket.c bind.c connect.c send.c sendto.c recv.c recvfrom.c \
net_sockets.c net-close.c setsockopt.c getsockopt.c net_sockets.c net-close.c
include uip/Make.defs include uip/Make.defs
endif endif

102
net/getsockopt.c Normal file
View File

@ -0,0 +1,102 @@
/****************************************************************************
* net/getsockopt.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>
#ifdef CONFIG_NET
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include "net_internal.h"
/****************************************************************************
* Global Functions
****************************************************************************/
/****************************************************************************
* Function: getsockopt
*
* Description:
* getsockopt() retrieve thse value for the option specified by the
* 'option' argument for the socket specified by the 'sockfd' argument. If
* the size of the option value is greater than 'value_len', the value
* stored in the object pointed to by the 'value' argument will be silently
* truncated. Otherwise, the length pointed to by the 'value_len' argument
* will be modified to indicate the actual length of the'value'.
*
* The 'level' argument specifies the protocol level of the option. To
* retrieve options at the socket level, specify the level argument as
* SOL_SOCKET.
*
* See <sys/socket.h> a complete list of values for the 'option' argument.
*
* Parameters:
* sockfd Socket descriptor of socket
* level Protocol level to set the option
* option identifies the option to get
* value Points to the argument value
* value_len The length of the argument value
*
* Returned Value:
*
* EBADF
* The 'sockfd' argument is not a valid socket descriptor.
* EINVAL
* The specified option is invalid at the specified socket 'level' or the
* socket has been shutdown.
* ENOPROTOOPT
* The 'option' is not supported by the protocol.
* ENOTSOCK
* The 'sockfd' argument does not refer to a socket.
* ENOBUFS
* Insufficient resources are available in the system to complete the
* call.
*
* Assumptions:
*
****************************************************************************/
int getsockopt(int sockfd, int level, int option, void *value, socklen_t *value_len)
{
*get_errno_ptr() = ENOPROTOOPT;
return ERROR;
}
#endif /* CONFIG_NET */

View File

@ -51,6 +51,47 @@
* Definitions * Definitions
****************************************************************************/ ****************************************************************************/
/* 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)
/* This idenfies the options that have been implemented. Someday this
* should be 0xffff
*/
#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
/**************************************************************************** /****************************************************************************
* Public Types * Public Types
****************************************************************************/ ****************************************************************************/

108
net/setsockopt.c Normal file
View File

@ -0,0 +1,108 @@
/****************************************************************************
* net/setsockopt.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>
#ifdef CONFIG_NET
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include "net_internal.h"
/****************************************************************************
* Global Functions
****************************************************************************/
/****************************************************************************
* Function: setsockopt
*
* Description:
* setsockopt() sets the option specified by the 'option' argument,
* at the protocol level specified by the 'level' argument, to the value
* pointed to by the 'value' argument for the socket associated with the
* file descriptor specified by the 'sockfd' argument.
*
* The 'level' argument specifies the protocol level of the option. To set
* options at the socket level, specify the level argument as SOL_SOCKET.
*
* See <sys/socket.h> a complete list of values for the 'option' argument.
*
* Parameters:
* sockfd Socket descriptor of socket
* level Protocol level to set the option
* option identifies the option to set
* value Points to the argument value
* value_len The length of the argument value
*
* Returned Value:
* 0 on success; -1 on failure
*
* EBADF
* The 'sockfd' argument is not a valid socket descriptor.
* EDOM
* The send and receive timeout values are too big to fit into the
* timeout fields in the socket structure.
* EINVAL
* The specified option is invalid at the specified socket 'level' or the
* socket has been shut down.
* EISCONN
* The socket is already connected, and a specified option cannot be set
* while the socket is connected.
* ENOPROTOOPT
* The 'option' is not supported by the protocol.
* ENOTSOCK
* The 'sockfd' argument does not refer to a socket.
* ENOMEM
* There was insufficient memory available for the operation to complete.
* ENOBUFS
* Insufficient resources are available in the system to complete the
* call.
*
* Assumptions:
*
****************************************************************************/
int setsockopt(int sockfd, int level, int option, const void *value, socklen_t value_len)
{
*get_errno_ptr() = ENOPROTOOPT;
return ERROR;
}
#endif /* CONFIG_NET */