12#ifndef OPENVPN_TUN_CLIENT_DHCP_CAPTURE_H
13#define OPENVPN_TUN_CLIENT_DHCP_CAPTURE_H
35 OPENVPN_LOG(
"NOTE: pushed ifconfig directive is ignored in layer 2 mode");
37 OPENVPN_LOG(
"NOTE: pushed DNS servers are ignored in layer 2 mode");
46 if (!is_safe_conversion<unsigned int>(buf.
size()))
55 const unsigned int optlen =
static_cast<unsigned int>(buf.
size() -
sizeof(
DHCPPacket));
68 const std::vector<DnsAddress> dns_addresses =
get_dns(dhcp, optlen);
81 if (
host.unspecified())
83 OPENVPN_LOG(
"NOTE: failed to obtain host address via DHCP");
88 OPENVPN_LOG(
"NOTE: failed to obtain netmask via DHCP");
93 OPENVPN_LOG(
"NOTE: bad netmask obtained via DHCP: " << netmask);
98 OPENVPN_LOG(
"NOTE: failed to obtain router via DHCP");
105 if (dns_addresses.empty())
106 OPENVPN_LOG(
"NOTE: failed to obtain DNS servers via DHCP");
137 const std::uint8_t *p = dhcp->
options;
138 for (
unsigned int i = 0; i < optlen; ++i)
140 const std::uint8_t type = p[i];
141 const unsigned int room = optlen - i;
160 const unsigned int len = p[i + 1];
170 std::uint8_t *p = dhcp->
options;
173 for (
unsigned int i = 0; i < optlen;)
175 const std::uint8_t type = p[i];
176 const unsigned int room = optlen - i;
186 const unsigned int len = p[i + 1];
187 if (len <= (room - 2))
190 if (
ret.unspecified() && len >= 4 && (len & 3) == 0)
194 std::uint8_t *dest = p + i;
195 const unsigned int owlen = len + 2;
196 std::uint8_t *src = dest + owlen;
197 std::uint8_t *end = p + optlen;
198 const ssize_t movlen = end - src;
200 std::memmove(dest, src,
static_cast<size_t>(movlen));
213 const unsigned int len = p[i + 1];
225 const std::uint8_t *p = dhcp->
options;
228 for (
unsigned int i = 0; i < optlen;)
230 const std::uint8_t type = p[i];
231 const unsigned int room = optlen - i;
241 const unsigned int len = p[i + 1];
242 if (len <= (room - 2) && len == 4)
254 const unsigned int len = p[i + 1];
266 const std::uint8_t *p = dhcp->
options;
267 std::vector<DnsAddress>
ret;
269 for (
unsigned int i = 0; i < optlen;)
271 const std::uint8_t type = p[i];
272 const unsigned int room = optlen - i;
282 const unsigned int len = p[i + 1];
283 if (len <= (room - 2) && (len & 3) == 0)
286 for (
unsigned int j = 0; j < len; j += 4)
287 ret.push_back({IPv4::Addr::from_bytes_net(p + i + j + 2).to_string(), 0});
301 const unsigned int len = p[i + 1];
size_t size() const
Returns the size of the buffer in T objects.
T * data()
Get a mutable pointer to the start of the array.
static int dhcp_message_type(const DHCPPacket *dhcp, const unsigned int optlen)
static IPv4::Addr extract_router(DHCPPacket *dhcp, const unsigned int optlen)
TunBuilderCapture::Ptr props
const TunBuilderCapture & get_props() const
DHCPCapture(const TunBuilderCapture::Ptr &props_arg)
static std::vector< DnsAddress > get_dns(const DHCPPacket *dhcp, const unsigned int optlen)
static IPv4::Addr get_netmask(const DHCPPacket *dhcp, const unsigned int optlen)
bool mod_reply(Buffer &buf)
static Addr from_bytes_net(const unsigned char *bytes)
static Addr from_uint32_net(const base_type addr)
int prefix_len_nothrow() const
std::string to_string() const
bool tun_builder_set_dns_options(const DnsOptions &dns) override
Set DNS options for use with tun builder.
void reset_dns_options()
Resets DNS options to default values.
bool tun_builder_add_address(const std::string &address, int prefix_length, const std::string &gateway, bool ipv6, bool net30) override
Adds a local address to the TUN interface.
const RouteAddress * vpn_ipv4() const
Gets the IPv4 tunnel address.
void reset_tunnel_addresses()
Resets all tunnel addresses.
#define OPENVPN_LOG(args)
std::uint16_t udp_checksum(const std::uint8_t *buf, const unsigned int len_udp, const std::uint8_t *src_addr, const std::uint8_t *dest_addr)
All DNS options set with the –dns or –dhcp-option directive.
std::map< int, DnsServer > servers
DNS settings for a name server.
std::vector< DnsAddress > addresses
int prefix_len(const IPv4::Addr::base_type mask)