2003-03-25 12:58:22 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* User Datagram Protocol module
|
|
|
|
*
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
/*
|
2003-01-08 10:09:39 +00:00
|
|
|
* Copyright (c) 2001-2003 Swedish Institute of Computer Science.
|
2003-06-09 21:14:47 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
2002-10-19 12:59:30 +00:00
|
|
|
* 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
|
2003-06-09 21:14:47 +00:00
|
|
|
* derived from this software without specific prior written permission.
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
2003-06-09 21:14:47 +00:00
|
|
|
* 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
|
2002-10-19 12:59:30 +00:00
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
2003-06-09 21:14:47 +00:00
|
|
|
*
|
2002-10-19 12:59:30 +00:00
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* udp.c
|
|
|
|
*
|
|
|
|
* The code for the User Datagram Protocol UDP.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
2003-02-21 16:43:46 +00:00
|
|
|
#include "lwip/opt.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
#include "lwip/def.h"
|
|
|
|
#include "lwip/memp.h"
|
|
|
|
#include "lwip/inet.h"
|
|
|
|
#include "lwip/netif.h"
|
|
|
|
#include "lwip/udp.h"
|
|
|
|
#include "lwip/icmp.h"
|
2003-04-01 15:00:26 +00:00
|
|
|
#include "lwip/ip_addr.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
#include "lwip/stats.h"
|
|
|
|
|
|
|
|
#include "arch/perf.h"
|
2003-02-10 21:58:34 +00:00
|
|
|
#include "lwip/snmp.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-03-31 08:00:01 +00:00
|
|
|
/* The list of UDP PCBs */
|
2002-10-19 12:59:30 +00:00
|
|
|
#if LWIP_UDP
|
2003-03-31 08:00:01 +00:00
|
|
|
/* was static, but we may want to access this from a socket layer */
|
|
|
|
struct udp_pcb *udp_pcbs = NULL;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
static struct udp_pcb *pcb_cache = NULL;
|
|
|
|
|
|
|
|
#if UDP_DEBUG
|
|
|
|
int udp_debug_print(struct udp_hdr *udphdr);
|
|
|
|
#endif /* UDP_DEBUG */
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
udp_init(void)
|
|
|
|
{
|
|
|
|
udp_pcbs = pcb_cache = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
/* udp_lookup:
|
|
|
|
*
|
|
|
|
* An experimental feature that will be changed in future versions. Do
|
|
|
|
* not depend on it yet...
|
|
|
|
*/
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
#ifdef LWIP_DEBUG
|
|
|
|
u8_t
|
|
|
|
udp_lookup(struct ip_hdr *iphdr, struct netif *inp)
|
|
|
|
{
|
|
|
|
struct udp_pcb *pcb;
|
|
|
|
struct udp_hdr *udphdr;
|
|
|
|
u16_t src, dest;
|
2003-01-27 13:58:45 +00:00
|
|
|
|
|
|
|
PERF_START;
|
2003-02-06 22:18:56 +00:00
|
|
|
(void)inp;
|
2003-01-27 13:58:45 +00:00
|
|
|
|
|
|
|
udphdr = (struct udp_hdr *)(u8_t *)iphdr + IPH_HL(iphdr) * 4;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-02-21 16:43:46 +00:00
|
|
|
src = ntohs(udphdr->src);
|
|
|
|
dest = ntohs(udphdr->dest);
|
2003-01-27 13:58:45 +00:00
|
|
|
|
|
|
|
pcb = pcb_cache;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pcb != NULL &&
|
2003-01-27 13:58:45 +00:00
|
|
|
pcb->remote_port == src &&
|
|
|
|
pcb->local_port == dest &&
|
|
|
|
(ip_addr_isany(&pcb->remote_ip) ||
|
|
|
|
ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src))) &&
|
|
|
|
(ip_addr_isany(&pcb->local_ip) ||
|
|
|
|
ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)))) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return 1;
|
2003-01-27 13:58:45 +00:00
|
|
|
}
|
2003-06-09 21:14:47 +00:00
|
|
|
else {
|
2002-10-19 12:59:30 +00:00
|
|
|
for(pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pcb->remote_port == src &&
|
2003-06-09 21:14:47 +00:00
|
|
|
pcb->local_port == dest &&
|
|
|
|
(ip_addr_isany(&pcb->remote_ip) ||
|
|
|
|
ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src))) &&
|
|
|
|
(ip_addr_isany(&pcb->local_ip) ||
|
|
|
|
ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)))) {
|
|
|
|
pcb_cache = pcb;
|
2003-01-27 13:58:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pcb == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
for(pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
|
2003-06-09 21:14:47 +00:00
|
|
|
if (pcb->remote_port == 0 &&
|
|
|
|
pcb->local_port == dest &&
|
|
|
|
(ip_addr_isany(&pcb->remote_ip) ||
|
|
|
|
ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src))) &&
|
|
|
|
(ip_addr_isany(&pcb->local_ip) ||
|
|
|
|
ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)))) {
|
|
|
|
break;
|
2003-01-30 15:02:48 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PERF_STOP("udp_lookup");
|
2003-01-27 13:58:45 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pcb != NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return 1;
|
2003-01-27 13:58:45 +00:00
|
|
|
}
|
2003-06-09 21:14:47 +00:00
|
|
|
else {
|
2002-10-19 12:59:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DEBUG */
|
2003-01-27 13:58:45 +00:00
|
|
|
/**
|
|
|
|
* Process an incoming UDP datagram.
|
|
|
|
*
|
|
|
|
* Given an incoming UDP datagram (as a chain of pbufs) this function
|
|
|
|
* finds a corresponding UDP PCB and
|
|
|
|
*
|
|
|
|
* @param pbuf pbuf to be demultiplexed to a UDP PCB.
|
|
|
|
* @param netif network interface on which the datagram was received.
|
|
|
|
*
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
udp_input(struct pbuf *p, struct netif *inp)
|
|
|
|
{
|
2003-06-09 21:14:47 +00:00
|
|
|
struct udp_hdr *udphdr;
|
2002-10-19 12:59:30 +00:00
|
|
|
struct udp_pcb *pcb;
|
|
|
|
struct ip_hdr *iphdr;
|
|
|
|
u16_t src, dest;
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
PERF_START;
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#ifdef UDP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.udp.recv;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* UDP_STATS */
|
|
|
|
|
|
|
|
iphdr = p->payload;
|
|
|
|
|
2003-05-01 08:03:51 +00:00
|
|
|
if (pbuf_header(p, -((s16_t)(UDP_HLEN + IPH_HL(iphdr) * 4)))) {
|
|
|
|
/* drop short packets */
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_input: short UDP datagram (%u bytes) discarded\n", p->tot_len));
|
2003-05-01 08:03:51 +00:00
|
|
|
#ifdef UDP_STATS
|
2003-06-09 21:14:47 +00:00
|
|
|
++lwip_stats.udp.lenerr;
|
|
|
|
++lwip_stats.udp.drop;
|
2003-05-01 08:03:51 +00:00
|
|
|
#endif /* UDP_STATS */
|
|
|
|
snmp_inc_udpinerrors();
|
2003-06-09 21:14:47 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
goto end;
|
2003-05-01 08:03:51 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
udphdr = (struct udp_hdr *)((u8_t *)p->payload - UDP_HLEN);
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_input: received datagram of length %u\n", p->tot_len));
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2003-02-21 16:43:46 +00:00
|
|
|
src = ntohs(udphdr->src);
|
|
|
|
dest = ntohs(udphdr->dest);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
#if UDP_DEBUG
|
|
|
|
udp_debug_print(udphdr);
|
|
|
|
#endif /* UDP_DEBUG */
|
2003-01-17 15:16:33 +00:00
|
|
|
|
2003-01-30 15:02:48 +00:00
|
|
|
/* print the UDP source and destination */
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp (%u.%u.%u.%u, %u) <-- (%u.%u.%u.%u, %u)\n",
|
2003-01-30 15:02:48 +00:00
|
|
|
ip4_addr1(&iphdr->dest), ip4_addr2(&iphdr->dest),
|
|
|
|
ip4_addr3(&iphdr->dest), ip4_addr4(&iphdr->dest), ntohs(udphdr->dest),
|
|
|
|
ip4_addr1(&iphdr->src), ip4_addr2(&iphdr->src),
|
|
|
|
ip4_addr3(&iphdr->src), ip4_addr4(&iphdr->src), ntohs(udphdr->src)));
|
2003-01-17 15:16:33 +00:00
|
|
|
/* Iterate through the UDP pcb list for a fully matching pcb */
|
2002-10-19 12:59:30 +00:00
|
|
|
for(pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
|
2003-01-30 15:02:48 +00:00
|
|
|
/* print the PCB local and remote address */
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("pcb (%u.%u.%u.%u, %u) --- (%u.%u.%u.%u, %u)\n",
|
2003-01-17 15:16:33 +00:00
|
|
|
ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
|
2003-01-30 15:02:48 +00:00
|
|
|
ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
|
|
|
|
ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
|
|
|
|
ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
|
2003-01-17 15:16:33 +00:00
|
|
|
|
2003-01-30 15:02:48 +00:00
|
|
|
/* PCB remote port matches UDP source port? */
|
2003-05-01 13:24:01 +00:00
|
|
|
if ((pcb->remote_port == src) &&
|
2003-01-30 15:02:48 +00:00
|
|
|
/* PCB local port matches UDP destination port? */
|
|
|
|
(pcb->local_port == dest) &&
|
|
|
|
/* accepting from any remote (source) IP address? or... */
|
2002-10-19 12:59:30 +00:00
|
|
|
(ip_addr_isany(&pcb->remote_ip) ||
|
2003-01-30 15:02:48 +00:00
|
|
|
/* PCB remote IP address matches UDP source IP address? */
|
2003-06-09 21:14:47 +00:00
|
|
|
ip_addr_cmp(&(pcb->remote_ip), &(iphdr->src))) &&
|
2003-01-30 15:02:48 +00:00
|
|
|
/* accepting on any local (netif) IP address? or... */
|
2002-10-19 12:59:30 +00:00
|
|
|
(ip_addr_isany(&pcb->local_ip) ||
|
2003-01-30 15:02:48 +00:00
|
|
|
/* PCB local IP address matches UDP destination IP address? */
|
2003-06-09 21:14:47 +00:00
|
|
|
ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)))) {
|
2002-10-19 12:59:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-01-22 16:18:05 +00:00
|
|
|
/* no fully matching pcb found? then look for an unconnected pcb */
|
2003-01-30 15:02:48 +00:00
|
|
|
if (pcb == NULL) {
|
|
|
|
/* Iterate through the UDP PCB list for a pcb that matches
|
2003-01-17 15:16:33 +00:00
|
|
|
the local address. */
|
2002-10-19 12:59:30 +00:00
|
|
|
for(pcb = udp_pcbs; pcb != NULL; pcb = pcb->next) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("pcb (%u.%u.%u.%u, %u) --- (%u.%u.%u.%u, %u)\n",
|
2003-01-30 15:02:48 +00:00
|
|
|
ip4_addr1(&pcb->local_ip), ip4_addr2(&pcb->local_ip),
|
|
|
|
ip4_addr3(&pcb->local_ip), ip4_addr4(&pcb->local_ip), pcb->local_port,
|
|
|
|
ip4_addr1(&pcb->remote_ip), ip4_addr2(&pcb->remote_ip),
|
|
|
|
ip4_addr3(&pcb->remote_ip), ip4_addr4(&pcb->remote_ip), pcb->remote_port));
|
2003-01-27 08:50:28 +00:00
|
|
|
/* unconnected? */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (((pcb->flags & UDP_FLAGS_CONNECTED) == 0) &&
|
2003-06-09 21:14:47 +00:00
|
|
|
/* destination port matches? */
|
|
|
|
(pcb->local_port == dest) &&
|
|
|
|
/* not bound to a specific (local) interface address? or... */
|
|
|
|
(ip_addr_isany(&pcb->local_ip) ||
|
|
|
|
/* ...matching interface address? */
|
|
|
|
ip_addr_cmp(&(pcb->local_ip), &(iphdr->dest)))) {
|
|
|
|
break;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check checksum if this is a match or if it was directed at us. */
|
2003-06-09 21:14:47 +00:00
|
|
|
if (pcb != NULL || ip_addr_cmp(&inp->ip_addr, &iphdr->dest))
|
2003-01-23 16:46:01 +00:00
|
|
|
{
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: calculating checksum\n"));
|
2003-06-09 21:14:47 +00:00
|
|
|
pbuf_header(p, UDP_HLEN);
|
2002-10-19 12:59:30 +00:00
|
|
|
#ifdef IPv6
|
2003-06-09 21:14:47 +00:00
|
|
|
if (iphdr->nexthdr == IP_PROTO_UDPLITE) {
|
2002-10-19 12:59:30 +00:00
|
|
|
#else
|
2003-06-09 21:14:47 +00:00
|
|
|
if (IPH_PROTO(iphdr) == IP_PROTO_UDPLITE) {
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* IPv4 */
|
|
|
|
/* Do the UDP Lite checksum */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
|
2003-06-09 21:14:47 +00:00
|
|
|
(struct ip_addr *)&(iphdr->dest),
|
|
|
|
IP_PROTO_UDPLITE, ntohs(udphdr->len)) != 0) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | 2, ("udp_input: UDP Lite datagram discarded due to failing checksum\n"));
|
2002-10-19 12:59:30 +00:00
|
|
|
#ifdef UDP_STATS
|
2003-06-09 21:14:47 +00:00
|
|
|
++lwip_stats.udp.chkerr;
|
|
|
|
++lwip_stats.udp.drop;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* UDP_STATS */
|
2003-02-10 13:47:47 +00:00
|
|
|
snmp_inc_udpinerrors();
|
2003-06-09 21:14:47 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
goto end;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2003-05-01 13:24:01 +00:00
|
|
|
if (udphdr->chksum != 0) {
|
2003-06-09 21:14:47 +00:00
|
|
|
if (inet_chksum_pseudo(p, (struct ip_addr *)&(iphdr->src),
|
|
|
|
(struct ip_addr *)&(iphdr->dest),
|
|
|
|
IP_PROTO_UDP, p->tot_len) != 0) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | 2, ("udp_input: UDP datagram discarded due to failing checksum\n"));
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#ifdef UDP_STATS
|
2003-06-09 21:14:47 +00:00
|
|
|
++lwip_stats.udp.chkerr;
|
|
|
|
++lwip_stats.udp.drop;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* UDP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
snmp_inc_udpinerrors();
|
2003-06-09 21:14:47 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
goto end;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2003-06-09 21:14:47 +00:00
|
|
|
pbuf_header(p, -UDP_HLEN);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pcb != NULL) {
|
2002-11-21 10:32:19 +00:00
|
|
|
snmp_inc_udpindatagrams();
|
2003-02-10 13:47:47 +00:00
|
|
|
pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src), src);
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE, ("udp_input: not for us.\n"));
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/* No match was found, send ICMP destination port unreachable unless
|
2003-06-09 21:14:47 +00:00
|
|
|
destination address was broadcast/multicast. */
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (!ip_addr_isbroadcast(&iphdr->dest, &inp->netmask) &&
|
2003-06-09 21:14:47 +00:00
|
|
|
!ip_addr_ismulticast(&iphdr->dest)) {
|
|
|
|
|
|
|
|
/* adjust pbuf pointer */
|
|
|
|
p->payload = iphdr;
|
|
|
|
icmp_dest_unreach(p, ICMP_DUR_PORT);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
#ifdef UDP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.udp.proterr;
|
|
|
|
++lwip_stats.udp.drop;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* UDP_STATS */
|
2002-11-21 10:32:19 +00:00
|
|
|
snmp_inc_udpnoports();
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pbuf_free(p);
|
|
|
|
}
|
|
|
|
end:
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
PERF_STOP("udp_input");
|
|
|
|
}
|
2003-01-27 13:58:45 +00:00
|
|
|
/**
|
2003-06-09 21:14:47 +00:00
|
|
|
* Send data using UDP.
|
2003-01-27 13:58:45 +00:00
|
|
|
*
|
|
|
|
* @param pcb UDP PCB used to send the data.
|
|
|
|
* @param pbuf chain of pbuf's to be sent.
|
|
|
|
*
|
|
|
|
* @return lwIP error code.
|
|
|
|
* - ERR_OK. Successful. No error occured.
|
|
|
|
* - ERR_MEM. Out of memory.
|
|
|
|
* - ERR_USE. The specified ipaddr and port are already bound to by
|
|
|
|
* another UDP PCB.
|
2003-06-09 21:14:47 +00:00
|
|
|
*
|
2003-01-27 13:58:45 +00:00
|
|
|
* @see udp_disconnect()
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
udp_send(struct udp_pcb *pcb, struct pbuf *p)
|
|
|
|
{
|
|
|
|
struct udp_hdr *udphdr;
|
|
|
|
struct netif *netif;
|
|
|
|
struct ip_addr *src_ip;
|
|
|
|
err_t err;
|
2003-03-25 12:58:22 +00:00
|
|
|
struct pbuf *q; /* q will be sent down the stack */
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_send\n"));
|
2003-02-06 22:18:56 +00:00
|
|
|
|
2003-04-16 07:39:48 +00:00
|
|
|
/* if the PCB is not yet bound to a port, bind it here */
|
2003-03-31 08:00:01 +00:00
|
|
|
if (pcb->local_port == 0) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: not yet bound to a port, binding now\n"));
|
2003-02-06 22:18:56 +00:00
|
|
|
err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
|
2003-03-31 08:00:01 +00:00
|
|
|
if (err != ERR_OK) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: forced port bind failed\n"));
|
2003-02-06 22:18:56 +00:00
|
|
|
return err;
|
2003-03-31 08:00:01 +00:00
|
|
|
}
|
2003-02-06 22:18:56 +00:00
|
|
|
}
|
|
|
|
|
2003-03-24 12:57:00 +00:00
|
|
|
/* not enough space to add an UDP header to first pbuf in given p chain? */
|
2003-03-25 12:58:22 +00:00
|
|
|
if (pbuf_header(p, UDP_HLEN)) {
|
2003-01-27 13:58:45 +00:00
|
|
|
/* allocate header in new pbuf */
|
2003-03-25 12:58:22 +00:00
|
|
|
q = pbuf_alloc(PBUF_IP, UDP_HLEN, PBUF_RAM);
|
2003-01-27 13:58:45 +00:00
|
|
|
/* new header pbuf could not be allocated? */
|
2003-03-25 12:58:22 +00:00
|
|
|
if (q == NULL) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 2, ("udp_send: could not allocate header\n"));
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2003-03-25 12:58:22 +00:00
|
|
|
/* chain header q in front of given pbuf p */
|
|
|
|
pbuf_chain(q, p);
|
|
|
|
/* { first pbuf q points to header pbuf } */
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
|
2003-04-16 07:39:48 +00:00
|
|
|
/* adding a header within p succeeded */
|
2003-06-09 21:14:47 +00:00
|
|
|
} else {
|
2003-03-25 12:58:22 +00:00
|
|
|
/* first pbuf q equals given pbuf */
|
|
|
|
q = p;
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: added header in given pbuf %p\n", (void *)p));
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
2003-03-25 12:58:22 +00:00
|
|
|
udphdr = q->payload;
|
2002-10-19 12:59:30 +00:00
|
|
|
udphdr->src = htons(pcb->local_port);
|
|
|
|
udphdr->dest = htons(pcb->remote_port);
|
|
|
|
udphdr->chksum = 0x0000;
|
|
|
|
|
2003-04-16 07:39:48 +00:00
|
|
|
if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | 1, ("udp_send: No route to 0x%lx\n", pcb->remote_ip.addr));
|
2002-10-19 12:59:30 +00:00
|
|
|
#ifdef UDP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.udp.rterr;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* UDP_STATS */
|
|
|
|
return ERR_RTE;
|
|
|
|
}
|
2003-01-27 13:58:45 +00:00
|
|
|
/* using IP_ANY_ADDR? */
|
2003-04-16 07:39:48 +00:00
|
|
|
if (ip_addr_isany(&pcb->local_ip)) {
|
|
|
|
/* use outgoing network interface IP address as source address */
|
2002-10-19 12:59:30 +00:00
|
|
|
src_ip = &(netif->ip_addr);
|
|
|
|
} else {
|
2003-01-27 13:58:45 +00:00
|
|
|
/* use UDP PCB local IP address as source address */
|
2002-10-19 12:59:30 +00:00
|
|
|
src_ip = &(pcb->local_ip);
|
|
|
|
}
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: sending datagram of length %u\n", q->tot_len));
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2003-01-27 13:58:45 +00:00
|
|
|
/* UDP Lite protocol? */
|
2003-04-16 07:39:48 +00:00
|
|
|
if (pcb->flags & UDP_FLAGS_UDPLITE) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP LITE packet length %u\n", q->tot_len));
|
2003-01-13 13:22:09 +00:00
|
|
|
/* set UDP message length in UDP header */
|
2002-10-19 12:59:30 +00:00
|
|
|
udphdr->len = htons(pcb->chksum_len);
|
|
|
|
/* calculate checksum */
|
2003-03-25 12:58:22 +00:00
|
|
|
udphdr->chksum = inet_chksum_pseudo(q, src_ip, &(pcb->remote_ip),
|
2003-06-09 21:14:47 +00:00
|
|
|
IP_PROTO_UDP, pcb->chksum_len);
|
2003-01-27 13:58:45 +00:00
|
|
|
/* chksum zero must become 0xffff, as zero means 'no checksum' */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
|
2003-01-27 13:58:45 +00:00
|
|
|
/* output to IP */
|
2003-06-09 21:14:47 +00:00
|
|
|
err = ip_output_if (q, src_ip, &pcb->remote_ip, UDP_TTL, IP_PROTO_UDPLITE, netif);
|
2002-11-21 10:32:19 +00:00
|
|
|
snmp_inc_udpoutdatagrams();
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP packet length %u\n", q->tot_len));
|
2003-03-25 12:58:22 +00:00
|
|
|
udphdr->len = htons(q->tot_len);
|
2002-10-19 12:59:30 +00:00
|
|
|
/* calculate checksum */
|
2003-04-16 07:39:48 +00:00
|
|
|
if ((pcb->flags & UDP_FLAGS_NOCHKSUM) == 0) {
|
2003-03-25 12:58:22 +00:00
|
|
|
udphdr->chksum = inet_chksum_pseudo(q, src_ip, &pcb->remote_ip, IP_PROTO_UDP, q->tot_len);
|
2003-01-27 13:58:45 +00:00
|
|
|
/* chksum zero must become 0xffff, as zero means 'no checksum' */
|
2003-04-16 07:39:48 +00:00
|
|
|
if (udphdr->chksum == 0x0000) udphdr->chksum = 0xffff;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: UDP checksum 0x%04x\n", udphdr->chksum));
|
2002-11-21 10:32:19 +00:00
|
|
|
snmp_inc_udpoutdatagrams();
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_send: ip_output_if (,,,,IP_PROTO_UDP,)\n"));
|
2003-01-27 13:58:45 +00:00
|
|
|
/* output to IP */
|
2003-06-09 21:14:47 +00:00
|
|
|
err = ip_output_if (q, src_ip, &pcb->remote_ip, UDP_TTL, IP_PROTO_UDP, netif);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-03-25 12:58:22 +00:00
|
|
|
|
2003-04-01 14:02:50 +00:00
|
|
|
/* did we chain a header earlier? */
|
2003-03-25 12:58:22 +00:00
|
|
|
if (q != p) {
|
2003-03-30 00:39:03 +00:00
|
|
|
/* free the header */
|
|
|
|
pbuf_free(q);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-06-09 21:14:47 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#ifdef UDP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.udp.xmit;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* UDP_STATS */
|
|
|
|
return err;
|
|
|
|
}
|
2003-03-25 12:58:22 +00:00
|
|
|
|
2003-01-27 13:58:45 +00:00
|
|
|
/**
|
|
|
|
* Bind an UDP PCB.
|
|
|
|
*
|
|
|
|
* @param pcb UDP PCB to be bound with a local address ipaddr and port.
|
|
|
|
* @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
|
|
|
|
* bind to all local interfaces.
|
|
|
|
* @param port local UDP port to bind with.
|
|
|
|
*
|
|
|
|
* @return lwIP error code.
|
|
|
|
* - ERR_OK. Successful. No error occured.
|
|
|
|
* - ERR_USE. The specified ipaddr and port are already bound to by
|
|
|
|
* another UDP PCB.
|
2003-06-09 21:14:47 +00:00
|
|
|
*
|
2003-01-27 13:58:45 +00:00
|
|
|
* @see udp_disconnect()
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
udp_bind(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
|
|
|
|
{
|
|
|
|
struct udp_pcb *ipcb;
|
2003-02-14 15:49:02 +00:00
|
|
|
u8_t rebind;
|
2003-06-19 11:15:39 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, ("udp_bind(ipaddr = "));
|
|
|
|
ip_addr_debug_print(UDP_DEBUG, ipaddr);
|
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | 3, (", port = %u)\n", port));
|
2003-02-14 15:49:02 +00:00
|
|
|
rebind = 0;
|
2003-01-22 16:18:05 +00:00
|
|
|
/* Check for double bind and rebind of the same pcb */
|
2003-04-16 07:39:48 +00:00
|
|
|
for (ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
|
2003-06-09 21:14:47 +00:00
|
|
|
/* is this UDP PCB already on active list? */
|
2003-01-22 16:18:05 +00:00
|
|
|
if (pcb == ipcb) {
|
2003-04-01 14:52:41 +00:00
|
|
|
/* pcb may occur at most once in active list */
|
|
|
|
LWIP_ASSERT("rebind == 0", rebind == 0);
|
|
|
|
/* pcb already in list, just rebind */
|
2003-06-09 21:14:47 +00:00
|
|
|
rebind = 1;
|
2003-01-27 13:58:45 +00:00
|
|
|
}
|
2003-02-14 15:49:02 +00:00
|
|
|
/* this code does not allow upper layer to share a UDP port for
|
|
|
|
listening to broadcast or multicast traffic (See SO_REUSE_ADDR and
|
|
|
|
SO_REUSE_PORT under *BSD). TODO: See where it fits instead, OR
|
|
|
|
combine with implementation of UDP PCB flags. Leon Woestenberg. */
|
2003-06-09 21:14:47 +00:00
|
|
|
#if 0
|
2003-01-27 13:58:45 +00:00
|
|
|
/* port matches that of PCB in list? */
|
2003-02-14 15:49:02 +00:00
|
|
|
else if ((ipcb->local_port == port) &&
|
2003-01-27 13:58:45 +00:00
|
|
|
/* IP address matches, or one is IP_ADDR_ANY? */
|
2003-02-14 15:49:02 +00:00
|
|
|
(ip_addr_isany(&(ipcb->local_ip)) ||
|
2003-06-09 21:14:47 +00:00
|
|
|
ip_addr_isany(ipaddr) ||
|
|
|
|
ip_addr_cmp(&(ipcb->local_ip), ipaddr))) {
|
2003-01-27 13:58:45 +00:00
|
|
|
/* other PCB already binds to this local IP and port */
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: local port %u already bound by another pcb\n", port));
|
2003-06-09 21:14:47 +00:00
|
|
|
return ERR_USE;
|
2003-01-21 14:09:31 +00:00
|
|
|
}
|
2003-02-14 15:49:02 +00:00
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-01-27 13:58:45 +00:00
|
|
|
/* bind local address */
|
2003-01-21 14:09:31 +00:00
|
|
|
ip_addr_set(&pcb->local_ip, ipaddr);
|
2003-04-01 14:52:41 +00:00
|
|
|
/* no port specified? */
|
2003-02-14 15:49:02 +00:00
|
|
|
if (port == 0) {
|
2003-02-06 22:18:56 +00:00
|
|
|
#ifndef UDP_LOCAL_PORT_RANGE_START
|
|
|
|
#define UDP_LOCAL_PORT_RANGE_START 4096
|
|
|
|
#define UDP_LOCAL_PORT_RANGE_END 0x7fff
|
|
|
|
#endif
|
2003-03-19 16:04:54 +00:00
|
|
|
port = UDP_LOCAL_PORT_RANGE_START;
|
|
|
|
ipcb = udp_pcbs;
|
2003-04-01 14:52:41 +00:00
|
|
|
while ((ipcb != NULL) && (port != UDP_LOCAL_PORT_RANGE_END)) {
|
|
|
|
if (ipcb->local_port == port) {
|
2003-03-19 16:04:54 +00:00
|
|
|
port++;
|
|
|
|
ipcb = udp_pcbs;
|
|
|
|
} else
|
2003-06-09 21:14:47 +00:00
|
|
|
ipcb = ipcb->next;
|
2003-03-19 16:04:54 +00:00
|
|
|
}
|
2003-04-01 14:52:41 +00:00
|
|
|
if (ipcb != NULL) {
|
2003-03-19 16:04:54 +00:00
|
|
|
/* no more ports available in local range */
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: out of free UDP ports\n"));
|
2003-03-19 16:04:54 +00:00
|
|
|
return ERR_USE;
|
2003-06-09 21:14:47 +00:00
|
|
|
}
|
2003-02-06 22:18:56 +00:00
|
|
|
}
|
2003-01-21 14:09:31 +00:00
|
|
|
pcb->local_port = port;
|
2003-04-01 14:52:41 +00:00
|
|
|
/* pcb not active yet? */
|
2003-01-22 16:18:05 +00:00
|
|
|
if (rebind == 0) {
|
2003-04-01 14:52:41 +00:00
|
|
|
/* place the PCB on the active list if not already there */
|
2003-01-22 16:18:05 +00:00
|
|
|
pcb->next = udp_pcbs;
|
|
|
|
udp_pcbs = pcb;
|
2003-06-09 21:14:47 +00:00
|
|
|
}
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE, ("udp_bind: bound to %u.%u.%u.%u, port %u\n",
|
2003-06-11 22:11:42 +00:00
|
|
|
(unsigned int)(ntohl(pcb->local_ip.addr) >> 24 & 0xff),
|
|
|
|
(unsigned int)(ntohl(pcb->local_ip.addr) >> 16 & 0xff),
|
|
|
|
(unsigned int)(ntohl(pcb->local_ip.addr) >> 8 & 0xff),
|
|
|
|
(unsigned int)(ntohl(pcb->local_ip.addr) & 0xff), pcb->local_port));
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2003-01-27 13:58:45 +00:00
|
|
|
/**
|
|
|
|
* Connect an UDP PCB.
|
|
|
|
*
|
2003-02-14 15:49:02 +00:00
|
|
|
* This will associate the UDP PCB with the remote address.
|
|
|
|
*
|
2003-01-27 13:58:45 +00:00
|
|
|
* @param pcb UDP PCB to be connected with remote address ipaddr and port.
|
|
|
|
* @param ipaddr remote IP address to connect with.
|
|
|
|
* @param port remote UDP port to connect with.
|
|
|
|
*
|
|
|
|
* @return lwIP error code
|
2003-06-09 21:14:47 +00:00
|
|
|
*
|
2003-01-27 13:58:45 +00:00
|
|
|
* @see udp_disconnect()
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
udp_connect(struct udp_pcb *pcb, struct ip_addr *ipaddr, u16_t port)
|
|
|
|
{
|
|
|
|
struct udp_pcb *ipcb;
|
2003-02-06 22:18:56 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pcb->local_port == 0) {
|
2003-02-10 13:47:47 +00:00
|
|
|
err_t err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (err != ERR_OK)
|
2003-02-10 13:47:47 +00:00
|
|
|
return err;
|
2003-02-06 22:18:56 +00:00
|
|
|
}
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
ip_addr_set(&pcb->remote_ip, ipaddr);
|
|
|
|
pcb->remote_port = port;
|
2003-05-20 11:23:59 +00:00
|
|
|
pcb->flags |= UDP_FLAGS_CONNECTED;
|
2003-02-14 15:49:02 +00:00
|
|
|
/** TODO: this functionality belongs in upper layers */
|
|
|
|
#if 0
|
2003-02-06 22:18:56 +00:00
|
|
|
/* Nail down local IP for netconn_addr()/getsockname() */
|
2003-06-09 21:14:47 +00:00
|
|
|
if (ip_addr_isany(&pcb->local_ip) && !ip_addr_isany(&pcb->remote_ip)) {
|
2003-02-06 22:18:56 +00:00
|
|
|
struct netif *netif;
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if ((netif = ip_route(&(pcb->remote_ip))) == NULL) {
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("udp_connect: No route to 0x%lx\n", pcb->remote_ip.addr));
|
2003-02-06 22:18:56 +00:00
|
|
|
#ifdef UDP_STATS
|
2003-02-10 13:47:47 +00:00
|
|
|
++lwip_stats.udp.rterr;
|
2003-02-06 22:18:56 +00:00
|
|
|
#endif /* UDP_STATS */
|
2003-06-09 21:14:47 +00:00
|
|
|
return ERR_RTE;
|
2003-02-06 22:18:56 +00:00
|
|
|
}
|
2003-02-14 15:49:02 +00:00
|
|
|
/** TODO: this will bind the udp pcb locally, to the interface which
|
|
|
|
is used to route output packets to the remote address. However, we
|
|
|
|
might want to accept incoming packets on any interface! */
|
2003-02-06 22:18:56 +00:00
|
|
|
pcb->local_ip = netif->ip_addr;
|
2003-06-09 21:14:47 +00:00
|
|
|
} else if (ip_addr_isany(&pcb->remote_ip)) {
|
2003-02-06 22:18:56 +00:00
|
|
|
pcb->local_ip.addr = 0;
|
|
|
|
}
|
2003-02-10 13:47:47 +00:00
|
|
|
#endif
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG | DBG_TRACE | DBG_STATE, ("udp_connect: connected to %u.%u.%u.%u, port %u\n",
|
2003-06-11 22:11:42 +00:00
|
|
|
(unsigned int)(ntohl(pcb->remote_ip.addr) >> 24 & 0xff),
|
|
|
|
(unsigned int)(ntohl(pcb->remote_ip.addr) >> 16 & 0xff),
|
|
|
|
(unsigned int)(ntohl(pcb->remote_ip.addr) >> 8 & 0xff),
|
|
|
|
(unsigned int)(ntohl(pcb->remote_ip.addr) & 0xff), pcb->remote_port));
|
2003-04-04 08:14:49 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/* Insert UDP PCB into the list of active UDP PCBs. */
|
|
|
|
for(ipcb = udp_pcbs; ipcb != NULL; ipcb = ipcb->next) {
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pcb == ipcb) {
|
2003-01-27 13:58:45 +00:00
|
|
|
/* already on the list, just return */
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
}
|
2003-01-27 13:58:45 +00:00
|
|
|
/* PCB not yet on the list, add PCB now */
|
2002-10-19 12:59:30 +00:00
|
|
|
pcb->next = udp_pcbs;
|
|
|
|
udp_pcbs = pcb;
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
2003-01-22 16:18:05 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
udp_disconnect(struct udp_pcb *pcb)
|
|
|
|
{
|
2003-06-09 21:14:47 +00:00
|
|
|
pcb->flags &= ~UDP_FLAGS_CONNECTED;
|
2003-01-22 16:18:05 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
udp_recv(struct udp_pcb *pcb,
|
2003-06-09 21:14:47 +00:00
|
|
|
void (* recv)(void *arg, struct udp_pcb *upcb, struct pbuf *p,
|
|
|
|
struct ip_addr *addr, u16_t port),
|
|
|
|
void *recv_arg)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2003-01-27 13:58:45 +00:00
|
|
|
/* remember recv() callback and user data */
|
2002-10-19 12:59:30 +00:00
|
|
|
pcb->recv = recv;
|
|
|
|
pcb->recv_arg = recv_arg;
|
|
|
|
}
|
2003-01-27 13:58:45 +00:00
|
|
|
/**
|
|
|
|
* Remove an UDP PCB.
|
|
|
|
*
|
|
|
|
* @param pcb UDP PCB to be removed. The PCB is removed from the list of
|
|
|
|
* UDP PCB's and the data structure is freed from memory.
|
2003-06-09 21:14:47 +00:00
|
|
|
*
|
2003-01-27 13:58:45 +00:00
|
|
|
* @see udp_new()
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
udp_remove(struct udp_pcb *pcb)
|
|
|
|
{
|
|
|
|
struct udp_pcb *pcb2;
|
2003-01-27 13:58:45 +00:00
|
|
|
/* pcb to be removed is first in list? */
|
|
|
|
if (udp_pcbs == pcb) {
|
|
|
|
/* make list start at 2nd pcb */
|
2002-10-19 12:59:30 +00:00
|
|
|
udp_pcbs = udp_pcbs->next;
|
2003-01-27 13:58:45 +00:00
|
|
|
/* pcb not 1st in list */
|
2002-10-19 12:59:30 +00:00
|
|
|
} else for(pcb2 = udp_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
|
2003-01-27 13:58:45 +00:00
|
|
|
/* find pcb in udp_pcbs list */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pcb2->next != NULL && pcb2->next == pcb) {
|
2003-01-27 13:58:45 +00:00
|
|
|
/* remove pcb from list */
|
2002-10-19 12:59:30 +00:00
|
|
|
pcb2->next = pcb->next;
|
|
|
|
}
|
|
|
|
}
|
2003-06-09 21:14:47 +00:00
|
|
|
memp_free(MEMP_UDP_PCB, pcb);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-01-27 13:58:45 +00:00
|
|
|
/**
|
|
|
|
* Create a UDP PCB.
|
|
|
|
*
|
|
|
|
* @return The UDP PCB which was created. NULL if the PCB data structure
|
|
|
|
* could not be allocated.
|
2003-06-09 21:14:47 +00:00
|
|
|
*
|
2003-01-27 13:58:45 +00:00
|
|
|
* @see udp_remove()
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
struct udp_pcb *
|
|
|
|
udp_new(void) {
|
|
|
|
struct udp_pcb *pcb;
|
|
|
|
pcb = memp_malloc(MEMP_UDP_PCB);
|
2003-01-27 13:58:45 +00:00
|
|
|
/* could allocate UDP PCB? */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (pcb != NULL) {
|
2003-01-27 13:58:45 +00:00
|
|
|
/* initialize PCB to all zeroes */
|
2002-12-18 10:40:01 +00:00
|
|
|
memset(pcb, 0, sizeof(struct udp_pcb));
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-01-27 13:58:45 +00:00
|
|
|
return pcb;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
#if UDP_DEBUG
|
|
|
|
int
|
|
|
|
udp_debug_print(struct udp_hdr *udphdr)
|
|
|
|
{
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("UDP header:\n"));
|
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
|
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("| %5u | %5u | (src port, dest port)\n",
|
2003-06-09 21:14:47 +00:00
|
|
|
ntohs(udphdr->src), ntohs(udphdr->dest)));
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
|
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("| %5u | 0x%04x | (len, chksum)\n",
|
2003-06-09 21:14:47 +00:00
|
|
|
ntohs(udphdr->len), ntohs(udphdr->chksum)));
|
2003-06-10 10:45:29 +00:00
|
|
|
LWIP_DEBUGF(UDP_DEBUG, ("+-------------------------------+\n"));
|
2002-10-19 12:59:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* UDP_DEBUG */
|
|
|
|
/*-----------------------------------------------------------------------------------*/
|
|
|
|
#endif /* LWIP_UDP */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|