OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [ethernet/] [lwIP/] [core/] [udp.c] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 606 jeremybenn
/**
2
 * @file
3
 * User Datagram Protocol module
4
 *
5
 */
6
/*
7
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms, with or without modification,
11
 * are permitted provided that the following conditions are met:
12
 *
13
 * 1. Redistributions of source code must retain the above copyright notice,
14
 *    this list of conditions and the following disclaimer.
15
 * 2. Redistributions in binary form must reproduce the above copyright notice,
16
 *    this list of conditions and the following disclaimer in the documentation
17
 *    and/or other materials provided with the distribution.
18
 * 3. The name of the author may not be used to endorse or promote products
19
 *    derived from this software without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30
 * OF SUCH DAMAGE.
31
 *
32
 * This file is part of the lwIP TCP/IP stack.
33
 *
34
 * Author: Adam Dunkels <adam@sics.se>
35
 *
36
 */
37
 
38
 
39
/* udp.c
40
 *
41
 * The code for the User Datagram Protocol UDP.
42
 *
43
 */
44
 
45
#include <string.h>
46
 
47
#include "lwip/opt.h"
48
 
49
#include "lwip/def.h"
50
#include "lwip/memp.h"
51
#include "lwip/inet.h"
52
#include "lwip/ip_addr.h"
53
#include "lwip/netif.h"
54
#include "lwip/udp.h"
55
#include "lwip/icmp.h"
56
 
57
#include "lwip/stats.h"
58
 
59
#include "arch/perf.h"
60
#include "lwip/snmp.h"
61
 
62
/* The list of UDP PCBs */
63
#if LWIP_UDP
64
/* exported in udp.h (was static) */
65
struct udp_pcb *udp_pcbs = NULL;
66
 
67
static struct udp_pcb *pcb_cache = NULL;
68
 
69
void
70
udp_init(void)
71
{
72
  udp_pcbs = pcb_cache = NULL;
73
}
74
 
75
/**
76
 * Process an incoming UDP datagram.
77
 *
78
 * Given an incoming UDP datagram (as a chain of pbufs) this function
79
 * finds a corresponding UDP PCB and
80
 *
81
 * @param pbuf pbuf to be demultiplexed to a UDP PCB.
82
 * @param netif network interface on which the datagram was received.
83
 *
84
 */
85
void
86
udp_input(struct pbuf *p, struct netif *inp)
87
{
88
  struct udp_hdr *udphdr;
89
  struct udp_pcb *pcb;
90
  struct udp_pcb *uncon_pcb;
91
  struct ip_hdr *iphdr;
92
  u16_t src, dest;
93
  u8_t local_match;
94
 
95
  PERF_START;
96
 
97
  UDP_STATS_INC(udp.recv);
98
 
99
  iphdr = p->payload;
100
 
101
  if (p->tot_len < (IPH_HL(iphdr) * 4 + UDP_HLEN)) {
102
    /* drop short packets */
103
//    LWIP_DEBUGF(UDP_DEBUG,
104
//                ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
105
 
106
    LWIP_DEBUGF(UDP_DEBUG,
107
                ("udp_input: short UDP datagram (%u bytes) discarded\n", p->tot_len));
108
 
109
    UDP_STATS_INC(udp.lenerr);
110
    UDP_STATS_INC(udp.drop);
111
    snmp_inc_udpinerrors();
112
    pbuf_free(p);
113
    goto end;
114
  }
115
 
116
  pbuf_header(p, -((s16_t)(IPH_HL(iphdr) * 4)));
117
 
118
  udphdr = (struct udp_hdr *)p->payload;
119
 
120
  LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
121
 
122
  src = ntohs(udphdr->src);
123
  dest = ntohs(udphdr->dest);
124
 
125
  udp_debug_print(udphdr);
126
 
127
  /* print the UDP source and destination */
128
  LWIP_DEBUGF(UDP_DEBUG,
129
              ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- "
130
               "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
131
               ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),
132
               ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),
