-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipaddr-to-cidr-worker.php
161 lines (117 loc) · 6.64 KB
/
ipaddr-to-cidr-worker.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
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
<?php
/*
Filename: ipaddr-to-cidr-worker.php
Rev 2015.0309.0110
Project: ip-addr-to-arin-cidr
Copyright (C) Rex Addiscentis - raddiscentis@addiscent.com
Description:
The IPaddrToCIDRworker class is responsible for requesting data from
the ARIN Whois-RWS API, for a specific IP address. The data is fetched
via the HTTPRequest class. The data received is an XML object. The
IPaddrToCIDRworker parses the desired information from the XML object,
and stores it in the CIDRdataSet class.
For more information about re-using the source code in this product, see
the other project .php files.
License:
The license under which this software product is released is GPLv2.
Disclaimer:
Some of the source code used in this product may have been re-used from
other sources.
None of the source code in this product, original or derived, has been
surveyed for vulnerabilities to potential security risks.
Use at your own risk, author is not liable for damages, product is not
warranted to be useful for anything, copyrights held by their respective
owners.
*/
// status returned by class IPaddrToCIDRworker
define ("STATUS_SUCCESS", TRUE);
define ("STATUS_FAIL", FALSE);
class IPaddrToCIDRworker
{
// Remove undesired chars from the beginning of $in_string.
// $in_string: string to remove chars from, (left end)
// $strstr_arg: remove all chars from left of $in_string, up to,
// but not including, $strstr_arg. Note: strstr() leaves $strstr_arg
// match not removed, must use ltrim() to remove it
private function ltrim_to_data ($in_string, $strstr_arg) {
$str_remain = strstr($in_string, $strstr_arg);
$str_remain = ltrim($str_remain, $strstr_arg);
return $str_remain;
}
//
public function get_cidr_data_result ($ipaddr = NULL, &$CIDRdataSet = NULL) {
// query ARIN for net block data of specified IP address
$result = new HTTPRequest ('http://whois.arin.net/rest/ip/' . $ipaddr);
$body_string = $result->DownloadToString();
// search for cidrLength first because it is encountered before associated startAddress
// find cidrLength by discarding string segment up to and including "cidrLength>"
$string_remainder = $this->ltrim_to_data ($body_string, "cidrLength>");
// save the cidrlength ---------------------------------------
$cidr_length = strstr($string_remainder, "<", TRUE);
// There must be at least one CIDR to build. Bug out if $cidr_length is invalid
if (($cidr_length == "") || ($cidr_length > "32") || ($cidr_length < "1"))
return STATUS_FAIL;
// find block start IP address by discarding string
// segment up to and including "startAddress>"
$string_remainder = $this->ltrim_to_data ($string_remainder, "startAddress>");
// save the block start IP address ---------------------------
$address_range_start = strstr($string_remainder, "<", TRUE);
// bug out if IP startAddress not found
if ($address_range_start == "")
return STATUS_FAIL;
// stash sthe startAddress and cidrLength we've found thus far
$CIDRdataSet->add_cidr ($address_range_start, $cidr_length);
$CIDRdataSet->set_ipaddr ($ipaddr);
$CIDRdataSet->set_cidr_date_time (date('M d, Y H:i') . " GMT/UTC");
// there may be more than one net block. Keep searching until no more are found
$netblock_found = TRUE;
do {
// find CIDRlength by discarding string segment up to and including "cidrLength>"
$string_remainder = $this->ltrim_to_data ($string_remainder, "cidrLength>");
// save the cidrlength ---------------------------------------
$cidr_length = strstr($string_remainder, "<", TRUE);
// exit loop if no more valid cidrLengths found
if (($cidr_length == "") || ($cidr_length > "32") || ($cidr_length < "1"))
break;
// find block start IP address by discarding string
// segment up to and including "startAddress>"
$string_remainder = $this->ltrim_to_data ($string_remainder, "startAddress>");
// save the range start IP address ---------------------------
$address_range_start = strstr($string_remainder, "<", TRUE);
// exit loop if no more valid data found
if ($address_range_start == "")
break;
$CIDRdataSet->add_cidr ($address_range_start, $cidr_length);
} while ($netblock_found == TRUE);
// find organization name by discarding string segment up to
// and including 'orgRef name="'
$string_remainder = $this->ltrim_to_data ($body_string, 'orgRef name="');
// save 'orgRef name"' if it was found
if ($string_remainder != "") { // orgRef name=" found
// save the organization name -----------------------------
$company_name = strstr($string_remainder, '"', TRUE);
// find organization handle by discarding string
// segment up to and including 'handle="'
$string_remainder = $this->ltrim_to_data ($string_remainder, 'handle="');
// save the organization handle ----------------------------
$company_handle = strstr($string_remainder, '"', TRUE);
$CIDRdataSet->set_company_info ($company_name, $company_handle);
}
// find customer name by discarding string
// segment up to and including 'customerRef name="'
$string_remainder = $this->ltrim_to_data ($body_string, 'customerRef name="');
// save 'customerRef name"' if it was found
if ($string_remainder != "") { // customerRef name=" found
// save the customer name --------------------------------
$customer_name = strstr($string_remainder, '"', TRUE);
// find customer handle by discarding string
// segment up to and including 'handle="'
$string_remainder = $this->ltrim_to_data ($string_remainder, 'handle="');
// save the customer handle ------------------------------
$customer_handle = strstr($string_remainder, '"', TRUE);
$CIDRdataSet->set_customer_info ($customer_name, $customer_handle);
}
return STATUS_SUCCESS;
}
}
?>