Semi-decennial update. 50% code inflation.
[cbaos.git] / net / udp.c
blobd5d3c389b23deec194add36873b0ee8ee339d9c4
1 /* Author: Domen Puncer Kugler <domen@cba.si>. License: WTFPL, see file LICENSE */
3 #include <list.h>
4 #include <net/ip.h>
5 #include <net/udp.h>
6 #include <helpers.h>
9 //#define udp_print_array print_array
10 #define udp_print_array(tag, buf, size)
13 static LIST_DECLARE_INIT(udp_handler_list);
16 static int udp_handle(u8 *buf, unsigned len)
18 struct udp_packet *udp = (struct udp_packet *)buf;
19 int l = get_be16(udp->len);
20 int port = get_be16(udp->dport);
22 if (len > l)
23 len = l;
25 udp_print_array("UDX RX", buf, sizeof(*udp));
26 /* no data? */
27 if (len <= sizeof(*udp))
28 return 0;
30 /* skip ports, len, checksum for now */
31 len -= sizeof(*udp);
33 udp_print_array("UDX RX data", udp->payload, len);
35 struct list *it;
36 list_for_each(&udp_handler_list, it) {
37 struct udp_handler *han = list_entry(it, struct udp_handler, list);
39 if (port == han->port)
40 return han->handler(udp->payload, len);
43 return -1;
46 static struct ip_handler ip_handler_udp = {
47 .list = LIST_INIT(ip_handler_udp.list),
48 .protocol = IP_PROT_UDP,
49 .handler = udp_handle,
52 int udp_init(void)
54 return ip_register_handler(&ip_handler_udp);
57 int udp_register_handler(struct udp_handler *han)
59 list_add_tail(&udp_handler_list, &han->list);
60 return 0;