fsockopen issue when opening port to sphinx only in php - php

I have an issue when connecting php and only php to the sphinx daemon. I have tested the sample sphinx php example test.php and narrowed it down to being a connection problem.
I have the follow piece of code which shows the problem,
<?php
$fp = #fsockopen ( '127.0.0.1', '9312', $errno, $errstr,300 );
if(!$fp) {
echo "$errstr ($errno)";
}
?>
Whenever it runs from command line or browser I get the following error,
Connection refused (111)
I have verified the following,
searchd is indeed running and on port 9312
searchd can be connected to using telnet
searchd can be connected to using test.py in the sphinx api
php can connect to port 80
It is running on a Westhost VPS which seems to be the issue since it works fine on my local test machine. I have existing code which was using Python to run searches against the index without any issues so this one has me well and truly stumped.
Some additional info,
PHP 5.2.5
Sphinx 0.9.9-release
Anyone have any ideas how I could diagnose and fix this issue further?

Maybe you could use cmd: netstat -an
To see what ip:port is Sphinx Server running, then use that ip and port in fsocketopen() function

Related

Lost UDP packages with PHP socket_sendto

I have a PHP CMS system, that's sending a UDP package to switch off a device on the local network.
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if (!$sock) {
echo "COULD NOT CREATE SOCKET";
}
$result = socket_sendto($sock, "Ausgang1AUS", 11, 0, "192.168.20.49", 30303);
socket_close($sock);
if ($result) echo "UDP SENT ".$result." BYTES";
This has been working perfectly on our old (dedicated) linux server.
Now since switching to a new (virtual) linux server, these packages never reach their target.
I get no error in PHP (just my "UDP SENT 11 BYTES", but the packages just do not arrive anywhere in the local network.
It DOES work if I send the UDP to the server itself (using it's own IP).
It DOES work if I run this script on another machine in the local network.
It DOES work if I send UDP via netcat, e.g.
echo "Ausgang1AUS" | nc -u 192.168.20.49 30303. So it shouldn't be a general blocking problem.
Firewall and SELinux are both disabled on the machine.
Does anyone have an idea whats happening? Could it be some PHP setting or some user privilege? Is there any tool I could use to find out whats's happening to the UDP package?
I'm pretty lost at the moment...
Edit:
Seems to have nothing to do with php socket_sendto in the end, but connect to the LENGTH of the data sent. So #stark was right, using echo (thus introducing a newline) changed the length from 11 bytes to 12 bytes, and suddenly packages do arrive.
I posted a new question on SuperUser
Is this virtual Linux server running on a Windows VM? There was a recent Windows update that introduced a bug in hypervisor which is causing UDP packets under 12 bytes to fail checksum validation.
There's a Github issues here where other users are reporting the same issue on WSL2.
https://github.com/microsoft/WSL/issues/8610
You can see there's a workaround posted in that thread that might help in your situation, at least until Microsoft manage to fix the issue:
ethtool -K eth0 tx off

Curl Error Hostname Not Found in DNS Cache

I have a website (www.example.com | 123.456.789), in that machine i only have 1 web app running. i am trying to use php file_get_content() to a file which in my own server (located on some file like /var/www/my_site/public_html).
The PHP code i am referring is:
$url = 'http://example.com/282-home_default/short-wallet-tan.jpg';
var_dump($url);
$json = #file_get_contents($url);
var_dump($json);
die();
However it always returns error. When i try to do it manually, using CURL
I can confirm that i can CURL other website such as ~$ curl google.com
Can anyone suggest me what to do to resolve this. Thanks
I am using Ubuntu 14.04 LTS to host my webapp.
Some of the solution regarding this question, points out that i should downgrade my version of curl. Did that but still have the same problem.
curl failed to connect to that host's TCP port number 80. You're either not running the http server on port 80, you've made curl try to connect to the wrong server or there's something else in your network that prevents the connect handshake to succeed.

"Connection refused" error on socket_connect in php

I am trying to convert some code from perl to php.
Perl code looks like below:
my $handle = Connect($port, $host);
and I am trying to use socket to do the same thing in php.
I have tried socket_create and socket_connect,
socket_create and socket_bind, and fsocketopen.
As a result, I'm stuck with error messages saying "Connection refused" or "permission denied":
socket_connect() [function.socket-connect]: unable to connect [111]: Connection refused in
I am not sure if this is the problem I need to solve, or the problem of permission because the code in perl works fine (I did not write that code).
my php code looks like below:
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$socket){
die('Error: socket_create()');
}
if(!socket_connect($socket,$host,$port)) {
die('Error: socket_connect()');
}
I'm not the one who manages the server, so I will need to ask someone else for the access if it is a permission issue. What should I ask for specifically?
Or should I use some other function to connect to the server? I am new to perl, so I am not sure if socket_connect is the equivalent function to use or not.
Thanks.
If your perl code is able to establish the connection, no additional permissions should be needed to do the same in php. Connection refused means the remote host doesn't let you in (you probably connect to wrong address/port). Permission denied is more surprising, a lot of people have this kind of problem while running httpd scripts with SELinux enabled. If you're one of them, refer to the SELinux manpage:
SELinux policy can be setup such that httpd scripts are not allowed to connect out to the network. This would prevent a hacker from breaking into you httpd server and attacking other machines. If you need scripts to be able to connect you can set the httpd_can_network_connect boolean on:
setsebool -P httpd_can_network_connect 1
I have a few concers to your examples though. Connect from your Perl snippet doesn't seem to be the standard socket connect; I don't know which module it belongs to, but are you sure there is no magic behind the call? As socket_connect takes address in dotted-quad notation (for IPv4), make sure you're not passing a hostname (you would need to make a DNS lookup first). At the very end check if it's really a TCP socket you need, not UDP.

Weird behaviour: Apache PHP Windows with FSockopen

Consider this simple code:
<?php
error_reporting(E_ALL);
//error_reporting(E_STRICT);
date_default_timezone_set('Asia/Calcutta');
$smtp_server = fsockopen("onelocalserver.com", 25, $errno, $errstr, 120);
if(!$smtp_server)
{
// We have an error, do something
echo " Error found! <br>$errno <br>$errstr";
exit;
}
?>
On Windows XP + php5.3.8 + apache 2.2, i get an error:
Warning: fsockopen(): unable to connect to onelocalserver.com:25 (No connection could be made because the target machine actively refused it. )
However, on the same system where Apache and PHP are installed, if i run php from the command prompt ex: c:\php\php.exe filename.php (where above code is in the filename), then i don't get the error!
The server onelocalserver.com is listening on port 25 : there is an SMTP server running there.
Any ideas? This is really bizzare
Thanks in advance
After several tries with different IP addresses, i realised that the only problem was with fsockopen on port 25. It works well with command line interface but didn't work through the web browser. PHP was earlier installed in windows as CGI. I thought i will see if it still doesn't work when set up as an Apache handler and it works! http://www.php.net/manual/en/install.windows.apache2.php .
Hope this helps someone!

php fsockopen

I have a simple php script on a server that's using fsockopen to connect to a server.
<?php
$fp = fsockopen("smtp.gmail.com", 25, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
echo fgets($fp, 1024);
fclose($fp);
}
?>
The problem is the the script times out and fails to connect. If i change the port from 25 to 80 for example it works without problems on any host. So the problem seems to be only the port 25 no matter what host i use, i tried a lot of them and all work for port 80 and others but for 25 fails.
Connections are not blocked form firewall as if i telnet from shell it successfully connects to any port on any host.
Any idea what could be the problem as it's really weird?
LE: If i run the same php script from the shell, php scriptname.php it works so only when i run it by http it fails. I have apache with SuPHP so the problem is around here somewhere
Interesting...
Some firewalls can block specific program's connections to specific ports.
Please check it again, try to stop firewall completely. Also try to stop any anti-spyware.
Like maxnk mentioned firewalling is the most likely issue, either on the server, or by your ISP. Port 25 is frequently firewalled as a method to prevent spam.
Just as a quick test, since you mentioned gmail, you might want to try connecting to port 587 instead. Gmail listens for smpt on this alternate port in addition to port 25 to help users bypass overly restrictive firewalls.
I've run into some strange issues with PHP's socket handling, too. It ended up being a problem with the system it was running on. Have you tried running your code on a different machine?
I think the connection problem is with your machine. I just copied your code into a script on my machine(linux suse) and ran it with php -f test_script. I got the following message
220 mx.google.com ESMTP j8sm1814228gvb.0
CentOS can have SELinux enabled which can cause connection weirdness. Have you checked your error logs?

Categories