summaryrefslogtreecommitdiff
path: root/nuttx/net/uip/uip-udpconn.c
blob: 1738e5d5a45d2897e7cb2520e289ec35a0583a13 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/************************************************************
 * uip-udpconn.c
 *
 *   Copyright (C) 2007 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * Large parts of this file were leveraged from uIP logic:
 *
 *   Copyright (c) 2001-2003, Adam Dunkels.
 *   All rights reserved.
 *
 * 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
 *    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.
 *
 ************************************************************/

/************************************************************
 * Compilation Switches
 ************************************************************/

/************************************************************
 * Included Files
 ************************************************************/

#include <nuttx/config.h>
#if defined(CONFIG_NET) && defined(CONFIG_NET_UDP)

#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <arch/irq.h>

#include <net/uip/uipopt.h>
#include <net/uip/uip.h>
#include <net/uip/uip-arch.h>

#include "uip-internal.h"

/************************************************************
 * Private Data
 ************************************************************/

/* The array containing all uIP UDP connections. */

struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];

/* Last port used by a UDP connection connection. */

static uint16 g_last_udp_port;

/************************************************************
 * Private Functions
 ************************************************************/

#ifdef CONFIG_NET_UDP
struct uip_udp_conn *uip_find_udp_conn( uint16 portno )
{
  struct uip_udp_conn *conn;
  int i;

  for (i = 0; i < UIP_UDP_CONNS; i++)
    {
      if (uip_udp_conns[i].lport == htons(g_last_udp_port))
        {
          return conn;
        }
    }

  return NULL;
}
#endif   /* CONFIG_NET_UDP */

/************************************************************
 * Public Functions
 ************************************************************/

/****************************************************************************
 * Name: uip_udpinit()
 *
 * Description:
 *   Initialize the UDP connection structures.  Called once and only from
 *   the UIP layer.
 *
 ****************************************************************************/

void uip_udpinit(void)
{
  int i;
  for (i = 0; i < UIP_UDP_CONNS; i++)
    {
      uip_udp_conns[i].lport = 0;
    }

  g_last_udp_port = 1024;
}

/****************************************************************************
 * Name: uip_udpalloc()
 *
 * Description:
 *   Find a free UDP connection structure and allocate it for use.  This is
 *   normally something done by the implementation of the socket() API.
 *
 ****************************************************************************/

struct uip_udp_conn *uip_udpalloc(void)
{
#warning "Need to implement allocation logic"
  return NULL;
}

/****************************************************************************
 * Name: uip_udpfree()
 *
 * Description:
 *   Free a UDP connection structure that is no longer in use. This should be
 *   done by the implementation of close()
 *
 ****************************************************************************/

void uip_udpfree(struct uip_udp_conn *conn)
{
#warning "Need to implement release logic"
}

/****************************************************************************
 * Name: uip_udpactive()
 *
 * Description:
 *   Find a connection structure that is the appropriate
 *   connection to be used withi the provided TCP/IP header
 *
 * Assumptions:
 *   This function is called from UIP logic at interrupt level
 *
 ****************************************************************************/

struct uip_udp_conn *uip_udpactive(struct uip_udpip_hdr *buf)
{
  struct uip_udp_conn *conn;
  for (conn = &uip_udp_conns[0]; conn < &uip_udp_conns[UIP_UDP_CONNS]; conn++)
    {
      /* 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) &&
            (uip_ipaddr_cmp(conn->ripaddr, all_zeroes_addr) ||
             uip_ipaddr_cmp(conn->ripaddr, all_ones_addr) ||
             uip_ipaddr_cmp(buf->srcipaddr, conn->ripaddr)))
        {
          /* Matching connection found.. return a reference to it */

          return conn;
        }
    }

  /* No match found */

  return NULL;
}

/****************************************************************************
 * Name: uip_udppoll()
 *
 * Description:
 *   Periodic processing for a UDP connection identified by its number.
 *   This function does the necessary periodic processing (timers,
 *   polling) for a uIP TCP conneciton, and should be called by the UIP
 *   device driver when the periodic uIP timer goes off. It should be
 *   called for every connection, regardless of whether they are open of
 *   closed.
 *
 * Assumptions:
 *   This function is called from the CAN device driver may be called from
 *   the timer interrupt/watchdog handle level.
 *
 ****************************************************************************/

void uip_udppoll(unsigned int conn)
{
  uip_udp_conn = &uip_udp_conns[conn];
  uip_interrupt(UIP_UDP_TIMER);
}

/****************************************************************************
 * Name: uip_tcpbind()
 *
 * Description:
 *   This function implements the UIP specific parts of the standard TCP
 *   bind() operation.
 *
 * Assumptions:
 *   This function is called from normal user level code.
 *
 ****************************************************************************/

#ifdef CONFIG_NET_IPv6
int uip_udpbind(struct uip_udp_conn *conn, const struct sockaddr_in6 *addr)
#else
int uip_udpbind(struct uip_udp_conn *conn, const struct sockaddr_in *addr)
#endif
{
#warning "Need to implement bind logic"
  return -ENOSYS;
}

/* Set up a new UDP connection.
 *
 * 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
 * uip_udp_bind() call, after the uip_udp_new() function has been
 * called.
 *
 * Example:
 *
 *   uip_ipaddr_t addr;
 *   struct uip_udp_conn *c;
 * 
 *   uip_ipaddr(&addr, 192,168,2,1);
 *   c = uip_udp_new(&addr, HTONS(12345));
 *   if(c != NULL) {
 *     uip_udp_bind(c, HTONS(12344));
 *   }
 *
 * ripaddr The IP address of the remote host.
 *
 * rport The remote port number in network byte order.
 *
 * Return:  The uip_udp_conn structure for the new connection or NULL
 * if no connection could be allocated.
 */

struct uip_udp_conn *uip_udp_new(uip_ipaddr_t *ripaddr, uint16 rport)
{
  struct uip_udp_conn *conn;
  int i;

  /* Find an unused local port number.  Loop until we find a valid listen port
   * number that is not being used by any other connection.
   */

  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;
        }
    }
  while (uip_find_udp_conn(g_last_udp_port));

  /* Now find an available UDP connection structure */

  conn = 0;
  for (i = 0; i < UIP_UDP_CONNS; i++)
    {
      if (uip_udp_conns[i].lport == 0)
        {
          conn = &uip_udp_conns[i];
          break;
        }
    }

  /* Return an error if no connection is available */

  if (conn == 0)
    {
      return 0;
    }

  /* Initialize and return the connection structure, bind it to the port number */

  conn->lport = HTONS(g_last_udp_port);
  conn->rport = rport;

  if (ripaddr == NULL)
    {
      memset(conn->ripaddr, 0, sizeof(uip_ipaddr_t));
    }
  else
    {
      uip_ipaddr_copy(&conn->ripaddr, ripaddr);
    }

  conn->ttl = UIP_TTL;

  return conn;
}

#endif /* CONFIG_NET && CONFIG_NET_UDP */