| 1 |
/* |
| 2 |
* Copyright (c) 2016 Stephan Linz <[email protected]>, Li-Pro.Net |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Based on examples provided by |
| 6 |
* Iwan Budi Kusnanto <[email protected]> (https://gist.github.com/iwanbk/1399729) |
| 7 |
* Juri Haberland <[email protected]> (https://lists.gnu.org/archive/html/lwip-users/2007-06/msg00078.html) |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without modification, |
| 10 |
* are permitted provided that the following conditions are met: |
| 11 |
* |
| 12 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 13 |
* this list of conditions and the following disclaimer. |
| 14 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 |
* this list of conditions and the following disclaimer in the documentation |
| 16 |
* and/or other materials provided with the distribution. |
| 17 |
* 3. The name of the author may not be used to endorse or promote products |
| 18 |
* derived from this software without specific prior written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 21 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 23 |
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 25 |
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 28 |
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 29 |
* OF SUCH DAMAGE. |
| 30 |
* |
| 31 |
* This file is part of and a contribution to the lwIP TCP/IP stack. |
| 32 |
* |
| 33 |
* Credits go to Adam Dunkels (and the current maintainers) of this software. |
| 34 |
* |
| 35 |
* Stephan Linz rewrote this file to get a basic echo example. |
| 36 |
*/ |
| 37 |
|
| 38 |
/** |
| 39 |
* @file |
| 40 |
* UDP echo server example using raw API. |
| 41 |
* |
| 42 |
* Echos all bytes sent by connecting client, |
| 43 |
* and passively closes when client is done. |
| 44 |
* |
| 45 |
*/ |
| 46 |
|
| 47 |
#include "lwip/opt.h" |
| 48 |
#include "lwip/debug.h" |
| 49 |
#include "lwip/stats.h" |
| 50 |
#include "lwip/udp.h" |
| 51 |
#include "udpecho_raw.h" |
| 52 |
|
| 53 |
#if LWIP_UDP |
| 54 |
|
| 55 |
static struct udp_pcb *udpecho_raw_pcb; |
| 56 |
|
| 57 |
static void |
| 58 |
udpecho_raw_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, |
| 59 |
const ip_addr_t *addr, u16_t port) |
| 60 |
{ |
| 61 |
LWIP_UNUSED_ARG(arg); |
| 62 |
if (p != NULL) { |
| 63 |
/* send received packet back to sender */ |
| 64 |
udp_sendto(upcb, p, addr, port); |
| 65 |
/* free the pbuf */ |
| 66 |
pbuf_free(p); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void |
| 71 |
udpecho_raw_init(void) |
| 72 |
{ |
| 73 |
udpecho_raw_pcb = udp_new_ip_type(IPADDR_TYPE_ANY); |
| 74 |
if (udpecho_raw_pcb != NULL) { |
| 75 |
err_t err; |
| 76 |
|
| 77 |
err = udp_bind(udpecho_raw_pcb, IP_ANY_TYPE, 7); |
| 78 |
if (err == ERR_OK) { |
| 79 |
udp_recv(udpecho_raw_pcb, udpecho_raw_recv, NULL); |
| 80 |
} else { |
| 81 |
/* abort? output diagnostic? */ |
| 82 |
} |
| 83 |
} else { |
| 84 |
/* abort? output diagnostic? */ |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#endif /* LWIP_UDP */ |