133
               ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),
134
               ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));
135
 
136
  local_match = 0;
137
  uncon_pcb = NULL;
138
  /* Iterate through the UDP pcb list for a matching pcb */
139
  for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
140
    /* print the PCB local and remote address */
141
    LWIP_DEBUGF(UDP_DEBUG,
142
                ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- "
143
                 "(%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
144
                 ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
145
                 ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
146
                 ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
147
                 ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
148
 
149
    /* compare PCB local addr+port to UDP destination addr+port */
150
    if ((pcb->local_port == dest) &&
151
        (ip_addr_isany(&pcb->local_ip) ||
152
         ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)) ||
153
         ip_addr_isbroadcast(&(iphdr->dest), inp))) {
154
      local_match = 1;
155
      if ((uncon_pcb == NULL) &&
156
          ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
157
        /* the first unconnected matching PCB */
158
        uncon_pcb = pcb;
159
      }
160
    }
161
    /* compare PCB remote addr+port to UDP source addr+port */
162
    if ((local_match != 0) &&
163
        (pcb->remote_port == src) &&
164
        (ip_addr_isany(&pcb->remote_ip) ||
165
         ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)))) {
166
      /* the first fully matching PCB */
167
      break;
168
    }
169
  }
170
  /* no fully matching pcb found? then look for an unconnected pcb */
171
  if (pcb == NULL) {
172
    pcb = uncon_pcb;
173
  }
174
 
175
  /* Check checksum if this is a match or if it was directed at us. */
176
  if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &iphdr->dest)) {
177
    LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: calculating checksum\n"));
178
#ifdef IPv6
179
    if (iphdr->nexthdr == IP_PROTO_UDPLITE) {
180
#else
181
    if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
182
#endif /* IPv4 */
183
      /* Do the UDP Lite checksum */
184
#if CHECKSUM_CHECK_UDP
185
      if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
186
                             (struct ip_addr *)&(iphdr->dest),
187
                             IP_PROTO_UDPLITE, ntohs(udphdr->len)) != 0) {
188
        LWIP_DEBUGF(UDP_DEBUG | 2,
189
                    ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
190
        UDP_STATS_INC(udp.chkerr);
191
        UDP_STATS_INC(udp.drop);
192
        snmp_inc_udpinerrors();
193
        pbuf_free(p);
194
        goto end;
195
      }
196
#endif
197
    } else {
198
#if CHECKSUM_CHECK_UDP
199
      if (udphdr->chksum != 0) {
200
        if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
201
                               (struct ip_addr *)&(iphdr->dest),
202
                               IP_PROTO_UDP, p->tot_len) != 0) {
203
          LWIP_DEBUGF(UDP_DEBUG | 2,
204
                      ("udp_input: UDP datagram discarded due to failing checksum\n"));
205
          UDP_STATS_INC(udp.chkerr);
206
          UDP_STATS_INC(udp.drop);
207
          snmp_inc_udpinerrors();
208
          pbuf_free(p);
209
          goto end;
210
        }
211
      }
212
#endif
213
    }
214
    pbuf_header(p, -UDP_HLEN);
215
    if (pcb != NULL) {
216
      snmp_inc_udpindatagrams();
217
      /* callback */
218
      if (pcb->recv != NULL)
219
        pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src), src);
220
    } else {
221
      LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: not for us.\n"));
222
 
223
      /* No match was found, send ICMP destination port unreachable unless
224
         destination address was broadcast/multicast. */
225
 
226
      if (!ip_addr_isbroadcast(&iphdr->dest, inp) &&
227
          !ip_addr_ismulticast(&iphdr->dest)) {
228
 
229
        /* restore pbuf pointer */
230
        p->payload = iphdr;
231
        icmp_dest_unreach(p, ICMP_DUR_PORT);
232
      }
233
      UDP_STATS_INC(udp.proterr);
234
      UDP_STATS_INC(udp.drop);
235
      snmp_inc_udpnoports();
236
      pbuf_free(p);
237
    }
