/** * scans the local network for upnp devices * * @return array with discovered devices */ function scan() { $this->checkCredentials(); $result = array(); $buffer = ''; $tmp = ''; $headers = "M-SEARCH * HTTP/1.1\r\nHost:239.255.255.250:1900\r\nST:" . $this->type . "\r\nMan:\"ssdp:discover\"\r\nMX:3\r\n\r\n"; $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 10000)); $i = 0; socket_sendto($socket, $headers, 1024, 0, '239.255.255.250', 1900); while (@socket_recvfrom($socket, $buffer, 1024, MSG_WAITALL, $tmp, $mp)) { $i++; $res = $this->decode($buffer); if (isset($res['location'])) { $res_details = $this->getDetails($res['location']); if ($res_details[0] == true) { $res['details'] = $res_details[1]; } else { $res['details'] = false; } } $result[] = $res; } socket_close($socket); return array(true, $result); }
function wol($host, $mac) { //get the broadcast addr $range = $this->iprange($host, 24); $broadcast = $range['last_ip']; $mac_array = explode(':', $mac); $hwaddr = ''; foreach ($mac_array as $octet) { $hwaddr .= chr(hexdec($octet)); } // Create Magic Packet $packet = ''; for ($i = 1; $i <= 6; $i++) { $packet .= chr(255); } for ($i = 1; $i <= 16; $i++) { $packet .= $hwaddr; } $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if ($sock) { $options = socket_set_option($sock, 1, 6, true); if ($options >= 0) { $e = socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 9); socket_close($sock); } } }
function processPacket($packetData) { $packet = new dhcpPacket(); $packet->parse($packetData); $processor = new dhcpRequestProcessor($this, $this->packetProcessor, $this->storage, $packet); if ($responsePacket = $processor->getResponse()) { $responseData = $responsePacket->build(); $this->verbosity && (print "Sending response" . "\n"); $ciaddr = $packet->getClientAddress(); if ($ciaddr == '0.0.0.0') { $this->verbosity && (print "Switching to broadcast address...\n"); $ciaddr = '255.255.255.255'; } $this->verbosity && (print "Attempting to send response packet to " . $ciaddr . "\n"); $numBytesSent = socket_sendto($this->socket, $responseData, strlen($responseData), 0, $ciaddr, 68); if ($numBytesSent === FALSE) { $this->verbosity && (print "send failed for specific address, broadcast.\n"); $numBytesSent = socket_sendto($this->socket, $responseData, strlen($responseData), 0, "255.255.255.255", 68); $numBytesSent === FALSE && $this->verbosity && printf('socket send error: %s\\n', socket_strerror(socket_last_error($this->socket))); } $numBytesSent && $this->verbosity && (print "Response packet sent.\n"); } else { $this->verbosity && (print "Packet ignored\n"); } }
/** * 送信する * @param string $buffer */ public function send($buffer) { $result = socket_sendto($this->resource, $buffer, strlen($buffer), $this->flags, $this->address, $this->port); if ($result === false) { throw new \RuntimeException('send fail'); } }
/** * Squirt the metrics over UDP * * @param array $data Incoming Data * @param int $sampleRate the rate (0-1) for sampling. * @param array|string $tags Key Value array of Tag => Value, or single tag as string * * @return null */ private function flush($data, $sampleRate = 1, array $tags = null) { // sampling $sampledData = array(); if ($sampleRate < 1) { foreach ($data as $stat => $value) { if (mt_rand() / mt_getrandmax() <= $sampleRate) { $sampledData[$stat] = "{$value}|@{$sampleRate}"; } } } else { $sampledData = $data; } if (empty($sampledData)) { return; } foreach ($sampledData as $stat => $value) { if ($tags !== NULL && is_array($tags) && count($tags) > 0) { $value .= '|#'; foreach ($tags as $tag_key => $tag_val) { $value .= $tag_key . ':' . $tag_val . ','; } $value = substr($value, 0, -1); } elseif (isset($tags) && !empty($tags)) { $value .= '|#' . $tags; } $message = "{$stat}:{$value}"; // Non - Blocking UDP I/O - Use IP Addresses! $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_set_nonblock($socket); socket_sendto($socket, $message, strlen($message), 0, $this->server, $this->port); socket_close($socket); } }
public function logData() { global $wgUDPProfilerHost, $wgUDPProfilerPort, $wgUDPProfilerFormatString; $this->close(); if (!function_exists('socket_create')) { # Sockets are not enabled return; } $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $plength = 0; $packet = ""; foreach ($this->mCollated as $entry => $pfdata) { if (!isset($pfdata['count']) || !isset($pfdata['cpu']) || !isset($pfdata['cpu_sq']) || !isset($pfdata['real']) || !isset($pfdata['real_sq'])) { continue; } $pfline = sprintf($wgUDPProfilerFormatString, $this->getProfileID(), $pfdata['count'], $pfdata['cpu'], $pfdata['cpu_sq'], $pfdata['real'], $pfdata['real_sq'], $entry, $pfdata['memory']); $length = strlen($pfline); /* printf("<!-- $pfline -->"); */ if ($length + $plength > 1400) { socket_sendto($sock, $packet, $plength, 0, $wgUDPProfilerHost, $wgUDPProfilerPort); $packet = ""; $plength = 0; } $packet .= $pfline; $plength += $length; } socket_sendto($sock, $packet, $plength, 0x100, $wgUDPProfilerHost, $wgUDPProfilerPort); }
function sendMessage($udp_message) { $message = vsprintf(str_repeat('%c', count($udp_message)), $udp_message); global $server_ip, $server_port, $delay, $debug, $error_value, $ok_value; if ($debug) { echo '<b>remote host/IP:</b> ' . $server_ip . '<br/>'; echo '<b>remote port:</b> ' . $server_port . '<br/>'; echo '<b>UDP message:</b> '; foreach ($udp_message as $key => $value) { echo '0x'; printf("%02x\n", $value); } echo '<br/>'; echo '<b>result:</b> '; } if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) { socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port); sleep($delay); socket_close($socket); echo $ok_value; } else { echo $error_value; } if ($debug) { echo '<br/>'; echo '---------- debug info ----------<br/><br/>'; } }
function WakeOnLan($addr, $mac_address, $socket_number) { $addr_byte = explode(':', $mac_address); $hw_addr = ''; for ($a = 0; $a < 6; $a++) { $hw_addr .= chr(hexdec($addr_byte[$a])); $msg = chr(255) . chr(255) . chr(255) . chr(255) . chr(255) . chr(255); for ($a = 1; $a <= 16; $a++) { $msg .= $hw_addr; // send it to the broadcast address using UDP // SQL_BROADCAST option isn't help!! $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if ($s == false) { echo "Error creating socket!\n"; echo "Error code is '" . socket_last_error($s) . "' - " . socket_strerror(socket_last_error($s)); return FALSE; } else { // setting a broadcast option to socket: $opt_ret = socket_set_option($s, 1, 6, TRUE); if ($opt_ret < 0) { echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n"; return FALSE; } if (socket_sendto($s, $msg, strlen($msg), 0, $ip_address, $socket_number)) { echo "Magic Packet sent successfully!"; socket_close($s); return TRUE; } else { echo "Magic packet failed!"; return FALSE; } } } } }
/** * Does a TFTP Check by connecting to $host looking for $filename * @author http://www.php.net/manual/en/function.socket-create.php#43057 * @param string $host * @param string $filename * @return mixed file contents */ function tftp_fetch($host, $filename) { //first off let's check if this is installed or disabled $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // create the request packet $packet = chr(0) . chr(1) . $filename . chr(0) . 'octet' . chr(0); // UDP is connectionless, so we just send on it. socket_sendto($socket, $packet, strlen($packet), 0x100, $host, 69); $buffer = ''; $port = ''; $ret = ''; $time = time(); do { $new_time = time() - $time; if ($new_time > 5) { break; } // $buffer and $port both come back with information for the ack // 516 = 4 bytes for the header + 512 bytes of data socket_recvfrom($socket, $buffer, 516, 0, $host, $port); // add the block number from the data packet to the ack packet $packet = chr(0) . chr(4) . substr($buffer, 2, 2); // send ack socket_sendto($socket, $packet, strlen($packet), 0, $host, $port); // append the data to the return variable // for large files this function should take a file handle as an arg $ret .= substr($buffer, 4); } while (strlen($buffer) == 516); // the first non-full packet is the last. return $ret; }
/** * Perform an M-SEARCH multicast request for detecting UPnP-devices in network. * * @todo Allow unicasting. * @todo Sort arguments better. */ public function mSearch($st = 'ssdp:all', $mx = 2, $man = 'ssdp:discover', $from = null, $port = null, $sockTimout = '5') { // BUILD MESSAGE $msg = 'M-SEARCH * HTTP/1.1' . "\r\n"; $msg .= 'HOST: 239.255.255.250:1900' . "\r\n"; $msg .= 'MAN: "' . $man . '"' . "\r\n"; $msg .= 'MX: ' . $mx . "\r\n"; $msg .= 'ST:' . $st . "\r\n"; $msg .= 'USER-AGENT: ' . static::USER_AGENT . "\r\n"; $msg .= '' . "\r\n"; // MULTICAST MESSAGE $sock = socket_create(AF_INET, SOCK_DGRAM, 0); $opt_ret = socket_set_option($sock, 1, 6, TRUE); $send_ret = socket_sendto($sock, $msg, strlen($msg), 0, '239.255.255.250', 1900); // SET TIMEOUT FOR RECIEVE socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sockTimout, 'usec' => '0')); // RECIEVE RESPONSE $response = array(); do { $buf = null; @socket_recvfrom($sock, $buf, 1024, MSG_WAITALL, $from, $port); if (!is_null($buf)) { $response[] = $this->parseMSearchResponse($buf); } } while (!is_null($buf)); // CLOSE SOCKET socket_close($sock); return $response; }
/** * Send data via UDP * * @param ProfilerData $data */ public function send(ProfilerData $data) { list($host, $port) = $this->getEndpoint($data->getEngine()); if (!$host || !$port) { return; } $profile = $data->getProfile(); $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $plength = 0; $packet = ""; foreach ($data->getEntries() as $name => $pfdata) { foreach (array('cpu', 'cpu_sq', 'real', 'real_sq') as $key) { if (!isset($pfdata[$key])) { $pfdata[$key] = -1; } } $pfline = sprintf("%s %s %d %f %f %f %f %s\n", $profile, "-", $pfdata['count'], $pfdata['cpu'], $pfdata['cpu_sq'], $pfdata['real'], $pfdata['real_sq'], $name); $length = strlen($pfline); if ($length + $plength > self::LESS_THAN_MTU) { socket_sendto($sock, $packet, $plength, 0, $host, $port); $packet = ""; $plength = 0; } $packet .= $pfline; $plength += $length; } socket_sendto($sock, $packet, $plength, 0x100, $host, $port); }
function getFunctionReport() { global $wgUDPProfilerHost; global $wgUDPProfilerPort; if ($this->mCollated['-total']['real'] < $this->mMinimumTime) { # Less than minimum, ignore return; } $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $plength = 0; $packet = ""; foreach ($this->mCollated as $entry => $pfdata) { $pfline = sprintf("%s %s %d %f %f %f %f %s\n", $this->getProfileID(), "-", $pfdata['count'], $pfdata['cpu'], $pfdata['cpu_sq'], $pfdata['real'], $pfdata['real_sq'], $entry); $length = strlen($pfline); /* printf("<!-- $pfline -->"); */ if ($length + $plength > 1400) { socket_sendto($sock, $packet, $plength, 0, $wgUDPProfilerHost, $wgUDPProfilerPort); $packet = ""; $plength = 0; } $packet .= $pfline; $plength += $length; } socket_sendto($sock, $packet, $plength, 0x100, $wgUDPProfilerHost, $wgUDPProfilerPort); }
public function search($st = 'ssdp:all', $mx = 2, $man = 'ssdp:discover', $from = null, $port = null, $sockTimout = '2') { $request = 'M-SEARCH * HTTP/1.1' . "\r\n"; $request .= 'HOST: 239.255.255.250:1900' . "\r\n"; $request .= 'MAN: "' . $man . '"' . "\r\n"; $request .= 'MX: ' . $mx . '' . "\r\n"; $request .= 'ST: ' . $st . '' . "\r\n"; $request .= 'USER-AGENT: ' . $this->user_agent . "\r\n"; $request .= "\r\n"; $socket = socket_create(AF_INET, SOCK_DGRAM, 0); socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, true); socket_sendto($socket, $request, strlen($request), 0, '239.255.255.250', 1900); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $sockTimout, 'usec' => '0')); $response = array(); do { $buf = null; socket_recvfrom($socket, $buf, 1024, MSG_WAITALL, $from, $port); if (!is_null($buf)) { $data = $this->parseSearchResponse($buf); $response[$data['usn']] = $data; } } while (!is_null($buf)); socket_close($socket); return $response; }
private function get_peers_blocking($info_hash, $host = "router.bittorrent.com", $port = 6881) { //create a UDP socket to send commands through $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); //Create Command Packet $packet = bencode::encode(array("id" => $this->get_unique_node_id(), "info_hash" => hex2bin($info_hash)), array("q" => "get_peers", "t" => $this->unique_id(), "y" => "q")); socket_sendto($socket, $packet, strlen($packet), 0, $host, $port); //set timeout $timeout = array('sec' => 5, 'usec' => 0); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, $timeout); $time = time(); //recieve data try { socket_recvfrom($socket, $buf, 12000, 0, $host, $port); } catch (Exception $e) { echo "Error"; return FALSE; } //have to manually do the timeout, cant seem to get info from this socket if (time() - $time >= 4) { socket_close($socket); return FALSE; } //close socket so bad shit don't happen socket_close($socket); return nodeExtract::return_nodes(bencode::decode($buf)); }
public function logData() { global $wgUDPProfilerHost, $wgUDPProfilerPort; $this->close(); if (isset($this->mCollated['-total']) && $this->mCollated['-total']['real'] < $this->mMinimumTime) { # Less than minimum, ignore return; } if (!MWInit::functionExists('socket_create')) { # Sockets are not enabled return; } $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); $plength = 0; $packet = ""; foreach ($this->mCollated as $entry => $pfdata) { if (!isset($pfdata['count']) || !isset($pfdata['cpu']) || !isset($pfdata['cpu_sq']) || !isset($pfdata['real']) || !isset($pfdata['real_sq'])) { continue; } $pfline = sprintf("%s %s %d %f %f %f %f %s\n", $this->getProfileID(), "-", $pfdata['count'], $pfdata['cpu'], $pfdata['cpu_sq'], $pfdata['real'], $pfdata['real_sq'], $entry); $length = strlen($pfline); /* printf("<!-- $pfline -->"); */ if ($length + $plength > 1400) { socket_sendto($sock, $packet, $plength, 0, $wgUDPProfilerHost, $wgUDPProfilerPort); $packet = ""; $plength = 0; } $packet .= $pfline; $plength += $length; } socket_sendto($sock, $packet, $plength, 0x100, $wgUDPProfilerHost, $wgUDPProfilerPort); }
/** * @param string $data * @return int|null Number of bytes written */ public function write($data) { if (!$this->socket) { $this->open(); } return socket_sendto($this->socket, $data, strlen($data), 0, $this->host, $this->port); }
function wol($mac, $broadcast) { $mac_array = split(':', $mac); $hwaddr = ''; //Agafem cada octet de la MAC. foreach ($mac_array as $octet) { $hwaddr .= chr(hexdec($octet)); } //Creem el Magic Packet que rebrà la targeta de xarxa solicitada. $packet = ''; for ($i = 1; $i <= 6; $i++) { $packet .= chr(255); } for ($i = 1; $i <= 16; $i++) { $packet .= $hwaddr; } //Creem el socket. $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if ($sock) { $options = socket_set_option($sock, 1, 6, true); if ($options >= 0) { $e = socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 7); //L'enviem. socket_close($sock); } } return "WOL sended: {$mac}"; }
protected function send($chunk) { if (!is_resource($this->socket)) { throw new \LogicException('The UdpSocket to ' . $this->ip . ':' . $this->port . ' has been closed and can not be written to anymore'); } socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port); }
static function send($broadcast, $mac) { $mac_array = split(':', $mac); $hwaddr = ''; foreach ($mac_array as $octet) { $hwaddr .= chr(hexdec($octet)); } // Create Magic Packet $packet = ''; for ($i = 1; $i <= 6; $i++) { $packet .= chr(255); } for ($i = 1; $i <= 16; $i++) { $packet .= $hwaddr; } $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if ($sock) { $options = socket_set_option($sock, 1, 6, true); if ($options >= 0) { echo "sended"; $e = socket_sendto($sock, $packet, strlen($packet), 0, $broadcast, 7); socket_close($sock); } } }
private function send($message) { //BY UDP socket_sendto($this->connection, $message, strlen($message), 0, Yii::app()->params['graphite_server_ip'], Yii::app()->params['graphite_server_port_udp']); //BY TCP // socket_write($this->connection, $message); }
public function write($data, $dest, $port) { if ($this->connected === false) { return false; } return @socket_sendto($this->sock, $data, strlen($data), 0, $dest, $port); }
public function index() { error_reporting(~E_WARNING); $server = '127.0.0.1'; $port = 9999; if (!($sock = socket_create(AF_INET, SOCK_DGRAM, 0))) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("Couldn't create socket: [{$errorcode}] {$errormsg} \n"); } echo "Socket created \n"; echo 'Enter a message to send : '; $input = 'abc'; //Send the message to the server if (!socket_sendto($sock, $input, strlen($input), 0, $server, $port)) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("Could not send data: [{$errorcode}] {$errormsg} \n"); } //Now receive reply from server and print it if (socket_recv($sock, $reply, 5, 256) === false) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); die("Could not receive data: [{$errorcode}] {$errormsg} \n"); } echo "Reply : {$reply}"; }
public function wake($mac, $ip) { global $l; //looking for values of wol config $wol_info = look_config_default_values('WOL_PORT'); if (!isset($wol_info['name']['WOL_PORT'])) { $this->wol_send = $l->g(1321); } else { $wol_port = explode(',', $wol_info['tvalue']['WOL_PORT']); } foreach ($wol_port as $k => $v) { if (is_numeric($v)) { $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if (!$s) { @socket_close($s); $this->wol_send = $l->g(1322); } else { $s_opt = socket_set_option($s, SOL_SOCKET, SO_BROADCAST, true); socket_sendto($s, $this->pacquet($mac), strlen($this->pacquet($mac)), 0, "255.255.255.255", $v); socket_close($s); $this->wol_send = $l->g(1282); } } } // $this->wol_send='toto'; //return $wol_send; }
public function send($data) { if (!$this->socket) { throw new \Exception('Stream not opened'); } socket_sendto($this->socket, $data, strlen($data), 0, $this->host, $this->port); return true; }
function trace($message) { global $trace; if (strlen($message) == 0) { return; } @socket_sendto($trace, $message, strlen($message), 0, 'localhost', 7535); }
private function smi_write($input) { $len = strlen($input); if (socket_sendto($this->sock, $input, $len, 0, $this->raddr, $this->rport) == false) { return false; } return true; }
/** * Writes a message * * @param mixed $logData * @return mixed * @throws \Exception */ public function send($logData) { $this->open(); $chunk = $this->header . substr($logData, 0, self::DATAGRAM_MAX_LENGTH - strlen($this->header)); socket_sendto($this->socket, $chunk, strlen($chunk), 0, $this->address, $this->port); $this->close(); return true; }
private static function udpSend($buf, $delay = 15, $host = "239.255.255.250", $port = 1900) { $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1); socket_sendto($socket, $buf, strlen($buf), 0, $host, $port); socket_close($socket); usleep($delay * 1000); }
/** * @param $dgram * @param $hostname * @param $port * * @return int */ public function send($dgram, $hostname, $port) { $sent = socket_sendto($this->socket, $dgram, strlen($dgram), 0, $hostname, $port); if ($sent === false) { throw new SocketException("Socket unavailable"); } return $sent; }
public function sendBroadcastUDP($data, $broadcastIP, $port) { $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, true); $sentBytes = socket_sendto($socket, $data, strlen($data), MSG_DONTROUTE, $broadcastIP, $port); socket_close($socket); return true; }