php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40674 socket_bind on interface wont see broadcasts to 255.255.255.255
Submitted: 2007-03-01 00:41 UTC Modified: 2007-09-06 01:00 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: matth at mystictrd dot com Assigned:
Status: No Feedback Package: Sockets related
PHP Version: 5.2.1 OS: Debian 3.1
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: matth at mystictrd dot com
New email:
PHP Version: OS:

 

 [2007-03-01 00:41 UTC] matth at mystictrd dot com
Description:
------------
A SOCK_DGRAM SOL_UDP socket using socket_bind to an IP address 
associated with an interface will not see UDP broadcasts destined to 
255.255.255.255.  I do not know if this is intended but you can see them 
when listening on 0.0.0.0

Reproduce code:
---------------
<?php
//listens for dhcp requests
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
//you must bind to 0.0.0.0 to actually see anything
socket_bind($socket, "10.66.66.1", 67);
while(1)
{
  if($src = @socket_recv($socket, $data, 9999, 0))
  {
    echo "data!\n";
  }
}
?>

Expected result:
----------------
When a DHCP request is made we should see "data!" echoed.

Actual result:
--------------
Nothing was read on the interface - it can only see localhost or direct 
packets sent to it (destination interface IP address).

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-03-01 09:01 UTC] tony2001@php.net
And why do you think it's PHP related?
 [2007-03-01 17:25 UTC] matth at mystictrd dot com
The reason why is if you take a normal DHCP server you can typically listen on multiple interfaces.  If one wanted to know what interface is receiving the data you would think to bind on the interfaces IP address.  But since you cannot see that type of packet binding to an IP like that you must bind to 0 or 0.0.0.0 which doesn't help with knowing what interface recieved the data since the data does not contain information about interface mac/ip destination from the client. I'm not positive if this is a PHP issue or not but I believe you can do it in C/C++ and that you should be able to do it with PHP.  A work around I'm experimenting with  goes as:

<?php
$interface_ip = "10.66.66.1";
$interface_port = 67;
//listens for dhcp requests
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
//you must bind to 0.0.0.0 to actually see anything
socket_bind($socket, 0, 67);
while(1)
{
  if($src = @socket_recvfrom($socket, $data, 9999, 0, $interface_ip, $interface_port))
  {
    echo "data!\n";
  }
}
?>
 [2007-03-01 17:38 UTC] matth at mystictrd dot com
Ignore my previous workaround example, it did not work as expected.
 [2007-03-02 17:21 UTC] matth at mystictrd dot com
I believe in C/C++ you can specify hardware addresses to listen on - specifically either a MAC address/an interface name (eth0 in linux).  

After doing some tests in C it seems that if you listen on an ip (i.e. 192.168.0.1) and you will not see that type of packet but if you listen on 0.0.0.0 or INADDR_ANY you can see it.
 [2007-03-02 20:24 UTC] matth at mystictrd dot com
The way it seems this should work (I easily could be completely off 
base) is:

socket_bind could be defined as:

bool socket_bind ( resource $socket, string $address [, int $port [, 
string $mac or $iface ]] )
 [2007-03-06 17:12 UTC] matth at mystictrd dot com
My first message had a working example but here it is again with more 
information.

The purpose is to see DHCP requests from a DHCP Client.  So use a DHCP 
Client to get an IP address on a system thats on the same network that 
this script is running on. 

This first example WON'T see the packets:
<?php
$ip = "ENTER YOUR IP ADDRESS HERE"; //example: 192.168.0.100
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, $ip, 67);
while(1)
{
  if($src = @socket_recv($socket, $data, 9999, 0)) { echo "data!\n"; }
}
?>

This second example WILL see the packets:
<?php
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_bind($socket, 0, 67);
while(1)
{
  if($src = @socket_recv($socket, $data, 9999, 0)) { echo "data!\n"; }
}
?>


This reference might give you a better idea of what I'm talking about:
http://en.wikipedia.org/wiki/Dhcp#DHCP_discovery
 [2007-08-29 11:55 UTC] jani@php.net
"A non-authoritative server simply ignores the request, leading to an implementation dependent time out for the client to give up on the request and ask for a new IP."

Isn't that exactly what is happening here?
 [2007-09-06 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2010-05-06 12:21 UTC] agv at rsh dot hu
Hy! I solved the problem:

<?php
$bc_socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // for broadcast packets
$if_socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); // for answer packets

socket_bind($bc_socket, "255.255.255.255", 67); // listen to discover pcks.
socket_bind($if_socket, "10.0.0.1", 67); // target interface address

socket_set_option($bc_socket, SOL_SOCKET, SO_BROADCAST, 1);
socket_set_option($bc_socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($bc_socket, SOL_SOCKET, SO_DEBUG, 0);

socket_set_option($if_socket, SOL_SOCKET, SO_BROADCAST, 1);
socket_set_option($if_socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($if_socket, SOL_SOCKET, SO_DEBUG, 0);

while(1)
{
  if($src = @socket_recv($bc_socket, $data, 9999, 0))
  {
    echo "data!\n";
    $senddata = "....";
 	socket_sendto($if_socket, $senddata, strlen($senddata),0, "255.255.255.255", 68);
  }
}


?>
 [2022-03-01 09:31 UTC] worker at cksn dot tk
i'm running into the same problem to receive DHCP packets via PHP8.1 ... all the here mentioned codes arnt working now, maybe someone can confirm the behavior or give me a new hint to solve this problem.

regards, volker.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC