resolved: add udp_header_size() helper

This commit is contained in:
Lennart Poettering
2021-02-18 15:55:58 +01:00
parent 20a001bdd7
commit 4565863fff
2 changed files with 24 additions and 6 deletions
+23 -5
View File
@@ -2,6 +2,7 @@
#pragma once
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include "hashmap.h"
@@ -32,14 +33,19 @@ struct DnsPacketHeader {
be16_t ancount;
be16_t nscount;
be16_t arcount;
};
} _packed_;
#define DNS_PACKET_HEADER_SIZE sizeof(DnsPacketHeader)
#define UDP_PACKET_HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr))
#define UDP4_PACKET_HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr))
#define UDP6_PACKET_HEADER_SIZE (sizeof(struct ip6_hdr) + sizeof(struct udphdr))
/* The various DNS protocols deviate in how large a packet can grow,
* but the TCP transport has a 16bit size field, hence that appears to
* be the absolute maximum. */
assert_cc(sizeof(struct ip6_hdr) == 40);
assert_cc(sizeof(struct iphdr) == 20);
assert_cc(sizeof(struct udphdr) == 8);
assert_cc(sizeof(DnsPacketHeader) == 12);
/* The various DNS protocols deviate in how large a packet can grow, but the TCP transport has a 16bit size
* field, hence that appears to be the absolute maximum. */
#define DNS_PACKET_SIZE_MAX 0xFFFFu
/* The default size to use for allocation when we don't know how large
@@ -307,3 +313,15 @@ static inline size_t dns_packet_size_max(DnsPacket *p) {
return p->max_size != 0 ? p->max_size : DNS_PACKET_SIZE_MAX;
}
static inline size_t udp_header_size(int af) {
switch (af) {
case AF_INET:
return UDP4_PACKET_HEADER_SIZE;
case AF_INET6:
return UDP6_PACKET_HEADER_SIZE;
default:
assert_not_reached("Unexpected address family");
}
}
+1 -1
View File
@@ -212,7 +212,7 @@ static int dns_scope_emit_one(DnsScope *s, int fd, DnsPacket *p) {
if (p->size > DNS_PACKET_UNICAST_SIZE_MAX)
return -EMSGSIZE;
if (p->size + UDP_PACKET_HEADER_SIZE > mtu)
if (p->size + UDP4_PACKET_HEADER_SIZE > mtu)
return -EMSGSIZE;
r = manager_write(s->manager, fd, p);