From 2c2aa94b7d95a3b0259ec228095e3e0f52a5f35d Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 30 Sep 2017 08:18:08 -0600 Subject: [PATCH] Squashed commit of the following: net/: psock_recvfrom() is an internal interface and should not set the errno nor should it be a cancellation point. net/: psock_accept() is not a cancellation point. net/: psock_getsockopt() and psock_socket*9 are an internal interfaces and should not set the errno. net/: psock_getsockopt() is an internal interface and should not set the errno. net/: psock_listen() is an internal interface and should not set the errno. net/: psock_connect(( is an internal interface and should not set the errno nor should it be a cancellation point. net/: psock_bind() is an internal interface and should not set the errno. net/: psock_accept() is an internal interface and should not set the errno. --- fs/nfs/rpc_clnt.c | 24 +++++++------- graphics/vnc/server/vnc_negotiate.c | 7 ++-- graphics/vnc/server/vnc_server.c | 6 +--- include/nuttx/net/net.h | 30 +++++++++-------- net/socket/accept.c | 38 +++++++--------------- net/socket/bind.c | 34 ++++++++++---------- net/socket/connect.c | 34 ++++++++------------ net/socket/getsockopt.c | 50 ++++++++++++----------------- net/socket/listen.c | 25 ++++++++------- net/socket/recvfrom.c | 43 +++++++------------------ net/socket/setsockopt.c | 47 +++++++++++---------------- net/socket/socket.c | 38 +++++++++------------- 12 files changed, 155 insertions(+), 221 deletions(-) diff --git a/fs/nfs/rpc_clnt.c b/fs/nfs/rpc_clnt.c index e5028fe1a9..61bfe7c6d4 100644 --- a/fs/nfs/rpc_clnt.c +++ b/fs/nfs/rpc_clnt.c @@ -203,7 +203,7 @@ static int rpcclnt_receive(FAR struct rpcclnt *rpc, FAR struct sockaddr *aname, nbytes = psock_recvfrom(rpc->rc_so, reply, resplen, 0, aname, &fromlen); if (nbytes < 0) { - error = get_errno(); + error = (int)-nbytes; ferr("ERROR: psock_recvfrom failed: %d\n", error); } @@ -403,9 +403,9 @@ int rpcclnt_connect(struct rpcclnt *rpc) error = psock_socket(saddr->sa_family, rpc->rc_sotype, IPPROTO_UDP, so); if (error < 0) { - errval = get_errno(); + errval = -error; ferr("ERROR: psock_socket failed: %d", errval); - return error; + return errval; } so->s_crefs = 1; @@ -422,7 +422,7 @@ int rpcclnt_connect(struct rpcclnt *rpc) (const void *)&tv, sizeof(tv)); if (error < 0) { - errval = get_errno(); + errval = -error; ferr("ERROR: psock_setsockopt failed: %d\n", errval); goto bad; } @@ -441,10 +441,11 @@ int rpcclnt_connect(struct rpcclnt *rpc) { tport--; sin.sin_port = htons(tport); + error = psock_bind(rpc->rc_so, (struct sockaddr *)&sin, sizeof(sin)); if (error < 0) { - errval = get_errno(); + errval = -error; ferr("ERROR: psock_bind failed: %d\n", errval); } } @@ -464,7 +465,7 @@ int rpcclnt_connect(struct rpcclnt *rpc) error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); if (error < 0) { - errval = get_errno(); + errval = -error; ferr("ERROR: psock_connect to PMAP port failed: %d", errval); goto bad; } @@ -493,7 +494,7 @@ int rpcclnt_connect(struct rpcclnt *rpc) error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); if (error < 0) { - errval = get_errno(); + errval = -error; ferr("ERROR: psock_connect MOUNTD port failed: %d\n", errval); goto bad; } @@ -530,7 +531,7 @@ int rpcclnt_connect(struct rpcclnt *rpc) error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); if (error < 0) { - errval = get_errno(); + errval = -error; ferr("ERROR: psock_connect PMAP port failed: %d\n", errval); goto bad; } @@ -552,8 +553,9 @@ int rpcclnt_connect(struct rpcclnt *rpc) sa->sin_port = htons(fxdr_unsigned(uint32_t, response.rdata.pmap.port)); error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); - if (error) + if (error < 0) { + error = -error; ferr("ERROR: psock_connect NFS port returns %d\n", error); goto bad; } @@ -621,7 +623,7 @@ int rpcclnt_umount(struct rpcclnt *rpc) ret = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); if (ret < 0) { - error = get_errno(); + error = -ret; ferr("ERROR: psock_connect failed [port=%d]: %d\n", ntohs(sa->sin_port), error); goto bad; @@ -646,7 +648,7 @@ int rpcclnt_umount(struct rpcclnt *rpc) ret = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); if (ret < 0) { - error = get_errno(); + error = -ret; ferr("ERROR: psock_connect failed [port=%d]: %d\n", ntohs(sa->sin_port), error); goto bad; diff --git a/graphics/vnc/server/vnc_negotiate.c b/graphics/vnc/server/vnc_negotiate.c index 026680ef56..31ee14322f 100644 --- a/graphics/vnc/server/vnc_negotiate.c +++ b/graphics/vnc/server/vnc_negotiate.c @@ -127,14 +127,13 @@ int vnc_negotiate(FAR struct vnc_session_s *session) tv.tv_sec = 5; tv.tv_usec = 0; + ret = psock_setsockopt(&session->connect, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)); if (ret < 0) { - errcode = get_errno(); - gerr("ERROR: Failed to set receive timeout: %d\n", errcode); - DEBUGASSERT(errcode > 0); - return -errcode; + gerr("ERROR: Failed to set receive timeout: %d\n", ret); + return ret; } #endif diff --git a/graphics/vnc/server/vnc_server.c b/graphics/vnc/server/vnc_server.c index 513d10c736..99fb929014 100644 --- a/graphics/vnc/server/vnc_server.c +++ b/graphics/vnc/server/vnc_server.c @@ -1,7 +1,7 @@ /**************************************************************************** * graphics/vnc/vnc_server.c * - * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Copyright (C) 2016=-2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -176,7 +176,6 @@ static int vnc_connect(FAR struct vnc_session_s *session, int port) ret = psock_socket(AF_INET, SOCK_STREAM, 0, &session->listen); if (ret < 0) { - ret = -get_errno(); return ret; } @@ -186,7 +185,6 @@ static int vnc_connect(FAR struct vnc_session_s *session, int port) sizeof(struct sockaddr_in)); if (ret < 0) { - ret = -get_errno(); goto errout_with_listener; } @@ -195,7 +193,6 @@ static int vnc_connect(FAR struct vnc_session_s *session, int port) ret = psock_listen(&session->listen, 5); if (ret < 0) { - ret = -get_errno(); goto errout_with_listener; } @@ -206,7 +203,6 @@ static int vnc_connect(FAR struct vnc_session_s *session, int port) ret = psock_accept(&session->listen, NULL, NULL, &session->connect); if (ret < 0) { - ret = -get_errno(); goto errout_with_listener; } diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h index b3c0da571b..ef171edf3a 100644 --- a/include/nuttx/net/net.h +++ b/include/nuttx/net/net.h @@ -422,7 +422,8 @@ FAR struct socket *sockfd_socket(int sockfd); * psock A pointer to a user allocated socket structure to be initialized. * * Returned Value: - * 0 on success; -1 on error with errno set appropriately + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error: * * EACCES * Permission to create a socket of the specified type and/or protocol @@ -499,7 +500,8 @@ int psock_close(FAR struct socket *psock); * addrlen Length of 'addr' * * Returned Value: - * 0 on success; -1 on error with errno set appropriately + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. * * EACCES * The address is protected, and the user is not the superuser. @@ -537,8 +539,8 @@ int psock_bind(FAR struct socket *psock, FAR const struct sockaddr *addr, * may be ignored so that retries succeed. * * Returned Value: - * On success, zero is returned. On error, -1 is returned, and errno is set - * appropriately. + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. * * EADDRINUSE * Another socket is already listening on the same port. @@ -584,8 +586,8 @@ int psock_listen(FAR struct socket *psock, int backlog); * newsock Location to return the accepted socket information. * * Returned Value: - * Returns 0 (OK) on success. On failure, it returns -1 (ERROR) with the - * errno variable set to indicate the nature of the error. + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. * * EAGAIN or EWOULDBLOCK * The socket is marked non-blocking and no connections are present to @@ -645,7 +647,8 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr, * addrlen Length of actual 'addr' * * Returned Value: - * 0 on success; -1 on error with errno set appropriately + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. * * EACCES, EPERM * The user tried to connect to a broadcast address without having the @@ -847,8 +850,8 @@ ssize_t psock_sendto(FAR struct socket *psock, FAR const void *buf, * Returned Value: * On success, returns the number of characters sent. If no data is * available to be received and the peer has performed an orderly shutdown, - * recv() will return 0. Otherwise, on errors, -1 is returned, and errno - * is set appropriately: + * recv() will return 0. Otherwise, on any failure, a negated errno value + * is returned. One of: * * EAGAIN * The socket is marked non-blocking and the receive operation would block, @@ -874,8 +877,6 @@ ssize_t psock_sendto(FAR struct socket *psock, FAR const void *buf, * ENOTSOCK * The argument sockfd does not refer to a socket. * - * Assumptions: - * ****************************************************************************/ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, @@ -912,6 +913,8 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, * value_len The length of the argument value * * Returned Value: + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error: * * EINVAL * The specified option is invalid at the specified socket 'level' or the @@ -924,8 +927,6 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, * Insufficient resources are available in the system to complete the * call. * - * Assumptions: - * ****************************************************************************/ int psock_getsockopt(FAR struct socket *psock, int level, int option, @@ -952,7 +953,8 @@ int psock_getsockopt(FAR struct socket *psock, int level, int option, * value_len The length of the argument value * * Returned Value: - * 0 on success; -1 on failure + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error: * * EDOM * The send and receive timeout values are too big to fit into the diff --git a/net/socket/accept.c b/net/socket/accept.c index bf58e25b56..5dfe85a4dc 100644 --- a/net/socket/accept.c +++ b/net/socket/accept.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/socket/accept.c * - * Copyright (C) 2007-2012, 2015-2016 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2012, 2015-2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -92,8 +92,8 @@ * newsock Location to return the accepted socket information. * * Returned Value: - * Returns 0 (OK) on success. On failure, it returns -1 (ERROR) with the - * errno variable set to indicate the nature of the error. + * Returns 0 (OK) on success. On failure, it returns a negated errno value + * to indicate the nature of the error. * * EAGAIN or EWOULDBLOCK * The socket is marked non-blocking and no connections are present to @@ -125,22 +125,16 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr, FAR socklen_t *addrlen, FAR struct socket *newsock) { - int errcode; int ret; DEBUGASSERT(psock != NULL && psock->s_conn != NULL && newsock != NULL); - /* Treat as a cancellation point */ - - (void)enter_cancellation_point(); - /* May sure that the socket has been opened with socket() */ if (psock == NULL || psock->s_conn == NULL) { nerr("ERROR: Socket invalid or not opened\n"); - errcode = EINVAL; - goto errout; + return -EINVAL; } /* Is the socket listening for a connection? */ @@ -148,8 +142,7 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr, if (!_SS_ISLISTENING(psock->s_flags)) { nerr("ERROR: Socket is not listening for a connection.\n"); - errcode = EINVAL; - goto errout; + return -EINVAL; } /* Let the address family's accept() method handle the operation */ @@ -161,7 +154,6 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr, if (ret < 0) { nerr("ERROR: si_accept failed: %d\n", ret); - errcode = -ret; goto errout_with_lock; } @@ -169,18 +161,12 @@ int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr, newsock->s_flags |= _SF_CONNECTED; newsock->s_flags &= ~_SF_CLOSED; - net_unlock(); - leave_cancellation_point(); - return OK; + ret = OK; errout_with_lock: net_unlock(); - -errout: - set_errno(errcode); - leave_cancellation_point(); - return ERROR; + return ret; } /**************************************************************************** @@ -307,11 +293,8 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen) ret = psock_accept(psock, addr, addrlen, newsock); if (ret < 0) { - /* The errno value has already been set */ - - sockfd_release(newfd); - leave_cancellation_point(); - return ERROR; + errcode = -ret; + goto errout_with_socket; } leave_cancellation_point(); @@ -321,8 +304,9 @@ errout_with_socket: sockfd_release(newfd); errout: - set_errno(errcode); leave_cancellation_point(); + + set_errno(errcode); return ERROR; } diff --git a/net/socket/bind.c b/net/socket/bind.c index b2c3382255..89e4961573 100644 --- a/net/socket/bind.c +++ b/net/socket/bind.c @@ -74,7 +74,8 @@ * addrlen Length of 'addr' * * Returned Value: - * 0 on success; -1 on error with errno set appropriately + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. * * EACCES * The address is protected, and the user is not the superuser. @@ -85,22 +86,18 @@ * ENOTSOCK * psock is a descriptor for a file, not a socket. * - * Assumptions: - * ****************************************************************************/ int psock_bind(FAR struct socket *psock, const struct sockaddr *addr, socklen_t addrlen) { - int errcode; int ret = OK; /* Verify that the psock corresponds to valid, allocated socket */ if (!psock || psock->s_crefs <= 0) { - errcode = ENOTSOCK; - goto errout; + return -ENOTSOCK; } /* Let the address family's connect() method handle the operation */ @@ -112,15 +109,10 @@ int psock_bind(FAR struct socket *psock, const struct sockaddr *addr, if (ret < 0) { - errcode = -ret; - goto errout; + return ret; } return OK; - -errout: - set_errno(errcode); - return ERROR; } /**************************************************************************** @@ -151,19 +143,27 @@ errout: * ENOTSOCK * sockfd is a descriptor for a file, not a socket. * - * Assumptions: - * ****************************************************************************/ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { - /* Make the socket descriptor to the underlying socket structure */ + FAR struct socket *psock; + int ret; - FAR struct socket *psock = sockfd_socket(sockfd); + /* Use the socket descriptor to get the underlying socket structure */ + + psock = sockfd_socket(sockfd); /* Then let psock_bind do all of the work */ - return psock_bind(psock, addr, addrlen); + ret = psock_bind(psock, addr, addrlen); + if (ret < 0) + { + set_errno(-ret); + return ERROR; + } + + return OK; } #endif /* CONFIG_NET */ diff --git a/net/socket/connect.c b/net/socket/connect.c index e3b6556da2..af8884446e 100644 --- a/net/socket/connect.c +++ b/net/socket/connect.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/socket/connect.c * - * Copyright (C) 2007-2012, 2015-2016 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2012, 2015-2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -85,7 +85,8 @@ * addrlen Length of actual 'addr' * * Returned Value: - * 0 on success; -1 on error with errno set appropriately + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. * * EACCES, EPERM * The user tried to connect to a broadcast address without having the @@ -130,27 +131,20 @@ int psock_connect(FAR struct socket *psock, FAR const struct sockaddr *addr, socklen_t addrlen) { - int errcode; int ret; - /* Treat as a cancellation point */ - - (void)enter_cancellation_point(); - /* Verify that the psock corresponds to valid, allocated socket */ if (psock == NULL || psock->s_crefs <= 0) { - errcode = EBADF; - goto errout; + return -EBADF; } /* Make sure that an address was provided */ if (addr == NULL) { - errcode = EFAULT; - goto errout; + return -EFAULT; } /* Let the address family's connect() method handle the operation */ @@ -159,17 +153,10 @@ int psock_connect(FAR struct socket *psock, FAR const struct sockaddr *addr, ret = psock->s_sockif->si_connect(psock, addr, addrlen); if (ret < 0) { - errcode = -ret; - goto errout; + return ret; } - leave_cancellation_point(); return OK; - -errout: - set_errno(errcode); - leave_cancellation_point(); - return ERROR; } /**************************************************************************** @@ -243,6 +230,7 @@ errout: int connect(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen) { + FAR struct socket *psock; int ret; /* accept() is a cancellation point */ @@ -251,11 +239,17 @@ int connect(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen) /* Get the underlying socket structure */ - FAR struct socket *psock = sockfd_socket(sockfd); + psock = sockfd_socket(sockfd); /* Then let psock_connect() do all of the work */ ret = psock_connect(psock, addr, addrlen); + if (ret < 0) + { + set_errno(-ret); + ret = ERROR; + } + leave_cancellation_point(); return ret; } diff --git a/net/socket/getsockopt.c b/net/socket/getsockopt.c index 00841fe511..eebaefe019 100644 --- a/net/socket/getsockopt.c +++ b/net/socket/getsockopt.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/socket/getsockopt.c * - * Copyright (C) 2007-2009, 2012, 2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2012, 2014, 2017 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -80,6 +80,8 @@ * value_len The length of the argument value * * Returned Value: + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. * * EINVAL * The specified option is invalid at the specified socket 'level' or the @@ -92,21 +94,16 @@ * Insufficient resources are available in the system to complete the * call. * - * Assumptions: - * ****************************************************************************/ int psock_getsockopt(FAR struct socket *psock, int level, int option, FAR void *value, FAR socklen_t *value_len) { - int errcode; - /* Verify that the socket option if valid (but might not be supported ) */ if (!_SO_GETVALID(option) || !value || !value_len) { - errcode = EINVAL; - goto errout; + return -EINVAL; } #ifdef CONFIG_NET_USRSOCK @@ -130,14 +127,7 @@ int psock_getsockopt(FAR struct socket *psock, int level, int option, default: /* Other options are passed to usrsock daemon. */ { - ret = usrsock_getsockopt(conn, level, option, value, value_len); - if (ret < 0) - { - errcode = -ret; - goto errout; - } - - return OK; + return usrsock_getsockopt(conn, level, option, value, value_len); } } } @@ -168,8 +158,7 @@ int psock_getsockopt(FAR struct socket *psock, int level, int option, if (*value_len < sizeof(int)) { - errcode = EINVAL; - goto errout; + return -EINVAL; } /* Sample the current options. This is atomic operation and so @@ -192,8 +181,7 @@ int psock_getsockopt(FAR struct socket *psock, int level, int option, if (*value_len < sizeof(int)) { - errcode = EINVAL; - goto errout; + return -EINVAL; } #ifdef CONFIG_NET_USRSOCK @@ -230,8 +218,7 @@ int psock_getsockopt(FAR struct socket *psock, int level, int option, if (*value_len < sizeof(struct timeval)) { - errcode = EINVAL; - goto errout; + return -EINVAL; } /* Get the timeout value. This is a atomic operation and should @@ -265,15 +252,10 @@ int psock_getsockopt(FAR struct socket *psock, int level, int option, case SO_SNDLOWAT: /* Sets the minimum number of bytes to output */ default: - errcode = ENOPROTOOPT; - goto errout; + return -ENOPROTOOPT; } return OK; - -errout: - set_errno(errcode); - return ERROR; } /**************************************************************************** @@ -301,6 +283,8 @@ errout: * value_len The length of the argument value * * Returned Value: + * Returns zero (OK) on success. On failure, -1 (ERROR) is returned and th + * errno variable is set appropriately: * * EBADF * The 'sockfd' argument is not a valid socket descriptor. @@ -315,13 +299,12 @@ errout: * 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) { FAR struct socket *psock; + int ret; /* Get the underlying socket structure */ /* Verify that the sockfd corresponds to valid, allocated socket */ @@ -335,7 +318,14 @@ int getsockopt(int sockfd, int level, int option, void *value, socklen_t *value_ /* Then let psock_getsockopt() do all of the work */ - return psock_getsockopt(psock, level, option, value, value_len); + ret = psock_getsockopt(psock, level, option, value, value_len); + if (ret < 0) + { + set_errno(-ret); + return ERROR; + } + + return OK; } #endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS */ diff --git a/net/socket/listen.c b/net/socket/listen.c index fd4a410c94..8bc1be5ca6 100644 --- a/net/socket/listen.c +++ b/net/socket/listen.c @@ -71,8 +71,8 @@ * may be ignored so that retries succeed. * * Returned Value: - * On success, zero is returned. On error, -1 is returned, and errno is set - * appropriately. + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error. * * EADDRINUSE * Another socket is already listening on the same port. @@ -83,7 +83,6 @@ int psock_listen(FAR struct socket *psock, int backlog) { - int errcode; int ret; DEBUGASSERT(psock != NULL); @@ -93,8 +92,7 @@ int psock_listen(FAR struct socket *psock, int backlog) if (psock == NULL || psock->s_conn == NULL) { nerr("ERROR: Invalid or unconnected socket\n"); - errcode = EINVAL; - goto errout; + return -EINVAL; } /* Let the address family's listen() method handle the operation */ @@ -104,16 +102,11 @@ int psock_listen(FAR struct socket *psock, int backlog) if (ret < 0) { nerr("ERROR: si_listen failed: %d\n", ret); - errcode = -ret; - goto errout; + return ret; } psock->s_flags |= _SF_LISTENING; return OK; - -errout: - set_errno(errcode); - return ERROR; } /**************************************************************************** @@ -153,6 +146,7 @@ int listen(int sockfd, int backlog) { FAR struct socket *psock = sockfd_socket(sockfd); int errcode; + int ret; /* Verify that the sockfd corresponds to valid, allocated socket */ @@ -182,7 +176,14 @@ int listen(int sockfd, int backlog) * set the errno variable. */ - return psock_listen(psock, backlog); + ret = psock_listen(psock, backlog); + if (ret < 0) + { + set_errno(-ret); + return ERROR; + } + + return OK; } #endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/net/socket/recvfrom.c b/net/socket/recvfrom.c index 3f86ce33a2..0f78d08a7f 100644 --- a/net/socket/recvfrom.c +++ b/net/socket/recvfrom.c @@ -74,10 +74,10 @@ * fromlen The length of the address structure * * Returned Value: - * On success, returns the number of characters received. If no data is + * On success, returns the number of characters sent. If no data is * available to be received and the peer has performed an orderly shutdown, - * recv() will return 0. Otherwise, on errors, -1 is returned, and errno - * is set appropriately: + * recv() will return 0. Otherwise, on any failure, a negated errno value + * is returned. One of: * * EAGAIN * The socket is marked non-blocking and the receive operation would block, @@ -103,8 +103,6 @@ * ENOTSOCK * The argument sockfd does not refer to a socket. * - * Assumptions: - * ****************************************************************************/ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, @@ -112,34 +110,26 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, FAR socklen_t *fromlen) { ssize_t ret; - int errcode; - - /* Treat as a cancellation point */ - - (void)enter_cancellation_point(); /* Verify that non-NULL pointers were passed */ #ifdef CONFIG_DEBUG_FEATURES if (!buf) { - errcode = EINVAL; - goto errout; + return -EINVAL; } #endif if (from != NULL && fromlen != NULL && *fromlen <= 0) { - errcode = EINVAL; - goto errout; + return -EINVAL; } /* Verify that the sockfd corresponds to valid, allocated socket */ if (psock == NULL || psock->s_crefs <= 0) { - errcode = EBADF; - goto errout; + return -EBADF; } /* Set the socket state to receiving */ @@ -158,20 +148,7 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, /* Set the socket state to idle */ psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_IDLE); - - if (ret < 0) - { - errcode = -ret; - goto errout; - } - - leave_cancellation_point(); return ret; - -errout: - set_errno(errcode); - leave_cancellation_point(); - return ERROR; } /**************************************************************************** @@ -222,8 +199,6 @@ errout: * ENOTSOCK * The argument sockfd does not refer to a socket. * - * Assumptions: - * ****************************************************************************/ ssize_t recvfrom(int sockfd, FAR void *buf, size_t len, int flags, @@ -243,6 +218,12 @@ ssize_t recvfrom(int sockfd, FAR void *buf, size_t len, int flags, /* Then let psock_recvfrom() do all of the work */ ret = psock_recvfrom(psock, buf, len, flags, from, fromlen); + if (ret < 0) + { + set_errno(-ret); + ret = ERROR; + } + leave_cancellation_point(); return ret; } diff --git a/net/socket/setsockopt.c b/net/socket/setsockopt.c index 856f2ce700..522658e460 100644 --- a/net/socket/setsockopt.c +++ b/net/socket/setsockopt.c @@ -1,8 +1,8 @@ /**************************************************************************** * net/socket/setsockopt.c * - * Copyright (C) 2007, 2008, 2011-2012, 2014-2015 Gregory Nutt. All rights - * reserved. + * Copyright (C) 2007, 2008, 2011-2012, 2014-2015, 2017 Gregory Nutt. All + * rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -80,7 +80,8 @@ * value_len The length of the argument value * * Returned Value: - * 0 on success; -1 on failure + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error: * * EDOM * The send and receive timeout values are too big to fit into the @@ -108,14 +109,11 @@ int psock_setsockopt(FAR struct socket *psock, int level, int option, FAR const void *value, socklen_t value_len) { - int errcode; - /* Verify that the socket option if valid (but might not be supported ) */ if (!_SO_SETVALID(option) || !value) { - errcode = EINVAL; - goto errout; + return -EINVAL; } #ifdef CONFIG_NET_USRSOCK @@ -138,14 +136,7 @@ int psock_setsockopt(FAR struct socket *psock, int level, int option, default: /* Other options are passed to usrsock daemon. */ { - ret = usrsock_setsockopt(conn, level, option, value, value_len); - if (ret < 0) - { - errcode = -ret; - goto errout; - } - - return OK; + return usrsock_setsockopt(conn, level, option, value, value_len); } } } @@ -176,8 +167,7 @@ int psock_setsockopt(FAR struct socket *psock, int level, int option, if (value_len != sizeof(int)) { - errcode = EINVAL; - goto errout; + return -EINVAL; } /* Get the value. Is the option being set or cleared? */ @@ -215,8 +205,7 @@ int psock_setsockopt(FAR struct socket *psock, int level, int option, if (tv == NULL || value_len != sizeof(struct timeval)) { - errcode = EINVAL; - goto errout; + return -EINVAL; } /* Get the timeout value. Any microsecond remainder will be @@ -258,8 +247,7 @@ int psock_setsockopt(FAR struct socket *psock, int level, int option, if (value_len < sizeof(FAR struct linger)) { - errcode = EINVAL; - goto errout; + return -EINVAL; } /* Get the value. Is the option being set or cleared? */ @@ -303,15 +291,10 @@ int psock_setsockopt(FAR struct socket *psock, int level, int option, case SO_TYPE: /* Reports the socket type */ default: - errcode = ENOPROTOOPT; - goto errout; + return -ENOPROTOOPT; } return OK; - -errout: - set_errno(errcode); - return ERROR; } /**************************************************************************** @@ -366,6 +349,7 @@ errout: int setsockopt(int sockfd, int level, int option, const void *value, socklen_t value_len) { FAR struct socket *psock; + int ret; /* Get the underlying socket structure */ /* Verify that the sockfd corresponds to valid, allocated socket */ @@ -379,7 +363,14 @@ int setsockopt(int sockfd, int level, int option, const void *value, socklen_t v /* Then let psock_setockopt() do all of the work */ - return psock_setsockopt(psock, level, option, value, value_len); + ret = psock_setsockopt(psock, level, option, value, value_len); + if (ret < 0) + { + set_errno(-ret); + return ERROR; + } + + return OK; } #endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS */ diff --git a/net/socket/socket.c b/net/socket/socket.c index eb7db70d7d..d231a4dae9 100644 --- a/net/socket/socket.c +++ b/net/socket/socket.c @@ -67,7 +67,8 @@ * psock A pointer to a user allocated socket structure to be initialized. * * Returned Value: - * 0 on success; -1 on error with errno set appropriately + * Returns zero (OK) on success. On failure, it returns a negated errno + * value to indicate the nature of the error: * * EACCES * Permission to create a socket of the specified type and/or protocol @@ -87,14 +88,11 @@ * The protocol type or the specified protocol is not supported within * this domain. * - * Assumptions: - * ****************************************************************************/ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock) { FAR const struct sock_intf_s *sockif = NULL; - int errcode; int ret; /* Initialize the socket structure */ @@ -126,8 +124,7 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock) if (ret < 0) { - errcode = -ret; - goto errout; + return ret; } return ret; @@ -141,8 +138,7 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock) if (sockif == NULL) { nerr("ERROR: socket address family unsupported: %d\n", domain); - errcode = EAFNOSUPPORT; - goto errout; + return -EAFNOSUPPORT; } /* The remaining of the socket initialization depends on the address @@ -156,15 +152,10 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock) if (ret < 0) { nerr("ERROR: socket si_setup() failed: %d\n", ret); - errcode = -ret; - goto errout; + return ret; } return OK; - -errout: - set_errno(errcode); - return ERROR; } /**************************************************************************** @@ -207,6 +198,7 @@ errout: int socket(int domain, int type, int protocol) { FAR struct socket *psock; + int errcode; int sockfd; int ret; @@ -216,8 +208,8 @@ int socket(int domain, int type, int protocol) if (sockfd < 0) { nerr("ERROR: Failed to allodate a socket descriptor\n"); - set_errno(ENFILE); - return ERROR; + errcode = ENFILE; + goto errout; } /* Get the underlying socket structure */ @@ -225,8 +217,8 @@ int socket(int domain, int type, int protocol) psock = sockfd_socket(sockfd); if (!psock) { - set_errno(ENOSYS); /* should not happen */ - goto errout; + errcode = ENOSYS; /* should not happen */ + goto errout_with_sockfd; } /* Initialize the socket structure */ @@ -234,16 +226,18 @@ int socket(int domain, int type, int protocol) ret = psock_socket(domain, type, protocol, psock); if (ret < 0) { - /* errno already set by psock_socket() */ - nerr("ERROR: psock_socket() failed: %d\n", ret); - goto errout; + errcode = -ret; + goto errout_with_sockfd; } return sockfd; -errout: +errout_with_sockfd: sockfd_release(sockfd); + +errout: + set_errno(errcode); return ERROR; }