http://www.perlmonks.org?node_id=1199616


in reply to Re^4: Building a UDP Ethernet header
in thread Building a UDP Ethernet header

Based on my reading of your Stack Overflow posts (now deleted), you don't have a deep understanding of networking protocols, which makes coding for them all the more difficult. Your OP cites code from this site dated Jun 11, 2000, and as one of the comments on that node notes, there are surely more relevant, recent and working examples.

First off, terminology:

[ ETHERNET HEADER [ IP HEADER [ UDP HEADER [ DATA ] ] ] ] (frame) (packet) (datagram)

Perl's IO::Socket::IP will handle IP and up (to the right) and your operating system, once you send() will handle ETHERNET Header. Unless you specify raw sockets (which you're trying to do); in which case you may need to code up the Ethernet layer yourself - mileage may vary depending on OS (e.g., *nix may handle this in some cases, Windows will definitely *not*).

Creating a raw packet can be as simple as coding the whole thing in hex and pack()-ing it but you still need to send(). Unless you plan on using something like Net::Pcap, your program won't be able to send the packet your creating because you'll have coded a frame and the send() routine expects a packet.

You didn't answer my question about what this is for. We know what you're trying to do ... WHY? Do you have UDP data to send to a remote host? Easy, change the PeerAddr and PeerPort parameters below to suite the remote destination address and port:

#!/usr/bin/perl use strict; use warnings; use IO::Socket::IP; my $socket = IO::Socket::IP->new( Proto => "udp", PeerAddr => "remote.host.com", PeerPort => 12345 ) || die "Cannot create client\n"; while(1) { print "PROMPT> "; my $message = <STDIN>; chomp $message; if (($message !~ /^q(?:uit)?$/i) && ($message !~ /^e(?:xit)?$/i)) +{ $socket->send($message) } else { last } } close($socket);

We don't even know what data you're trying to send? Is it SNMP, Syslog, TFTP, NTP or one of the *many* known UDP upper layer protocols / applications all of which have Perl modules to help (e.g., Net::SNMP, Net::Syslog, Net::TFTP, Net::SNTP::Client) and none of which require raw sockets or to "control the whole packet".

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.