123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /****************************************************************************
- * net/icmpv6/icmpv6_conn.c
- *
- * 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * 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.
- *
- ****************************************************************************/
- /****************************************************************************
- * Included Files
- ****************************************************************************/
- #include <nuttx/config.h>
- #include <string.h>
- #include <assert.h>
- #include <errno.h>
- #include <debug.h>
- #include <arch/irq.h>
- #include <nuttx/semaphore.h>
- #include <nuttx/net/netconfig.h>
- #include <nuttx/net/net.h>
- #include <nuttx/net/netdev.h>
- #include "devif/devif.h"
- #include "icmpv6/icmpv6.h"
- #ifdef CONFIG_NET_ICMPv6_SOCKET
- /****************************************************************************
- * Private Data
- ****************************************************************************/
- /* The array containing all IPPROTO_ICMP socket connections */
- static struct icmpv6_conn_s g_icmpv6_connections[CONFIG_NET_ICMPv6_NCONNS];
- /* A list of all free IPPROTO_ICMP socket connections */
- static dq_queue_t g_free_icmpv6_connections;
- static sem_t g_free_sem;
- /* A list of all allocated IPPROTO_ICMP socket connections */
- static dq_queue_t g_active_icmpv6_connections;
- /****************************************************************************
- * Public Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: icmpv6_sock_initialize
- *
- * Description:
- * Initialize the IPPROTO_ICMP socket connection structures. Called once
- * and only from the network initialization layer.
- *
- ****************************************************************************/
- void icmpv6_sock_initialize(void)
- {
- int i;
- /* Initialize the queues */
- dq_init(&g_free_icmpv6_connections);
- dq_init(&g_active_icmpv6_connections);
- nxsem_init(&g_free_sem, 0, 1);
- for (i = 0; i < CONFIG_NET_ICMPv6_NCONNS; i++)
- {
- /* Move the connection structure to the free list */
- dq_addlast(&g_icmpv6_connections[i].node, &g_free_icmpv6_connections);
- }
- }
- /****************************************************************************
- * Name: icmpv6_alloc
- *
- * Description:
- * Allocate a new, uninitialized IPPROTO_ICMP socket connection structure.
- * This is normally something done by the implementation of the socket()
- * interface.
- *
- ****************************************************************************/
- FAR struct icmpv6_conn_s *icmpv6_alloc(void)
- {
- FAR struct icmpv6_conn_s *conn = NULL;
- int ret;
- /* The free list is protected by a semaphore (that behaves like a mutex). */
- ret = net_lockedwait(&g_free_sem);
- if (ret >= 0)
- {
- conn = (FAR struct icmpv6_conn_s *)
- dq_remfirst(&g_free_icmpv6_connections);
- if (conn != NULL)
- {
- /* Clear the connection structure */
- memset(conn, 0, sizeof(struct icmpv6_conn_s));
- /* Enqueue the connection into the active list */
- dq_addlast(&conn->node, &g_active_icmpv6_connections);
- }
- nxsem_post(&g_free_sem);
- }
- return conn;
- }
- /****************************************************************************
- * Name: icmpv6_free
- *
- * Description:
- * Free a IPPROTO_ICMP socket connection structure that is no longer in
- * use. This should be done by the implementation of close().
- *
- ****************************************************************************/
- void icmpv6_free(FAR struct icmpv6_conn_s *conn)
- {
- /* The free list is protected by a semaphore (that behaves like a mutex). */
- DEBUGASSERT(conn->crefs == 0);
- /* Take the semaphore (perhaps waiting) */
- net_lockedwait_uninterruptible(&g_free_sem);
- /* Remove the connection from the active list */
- dq_rem(&conn->node, &g_active_icmpv6_connections);
- /* Free the connection */
- dq_addlast(&conn->node, &g_free_icmpv6_connections);
- nxsem_post(&g_free_sem);
- }
- /****************************************************************************
- * Name: icmpv6_active()
- *
- * Description:
- * Find a connection structure that is the appropriate connection to be
- * used with the provided ECHO request ID.
- *
- * Assumptions:
- * This function is called from network logic at with the network locked.
- *
- ****************************************************************************/
- FAR struct icmpv6_conn_s *icmpv6_active(uint16_t id)
- {
- FAR struct icmpv6_conn_s *conn =
- (FAR struct icmpv6_conn_s *)g_active_icmpv6_connections.head;
- while (conn != NULL)
- {
- /* FIXME lmac in conn should have been set by icmpv6_bind() */
- if (id == conn->id)
- {
- /* Matching connection found.. return a reference to it */
- break;
- }
- /* Look at the next active connection */
- conn = (FAR struct icmpv6_conn_s *)conn->node.flink;
- }
- return conn;
- }
- /****************************************************************************
- * Name: icmpv6_nextconn
- *
- * Description:
- * Traverse the list of allocated packet connections
- *
- * Assumptions:
- * This function is called from network logic at with the network locked.
- *
- ****************************************************************************/
- FAR struct icmpv6_conn_s *icmpv6_nextconn(FAR struct icmpv6_conn_s *conn)
- {
- if (conn == NULL)
- {
- return (FAR struct icmpv6_conn_s *)g_active_icmpv6_connections.head;
- }
- else
- {
- return (FAR struct icmpv6_conn_s *)conn->node.flink;
- }
- }
- /****************************************************************************
- * Name: icmpv6_findconn
- *
- * Description:
- * Find an ICMPv6 connection structure that is expecting a ICMPv6 ECHO
- * response with this ID from this device
- *
- * Assumptions:
- * This function is called from network logic at with the network locked.
- *
- ****************************************************************************/
- FAR struct icmpv6_conn_s *icmpv6_findconn(FAR struct net_driver_s *dev,
- uint16_t id)
- {
- FAR struct icmpv6_conn_s *conn;
- for (conn = icmpv6_nextconn(NULL); conn != NULL;
- conn = icmpv6_nextconn(conn))
- {
- if (conn->id == id && conn->dev == dev && conn->nreqs > 0)
- {
- return conn;
- }
- }
- return conn;
- }
- #endif /* CONFIG_NET_ICMP */
|