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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [lwIP_MCF5235_GCC/] [lwip/] [src/] [core/] [udp.c] - Blame information for rev 583

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 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
/* was static, but we may want to access this from a socket layer */
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 (pbuf_header(p, -((s16_t)(UDP_HLEN + IPH_HL(iphdr) * 4)))) {
102
    /* drop short packets */
103
    LWIP_DEBUGF(UDP_DEBUG, ("udp_input: short UDP datagram (%"U16_F" bytes) discarded\n", p->tot_len));
104
    UDP_STATS_INC(udp.lenerr);
105
    UDP_STATS_INC(udp.drop);
106
    snmp_inc_udpinerrors();
107
    pbuf_free(p);
108
    goto end;
109
  }
110
 
111
  udphdr = (struct udp_hdr *)((u8_t *)p->payload - UDP_HLEN);
112
 
113
  LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %"U16_F"\n", p->tot_len));
114
 
115
  src = ntohs(udphdr->src);
116
  dest = ntohs(udphdr->dest);
117
 
118
  udp_debug_print(udphdr);
119
 
120
  /* print the UDP source and destination */
121
  LWIP_DEBUGF(UDP_DEBUG, ("udp (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") <-- (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
122
    ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),
123
    ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),
124
    ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),
125
    ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));
126
 
127
  local_match = 0;
128
  uncon_pcb = NULL;
129
  /* Iterate through the UDP pcb list for a matching pcb */
130
  for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
131
    /* print the PCB local and remote address */
132
    LWIP_DEBUGF(UDP_DEBUG, ("pcb (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F") --- (%"U16_F".%"U16_F".%"U16_F".%"U16_F", %"U16_F")\n",
133
      ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
134
      ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
135
      ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
136
      ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
137
 
138
    /* compare PCB local addr+port to UDP destination addr+port */
139
    if ((pcb->local_port == dest) &&
140
       (ip_addr_isany(&pcb->local_ip) ||
141
        ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)))) {
142
       local_match = 1;
143
       if ((uncon_pcb == NULL) &&
144
           ((pcb->flags & UDP_FLAGS_CONNECTED) == 0)) {
145
         /* the first unconnected matching PCB */
146
         uncon_pcb = pcb;
147
       }
148
    }
149
    /* compare PCB remote addr+port to UDP source addr+port */
150
    if ((local_match != 0) &&
151
       (pcb->remote_port == src) &&
152
       (ip_addr_isany(&pcb->remote_ip) ||
153
       ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src)))) {
154
       /* the first fully matching PCB */
155
      break;
156
    }
157
  }
158
  /* no fully matching pcb found? then look for an unconnected pcb */
159
  if (pcb == NULL) {
160
    pcb = uncon_pcb;
161
  }
162
 
163
  /* Check checksum if this is a match or if it was directed at us. */
164
  if (pcb != NULL  || ip_addr_cmp(&inp->ip_addr, &iphdr->dest))
165
    {
166
    LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: calculating checksum\n"));
167
    pbuf_header(p, UDP_HLEN);
168
#ifdef IPv6
169
    if (iphdr->nexthdr == IP_PROTO_UDPLITE) {
170
#else
171
    if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
172
#endif /* IPv4 */
173
      /* Do the UDP Lite checksum */
174
#if CHECKSUM_CHECK_UDP
175
      if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
176
         (struct ip_addr *)&(iphdr->dest),
177
         IP_PROTO_UDPLITE, ntohs(udphdr->len)) != 0) {
178
  LWIP_DEBUGF(UDP_DEBUG | 2, ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
179
  UDP_STATS_INC(udp.chkerr);
180
  UDP_STATS_INC(udp.drop);
181
  snmp_inc_udpinerrors();
182
  pbuf_free(p);
183
  goto end;
184
      }
185
#endif
186
    } else {
187
#if CHECKSUM_CHECK_UDP
188
      if (udphdr->chksum != 0) {
189
  if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
190
       (struct ip_addr *)&(iphdr->dest),
191
        IP_PROTO_UDP, p->tot_len) != 0) {
192
    LWIP_DEBUGF(UDP_DEBUG | 2, ("udp_input: UDP datagram discarded due to failing checksum\n"));
193
 
194
    UDP_STATS_INC(udp.chkerr);
195
    UDP_STATS_INC(udp.drop);
196
    snmp_inc_udpinerrors();
197
    pbuf_free(p);
198
    goto end;
199
  }
