-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator_tools.php
102 lines (82 loc) · 2.35 KB
/
calculator_tools.php
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
<?php
/**
* Convert an IPv6 from human readable format to a hexadecimal array
*
* @param string human readable IPv6 address
* @return array IPv6 address in hex (8 groups of 4 hex digits)
*/
function human_to_hex( $human_ip )
{
// Convert the human readable form to a string of 16 8-bit ASCII characters
$addr = inet_pton($human_ip);
// Convert from ascii to decimal to hexadecimal and concatenate
$hex_string = '';
foreach( str_split($addr) as $char)
{
$hex_string .= str_pad( dechex(ord($char)), 2, '0', STR_PAD_LEFT );
}
// Split the string into an array of 8 groups of 16-bit values
// with each group represented as 4 hexadecimal digits
return str_split( $hex_string, 4 );
}
/**
* Convert an IPv6 hex array to human readable format
*
* @param array IPv6 address in hex (8 groups of 4 hex digits)
* @return string human readable IPv6 address
*/
function hex_to_human( $hex_ip )
{
// Convert array to string and make sure each unit has 4 digits
$hex_string = '';
foreach( $hex_ip as $unit )
{
$hex_string .= str_pad( $unit, 4, '0', STR_PAD_LEFT );
}
// Convert to array of 16 8-bit groups, currently represented as 2 hex digits
$hex_array = str_split($hex_string, 2);
// Convert to a string of 16 8-bit ASCII characters
$addr = '';
foreach( $hex_array as $char )
{
$addr .= chr( hexdec($char) );
}
// Convert to human readable format
return inet_ntop( $addr );
}
/**
* Print the network in tree form
*
* @param Network The network to print
* @return void
*/
function print_network( $network )
{
echo $network->position;
$indent = substr_count($network->position, '.');
for( $i=0; $i<$indent; $i++ )
echo ' ';
echo hex_to_human($network->ip) . '/' . $network->prefix . "\n";
foreach( $network->subnets as $subnet )
print_network($subnet);
}
/**
* Turn a network tree into html
*
* @param Network The network to print
* @return void
*/
function print_html( $network )
{
echo '<div class="branch" style="display: none;">';
echo $network->position;
$indent = substr_count($network->position, '.') * 2;
for( $i=0; $i<$indent; $i++ )
echo ' ';
if ( count($network->subnets) )
echo '<a href="#" class="open-children">+</a>';
echo hex_to_human($network->ip) . '/' . $network->prefix . "\n";
foreach( $network->subnets as $subnet )
$network_array['subnets'][] = print_html($subnet);
echo '</div>';
}