net: move ascii_to() into Net namespace

The combination of Net::Mac_address and
Genode::ascii_to(Net::Mac_address) required shaky quirks in several
places because GCC is not able to resolve the ascii_to overload if
base/xml_node.h was included to early. The current solution moves the
several ascii_to overloads "closer" to the Net types by putting them
into the Net namespace, where GCC reliably picks them up.

Hence, co-locating the ascii_to() utility with the overload type in the
same scope/namespace is good practice.

This patch removes the now obsolete <nic/xml_node.h> header file.
This commit is contained in:
Christian Helmuth 2020-09-11 10:44:44 +02:00 committed by Norman Feske
parent c0f9f2c6d7
commit 2eb8c5e21a
15 changed files with 130 additions and 164 deletions

View File

@ -14,7 +14,6 @@
#include <base/rpc_server.h>
#include <base/snprintf.h>
#include <nic_session/nic_session.h>
#include <nic/xml_node.h>
#include <util/xml_node.h>
#include <lx_kit/env.h>

View File

@ -17,7 +17,6 @@
#include <base/rpc_server.h>
#include <base/snprintf.h>
#include <base/tslab.h>
#include <nic/xml_node.h>
#include <nic/component.h>
#include <root/component.h>
#include <util/xml_node.h>

View File

@ -17,7 +17,11 @@
/* Genode includes */
#include <net/ipv4.h>
namespace Net { class Ipv4_address_prefix; }
namespace Net {
class Ipv4_address_prefix;
static inline Genode::size_t ascii_to(char const *, Net::Ipv4_address_prefix &);
}
struct Net::Ipv4_address_prefix
@ -48,14 +52,10 @@ struct Net::Ipv4_address_prefix
};
namespace Genode {
inline size_t ascii_to(char const *s, Net::Ipv4_address_prefix &result);
}
Genode::size_t Genode::ascii_to(char const *s, Net::Ipv4_address_prefix &result)
Genode::size_t Net::ascii_to(char const *s, Ipv4_address_prefix &result)
{
using namespace Genode;
/* read the leading IPv4 address, fail if there's no address */
Net::Ipv4_address_prefix buf;
size_t read_len = ascii_to(s, buf.address);

View File

@ -25,13 +25,13 @@
namespace Genode { class Output; }
namespace Net
{
namespace Net {
enum { IPV4_ADDR_LEN = 4 };
class Ipv4_address;
class Ipv4_packet;
static inline Genode::size_t ascii_to(char const *, Net::Ipv4_address &);
}
@ -191,17 +191,14 @@ class Net::Ipv4_packet
} __attribute__((packed));
namespace Genode {
inline size_t ascii_to(char const *s, Net::Ipv4_address &result);
}
Genode::size_t Genode::ascii_to(char const *s, Net::Ipv4_address &result)
Genode::size_t Net::ascii_to(char const *s, Net::Ipv4_address &result)
{
using namespace Genode;
Net::Ipv4_address buf;
size_t number_idx = 0;
size_t read_len = 0;
while (1) {
/* read the current number, fail if there's no number */

View File

@ -15,9 +15,15 @@
#define _NET__MAC_ADDRESS_H_
/* OS includes */
#include <util/string.h>
#include <net/netaddress.h>
namespace Net { struct Mac_address; }
namespace Net {
struct Mac_address;
static inline Genode::size_t ascii_to(char const *, Mac_address &);
}
struct Net::Mac_address : Net::Network_address<6, ':', true>
{
@ -25,4 +31,34 @@ struct Net::Mac_address : Net::Network_address<6, ':', true>
bool multicast() const { return addr[0] & 1; }
};
Genode::size_t Net::ascii_to(char const *s, Net::Mac_address &mac)
{
using namespace Genode;
enum {
HEX = true,
MAC_CHAR_LEN = 17, /* 12 number and 6 colons */
MAC_SIZE = 6,
};
if (Genode::strlen(s) < MAC_CHAR_LEN)
throw -1;
char mac_str[6];
for (int i = 0; i < MAC_SIZE; i++) {
int hi = i * 3;
int lo = hi + 1;
if (!is_digit(s[hi], HEX) || !is_digit(s[lo], HEX))
throw -1;
mac_str[i] = (digit(s[hi], HEX) << 4) | digit(s[lo], HEX);
}
Genode::memcpy(mac.addr, mac_str, MAC_SIZE);
return MAC_CHAR_LEN;
}
#endif /* _NET__MAC_ADDRESS_H_ */

View File

@ -19,7 +19,13 @@
#include <util/string.h>
#include <base/output.h>
namespace Net { template <unsigned, char, bool> class Network_address; }
namespace Net {
template <unsigned, char, bool> class Network_address;
template <unsigned LEN, char DELIM, bool HEX>
static inline Genode::size_t ascii_to(char const *,
Network_address<LEN, DELIM, HEX> &);
}
/**
@ -84,47 +90,44 @@ struct Net::Network_address
__attribute__((packed));
namespace Genode {
template <unsigned LEN, char DELIM, bool HEX>
Genode::size_t Net::ascii_to(char const *str, Net::Network_address<LEN, DELIM, HEX> &result)
{
using namespace Genode;
template <unsigned LEN, char DELIM, bool HEX>
inline size_t ascii_to(char const *str,
Net::Network_address<LEN, DELIM, HEX> &result)
{
Net::Network_address<LEN, DELIM, HEX> result_buf;
size_t number_id = 0;
size_t read_len = 0;
while (1) {
Net::Network_address<LEN, DELIM, HEX> result_buf;
size_t number_id = 0;
size_t read_len = 0;
/* read the current number */
size_t number_len =
ascii_to_unsigned(str, result_buf.addr[number_id],
HEX ? 16 : 10);
while (1) {
/* fail if there's no number */
if (!number_len) {
return 0; }
/* read the current number */
size_t number_len =
ascii_to_unsigned(str, result_buf.addr[number_id],
HEX ? 16 : 10);
/* update read length and number index */
read_len += number_len;
number_id++;
/* fail if there's no number */
if (!number_len) {
return 0; }
/* if we have all numbers, fill result and return read length */
if (number_id == LEN) {
result = result_buf;
return read_len;
}
/* there are numbers left, check for the delimiter */
str += number_len;
if (*str != DELIM) {
return 0; }
/* update read length and number index */
read_len += number_len;
number_id++;
/* seek to next number */
read_len++;
str++;
/* if we have all numbers, fill result and return read length */
if (number_id == LEN) {
result = result_buf;
return read_len;
}
/* there are numbers left, check for the delimiter */
str += number_len;
if (*str != DELIM) {
return 0; }
/* seek to next number */
read_len++;
str++;
}
}
#endif /* _NET__NETADDRESS_H_ */

View File

@ -19,7 +19,11 @@
#include <base/output.h>
#include <util/string.h>
namespace Net { class Port; }
namespace Net {
class Port;
static inline Genode::size_t ascii_to(const char *, Net::Port &);
}
/**
* This class makes it clear what the port integer-value means at an interface
@ -37,20 +41,19 @@ struct Net::Port
__attribute__((packed));
namespace Genode {
/**
* Read port value from string
*
* \return number of consumed characters
*/
Genode::size_t Net::ascii_to(const char *s, Net::Port &result)
{
using namespace Genode;
/**
* Read port value from string
*
* \return number of consumed characters
*/
inline size_t ascii_to(const char *s, Net::Port &result)
{
unsigned value = 0;
size_t const consumed = ascii_to_unsigned(s, value, 0);
result = Net::Port(value);
return consumed;
}
unsigned value = 0;
size_t const consumed = ascii_to_unsigned(s, value, 0);
result = Net::Port(value);
return consumed;
}
#endif /* _NET__PORT_H_ */

View File

@ -1,54 +0,0 @@
/*
* \brief Xml-node routines used internally in NIC drivers
* \author Sebastian Sumpf
* \author Stefan Kalkowski
* \date 2012-10-22
*/
/*
* Copyright (C) 2012-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _INCLUDE__NIC__XML_NODE_H_
#define _INCLUDE__NIC__XML_NODE_H_
#include <nic_session/nic_session.h>
#include <util/string.h>
namespace Genode {
/**
* Convert ASCII string to mac address
*/
inline size_t ascii_to(char const *s, Nic::Mac_address &mac)
{
enum {
HEX = true,
MAC_CHAR_LEN = 17, /* 12 number and 6 colons */
MAC_SIZE = 6,
};
if(strlen(s) < MAC_CHAR_LEN)
throw -1;
char mac_str[6];
for (int i = 0; i < MAC_SIZE; i++) {
int hi = i * 3;
int lo = hi + 1;
if (!is_digit(s[hi], HEX) || !is_digit(s[lo], HEX))
throw -1;
mac_str[i] = (digit(s[hi], HEX) << 4) | digit(s[lo], HEX);
}
Genode::memcpy(mac.addr, mac_str, MAC_SIZE);
return MAC_CHAR_LEN;
}
}
#endif /* _INCLUDE__NIC__XML_NODE_H_ */

View File

@ -17,7 +17,11 @@
/* Genode includes */
#include <net/ipv4.h>
namespace Net { class Ipv4_address_prefix; }
namespace Net {
class Ipv4_address_prefix;
static inline Genode::size_t ascii_to(char const *, Net::Ipv4_address_prefix &);
}
struct Net::Ipv4_address_prefix
@ -48,14 +52,10 @@ struct Net::Ipv4_address_prefix
};
namespace Genode {
inline size_t ascii_to(char const *s, Net::Ipv4_address_prefix &result);
}
Genode::size_t Genode::ascii_to(char const *s, Net::Ipv4_address_prefix &result)
Genode::size_t Net::ascii_to(char const *s, Ipv4_address_prefix &result)
{
using namespace Genode;
/* read the leading IPv4 address, fail if there's no address */
Net::Ipv4_address_prefix buf;
size_t read_len = ascii_to(s, buf.address);

View File

@ -24,13 +24,6 @@
*/
/* Genode */
/*
* Needs to be included first because otherwise
* util/xml_node.h will not pick up the ascii_to
* overload.
*/
#include <nic/xml_node.h>
#include <base/attached_rom_dataspace.h>
#include <base/component.h>
#include <base/heap.h>

View File

@ -12,13 +12,6 @@
*/
/* Genode includes */
/*
* Needs to be included first because otherwise
* util/xml_node.h will not pick up the ascii_to
* overload.
*/
#include <nic/xml_node.h>
#include <base/attached_rom_dataspace.h>
#include <base/component.h>
#include <base/heap.h>

View File

@ -12,8 +12,6 @@
*/
/* Genode */
#include <nic/xml_node.h> /* ugly template dependency forces us
to include this before xml_node.h */
#include <base/attached_rom_dataspace.h>
#include <base/component.h>
#include <base/env.h>

View File

@ -17,7 +17,11 @@
/* Genode includes */
#include <net/ipv4.h>
namespace Net { class Ipv4_address_prefix; }
namespace Net {
class Ipv4_address_prefix;
static inline Genode::size_t ascii_to(char const *, Net::Ipv4_address_prefix &);
}
struct Net::Ipv4_address_prefix
@ -48,14 +52,10 @@ struct Net::Ipv4_address_prefix
};
namespace Genode {
inline size_t ascii_to(char const *s, Net::Ipv4_address_prefix &result);
}
Genode::size_t Genode::ascii_to(char const *s, Net::Ipv4_address_prefix &result)
Genode::size_t Net::ascii_to(char const *s, Ipv4_address_prefix &result)
{
using namespace Genode;
/* read the leading IPv4 address, fail if there's no address */
Net::Ipv4_address_prefix buf;
size_t read_len = ascii_to(s, buf.address);

View File

@ -15,7 +15,6 @@
#include <base/component.h>
#include <base/heap.h>
#include <base/attached_rom_dataspace.h>
#include <nic/xml_node.h>
#include <timer_session/connection.h>
/* local includes */

View File

@ -17,7 +17,11 @@
/* Genode includes */
#include <net/ipv4.h>
namespace Net { class Ipv4_address_prefix; }
namespace Net {
class Ipv4_address_prefix;
static inline Genode::size_t ascii_to(char const *, Net::Ipv4_address_prefix &);
}
struct Net::Ipv4_address_prefix
@ -48,14 +52,10 @@ struct Net::Ipv4_address_prefix
};
namespace Genode {
inline size_t ascii_to(char const *s, Net::Ipv4_address_prefix &result);
}
Genode::size_t Genode::ascii_to(char const *s, Net::Ipv4_address_prefix &result)
Genode::size_t Net::ascii_to(char const *s, Ipv4_address_prefix &result)
{
using namespace Genode;
/* read the leading IPv4 address, fail if there's no address */
Net::Ipv4_address_prefix buf;
size_t read_len = ascii_to(s, buf.address);