238
  } else {
239
    pbuf_free(p);
240
  }
241
end:
242
  PERF_STOP("udp_input");
243
}
244
 
245
/**
246
 * Send data to a specified address using UDP.
247
 *
248
 * @param pcb UDP PCB used to send the data.
249
 * @param pbuf chain of pbuf's to be sent.
250
 * @param dst_ip Destination IP address.
251
 * @param dst_port Destination UDP port.
252
 *
253
 * If the PCB already has a remote address association, it will
254
 * be restored after the data is sent.
255
 *
256
 * @return lwIP error code.
257
 * - ERR_OK. Successful. No error occured.
258
 * - ERR_MEM. Out of memory.
259
 * - ERR_RTE. Could not find route to destination address.
260
 *
261
 * @see udp_disconnect() udp_send()
262
 */
263
err_t
264
udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
265
  struct ip_addr *dst_ip, u16_t dst_port)
266
{
267
  err_t err;
268
  /* temporary space for current PCB remote address */
269
  struct ip_addr pcb_remote_ip;
270
  u16_t pcb_remote_port;
271
  /* remember current remote peer address of PCB */
272
  pcb_remote_ip.addr = pcb->remote_ip.addr;
273
  pcb_remote_port = pcb->remote_port;
274
  /* copy packet destination address to PCB remote peer address */
275
  pcb->remote_ip.addr = dst_ip->addr;
276
  pcb->remote_port = dst_port;
277
  /* send to the packet destination address */
278
  err = udp_send(pcb, p);
279
  /* restore PCB remote peer address */
280
  pcb->remote_ip.addr = pcb_remote_ip.addr;
281
  pcb->remote_port = pcb_remote_port;
282
  return err;
283
}
284
 
285
/**
286
 * Send data using UDP.
287
 *
288
 * @param pcb UDP PCB used to send the data.
289
 * @param pbuf chain of pbuf's to be sent.
290
 *
291
 * @return lwIP error code.
292
 * - ERR_OK. Successful. No error occured.
293
 * - ERR_MEM. Out of memory.
294
 * - ERR_RTE. Could not find route to destination address.
295
 *
296
 * @see udp_disconnect() udp_sendto()
297
 */
