in reply to UDP Broadcasts
For example, the following snippet of code can be used to capture UDP packets from the local network:
use Net::PcapUtils; use NetPacket::IP; use NetPacket::UDP; use NetPacket::Ethernet qw/:types/; use strict; Net::PcapUtils::loop( sub { my ($arg, $header, $packet) = @_; my $ethernet = NetPacket::Ethernet->decode($packet); if ($ethernet->{'type'} == ETH_TYPE_IP) { my $ip = NetPacket::IP->decode($ethernet->{'data'}, $ether +net); my $udp = NetPacket::UDP->decode($ip->{'data'}); . . print $ip->{'src_ip'}, ":", $udp->{'src_port'}, " -> ", $ip->{'dest_ip'}, ":", $udp->{'dest_port'}, "\n"; } }, 'DEV' => 'eth0', # 'FILTER' => 'udp dst host 192.168.1.1' );
The commented line in the above code snippet is an example filter which can be passed the packet-capturing library to minimise unwanted 'overhead' packets also captured.
With regard to the initial UDP broadcast over the network, you will need to set the SO_BROADCAST option on the socket in order to request permission for the communication given the priviledged nature of this type of communication. For example:
use IO::Socket; use strict; my $sock = new IO::Socket::INET( 'PeerAddr' => '55.255.255.255' 'PeerPort' => '9', 'Proto' => 'udp' ); $sock->sockopt(SO_BROADCAST, 1);
Alternatively, depending upon your data requirements, the NetPacket group of modules referenced earlier can also be used to manually construct packets for transmission.
|
|---|