-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathipcalc.c
1591 lines (1354 loc) · 38.5 KB
/
ipcalc.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997-2015 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Nikos Mavrogiannopoulos <nmav@redhat.com>
* Erik Troan <ewt@redhat.com>
* Preston Brown <pbrown@redhat.com>
* David Cantrell <dcantrell@redhat.com>
*/
#define _GNU_SOURCE /* asprintf */
#include <ctype.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h> /* open */
#include <fcntl.h> /* open */
#include <unistd.h> /* read */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <time.h> /* clock_gettime */
#include "ipcalc.h"
int beSilent = 0;
static unsigned colors = 0;
/*!
\file ipcalc.c
\brief provides utilities for manipulating IP addresses.
ipcalc provides utilities and a front-end command line interface for
manipulating IP addresses, and calculating various aspects of an ip
address/netmask/network address/prefix/etc.
Functionality can be accessed from other languages from the library
interface, documented here. To use ipcalc from the shell, read the
ipcalc(1) manual page.
When passing parameters to the various functions, take note of whether they
take host byte order or network byte order. Most take host byte order, and
return host byte order, but there are some exceptions.
*/
static int safe_atoi(const char *s, int *ret_i)
{
char *x = NULL;
long l;
errno = 0;
l = strtol(s, &x, 0);
if (!x || x == s || *x || errno)
return errno > 0 ? -errno : -EINVAL;
if ((long)(int)l != l)
return -ERANGE;
*ret_i = (int)l;
return 0;
}
/*!
\fn char safe_strdup(const char *s)
\brief strdup(3) that checks memory allocation or fail
This function does the same as strdup(3) with additional memory allocation
check. When check fails the function will cause program to exit.
\param string to be duplicated
\return allocated duplicate
*/
extern char __attribute__((warn_unused_result)) *safe_strdup(const char *str)
{
char *ret;
if (!str)
return NULL;
ret = strdup(str);
if (!ret) {
fprintf(stderr, "Memory allocation failure\n");
exit(1);
}
return ret;
}
/*!
\fn uint32_t prefix2mask(int bits)
\brief creates a netmask from a specified number of bits
This function converts a prefix length to a netmask. As CIDR (classless
internet domain internet domain routing) has taken off, more an more IP
addresses are being specified in the format address/prefix
(i.e. 192.168.2.3/24, with a corresponding netmask 255.255.255.0). If you
need to see what netmask corresponds to the prefix part of the address, this
is the function. See also \ref mask2prefix.
\param prefix is the number of bits to create a mask for.
\return a network mask, in network byte order.
*/
uint32_t prefix2mask(int prefix)
{
struct in_addr mask;
memset(&mask, 0, sizeof(mask));
if (prefix) {
return htonl(~((1 << (32 - prefix)) - 1));
} else {
return htonl(0);
}
}
/*!
\fn struct in_addr calc_broadcast(struct in_addr addr, int prefix)
\brief calculate broadcast address given an IP address and a prefix length.
\param addr an IP address in network byte order.
\param prefix a prefix length.
\return the calculated broadcast address for the network, in network byte
order.
*/
static struct in_addr calc_broadcast(struct in_addr addr, int prefix)
{
struct in_addr mask;
struct in_addr broadcast;
mask.s_addr = prefix2mask(prefix);
memset(&broadcast, 0, sizeof(broadcast));
broadcast.s_addr = (addr.s_addr & mask.s_addr) | ~mask.s_addr;
return broadcast;
}
/*!
\fn struct in_addr calc_network(struct in_addr addr, int prefix)
\brief calculates the network address for a specified address and prefix.
\param addr an IP address, in network byte order
\param prefix the network prefix
\return the base address of the network that addr is associated with, in
network byte order.
*/
static struct in_addr calc_network(struct in_addr addr, int prefix)
{
struct in_addr mask;
struct in_addr network;
mask.s_addr = prefix2mask(prefix);
memset(&network, 0, sizeof(network));
network.s_addr = addr.s_addr & mask.s_addr;
return network;
}
/*!
\fn const char *get_hostname(int family, void *addr)
\brief returns the hostname associated with the specified IP address
\param family the address family, either AF_INET or AF_INET6.
\param addr an IP address to find a hostname for, in network byte order,
should either be a pointer to a struct in_addr or a struct in6_addr.
\return a hostname, or NULL if one cannot be determined. Hostname is stored
in an allocated buffer.
*/
static char *get_hostname(int family, void *addr)
{
static char hostname[NI_MAXHOST];
int ret = -1;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
if (family == AF_INET) {
memset(&addr4, 0, sizeof(addr4));
addr4.sin_family = AF_INET;
memcpy(&addr4.sin_addr, addr, sizeof(struct in_addr));
ret = getnameinfo((struct sockaddr*)&addr4, sizeof(addr4), hostname, sizeof(hostname), NULL, 0, 0);
} else if (family == AF_INET6) {
memset(&addr6, 0, sizeof(addr6));
addr6.sin6_family = AF_INET6;
memcpy(&addr6.sin6_addr, addr, sizeof(struct in6_addr));
ret = getnameinfo((struct sockaddr*)&addr6, sizeof(addr6), hostname, sizeof(hostname), NULL, 0, 0);
}
if (ret != 0)
return NULL;
return safe_strdup(hostname);
}
/*!
\fn const char *get_ip_address(int family, void *addr)
\brief returns the IP address associated with the specified hostname
\param family the requested address family or AF_UNSPEC for any
\param host a hostname
\return an IP address, or NULL if one cannot be determined. The IP is stored
in an allocated buffer.
*/
static char *get_ip_address(int family, const char *host)
{
struct addrinfo *res, *rp;
struct addrinfo hints;
int err;
static char ipname[64];
void *addr;
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
err = getaddrinfo(host, NULL, &hints, &res);
if (err != 0)
return NULL;
for (rp=res;rp!=NULL;rp=rp->ai_next) {
if (rp->ai_family == AF_INET)
addr = (&((struct sockaddr_in *)(rp->ai_addr))->sin_addr);
else
addr = (&((struct sockaddr_in6 *)(rp->ai_addr))->sin6_addr);
if (inet_ntop(rp->ai_family, addr, ipname, sizeof(ipname)) != NULL) {
freeaddrinfo(res);
return safe_strdup(ipname);
}
}
freeaddrinfo(res);
return NULL;
}
static int bit_count(uint32_t i)
{
int c = 0;
unsigned int seen_one = 0;
while (i > 0) {
if (i & 1) {
seen_one = 1;
c++;
} else {
if (seen_one) {
return -1;
}
}
i >>= 1;
}
return c;
}
/*!
\fn int mask2prefix(struct in_addr mask)
\brief calculates the number of bits masked off by a netmask.
This function calculates the significant bits in an IP address as specified by
a netmask. See also \ref prefix2mask.
\param mask is the netmask, specified as an struct in_addr in network byte order.
\return the number of significant bits. */
static int mask2prefix(struct in_addr mask)
{
return bit_count(ntohl(mask.s_addr));
}
static
int ipv4_mask_to_int(const char *prefix)
{
int ret;
struct in_addr in;
ret = inet_pton(AF_INET, prefix, &in);
if (ret == 0)
return -1;
return mask2prefix(in);
}
/* Returns powers of two in textual format */
static const char *p2_table(unsigned pow)
{
static const char *pow2[] = {
"1",
"2",
"4",
"8",
"16",
"32",
"64",
"128",
"256",
"512",
"1024",
"2048",
"4096",
"8192",
"16384",
"32768",
"65536",
"131072",
"262144",
"524288",
"1048576",
"2097152",
"4194304",
"8388608",
"16777216",
"33554432",
"67108864",
"134217728",
"268435456",
"536870912",
"1073741824",
"2147483648",
"4294967296",
"8589934592",
"17179869184",
"34359738368",
"68719476736",
"137438953472",
"274877906944",
"549755813888",
"1099511627776",
"2199023255552",
"4398046511104",
"8796093022208",
"17592186044416",
"35184372088832",
"70368744177664",
"140737488355328",
"281474976710656",
"562949953421312",
"1125899906842624",
"2251799813685248",
"4503599627370496",
"9007199254740992",
"18014398509481984",
"36028797018963968",
"72057594037927936",
"144115188075855872",
"288230376151711744",
"576460752303423488",
"1152921504606846976",
"2305843009213693952",
"4611686018427387904",
"9223372036854775808",
"18446744073709551616",
"36893488147419103232",
"73786976294838206464",
"147573952589676412928",
"295147905179352825856",
"590295810358705651712",
"1180591620717411303424",
"2361183241434822606848",
"4722366482869645213696",
"9444732965739290427392",
"18889465931478580854784",
"37778931862957161709568",
"75557863725914323419136",
"151115727451828646838272",
"302231454903657293676544",
"604462909807314587353088",
"1208925819614629174706176",
"2417851639229258349412352",
"4835703278458516698824704",
"9671406556917033397649408",
"19342813113834066795298816",
"38685626227668133590597632",
"77371252455336267181195264",
"154742504910672534362390528",
"309485009821345068724781056",
"618970019642690137449562112",
"1237940039285380274899124224",
"2475880078570760549798248448",
"4951760157141521099596496896",
"9903520314283042199192993792",
"19807040628566084398385987584",
"39614081257132168796771975168",
"79228162514264337593543950336",
"158456325028528675187087900672",
"316912650057057350374175801344",
"633825300114114700748351602688",
"1267650600228229401496703205376",
"2535301200456458802993406410752",
"5070602400912917605986812821504",
"10141204801825835211973625643008",
"20282409603651670423947251286016",
"40564819207303340847894502572032",
"81129638414606681695789005144064",
"162259276829213363391578010288128",
"324518553658426726783156020576256",
"649037107316853453566312041152512",
"1298074214633706907132624082305024",
"2596148429267413814265248164610048",
"5192296858534827628530496329220096",
"10384593717069655257060992658440192",
"20769187434139310514121985316880384",
"41538374868278621028243970633760768",
"83076749736557242056487941267521536",
"166153499473114484112975882535043072",
"332306998946228968225951765070086144",
"664613997892457936451903530140172288",
"1329227995784915872903807060280344576",
"2658455991569831745807614120560689152",
"5316911983139663491615228241121378304",
"10633823966279326983230456482242756608",
"21267647932558653966460912964485513216",
"42535295865117307932921825928971026432",
"85070591730234615865843651857942052864",
"170141183460469231731687303715884105728",
};
if (pow <= 127)
return pow2[pow];
return "";
}
static const char *ipv4_net_to_type(struct in_addr net)
{
unsigned byte1 = (ntohl(net.s_addr) >> 24) & 0xff;
unsigned byte2 = (ntohl(net.s_addr) >> 16) & 0xff;
unsigned byte3 = (ntohl(net.s_addr) >> 8) & 0xff;
unsigned byte4 = (ntohl(net.s_addr)) & 0xff;
/* based on IANA's iana-ipv4-special-registry and ipv4-address-space
* Updated: 2015-05-12
*/
if (byte1 == 0) {
return "This host on this network";
}
if (byte1 == 10) {
return "Private Use";
}
if (byte1 == 100 && (byte2 & 0xc0) == 64) {
return "Shared Address Space";
}
if (byte1 == 127) {
return "Loopback";
}
if (byte1 == 169 && byte2 == 254) {
return "Link Local";
}
if (byte1 == 172 && (byte2 & 0xf0) == 16) {
return "Private Use";
}
if (byte1 == 192 && byte2 == 0 && byte3 == 0) {
return "IETF Protocol Assignments";
}
if (byte1 == 192 && byte2 == 2 && byte3 == 0) {
return "Documentation (TEST-NET-1)";
}
if (byte1 == 192 && byte2 == 51 && byte3 == 100) {
return "Documentation (TEST-NET-2)";
}
if (byte1 == 203 && byte2 == 0 && byte3 == 113) {
return "Documentation (TEST-NET-3)";
}
if (byte1 == 192 && byte2 == 88 && byte3 == 99) {
return "6 to 4 Relay Anycast (Deprecated)";
}
if (byte1 == 192 && byte2 == 52 && byte3 == 193) {
return "AMT";
}
if (byte1 == 192 && byte2 == 168) {
return "Private Use";
}
if (byte1 == 255 && byte2 == 255 && byte3 == 255 && byte4 == 255) {
return "Limited Broadcast";
}
if (byte1 == 198 && (byte2 & 0xfe) == 18) {
return "Benchmarking";
}
if (byte1 >= 224 && byte1 <= 239) {
return "Multicast";
}
if ((byte1 & 0xf0) == 240) {
return "Reserved";
}
return "Internet";
}
static
const char *ipv4_net_to_class(struct in_addr net)
{
unsigned byte1 = (ntohl(net.s_addr) >> 24) & 0xff;
if (byte1 < 128) {
return "Class A";
}
if (byte1 >= 128 && byte1 < 192) {
return "Class B";
}
if (byte1 >= 192 && byte1 < 224) {
return "Class C";
}
if (byte1 >= 224 && byte1 < 239) {
return "Class D";
}
return "Class E";
}
static
unsigned default_ipv4_prefix(struct in_addr net)
{
unsigned byte1 = (ntohl(net.s_addr) >> 24) & 0xff;
if (byte1 < 128) {
return 8;
}
if (byte1 >= 128 && byte1 < 192) {
return 16;
}
if (byte1 >= 192 && byte1 < 224) {
return 24;
}
return 24;
}
char *ipv4_prefix_to_hosts(char *hosts, unsigned hosts_size, unsigned prefix)
{
unsigned tmp;
if (prefix >= 31) {
snprintf(hosts, hosts_size, "%s", p2_table(32 - prefix));
} else {
tmp = (1 << (32 - prefix)) - 2;
snprintf(hosts, hosts_size, "%u", tmp);
}
return hosts;
}
char *ipv6_prefix_to_hosts(char *hosts, unsigned hosts_size, unsigned prefix)
{
snprintf(hosts, hosts_size, "%s", p2_table(128 - prefix));
return hosts;
}
static
int get_ipv4_info(const char *ipStr, int prefix, ip_info_st * info,
unsigned flags)
{
struct in_addr ip, netmask, network, broadcast, minhost, maxhost;
char namebuf[INET_ADDRSTRLEN + 1];
char errBuf[250];
memset(info, 0, sizeof(*info));
if (inet_pton(AF_INET, ipStr, &ip) <= 0) {
if (!beSilent)
fprintf(stderr, "ipcalc: bad IPv4 address: %s\n",
ipStr);
return -1;
}
/* Handle CIDR entries such as 172/8 */
if (prefix >= 0) {
char *tmp = (char *)ipStr;
int i;
for (i = 3; i > 0; i--) {
tmp = strchr(tmp, '.');
if (!tmp)
break;
else
tmp++;
}
tmp = NULL;
for (; i > 0; i--) {
if (asprintf(&tmp, "%s.0", ipStr) == -1) {
fprintf(stderr,
"Memory allocation failure line %d\n",
__LINE__);
abort();
}
ipStr = tmp;
}
} else { /* assume good old days classful Internet */
if (flags & FLAG_ASSUME_CLASS_PREFIX)
prefix = default_ipv4_prefix(ip);
else
prefix = 32;
}
if (prefix > 32) {
if (!beSilent)
fprintf(stderr, "ipcalc: bad IPv4 prefix %d\n", prefix);
return -1;
}
if (inet_ntop(AF_INET, &ip, namebuf, sizeof(namebuf)) == 0) {
if (!beSilent)
fprintf(stderr,
"ipcalc: error calculating the IPv4 network\n");
return -1;
}
info->ip = safe_strdup(namebuf);
netmask.s_addr = prefix2mask(prefix);
memset(namebuf, '\0', sizeof(namebuf));
if (inet_ntop(AF_INET, &netmask, namebuf, INET_ADDRSTRLEN) == NULL) {
fprintf(stderr, "Memory allocation failure line %d\n",
__LINE__);
abort();
}
info->netmask = safe_strdup(namebuf);
info->prefix = prefix;
broadcast = calc_broadcast(ip, prefix);
memset(namebuf, '\0', sizeof(namebuf));
if (inet_ntop(AF_INET, &broadcast, namebuf, INET_ADDRSTRLEN) == NULL) {
fprintf(stderr, "Memory allocation failure line %d\n",
__LINE__);
abort();
}
info->broadcast = safe_strdup(namebuf);
network = calc_network(ip, prefix);
info->reverse_dns = calc_reverse_dns4(network, prefix, network, broadcast);
memset(namebuf, '\0', sizeof(namebuf));
if (inet_ntop(AF_INET, &network, namebuf, INET_ADDRSTRLEN) == NULL) {
fprintf(stderr, "Memory allocation failure line %d\n",
__LINE__);
abort();
}
info->network = safe_strdup(namebuf);
info->type = ipv4_net_to_type(network);
info->class = ipv4_net_to_class(network);
if (prefix < 32) {
memcpy(&minhost, &network, sizeof(minhost));
if (prefix <= 30)
minhost.s_addr = htonl(ntohl(minhost.s_addr) | 1);
if (inet_ntop(AF_INET, &minhost, namebuf, INET_ADDRSTRLEN) ==
NULL) {
fprintf(stderr, "Memory allocation failure line %d\n",
__LINE__);
abort();
}
info->hostmin = safe_strdup(namebuf);
memcpy(&maxhost, &network, sizeof(minhost));
maxhost.s_addr |= ~netmask.s_addr;
if (prefix <= 30) {
maxhost.s_addr = htonl(ntohl(maxhost.s_addr) - 1);
}
if (inet_ntop(AF_INET, &maxhost, namebuf, sizeof(namebuf)) == 0) {
if (!beSilent)
fprintf(stderr,
"ipcalc: error calculating the IPv4 network\n");
return -1;
}
info->hostmax = safe_strdup(namebuf);
} else {
info->hostmin = info->network;
info->hostmax = info->network;
}
ipv4_prefix_to_hosts(info->hosts, sizeof(info->hosts), prefix);
#if defined(USE_GEOIP) || defined(USE_MAXMIND)
if (flags & FLAG_GET_GEOIP) {
geo_ip_lookup(ipStr, &info->geoip_country, &info->geoip_ccode, &info->geoip_city, &info->geoip_coord);
}
#endif
if (flags & FLAG_RESOLVE_HOST) {
info->hostname = get_hostname(AF_INET, &ip);
if (info->hostname == NULL) {
if (!beSilent) {
sprintf(errBuf,
"ipcalc: cannot find hostname for %s",
ipStr);
herror(errBuf);
}
return -1;
}
}
return 0;
}
int ipv6_prefix_to_mask(unsigned prefix, struct in6_addr *mask)
{
struct in6_addr in6;
int i, j;
if (prefix > 128)
return -1;
memset(&in6, 0x0, sizeof(in6));
for (i = prefix, j = 0; i > 0; i -= 8, j++) {
if (i >= 8) {
in6.s6_addr[j] = 0xff;
} else {
in6.s6_addr[j] = (unsigned long)(0xffU << (8 - i));
}
}
memcpy(mask, &in6, sizeof(*mask));
return 0;
}
static char *ipv6_mask_to_str(const struct in6_addr *mask)
{
char buf[128];
if (inet_ntop(AF_INET6, mask, buf, sizeof(buf)) == NULL)
return NULL;
return safe_strdup(buf);
}
static const char *ipv6_net_to_type(struct in6_addr *net, int prefix)
{
uint16_t word1 = net->s6_addr[0] << 8 | net->s6_addr[1];
uint16_t word2 = net->s6_addr[2] << 8 | net->s6_addr[3];
/* based on IANA's iana-ipv6-special-registry and ipv6-address-space
* Updated: 2015-05-12
*/
if (prefix == 128 && memcmp
(net->s6_addr,
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01",
16) == 0)
return "Loopback Address";
if (prefix == 128 && memcmp
(net->s6_addr,
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
16) == 0)
return "Unspecified Address";
if (prefix >= 96 && memcmp
(net->s6_addr, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff",
12) == 0)
return "IPv4-mapped Address";
if (prefix >= 96 && memcmp
(net->s6_addr, "\x00\x64\xff\x9b\x00\x00\x00\x00\x00\x00\x00\x00",
12) == 0)
return "IPv4-IPv6 Translat.";
if (prefix >= 96 && memcmp
(net->s6_addr, "\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
12) == 0)
return "Discard-Only Address Block";
if ((word1 & 0xfffe) == 0x2001 && word2 == 0)
return "IETF Protocol Assignments";
if ((word1 & 0xe000) == 0x2000) {
return "Global Unicast";
}
if (((net->s6_addr[0] & 0xfe) == 0xfc)) {
return "Unique Local Unicast";
}
if ((word1 & 0xffc0) == 0xfe80) {
return "Link-Scoped Unicast";
}
if ((net->s6_addr[0] & 0xff) == 0xff) {
return "Multicast";
}
if ((word1 & 0xfffe) == 0x2002)
return "6to4";
return "Reserved";
}
static
char *expand_ipv6(struct in6_addr *ip6)
{
char buf[128];
char *p;
unsigned i;
p = buf;
for (i = 0; i < 16; i++) {
sprintf(p, "%.2x", (unsigned)ip6->s6_addr[i]);
p += 2;
if (i % 2 != 0 && i != 15) {
*p = ':';
p++;
}
}
*p = 0;
return safe_strdup(buf);
}
static
int get_ipv6_info(const char *ipStr, int prefix, ip_info_st * info,
unsigned flags)
{
struct in6_addr ip6, mask, network;
char errBuf[250];
unsigned i;
memset(info, 0, sizeof(*info));
if (inet_pton(AF_INET6, ipStr, &ip6) <= 0) {
if (!beSilent)
fprintf(stderr, "ipcalc: bad IPv6 address: %s\n",
ipStr);
return -1;
}
/* expand */
info->expanded_ip = expand_ipv6(&ip6);
if (inet_ntop(AF_INET6, &ip6, errBuf, sizeof(errBuf)) == 0) {
if (!beSilent)
fprintf(stderr,
"ipcalc: error calculating the IPv6 network\n");
return -1;
}
info->ip = safe_strdup(errBuf);
if (prefix > 128) {
if (!beSilent)
fprintf(stderr, "ipcalc: bad IPv6 prefix: %d\n",
prefix);
return -1;
} else if (prefix < 0) {
prefix = 128;
}
info->prefix = prefix;
if (ipv6_prefix_to_mask(prefix, &mask) == -1) {
if (!beSilent)
fprintf(stderr,
"ipcalc: error converting IPv6 prefix: %d\n",
prefix);
return -1;
}
info->netmask = ipv6_mask_to_str(&mask);
for (i = 0; i < sizeof(struct in6_addr); i++)
network.s6_addr[i] = ip6.s6_addr[i] & mask.s6_addr[i];
if (inet_ntop(AF_INET6, &network, errBuf, sizeof(errBuf)) == 0) {
if (!beSilent)
fprintf(stderr,
"ipcalc: error calculating the IPv6 network\n");
return -1;
}
info->network = safe_strdup(errBuf);
info->expanded_network = expand_ipv6(&network);
info->type = ipv6_net_to_type(&network, prefix);
info->reverse_dns = calc_reverse_dns6(&network, prefix);
if (prefix < 128) {
info->hostmin = safe_strdup(errBuf);
for (i = 0; i < sizeof(struct in6_addr); i++)
network.s6_addr[i] |= ~mask.s6_addr[i];
if (inet_ntop(AF_INET6, &network, errBuf, sizeof(errBuf)) == 0) {
if (!beSilent)
fprintf(stderr,
"ipcalc: error calculating the IPv6 network\n");
return -1;
}
info->hostmax = safe_strdup(errBuf);
} else {
info->hostmin = info->network;
info->hostmax = info->network;
}
ipv6_prefix_to_hosts(info->hosts, sizeof(info->hosts), prefix);
#if defined(USE_GEOIP) || defined(USE_MAXMIND)
if (flags & FLAG_GET_GEOIP) {
geo_ip_lookup(ipStr, &info->geoip_country, &info->geoip_ccode, &info->geoip_city, &info->geoip_coord);
}
#endif
if (flags & FLAG_RESOLVE_HOST) {
info->hostname = get_hostname(AF_INET6, &ip6);
if (info->hostname == NULL) {
if (!beSilent) {
sprintf(errBuf,
"ipcalc: cannot find hostname for %s",
ipStr);
herror(errBuf);
}
return -1;
}
}
return 0;
}
static int randomize(void *ptr, unsigned size)
{
int fd, ret;
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0)
return -1;
ret = read(fd, ptr, size);
close(fd);
if (ret != size) {
return -1;
}
return 0;
}
static char *generate_ip_network(int ipv6, unsigned prefix)
{
struct timespec ts;
char ipbuf[64];
char *p = NULL;
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) < 0)
return NULL;
if (ipv6) {
struct in6_addr net;
net.s6_addr[0] = 0xfc;
net.s6_addr[0] |= ts.tv_nsec & 1;
if (randomize(&net.s6_addr[1], 15) < 0)
return NULL;
if (inet_ntop(AF_INET6, &net, ipbuf, sizeof(ipbuf)) == NULL)
return NULL;
} else {
struct in_addr net;
unsigned c = ts.tv_nsec % 4;
uint8_t bytes[4];
if (randomize(bytes, 4) < 0)
return NULL;
if (prefix >= 16 && c < 2) {
if (c == 1) {
bytes[0] = 192;