2007-08-29 01:38:15 +02:00
|
|
|
/****************************************************************************
|
2014-06-29 01:25:18 +02:00
|
|
|
* net/socket/socket.c
|
2007-08-29 01:38:15 +02:00
|
|
|
*
|
2020-04-28 18:17:03 +02: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
|
|
|
*
|
2020-04-28 18:17:03 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-08-29 01:38:15 +02:00
|
|
|
*
|
2020-04-28 18:17:03 +02: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/socket.h>
|
|
|
|
#include <errno.h>
|
2009-06-15 20:58:22 +02:00
|
|
|
#include <assert.h>
|
2007-11-22 19:36:46 +01:00
|
|
|
#include <debug.h>
|
2007-08-29 01:38:15 +02:00
|
|
|
|
2017-07-31 17:33:59 +02:00
|
|
|
#include "usrsock/usrsock.h"
|
2017-07-12 23:07:32 +02:00
|
|
|
#include "socket/socket.h"
|
2007-09-01 22:56:19 +02:00
|
|
|
|
2017-07-12 23:07:32 +02:00
|
|
|
#ifdef CONFIG_NET
|
2015-01-25 14:36:16 +01:00
|
|
|
|
2007-08-29 01:38:15 +02:00
|
|
|
/****************************************************************************
|
2015-01-19 23:02:56 +01:00
|
|
|
* Public Functions
|
2007-08-29 01:38:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: psock_socket
|
2007-08-29 01:38:15 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-03-02 20:57:52 +01:00
|
|
|
* socket() creates an endpoint for communication and returns a socket
|
|
|
|
* structure.
|
2007-08-29 01:38:15 +02:00
|
|
|
*
|
2017-10-23 16:45:12 +02:00
|
|
|
* Input Parameters:
|
2007-08-29 01:38:15 +02:00
|
|
|
* domain (see sys/socket.h)
|
|
|
|
* type (see sys/socket.h)
|
|
|
|
* protocol (see sys/socket.h)
|
2020-04-28 18:17:03 +02:00
|
|
|
* psock A pointer to a user allocated socket structure to be
|
|
|
|
* initialized.
|
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
|
|
|
|
* Permission to create a socket of the specified type and/or protocol
|
|
|
|
* is denied.
|
|
|
|
* EAFNOSUPPORT
|
|
|
|
* The implementation does not support the specified address family.
|
|
|
|
* EINVAL
|
|
|
|
* Unknown protocol, or protocol family not available.
|
|
|
|
* EMFILE
|
|
|
|
* Process file table overflow.
|
|
|
|
* ENFILE
|
|
|
|
* The system limit on the total number of open files has been reached.
|
|
|
|
* ENOBUFS or ENOMEM
|
|
|
|
* Insufficient memory is available. The socket cannot be created until
|
|
|
|
* sufficient resources are freed.
|
|
|
|
* EPROTONOSUPPORT
|
|
|
|
* The protocol type or the specified protocol is not supported within
|
|
|
|
* this domain.
|
|
|
|
*
|
2007-08-29 01:38:15 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-28 18:17:03 +02:00
|
|
|
int psock_socket(int domain, int type, int protocol,
|
|
|
|
FAR struct socket *psock)
|
2007-08-29 01:38:15 +02:00
|
|
|
{
|
2017-07-12 23:07:32 +02:00
|
|
|
FAR const struct sock_intf_s *sockif = NULL;
|
|
|
|
int ret;
|
2017-07-31 17:33:59 +02:00
|
|
|
|
2017-07-12 23:07:32 +02:00
|
|
|
/* Initialize the socket structure */
|
2007-09-01 22:56:19 +02:00
|
|
|
|
2015-01-16 15:51:18 +01:00
|
|
|
psock->s_domain = domain;
|
2020-08-28 23:51:26 +02:00
|
|
|
psock->s_proto = protocol;
|
2015-01-16 15:51:18 +01:00
|
|
|
psock->s_conn = NULL;
|
2022-02-08 06:18:01 +01:00
|
|
|
psock->s_type = type & SOCK_TYPE_MASK;
|
2020-08-13 09:30:54 +02:00
|
|
|
|
2017-07-31 17:33:59 +02:00
|
|
|
#ifdef CONFIG_NET_USRSOCK
|
2020-11-05 13:30:49 +01:00
|
|
|
if (domain != PF_LOCAL && domain != PF_UNSPEC && domain != PF_RPMSG)
|
2017-07-31 17:33:59 +02:00
|
|
|
{
|
|
|
|
/* Handle special setup for USRSOCK sockets (user-space networking
|
|
|
|
* stack).
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = g_usrsock_sockif.si_setup(psock, protocol);
|
2020-02-22 11:23:55 +01:00
|
|
|
psock->s_sockif = &g_usrsock_sockif;
|
2017-07-31 17:33:59 +02:00
|
|
|
}
|
2021-02-23 11:04:13 +01:00
|
|
|
else
|
2017-07-31 17:33:59 +02:00
|
|
|
#endif /* CONFIG_NET_USRSOCK */
|
2007-11-22 19:36:46 +01:00
|
|
|
{
|
2021-02-23 11:04:13 +01:00
|
|
|
/* Get the socket interface */
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
sockif = net_sockif(domain, psock->s_type, protocol);
|
2021-02-23 11:04:13 +01:00
|
|
|
if (sockif == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: socket address family unsupported: %d\n", domain);
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
}
|
2009-09-16 22:27:00 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
/* The remaining of the socket initialization depends on the address
|
|
|
|
* family.
|
|
|
|
*/
|
2012-03-02 20:57:52 +01:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
DEBUGASSERT(sockif->si_setup != NULL);
|
|
|
|
psock->s_sockif = sockif;
|
2012-03-02 20:57:52 +01:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
ret = sockif->si_setup(psock, protocol);
|
2022-02-08 06:18:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
FAR struct socket_conn_s *conn = psock->s_conn;
|
|
|
|
|
|
|
|
if (type & SOCK_NONBLOCK)
|
2021-02-23 11:04:13 +01:00
|
|
|
{
|
2022-02-08 06:18:01 +01:00
|
|
|
conn->s_flags |= _SF_NONBLOCK;
|
2021-02-23 11:04:13 +01:00
|
|
|
}
|
2012-03-02 20:57:52 +01:00
|
|
|
|
2022-02-08 06:18:01 +01:00
|
|
|
/* The socket has been successfully initialized */
|
|
|
|
|
|
|
|
conn->s_flags |= _SF_INITD;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nerr("ERROR: socket si_setup() failed: %d\n", ret);
|
|
|
|
}
|
2020-04-28 18:17:03 +02:00
|
|
|
|
2021-02-23 11:04:13 +01:00
|
|
|
return ret;
|
2012-03-02 20:57:52 +01:00
|
|
|
}
|
|
|
|
|
2007-09-01 22:56:19 +02:00
|
|
|
#endif /* CONFIG_NET */
|