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