-
Notifications
You must be signed in to change notification settings - Fork 4
/
ipv6-gen-slaac.c
61 lines (51 loc) · 1.38 KB
/
ipv6-gen-slaac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* ipv6-gen-slaac.c - Given an IPv6 network & MAC address create the
* corresponding SLAAC address.
*
* Copyright (C) 2017, 2021 Andrew Clayton <[email protected]>
*
* Licensed under the GNU General Public License Version 2 or
* the GNU Lesser General Public License Version 2.1
*
* See GPLv2 & LGPLv2.1 in the source tree.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "short_types.h"
#include "common.h"
int main(int argc, char *argv[])
{
int i = 0;
u8 mac[8];
u8 slaacn[sizeof(struct in6_addr)];
char slaac[INET6_ADDRSTRLEN];
char *ptr;
if (argc < 2) {
fprintf(stderr, "Usage: ipv6-gen-slaac <IPv6 network> <MAC address>\n");
exit(EXIT_FAILURE);
}
snprintf(slaac, sizeof(slaac), "%s", argv[1]);
/* Handle networks specified with a prefixlen e.g 2001:db8::/32 */
ptr = strchr(slaac, '/');
if (ptr)
*ptr = '\0';
ptr = slaac;
while ((ptr = strchr(ptr, ':'))) {
ptr++;
i++;
}
/* Handle networks like 2001:db8:a:b:: */
if (i > 4)
slaac[strlen(slaac)-1] = '\0';
mac_to_eui64(argv[2], mac);
for (i = 0; i < 8; i += 2)
snprintf(slaac + strlen(slaac), sizeof(slaac), "%02x%02x%s",
mac[i], mac[i+1], i+1 < 7 ? ":" : "");
/* Display in compressed form */
inet_pton(AF_INET6, slaac, slaacn);
inet_ntop(AF_INET6, slaacn, slaac, INET6_ADDRSTRLEN);
printf("SLAAC : %s\n", slaac);
exit(EXIT_SUCCESS);
}