Checking UDP port in PHP - php

I'm trying to check a servers UDP port using PHP. here is my code:
<?php
set_error_handler('my_error_handler');
function my_error_handler($errno, $errstr, $errfile, $errline) {}
function checkUDP($host,$port=80){
$fp = fsockopen("udp://".$host, $port, $errno, $errstr,1.0);
if (!$fp) {
return false;
} else {
fclose($fp);
return true;
}
}
if(checkUDP($MyIP,9)){
echo $MyIP.' is open';
}else{
echo $MyIP.' is closed';
}
echo '<br />';
?>
But I always get True result (port is open). This code works on TCP port but why I couldn't get the UDP port results?

Related

PHP Check connect to Remote SSL Address

Is there any reliable way via PHP to check if a local WebServer (php4) can access a remote SSL address ?
https://xxx.yyy.com
port 443 ?
All I'm interested in ensuring is the connection from the WebServer to the remote address is working and on port 443. I'm not bothered about checking certificates etc..
Thanks
UPDATE:
I've tried this, but is it reliable ?
<?php
function checkConnection()
{
$conn = #fsockopen("www.abc.com", 443, $errno, $errstr, 30);
if ($conn)
{
$status = "Connection is OK";
fclose($conn);
}
else
{
$status = "NO Connection<br/>\n";
$status .= "$errstr ($errno)";
}
return $status;
}
echo checkConnection();
?>

PHP how to connect with sockets and read the response?

The title explains it all...
How can i connect to an IP using tcp protocol and read/get the response?
I have searched a lot but i didnt find any solution.
$socket = stream_socket_server("tcp://127.0.0.1:22", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
while ($conn = stream_socket_accept($socket)) {
echo fread($conn, 26);
fclose($conn);
}
fclose($socket);
}
is this code ok?
Does the job?
Because it seems it doesn't do the job...
Thanks in advance
As suggested by others; avoid using port 22. I recommend using an obscure (unused) socket port number > 1024 such as 4444. Anything below 1024 normally requires root access.
If you need to test connectivity for 22 have your server script run additional functions.
As for sending a response back to the connected client use stream_socket_recvfrom($socket, $length, 0, $peer) instead of fread()
Then on the client side add a response listener:
client.php
$socket = stream_socket_client('tcp://127.0.0.1:4444');
if ($socket) {
$sent = stream_socket_sendto($socket, 'message');
if ($sent > 0) {
$server_response = fread($socket, 4096);
echo $server_response;
}
} else {
echo 'Unable to connect to server';
}
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
server.php
$conn = stream_socket_server('tcp://127.0.0.1:4444');
while ($socket = stream_socket_accept($conn)) {
$pkt = stream_socket_recvfrom($socket, 1500, 0, $peer);
if (false === empty($pkt)) {
stream_socket_sendto($socket, 'Received pkt ' . $pkt, 0, $peer);
}
fclose($socket);
usleep(10000); //100ms delay
}
stream_socket_shutdown($conn, \STREAM_SHUT_RDWR);
Run server.php which will listen in an endless loop listening for a non-empty packet
once server.php receives a packet it will respond back to the connected client with the received packet.
Then execute client.php which will send 'message' to server.php
Once sent it will then retrieve and echo the response from server.php which should read 'Received pkt message'
From http://php.net/stream_socket_accept
Accept a connection on a socket previously created by stream_socket_server().
That means it waits that one client wants to connect. (You just bind yourself to the port, but don't connect anything)
And fread is also the wrong function to use with socket_* functions. Correct function would be stream_socket_recvfrom().
But this really isn't what you seem to want. You appearently want to open a connection to some place. So fsockopen() is the right function:
$conn = fsockopen("127.0.0.1", 22, $errno, $errstr);
if (!$conn) {
echo "$errstr ($errno)<br />\n";
} else {
echo fread($conn, 26);
fclose($socket);
}

Checking service fsockopen()

I'm trying to check service on port 9998. To do so I'm using fsockopen() function.
Like this:
$host = "1.1.1.1";
$port = "9998";
$checkconn = fsockopen($host, $port, $errno, $errstr, 1);
if($checkconn >= 1){
echo 'ok';
} else {
echo "$errstr";
}
It always returns "Connection Timed Out - 110", but the port is open and there is service running there.
If I change the port (like 80), it returns successful, but why not on the 9998 port?
A 1 second connect timeout is not very long. Network lag can be enough to trigger that even when the host/port is valid. I would suggest using at least 2.5 - 5 seconds instead.
Also, you should be checking the result of fsockopen() for FALSE instead of < 1 to know when it fails.
Try this:
$host = "1.1.1.1";
$port = "9998";
$checkconn = fsockopen($host, $port, $errno, $errstr, 5);
if(!$checkconn){
echo "($errno) $errstr";
} else {
echo 'ok';
}

How to check a UDP address exists with PHP and fsockopen?

I am using fsockopen to get information from a UDP address, the only problem being that some of the UDP addresses may not still be active.
I create the socket by
$fp = fsockopen($tracker, $port, $errno, $errstr, 1);
If the address is valid everything works fine, but if the address is invalid it generates this error
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in
I tried doing this but it still generates the error,
if(!$fp = fsockopen($tracker, $port, $errno, $errstr, 1)) {
// ERROR
} else {
// CONTINUE
}
I can error suppress it and all is good but I do not like error suppressing in my code.
How can I make sure any given UDP address is still active with php?
Thanks
Instead of suppressing the error # which you could do, you could implement your own error handler. set_error_handler
<?php
//Simple Blank error handler
set_error_handler('my_error_handler');
function my_error_handler($errno, $errstr, $errfile, $errline) {}
function checkUDP($host,$port=80){
//look no suppression
$fp = fsockopen("udp://".$host, $port, $errno, $errstr,1.0);
if (!$fp) {
return false;
} else {
fclose($fp);
return true;
}
}
$good = 'tracker.publicbt.com';
$bad = 'trjjacker.publicbt.com';
if(checkUDP($good)){
echo $good.' Good';
}else{
echo $good.' Bad';
}
echo '<br />';
if(checkUDP($bad)){
echo $bad.' Good';
}else{
echo $bad.' Bad';
}
//tracker.publicbt.com Good
//trjjacker.publicbt.com Bad
?>

fsockopen doesn't work correctly with with if/else

I have this PHP code
$fp = #fsockopen($_SERVER['REMOTE_ADDR'], 80, $errno, $errstr, 1);
if ($fp) {
$url = "/";
fputs($fp, "GET $url HTTP/1.1\r\nHost: {$_SERVER['REMOTE_ADDR']}\r\nConnection: close\r\n\r\n");
$resp = '';
while(!feof($fp)) {
$resp .= fgets($fp, 1024);
}
if (preg_match('/^http\/1\.\d+ 401/i', $resp)) {
echo "Requires authentication";
} else {
echo "No auth required.";
}
} else {
echo "Failed to connect on port 80, reason: $errstr";
}
I have edited this lines
} else {
echo "Failed to connect on port 80, reason: $errstr";
}
I just have writen 2 lines to check if $errstr # Connection refused
to be like this
} else {
if ($errstr != 'Connection refused') {
echo "Ok;
}
}
But code doesn't work correctly
Can you check what is the problem please ?
Thanks

Categories