-
Notifications
You must be signed in to change notification settings - Fork 1
/
net.php
158 lines (148 loc) · 4.04 KB
/
net.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
<?php
/**
Network utilities for the PHP Fat-Free Framework
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
Copyright (c) 2009-2012 F3::Factory
Bong Cosca <[email protected]>
@package Network
@version 2.0.12
**/
//! Network utilities
class Net extends Base {
/**
Send ICMP echo request to specified host; Return array containing
minimum/average/maximum round-trip time (in millisecs) and number of
packets received, or FALSE if host is unreachable
@return mixed
@param $addr string
@param $dns boolean
@param $count integer
@param $wait integer
@param $ttl integer
@public
**/
static function ping($addr,$dns=FALSE,$count=3,$wait=3,$ttl=30) {
// ICMP transmit socket
$tsocket=socket_create(AF_INET,SOCK_RAW,1);
// Set TTL
socket_set_option($tsocket,0,PHP_OS!='Linux'?4:2,$ttl);
// ICMP receive socket
$rsocket=socket_create(AF_INET,SOCK_RAW,1);
// Bind to all network interfaces
socket_bind($rsocket,0,0);
// Initialize counters
list($rtt,$rcv,$min,$max)=array(0,0,0,0);
for ($i=0;$i<$count;$i++) {
// Send ICMP header and payload
$data=uniqid();
$payload=self::hexbin('0800000000000000').$data;
// Recalculate ICMP checksum
if (strlen($payload)%2)
$payload.=self::hexbin('00');
$bits=unpack('n*',$payload);
$sum=array_sum($bits);
while ($sum>>16)
$sum=($sum>>16)+($sum&0xFFFF);
$payload=self::hexbin('0800').pack('n*',~$sum).
self::hexbin('00000000').$data;
// Transmit ICMP packet
@socket_sendto($tsocket,$payload,strlen($payload),0,$addr,0);
// Start timer
$time=microtime(TRUE);
$rset=array($rsocket);
$tset=NULL;
$xset=NULL;
// Wait for incoming ICMP packet
socket_select($rset,$tset,$xset,$wait);
if ($rset &&
@socket_recvfrom($rsocket,$reply,255,0,$host,$port)) {
$elapsed=1e3*(microtime(TRUE)-$time);
// Socket didn't timeout; Record round-trip time
$rtt+=$elapsed;
if ($elapsed>$max)
$max=$elapsed;
if (!($min>0) || $elapsed<$min)
$min=$elapsed;
// Count packets received
$rcv++;
if ($host)
$addr=$host;
}
}
socket_close($tsocket);
socket_close($rsocket);
return $rcv?
array(
'host'=>$dns?gethostbyaddr($addr):$addr,
'min'=>(int)round($min),
'max'=>(int)round($max),
'avg'=>(int)round($rtt/$rcv),
'packets'=>$rcv
):
FALSE;
}
/**
Return the path taken by packets to a specified network destination
@return array
@param $addr string
@param $dns boolean
@param $wait integer
@param $hops integer
@public
**/
static function traceroute($addr,$dns=FALSE,$wait=3,$hops=30) {
$route=array();
for ($i=0;$i<$hops;$i++) {
set_time_limit(ini_get('default_socket_timeout'));
$result=self::ping($addr,$dns,3,$wait,$i+1);
$route[]=$result;
if (gethostbyname($result['host'])==gethostbyname($addr))
break;
}
return $route;
}
/**
Retrieve information from whois server
@return string
@param $addr
@public
**/
static function whois($addr) {
$socket=@fsockopen(self::$vars['WHOIS'],43,$errno,$errstr);
if (!$socket) {
// Can't establish connection
trigger_error($errstr);
return FALSE;
}
// Set connection timeout parameters
stream_set_blocking($socket,TRUE);
stream_set_timeout($socket,ini_get('default_socket_timeout'));
// Send request
fputs($socket,$addr."\r\n");
$info=stream_get_meta_data($socket);
// Get response
$response='';
while (!feof($socket) && !$info['timed_out']) {
$response.=fgets($socket,4096); // MDFK97
$info=stream_get_meta_data($socket);
}
fclose($socket);
if ($info['timed_out']) {
trigger_error(self::TEXT_Timeout);
return FALSE;
}
return $response;
}
/**
Class initializer
@public
**/
static function onload() {
if (!extension_loaded('sockets'))
// Sockets extension required
trigger_error(sprintf(self::TEXT_PHPExt,'sockets'));
}
}