298
err_t
299
udp_send(struct udp_pcb *pcb, struct pbuf *p)
300
{
301
  struct udp_hdr *udphdr;
302
  struct netif *netif;
303
  struct ip_addr *src_ip;
304
  err_t err;
305
  struct pbuf *q; /* q will be sent down the stack */
306
 
307
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_send\n"));
308
 
309
  /* if the PCB is not yet bound to a port, bind it here */
310
  if (pcb->local_port == 0) {
311
    LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding now\n"));
312
    err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
313
    if (err != ERR_OK) {
314
      LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: forced port bind failed\n"));
315
      return err;
316
    }
317
  }
318
  /* find the outgoing network interface for this packet */
319
  netif = ip_route(&(pcb->remote_ip));
320
  /* no outgoing network interface could be found? */
321
  if (netif == NULL) {
322
    LWIP_DEBUGF(UDP_DEBUG | 1, ("udp_send: No route to 0x%"X32_F"\n", pcb->remote_ip.addr));
323
    UDP_STATS_INC(udp.rterr);
324
    return ERR_RTE;
325
  }
326
 
327
  /* not enough space to add an UDP header to first pbuf in given p chain? */
328
  if (pbuf_header(p, UDP_HLEN)) {
329
    /* allocate header in a seperate new pbuf */
330
    q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
331
    /* new header pbuf could not be allocated? */
332
    if (q == NULL) {
333
      LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: could not allocate header\n"));
334
      return ERR_MEM;
335
    }
336
    /* chain header q in front of given pbuf p */
337
    pbuf_chain(q, p);
338
    /* first pbuf q points to header pbuf */
339
    LWIP_DEBUGF(UDP_DEBUG,
340
                ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
341
    /* adding a header within p succeeded */
342
  } else {
343
    /* first pbuf q equals given pbuf */
344
    q = p;
345
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
346
  }
347
  /* q now represents the packet to be sent */
348
  udphdr = q->payload;
349
  udphdr->src = htons(pcb->local_port);
350
  udphdr->dest = htons(pcb->remote_port);
351
  /* in UDP, 0 checksum means 'no checksum' */
352
  udphdr->chksum = 0x0000;
353
 
354
  /* PCB local address is IP_ANY_ADDR? */
355
  if (ip_addr_isany(&pcb->local_ip)) {
356
    /* use outgoing network interface IP address as source address */
357
    src_ip = &(netif->ip_addr);
358
  } else {
359
    /* use UDP PCB local IP address as source address */
360
    src_ip = &(pcb->local_ip);
361
  }
362
 
363
  LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
364
 
365
  /* UDP Lite protocol? */
366
  if (pcb->flags & UDP_FLAGS_UDPLITE) {
367
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
368
    /* set UDP message length in UDP header */
369
    udphdr->len = htons(pcb->chksum_len);
370
    /* calculate checksum */
371
#if CHECKSUM_GEN_UDP
372
    udphdr->chksum = inet_chksum_pseudo(q, src_ip, &(pcb->remote_ip),
373
                                        IP_PROTO_UDP, pcb->chksum_len);
374
    /* chksum zero must become 0xffff, as zero means 'no checksum' */
375
    if (udphdr->chksum == 0x0000)
376
      udphdr->chksum = 0xffff;
377
#else
378
    udphdr->chksum = 0x0000;
379
#endif
380
    /* output to IP */
381
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
382
    err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);
383
  } else {      /* UDP */
384
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
385
    udphdr->len = htons(q->tot_len);
386
    /* calculate checksum */
387
#if CHECKSUM_GEN_UDP
388
    if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
389
      udphdr->chksum = inet_chksum_pseudo(q, src_ip, &pcb->remote_ip, IP_PROTO_UDP, q->tot_len);
390
      /* chksum zero must become 0xffff, as zero means 'no checksum' */
391
      if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
392
    }
393
#else
394
    udphdr->chksum = 0x0000;
395
#endif
396
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
397
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
398
    /* output to IP */
399
    err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);
400
  }
401
  /* TODO: must this be increased even if error occured? */
402
  snmp_inc_udpoutdatagrams();
403
 
404
  /* did we chain a seperate header pbuf earlier? */
405
  if (q != p) {
406
    /* free the header pbuf */
407
    pbuf_free(q);
408
    q = NULL;
409
    /* p is still referenced by the caller, and will live on */
410
  }
411
 
412
  UDP_STATS_INC(udp.xmit);
413
  return err;
414
}
415
 
416
/**
417
 * Bind an UDP PCB.
418
 *
419
 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
420
 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
421
 * bind to all local interfaces.
422
 * @param port local UDP port to bind with.
423
 *
424
 * @return lwIP error code.
425
 * - ERR_OK. Successful. No error occured.
426
 * - ERR_USE. The specified ipaddr and port are already bound to by
427
 * another UDP PCB.
428
 *
429
 * @see udp_disconnect()
430
 */
