Created
March 2, 2012 21:25
-
-
Save fritz0705/1961516 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <arpa/inet.h> | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #include <sys/types.h> | |
| #include <unistd.h> | |
| #include <netinet/in.h> | |
| // gcc -o ipv6_addr ipv6_addr.c -std=c99 | |
| int main(int argc, char **argv) | |
| { | |
| char *ipv6_net = NULL; | |
| char *mac_addr = NULL; | |
| for (int i = 1; i < argc; ++i) | |
| { | |
| if (ipv6_net == NULL) | |
| ipv6_net = argv[i]; | |
| else if (mac_addr == NULL) | |
| mac_addr = argv[i]; | |
| } | |
| if (mac_addr == NULL) | |
| { | |
| printf("Usage: %s IPV6_ADDR MAC_ADDR\n", argv[0]); | |
| exit(0); | |
| } | |
| struct in6_addr ipv6_net_s; | |
| if (inet_pton(AF_INET6, ipv6_net, &ipv6_net_s) != 1) | |
| { | |
| fprintf(stderr, "Invalid ipv6 address: %s\n", ipv6_net); | |
| exit(1); | |
| } | |
| unsigned int mac[6]; | |
| memset(&mac, 0, sizeof(int) * 6); | |
| sscanf(mac_addr, "%x:%x:%x:%x:%x:%x", mac, mac + 1, mac + 2, mac + 3, mac + 4, mac + 5); | |
| ipv6_net_s.s6_addr[8] = mac[0] ^ 2; | |
| ipv6_net_s.s6_addr[9] = mac[1]; | |
| ipv6_net_s.s6_addr[10] = mac[2]; | |
| ipv6_net_s.s6_addr[11] = 0xff; | |
| ipv6_net_s.s6_addr[12] = 0xfe; | |
| ipv6_net_s.s6_addr[13] = mac[3]; | |
| ipv6_net_s.s6_addr[14] = mac[4]; | |
| ipv6_net_s.s6_addr[15] = mac[5]; | |
| char result[INET6_ADDRSTRLEN]; | |
| inet_ntop(AF_INET6, &ipv6_net_s, result, INET6_ADDRSTRLEN); | |
| printf("%s\n", result); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment