Skip to content

Commit a1dda9e

Browse files
committed
ipv6-gen-slaac: Handle < /64 IPv6 network addresses
When given an address like, 2001:db8::, we would print SLAAC : d00::100:0:0:0 this was due to trying to handle addresses like 2001:db8:a;b::, where we'd chop the trailing ';' off. That of course breaks for networks less than a /64, e.g 2001:db8::, where we need to leave the '::'. We now handle both those cases. It may also be useful to handle networks specified with a prefixlen, e.g 2001:db8::/32, so we now handle that case as well. Signed-off-by: Andrew Clayton <[email protected]>
1 parent 34cc4d0 commit a1dda9e

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

ipv6-gen-slaac.c

+17-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* ipv6-gen-slaac.c - Given an IPv6 network & MAC address create the
33
* corresponding SLAAC address.
44
*
5-
* Copyright (C) 2017 Andrew Clayton <[email protected]>
5+
* Copyright (C) 2017, 2021 Andrew Clayton <[email protected]>
66
*
77
* Licensed under the GNU General Public License Version 2 or
88
* the GNU Lesser General Public License Version 2.1
@@ -19,22 +19,31 @@
1919

2020
int main(int argc, char *argv[])
2121
{
22-
int i;
23-
int len;
22+
int i = 0;
2423
u8 mac[8];
2524
u8 slaacn[sizeof(struct in6_addr)];
2625
char slaac[INET6_ADDRSTRLEN];
26+
char *ptr;
2727

2828
if (argc < 2) {
2929
fprintf(stderr, "Usage: ipv6-gen-slaac <IPv6 network> <MAC address>\n");
3030
exit(EXIT_FAILURE);
3131
}
3232

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';
3847

3948
mac[0] = strtoul(argv[2], NULL, 16);
4049
mac[1] = strtoul(argv[2]+3, NULL, 16);

0 commit comments

Comments
 (0)