]> git.ipfire.org Git - people/pmueller/ipfire-3.x.git/blob - initscripts/patches/0001-ipcalc-Enhance-to-implode-explode-IPv6-addresses.patch
Merge branch 'dhcp'
[people/pmueller/ipfire-3.x.git] / initscripts / patches / 0001-ipcalc-Enhance-to-implode-explode-IPv6-addresses.patch
1 From 5baf67156ccb7fed775d8bd00298a6ccaed03214 Mon Sep 17 00:00:00 2001
2 From: Michael Tremer <michael.tremer@ipfire.org>
3 Date: Sun, 9 Sep 2012 21:34:19 +0000
4 Subject: [PATCH] ipcalc: Enhance to implode/explode IPv6 addresses.
5
6 These functionalities are handy to work with IPv6 addresses
7 in shell scripts.
8
9 implode:
10 Removes all leading zeroes from the IP address:
11 ::0:0:0:1 -> ::1
12
13 explode:
14 Adds all leading zeroes:
15 ::1 -> 0000:0000:0000:0000:0000:0000:0000:0001
16 ---
17 src/ipcalc.1 | 10 ++++++++++
18 src/ipcalc.c | 25 +++++++++++++++++++++++++
19 2 files changed, 35 insertions(+), 0 deletions(-)
20
21 diff --git a/src/ipcalc.1 b/src/ipcalc.1
22 index 30e0b20..654e2ba 100644
23 --- a/src/ipcalc.1
24 +++ b/src/ipcalc.1
25 @@ -51,6 +51,16 @@ Show the prefix for the given mask/IP address.
26 Display the network address for the given IP address and netmask.
27
28 .TP
29 +\fB\-i\fR, \fB\-\-implode\fR
30 +Implodes the given IPv6 address. Returns the shortest possible representation
31 +of the given IPv6 address.
32 +
33 +.TP
34 +\fB\-e\fR, \fB\-\-explode\fR
35 +Explodes the given IPv6 address. Adds all leading zeroes and replaces
36 +:: with zeroes, too.
37 +
38 +.TP
39 \fB\-s\fR, \fB\-\-silent\fR
40 Don't ever display error messages.
41
42 diff --git a/src/ipcalc.c b/src/ipcalc.c
43 index 7316f05..c3f4604 100644
44 --- a/src/ipcalc.c
45 +++ b/src/ipcalc.c
46 @@ -218,6 +218,7 @@ int main(int argc, const char **argv) {
47 int showHostname = 0, showNetmask = 0;
48 int beSilent = 0;
49 int doCheck = 0, familyIPv4 = 0, familyIPv6 = 0;
50 + int implodeIP6Address = 0, explodeIP6Address = 0;
51 int rc;
52 poptContext optCon;
53 char *ipStr, *prefixStr, *netmaskStr, *chptr;
54 @@ -246,6 +247,10 @@ int main(int argc, const char **argv) {
55 "Display network prefix", },
56 { "silent", 's', 0, &beSilent, 0,
57 "Don't ever display error messages" },
58 + { "implode", 'i', 0, &implodeIP6Address, 0,
59 + "Implode given IPv6 address", },
60 + { "explode", 'e', 0, &explodeIP6Address, 0,
61 + "Explode given IPv6 address", },
62 POPT_AUTOHELP
63 { NULL, '\0', 0, 0, 0, NULL, NULL }
64 };
65 @@ -472,5 +477,25 @@ int main(int argc, const char **argv) {
66 printf("HOSTNAME=%s\n", hostName);
67 }
68
69 + if (implodeIP6Address) {
70 + if (inet_ntop(AF_INET6, &ip6, namebuf, INET6_ADDRSTRLEN) == NULL) {
71 + perror("inet_ntop error");
72 + abort();
73 + }
74 +
75 + printf("ADDRESS6_IMPL=%s\n", namebuf);
76 + }
77 +
78 + if (explodeIP6Address) {
79 + sprintf(namebuf, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
80 + (int)ip6.s6_addr[0], (int)ip6.s6_addr[1], (int)ip6.s6_addr[2], (int)ip6.s6_addr[3],
81 + (int)ip6.s6_addr[4], (int)ip6.s6_addr[5], (int)ip6.s6_addr[6], (int)ip6.s6_addr[7],
82 + (int)ip6.s6_addr[8], (int)ip6.s6_addr[9], (int)ip6.s6_addr[10], (int)ip6.s6_addr[11],
83 + (int)ip6.s6_addr[12], (int)ip6.s6_addr[13], (int)ip6.s6_addr[14], (int)ip6.s6_addr[15]
84 + );
85 +
86 + printf("ADDRESS6_EXPL=%s\n", namebuf);
87 + }
88 +
89 return 0;
90 }
91 --
92 1.7.8.2
93