2007-08-28 23:38:15 +00:00
|
|
|
/****************************************************************************
|
2014-06-28 17:25:18 -06:00
|
|
|
* net/socket/bind.c
|
2007-08-28 23:38:15 +00:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-08-28 23:38:15 +00:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-08-28 23:38:15 +00:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-08-28 23:38:15 +00:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-09-01 20:56:19 +00:00
|
|
|
|
2007-08-28 23:38:15 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2017-07-13 09:27:56 -06:00
|
|
|
#include <assert.h>
|
2007-08-28 23:38:15 +00:00
|
|
|
#include <errno.h>
|
2015-01-18 10:33:27 -06:00
|
|
|
#include <debug.h>
|
2007-08-28 23:38:15 +00:00
|
|
|
|
2014-07-06 17:58:36 -06:00
|
|
|
#include <nuttx/net/net.h>
|
2014-06-26 09:32:39 -06:00
|
|
|
|
2014-06-28 17:25:18 -06:00
|
|
|
#include "socket/socket.h"
|
2007-09-01 20:56:19 +00:00
|
|
|
|
2017-07-13 09:27:56 -06:00
|
|
|
#ifdef CONFIG_NET
|
2014-06-12 11:52:06 -06:00
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-13 09:27:56 -06:00
|
|
|
* Private Functions
|
2014-06-12 11:52:06 -06:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2007-08-28 23:38:15 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-21 16:33:14 -06:00
|
|
|
* Name: psock_bind
|
2007-08-28 23:38:15 +00:00
|
|
|
*
|
|
|
|
* Description:
|
2012-03-02 19:57:52 +00:00
|
|
|
* bind() gives the socket 'psock' the local address 'addr'. 'addr' is
|
2015-01-25 08:33:39 -06:00
|
|
|
* '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-01 20:56:19 +00:00
|
|
|
* space (address family) but has no name assigned.
|
2007-08-28 23:38:15 +00:00
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2012-03-02 19:57:52 +00:00
|
|
|
* psock Socket structure of the socket to bind
|
2007-09-03 20:34:44 +00:00
|
|
|
* addr Socket local address
|
2007-09-08 15:26:55 +00:00
|
|
|
* addrlen Length of 'addr'
|
2007-08-28 23:38:15 +00:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-09-30 08:18:08 -06:00
|
|
|
* Returns zero (OK) on success. On failure, it returns a negated errno
|
|
|
|
* value to indicate the nature of the error.
|
2007-08-28 23:38:15 +00:00
|
|
|
*
|
2007-09-01 20:56:19 +00:00
|
|
|
* EACCES
|
|
|
|
* The address is protected, and the user is not the superuser.
|
|
|
|
* EADDRINUSE
|
|
|
|
* The given address is already in use.
|
|
|
|
* EINVAL
|
|
|
|
* The socket is already bound to an address.
|
|
|
|
* ENOTSOCK
|
2012-03-02 19:57:52 +00:00
|
|
|
* psock is a descriptor for a file, not a socket.
|
2007-09-01 20:56:19 +00:00
|
|
|
*
|
2007-08-28 23:38:15 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-03-02 19:57:52 +00:00
|
|
|
int psock_bind(FAR struct socket *psock, const struct sockaddr *addr,
|
2014-07-06 17:58:36 -06:00
|
|
|
socklen_t addrlen)
|
2007-08-28 23:38:15 +00:00
|
|
|
{
|
2014-02-10 18:08:49 -06:00
|
|
|
int ret = OK;
|
2007-09-01 20:56:19 +00:00
|
|
|
|
2012-03-02 19:57:52 +00:00
|
|
|
/* Verify that the psock corresponds to valid, allocated socket */
|
2007-09-01 20:56:19 +00:00
|
|
|
|
2021-02-23 18:04:13 +08:00
|
|
|
if (!psock || psock->s_conn == NULL)
|
2007-09-01 20:56:19 +00:00
|
|
|
{
|
2017-09-30 08:18:08 -06:00
|
|
|
return -ENOTSOCK;
|
2007-09-01 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 09:27:56 -06:00
|
|
|
/* Let the address family's connect() method handle the operation */
|
2015-01-25 08:33:39 -06:00
|
|
|
|
2017-07-13 09:27:56 -06:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_bind != NULL);
|
|
|
|
ret = psock->s_sockif->si_bind(psock, addr, addrlen);
|
2007-09-01 20:56:19 +00:00
|
|
|
|
2007-09-03 20:34:44 +00:00
|
|
|
/* Was the bind successful */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-09-30 08:18:08 -06:00
|
|
|
return ret;
|
2007-09-03 20:34:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 17:34:28 +08:00
|
|
|
/* Mark the socket bound */
|
|
|
|
|
|
|
|
psock->s_flags |= _SF_BOUND;
|
|
|
|
|
2007-09-03 20:34:44 +00:00
|
|
|
return OK;
|
2007-08-28 23:38:15 +00:00
|
|
|
}
|
|
|
|
|
2012-03-02 19:57:52 +00:00
|
|
|
/****************************************************************************
|
2017-04-21 16:33:14 -06:00
|
|
|
* Name: bind
|
2012-03-02 19:57:52 +00:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* 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
|
|
|
|
* space (address family) but has no name assigned.
|
|
|
|
*
|
2018-03-13 09:52:27 -06:00
|
|
|
* Input Parameters:
|
2012-03-02 19:57:52 +00:00
|
|
|
* sockfd Socket descriptor of the socket to bind
|
|
|
|
* addr Socket local address
|
|
|
|
* addrlen Length of 'addr'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; -1 on error with errno set appropriately
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
|
|
|
|
{
|
2017-09-30 08:18:08 -06:00
|
|
|
FAR struct socket *psock;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Use the socket descriptor to get the underlying socket structure */
|
2012-03-02 19:57:52 +00:00
|
|
|
|
2017-09-30 08:18:08 -06:00
|
|
|
psock = sockfd_socket(sockfd);
|
2012-03-02 19:57:52 +00:00
|
|
|
|
|
|
|
/* Then let psock_bind do all of the work */
|
|
|
|
|
2017-09-30 08:18:08 -06:00
|
|
|
ret = psock_bind(psock, addr, addrlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-12-24 08:09:55 -06:00
|
|
|
_SO_SETERRNO(psock, -ret);
|
2017-09-30 08:18:08 -06:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2012-03-02 19:57:52 +00:00
|
|
|
}
|
|
|
|
|
2007-09-01 20:56:19 +00:00
|
|
|
#endif /* CONFIG_NET */
|