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.
This commit is contained in:
Gregory Nutt 2017-09-30 08:18:08 -06:00
parent 054b147114
commit 2c2aa94b7d
12 changed files with 155 additions and 221 deletions

View File

@ -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); nbytes = psock_recvfrom(rpc->rc_so, reply, resplen, 0, aname, &fromlen);
if (nbytes < 0) if (nbytes < 0)
{ {
error = get_errno(); error = (int)-nbytes;
ferr("ERROR: psock_recvfrom failed: %d\n", error); 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); error = psock_socket(saddr->sa_family, rpc->rc_sotype, IPPROTO_UDP, so);
if (error < 0) if (error < 0)
{ {
errval = get_errno(); errval = -error;
ferr("ERROR: psock_socket failed: %d", errval); ferr("ERROR: psock_socket failed: %d", errval);
return error; return errval;
} }
so->s_crefs = 1; so->s_crefs = 1;
@ -422,7 +422,7 @@ int rpcclnt_connect(struct rpcclnt *rpc)
(const void *)&tv, sizeof(tv)); (const void *)&tv, sizeof(tv));
if (error < 0) if (error < 0)
{ {
errval = get_errno(); errval = -error;
ferr("ERROR: psock_setsockopt failed: %d\n", errval); ferr("ERROR: psock_setsockopt failed: %d\n", errval);
goto bad; goto bad;
} }
@ -441,10 +441,11 @@ int rpcclnt_connect(struct rpcclnt *rpc)
{ {
tport--; tport--;
sin.sin_port = htons(tport); sin.sin_port = htons(tport);
error = psock_bind(rpc->rc_so, (struct sockaddr *)&sin, sizeof(sin)); error = psock_bind(rpc->rc_so, (struct sockaddr *)&sin, sizeof(sin));
if (error < 0) if (error < 0)
{ {
errval = get_errno(); errval = -error;
ferr("ERROR: psock_bind failed: %d\n", errval); 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)); error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr));
if (error < 0) if (error < 0)
{ {
errval = get_errno(); errval = -error;
ferr("ERROR: psock_connect to PMAP port failed: %d", errval); ferr("ERROR: psock_connect to PMAP port failed: %d", errval);
goto bad; goto bad;
} }
@ -493,7 +494,7 @@ int rpcclnt_connect(struct rpcclnt *rpc)
error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr));
if (error < 0) if (error < 0)
{ {
errval = get_errno(); errval = -error;
ferr("ERROR: psock_connect MOUNTD port failed: %d\n", errval); ferr("ERROR: psock_connect MOUNTD port failed: %d\n", errval);
goto bad; goto bad;
} }
@ -530,7 +531,7 @@ int rpcclnt_connect(struct rpcclnt *rpc)
error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr));
if (error < 0) if (error < 0)
{ {
errval = get_errno(); errval = -error;
ferr("ERROR: psock_connect PMAP port failed: %d\n", errval); ferr("ERROR: psock_connect PMAP port failed: %d\n", errval);
goto bad; goto bad;
} }
@ -552,8 +553,9 @@ int rpcclnt_connect(struct rpcclnt *rpc)
sa->sin_port = htons(fxdr_unsigned(uint32_t, response.rdata.pmap.port)); sa->sin_port = htons(fxdr_unsigned(uint32_t, response.rdata.pmap.port));
error = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); 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); ferr("ERROR: psock_connect NFS port returns %d\n", error);
goto bad; goto bad;
} }
@ -621,7 +623,7 @@ int rpcclnt_umount(struct rpcclnt *rpc)
ret = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); ret = psock_connect(rpc->rc_so, saddr, sizeof(*saddr));
if (ret < 0) if (ret < 0)
{ {
error = get_errno(); error = -ret;
ferr("ERROR: psock_connect failed [port=%d]: %d\n", ferr("ERROR: psock_connect failed [port=%d]: %d\n",
ntohs(sa->sin_port), error); ntohs(sa->sin_port), error);
goto bad; goto bad;
@ -646,7 +648,7 @@ int rpcclnt_umount(struct rpcclnt *rpc)
ret = psock_connect(rpc->rc_so, saddr, sizeof(*saddr)); ret = psock_connect(rpc->rc_so, saddr, sizeof(*saddr));
if (ret < 0) if (ret < 0)
{ {
error = get_errno(); error = -ret;
ferr("ERROR: psock_connect failed [port=%d]: %d\n", ferr("ERROR: psock_connect failed [port=%d]: %d\n",
ntohs(sa->sin_port), error); ntohs(sa->sin_port), error);
goto bad; goto bad;

View File

@ -127,14 +127,13 @@ int vnc_negotiate(FAR struct vnc_session_s *session)
tv.tv_sec = 5; tv.tv_sec = 5;
tv.tv_usec = 0; tv.tv_usec = 0;
ret = psock_setsockopt(&session->connect, SOL_SOCKET, SO_RCVTIMEO, ret = psock_setsockopt(&session->connect, SOL_SOCKET, SO_RCVTIMEO,
&tv, sizeof(struct timeval)); &tv, sizeof(struct timeval));
if (ret < 0) if (ret < 0)
{ {
errcode = get_errno(); gerr("ERROR: Failed to set receive timeout: %d\n", ret);
gerr("ERROR: Failed to set receive timeout: %d\n", errcode); return ret;
DEBUGASSERT(errcode > 0);
return -errcode;
} }
#endif #endif

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* graphics/vnc/vnc_server.c * 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 <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * 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); ret = psock_socket(AF_INET, SOCK_STREAM, 0, &session->listen);
if (ret < 0) if (ret < 0)
{ {
ret = -get_errno();
return ret; return ret;
} }
@ -186,7 +185,6 @@ static int vnc_connect(FAR struct vnc_session_s *session, int port)
sizeof(struct sockaddr_in)); sizeof(struct sockaddr_in));
if (ret < 0) if (ret < 0)
{ {
ret = -get_errno();
goto errout_with_listener; 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); ret = psock_listen(&session->listen, 5);
if (ret < 0) if (ret < 0)
{ {
ret = -get_errno();
goto errout_with_listener; 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); ret = psock_accept(&session->listen, NULL, NULL, &session->connect);
if (ret < 0) if (ret < 0)
{ {
ret = -get_errno();
goto errout_with_listener; goto errout_with_listener;
} }

View File

@ -422,7 +422,8 @@ FAR struct socket *sockfd_socket(int sockfd);
* psock A pointer to a user allocated socket structure to be initialized. * psock A pointer to a user allocated socket structure to be initialized.
* *
* Returned Value: * 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 * EACCES
* Permission to create a socket of the specified type and/or protocol * 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' * addrlen Length of 'addr'
* *
* Returned Value: * 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 * EACCES
* The address is protected, and the user is not the superuser. * 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. * may be ignored so that retries succeed.
* *
* Returned Value: * Returned Value:
* On success, zero is returned. On error, -1 is returned, and errno is set * Returns zero (OK) on success. On failure, it returns a negated errno
* appropriately. * value to indicate the nature of the error.
* *
* EADDRINUSE * EADDRINUSE
* Another socket is already listening on the same port. * 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. * newsock Location to return the accepted socket information.
* *
* Returned Value: * Returned Value:
* Returns 0 (OK) on success. On failure, it returns -1 (ERROR) with the * Returns zero (OK) on success. On failure, it returns a negated errno
* errno variable set to indicate the nature of the error. * value to indicate the nature of the error.
* *
* EAGAIN or EWOULDBLOCK * EAGAIN or EWOULDBLOCK
* The socket is marked non-blocking and no connections are present to * 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' * addrlen Length of actual 'addr'
* *
* Returned Value: * 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 * EACCES, EPERM
* The user tried to connect to a broadcast address without having the * 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: * Returned Value:
* On success, returns the number of characters sent. 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, * available to be received and the peer has performed an orderly shutdown,
* recv() will return 0. Otherwise, on errors, -1 is returned, and errno * recv() will return 0. Otherwise, on any failure, a negated errno value
* is set appropriately: * is returned. One of:
* *
* EAGAIN * EAGAIN
* The socket is marked non-blocking and the receive operation would block, * 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 * ENOTSOCK
* The argument sockfd does not refer to a socket. * The argument sockfd does not refer to a socket.
* *
* Assumptions:
*
****************************************************************************/ ****************************************************************************/
ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, 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 * value_len The length of the argument value
* *
* Returned Value: * Returned Value:
* Returns zero (OK) on success. On failure, it returns a negated errno
* value to indicate the nature of the error:
* *
* EINVAL * EINVAL
* The specified option is invalid at the specified socket 'level' or the * 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 * Insufficient resources are available in the system to complete the
* call. * call.
* *
* Assumptions:
*
****************************************************************************/ ****************************************************************************/
int psock_getsockopt(FAR struct socket *psock, int level, int option, 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 * value_len The length of the argument value
* *
* Returned 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 * EDOM
* The send and receive timeout values are too big to fit into the * The send and receive timeout values are too big to fit into the

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* net/socket/accept.c * 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 <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -92,8 +92,8 @@
* newsock Location to return the accepted socket information. * newsock Location to return the accepted socket information.
* *
* Returned Value: * Returned Value:
* Returns 0 (OK) on success. On failure, it returns -1 (ERROR) with the * Returns 0 (OK) on success. On failure, it returns a negated errno value
* errno variable set to indicate the nature of the error. * to indicate the nature of the error.
* *
* EAGAIN or EWOULDBLOCK * EAGAIN or EWOULDBLOCK
* The socket is marked non-blocking and no connections are present to * 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, int psock_accept(FAR struct socket *psock, FAR struct sockaddr *addr,
FAR socklen_t *addrlen, FAR struct socket *newsock) FAR socklen_t *addrlen, FAR struct socket *newsock)
{ {
int errcode;
int ret; int ret;
DEBUGASSERT(psock != NULL && psock->s_conn != NULL && newsock != NULL); 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() */ /* May sure that the socket has been opened with socket() */
if (psock == NULL || psock->s_conn == NULL) if (psock == NULL || psock->s_conn == NULL)
{ {
nerr("ERROR: Socket invalid or not opened\n"); nerr("ERROR: Socket invalid or not opened\n");
errcode = EINVAL; return -EINVAL;
goto errout;
} }
/* Is the socket listening for a connection? */ /* 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)) if (!_SS_ISLISTENING(psock->s_flags))
{ {
nerr("ERROR: Socket is not listening for a connection.\n"); nerr("ERROR: Socket is not listening for a connection.\n");
errcode = EINVAL; return -EINVAL;
goto errout;
} }
/* Let the address family's accept() method handle the operation */ /* 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) if (ret < 0)
{ {
nerr("ERROR: si_accept failed: %d\n", ret); nerr("ERROR: si_accept failed: %d\n", ret);
errcode = -ret;
goto errout_with_lock; 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_CONNECTED;
newsock->s_flags &= ~_SF_CLOSED; newsock->s_flags &= ~_SF_CLOSED;
net_unlock();
leave_cancellation_point(); ret = OK;
return OK;
errout_with_lock: errout_with_lock:
net_unlock(); net_unlock();
return ret;
errout:
set_errno(errcode);
leave_cancellation_point();
return ERROR;
} }
/**************************************************************************** /****************************************************************************
@ -307,11 +293,8 @@ int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
ret = psock_accept(psock, addr, addrlen, newsock); ret = psock_accept(psock, addr, addrlen, newsock);
if (ret < 0) if (ret < 0)
{ {
/* The errno value has already been set */ errcode = -ret;
goto errout_with_socket;
sockfd_release(newfd);
leave_cancellation_point();
return ERROR;
} }
leave_cancellation_point(); leave_cancellation_point();
@ -321,8 +304,9 @@ errout_with_socket:
sockfd_release(newfd); sockfd_release(newfd);
errout: errout:
set_errno(errcode);
leave_cancellation_point(); leave_cancellation_point();
set_errno(errcode);
return ERROR; return ERROR;
} }

View File

@ -74,7 +74,8 @@
* addrlen Length of 'addr' * addrlen Length of 'addr'
* *
* Returned Value: * 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 * EACCES
* The address is protected, and the user is not the superuser. * The address is protected, and the user is not the superuser.
@ -85,22 +86,18 @@
* ENOTSOCK * ENOTSOCK
* psock is a descriptor for a file, not a socket. * psock is a descriptor for a file, not a socket.
* *
* Assumptions:
*
****************************************************************************/ ****************************************************************************/
int psock_bind(FAR struct socket *psock, const struct sockaddr *addr, int psock_bind(FAR struct socket *psock, const struct sockaddr *addr,
socklen_t addrlen) socklen_t addrlen)
{ {
int errcode;
int ret = OK; int ret = OK;
/* Verify that the psock corresponds to valid, allocated socket */ /* Verify that the psock corresponds to valid, allocated socket */
if (!psock || psock->s_crefs <= 0) if (!psock || psock->s_crefs <= 0)
{ {
errcode = ENOTSOCK; return -ENOTSOCK;
goto errout;
} }
/* Let the address family's connect() method handle the operation */ /* 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) if (ret < 0)
{ {
errcode = -ret; return ret;
goto errout;
} }
return OK; return OK;
errout:
set_errno(errcode);
return ERROR;
} }
/**************************************************************************** /****************************************************************************
@ -151,19 +143,27 @@ errout:
* ENOTSOCK * ENOTSOCK
* sockfd is a descriptor for a file, not a socket. * sockfd is a descriptor for a file, not a socket.
* *
* Assumptions:
*
****************************************************************************/ ****************************************************************************/
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) 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 */ /* 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 */ #endif /* CONFIG_NET */

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* net/socket/connect.c * 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 <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -85,7 +85,8 @@
* addrlen Length of actual 'addr' * addrlen Length of actual 'addr'
* *
* Returned Value: * 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 * EACCES, EPERM
* The user tried to connect to a broadcast address without having the * 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, int psock_connect(FAR struct socket *psock, FAR const struct sockaddr *addr,
socklen_t addrlen) socklen_t addrlen)
{ {
int errcode;
int ret; int ret;
/* Treat as a cancellation point */
(void)enter_cancellation_point();
/* Verify that the psock corresponds to valid, allocated socket */ /* Verify that the psock corresponds to valid, allocated socket */
if (psock == NULL || psock->s_crefs <= 0) if (psock == NULL || psock->s_crefs <= 0)
{ {
errcode = EBADF; return -EBADF;
goto errout;
} }
/* Make sure that an address was provided */ /* Make sure that an address was provided */
if (addr == NULL) if (addr == NULL)
{ {
errcode = EFAULT; return -EFAULT;
goto errout;
} }
/* Let the address family's connect() method handle the operation */ /* 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); ret = psock->s_sockif->si_connect(psock, addr, addrlen);
if (ret < 0) if (ret < 0)
{ {
errcode = -ret; return ret;
goto errout;
} }
leave_cancellation_point();
return OK; 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) int connect(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen)
{ {
FAR struct socket *psock;
int ret; int ret;
/* accept() is a cancellation point */ /* 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 */ /* 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 */ /* Then let psock_connect() do all of the work */
ret = psock_connect(psock, addr, addrlen); ret = psock_connect(psock, addr, addrlen);
if (ret < 0)
{
set_errno(-ret);
ret = ERROR;
}
leave_cancellation_point(); leave_cancellation_point();
return ret; return ret;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* net/socket/getsockopt.c * 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 <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -80,6 +80,8 @@
* value_len The length of the argument value * value_len The length of the argument value
* *
* Returned Value: * Returned Value:
* Returns zero (OK) on success. On failure, it returns a negated errno
* value to indicate the nature of the error.
* *
* EINVAL * EINVAL
* The specified option is invalid at the specified socket 'level' or the * 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 * Insufficient resources are available in the system to complete the
* call. * call.
* *
* Assumptions:
*
****************************************************************************/ ****************************************************************************/
int psock_getsockopt(FAR struct socket *psock, int level, int option, int psock_getsockopt(FAR struct socket *psock, int level, int option,
FAR void *value, FAR socklen_t *value_len) FAR void *value, FAR socklen_t *value_len)
{ {
int errcode;
/* Verify that the socket option if valid (but might not be supported ) */ /* Verify that the socket option if valid (but might not be supported ) */
if (!_SO_GETVALID(option) || !value || !value_len) if (!_SO_GETVALID(option) || !value || !value_len)
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
#ifdef CONFIG_NET_USRSOCK #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. */ default: /* Other options are passed to usrsock daemon. */
{ {
ret = usrsock_getsockopt(conn, level, option, value, value_len); return usrsock_getsockopt(conn, level, option, value, value_len);
if (ret < 0)
{
errcode = -ret;
goto errout;
}
return OK;
} }
} }
} }
@ -168,8 +158,7 @@ int psock_getsockopt(FAR struct socket *psock, int level, int option,
if (*value_len < sizeof(int)) if (*value_len < sizeof(int))
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
/* Sample the current options. This is atomic operation and so /* 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)) if (*value_len < sizeof(int))
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
#ifdef CONFIG_NET_USRSOCK #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)) if (*value_len < sizeof(struct timeval))
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
/* Get the timeout value. This is a atomic operation and should /* 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 */ case SO_SNDLOWAT: /* Sets the minimum number of bytes to output */
default: default:
errcode = ENOPROTOOPT; return -ENOPROTOOPT;
goto errout;
} }
return OK; return OK;
errout:
set_errno(errcode);
return ERROR;
} }
/**************************************************************************** /****************************************************************************
@ -301,6 +283,8 @@ errout:
* value_len The length of the argument value * value_len The length of the argument value
* *
* Returned Value: * Returned Value:
* Returns zero (OK) on success. On failure, -1 (ERROR) is returned and th
* errno variable is set appropriately:
* *
* EBADF * EBADF
* The 'sockfd' argument is not a valid socket descriptor. * The 'sockfd' argument is not a valid socket descriptor.
@ -315,13 +299,12 @@ errout:
* Insufficient resources are available in the system to complete the * Insufficient resources are available in the system to complete the
* call. * call.
* *
* Assumptions:
*
****************************************************************************/ ****************************************************************************/
int getsockopt(int sockfd, int level, int option, void *value, socklen_t *value_len) int getsockopt(int sockfd, int level, int option, void *value, socklen_t *value_len)
{ {
FAR struct socket *psock; FAR struct socket *psock;
int ret;
/* Get the underlying socket structure */ /* Get the underlying socket structure */
/* Verify that the sockfd corresponds to valid, allocated socket */ /* 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 */ /* 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 */ #endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS */

View File

@ -71,8 +71,8 @@
* may be ignored so that retries succeed. * may be ignored so that retries succeed.
* *
* Returned Value: * Returned Value:
* On success, zero is returned. On error, -1 is returned, and errno is set * Returns zero (OK) on success. On failure, it returns a negated errno
* appropriately. * value to indicate the nature of the error.
* *
* EADDRINUSE * EADDRINUSE
* Another socket is already listening on the same port. * Another socket is already listening on the same port.
@ -83,7 +83,6 @@
int psock_listen(FAR struct socket *psock, int backlog) int psock_listen(FAR struct socket *psock, int backlog)
{ {
int errcode;
int ret; int ret;
DEBUGASSERT(psock != NULL); DEBUGASSERT(psock != NULL);
@ -93,8 +92,7 @@ int psock_listen(FAR struct socket *psock, int backlog)
if (psock == NULL || psock->s_conn == NULL) if (psock == NULL || psock->s_conn == NULL)
{ {
nerr("ERROR: Invalid or unconnected socket\n"); nerr("ERROR: Invalid or unconnected socket\n");
errcode = EINVAL; return -EINVAL;
goto errout;
} }
/* Let the address family's listen() method handle the operation */ /* 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) if (ret < 0)
{ {
nerr("ERROR: si_listen failed: %d\n", ret); nerr("ERROR: si_listen failed: %d\n", ret);
errcode = -ret; return ret;
goto errout;
} }
psock->s_flags |= _SF_LISTENING; psock->s_flags |= _SF_LISTENING;
return OK; 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); FAR struct socket *psock = sockfd_socket(sockfd);
int errcode; int errcode;
int ret;
/* Verify that the sockfd corresponds to valid, allocated socket */ /* Verify that the sockfd corresponds to valid, allocated socket */
@ -182,7 +176,14 @@ int listen(int sockfd, int backlog)
* set the errno variable. * 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 */ #endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */

View File

@ -74,10 +74,10 @@
* fromlen The length of the address structure * fromlen The length of the address structure
* *
* Returned Value: * 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, * available to be received and the peer has performed an orderly shutdown,
* recv() will return 0. Otherwise, on errors, -1 is returned, and errno * recv() will return 0. Otherwise, on any failure, a negated errno value
* is set appropriately: * is returned. One of:
* *
* EAGAIN * EAGAIN
* The socket is marked non-blocking and the receive operation would block, * The socket is marked non-blocking and the receive operation would block,
@ -103,8 +103,6 @@
* ENOTSOCK * ENOTSOCK
* The argument sockfd does not refer to a socket. * The argument sockfd does not refer to a socket.
* *
* Assumptions:
*
****************************************************************************/ ****************************************************************************/
ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, 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) FAR socklen_t *fromlen)
{ {
ssize_t ret; ssize_t ret;
int errcode;
/* Treat as a cancellation point */
(void)enter_cancellation_point();
/* Verify that non-NULL pointers were passed */ /* Verify that non-NULL pointers were passed */
#ifdef CONFIG_DEBUG_FEATURES #ifdef CONFIG_DEBUG_FEATURES
if (!buf) if (!buf)
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
#endif #endif
if (from != NULL && fromlen != NULL && *fromlen <= 0) if (from != NULL && fromlen != NULL && *fromlen <= 0)
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
/* Verify that the sockfd corresponds to valid, allocated socket */ /* Verify that the sockfd corresponds to valid, allocated socket */
if (psock == NULL || psock->s_crefs <= 0) if (psock == NULL || psock->s_crefs <= 0)
{ {
errcode = EBADF; return -EBADF;
goto errout;
} }
/* Set the socket state to receiving */ /* 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 */ /* Set the socket state to idle */
psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_IDLE); psock->s_flags = _SS_SETSTATE(psock->s_flags, _SF_IDLE);
if (ret < 0)
{
errcode = -ret;
goto errout;
}
leave_cancellation_point();
return ret; return ret;
errout:
set_errno(errcode);
leave_cancellation_point();
return ERROR;
} }
/**************************************************************************** /****************************************************************************
@ -222,8 +199,6 @@ errout:
* ENOTSOCK * ENOTSOCK
* The argument sockfd does not refer to a socket. * The argument sockfd does not refer to a socket.
* *
* Assumptions:
*
****************************************************************************/ ****************************************************************************/
ssize_t recvfrom(int sockfd, FAR void *buf, size_t len, int flags, 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 */ /* Then let psock_recvfrom() do all of the work */
ret = psock_recvfrom(psock, buf, len, flags, from, fromlen); ret = psock_recvfrom(psock, buf, len, flags, from, fromlen);
if (ret < 0)
{
set_errno(-ret);
ret = ERROR;
}
leave_cancellation_point(); leave_cancellation_point();
return ret; return ret;
} }