200
      }
201
#endif
202
    }
203
    pbuf_header(p, -UDP_HLEN);
204
    if (pcb != NULL) {
205
      snmp_inc_udpindatagrams();
206
      /* callback */
207
      if (pcb->recv != NULL)
208
      {
209
        pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src), src);
210
      }
211
        } else {
212
      LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: not for us.\n"));
213
 
214
      /* No match was found, send ICMP destination port unreachable unless
215
      destination address was broadcast/multicast. */
216
 
217
      if (!ip_addr_isbroadcast(&iphdr->dest, inp) &&
218
          !ip_addr_ismulticast(&iphdr->dest)) {
219
 
220
  /* adjust pbuf pointer */
221
  p->payload = iphdr;
222
  icmp_dest_unreach(p, ICMP_DUR_PORT);
223
      }
224
      UDP_STATS_INC(udp.proterr);
225
      UDP_STATS_INC(udp.drop);
226
    snmp_inc_udpnoports();
227
      pbuf_free(p);
228
    }
229
  } else {
230
    pbuf_free(p);
231
  }
232
  end:
233
 
234
  PERF_STOP("udp_input");
235
}
236
 
237
/**
238
 * Send data to a specified address using UDP.
239
 *
240
 * @param pcb UDP PCB used to send the data.
241
 * @param pbuf chain of pbuf's to be sent.
242
 * @param dst_ip Destination IP address.
243
 * @param dst_port Destination UDP port.
244
 *
245
 * If the PCB already has a remote address association, it will
246
 * be restored after the data is sent.
247
 *
248
 * @return lwIP error code.
249
 * - ERR_OK. Successful. No error occured.
250
 * - ERR_MEM. Out of memory.
251
 * - ERR_RTE. Could not find route to destination address.
252
 *
253
 * @see udp_disconnect() udp_send()
254
 */
255
err_t
256
udp_sendto(struct udp_pcb *pcb, struct pbuf *p,
257
  struct ip_addr *dst_ip, u16_t dst_port)
258
{
259
  err_t err;
260
  /* temporary space for current PCB remote address */
261
  struct ip_addr pcb_remote_ip;
262
  u16_t pcb_remote_port;
263
  /* remember current remote peer address of PCB */
264
  pcb_remote_ip.addr = pcb->remote_ip.addr;
265
  pcb_remote_port = pcb->remote_port;
266
  /* copy packet destination address to PCB remote peer address */
267
  pcb->remote_ip.addr = dst_ip->addr;
268
  pcb->remote_port = dst_port;
269
  /* send to the packet destination address */
270
  err = udp_send(pcb, p);
271
  /* restore PCB remote peer address */
272
  pcb->remote_ip.addr = pcb_remote_ip.addr;
273
  pcb->remote_port = pcb_remote_port;
274
  return err;
275
}
276
 
277
/**
278
 * Send data using UDP.
279
 *
280
 * @param pcb UDP PCB used to send the data.
281
 * @param pbuf chain of pbuf's to be sent.
282
 *
283
 * @return lwIP error code.
284
 * - ERR_OK. Successful. No error occured.
285
 * - ERR_MEM. Out of memory.
286
 * - ERR_RTE. Could not find route to destination address.
287
 *
288
 * @see udp_disconnect() udp_sendto()
289
 */
