2007-08-29 07:38:15 +08:00
|
|
|
/****************************************************************************
|
2007-09-03 05:58:35 +08:00
|
|
|
* net/bind.c
|
2007-08-29 07:38:15 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-09-02 04:56:19 +08:00
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
2007-08-29 07:38:15 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2007-09-09 03:57:25 +08:00
|
|
|
#include "net-internal.h"
|
2007-09-02 04:56:19 +08:00
|
|
|
|
2007-08-29 07:38:15 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Global Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Function: bind
|
|
|
|
*
|
|
|
|
* Description:
|
2007-09-08 23:26:55 +08:00
|
|
|
* bind() gives the socket 'sockfd' the local address 'addr'. 'addr' is
|
|
|
|
* 'addrlen' bytes long. Traditionally, this is called “assigning a name to
|
|
|
|
* a socket.” When a socket is created with socket, it exists in a name
|
2007-09-02 04:56:19 +08:00
|
|
|
* space (address family) but has no name assigned.
|
2007-08-29 07:38:15 +08:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* sockfd Socket descriptor from socket
|
2007-09-04 04:34:44 +08:00
|
|
|
* addr Socket local address
|
2007-09-08 23:26:55 +08:00
|
|
|
* addrlen Length of 'addr'
|
2007-08-29 07:38:15 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; -1 on error with errno set appropriately
|
|
|
|
*
|
2007-09-02 04:56:19 +08:00
|
|
|
* EACCES
|
|
|
|
* The address is protected, and the user is not the superuser.
|
|
|
|
* EADDRINUSE
|
|
|
|
* The given address is already in use.
|
|
|
|
* EBADF
|
|
|
|
* sockfd is not a valid descriptor.
|
|
|
|
* EINVAL
|
|
|
|
* The socket is already bound to an address.
|
|
|
|
* ENOTSOCK
|
|
|
|
* sockfd is a descriptor for a file, not a socket.
|
|
|
|
*
|
2007-08-29 07:38:15 +08:00
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-09-03 05:58:35 +08:00
|
|
|
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
|
2007-08-29 07:38:15 +08:00
|
|
|
{
|
2007-09-02 04:56:19 +08:00
|
|
|
FAR struct socket *psock = sockfd_socket(sockfd);
|
2007-11-23 02:36:46 +08:00
|
|
|
|
|
|
|
#if defined(CONFIG_NET_TCP) || defined(CONFIG_NET_UDP)
|
2007-09-03 05:58:35 +08:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
FAR const struct sockaddr_in6 *inaddr = (const struct sockaddr_in6 *)addr;
|
|
|
|
#else
|
|
|
|
FAR const struct sockaddr_in *inaddr = (const struct sockaddr_in *)addr;
|
|
|
|
#endif
|
2007-11-23 02:36:46 +08:00
|
|
|
#endif
|
|
|
|
|
2007-09-02 04:56:19 +08:00
|
|
|
int err;
|
2007-09-04 04:34:44 +08:00
|
|
|
int ret;
|
2007-09-02 04:56:19 +08:00
|
|
|
|
|
|
|
/* Verify that the sockfd corresponds to valid, allocated socket */
|
|
|
|
|
|
|
|
if (!psock || psock->s_crefs <= 0)
|
|
|
|
{
|
|
|
|
err = EBADF;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2007-09-03 05:58:35 +08:00
|
|
|
/* Verify that a valid address has been provided */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
|
|
|
if (addr->sa_family != AF_INET6 || addrlen < sizeof(struct sockaddr_in6))
|
|
|
|
#else
|
|
|
|
if (addr->sa_family != AF_INET || addrlen < sizeof(struct sockaddr_in))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
err = EBADF;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2007-09-02 04:56:19 +08:00
|
|
|
/* Perform the binding depending on the protocol type */
|
2007-09-04 04:34:44 +08:00
|
|
|
|
2007-09-02 04:56:19 +08:00
|
|
|
switch (psock->s_type)
|
|
|
|
{
|
2007-11-23 02:36:46 +08:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2007-09-02 04:56:19 +08:00
|
|
|
case SOCK_STREAM:
|
2007-09-04 04:34:44 +08:00
|
|
|
ret = uip_tcpbind(psock->s_conn, inaddr);
|
2007-09-10 01:20:56 +08:00
|
|
|
psock->s_flags |= _SF_BOUND;
|
2007-09-02 04:56:19 +08:00
|
|
|
break;
|
2007-11-23 02:36:46 +08:00
|
|
|
#endif
|
2007-09-02 04:56:19 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_NET_UDP
|
|
|
|
case SOCK_DGRAM:
|
2007-09-04 04:34:44 +08:00
|
|
|
ret = uip_udpbind(psock->s_conn, inaddr);
|
2007-09-02 04:56:19 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2007-11-23 02:36:46 +08:00
|
|
|
|
2007-09-02 04:56:19 +08:00
|
|
|
default:
|
2007-11-17 02:48:39 +08:00
|
|
|
err = EBADF;
|
2007-09-02 04:56:19 +08:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2007-09-04 04:34:44 +08:00
|
|
|
/* Was the bind successful */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
err = -ret;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2007-09-02 04:56:19 +08:00
|
|
|
|
|
|
|
errout:
|
|
|
|
*get_errno_ptr() = err;
|
2007-08-29 07:38:15 +08:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2007-09-02 04:56:19 +08:00
|
|
|
#endif /* CONFIG_NET */
|