From 53524be28504c4ca044f2c13d621b7f987f27bdd Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Thu, 25 Aug 2016 17:45:34 +0200 Subject: [PATCH] net/mac_address: mac_from_string function --- repos/os/include/net/mac_address.h | 9 ++++- repos/os/src/lib/net/mac_address.cc | 63 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 repos/os/src/lib/net/mac_address.cc diff --git a/repos/os/include/net/mac_address.h b/repos/os/include/net/mac_address.h index 1e2df6327..6b1172e88 100644 --- a/repos/os/include/net/mac_address.h +++ b/repos/os/include/net/mac_address.h @@ -1,5 +1,5 @@ /* - * \brief Ethernet network or MAC address + * \brief Media access control (MAC) address * \author Martin Stein * \date 2016-06-22 */ @@ -17,6 +17,11 @@ /* OS includes */ #include -namespace Net { using Mac_address = Net::Network_address<6, ':', true>; } +namespace Net +{ + using Mac_address = Net::Network_address<6, ':', true>; + + Mac_address mac_from_string(const char * mac); +} #endif /* _NET__MAC_ADDRESS_H_ */ diff --git a/repos/os/src/lib/net/mac_address.cc b/repos/os/src/lib/net/mac_address.cc new file mode 100644 index 000000000..f7318d71b --- /dev/null +++ b/repos/os/src/lib/net/mac_address.cc @@ -0,0 +1,63 @@ +/* + * \brief Media access control (MAC) address + * \author Martin Stein + * \date 2016-06-22 + */ + +/* + * Copyright (C) 2016 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +/* Genode includes */ +#include +#include +#include + +using namespace Net; +using namespace Genode; + +struct Scanner_policy_number +{ + static bool identifier_char(char c, unsigned i ) { + return is_digit(c) && c !=':'; } +}; + +typedef Token Token; + + +Mac_address Net::mac_from_string(const char * mac) +{ + Mac_address mac_addr; + Token t(mac); + char tmpstr[3]; + int cnt = 0; + unsigned char ipb[6] = {0}; + + while(t) { + if (t.type() == Token::WHITESPACE || t[0] == ':') { + t = t.next(); + continue; + } + t.string(tmpstr, sizeof(tmpstr)); + + unsigned long tmpc = 0; + ascii_to(tmpstr, tmpc); + ipb[cnt] = tmpc & 0xFF; + t = t.next(); + + if (cnt == 6) { break; } + cnt++; + } + if (cnt == 6) { + mac_addr.addr[0] = ipb[0]; + mac_addr.addr[1] = ipb[1]; + mac_addr.addr[2] = ipb[2]; + mac_addr.addr[3] = ipb[3]; + mac_addr.addr[4] = ipb[4]; + mac_addr.addr[5] = ipb[5]; + } + return mac_addr; +}