AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
udp.c
Go to the documentation of this file.
1
9
10#include <core/system.h>
11#include <core/string.h>
12#include <net/byteorder.h>
13#include <net/inet.h>
14
24void udp_input(struct netif *netif, struct netbuf *nb, const struct ipv4_hdr *ih)
25{
26 struct udp_hdr *uh = (struct udp_hdr *) nb->data;
27 size_t len = nb->len;
28
29 if (len < sizeof(*uh) || ntohs(uh->len) < sizeof(*uh) || ntohs(uh->len) > len ||
30 (uh->checksum && inet_checksum_pseudo(ih->src, ih->dst, IP_PROTO_UDP, uh, ntohs(uh->len)))) {
31 netif->stats.rx_errors++;
32 netbuf_free(nb);
33 return;
34 }
35
36 nb->len = ntohs(uh->len);
37
38 struct inet_sock *sock = inet_sock_find(netif, IP_PROTO_UDP, ih->dst, uh->dst, ih->src, uh->src);
39
40 if (!sock) {
41 /* only for a datagram sent to one of our addresses, not a broadcast */
42 if (ipv4_is_local(ih->dst))
44
45 netif->stats.rx_dropped++;
46 netbuf_free(nb);
47 return;
48 }
49
50 nb->src_ip = ih->src;
51 nb->src_port = uh->src;
52 netbuf_pull(nb, sizeof(*uh));
53
54 inet_sock_deliver(sock, nb);
55}
#define ntohs(x)
Definition byteorder.h:17
struct inet_sock * inet_sock_find(struct netif *netif, uint8_t proto, uint32_t local, uint16_t port, uint32_t remote, uint16_t remote_port)
Finds the socket that a received datagram is for: a socket connected to its sender first,...
Definition socket.c:154
#define IP_PROTO_UDP
Definition inet.h:43
int ipv4_is_local(uint32_t ip)
Says whether an address is one of ours: a loopback address, or an interface's.
Definition ipv4.c:48
void icmp_unreachable(struct netif *netif, const struct ipv4_hdr *ih, int code)
Tells the sender of a datagram that it couldn't get where it was sent.
Definition icmp.c:105
uint16_t inet_checksum_pseudo(uint32_t src, uint32_t dst, uint8_t proto, const void *data, size_t len)
Computes the Internet checksum of data with the IPv4 pseudo-header before it (the addresses,...
Definition ether.c:106
void inet_sock_deliver(struct inet_sock *sock, struct netbuf *nb)
Queues a received datagram on its socket, and wakes up its readers.
Definition socket.c:185
void udp_input(struct netif *netif, struct netbuf *nb, const struct ipv4_hdr *ih)
Takes in a UDP datagram that IPv4 received, and delivers it to its socket.
Definition udp.c:24
#define ICMP_UNREACH_PORT
Definition inet.h:65
static void * netbuf_pull(struct netbuf *nb, size_t size)
Takes the header of size bytes off the front of the packet, and returns it, or NULL if the packet is ...
Definition netbuf.h:64
void netbuf_free(struct netbuf *nb)
Frees a netbuf.
Definition netbuf.c:99
A UDP or ping socket.
Definition socket.c:45
An IPv4 packet's header.
Definition inet.h:46
uint32_t dst
Definition inet.h:56
uint32_t src
Definition inet.h:55
A packet (net/netbuf.c).
Definition netbuf.h:16
uint32_t src_ip
Definition netbuf.h:25
uint8_t * data
Definition netbuf.h:19
uint16_t src_port
Definition netbuf.h:26
size_t len
Definition netbuf.h:20
A network interface (net/netif.c): a device that sends and receives Ethernet frames,...
Definition netif.h:16
struct netif::@020302303320120142126121344055047003105154122231 stats
A UDP datagram's header.
Definition inet.h:77
uint16_t checksum
Definition inet.h:81
uint16_t src
Definition inet.h:78
uint16_t len
Definition inet.h:80
uint16_t dst
Definition inet.h:79