LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-10-2009, 02:28 PM   #1
Hewson
Member
 
Registered: Feb 2007
Location: /home
Distribution: Kubuntu and CentOS
Posts: 214

Rep: Reputation: 32
perl -- pack


PERL's pack function is frustrating the heck out of me.

I need to build a UDP packet from the ethernet layer on up. I've found examples online for the IP and UDP layers, even so it seems like voodoo (I'm clearly missing something).

what should I be using for my template string???

for example: 0x0800 is the protocol number for an ip packet.

my $proto = 0x0800;
#my $template = 'hh' #this is wrong and I have no idea what would be right.
$packed_proto = pack($template, $proto);

I know how many bytes each part of my header should be (this is well documented...) and I know what values I want for each part of the header, but I can't figure out a sane method to convert between the two. Its clearly choosing the right template for the pack function.

(Or if any one can suggest a perl module that *works* and can help me pack the ethernet layer I would appreciate it.)
 
Old 11-10-2009, 03:06 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I had a lot of trouble with getting pack/unpack to do what I wanted, from memory if you are building a packet your template would need to be 'N' for network byte order.. hth
 
Old 11-10-2009, 03:27 PM   #3
pcunix
Member
 
Registered: Dec 2004
Location: MA
Distribution: Various
Posts: 149

Rep: Reputation: 23
As it stands, $proto is an integer, not a string, so it can't be "h".

You probably want "n" - 16 bit short in network order.
 
Old 11-11-2009, 07:10 PM   #4
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
I haven't used pack much, but you may find the tutorial helpful: http://perldoc.perl.org/perlpacktut.html.

Assuming you have a normal installation of Perl, you can also get this in a terminal via perldoc perlpacktut.
 
Old 11-11-2009, 11:46 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Have you looked at search.cpan.org eg http://search.cpan.org/search%3fmodule=NetPacket::UDP
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pack data into a 7z recursive + having autounpack EXE pack for windows? frenchn00b Linux - General 5 05-02-2009 01:59 PM
Icon pack carnold SUSE / openSUSE 4 09-04-2007 11:12 AM
LXer: Sun Announces the Latest in Open Source Tools with the Availability of NetBeans Visual Web Pack and NetBeans C/C++ Development Pack LXer Syndicated Linux News 1 12-31-2006 07:37 AM
perl pack function fvgestel Programming 2 10-27-2005 11:35 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration