fd-net-device, network, tap-bridge: Simplify IP and MAC address conversion functions

While at it, remove now unneeded defines and helper functions.
This commit is contained in:
André Apitzsch
2024-01-10 11:54:01 +01:00
parent 687876616a
commit a39bcfa7ea
7 changed files with 25 additions and 251 deletions

View File

@@ -197,6 +197,7 @@ if(${ENABLE_FDNETDEV})
helper/creator-utils.cc
helper/encode-decode.cc
helper/tap-device-creator.cc
LIBRARIES_TO_LINK ${libnetwork}
EXECUTABLE_DIRECTORY_PATH
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/
INSTALL_DIRECTORY_PATH ${CMAKE_INSTALL_LIBEXECDIR}/ns3

View File

@@ -17,6 +17,8 @@
#include "creator-utils.h"
#include "ns3/mac48-address.h"
#include <arpa/inet.h>
#include <cstring>
#include <errno.h>
@@ -38,18 +40,6 @@
#define TAP_MAGIC 95549
//
// Lots of the following helper code taken from corresponding functions in src/node.
//
#define ASCII_DOT (0x2e)
#define ASCII_ZERO (0x30)
#define ASCII_a (0x41)
#define ASCII_z (0x5a)
#define ASCII_A (0x61)
#define ASCII_Z (0x7a)
#define ASCII_COLON (0x3a)
#define ASCII_ZERO (0x30)
using namespace ns3;
/**
@@ -62,54 +52,6 @@ struct in6_ifreq
int32_t ifr6_ifindex; //!< interface index
};
char
AsciiToLowCase(char c)
{
if (c >= ASCII_a && c <= ASCII_z)
{
return c;
}
else if (c >= ASCII_A && c <= ASCII_Z)
{
return c + (ASCII_a - ASCII_A);
}
else
{
return c;
}
}
void
AsciiToMac48(const char* str, uint8_t addr[6])
{
int i = 0;
while (*str != 0 && i < 6)
{
uint8_t byte = 0;
while (*str != ASCII_COLON && *str != 0)
{
byte <<= 4;
char low = AsciiToLowCase(*str);
if (low >= ASCII_a)
{
byte |= low - ASCII_a + 10;
}
else
{
byte |= low - ASCII_ZERO;
}
str++;
}
addr[i] = byte;
i++;
if (*str == 0)
{
break;
}
str++;
}
}
void
SetIpv4(const char* deviceName, const char* ip, const char* netmask)
{
@@ -189,7 +131,7 @@ SetMacAddress(int fd, const char* mac)
memset(&ifr, 0, sizeof(struct ifreq));
ifr.ifr_hwaddr.sa_family = 1; // this is ARPHRD_ETHER from if_arp.h
AsciiToMac48(mac, (uint8_t*)ifr.ifr_hwaddr.sa_data);
Mac48Address(mac).CopyTo((uint8_t*)ifr.ifr_hwaddr.sa_data);
ABORT_IF(ioctl(fd, SIOCSIFHWADDR, &ifr) == -1, "Could not set MAC address", true);
LOG("Set device MAC address to " << mac);
}

View File

@@ -35,35 +35,6 @@ NS_LOG_COMPONENT_DEFINE("Mac16Address");
ATTRIBUTE_HELPER_CPP(Mac16Address);
#define ASCII_a (0x41)
#define ASCII_z (0x5a)
#define ASCII_A (0x61)
#define ASCII_Z (0x7a)
#define ASCII_COLON (0x3a)
#define ASCII_ZERO (0x30)
/**
* Converts a char to lower case.
* \param c the char
* \returns the lower case
*/
static char
AsciiToLowCase(char c)
{
if (c >= ASCII_a && c <= ASCII_z)
{
return c;
}
else if (c >= ASCII_A && c <= ASCII_Z)
{
return c + (ASCII_a - ASCII_A);
}
else
{
return c;
}
}
uint64_t Mac16Address::m_allocationIndex = 0;
Mac16Address::Mac16Address()
@@ -79,17 +50,17 @@ Mac16Address::Mac16Address(const char* str)
while (*str != 0 && i < 2)
{
uint8_t byte = 0;
while (*str != ASCII_COLON && *str != 0)
while (*str != ':' && *str != 0)
{
byte <<= 4;
char low = AsciiToLowCase(*str);
if (low >= ASCII_a)
char low = std::tolower(*str);
if (low >= 'a')
{
byte |= low - ASCII_a + 10;
byte |= low - 'a' + 10;
}
else
{
byte |= low - ASCII_ZERO;
byte |= low - '0';
}
str++;
}

View File

@@ -34,36 +34,6 @@ NS_LOG_COMPONENT_DEFINE("Mac48Address");
ATTRIBUTE_HELPER_CPP(Mac48Address);
#define ASCII_a (0x41)
#define ASCII_z (0x5a)
#define ASCII_A (0x61)
#define ASCII_Z (0x7a)
#define ASCII_COLON (0x3a)
#define ASCII_ZERO (0x30)
/**
* Converts a char to lower case.
* \param c the char
* \returns the lower case
*/
static char
AsciiToLowCase(char c)
{
NS_LOG_FUNCTION(c);
if (c >= ASCII_a && c <= ASCII_z)
{
return c;
}
else if (c >= ASCII_A && c <= ASCII_Z)
{
return c + (ASCII_a - ASCII_A);
}
else
{
return c;
}
}
uint64_t Mac48Address::m_allocationIndex = 0;
Mac48Address::Mac48Address()
@@ -79,17 +49,17 @@ Mac48Address::Mac48Address(const char* str)
while (*str != 0 && i < 6)
{
uint8_t byte = 0;
while (*str != ASCII_COLON && *str != 0)
while (*str != ':' && *str != 0)
{
byte <<= 4;
char low = AsciiToLowCase(*str);
if (low >= ASCII_a)
char low = std::tolower(*str);
if (low >= 'a')
{
byte |= low - ASCII_a + 10;
byte |= low - 'a' + 10;
}
else
{
byte |= low - ASCII_ZERO;
byte |= low - '0';
}
str++;
}

View File

@@ -34,36 +34,6 @@ NS_LOG_COMPONENT_DEFINE("Mac64Address");
ATTRIBUTE_HELPER_CPP(Mac64Address);
#define ASCII_a (0x41)
#define ASCII_z (0x5a)
#define ASCII_A (0x61)
#define ASCII_Z (0x7a)
#define ASCII_COLON (0x3a)
#define ASCII_ZERO (0x30)
/**
* Converts a char to lower case.
* \param c the char
* \returns the lower case
*/
static char
AsciiToLowCase(char c)
{
NS_LOG_FUNCTION(c);
if (c >= ASCII_a && c <= ASCII_z)
{
return c;
}
else if (c >= ASCII_A && c <= ASCII_Z)
{
return c + (ASCII_a - ASCII_A);
}
else
{
return c;
}
}
uint64_t Mac64Address::m_allocationIndex = 0;
Mac64Address::Mac64Address()
@@ -79,17 +49,17 @@ Mac64Address::Mac64Address(const char* str)
while (*str != 0 && i < 8)
{
uint8_t byte = 0;
while (*str != ASCII_COLON && *str != 0)
while (*str != ':' && *str != 0)
{
byte <<= 4;
char low = AsciiToLowCase(*str);
if (low >= ASCII_a)
char low = std::tolower(*str);
if (low >= 'a')
{
byte |= low - ASCII_a + 10;
byte |= low - 'a' + 10;
}
else
{
byte |= low - ASCII_ZERO;
byte |= low - '0';
}
str++;
}

View File

@@ -38,6 +38,7 @@ build_exec(
EXECNAME tap-creator
SOURCE_FILES model/tap-creator.cc
model/tap-encode-decode.cc
LIBRARIES_TO_LINK ${libnetwork}
EXECUTABLE_DIRECTORY_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/tap-bridge/
INSTALL_DIRECTORY_PATH ${CMAKE_INSTALL_LIBEXECDIR}/ns3
STANDALONE

View File

@@ -17,6 +17,8 @@
#include "tap-encode-decode.h"
#include "ns3/mac48-address.h"
#include <cerrno>
#include <cstdlib>
#include <cstring> // for strerror
@@ -61,89 +63,6 @@ static bool gVerbose = false; // Set to true to turn on logging messages.
ABORT(msg, printErrno); \
}
//
// Lots of the following helper code taken from corresponding functions in src/node.
//
#define ASCII_DOT (0x2e)
#define ASCII_ZERO (0x30)
#define ASCII_a (0x41)
#define ASCII_z (0x5a)
#define ASCII_A (0x61)
#define ASCII_Z (0x7a)
#define ASCII_COLON (0x3a)
static char
AsciiToLowCase(char c)
{
if (c >= ASCII_a && c <= ASCII_z)
{
return c;
}
else if (c >= ASCII_A && c <= ASCII_Z)
{
return c + (ASCII_a - ASCII_A);
}
else
{
return c;
}
}
static uint32_t
AsciiToIpv4(const char* address)
{
uint32_t host = 0;
while (true)
{
uint8_t byte = 0;
while (*address != ASCII_DOT && *address != 0)
{
byte *= 10;
byte += *address - ASCII_ZERO;
address++;
}
host <<= 8;
host |= byte;
if (*address == 0)
{
break;
}
address++;
}
return host;
}
static void
AsciiToMac48(const char* str, uint8_t addr[6])
{
int i = 0;
while (*str != 0 && i < 6)
{
uint8_t byte = 0;
while (*str != ASCII_COLON && *str != 0)
{
byte <<= 4;
char low = AsciiToLowCase(*str);
if (low >= ASCII_a)
{
byte |= low - ASCII_a + 10;
}
else
{
byte |= low - ASCII_ZERO;
}
str++;
}
addr[i] = byte;
i++;
if (*str == 0)
{
break;
}
str++;
}
}
static sockaddr
CreateInetAddress(uint32_t networkOrder)
{
@@ -326,7 +245,7 @@ CreateTap(const char* dev,
// Set the hardware (MAC) address of the new device
//
ifr.ifr_hwaddr.sa_family = 1; // this is ARPHRD_ETHER from if_arp.h
AsciiToMac48(mac, (uint8_t*)ifr.ifr_hwaddr.sa_data);
ns3::Mac48Address(mac).CopyTo((uint8_t*)ifr.ifr_hwaddr.sa_data);
status = ioctl(tap, SIOCSIFHWADDR, &ifr);
ABORT_IF(status == -1, "Could not set MAC address", true);
LOG("Set device MAC address to " << mac);
@@ -346,7 +265,7 @@ CreateTap(const char* dev,
//
// Set the IP address of the new interface/device.
//
ifr.ifr_addr = CreateInetAddress(AsciiToIpv4(ip));
ifr.ifr_addr = CreateInetAddress(ns3::Ipv4Address(ip).Get());
status = ioctl(fd, SIOCSIFADDR, &ifr);
ABORT_IF(status == -1, "Could not set IP address", true);
LOG("Set device IP address to " << ip);
@@ -354,7 +273,7 @@ CreateTap(const char* dev,
//
// Set the net mask of the new interface/device
//
ifr.ifr_netmask = CreateInetAddress(AsciiToIpv4(netmask));
ifr.ifr_netmask = CreateInetAddress(ns3::Ipv4Mask(netmask).Get());
status = ioctl(fd, SIOCSIFNETMASK, &ifr);
ABORT_IF(status == -1, "Could not set net mask", true);
LOG("Set device Net Mask to " << netmask);