yasharrashedi / LimitlessLED

Milight/LimitlessLED/EasyBulb Complete PHP API
63 stars 21 forks source link

Broadcast addresses aren't working #3

Closed richardtallent closed 8 years ago

richardtallent commented 9 years ago

Thanks for the library! I was about to write one myself, so this is going to save me a ton of work.

I'm creating a web page (hosted via Apache on OS X on my LAN) to control my home lights (among other home theater-related stuff). The Milight package works well when I use the bridge's IP address, but when I tried to use the LAN broadcast address (255.255.255.255), I get the following error:

Warning: socket_sendto(): unable to write to socket [13]: Permission denied in blah/Milight.php on line 201

I tried to add a line like this after the socket is created and before the message is sent, but it didn't help:

socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);

I'm a novice PHP user (my day job is .NET development), so I'm not sure if this is a gap in the Milight code or some obscure security issue with PHP and Apache.

Connecting directly to the bridge is fine, but I thought it might be a bit faster to broadcast.

mrwhale commented 9 years ago

For some reason or another that PHP function doesn't like sending 'limited' broadcast address (255.255.255.255) I recommend using your direct broadcast address (eg 192.169.1.255) This broadcast address can easily be gathered with PHP. If I have time this weekend I'll try add this functionality into the script :)

Otherwise if your keen on broadcasting to 255.255.255.255 here's some reading from stack exchange and php.net (trhey somewhat explain the difference between 255.255.255.255 and the direct broadcast address ) http://stackoverflow.com/questions/6650719/udp-broadcast-send-from-php-and-receive-in-c-sharp http://php.net/manual/en/function.socket-sendto.php http://cyruslab.net/2010/09/10/local-broadcast-and-directed-broadcast/

http://stackoverflow.com/questions/8880591/socket-permission-denied-in-c (look at the comments for this, could be related to the OS denying access)

mrwhale commented 9 years ago

Okay, so it was a simpler then first though. Just needed to set the broadcast option when creating the socket to send out of socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1); http://php.net/manual/en/function.socket-sendto.php

I have added this in, plus the function I mentioned above (if you leave the IP blank it was automatically use the LAN broadcast - 192.168.100.255 etc)

Will create a pull request to have these included :)

yasharrashedi commented 8 years ago

please get the latest version, #8