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-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
|
|
|
|
2022-10-19 15:53:46 +02:00
|
|
|
if (type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK | SOCK_TYPE_MASK))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-11-23 14:44:11 +01:00
|
|
|
/* Get the socket interface */
|
2017-07-31 17:33:59 +02:00
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
sockif = net_sockif(domain, psock->s_type, psock->s_proto);
|
2022-11-23 14:44:11 +01:00
|
|
|
if (sockif == NULL)
|
2007-11-22 19:36:46 +01:00
|
|
|
{
|
2022-11-23 14:44:11 +01:00
|
|
|
nerr("ERROR: socket address family unsupported: %d\n", domain);
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
}
|
2012-03-02 20:57:52 +01:00
|
|
|
|
2022-11-23 14:44:11 +01:00
|
|
|
/* The remaining of the socket initialization depends on the address
|
|
|
|
* family.
|
|
|
|
*/
|
2012-03-02 20:57:52 +01:00
|
|
|
|
2022-11-23 14:44:11 +01:00
|
|
|
DEBUGASSERT(sockif->si_setup != NULL);
|
|
|
|
psock->s_sockif = sockif;
|
2022-02-08 06:18:01 +01:00
|
|
|
|
2023-02-06 18:55:44 +01:00
|
|
|
ret = sockif->si_setup(psock);
|
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 */
|