2007-08-29 07:38:15 +08:00
|
|
|
/****************************************************************************
|
2014-06-29 07:25:18 +08:00
|
|
|
* net/socket/bind.c
|
2007-08-29 07:38:15 +08:00
|
|
|
*
|
2024-09-11 20:39:39 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 19:45:37 +08: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-29 07:38:15 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-08-29 07:38:15 +08:00
|
|
|
*
|
2021-02-19 19:45:37 +08: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-29 07:38:15 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-09-02 04:56:19 +08:00
|
|
|
|
2007-08-29 07:38:15 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2017-07-13 23:27:56 +08:00
|
|
|
#include <assert.h>
|
2007-08-29 07:38:15 +08:00
|
|
|
#include <errno.h>
|
2015-01-19 00:33:27 +08:00
|
|
|
#include <debug.h>
|
2007-08-29 07:38:15 +08:00
|
|
|
|
2023-12-25 15:04:01 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2014-07-07 07:58:36 +08:00
|
|
|
#include <nuttx/net/net.h>
|
2014-06-26 23:32:39 +08:00
|
|
|
|
2014-06-29 07:25:18 +08:00
|
|
|
#include "socket/socket.h"
|
2007-09-02 04:56:19 +08:00
|
|
|
|
2017-07-13 23:27:56 +08:00
|
|
|
#ifdef CONFIG_NET
|
2014-06-13 01:52:06 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
2017-07-13 23:27:56 +08:00
|
|
|
* Private Functions
|
2014-06-13 01:52:06 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
2007-08-29 07:38:15 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: psock_bind
|
2007-08-29 07:38:15 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2012-03-03 03:57:52 +08:00
|
|
|
* bind() gives the socket 'psock' the local address 'addr'. 'addr' is
|
2015-01-25 22:33:39 +08: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-02 04:56:19 +08:00
|
|
|
* space (address family) but has no name assigned.
|
2007-08-29 07:38:15 +08:00
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2012-03-03 03:57:52 +08:00
|
|
|
* psock Socket structure of the socket to bind
|
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:
|
2017-09-30 22:18:08 +08:00
|
|
|
* Returns zero (OK) on success. On failure, it returns a negated errno
|
|
|
|
* value to indicate the nature of the error.
|
2007-08-29 07:38:15 +08:00
|
|
|
*
|
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.
|
|
|
|
* EINVAL
|
|
|
|
* The socket is already bound to an address.
|
|
|
|
* ENOTSOCK
|
2012-03-03 03:57:52 +08:00
|
|
|
* psock is a descriptor for a file, not a socket.
|
2007-09-02 04:56:19 +08:00
|
|
|
*
|
2007-08-29 07:38:15 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-03-03 03:57:52 +08:00
|
|
|
int psock_bind(FAR struct socket *psock, const struct sockaddr *addr,
|
2014-07-07 07:58:36 +08:00
|
|
|
socklen_t addrlen)
|
2007-08-29 07:38:15 +08:00
|
|
|
{
|
2014-02-11 08:08:49 +08:00
|
|
|
int ret = OK;
|
2007-09-02 04:56:19 +08:00
|
|
|
|
2012-03-03 03:57:52 +08:00
|
|
|
/* Verify that the psock corresponds to valid, allocated socket */
|
2007-09-02 04:56:19 +08:00
|
|
|
|
2021-02-23 18:04:13 +08:00
|
|
|
if (!psock || psock->s_conn == NULL)
|
2007-09-02 04:56:19 +08:00
|
|
|
{
|
2023-08-29 15:29:17 +08:00
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that an address was provided */
|
|
|
|
|
|
|
|
if (addr == NULL)
|
|
|
|
{
|
|
|
|
return -EFAULT;
|
2007-09-02 04:56:19 +08:00
|
|
|
}
|
|
|
|
|
2017-07-13 23:27:56 +08:00
|
|
|
/* Let the address family's connect() method handle the operation */
|
2015-01-25 22:33:39 +08:00
|
|
|
|
2023-03-06 12:06:06 +08:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL);
|
|
|
|
if (psock->s_sockif->si_bind == NULL)
|
|
|
|
{
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
}
|
|
|
|
|
2017-07-13 23:27:56 +08:00
|
|
|
ret = psock->s_sockif->si_bind(psock, addr, addrlen);
|
2007-09-02 04:56:19 +08:00
|
|
|
|
2007-09-04 04:34:44 +08:00
|
|
|
/* Was the bind successful */
|
|
|
|
|
2022-02-08 13:18:01 +08:00
|
|
|
if (ret >= 0)
|
2007-09-04 04:34:44 +08:00
|
|
|
{
|
2022-02-08 13:18:01 +08:00
|
|
|
FAR struct socket_conn_s *conn = psock->s_conn;
|
2007-09-04 04:34:44 +08:00
|
|
|
|
2022-02-08 13:18:01 +08:00
|
|
|
/* Mark the socket bound */
|
2020-01-31 17:34:28 +08:00
|
|
|
|
2022-02-08 13:18:01 +08:00
|
|
|
conn->s_flags |= _SF_BOUND;
|
|
|
|
}
|
2020-01-31 17:34:28 +08:00
|
|
|
|
2022-02-08 13:18:01 +08:00
|
|
|
return ret;
|
2007-08-29 07:38:15 +08:00
|
|
|
}
|
|
|
|
|
2012-03-03 03:57:52 +08:00
|
|
|
/****************************************************************************
|
2017-04-22 06:33:14 +08:00
|
|
|
* Name: bind
|
2012-03-03 03:57:52 +08: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 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2012-03-03 03:57:52 +08: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 22:18:08 +08:00
|
|
|
FAR struct socket *psock;
|
2023-12-25 15:04:01 +08:00
|
|
|
FAR struct file *filep;
|
2017-09-30 22:18:08 +08:00
|
|
|
int ret;
|
|
|
|
|
2022-10-27 18:17:35 +08:00
|
|
|
/* Get the underlying socket structure */
|
2012-03-03 03:57:52 +08:00
|
|
|
|
2023-12-25 15:04:01 +08:00
|
|
|
ret = sockfd_socket(sockfd, &filep, &psock);
|
2012-03-03 03:57:52 +08:00
|
|
|
|
|
|
|
/* Then let psock_bind do all of the work */
|
|
|
|
|
2022-10-27 18:17:35 +08:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
ret = psock_bind(psock, addr, addrlen);
|
2023-12-25 15:04:01 +08:00
|
|
|
fs_putfilep(filep);
|
2022-10-27 18:17:35 +08:00
|
|
|
}
|
|
|
|
|
2017-09-30 22:18:08 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-02-14 13:32:41 +08:00
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
2017-09-30 22:18:08 +08:00
|
|
|
}
|
|
|
|
|
2023-02-14 13:32:41 +08:00
|
|
|
return ret;
|
2012-03-03 03:57:52 +08:00
|
|
|
}
|
|
|
|
|
2007-09-02 04:56:19 +08:00
|
|
|
#endif /* CONFIG_NET */
|