290
err_t
291
udp_send(struct udp_pcb *pcb, struct pbuf *p)
292
{
293
  struct udp_hdr *udphdr;
294
  struct netif *netif;
295
  struct ip_addr *src_ip;
296
  err_t err;
297
  struct pbuf *q; /* q will be sent down the stack */
298
 
299
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_send\n"));
300
 
301
  /* if the PCB is not yet bound to a port, bind it here */
302
  if (pcb->local_port == 0) {
303
    LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding now\n"));
304
    err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
305
    if (err != ERR_OK) {
306
      LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: forced port bind failed\n"));
307
      return err;
308
    }
309
  }
310
  /* find the outgoing network interface for this packet */
311
  netif = ip_route(&(pcb->remote_ip));
312
  /* no outgoing network interface could be found? */
313
  if (netif == NULL) {
314
    LWIP_DEBUGF(UDP_DEBUG | 1, ("udp_send: No route to 0x%"X32_F"\n", pcb->remote_ip.addr));
315
    UDP_STATS_INC(udp.rterr);
316
    return ERR_RTE;
317
  }
318
 
319
  /* not enough space to add an UDP header to first pbuf in given p chain? */
320
  if (pbuf_header(p, UDP_HLEN)) {
321
    /* allocate header in a seperate new pbuf */
322
    q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
323
    /* new header pbuf could not be allocated? */
324
    if (q == NULL) {
325
      LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: could not allocate header\n"));
326
      return ERR_MEM;
327
    }
328
    /* chain header q in front of given pbuf p */
329
    pbuf_chain(q, p);
330
    /* { first pbuf q points to header pbuf } */
331
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
332
  /* adding a header within p succeeded */
333
  } else {
334
    /* first pbuf q equals given pbuf */
335
    q = p;
336
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
337
  }
338
  /* { q now represents the packet to be sent } */
339
  udphdr = q->payload;
340
  udphdr->src = htons(pcb->local_port);
341
  udphdr->dest = htons(pcb->remote_port);
342
  /* in UDP, 0 checksum means 'no checksum' */
343
  udphdr->chksum = 0x0000;
344
 
345
  /* PCB local address is IP_ANY_ADDR? */
346
  if (ip_addr_isany(&pcb->local_ip)) {
347
    /* use outgoing network interface IP address as source address */
348
    src_ip = &(netif->ip_addr);
349
  } else {
350
    /* use UDP PCB local IP address as source address */
351
    src_ip = &(pcb->local_ip);
352
  }
353
 
354
  LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %"U16_F"\n", q->tot_len));
355
 
356
  /* UDP Lite protocol? */
357
  if (pcb->flags & UDP_FLAGS_UDPLITE) {
358
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %"U16_F"\n", q->tot_len));
359
    /* set UDP message length in UDP header */
360
    udphdr->len = htons(pcb->chksum_len);
361
    /* calculate checksum */
362
#if CHECKSUM_GEN_UDP
363
    udphdr->chksum = inet_chksum_pseudo(q, src_ip, &(pcb->remote_ip),
364
          IP_PROTO_UDP, pcb->chksum_len);
365
    /* chksum zero must become 0xffff, as zero means 'no checksum' */
366
    if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
367
#else
368
    udphdr->chksum = 0x0000;
369
#endif
370
    /* output to IP */
371
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDPLITE,)\n"));
372
    err = ip_output_if (q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDPLITE, netif);
373
  /* UDP */
374
  } else {
375
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %"U16_F"\n", q->tot_len));
376
    udphdr->len = htons(q->tot_len);
377
    /* calculate checksum */
378
#if CHECKSUM_GEN_UDP
379
    if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
380
      udphdr->chksum = inet_chksum_pseudo(q, src_ip, &pcb->remote_ip, IP_PROTO_UDP, q->tot_len);
381
      /* chksum zero must become 0xffff, as zero means 'no checksum' */
382
      if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
383
    }
384
#else
385
    udphdr->chksum = 0x0000;
386
#endif
387
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04"X16_F"\n", udphdr->chksum));
388
    LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
389
    /* output to IP */
390
    err = ip_output_if(q, src_ip, &pcb->remote_ip, pcb->ttl, pcb->tos, IP_PROTO_UDP, netif);
391
  }
392
  /* TODO: must this be increased even if error occured? */
393
  snmp_inc_udpoutdatagrams();
394
 
395
  /* did we chain a seperate header pbuf earlier? */
396
  if (q != p) {
397
    /* free the header pbuf */
398
    pbuf_free(q); q = NULL;
399
    /* { p is still referenced by the caller, and will live on } */
400
  }
401
 
402
  UDP_STATS_INC(udp.xmit);
403
  return err;
404
}
405
 
406
/**
407
 * Bind an UDP PCB.
408
 *
409
 * @param pcb UDP PCB to be bound with a local address ipaddr and port.
410
 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
411
 * bind to all local interfaces.
412
 * @param port local UDP port to bind with.
413
 *
414
 * @return lwIP error code.
415
 * - ERR_OK. Successful. No error occured.
416
 * - ERR_USE. The specified ipaddr and port are already bound to by
417
 * another UDP PCB.
418
 *
419
 * @see udp_disconnect()
420
 */