View File

@ -1,8 +1,8 @@
/**************************************************************************** /****************************************************************************
* net/socket/setsockopt.c * net/socket/setsockopt.c
* *
* Copyright (C) 2007, 2008, 2011-2012, 2014-2015 Gregory Nutt. All rights * Copyright (C) 2007, 2008, 2011-2012, 2014-2015, 2017 Gregory Nutt. All
* reserved. * rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -80,7 +80,8 @@
* value_len The length of the argument value * value_len The length of the argument value
* *
* Returned 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 * EDOM
* The send and receive timeout values are too big to fit into the * 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, int psock_setsockopt(FAR struct socket *psock, int level, int option,
FAR const void *value, socklen_t value_len) FAR const void *value, socklen_t value_len)
{ {
int errcode;
/* Verify that the socket option if valid (but might not be supported ) */ /* Verify that the socket option if valid (but might not be supported ) */
if (!_SO_SETVALID(option) || !value) if (!_SO_SETVALID(option) || !value)
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
#ifdef CONFIG_NET_USRSOCK #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. */ default: /* Other options are passed to usrsock daemon. */
{ {
ret = usrsock_setsockopt(conn, level, option, value, value_len); return usrsock_setsockopt(conn, level, option, value, value_len);
if (ret < 0)
{
errcode = -ret;
goto errout;
}
return OK;
} }
} }
} }
@ -176,8 +167,7 @@ int psock_setsockopt(FAR struct socket *psock, int level, int option,
if (value_len != sizeof(int)) if (value_len != sizeof(int))
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
/* Get the value. Is the option being set or cleared? */ /* 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)) if (tv == NULL || value_len != sizeof(struct timeval))
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
/* Get the timeout value. Any microsecond remainder will be /* 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)) if (value_len < sizeof(FAR struct linger))
{ {
errcode = EINVAL; return -EINVAL;
goto errout;
} }
/* Get the value. Is the option being set or cleared? */ /* 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 */ case SO_TYPE: /* Reports the socket type */
default: default:
errcode = ENOPROTOOPT; return -ENOPROTOOPT;
goto errout;
} }
return OK; 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) int setsockopt(int sockfd, int level, int option, const void *value, socklen_t value_len)
{ {
FAR struct socket *psock; FAR struct socket *psock;
int ret;
/* Get the underlying socket structure */ /* Get the underlying socket structure */
/* Verify that the sockfd corresponds to valid, allocated socket */ /* 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 */ /* 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 */ #endif /* CONFIG_NET && CONFIG_NET_SOCKOPTS */

View File

@ -67,7 +67,8 @@
* psock A pointer to a user allocated socket structure to be initialized. * psock A pointer to a user allocated socket structure to be initialized.
* *
* Returned Value: * 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 * EACCES
* Permission to create a socket of the specified type and/or protocol * 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 * The protocol type or the specified protocol is not supported within
* this domain. * this domain.
* *
* Assumptions:
*
****************************************************************************/ ****************************************************************************/
int psock_socket(int domain, int type, int protocol, FAR struct socket *psock) int psock_socket(int domain, int type, int protocol, FAR struct socket *psock)
{ {
FAR const struct sock_intf_s *sockif = NULL; FAR const struct sock_intf_s *sockif = NULL;
int errcode;
int ret; int ret;
/* Initialize the socket structure */ /* Initialize the socket structure */
@ -126,8 +124,7 @@ int psock_socket(int domain, int type, int protocol, FAR struct socket *psock)
if (ret < 0) if (ret < 0)
{ {
errcode = -ret; return 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) if (sockif == NULL)
{ {
nerr("ERROR: socket address family unsupported: %d\n", domain); nerr("ERROR: socket address family unsupported: %d\n", domain);
errcode = EAFNOSUPPORT; return -EAFNOSUPPORT;
goto errout;
} }
/* The remaining of the socket initialization depends on the address /* 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) if (ret < 0)
{ {
nerr("ERROR: socket si_setup() failed: %d\n", ret); nerr("ERROR: socket si_setup() failed: %d\n", ret);
errcode = -ret; return ret;
goto errout;
} }
return OK; return OK;
errout:
set_errno(errcode);
return ERROR;
} }
/**************************************************************************** /****************************************************************************
@ -207,6 +198,7 @@ errout:
int socket(int domain, int type, int protocol) int socket(int domain, int type, int protocol)
{ {
FAR struct socket *psock; FAR struct socket *psock;
int errcode;
int sockfd; int sockfd;
int ret; int ret;
@ -216,8 +208,8 @@ int socket(int domain, int type, int protocol)
if (sockfd < 0) if (sockfd < 0)
{ {
nerr("ERROR: Failed to allodate a socket descriptor\n"); nerr("ERROR: Failed to allodate a socket descriptor\n");
set_errno(ENFILE); errcode = ENFILE;
return ERROR; goto errout;
} }
/* Get the underlying socket structure */ /* Get the underlying socket structure */
@ -225,8 +217,8 @@ int socket(int domain, int type, int protocol)
psock = sockfd_socket(sockfd); psock = sockfd_socket(sockfd);
if (!psock) if (!psock)
{ {
set_errno(ENOSYS); /* should not happen */ errcode = ENOSYS; /* should not happen */
goto errout; goto errout_with_sockfd;
} }
/* Initialize the socket structure */ /* Initialize the socket structure */
@ -234,16 +226,18 @@ int socket(int domain, int type, int protocol)
ret = psock_socket(domain, type, protocol, psock); ret = psock_socket(domain, type, protocol, psock);
if (ret < 0) if (ret < 0)
{ {
/* errno already set by psock_socket() */
nerr("ERROR: psock_socket() failed: %d\n", ret); nerr("ERROR: psock_socket() failed: %d\n", ret);
goto errout; errcode = -ret;
goto errout_with_sockfd;
} }
return sockfd; return sockfd;
errout: errout_with_sockfd:
sockfd_release(sockfd); sockfd_release(sockfd);
errout:
set_errno(errcode);
return ERROR; return ERROR;
} }