in reply to Re^3: IP Header
in thread IP Header

Thank you for your help thus far. I am now getting the expected results for $tos and $len, however, $ver and $hlen are still not right.

first, your "vec($ver_hlen, 4, 0)" brings up "illegal number of bits", due to 0 but being a power of 2 between 1 and 32 (from perldoc -f vec).

I am having trouble understanding exactly what the bits field is. Is that the number of bits that you want to read in? if so, wouldn't:
my ($ver, $hlen) = (vec($ver_hlen, 0, 4), vec($ver_hlen, 4, 4));
be better? Using the the changes i made above, I get unexpected results for the values for $ver and $hlen, which leads me to believe I am wrong.

Replies are listed 'Best First'.
B versus b
by Thelonius (Priest) on Nov 03, 2002 at 14:27 UTC
    In addition to the problems Aristotle outlined above, there is one more problem. The functions unpack("b") and vec() unpack the bits in the wrong order within bytes.

    If you unpack("B*", $foo), you will get all the bits (as a string) in the right order. Here is one way to get your final answer, although I'm not sure that eval "0b$_" is the best solution:

    ($ver, $hlen, $tos, $len) = map { eval "0b$_" } unpack "A4A4A8A16", un +pack("B*", $a);
      ++ for pointing out the B vs b concern, but you're wrong about vec. And that code is an eval $REDFLAG;, you should do it thusly: my ($ver, $hlen, $tos, $len) = map pack "B*", unpack "A4A4A8A16", unpack "B*", $foo; However, that will not honour the network byte ordering in $len.

      Makeshifts last the longest.

Re^5: IP Header
by Aristotle (Chancellor) on Nov 02, 2002 at 23:49 UTC
    Argh. This ain't my day. My parameters were in the wrong order, it's my ($ver, $hlen) = (vec($ver_hlen, 0, 4), vec($ver_hlen, 1, 4)); or also my ($ver, $hlen) = map(vec($ver_hlen, $_, 4), (0 .. 1)); The 4 means I want to treat this as an "array" of 4-bit-elements, and the offset specifies the "index" in that "array". I hope I haven't left any errors still to correct this time. grumble

    Makeshifts last the longest.