[tpm] Strange UDP broadast socket operation

Fulko Hew fulko.hew at gmail.com
Mon Nov 22 14:45:27 PST 2021


I hope someone can explain what I'm seeing...

I've written a library to interact with TP-Link / Kasa home automation
devices
and part of the function is to 'discover' devices on your network.
This happens via a UDP broadcast message, that the devices will respond to.
So you collect all of the responses and build a list of your devices.

a) When I wrote the original function I found I needed this pseudo code:

   $out = IO::Socket::INET->new(PEERPORT, BROADCAST_ADDR, 'udp');
    $local_port = $out->sockport();
    $in = IO::Socket::INET->new(PEER_PORT, $local_port, 'udp');
    $out->send($msg);
    $in->recv($rsp, 1024);

   I.e. For some reason I needed a separate socket to receive on.  Huh...
why ?

b) Then I wanted to run a test and send a directed UDP message instead.
   So I used the same code, except I used the specific IP address instead
of the broadcast address.
   But I found it would NOT receive the response.

   I had to recv() on the 'out' socket (as you would have expected)
   receiving on that 'in' port no longer works.  I.e.

   $out = IO::Socket::INET->new(PEERPORT, "192.168.1.25", 'udp');
    $out->send($msg);
    $out->recv($rsp, 1024);


So...
- why do I have to do a) (use two sockets) ?
- If I have to do a) for broadcast addresses, why doesn't it also work
using a specific IP address ?
- and more importantly... why can't I just do b) to a broadcast address ?

Thanks
Fulko
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20211122/cf737c17/attachment.html>


More information about the toronto-pm mailing list