ipv4 address: conversion from and to integer

Ref #2490
This commit is contained in:
Martin Stein 2017-09-27 13:54:58 +02:00 committed by Christian Helmuth
parent d0f5838c61
commit 791fd9806f
2 changed files with 48 additions and 0 deletions

View File

@ -43,6 +43,14 @@ struct Net::Ipv4_address : Network_address<IPV4_ADDR_LEN, '.', false>
Ipv4_address(void *src) : Network_address(src) { }
bool valid() const { return *this != Ipv4_address(); }
Genode::uint32_t to_uint32_big_endian() const;
static Ipv4_address from_uint32_big_endian(Genode::uint32_t ip_raw);
Genode::uint32_t to_uint32_little_endian() const;
static Ipv4_address from_uint32_little_endian(Genode::uint32_t ip_raw);
}
__attribute__((packed));

View File

@ -38,6 +38,46 @@ void Net::Ipv4_packet::print(Genode::Output &output) const
}
uint32_t Ipv4_address::to_uint32_big_endian() const
{
return addr[0] |
addr[1] << 8 |
addr[2] << 16 |
addr[3] << 24;
}
Ipv4_address Ipv4_address::from_uint32_big_endian(uint32_t ip_raw)
{
Ipv4_address ip;
ip.addr[0] = ip_raw;
ip.addr[1] = ip_raw >> 8;
ip.addr[2] = ip_raw >> 16;
ip.addr[3] = ip_raw >> 24;
return ip;
}
uint32_t Ipv4_address::to_uint32_little_endian() const
{
return addr[3] |
addr[2] << 8 |
addr[1] << 16 |
addr[0] << 24;
}
Ipv4_address Ipv4_address::from_uint32_little_endian(uint32_t ip_raw)
{
Ipv4_address ip;
ip.addr[3] = ip_raw;
ip.addr[2] = ip_raw >> 8;
ip.addr[1] = ip_raw >> 16;
ip.addr[0] = ip_raw >> 24;
return ip;
}
struct Scanner_policy_number
{
static bool identifier_char(char c, unsigned i ) {