Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Creating trailer on packets

by mosh (Scribe)
on May 16, 2005 at 08:35 UTC ( #457382=perlquestion: print w/ replies, xml ) Need Help??
mosh has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks !

I need a mechanism that checks a packet length and if the packet length is less than 60 bytes, it will be padded with nulls (trailer).

Do anyone know mechnism \ module that has those abilities ?
If there's no such a module how can I start writing it ?

... # creating objects my $ethernet = NetPacket::Ethernet->decode(); my $ip = NetPacket::IP->decode(); # creating the packet my $packet = $ethernet->encode() . $ip->encode(); # creating the trailer if (length($packet) < 60) { ??? }
Note that the trailer (null padded) is located before the IP header.

Ideas, anyone?

Mosh.

Comment on Creating trailer on packets
Download Code
Re: Creating trailer on packets
by polettix (Vicar) on May 16, 2005 at 09:08 UTC
    Don't know if it actually solves your problem (untested!):
    # creating the packet chunks my $packet_eth = $ethernet->encode(); my $packet_ip = $ip->encode(); my $packet = $packet_eth . (0 x (60 - length($packet_eth) - length($packet_ip))) . $packet_ip;
    The "x" operator does the right thing when (60 - length($packet_eth) - length($packet_ip)) isn't positive, of course.

    30-sec-after update: it would probably pad with character '0' instead of nulls as you want; a quick hack could be:

    # creating the packet chunks my $packet_eth = $ethernet->encode(); my $packet_ip = $ip->encode(); my $packet = $packet_eth . (pack 'H*', ('00' x (60 - length($packet_eth) - length($packet_ip +)))) # Note 0 -> 00 . $packet_ip;

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
      Thanks a lot guys, it was very helpful !

      Mosh.

Re: Creating trailer on packets
by ghenry (Vicar) on May 16, 2005 at 09:28 UTC

    A quick one on CPAN, lists a few interesting possibilities:

    packet

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
Re: Creating trailer on packets
by BrowserUk (Pope) on May 16, 2005 at 09:35 UTC

    A couple of routines from my utils.pm

    sub padr{ substr $_[ 0 ] . $_[ 1 ] x $_[ 2 ], 0, $_[ 2 ]} sub padl{ substr $_[ 1 ] x $_[ 2 ] . $_[ 0 ], -$_[ 2 ] } print padl 'fred', '.', 20; ................fred print padr 'fred', '.', 20; fred................

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

      These are rather procrustean: they pad the short strings, but also truncate the long ones.

      the lowliest monk

        These are rather procrustean:

        Having looked that up. Agreed!

        Another way of looking at that is WAD--working as designed for the purpose I had for them.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: Creating trailer on packets
by kwaping (Priest) on May 16, 2005 at 18:22 UTC
    How about this?
    # ... $packet = sprintf("%-60s",$packet); $packet =~ s/\s/\0/sg; #<-- kludgey #...
    I couldn't get sprintf to pad with a certain character and left-justify at the same time - maybe someone else can suggest something?
Re: Creating trailer on packets
by eXile (Priest) on May 16, 2005 at 18:59 UTC
Re: Creating trailer on packets
by Jim_Gillespie (Initiate) on May 17, 2005 at 14:59 UTC
    Hi all,

    Sorry if I'm missing something, but isn't this what pack() is for? E.g.

    my $packet = pack "a60", $data;

    I'm afraid I don't know anything about packet structures.

    Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://457382]
Approved by muntfish
Front-paged by one4k4
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-07-28 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (186 votes), past polls