421
err_t
422
udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
423
{
424
  struct udp_pcb *ipcb;
425
  u8_t rebind;
426
 
427
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_bind(ipaddr = "));
428
  ip_addr_debug_print(UDP_DEBUG, ipaddr);
429
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, (", port = %"U16_F")\n", port));
430
 
431
  rebind = 0;
432
  /* Check for double bind and rebind of the same pcb */
433
  for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
434
    /* is this UDP PCB already on active list? */
435
    if (pcb == ipcb) {
436
      /* pcb may occur at most once in active list */
437
      LWIP_ASSERT("rebind == 0", rebind == 0);
438
      /* pcb already in list, just rebind */
439
      rebind = 1;
440
    }
441
 
442
/* this code does not allow upper layer to share a UDP port for
443
   listening to broadcast or multicast traffic (See SO_REUSE_ADDR and
444
   SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR
445
   combine with implementation of UDP PCB flags. Leon Woestenberg. */
446
#ifdef LWIP_UDP_TODO
447
    /* port matches that of PCB in list? */
448
    else if ((ipcb->local_port == port) &&
449
       /* IP address matches, or one is IP_ADDR_ANY? */
450
       (ip_addr_isany(&(ipcb->local_ip)) ||
451
       ip_addr_isany(ipaddr) ||
452
       ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
453
      /* other PCB already binds to this local IP and port */
454
      LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: local port %"U16_F" already bound by another pcb\n", port));
455
      return ERR_USE;
456
    }
457
#endif
458
 
459
  }
460
 
461
  ip_addr_set(&pcb->local_ip, ipaddr);
462
  /* no port specified? */
463
  if (port == 0) {
464
#ifndef UDP_LOCAL_PORT_RANGE_START
465
#define UDP_LOCAL_PORT_RANGE_START 4096
466
#define UDP_LOCAL_PORT_RANGE_END   0x7fff
467
#endif
468
    port = UDP_LOCAL_PORT_RANGE_START;
469
    ipcb = udp_pcbs;
470
    while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
471
      if (ipcb->local_port == port) {
472
        port++;
473
        ipcb = udp_pcbs;
474
      } else
475
        ipcb = ipcb->next;
476
    }
477
    if (ipcb != NULL) {
478
      /* no more ports available in local range */
479
      LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
480
      return ERR_USE;
481
    }
482
  }
483
  pcb->local_port = port;
484
  /* pcb not active yet? */
485
  if (rebind == 0) {
486
    /* place the PCB on the active list if not already there */
487
    pcb->next = udp_pcbs;
488
    udp_pcbs = pcb;
489
  }
490
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE, ("udp_bind: bound to %"U16_F".%"U16_F".%"U16_F".%"U16_F", port %"U16_F"\n",
491
   (u16_t)(ntohl(pcb->local_ip.addr) >> 24 & 0xff),
492
   (u16_t)(ntohl(pcb->local_ip.addr) >> 16 & 0xff),
493
   (u16_t)(ntohl(pcb->local_ip.addr) >> 8 & 0xff),
494
   (u16_t)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));
495
  return ERR_OK;
496
}
497
/**
498
 * Connect an UDP PCB.
499
 *
500
 * This will associate the UDP PCB with the remote address.
501
 *
502
 * @param pcb UDP PCB to be connected with remote address ipaddr and port.
503
 * @param ipaddr remote IP address to connect with.
504
 * @param port remote UDP port to connect with.
505
 *
506
 * @return lwIP error code
507
 *
508
 * @see udp_disconnect()
509
 */
510
err_t
511
udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
512
{
513
  struct udp_pcb *ipcb;
514
 
515
  if (pcb->local_port == 0) {
516
    err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
517
    if (err != ERR_OK)
518
      return err;
519
  }
520
 
521
  ip_addr_set(&pcb->remote_ip, ipaddr);
522
  pcb->remote_port = port;
523
  pcb->flags |= UDP_FLAGS_CONNECTED;
524
/** TODO: this functionality belongs in upper layers */
525
#ifdef LWIP_UDP_TODO
526
  /* Nail down local IP for netconn_addr()/getsockname() */
527
  if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
528
    struct netif *netif;
529
 
530
    if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
531
      LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
532
        UDP_STATS_INC(udp.rterr);
533
      return ERR_RTE;
534
    }
535
    /** TODO: this will bind the udp pcb locally, to the interface which
536
        is used to route output packets to the remote address. However, we
537
        might want to accept incoming packets on any interface! */
