1
0
Fork 0
forked from nuttx/nuttx-update

socket: separation error code EBADF and ENOTSOCK

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu 2022-10-27 18:17:35 +08:00 committed by Petro Karashchenko
parent 2c95c04f4e
commit f00c11aec4
16 changed files with 115 additions and 79 deletions

View file

@ -941,12 +941,11 @@ static int telnet_session(FAR struct telnet_session_s *session)
* instance resided in the daemon's task group`).
*/
psock = sockfd_socket(session->ts_sd);
if (!psock)
ret = sockfd_socket(session->ts_sd, &psock);
if (ret != OK)
{
nerr("ERROR: Failed to convert sd=%d to a socket structure\n",
session->ts_sd);
ret = -EINVAL;
goto errout_with_dev;
}

View file

@ -175,9 +175,13 @@ int sockfd_allocate(FAR struct socket *psock, int oflags)
* Input Parameters:
* sockfd - The socket descriptor index to use.
*
* Returned Value:
* On success, a reference to the socket structure associated with the
* the socket descriptor is returned. NULL is returned on any failure.
* Returns zero (OK) on success. On failure, it returns a negated errno
* value to indicate the nature of the error.
*
* EBADF
* The file descriptor is not a valid index in the descriptor table.
* ENOTSOCK
* psock is a descriptor for a file, not a socket.
*
****************************************************************************/
@ -192,16 +196,19 @@ FAR struct socket *file_socket(FAR struct file *filep)
return NULL;
}
FAR struct socket *sockfd_socket(int sockfd)
int sockfd_socket(int sockfd, FAR struct socket **socketp)
{
FAR struct file *filep;
if (fs_getfilep(sockfd, &filep) < 0)
{
return NULL;
*socketp = NULL;
return -EBADF;
}
return file_socket(filep);
*socketp = file_socket(filep);
return *socketp != NULL ? OK : -ENOTSOCK;
}
/****************************************************************************

View file

@ -527,14 +527,18 @@ int sockfd_allocate(FAR struct socket *psock, int oflags);
* Input Parameters:
* sockfd - The socket descriptor index to use.
*
* Returned Value:
* On success, a reference to the socket structure associated with the
* the socket descriptor is returned. NULL is returned on any failure.
* Returns zero (OK) on success. On failure, it returns a negated errno
* value to indicate the nature of the error.
*
* EBADF
* The file descriptor is not a valid index in the descriptor table.
* ENOTSOCK
* psock is a descriptor for a file, not a socket.
*
****************************************************************************/
FAR struct socket *file_socket(FAR struct file *filep);
FAR struct socket *sockfd_socket(int sockfd);
int sockfd_socket(int sockfd, FAR struct socket **socketp);
/****************************************************************************
* Name: psock_socket

View file

@ -229,9 +229,8 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
{
FAR struct socket *psock = sockfd_socket(sockfd);
FAR struct socket *psock;
FAR struct socket *newsock;
FAR struct file *filep;
int errcode;
int newfd;
int ret;
@ -240,24 +239,15 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
enter_cancellation_point();
/* Get the underlying socket structure */
ret = sockfd_socket(sockfd, &psock);
/* Verify that the sockfd corresponds to valid, allocated socket */
if (psock == NULL || psock->s_conn == NULL)
if (ret < 0)
{
/* It is not a valid socket description. Distinguish between the cases
* where sockfd is a just valid and when it is a valid file descriptor
* used in the wrong context.
*/
if (fs_getfilep(sockfd, &filep) == 0)
{
errcode = ENOTSOCK;
}
else
{
errcode = EBADF;
}
errcode = -ret;
goto errout;
}

View file

@ -139,13 +139,17 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
FAR struct socket *psock;
int ret;
/* Use the socket descriptor to get the underlying socket structure */
/* Get the underlying socket structure */
psock = sockfd_socket(sockfd);
ret = sockfd_socket(sockfd, &psock);
/* Then let psock_bind do all of the work */
ret = psock_bind(psock, addr, addrlen);
if (ret == OK)
{
ret = psock_bind(psock, addr, addrlen);
}
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -226,11 +226,15 @@ int connect(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen)
/* Get the underlying socket structure */
psock = sockfd_socket(sockfd);
ret = sockfd_socket(sockfd, &psock);
/* Then let psock_connect() do all of the work */
ret = psock_connect(psock, addr, addrlen);
if (ret == OK)
{
ret = psock_connect(psock, addr, addrlen);
}
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -145,12 +145,20 @@ int psock_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr,
int getpeername(int sockfd, FAR struct sockaddr *addr,
FAR socklen_t *addrlen)
{
FAR struct socket *psock = sockfd_socket(sockfd);
FAR struct socket *psock;
int ret;
/* Get the underlying socket structure */
ret = sockfd_socket(sockfd, &psock);
/* Let psock_getpeername() do all of the work */
ret = psock_getpeername(psock, addr, addrlen);
if (ret == OK)
{
ret = psock_getpeername(psock, addr, addrlen);
}
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -139,12 +139,20 @@ int psock_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr,
int getsockname(int sockfd, FAR struct sockaddr *addr,
FAR socklen_t *addrlen)
{
FAR struct socket *psock = sockfd_socket(sockfd);
FAR struct socket *psock;
int ret;
/* Get the underlying socket structure */
ret = sockfd_socket(sockfd, &psock);
/* Let psock_getsockname() do all of the work */
ret = psock_getsockname(psock, addr, addrlen);
if (ret == OK)
{
ret = psock_getsockname(psock, addr, addrlen);
}
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -507,11 +507,15 @@ int getsockopt(int sockfd, int level, int option,
/* Get the underlying socket structure */
psock = sockfd_socket(sockfd);
ret = sockfd_socket(sockfd, &psock);
/* Then let psock_getsockopt() do all of the work */
ret = psock_getsockopt(psock, level, option, value, value_len);
if (ret == OK)
{
ret = psock_getsockopt(psock, level, option, value, value_len);
}
if (ret < 0)
{
set_errno(-ret);

View file

@ -132,38 +132,22 @@ int psock_listen(FAR struct socket *psock, int backlog)
int listen(int sockfd, int backlog)
{
FAR struct socket *psock = sockfd_socket(sockfd);
FAR struct file *filep;
int errcode;
FAR struct socket *psock;
int ret;
/* Verify that the sockfd corresponds to valid, allocated socket */
/* Get the underlying socket structure */
if (psock == NULL || psock->s_conn == NULL)
{
/* It is not a valid socket description. Distinguish between the
* cases where sockfd is a just invalid and when it is a valid file
* descriptor used in the wrong context.
*/
if (fs_getfilep(sockfd, &filep) == 0)
{
errcode = ENOTSOCK;
}
else
{
errcode = EBADF;
}
_SO_SETERRNO(psock, errcode);
return ERROR;
}
ret = sockfd_socket(sockfd, &psock);
/* The let psock_listen to the work. If psock_listen() fails, it will have
* set the errno variable.
*/
ret = psock_listen(psock, backlog);
if (ret == OK)
{
ret = psock_listen(psock, backlog);
}
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -159,11 +159,15 @@ ssize_t recvfrom(int sockfd, FAR void *buf, size_t len, int flags,
/* Get the underlying socket structure */
psock = sockfd_socket(sockfd);
ret = sockfd_socket(sockfd, &psock);
/* Then let psock_recvfrom() do all of the work */
ret = psock_recvfrom(psock, buf, len, flags, from, fromlen);
if (ret == OK)
{
ret = psock_recvfrom(psock, buf, len, flags, from, fromlen);
}
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -157,11 +157,15 @@ ssize_t recvmsg(int sockfd, FAR struct msghdr *msg, int flags)
/* Get the underlying socket structure */
psock = sockfd_socket(sockfd);
ret = sockfd_socket(sockfd, &psock);
/* Then let psock_recvmsg() do all of the work */
/* Let psock_recvmsg() do all of the work */
if (ret == OK)
{
ret = psock_recvmsg(psock, msg, flags);
}
ret = psock_recvmsg(psock, msg, flags);
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -165,11 +165,15 @@ ssize_t send(int sockfd, FAR const void *buf, size_t len, int flags)
/* Get the underlying socket structure */
psock = sockfd_socket(sockfd);
ret = sockfd_socket(sockfd, &psock);
/* And let psock_send do all of the work */
ret = psock_send(psock, buf, len, flags);
if (ret == OK)
{
ret = psock_send(psock, buf, len, flags);
}
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -147,11 +147,15 @@ ssize_t sendmsg(int sockfd, FAR struct msghdr *msg, int flags)
/* Get the underlying socket structure */
psock = sockfd_socket(sockfd);
ret = sockfd_socket(sockfd, &psock);
/* Then let psock_sendmsg() do all of the work */
/* Let psock_sendmsg() do all of the work */
if (ret == OK)
{
ret = psock_sendmsg(psock, msg, flags);
}
ret = psock_sendmsg(psock, msg, flags);
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -205,11 +205,15 @@ ssize_t sendto(int sockfd, FAR const void *buf, size_t len, int flags,
/* Get the underlying socket structure */
psock = sockfd_socket(sockfd);
ret = sockfd_socket(sockfd, &psock);
/* And let psock_sendto do all of the work */
ret = psock_sendto(psock, buf, len, flags, to, tolen);
if (ret == OK)
{
ret = psock_sendto(psock, buf, len, flags, to, tolen);
}
if (ret < 0)
{
_SO_SETERRNO(psock, -ret);

View file

@ -642,11 +642,15 @@ int setsockopt(int sockfd, int level, int option, const void *value,
/* Get the underlying socket structure */
psock = sockfd_socket(sockfd);
ret = sockfd_socket(sockfd, &psock);
/* Then let psock_setockopt() do all of the work */
ret = psock_setsockopt(psock, level, option, value, value_len);
if (ret == OK)
{
ret = psock_setsockopt(psock, level, option, value, value_len);
}
if (ret < 0)
{
set_errno(-ret);