2017-03-31 16:58:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/usrsock/usrsock_socket.c
|
|
|
|
*
|
2021-11-15 07:53:26 +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
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
2021-11-15 07:53:26 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
2021-11-15 07:53:26 +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.
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_USRSOCK)
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/usrsock.h>
|
|
|
|
|
|
|
|
#include "devif/devif.h"
|
|
|
|
#include "usrsock/usrsock.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-25 15:17:57 +02:00
|
|
|
static uint16_t socket_event(FAR struct net_driver_s *dev,
|
2017-03-31 16:58:14 +02:00
|
|
|
FAR void *pvpriv, uint16_t flags)
|
|
|
|
{
|
|
|
|
FAR struct usrsock_reqstate_s *pstate = pvpriv;
|
2022-08-25 14:15:56 +02:00
|
|
|
FAR struct usrsock_conn_s *conn = pstate->conn;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
if (flags & USRSOCK_EVENT_ABORT)
|
|
|
|
{
|
|
|
|
ninfo("socket aborted.\n");
|
|
|
|
|
|
|
|
pstate->result = -ENETDOWN;
|
|
|
|
|
|
|
|
/* Stop further callbacks */
|
|
|
|
|
2022-09-08 09:28:46 +02:00
|
|
|
pstate->cb->flags = 0;
|
|
|
|
pstate->cb->priv = NULL;
|
|
|
|
pstate->cb->event = NULL;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
/* Wake up the waiting thread */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&pstate->recvsem);
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
else if (flags & USRSOCK_EVENT_REQ_COMPLETE)
|
|
|
|
{
|
|
|
|
ninfo("request completed.\n");
|
|
|
|
|
|
|
|
pstate->result = conn->resp.result;
|
|
|
|
if (pstate->result >= 0)
|
|
|
|
{
|
|
|
|
/* We might start getting events for this socket right after
|
2019-03-11 19:48:17 +01:00
|
|
|
* returning to daemon, so setup 'conn' already here.
|
|
|
|
*/
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
conn->state = USRSOCK_CONN_STATE_READY;
|
|
|
|
conn->usockid = pstate->result;
|
2022-10-08 05:24:10 +02:00
|
|
|
if (flags & USRSOCK_EVENT_SENDTO_READY)
|
|
|
|
{
|
|
|
|
conn->flags |= USRSOCK_EVENT_SENDTO_READY;
|
|
|
|
}
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Stop further callbacks */
|
|
|
|
|
2022-09-08 09:28:46 +02:00
|
|
|
pstate->cb->flags = 0;
|
|
|
|
pstate->cb->priv = NULL;
|
|
|
|
pstate->cb->event = NULL;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
/* Wake up the waiting thread */
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&pstate->recvsem);
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: do_socket_request
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int do_socket_request(FAR struct usrsock_conn_s *conn, int domain,
|
|
|
|
int type, int protocol)
|
|
|
|
{
|
2019-03-11 19:48:17 +01:00
|
|
|
struct usrsock_request_socket_s req =
|
|
|
|
{
|
|
|
|
};
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
struct iovec bufs[1];
|
|
|
|
|
|
|
|
/* Prepare request for daemon to read. */
|
|
|
|
|
|
|
|
req.head.reqid = USRSOCK_REQUEST_SOCKET;
|
|
|
|
req.domain = domain;
|
|
|
|
req.type = type;
|
|
|
|
req.protocol = protocol;
|
|
|
|
|
|
|
|
if (req.domain != domain)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.type != type)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.protocol != protocol)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufs[0].iov_base = (FAR void *)&req;
|
|
|
|
bufs[0].iov_len = sizeof(req);
|
|
|
|
|
2023-02-02 13:31:22 +01:00
|
|
|
return usrsock_do_request(conn, bufs, nitems(bufs));
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: usrsock_socket
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* socket() creates an endpoint for communication and returns a socket
|
|
|
|
* structure.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2019-03-11 19:48:17 +01:00
|
|
|
* domain - (see sys/socket.h)
|
|
|
|
* type - (see sys/socket.h)
|
|
|
|
* protocol - (see sys/socket.h)
|
|
|
|
* psock - A pointer to a user allocated socket structure to be
|
|
|
|
* initialized.
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 on success; negative error-code on error
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-03-11 19:48:17 +01:00
|
|
|
int usrsock_socket(int domain, int type, int protocol,
|
|
|
|
FAR struct socket *psock)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
2019-03-11 19:48:17 +01:00
|
|
|
struct usrsock_reqstate_s state =
|
|
|
|
{
|
|
|
|
};
|
2019-10-25 19:31:42 +02:00
|
|
|
|
2017-03-31 16:58:14 +02:00
|
|
|
FAR struct usrsock_conn_s *conn;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Allocate the usrsock socket connection structure and save in the new
|
|
|
|
* socket instance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
conn = usrsock_alloc();
|
|
|
|
if (!conn)
|
|
|
|
{
|
|
|
|
/* Failed to reserve a connection structure */
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
|
|
|
|
/* Set up event callback for usrsock. */
|
|
|
|
|
|
|
|
err = usrsock_setup_request_callback(conn, &state, socket_event,
|
|
|
|
USRSOCK_EVENT_ABORT |
|
|
|
|
USRSOCK_EVENT_REQ_COMPLETE);
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
goto errout_free_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Request user-space daemon for new socket. */
|
|
|
|
|
|
|
|
err = do_socket_request(conn, domain, type, protocol);
|
|
|
|
if (err < 0)
|
|
|
|
{
|
|
|
|
goto errout_teardown_callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for completion of request. */
|
|
|
|
|
2023-01-13 07:51:38 +01:00
|
|
|
net_sem_wait_uninterruptible(&state.recvsem);
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
if (state.result < 0)
|
|
|
|
{
|
|
|
|
err = state.result;
|
|
|
|
goto errout_teardown_callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
psock->s_conn = conn;
|
|
|
|
conn->crefs = 1;
|
|
|
|
|
|
|
|
usrsock_teardown_request_callback(&state);
|
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_teardown_callback:
|
|
|
|
usrsock_teardown_request_callback(&state);
|
|
|
|
errout_free_conn:
|
|
|
|
usrsock_free(conn);
|
|
|
|
net_unlock();
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_USRSOCK */
|