1/*
2 * Copyright (c) 2016 Stephan Linz <linz@li-pro.net>, Li-Pro.Net
3 * All rights reserved.
4 *
5 * Based on examples provided by
6 * Iwan Budi Kusnanto <ibk@labhijau.net> (https://gist.github.com/iwanbk/1399729)
7 * Juri Haberland <juri@sapienti-sat.org> (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#include <stdio.h>
54
55#if LWIP_UDP
56
57static struct udp_pcb *udpecho_raw_pcb;
58
59static void
60udpecho_raw_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p,
61                 const ip_addr_t *addr, u16_t port)
62{
63  LWIP_UNUSED_ARG(arg);
64
65  printf("%s:%u %p\n", __FUNCTION__, __LINE__, p);
66
67  if (p != NULL) {
68    /* send received packet back to sender */
69    udp_sendto(upcb, p, addr, port);
70    /* free the pbuf */
71    pbuf_free(p);
72  }
73}
74
75void
76udpecho_raw_init(void)
77{
78  udpecho_raw_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
79  if (udpecho_raw_pcb != NULL) {
80    err_t err;
81
82    printf("%s:%u\n", __FUNCTION__, __LINE__);
83
84    err = udp_bind(udpecho_raw_pcb, IP_ANY_TYPE, 7);
85    if (err == ERR_OK) {
86      udp_recv(udpecho_raw_pcb, udpecho_raw_recv, NULL);
87      printf("%s:%u\n", __FUNCTION__, __LINE__);
88    } else {
89      /* abort? output diagnostic? */
90    }
91  } else {
92    /* abort? output diagnostic? */
93  }
94}
95
96#endif /* LWIP_UDP */
97