mirror of
https://github.com/apache/nuttx.git
synced 2026-05-26 18:56:10 +08:00
net: unify socket into file descriptor
Change-Id: I9bcd21564e6c97d3edbb38aed1748c114160ea36 Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
+1
-1
@@ -113,7 +113,7 @@ struct aiocb
|
||||
FAR volatile void *aio_buf; /* Location of buffer */
|
||||
off_t aio_offset; /* File offset */
|
||||
size_t aio_nbytes; /* Length of transfer */
|
||||
#if (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS) > 127
|
||||
#if CONFIG_NFILE_DESCRIPTORS > 127
|
||||
int16_t aio_fildes; /* File descriptor (should be int) */
|
||||
#else
|
||||
int8_t aio_fildes; /* File descriptor (should be int) */
|
||||
|
||||
@@ -114,6 +114,7 @@
|
||||
#define FSNODEFLAG_TYPE_SHM 0x00000006 /* Shared memory region */
|
||||
#define FSNODEFLAG_TYPE_MTD 0x00000007 /* Named MTD driver */
|
||||
#define FSNODEFLAG_TYPE_SOFTLINK 0x00000008 /* Soft link */
|
||||
#define FSNODEFLAG_TYPE_SOCKET 0x00000009 /* Socket */
|
||||
#define FSNODEFLAG_DELETED 0x00000010 /* Unlinked */
|
||||
|
||||
#define INODE_IS_TYPE(i,t) \
|
||||
@@ -128,6 +129,7 @@
|
||||
#define INODE_IS_SHM(i) INODE_IS_TYPE(i,FSNODEFLAG_TYPE_SHM)
|
||||
#define INODE_IS_MTD(i) INODE_IS_TYPE(i,FSNODEFLAG_TYPE_MTD)
|
||||
#define INODE_IS_SOFTLINK(i) INODE_IS_TYPE(i,FSNODEFLAG_TYPE_SOFTLINK)
|
||||
#define INODE_IS_SOCKET(i) INODE_IS_TYPE(i,FSNODEFLAG_TYPE_SOCKET)
|
||||
|
||||
#define INODE_GET_TYPE(i) ((i)->i_flags & FSNODEFLAG_TYPE_MASK)
|
||||
#define INODE_SET_TYPE(i,t) \
|
||||
@@ -145,6 +147,7 @@
|
||||
#define INODE_SET_SHM(i) INODE_SET_TYPE(i,FSNODEFLAG_TYPE_SHM)
|
||||
#define INODE_SET_MTD(i) INODE_SET_TYPE(i,FSNODEFLAG_TYPE_MTD)
|
||||
#define INODE_SET_SOFTLINK(i) INODE_SET_TYPE(i,FSNODEFLAG_TYPE_SOFTLINK)
|
||||
#define INODE_SET_SOCKET(i) INODE_SET_TYPE(i,FSNODEFLAG_TYPE_SOCKET)
|
||||
|
||||
/* Mountpoint fd_flags values */
|
||||
|
||||
|
||||
+37
-208
@@ -92,7 +92,6 @@
|
||||
/* Definitions of 8-bit socket flags */
|
||||
|
||||
#define _SF_INITD 0x01 /* Bit 0: Socket structure is initialized */
|
||||
#define _SF_CLOEXEC 0x04 /* Bit 2: Close on execute */
|
||||
#define _SF_NONBLOCK 0x08 /* Bit 3: Don't block if no data (TCP/READ only) */
|
||||
#define _SF_LISTENING 0x10 /* Bit 4: SOCK_STREAM is listening */
|
||||
#define _SF_BOUND 0x20 /* Bit 5: SOCK_STREAM is bound to an address */
|
||||
@@ -111,7 +110,6 @@
|
||||
/* Macro to manage the socket state and flags */
|
||||
|
||||
#define _SS_INITD(s) (((s) & _SF_INITD) != 0)
|
||||
#define _SS_ISCLOEXEC(s) (((s) & _SF_CLOEXEC) != 0)
|
||||
#define _SS_ISNONBLOCK(s) (((s) & _SF_NONBLOCK) != 0)
|
||||
#define _SS_ISLISTENING(s) (((s) & _SF_LISTENING) != 0)
|
||||
#define _SS_ISBOUND(s) (((s) & _SF_BOUND) != 0)
|
||||
@@ -121,7 +119,7 @@
|
||||
/* Determine if a socket is valid. Valid means both (1) allocated and (2)
|
||||
* successfully initialized:
|
||||
*
|
||||
* Allocated: psock->s_crefs > 0
|
||||
* Allocated: psock->s_conn != NULL
|
||||
* Initialized: _SF_INITD bit set in psock->s_flags
|
||||
*
|
||||
* This logic is used within the OS to pick the sockets to be cloned when a
|
||||
@@ -130,7 +128,7 @@
|
||||
* pthread.
|
||||
*/
|
||||
|
||||
#define _PS_ALLOCD(psock) ((psock)->s_crefs > 0)
|
||||
#define _PS_ALLOCD(psock) ((psock)->s_conn != NULL)
|
||||
#define _PS_INITD(psock) (_SS_INITD((psock)->s_flags))
|
||||
#define _PS_VALID(psock) (_PS_ALLOCD(psock) && _PS_INITD(psock))
|
||||
|
||||
@@ -258,7 +256,6 @@ struct devif_callback_s; /* Forward reference */
|
||||
|
||||
struct socket
|
||||
{
|
||||
int16_t s_crefs; /* Reference count on the socket */
|
||||
uint8_t s_domain; /* IP domain */
|
||||
uint8_t s_type; /* Protocol type */
|
||||
uint8_t s_proto; /* Socket Protocol */
|
||||
@@ -293,16 +290,6 @@ struct socket
|
||||
#endif
|
||||
};
|
||||
|
||||
/* This defines a list of sockets indexed by the socket descriptor */
|
||||
|
||||
#ifdef CONFIG_NET
|
||||
struct socketlist
|
||||
{
|
||||
sem_t sl_sem; /* Manage access to the socket list */
|
||||
struct socket sl_sockets[CONFIG_NSOCKET_DESCRIPTORS];
|
||||
};
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
@@ -520,51 +507,22 @@ FAR struct iob_s *net_ioballoc(bool throttled, enum iob_user_e consumerid);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_checksd
|
||||
* Name: sockfd_allocate
|
||||
*
|
||||
* Description:
|
||||
* Check if the socket descriptor is valid for the provided TCB and if it
|
||||
* supports the requested access. This trivial operation is part of the
|
||||
* fdopen() operation when the fdopen() is performed on a socket
|
||||
* descriptor. It simply performs some sanity checking before permitting
|
||||
* the socket descriptor to be wrapped as a C FILE stream.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int net_checksd(int fd, int oflags);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_initlist
|
||||
*
|
||||
* Description:
|
||||
* Initialize a list of sockets for a new task
|
||||
* Allocate a socket descriptor
|
||||
*
|
||||
* Input Parameters:
|
||||
* list -- A reference to the pre-alloated socket list to be initialized.
|
||||
* psock A double pointer to socket structure to be allocated.
|
||||
* oflags Open mode flags.
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
* Allocate a struct files instance and associate it with an socket
|
||||
* instance. Returns the file descriptor == index into the files array.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void net_initlist(FAR struct socketlist *list);
|
||||
|
||||
/****************************************************************************
|
||||
* 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
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void net_releaselist(FAR struct socketlist *list);
|
||||
int sockfd_allocate(FAR struct socket **psock, int oflags);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: sockfd_socket
|
||||
@@ -624,23 +582,6 @@ FAR struct socket *sockfd_socket(int sockfd);
|
||||
int psock_socket(int domain, int type, int protocol,
|
||||
FAR struct socket *psock);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_close
|
||||
*
|
||||
* Description:
|
||||
* Performs the close operation on socket descriptors
|
||||
*
|
||||
* Input Parameters:
|
||||
* sockfd Socket descriptor of socket
|
||||
*
|
||||
* Returned Value:
|
||||
* Returns zero (OK) on success. On failure, it returns a negated errno
|
||||
* value to indicate the nature of the error.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int net_close(int sockfd);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: psock_close
|
||||
*
|
||||
@@ -1250,9 +1191,9 @@ int psock_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr,
|
||||
* Perform network device specific operations.
|
||||
*
|
||||
* Input Parameters:
|
||||
* psock A pointer to a NuttX-specific, internal socket structure
|
||||
* cmd The ioctl command
|
||||
* ap The argument of the ioctl cmd
|
||||
* psock A pointer to a NuttX-specific, internal socket structure.
|
||||
* cmd The ioctl command.
|
||||
* ap The argument of the ioctl cmd.
|
||||
*
|
||||
* Returned Value:
|
||||
* A non-negative value is returned on success; a negated errno value is
|
||||
@@ -1277,39 +1218,6 @@ int psock_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr,
|
||||
int psock_vioctl(FAR struct socket *psock, int cmd, va_list ap);
|
||||
int psock_ioctl(FAR struct socket *psock, int cmd, ...);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: netdev_vioctl
|
||||
*
|
||||
* Description:
|
||||
* Perform network device specific operations.
|
||||
*
|
||||
* Input Parameters:
|
||||
* sockfd Socket descriptor of device
|
||||
* cmd The ioctl command
|
||||
* ap The argument of the ioctl cmd
|
||||
*
|
||||
* Returned Value:
|
||||
* A non-negative value is returned on success; a negated errno value is
|
||||
* returned on any failure to indicate the nature of the failure:
|
||||
*
|
||||
* EBADF
|
||||
* 'sockfd' is not a valid socket descriptor.
|
||||
* EFAULT
|
||||
* 'arg' references an inaccessible memory area.
|
||||
* ENOTTY
|
||||
* 'cmd' not valid.
|
||||
* EINVAL
|
||||
* 'arg' is not valid.
|
||||
* ENOTTY
|
||||
* 'sockfd' is not associated with a network device.
|
||||
* ENOTTY
|
||||
* The specified request does not apply to the kind of object that the
|
||||
* descriptor 'sockfd' references.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int netdev_vioctl(int sockfd, int cmd, va_list ap);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: psock_poll
|
||||
*
|
||||
@@ -1321,10 +1229,10 @@ int netdev_vioctl(int sockfd, int cmd, va_list ap);
|
||||
* psock - An instance of the internal socket structure.
|
||||
* fds - The structure describing the events to be monitored, OR NULL if
|
||||
* this is a request to stop monitoring events.
|
||||
* setup - true: Setup up the poll; false: Teardown the poll
|
||||
* setup - true: Setup up the poll; false: Teardown the poll.
|
||||
*
|
||||
* Returned Value:
|
||||
* 0: Success; Negated errno on failure
|
||||
* 0: Success; Negated errno on failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
@@ -1332,89 +1240,29 @@ struct pollfd; /* Forward reference -- see poll.h */
|
||||
|
||||
int psock_poll(FAR struct socket *psock, struct pollfd *fds, bool setup);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_poll
|
||||
*
|
||||
* Description:
|
||||
* The standard poll() operation redirects operations on socket descriptors
|
||||
* to this function.
|
||||
*
|
||||
* Input Parameters:
|
||||
* fd - The socket descriptor of interest
|
||||
* fds - The structure describing the events to be monitored, OR NULL if
|
||||
* this is a request to stop monitoring events.
|
||||
* setup - true: Setup up the poll; false: Teardown the poll
|
||||
*
|
||||
* Returned Value:
|
||||
* 0: Success; Negated errno on failure
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
struct pollfd; /* Forward reference -- see poll.h */
|
||||
|
||||
int net_poll(int sockfd, struct pollfd *fds, bool setup);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: psock_dup
|
||||
*
|
||||
* Description:
|
||||
* Clone a socket descriptor to an arbitrary descriptor number.
|
||||
*
|
||||
* Returned Value:
|
||||
* On success, returns the number of new socket. On any error,
|
||||
* a negated errno value is returned.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int psock_dup(FAR struct socket *psock, int minsd);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_dup
|
||||
*
|
||||
* Description:
|
||||
* Clone a socket descriptor to an arbitrary descriptor number.
|
||||
*
|
||||
* Returned Value:
|
||||
* On success, returns the number of new socket. On any error,
|
||||
* a negated errno value is returned.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int net_dup(int sockfd, int minsd);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: psock_dup2
|
||||
*
|
||||
* Description:
|
||||
* Performs the low level, common portion of net_dup() and net_dup2()
|
||||
* Clone a socket instance to a new instance.
|
||||
*
|
||||
* Returned Value:
|
||||
* On success, returns the number of new socket. On any error,
|
||||
* a negated errno value is returned.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int psock_dup2(FAR struct socket *psock1, FAR struct socket *psock2);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_dup2
|
||||
* Name: psock_fstat
|
||||
*
|
||||
* Description:
|
||||
* Clone a socket descriptor to an arbitrary descriptor number.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) is returned on success; a negated errno value is returned on
|
||||
* any failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int net_dup2(int sockfd1, int sockfd2);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_fstat
|
||||
*
|
||||
* Description:
|
||||
* Performs fstat operations on socket
|
||||
* Performs fstat operations on socket.
|
||||
*
|
||||
* Input Parameters:
|
||||
* sockfd - Socket descriptor of the socket to operate on
|
||||
* buf - Caller-provided location in which to return the fstat data
|
||||
* psock - An instance of the internal socket structure.
|
||||
* buf - Caller-provided location in which to return the fstat data.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) is returned on success; a negated errno value is returned on
|
||||
@@ -1424,10 +1272,10 @@ int net_dup2(int sockfd1, int sockfd2);
|
||||
|
||||
struct stat; /* Forward reference. See sys/stat.h */
|
||||
|
||||
int net_fstat(int sockfd, FAR struct stat *buf);
|
||||
int psock_fstat(FAR struct socket *psock, FAR struct stat *buf);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_sendfile
|
||||
* Name: psock_sendfile
|
||||
*
|
||||
* Description:
|
||||
* The send() call may be used only when the socket is in a connected state
|
||||
@@ -1438,9 +1286,9 @@ int net_fstat(int sockfd, FAR struct stat *buf);
|
||||
*
|
||||
* Input Parameters:
|
||||
* psock An instance of the internal socket structure.
|
||||
* buf Data to send
|
||||
* len Length of data to send
|
||||
* flags Send flags
|
||||
* buf Data to send.
|
||||
* len Length of data to send.
|
||||
* flags Send flags.
|
||||
*
|
||||
* Returned Value:
|
||||
* On success, returns the number of characters sent. On error,
|
||||
@@ -1490,20 +1338,20 @@ int net_fstat(int sockfd, FAR struct stat *buf);
|
||||
|
||||
#ifdef CONFIG_NET_SENDFILE
|
||||
struct file;
|
||||
ssize_t net_sendfile(int outfd, struct file *infile, off_t *offset,
|
||||
size_t count);
|
||||
ssize_t psock_sendfile(FAR struct socket *psock, FAR struct file *infile,
|
||||
FAR off_t *offset, size_t count);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: psock_vfcntl
|
||||
*
|
||||
* Description:
|
||||
* Performs fcntl operations on socket
|
||||
* Performs fcntl operations on socket.
|
||||
*
|
||||
* Input Parameters:
|
||||
* psock - An instance of the internal socket structure.
|
||||
* cmd - The fcntl command.
|
||||
* ap - Command-specific arguments
|
||||
* ap - Command-specific arguments.
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) is returned on success; a negated errno value is returned on
|
||||
@@ -1534,25 +1382,6 @@ int psock_vfcntl(FAR struct socket *psock, int cmd, va_list ap);
|
||||
|
||||
int psock_fcntl(FAR struct socket *psock, int cmd, ...);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: net_vfcntl
|
||||
*
|
||||
* Description:
|
||||
* Performs fcntl operations on socket
|
||||
*
|
||||
* Input Parameters:
|
||||
* sockfd - Socket descriptor of the socket to operate on
|
||||
* cmd - The fcntl command.
|
||||
* ap - Command-specific arguments
|
||||
*
|
||||
* Returned Value:
|
||||
* Zero (OK) is returned on success; a negated errno value is returned on
|
||||
* any failure to indicate the nature of the failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int net_vfcntl(int sockfd, int cmd, va_list ap);
|
||||
|
||||
/****************************************************************************
|
||||
* Name: netdev_register
|
||||
*
|
||||
@@ -1571,10 +1400,10 @@ int net_vfcntl(int sockfd, int cmd, va_list ap);
|
||||
* ...
|
||||
*
|
||||
* Returned Value:
|
||||
* 0:Success; negated errno on failure
|
||||
* 0:Success; negated errno on failure.
|
||||
*
|
||||
* Assumptions:
|
||||
* Called during system initialization from normal user mode
|
||||
* Called during system initialization from normal user mode.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
@@ -1588,14 +1417,14 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype);
|
||||
* Unregister a network device driver.
|
||||
*
|
||||
* Input Parameters:
|
||||
* dev - The device driver structure to un-register
|
||||
* dev - The device driver structure to un-register.
|
||||
*
|
||||
* Returned Value:
|
||||
* 0:Success; negated errno on failure
|
||||
* 0:Success; negated errno on failure.
|
||||
*
|
||||
* Assumptions:
|
||||
* Currently only called for USB networking devices when the device is
|
||||
* physically removed from the slot
|
||||
* physically removed from the slot.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
|
||||
@@ -594,12 +594,6 @@ struct task_group_s
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_NET
|
||||
/* Sockets ********************************************************************/
|
||||
|
||||
struct socketlist tg_socketlist; /* Maps socket descriptor to socket */
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_ARCH_ADDRENV
|
||||
/* Address Environment ********************************************************/
|
||||
|
||||
@@ -910,10 +904,6 @@ FAR struct filelist *nxsched_get_files(void);
|
||||
FAR struct streamlist *nxsched_get_streams(void);
|
||||
#endif /* CONFIG_FILE_STREAM */
|
||||
|
||||
#ifdef CONFIG_NET
|
||||
FAR struct socketlist *nxsched_get_sockets(void);
|
||||
#endif
|
||||
|
||||
/********************************************************************************
|
||||
* Name: nxtask_init
|
||||
*
|
||||
|
||||
@@ -37,11 +37,7 @@
|
||||
|
||||
/* Get the total number of descriptors that we will have to support */
|
||||
|
||||
#ifdef CONFIG_NSOCKET_DESCRIPTORS
|
||||
# define FD_SETSIZE (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)
|
||||
#else
|
||||
# define FD_SETSIZE CONFIG_NFILE_DESCRIPTORS
|
||||
#endif
|
||||
#define FD_SETSIZE CONFIG_NFILE_DESCRIPTORS
|
||||
|
||||
/* We will use a 32-bit bitsets to represent the set of descriptors. How
|
||||
* many uint32_t's do we need to span all descriptors?
|
||||
|
||||
Reference in New Issue
Block a user