|
2 | 2 | * ipv6-gen-slaac.c - Given an IPv6 network & MAC address create the
|
3 | 3 | * corresponding SLAAC address.
|
4 | 4 | *
|
5 |
| - * Copyright (C) 2017 Andrew Clayton <[email protected]> |
| 5 | + * Copyright (C) 2017, 2021 Andrew Clayton <[email protected]> |
6 | 6 | *
|
7 | 7 | * Licensed under the GNU General Public License Version 2 or
|
8 | 8 | * the GNU Lesser General Public License Version 2.1
|
|
19 | 19 |
|
20 | 20 | int main(int argc, char *argv[])
|
21 | 21 | {
|
22 |
| - int i; |
23 |
| - int len; |
| 22 | + int i = 0; |
24 | 23 | u8 mac[8];
|
25 | 24 | u8 slaacn[sizeof(struct in6_addr)];
|
26 | 25 | char slaac[INET6_ADDRSTRLEN];
|
| 26 | + char *ptr; |
27 | 27 |
|
28 | 28 | if (argc < 2) {
|
29 | 29 | fprintf(stderr, "Usage: ipv6-gen-slaac <IPv6 network> <MAC address>\n");
|
30 | 30 | exit(EXIT_FAILURE);
|
31 | 31 | }
|
32 | 32 |
|
33 |
| - len = snprintf(slaac, sizeof(slaac), "%s", argv[1]); |
34 |
| - if (slaac[len-1] == ':' && slaac[len-2] == ':') |
35 |
| - slaac[len-1] = '\0'; |
36 |
| - else if (slaac[len-1] != ':') |
37 |
| - snprintf(slaac + len, sizeof(slaac), ":"); |
| 33 | + snprintf(slaac, sizeof(slaac), "%s", argv[1]); |
| 34 | + /* Handle networks specified with a prefixlen e.g 2001:db8::/32 */ |
| 35 | + ptr = strchr(slaac, '/'); |
| 36 | + if (ptr) |
| 37 | + *ptr = '\0'; |
| 38 | + |
| 39 | + ptr = slaac; |
| 40 | + while ((ptr = strchr(ptr, ':'))) { |
| 41 | + ptr++; |
| 42 | + i++; |
| 43 | + } |
| 44 | + /* Handle networks like 2001:db8:a:b:: */ |
| 45 | + if (i > 4) |
| 46 | + slaac[strlen(slaac)-1] = '\0'; |
38 | 47 |
|
39 | 48 | mac[0] = strtoul(argv[2], NULL, 16);
|
40 | 49 | mac[1] = strtoul(argv[2]+3, NULL, 16);
|
|
0 commit comments