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
+2 -3
View File
@@ -941,12 +941,11 @@ static int telnet_session(FAR struct telnet_session_s *session)
* instance resided in the daemon's task group`). * instance resided in the daemon's task group`).
*/ */
psock = sockfd_socket(session->ts_sd); ret = sockfd_socket(session->ts_sd, &psock);
if (!psock) if (ret != OK)
{ {
nerr("ERROR: Failed to convert sd=%d to a socket structure\n", nerr("ERROR: Failed to convert sd=%d to a socket structure\n",
session->ts_sd); session->ts_sd);
ret = -EINVAL;
goto errout_with_dev; goto errout_with_dev;
} }
+13 -6
View File
@@ -175,9 +175,13 @@ int sockfd_allocate(FAR struct socket *psock, int oflags)
* Input Parameters: * Input Parameters:
* sockfd - The socket descriptor index to use. * sockfd - The socket descriptor index to use.
* *
* Returned Value: * Returns zero (OK) on success. On failure, it returns a negated errno
* On success, a reference to the socket structure associated with the * value to indicate the nature of the error.
* the socket descriptor is returned. NULL is returned on any failure. *
* 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; return NULL;
} }
FAR struct socket *sockfd_socket(int sockfd) int sockfd_socket(int sockfd, FAR struct socket **socketp)
{ {
FAR struct file *filep; FAR struct file *filep;
if (fs_getfilep(sockfd, &filep) < 0) 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;
} }
/**************************************************************************** /****************************************************************************
+8 -4
View File
@@ -527,14 +527,18 @@ int sockfd_allocate(FAR struct socket *psock, int oflags);
* Input Parameters: * Input Parameters:
* sockfd - The socket descriptor index to use. * sockfd - The socket descriptor index to use.
* *
* Returned Value: * Returns zero (OK) on success. On failure, it returns a negated errno
* On success, a reference to the socket structure associated with the * value to indicate the nature of the error.
* the socket descriptor is returned. NULL is returned on any failure. *
* 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 *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 * Name: psock_socket
+7 -17
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) 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 socket *newsock;
FAR struct file *filep;
int errcode; int errcode;
int newfd; int newfd;
int ret; int ret;
@@ -240,24 +239,15 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
enter_cancellation_point(); enter_cancellation_point();
/* Get the underlying socket structure */
ret = sockfd_socket(sockfd, &psock);
/* Verify that the sockfd corresponds to valid, allocated socket */ /* 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 errcode = -ret;
* 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;
}
goto errout; goto errout;
} }
+7 -3
View File
@@ -139,13 +139,17 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
FAR struct socket *psock; FAR struct socket *psock;
int ret; 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 */ /* 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) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+6 -2
View File
@@ -226,11 +226,15 @@ int connect(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen)
/* Get the underlying socket structure */ /* Get the underlying socket structure */
psock = sockfd_socket(sockfd); ret = sockfd_socket(sockfd, &psock);
/* Then let psock_connect() do all of the work */ /* 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) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+10 -2
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, int getpeername(int sockfd, FAR struct sockaddr *addr,
FAR socklen_t *addrlen) FAR socklen_t *addrlen)
{ {
FAR struct socket *psock = sockfd_socket(sockfd); FAR struct socket *psock;
int ret; int ret;
/* Get the underlying socket structure */
ret = sockfd_socket(sockfd, &psock);
/* Let psock_getpeername() do all of the work */ /* 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) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+10 -2
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, int getsockname(int sockfd, FAR struct sockaddr *addr,
FAR socklen_t *addrlen) FAR socklen_t *addrlen)
{ {
FAR struct socket *psock = sockfd_socket(sockfd); FAR struct socket *psock;
int ret; int ret;
/* Get the underlying socket structure */
ret = sockfd_socket(sockfd, &psock);
/* Let psock_getsockname() do all of the work */ /* 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) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+6 -2
View File
@@ -507,11 +507,15 @@ int getsockopt(int sockfd, int level, int option,
/* Get the underlying socket structure */ /* Get the underlying socket structure */
psock = sockfd_socket(sockfd); ret = sockfd_socket(sockfd, &psock);
/* Then let psock_getsockopt() do all of the work */ /* 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) if (ret < 0)
{ {
set_errno(-ret); set_errno(-ret);
+8 -24
View File
@@ -132,38 +132,22 @@ int psock_listen(FAR struct socket *psock, int backlog)
int listen(int sockfd, int backlog) int listen(int sockfd, int backlog)
{ {
FAR struct socket *psock = sockfd_socket(sockfd); FAR struct socket *psock;
FAR struct file *filep;
int errcode;
int ret; int ret;
/* Verify that the sockfd corresponds to valid, allocated socket */ /* Get the underlying socket structure */
if (psock == NULL || psock->s_conn == NULL) ret = sockfd_socket(sockfd, &psock);
{
/* 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;
}
/* The let psock_listen to the work. If psock_listen() fails, it will have /* The let psock_listen to the work. If psock_listen() fails, it will have
* set the errno variable. * set the errno variable.
*/ */
ret = psock_listen(psock, backlog); if (ret == OK)
{
ret = psock_listen(psock, backlog);
}
if (ret < 0) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+6 -2
View File
@@ -159,11 +159,15 @@ ssize_t recvfrom(int sockfd, FAR void *buf, size_t len, int flags,
/* Get the underlying socket structure */ /* Get the underlying socket structure */
psock = sockfd_socket(sockfd); ret = sockfd_socket(sockfd, &psock);
/* Then let psock_recvfrom() do all of the work */ /* 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) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+7 -3
View File
@@ -157,11 +157,15 @@ ssize_t recvmsg(int sockfd, FAR struct msghdr *msg, int flags)
/* Get the underlying socket structure */ /* 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) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+6 -2
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 */ /* Get the underlying socket structure */
psock = sockfd_socket(sockfd); ret = sockfd_socket(sockfd, &psock);
/* And let psock_send do all of the work */ /* 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) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+7 -3
View File
@@ -147,11 +147,15 @@ ssize_t sendmsg(int sockfd, FAR struct msghdr *msg, int flags)
/* Get the underlying socket structure */ /* 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) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+6 -2
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 */ /* Get the underlying socket structure */
psock = sockfd_socket(sockfd); ret = sockfd_socket(sockfd, &psock);
/* And let psock_sendto do all of the work */ /* 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) if (ret < 0)
{ {
_SO_SETERRNO(psock, -ret); _SO_SETERRNO(psock, -ret);
+6 -2
View File
@@ -642,11 +642,15 @@ int setsockopt(int sockfd, int level, int option, const void *value,
/* Get the underlying socket structure */ /* Get the underlying socket structure */
psock = sockfd_socket(sockfd); ret = sockfd_socket(sockfd, &psock);
/* Then let psock_setockopt() do all of the work */ /* 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) if (ret < 0)
{ {
set_errno(-ret); set_errno(-ret);