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
|
|
|
*
|
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
|
|
|
|
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
|
|
|
{
|
2017-09-30 16:18:08 +02:00
|
|
|
return -ENOTSOCK;
|
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
|
|
|
|
2017-07-13 17:27:56 +02:00
|
|
|
DEBUGASSERT(psock->s_sockif != NULL && psock->s_sockif->si_bind != NULL);
|
|
|
|
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;
|
|
|
|
int ret;
|
|
|
|
|
2022-10-27 12:17:35 +02:00
|
|
|
/* Get the underlying socket structure */
|
2012-03-02 20:57:52 +01:00
|
|
|
|
2022-10-27 12:17:35 +02:00
|
|
|
ret = sockfd_socket(sockfd, &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);
|
|
|
|
}
|
|
|
|
|
2017-09-30 16:18:08 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-12-24 15:09:55 +01:00
|
|
|
_SO_SETERRNO(psock, -ret);
|
2017-09-30 16:18:08 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
2012-03-02 20:57:52 +01:00
|
|
|
}
|
|
|
|
|
2007-09-01 22:56:19 +02:00
|
|
|
#endif /* CONFIG_NET */
|