2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2014-06-18 18:50:09 +02:00
|
|
|
* net/udp/udp_conn.c
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
2012-06-08 20:56:01 +02:00
|
|
|
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
2007-09-03 01:11:10 +02:00
|
|
|
* Large parts of this file were leveraged from uIP logic:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001-2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2007-09-02 23:58:35 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
2007-09-03 01:11:10 +02:00
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
2007-09-03 22:34:44 +02:00
|
|
|
****************************************************************************/
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2007-09-02 23:58:35 +02:00
|
|
|
* Included Files
|
2007-09-03 22:34:44 +02:00
|
|
|
****************************************************************************/
|
2007-09-02 23:58:35 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)
|
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2007-09-03 01:11:10 +02:00
|
|
|
#include <string.h>
|
2007-09-03 22:34:44 +02:00
|
|
|
#include <semaphore.h>
|
|
|
|
#include <assert.h>
|
2007-09-03 01:11:10 +02:00
|
|
|
#include <errno.h>
|
2007-11-23 20:25:39 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2007-09-03 01:11:10 +02:00
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/net.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <nuttx/net/ip.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/udp.h>
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 02:55:01 +02:00
|
|
|
#include "udp/udp.h"
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2007-09-03 01:11:10 +02:00
|
|
|
* Private Data
|
2007-09-03 22:34:44 +02:00
|
|
|
****************************************************************************/
|
2007-09-03 01:11:10 +02:00
|
|
|
|
|
|
|
/* The array containing all uIP UDP connections. */
|
|
|
|
|
2014-06-25 02:55:01 +02:00
|
|
|
struct udp_conn_s g_udp_connections[CONFIG_NET_UDP_CONNS];
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* A list of all free UDP connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_free_udp_connections;
|
|
|
|
static sem_t g_free_sem;
|
|
|
|
|
|
|
|
/* A list of all allocated UDP connections */
|
|
|
|
|
|
|
|
static dq_queue_t g_active_udp_connections;
|
2007-09-03 01:11:10 +02:00
|
|
|
|
|
|
|
/* Last port used by a UDP connection connection. */
|
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
static uint16_t g_last_udp_port;
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2007-09-02 23:58:35 +02:00
|
|
|
* Private Functions
|
2007-09-03 22:34:44 +02:00
|
|
|
****************************************************************************/
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2014-06-29 03:26:16 +02:00
|
|
|
* Name: _udp_semtake() and _udp_semgive()
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Take/give semaphore
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
static inline void _udp_semtake(FAR sem_t *sem)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
|
|
|
/* Take the semaphore (perhaps waiting) */
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
while (net_lockedwait(sem) != 0)
|
2007-09-03 22:34:44 +02:00
|
|
|
{
|
2011-03-15 21:26:34 +01:00
|
|
|
/* The only case that an error should occur here is if
|
2007-09-03 22:34:44 +02:00
|
|
|
* the wait was awakened by a signal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ASSERT(*get_errno_ptr() == EINTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
#define _udp_semgive(sem) sem_post(sem)
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-29 03:26:16 +02:00
|
|
|
* Name: udp_find_conn()
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find the UDP connection that uses this local port number. Called only
|
2007-09-09 21:47:52 +02:00
|
|
|
* from user user level code, but with interrupts disabled.
|
2007-09-03 22:34:44 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
static FAR struct udp_conn_s *udp_find_conn(uint16_t portno)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
2007-09-09 21:47:52 +02:00
|
|
|
int i;
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-09-09 21:47:52 +02:00
|
|
|
/* Now search each connection structure.*/
|
2007-09-03 22:34:44 +02:00
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_UDP_CONNS; i++)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
2007-10-31 01:13:07 +01:00
|
|
|
if (g_udp_connections[ i ].lport == portno)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
2007-09-09 21:47:52 +02:00
|
|
|
return &g_udp_connections[ i ];
|
2007-09-03 01:11:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-09 21:47:52 +02:00
|
|
|
return NULL;
|
2007-09-03 01:11:10 +02:00
|
|
|
}
|
|
|
|
|
2009-03-19 01:18:21 +01:00
|
|
|
/****************************************************************************
|
2014-06-29 03:26:16 +02:00
|
|
|
* Name: udp_select_port()
|
2009-03-19 01:18:21 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Select an unused port number.
|
|
|
|
*
|
2014-04-12 20:13:01 +02:00
|
|
|
* NOTE that in principle this function could fail if there is no available
|
2009-03-19 01:18:21 +01:00
|
|
|
* port number. There is no check for that case and it would actually
|
|
|
|
* in an infinite loop if that were the case. In this simple, small UDP
|
|
|
|
* implementation, it is reasonable to assume that that error cannot happen
|
|
|
|
* and that a port number will always be available.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* Next available port number
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
static uint16_t udp_select_port(void)
|
2009-03-19 01:18:21 +01:00
|
|
|
{
|
2009-12-15 15:53:45 +01:00
|
|
|
uint16_t portno;
|
2009-03-19 01:18:21 +01:00
|
|
|
|
|
|
|
/* Find an unused local port number. Loop until we find a valid
|
|
|
|
* listen port number that is not being used by any other connection.
|
|
|
|
*/
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
net_lock_t flags = net_lock();
|
2009-03-19 01:18:21 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Guess that the next available port number will be the one after
|
|
|
|
* the last port number assigned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
++g_last_udp_port;
|
|
|
|
|
|
|
|
/* Make sure that the port number is within range */
|
|
|
|
|
|
|
|
if (g_last_udp_port >= 32000)
|
|
|
|
{
|
|
|
|
g_last_udp_port = 4096;
|
|
|
|
}
|
|
|
|
}
|
2014-06-29 03:26:16 +02:00
|
|
|
while (udp_find_conn(htons(g_last_udp_port)));
|
2009-03-19 01:18:21 +01:00
|
|
|
|
|
|
|
/* Initialize and return the connection structure, bind it to the
|
|
|
|
* port number
|
|
|
|
*/
|
|
|
|
|
|
|
|
portno = g_last_udp_port;
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(flags);
|
2009-03-19 01:18:21 +01:00
|
|
|
|
|
|
|
return portno;
|
|
|
|
}
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/****************************************************************************
|
2007-09-02 23:58:35 +02:00
|
|
|
* Public Functions
|
2007-09-03 22:34:44 +02:00
|
|
|
****************************************************************************/
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-03 01:11:10 +02:00
|
|
|
/****************************************************************************
|
2014-06-25 02:55:01 +02:00
|
|
|
* Name: udp_initialize()
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the UDP connection structures. Called once and only from
|
|
|
|
* the UIP layer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2014-06-25 02:55:01 +02:00
|
|
|
void udp_initialize(void)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* Initialize the queues */
|
|
|
|
|
|
|
|
dq_init(&g_free_udp_connections);
|
|
|
|
dq_init(&g_active_udp_connections);
|
|
|
|
sem_init(&g_free_sem, 0, 1);
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
for (i = 0; i < CONFIG_NET_UDP_CONNS; i++)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Mark the connection closed and move it to the free list */
|
|
|
|
|
|
|
|
g_udp_connections[i].lport = 0;
|
|
|
|
dq_addlast(&g_udp_connections[i].node, &g_free_udp_connections);
|
2007-09-03 01:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_last_udp_port = 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:55:01 +02:00
|
|
|
* Name: udp_alloc()
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2014-07-05 22:40:29 +02:00
|
|
|
* Allocate a new, uninitialized UDP connection structure. This is
|
|
|
|
* normally something done by the implementation of the socket() API
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:55:01 +02:00
|
|
|
FAR struct udp_conn_s *udp_alloc(void)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
2014-06-25 02:55:01 +02:00
|
|
|
FAR struct udp_conn_s *conn;
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
/* The free list is only accessed from user, non-interrupt level and
|
|
|
|
* is protected by a semaphore (that behaves like a mutex).
|
|
|
|
*/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
_udp_semtake(&g_free_sem);
|
2014-06-25 02:55:01 +02:00
|
|
|
conn = (FAR struct udp_conn_s *)dq_remfirst(&g_free_udp_connections);
|
2007-09-03 22:34:44 +02:00
|
|
|
if (conn)
|
|
|
|
{
|
2007-09-09 21:47:52 +02:00
|
|
|
/* Make sure that the connection is marked as uninitialized */
|
2007-09-03 22:34:44 +02:00
|
|
|
|
|
|
|
conn->lport = 0;
|
2013-10-11 18:48:00 +02:00
|
|
|
|
|
|
|
/* Enqueue the connection into the active list */
|
|
|
|
|
|
|
|
dq_addlast(&conn->node, &g_active_udp_connections);
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
_udp_semgive(&g_free_sem);
|
2007-09-03 22:34:44 +02:00
|
|
|
return conn;
|
2007-09-03 01:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:55:01 +02:00
|
|
|
* Name: udp_free()
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free a UDP connection structure that is no longer in use. This should be
|
2014-07-05 22:40:29 +02:00
|
|
|
* done by the implementation of close().
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:55:01 +02:00
|
|
|
void udp_free(FAR struct udp_conn_s *conn)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
/* The free list is only accessed from user, non-interrupt level and
|
|
|
|
* is protected by a semaphore (that behaves like a mutex).
|
|
|
|
*/
|
|
|
|
|
2009-06-15 20:58:22 +02:00
|
|
|
DEBUGASSERT(conn->crefs == 0);
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
_udp_semtake(&g_free_sem);
|
2007-09-03 22:34:44 +02:00
|
|
|
conn->lport = 0;
|
2013-10-11 18:48:00 +02:00
|
|
|
|
|
|
|
/* Remove the connection from the active list */
|
|
|
|
|
|
|
|
dq_rem(&conn->node, &g_active_udp_connections);
|
|
|
|
|
|
|
|
/* Free the connection */
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
dq_addlast(&conn->node, &g_free_udp_connections);
|
2014-06-29 03:26:16 +02:00
|
|
|
_udp_semgive(&g_free_sem);
|
2007-09-03 01:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:55:01 +02:00
|
|
|
* Name: udp_active()
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a connection structure that is the appropriate
|
2014-11-08 13:18:21 +01:00
|
|
|
* connection to be used within the provided UDP header
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from UIP logic at interrupt level
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:55:01 +02:00
|
|
|
FAR struct udp_conn_s *udp_active(FAR struct udp_iphdr_s *buf)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
2014-06-25 02:55:01 +02:00
|
|
|
FAR struct udp_conn_s *conn =
|
|
|
|
(FAR struct udp_conn_s *)g_active_udp_connections.head;
|
2007-11-23 20:25:39 +01:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
while (conn)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
|
|
|
/* If the local UDP port is non-zero, the connection is considered
|
|
|
|
* to be used. If so, the local port number is checked against the
|
|
|
|
* destination port number in the received packet. If the two port
|
|
|
|
* numbers match, the remote port number is checked if the
|
|
|
|
* connection is bound to a remote port. Finally, if the
|
|
|
|
* connection is bound to a remote IP address, the source IP
|
|
|
|
* address of the packet is checked.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (conn->lport != 0 && buf->destport == conn->lport &&
|
|
|
|
(conn->rport == 0 || buf->srcport == conn->rport) &&
|
2014-06-29 21:11:08 +02:00
|
|
|
(net_ipaddr_cmp(conn->ripaddr, g_allzeroaddr) ||
|
|
|
|
net_ipaddr_cmp(conn->ripaddr, g_alloneaddr) ||
|
2014-07-02 02:11:05 +02:00
|
|
|
net_ipaddr_hdrcmp(buf->srcipaddr, &conn->ripaddr)))
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
|
|
|
/* Matching connection found.. return a reference to it */
|
2012-06-08 20:56:01 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
break;
|
2007-09-03 01:11:10 +02:00
|
|
|
}
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
/* Look at the next active connection */
|
|
|
|
|
2014-06-25 02:55:01 +02:00
|
|
|
conn = (FAR struct udp_conn_s *)conn->node.flink;
|
2007-09-03 22:34:44 +02:00
|
|
|
}
|
2007-09-03 01:11:10 +02:00
|
|
|
|
2007-11-23 20:25:39 +01:00
|
|
|
return conn;
|
2007-09-03 01:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-07-01 02:03:58 +02:00
|
|
|
* Name: udp_nextconn()
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2007-11-06 00:04:16 +01:00
|
|
|
* Traverse the list of allocated UDP connections
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2007-11-06 00:04:16 +01:00
|
|
|
* This function is called from UIP logic at interrupt level (or with
|
|
|
|
* interrupts disabled).
|
2007-09-03 01:11:10 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-01 02:03:58 +02:00
|
|
|
FAR struct udp_conn_s *udp_nextconn(FAR struct udp_conn_s *conn)
|
2007-09-03 01:11:10 +02:00
|
|
|
{
|
2007-11-06 00:04:16 +01:00
|
|
|
if (!conn)
|
|
|
|
{
|
2014-06-25 02:55:01 +02:00
|
|
|
return (FAR struct udp_conn_s *)g_active_udp_connections.head;
|
2007-11-06 00:04:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-25 02:55:01 +02:00
|
|
|
return (FAR struct udp_conn_s *)conn->node.flink;
|
2007-11-06 00:04:16 +01:00
|
|
|
}
|
2007-09-03 01:11:10 +02:00
|
|
|
}
|
|
|
|
|
2007-10-31 01:13:07 +01:00
|
|
|
/****************************************************************************
|
2014-06-25 02:55:01 +02:00
|
|
|
* Name: udp_bind()
|
2007-10-31 01:13:07 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements the UIP specific parts of the standard UDP
|
|
|
|
* bind() operation.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called from normal user level code.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-06-25 02:55:01 +02:00
|
|
|
int udp_bind(FAR struct udp_conn_s *conn, FAR const struct sockaddr_in6 *addr)
|
2007-10-31 01:13:07 +01:00
|
|
|
#else
|
2014-06-25 02:55:01 +02:00
|
|
|
int udp_bind(FAR struct udp_conn_s *conn, FAR const struct sockaddr_in *addr)
|
2007-10-31 01:13:07 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int ret = -EADDRINUSE;
|
2014-06-26 22:23:21 +02:00
|
|
|
net_lock_t flags;
|
2007-11-23 20:25:39 +01:00
|
|
|
|
2009-03-19 01:18:21 +01:00
|
|
|
/* Is the user requesting to bind to any port? */
|
2007-11-23 20:25:39 +01:00
|
|
|
|
2009-03-19 01:18:21 +01:00
|
|
|
if (!addr->sin_port)
|
|
|
|
{
|
|
|
|
/* Yes.. Find an unused local port number */
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
conn->lport = htons(udp_select_port());
|
2009-03-19 01:18:21 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
else
|
2007-10-31 01:13:07 +01:00
|
|
|
{
|
2007-11-23 20:25:39 +01:00
|
|
|
/* Interrupts must be disabled while access the UDP connection list */
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
flags = net_lock();
|
2007-11-23 20:25:39 +01:00
|
|
|
|
|
|
|
/* Is any other UDP connection bound to this port? */
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
if (!udp_find_conn(addr->sin_port))
|
2007-11-23 20:25:39 +01:00
|
|
|
{
|
|
|
|
/* No.. then bind the socket to the port */
|
|
|
|
|
|
|
|
conn->lport = addr->sin_port;
|
2009-03-19 01:18:21 +01:00
|
|
|
ret = OK;
|
2007-11-23 20:25:39 +01:00
|
|
|
}
|
|
|
|
|
2014-06-26 22:23:21 +02:00
|
|
|
net_unlock(flags);
|
2007-10-31 01:13:07 +01:00
|
|
|
}
|
2014-04-12 20:13:01 +02:00
|
|
|
|
2007-10-31 01:13:07 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-09 21:47:52 +02:00
|
|
|
/****************************************************************************
|
2014-06-25 02:55:01 +02:00
|
|
|
* Name: udp_connect()
|
2007-09-09 21:47:52 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2007-11-06 00:04:16 +01:00
|
|
|
* This function sets up a new UDP connection. The function will
|
|
|
|
* automatically allocate an unused local port for the new
|
|
|
|
* connection. However, another port can be chosen by using the
|
2014-06-25 02:55:01 +02:00
|
|
|
* udp_bind() call, after the udp_connect() function has been
|
2007-11-06 00:04:16 +01:00
|
|
|
* called.
|
2007-09-02 23:58:35 +02:00
|
|
|
*
|
2014-07-05 22:40:29 +02:00
|
|
|
* This function is called as part of the implementation of sendto
|
|
|
|
* and recvfrom.
|
2007-09-09 21:47:52 +02:00
|
|
|
*
|
2014-07-05 22:40:29 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* conn - A reference to UDP connection structure
|
|
|
|
* addr - The address of the remote host.
|
2007-09-09 21:47:52 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function is called user code. Interrupts may be enabled.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
#ifdef CONFIG_NET_IPv6
|
2014-06-25 02:55:01 +02:00
|
|
|
int udp_connect(FAR struct udp_conn_s *conn,
|
|
|
|
FAR const struct sockaddr_in6 *addr)
|
2007-09-03 22:34:44 +02:00
|
|
|
#else
|
2014-06-25 02:55:01 +02:00
|
|
|
int udp_connect(FAR struct udp_conn_s *conn,
|
|
|
|
FAR const struct sockaddr_in *addr)
|
2007-09-03 22:34:44 +02:00
|
|
|
#endif
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2009-03-19 01:18:21 +01:00
|
|
|
/* Has this address already been bound to a local port (lport)? */
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2007-10-31 01:13:07 +01:00
|
|
|
if (!conn->lport)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2009-03-19 01:18:21 +01:00
|
|
|
/* No.. Find an unused local port number and bind it to the
|
|
|
|
* connection structure.
|
2007-09-02 23:58:35 +02:00
|
|
|
*/
|
|
|
|
|
2014-06-29 03:26:16 +02:00
|
|
|
conn->lport = htons(udp_select_port());
|
2007-10-31 01:13:07 +01:00
|
|
|
}
|
2007-09-02 23:58:35 +02:00
|
|
|
|
2009-03-19 01:18:21 +01:00
|
|
|
/* Is there a remote port (rport) */
|
|
|
|
|
2007-09-03 22:34:44 +02:00
|
|
|
if (addr)
|
2007-09-02 23:58:35 +02:00
|
|
|
{
|
2007-09-03 22:34:44 +02:00
|
|
|
conn->rport = addr->sin_port;
|
2014-06-29 21:11:08 +02:00
|
|
|
net_ipaddr_copy(conn->ripaddr, addr->sin_addr.s_addr);
|
2007-09-02 23:58:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-09-09 21:47:52 +02:00
|
|
|
conn->rport = 0;
|
2014-06-29 21:11:08 +02:00
|
|
|
net_ipaddr_copy(conn->ripaddr, g_allzeroaddr);
|
2007-09-09 21:47:52 +02:00
|
|
|
}
|
2007-10-31 01:13:07 +01:00
|
|
|
|
2014-07-07 00:19:26 +02:00
|
|
|
conn->ttl = IP_TTL;
|
2007-09-09 21:47:52 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2007-09-03 01:11:10 +02:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_UDP */
|