aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorDavid M. Syzdek <syzdek@bindlebinaries.com>2013-12-05 11:59:28 -0900
committerDavid M. Syzdek <syzdek@bindlebinaries.com>2013-12-05 11:59:28 -0900
commite3e4b582d128a73f3da7f9889f43f5872be0cd72 (patch)
treee3e144a0dc504389e78336b6ee47dc27508d5736 /src
parentc0eaa57f3ec4f8590b648752f837d1473a1ce25c (diff)
downloaddmstools-e3e4b582d128a73f3da7f9889f43f5872be0cd72.tar.gz
dmstools-e3e4b582d128a73f3da7f9889f43f5872be0cd72.tar.bz2
dmstools-e3e4b582d128a73f3da7f9889f43f5872be0cd72.tar.xz
dmstools-e3e4b582d128a73f3da7f9889f43f5872be0cd72.zip
Adding ability to derive EUI-64 and IPv6 Link Local address from MAC.
Diffstat (limited to 'src')
-rw-r--r--src/macaddrutil.c77
1 files changed, 69 insertions, 8 deletions
diff --git a/src/macaddrutil.c b/src/macaddrutil.c
index f7fe87f..49d5fbe 100644
--- a/src/macaddrutil.c
+++ b/src/macaddrutil.c
@@ -76,6 +76,10 @@
76#include <stdarg.h> 76#include <stdarg.h>
77#include <assert.h> 77#include <assert.h>
78#include <regex.h> 78#include <regex.h>
79#include <sys/socket.h>
80#include <signal.h>
81#include <netdb.h>
82#include <arpa/inet.h>
79 83
80 84
81/////////////////// 85///////////////////
@@ -160,9 +164,10 @@
160#pragma mark - Data Types 164#pragma mark - Data Types
161#endif 165#endif
162 166
163typedef uint8_t mauaddr_t[6]; 167typedef uint8_t mauaddr_t[6]; // Media Access Control address (MAC address)
164typedef uint8_t mauoui_t[3]; 168typedef uint8_t mauoui_t[3]; // Organizationally Unique Identifier (OUI)
165typedef char maustr_t[18]; 169typedef uint8_t maueui64_t[8]; // 64-bit Global Identifier (EUI-64)
170typedef char maustr_t[INET6_ADDRSTRLEN+1];
166 171
167typedef struct mau_config mau_config; 172typedef struct mau_config mau_config;
168typedef struct mau_command mau_command; 173typedef struct mau_command mau_command;
@@ -234,7 +239,10 @@ void mau_log_warn(mau_config * cnf, const char * fmt, ...);
234 239
235void mau_logv(mau_config * cnf, const char * fmt, va_list args); 240void mau_logv(mau_config * cnf, const char * fmt, va_list args);
236 241
242int mau_eui2str(mau_config * cnf, const maueui64_t eui, maustr_t str);
243int mau_mac2eui(mau_config * cnf, const mauaddr_t addr, maueui64_t eui);
237int mau_mac2str(mau_config * cnf, const mauaddr_t addr, maustr_t str); 244int mau_mac2str(mau_config * cnf, const mauaddr_t addr, maustr_t str);
245int mau_eui2mac(mau_config * cnf, const maueui64_t eui, mauaddr_t addr);
238int mau_str2mac(mau_config * cnf, const maustr_t str, mauaddr_t addr); 246int mau_str2mac(mau_config * cnf, const maustr_t str, mauaddr_t addr);
239 247
240//displays usage information 248//displays usage information
@@ -583,8 +591,12 @@ int mau_cmd_info(mau_config * cnf, int argc, char * argv[])
583{ 591{
584 int c; 592 int c;
585 int opt_index; 593 int opt_index;
586 maustr_t str;
587 mauaddr_t addr; 594 mauaddr_t addr;
595 maustr_t addr_str;
596 maueui64_t eui;
597 maustr_t eui_str;
598 maustr_t ipv6_str;
599 struct sockaddr_in6 sin;
588 600
589 // getopt options 601 // getopt options
590 static char short_opt[] = MAU_GETOPT_SHORT; 602 static char short_opt[] = MAU_GETOPT_SHORT;
@@ -632,11 +644,24 @@ int mau_cmd_info(mau_config * cnf, int argc, char * argv[])
632 if ((mau_str2mac(cnf, argv[optind], addr))) 644 if ((mau_str2mac(cnf, argv[optind], addr)))
633 return(1); 645 return(1);
634 646
635 mau_mac2str(cnf, addr, str); 647 mau_mac2str(cnf, addr, addr_str);
648 mau_mac2eui(cnf, addr, eui);
649 mau_eui2str(cnf, eui, eui_str);
650
651 memset(&sin, 0, sizeof(sin));
652 sin.sin6_len = sizeof(sin);
653 sin.sin6_family = AF_INET6;
654 sin.sin6_addr.s6_addr[0] = 0xfe;
655 sin.sin6_addr.s6_addr[1] = 0x80;
656 memcpy(&sin.sin6_addr.s6_addr[8], eui, 8);
636 657
637 printf("MAC Address: %s\n", str); 658 getnameinfo((struct sockaddr *)&sin, sin.sin6_len, ipv6_str, sizeof(ipv6_str), NULL, 0, NI_NUMERICHOST|NI_NUMERICSERV);
638 printf("U/L Bit: %s\n", ((addr[0] & 0x02) == 0x02) ? "1 (locally administered)" : "0 (universally administered)"); 659
639 printf("Multicast Bit: %s\n", ((addr[0] & 0x01) == 0x01) ? "1 (multicast)" : "0 (unicast)"); 660 printf("MAC Address: %s\n", addr_str);
661 printf("U/L Bit: %s\n", ((addr[0] & 0x02) == 0x02) ? "1 (locally administered)" : "0 (universally administered)");
662 printf("Multicast Bit: %s\n", ((addr[0] & 0x01) == 0x01) ? "1 (multicast)" : "0 (unicast)");
663 printf("Modified EUI-64: %s\n", eui_str);
664 printf("IPv6 Link-local: %s\n", ipv6_str);
640 665
641 return(0); 666 return(0);
642} 667}
@@ -823,6 +848,42 @@ void mau_logv(mau_config * cnf, const char * fmt, va_list args)
823} 848}
824 849
825 850
851int mau_eui2str(mau_config * cnf, const maueui64_t eui, maustr_t str)
852{
853 switch (cnf->notation | MAU_MASK_CASE)
854 {
855 case MAU_CASE_LOWER:
856 snprintf(str, sizeof(maustr_t), "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x", eui[0],eui[1],eui[2],eui[3],eui[4],eui[5],eui[6],eui[7]);
857 break;
858
859 case MAU_CASE_UPPER:
860 default:
861 snprintf(str, sizeof(maustr_t), "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X", eui[0],eui[1],eui[2],eui[3],eui[4],eui[5],eui[6],eui[7]);
862 break;
863 }
864 return(0);
865}
866
867
868int mau_mac2eui(mau_config * cnf, const mauaddr_t addr, maueui64_t eui)
869{
870 assert(cnf != NULL);
871 assert(eui != NULL);
872 assert(addr != NULL);
873
874 eui[0] = (addr[0] ^ 0x02);
875 eui[1] = addr[1];
876 eui[2] = addr[2];
877 eui[3] = 0xff;
878 eui[4] = 0xfe;
879 eui[5] = addr[3];
880 eui[6] = addr[4];
881 eui[7] = addr[5];
882
883 return(0);
884}
885
886
826int mau_mac2str(mau_config * cnf, const mauaddr_t addr, maustr_t str) 887int mau_mac2str(mau_config * cnf, const mauaddr_t addr, maustr_t str)
827{ 888{
828 switch (cnf->notation | (MAU_MASK_NOTATION & MAU_MASK_CASE)) 889 switch (cnf->notation | (MAU_MASK_NOTATION & MAU_MASK_CASE))