-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmdline.c
81 lines (63 loc) · 1.91 KB
/
cmdline.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "ipv6.h"
#include "ipv6_config.h"
#ifdef WIN32
#pragma warning(disable:4820) // padding warnings
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
typedef struct {
const char* message;
ipv6_diag_event_t event;
size_t calls;
} diag_capture_t;
// Function that is called by the address parser to report diagnostics
static void cmdline_parsing_diag_fn (
ipv6_diag_event_t event,
const ipv6_diag_info_t* info,
void* user_data)
{
(void)user_data;
printf("error: %s, event-code: (%u)\n", info->message, event);
printf(" %s\n", info->input);
printf(" %*s\n", info->position, info->input);
}
int main (int argc, const char** argv) {
if (argc < 2) {
printf("usage: %s <address>\n", argv[0]);
return 1;
}
{
ipv6_address_full_t addr, addr2;
const char* str = argv[1];
if (!ipv6_from_str_diag(str, strlen(str), &addr, cmdline_parsing_diag_fn, NULL)) {
printf("- failed to parse: '%s'\n", str);
return 2;
}
char* buffer = (char*)alloca(IPV6_STRING_SIZE);
if (!ipv6_to_str(&addr, buffer, sizeof(char) * IPV6_STRING_SIZE)) {
printf("- failed to convert: '%s'\n", str);
return 3;
}
if (!ipv6_from_str_diag(buffer, strlen(buffer), &addr2, cmdline_parsing_diag_fn, NULL)) {
printf("- failed to roundtrip: '%s'\n", buffer);
return 4;
}
// Allow embedded and compatible addresses to compare as equal
if (IPV6_COMPARE_OK != ipv6_compare(&addr, &addr2, IPV6_FLAG_IPV4_COMPAT)) {
printf("- failed to compare: '%s' != '%s'\n", str, buffer);
return 5;
}
printf("OK (%s)\n", buffer);
}
return 0;
}