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
|
<?php
/*
* Looking Glass - An easy to deploy Looking Glass
* Copyright (C) 2014 Guillaume Mazoyer <gmazoyer@gravitons.in>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once 'config.php';
/**
* Test if a given parameter is an IPv4 or not.
*
* @param string $ip the parameter to test.
* @param boolean $ip_only optional parameter, if omitted or set to true, it
* will ensure that the first parameter is an IPv4
* address without mask, if set to false the IP/MASK
* form is considered as valid.
* @return boolean true if the parameter matches an IPv4 address, false
* otherwise.
*/
function match_ipv4($ip, $ip_only = true) {
if (strrpos($ip, '/') && !$ip_only) {
$ip_and_mask = explode('/', $ip, 2);
return filter_var($ip_and_mask[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) &&
filter_var($ip_and_mask[1], FILTER_VALIDATE_INT);
} else {
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
}
/**
* Test if a given parameter is an IPv6 or not.
*
* @param string $ip the parameter to test.
* @param boolean $ip_only optional parameter, if omitted or set to true, it
* will ensure that the first parameter is an IPv6
* address without mask, if set to false the IP/MASK
* form is considered as valid.
* @return boolean true if the parameter matches an IPv6 address, false
* otherwise.
*/
function match_ipv6($ip, $ip_only = true) {
if (strrpos($ip, '/') && !$ip_only) {
$ip_and_mask = explode('/', $ip, 2);
return filter_var($ip_and_mask[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) &&
filter_var($ip_and_mask[1], FILTER_VALIDATE_INT);
} else {
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
}
}
/**
* Test if a given parameter is a valid FQDN or not.
*
* @param string $fqdn the parameter to test.
* @return boolean true if the parameter matches a valid FQDN, false otherwise.
*/
function match_fqdn($fqdn) {
$regex = '/(?=^.{4,255}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)/';
if ((preg_match($regex, $fqdn) === false) ||
(preg_match($regex, $fqdn) === 0)) {
return false;
} else {
return true;
}
}
/**
* Test if a given parameter is a valid AS number or not.
*
* @param integer $as the parameter to test.
* @return boolean true if the parameter matches a valid ASN, false otherwise.
*/
function match_as($as) {
global $config;
$options_wide_range = array(
'options' => array('min_range' => 1, 'max_range' => 4294967294)
);
$options_16bit_private = array(
'options' => array( 'min_range' => 64512, 'max_range' => 65534)
);
$options_32bit_private = array(
'options' => array('min_range' => 4200000000, 'max_range' => 4294967294)
);
if (!filter_var($as, FILTER_VALIDATE_INT, $options_wide_range)) {
return false;
}
if (filter_var($as, FILTER_VALIDATE_INT, $options_16bit_private)) {
return isset($config['misc']['allow_private_asn']) &&
$config['misc']['allow_private_asn'] == true;
}
if (filter_var($as, FILTER_VALIDATE_INT, $options_32bit_private)) {
return isset($config['misc']['allow_private_asn']) &&
$config['misc']['allow_private_asn'] == true;
}
return true;
}
function match_aspath_regex($aspath_regex) {
// TODO: validate a regex with a regex?
return true;
}
/**
* For a given FQDN try to find the corresponding IPv4 or IPv6 address.
*
* If there is multiple addresses attached to the same FQDN it will give the
* first IPv4 or IPv6 found.
*
* If the FQDN have both IPv4 and IPv6 addresses it will give the first IPv6
* address found.
*
* @param string $fqdn the FQDN to use to search in the DNS databases.
* @return string an IPv4 or IPv6 address based on the DNS records.
*/
function fqdn_to_ip_address($fqdn) {
$dns_record = dns_get_record($fqdn, DNS_A + DNS_AAAA);
// No DNS record found
if (!$dns_record) {
return false;
}
$records_nb = count($dns_record);
// Only one record found
if ($records_nb == 1) {
if ($dns_record[0]['type'] == 'AAAA') {
return $dns_record[0]['ipv6'];
} else if ($dns_record[0]['type'] == 'A') {
return $dns_record[0]['ip'];
} else {
return false;
}
}
// Several records found
if ($records_nb > 1) {
// TODO: this could probably be more optimal
foreach ($dns_record as $record) {
if ($record['type'] == 'AAAA') {
return $record['ipv6'];
}
}
foreach ($dns_record as $record) {
if ($record['type'] == 'AAA') {
return $record['ipv4'];
}
}
return false;
}
}
/**
* Send the given parameter to the logs file.
*
* @param string $log the log to be recorded in the logs file.
*/
function log_to_file($log) {
global $config;
$log = '['.date("Y-m-d H:i:s").'] '.$log."\n";
file_put_contents($config['misc']['logs'], $log, FILE_APPEND | LOCK_EX);
}
// End of utils.php
|