Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: SNTP pack and unpack

by cheako (Beadle)
on Mar 07, 2015 at 00:00 UTC ( [id://1119137]=note: print w/replies, xml ) Need Help??


in reply to SNTP pack and unpack

Here are two solutions and the problem.
cheako@arcadia:~$ perl warn unpack("B620",pack( 'B8 C3 N10 B32', '00100011', '0', '0', '0', '0', '0', '0', '0', '0', 1425691777, 761152, '0', '0', 1425691777, 761152 )); 00100011000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 01010100111110100101010010000001 00000000000010111001110101000000 00000000000000000000000000000000 00000000000000000000000000000000 01010100111110100101010010000001 10111000000000000000000000000000 at - line 1. Different output, but identical input :/. cheako@arcadia:~$ perl warn unpack("B620",pack( 'B8 C3 N11', '00100011', '0', '0', '0', '0', '0', '0', '0', '0', 1425691777, 761152, '0', '0', 1425691777, 761152 )); cheako@arcadia:~$ perl warn unpack("B620",pack( 'B8 C3 N11', '00100011', '0', '0', '0', '0', '0', '0', '0', '0', 1425691777, 761152, '0', '0', 1425691777, 761152 )); 00100011000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 01010100111110100101010010000001 00000000000010111001110101000000 00000000000000000000000000000000 00000000000000000000000000000000 01010100111110100101010010000001 00000000000010111001110101000000 at - line 1. cheako@arcadia:~$ perl warn unpack("B620",pack( 'B8 C3 N10 B32', '00100011', '0', '0', '0', '0', '0', '0', '0', '0', 1425691777, 761152, '0', '0', 1425691777, '00000000000010111001110101000000' )); 00100011000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 00000000000000000000000000000000 01010100111110100101010010000001 00000000000010111001110101000000 00000000000000000000000000000000 00000000000000000000000000000000 01010100111110100101010010000001 00000000000010111001110101000000 at - line 1.

I've a lot of experience with pack and unpack and every time I learn something new, seems they never behave the way one would think they should. Check it with a warn unpack("B384",$send_sntp_packet) the best thing you can do at times like this is add in warn every branch.

my@argsforpacksend_sntp_packet=($client_li_vm_mode , + $client_stratum , $client_poll , $client_precision , $client_root_de +lay , $client_dispersion , $client_reference_identifier , $client_ref +erence_timestamp_sec , $client_reference_timestamp_microsec , $client +_originate_timestamp_sec , $client_originate_timestamp_microsec , $cl +ient_receive_timestamp_sec , $client_receive_timestamp_microsec , $cl +ient_transmit_sec , $client_transmit_microsec); warn Dumper \@argsfo +rpacksend_sntp_packet; my $send_sntp_packet = pack "B8 C3 N10 B32", @argsforpacksend_sntp_p +acket; warn unpack("B384",$send_sntp_packet)
Wrap each call to pack/unpack like this and you'll see what's wrong.

Replies are listed 'Best First'.
Re^2: SNTP pack and unpack
by Anonymous Monk on Mar 07, 2015 at 11:06 UTC

    Node has been edited again; please mark your updates. See also the section "It is uncool to update a node in a way that renders replies confusing or meaningless" in How do I change/delete my post?

Re^2: SNTP pack and unpack
by Anonymous Monk on Mar 07, 2015 at 00:42 UTC

    At the time of posting the node consisted of a single dot, and was replaced by its current content roughly 30 mins later. Looks like cheako followed ambrus's advice.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: SNTP pack and unpack
by thanos1983 (Parson) on Mar 07, 2015 at 01:33 UTC

    Hello cheako,

    Thank you for your time and effort reading and replying to my question.

    I modified the code as you propose and also I am using use diagnostics; as I found online to assist me identifying the error. Still I can not understand why my send packet is wrong.

    This is the output that I am getting when I execute the code.

    Client Transmit: Sat Mar 7 02:29:37 2015 $VAR1 = [ 'B8 C3 N10 B32', '00100011', '0', '0', '0', '0', '0', '0', '0', '0', 1425691777, 761152, '0', '0', 1425691777, 761152 ]; Invalid type '1' in pack at NTPperl.pl line 50 (#1) (F) The given character is not a valid pack or unpack type. See "pack" in perlfunc. (W) The given character is not a valid pack or unpack type but use +d to be silently ignored. Uncaught exception from user code: Invalid type '1' in pack at NTPperl.pl line 50.

    Line 50 is:

    my $send_sntp_packet = pack @argsforpacksend_sntp_packet;
    Seeking for Perl wisdom...on the process of learning...not there...yet!
      As can be easily demonstrated by
      print prototype 'CORE::pack';

      pack evaluates the first argument in scalar context. If you supply an array, it turns into the number of its members. You got 1, which is double weird, as it means the array only had one member - you have to supply two things to pack, the template and the list (and, as we have just seen, you can't bundle them in one array).

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Hello choroba,

        Thank you for your time and effort, there are so many things that I do not know. Your answer helped to view the way that pack should be defined.

        I minor note, it would be easier for me and in general for people who do not know much of Perl to write it like this.

        perl -le 'print prototype "CORE::pack"'

        Output:

        $@

        Thanks again for the time and effort, you helped me a lot understanding. Every area that someone has not worked before is always confusing at the beginning.

        Reference to using CORE::pack, CORE::unpack.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re^2: SNTP pack and unpack
by thanos1983 (Parson) on Mar 07, 2015 at 12:13 UTC

    Hello again cheako,

    Thank you for your time and effort again. You answer helped a lot to understand much more. I have updated my code and the sample of output that I am getting. and also the expected output.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re^2: SNTP pack and unpack
by Anonymous Monk on Mar 07, 2015 at 00:07 UTC
    Hi cheako , if you're itching for replies, reply to your own nodes, don't bother the nice people working on code ... also look at Past Polls, no code required there, just jokes and regular opinions, great place to earn some karma
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: SNTP pack and unpack
by cheako (Beadle) on Mar 07, 2015 at 02:09 UTC
    Adding debugging and working with the output was the right path. Even if I didn't know pack would behave that way when given a list.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1119137]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-26 04:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found