538
    pcb->local_ip = netif->ip_addr;
539
  } else if (ip_addr_isany(&pcb->remote_ip)) {
540
    pcb->local_ip.addr = 0;
541
  }
542
#endif
543
  LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE, ("udp_connect: connected to %"U16_F".%"U16_F".%"U16_F".%"U16_F",port %"U16_F"\n",
544
   (u16_t)(ntohl(pcb->remote_ip.addr) >> 24 & 0xff),
545
   (u16_t)(ntohl(pcb->remote_ip.addr) >> 16 & 0xff),
546
   (u16_t)(ntohl(pcb->remote_ip.addr) >> 8 & 0xff),
547
   (u16_t)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));
548
 
549
  /* Insert UDP PCB into the list of active UDP PCBs. */
550
  for(ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
551
    if (pcb == ipcb) {
552
      /* already on the list, just return */
553
      return ERR_OK;
554
    }
555
  }
556
  /* PCB not yet on the list, add PCB now */
557
  pcb->next = udp_pcbs;
558
  udp_pcbs = pcb;
559
  return ERR_OK;
560
}
561
 
562
void
563
udp_disconnect(struct udp_pcb *pcb)
564
{
565
  /* reset remote address association */
566
  ip_addr_set(&pcb->remote_ip, IP_ADDR_ANY);
567
  pcb->remote_port = 0;
568
  /* mark PCB as unconnected */
569
  pcb->flags &= ~UDP_FLAGS_CONNECTED;
570
}
571
 
572
void
573
udp_recv(struct udp_pcb *pcb,
574
   void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,
575
           struct ip_addr *addr, u16_t port),
576
   void *recv_arg)
577
{
578
  /* remember recv() callback and user data */
579
  pcb->recv = recv;
580
  pcb->recv_arg = recv_arg;
581
}
582
/**
583
 * Remove an UDP PCB.
584
 *
585
 * @param pcb UDP PCB to be removed. The PCB is removed from the list of
586
 * UDP PCB's and the data structure is freed from memory.
587
 *
588
 * @see udp_new()
589
 */
590
void
591
udp_remove(struct udp_pcb *pcb)
592
{
593
  struct udp_pcb *pcb2;
594
  /* pcb to be removed is first in list? */
595
  if (udp_pcbs == pcb) {
596
    /* make list start at 2nd pcb */
597
    udp_pcbs = udp_pcbs->next;
598
  /* pcb not 1st in list */
599
  } else for(pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
600
    /* find pcb in udp_pcbs list */
601
    if (pcb2->next != NULL && pcb2->next == pcb) {
602
      /* remove pcb from list */
603
      pcb2->next = pcb->next;
604
    }
605
  }
606
  memp_free(MEMP_UDP_PCB, pcb);
607
}
608
/**
609
 * Create a UDP PCB.
610
 *
611
 * @return The UDP PCB which was created. NULL if the PCB data structure
612
 * could not be allocated.
613
 *
614
 * @see udp_remove()
615
 */
616
struct udp_pcb *
617
udp_new(void) {
618
  struct udp_pcb *pcb;
619
  pcb = memp_malloc(MEMP_UDP_PCB);
620
  /* could allocate UDP PCB? */
621
  if (pcb != NULL) {
622
    /* initialize PCB to all zeroes */
623
    memset(pcb, 0, sizeof(struct udp_pcb));
624
    pcb->ttl = UDP_TTL;
625
  }
626
 
627
 
628
  return pcb;
629
}
630
 
631
#if UDP_DEBUG
632
void
633
udp_debug_print(struct udp_hdr *udphdr)
634
{
635
  LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
636
  LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
637
  LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     %5"U16_F"     | (src port, dest port)\n",
638
         ntohs(udphdr->src), ntohs(udphdr->dest)));
639
  LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
640
  LWIP_DEBUGF(UDP_DEBUG, ("|     %5"U16_F"     |     0x%04"X16_F"    | (len, chksum)\n",
641
         ntohs(udphdr->len), ntohs(udphdr->chksum)));
642
  LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
643
}
644
#endif /* UDP_DEBUG */
645
 
646
#endif /* LWIP_UDP */
647
 
648
 
649
 
650
 
651
 
652
 
653
 
654
 
655
 

powered by: WebSVN 2.1.0

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