AquilaOS documentation
How it works, and its code
Toggle main menu visibility
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
24
void
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
))
43
icmp_unreachable
(
netif
, ih,
ICMP_UNREACH_PORT
);
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
}
byteorder.h
ntohs
#define ntohs(x)
Definition
byteorder.h:17
system.h
inet.h
inet_sock_find
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
IP_PROTO_UDP
#define IP_PROTO_UDP
Definition
inet.h:43
ipv4_is_local
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
icmp_unreachable
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
inet_checksum_pseudo
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
inet_sock_deliver
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
udp_input
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
ICMP_UNREACH_PORT
#define ICMP_UNREACH_PORT
Definition
inet.h:65
netbuf_pull
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
netbuf_free
void netbuf_free(struct netbuf *nb)
Frees a netbuf.
Definition
netbuf.c:99
string.h
inet_sock
A UDP or ping socket.
Definition
socket.c:45
ipv4_hdr
An IPv4 packet's header.
Definition
inet.h:46
ipv4_hdr::dst
uint32_t dst
Definition
inet.h:56
ipv4_hdr::src
uint32_t src
Definition
inet.h:55
netbuf
A packet (net/netbuf.c).
Definition
netbuf.h:16
netbuf::src_ip
uint32_t src_ip
Definition
netbuf.h:25
netbuf::data
uint8_t * data
Definition
netbuf.h:19
netbuf::src_port
uint16_t src_port
Definition
netbuf.h:26
netbuf::len
size_t len
Definition
netbuf.h:20
netif
A network interface (net/netif.c): a device that sends and receives Ethernet frames,...
Definition
netif.h:16
netif::stats
struct netif::@020302303320120142126121344055047003105154122231 stats
udp_hdr
A UDP datagram's header.
Definition
inet.h:77
udp_hdr::checksum
uint16_t checksum
Definition
inet.h:81
udp_hdr::src
uint16_t src
Definition
inet.h:78
udp_hdr::len
uint16_t len
Definition
inet.h:80
udp_hdr::dst
uint16_t dst
Definition
inet.h:79
kernel
net
inet
udp.c
Generated by
1.18.0