431
err_t
432
udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
433
{
434
  struct udp_pcb *ipcb;
435
  u8_t rebind;
436
 
437
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_bind(ipaddr = "));
438
  ip_addr_debug_print(UDP_DEBUG, ipaddr);
439
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, (", port = %"U16_F")\n", port));
440
 
441
  rebind = 0;
442
  /* Check for double bind and rebind of the same pcb */
443
  for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
444
    /* is this UDP PCB already on active list? */
445
    if (pcb == ipcb) {
446
      /* pcb may occur at most once in active list */
447
      LWIP_ASSERT("rebind == 0", rebind == 0);
448
      /* pcb already in list, just rebind */
449
      rebind = 1;
450
    }
451
 
452
    /* this code does not allow upper layer to share a UDP port for
453
       listening to broadcast or multicast traffic (See SO_REUSE_ADDR and
454
       SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR
455
       combine with implementation of UDP PCB flags. Leon Woestenberg. */
456
#ifdef LWIP_UDP_TODO
457
    /* port matches that of PCB in list? */
458
    else
459
      if ((ipcb->local_port == port) &&
460
          /* IP address matches, or one is IP_ADDR_ANY? */
461
          (ip_addr_isany(&(ipcb->local_ip)) ||
462
           ip_addr_isany(ipaddr) ||
463
           ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
464
        /* other PCB already binds to this local IP and port */
465
        LWIP_DEBUGF(UDP_DEBUG,
466
                    ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
467
        return ERR_USE;
468
      }
469
#endif
470
  }
471
 
472
  ip_addr_set(&pcb->local_ip, ipaddr);
473
 
474
  /* no port specified? */
475
  if (port == 0) {
476
#ifndef UDP_LOCAL_PORT_RANGE_START
477
#define UDP_LOCAL_PORT_RANGE_START 4096
478
#define UDP_LOCAL_PORT_RANGE_END   0x7fff
479
#endif
480
    port = UDP_LOCAL_PORT_RANGE_START;
481
    ipcb = udp_pcbs;
482
    while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
483
      if (ipcb->local_port == port) {
484
        port++;
485
        ipcb = udp_pcbs;
486
      } else
487
        ipcb = ipcb->next;
488
    }
489
    if (ipcb != NULL) {
490
      /* no more ports available in local range */
491
      LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
492
      return ERR_USE;
493
    }
494
  }
495
  pcb->local_port = port;
496
  snmp_insert_udpidx_tree(pcb);
497
  /* pcb not active yet? */
498
  if (rebind == 0) {
499
    /* place the PCB on the active list if not already there */
500
    pcb->next = udp_pcbs;
501
    udp_pcbs = pcb;
502
  }
503
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE,
504
              ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",
505
               (u16_t)(ntohl(pcb->local_ip.addr) >> 24 & 0xff),
506
               (u16_t)(ntohl(pcb->local_ip.addr) >> 16 & 0xff),
507
               (u16_t)(ntohl(pcb->local_ip.addr) >> 8 & 0xff),
508
               (u16_t)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));
509
  return ERR_OK;
510
}
511
/**
512
 * Connect an UDP PCB.
513
 *
514
 * This will associate the UDP PCB with the remote address.
515
 *
516
 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
517
 * @param ipaddr remote IP address to connect with.
518
 * @param port remote UDP port to connect with.
519
 *
520
 * @return lwIP error code
521
 *
522
 * @see udp_disconnect()
523
 */
524
err_t
525
udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
526
{
527
  struct udp_pcb *ipcb;
528
 
529
  if (pcb->local_port == 0) {
530
    err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
531
    if (err != ERR_OK)
532
      return err;
533
  }
534
 
535
  ip_addr_set(&pcb->remote_ip, ipaddr);
536
  pcb->remote_port = port;
537
  pcb->flags |= UDP_FLAGS_CONNECTED;
538
/** TODO: this functionality belongs in upper layers */
539
#ifdef LWIP_UDP_TODO
540
  /* Nail down local IP for netconn_addr()/getsockname() */
541
  if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
542
    struct netif *netif;
543
 
544
    if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
545
      LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
546
      UDP_STATS_INC(udp.rterr);
547
      return ERR_RTE;
548
    }
549
    /** TODO: this will bind the udp pcb locally, to the interface which
550
        is used to route output packets to the remote address. However, we
551
        might want to accept incoming packets on any interface! */
552
    pcb->local_ip = netif->ip_addr;
553
  } else if (ip_addr_isany(&pcb->remote_ip)) {
554
    pcb->local_ip.addr = 0;
555
  }
556
#endif
557
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE,
558
              ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",
559
               (u16_t)(ntohl(pcb->remote_ip.addr) >> 24 & 0xff),
560
               (u16_t)(ntohl(pcb->remote_ip.addr) >> 16 & 0xff),
561
               (u16_t)(ntohl(pcb->remote_ip.addr) >> 8 & 0xff),
562
               (u16_t)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));
563
 
564
  /* Insert UDP PCB into the list of active UDP PCBs. */
565
  for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
566
    if (pcb == ipcb) {
567
      /* already on the list, just return */
568
      return ERR_OK;
569
    }
570
  }
571
  /* PCB not yet on the list, add PCB now */
572
  pcb->next = udp_pcbs;
573
  udp_pcbs = pcb;
574
  return ERR_OK;
575
}
576
 
577
void
578
udp_disconnect(struct udp_pcb *pcb)
579
{
580
  /* reset remote address association */
581
  ip_addr_set(&pcb->remote_ip, IP_ADDR_ANY);
582
  pcb->remote_port = 0;
583
  /* mark PCB as unconnected */
584
  pcb->flags &= ~UDP_FLAGS_CONNECTED;
585
}
586
 
587
void
588
udp_recv(struct udp_pcb *pcb,
589
         void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,
590
                       struct ip_addr *addr, u16_t port),
591
         void *recv_arg)
592
{
593
  /* remember recv() callback and user data */
594
  pcb->recv = recv;
595
  pcb->recv_arg = recv_arg;
596
}
597
 
598
/**
599
 * Remove an UDP PCB.
600
 *
601
 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
602
 * UDP PCB's and the data structure is freed from memory.
603
 *
604
 * @see udp_new()
605
 */
606
void
607
udp_remove(struct udp_pcb *pcb)
608
{
609
  struct udp_pcb *pcb2;
610
 
611
  snmp_delete_udpidx_tree(pcb);
612
  /* pcb to be removed is first in list? */
613
  if (udp_pcbs == pcb) {
614
    /* make list start at 2nd pcb */
615
    udp_pcbs = udp_pcbs->next;
616
    /* pcb not 1st in list */
617
  } else
618
    for (pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
619
      /* find pcb in udp_pcbs list */
620
      if (pcb2->next != NULL && pcb2->next == pcb) {
621
        /* remove pcb from list */
622
        pcb2->next = pcb->next;
623
      }
624
    }
625
  memp_free(MEMP_UDP_PCB, pcb);
626
}
627
 
628
/**
629
 * Create a UDP PCB.
630
 *
631
 * @return The UDP PCB which was created. NULL if the PCB data structure
632
 * could not be allocated.
633
 *
634
 * @see udp_remove()
635
 */
636
struct udp_pcb *
637
udp_new(void)
638
{
639
  struct udp_pcb *pcb;
640
  pcb = memp_malloc(MEMP_UDP_PCB);
641
  /* could allocate UDP PCB? */
642
  if (pcb != NULL) {
643
    /* initialize PCB to all zeroes */
644
    memset(pcb, 0, sizeof(struct udp_pcb));
645
    pcb->ttl = UDP_TTL;
646
  }
647
  return pcb;
648
}
649
 
650
#if UDP_DEBUG
651
void
652
udp_debug_print(struct udp_hdr *udphdr)
653
{
654
  LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
655
  LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
656
  LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     %5"U16_F"     | (src port, dest port)\n",
657
                          ntohs(udphdr->src), ntohs(udphdr->dest)));
658
  LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
659
  LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     0x%04"X16_F"    | (len, chksum)\n",
660
                          ntohs(udphdr->len), ntohs(udphdr->chksum)));
661
  LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
662
}
663
#endif /* UDP_DEBUG */
664
 
665
#endif /* LWIP_UDP */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.