sntp code: what pack_ntp is doung, - NTP

This is a discussion on sntp code: what pack_ntp is doung, - NTP ; hi all, i am trying to work on SNTP by using the opensource code from ntp.org. i downloaded the code and its working. but i am not understanding what the function pack_ntp() is doing. its in the file main.c this ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: sntp code: what pack_ntp is doung,

  1. sntp code: what pack_ntp is doung,

    hi all,
    i am trying to work on SNTP by using the opensource code from ntp.org.
    i downloaded the code and its working. but i am not understanding what
    the function pack_ntp() is doing. its in the file main.c
    this is the code:
    void pack_ntp (unsigned char *packet, int length, ntp_data *data) {

    /* Pack the essential data into an NTP packet, bypassing struct layout
    and
    endian problems. Note that it ignores fields irrelevant to SNTP. */

    int i, k;
    double d;

    memset(packet,0,(size_t)length);
    packet[0] = (data->status<<6)|(data->version<<3)|data->mode;
    packet[1] = data->stratum;
    packet[2] = data->polling;
    packet[3] = data->precision;
    d = data->originate/NTP_SCALE; // 2e32
    for (i = 0; i < 8; ++i) {
    if ((k = (int)(d *= 256.0)) >= 256) k = 255;
    packet[NTP_ORIGINATE+i] = k;
    d -= k;
    }
    d = data->receive/NTP_SCALE;
    for (i = 0; i < 8; ++i) {
    if ((k = (int)(d *= 256.0)) >= 256) k = 255;
    packet[NTP_RECEIVE+i] = k;
    d -= k;
    }
    d = data->transmit/NTP_SCALE;
    for (i = 0; i < 8; ++i) {
    if ((k = (int)(d *= 256.0)) >= 256) k = 255;
    packet[NTP_TRANSMIT+i] = k;
    d -= k;
    }
    }

    in this what is happening after deviding by NTP_SCALE and what is
    happening inside for loop.
    if you are having time, pls help me.

    with thanks in advance,
    arun.pg

  2. Re: sntp code: what pack_ntp is doung,

    Pg wrote:

    > for (i = 0; i < 8; ++i) {
    > if ((k = (int)(d *= 256.0)) >= 256) k = 255;
    > packet[NTP_TRANSMIT+i] = k;
    > d -= k;
    > }


    >
    > in this what is happening after deviding by NTP_SCALE and what is
    > happening inside for loop.


    Putting the values into network byte order.

  3. Re: sntp code: what pack_ntp is doung,

    Pg wrote:

    > for (i = 0; i < 8; ++i) {
    > if ((k = (int)(d *= 256.0)) >= 256) k = 255;
    > packet[NTP_TRANSMIT+i] = k;
    > d -= k;
    > }


    More precisely, it is doing a float to scaled binary conversion in
    network byte order, with a hack to cope with rounding errors. I'm a bit
    uneasy about the hack.

  4. Re: sntp code: what pack_ntp is doung,

    > Pg wrote:
    >
    >> for (i = 0; i < 8; ++i) {
    >> if ((k = (int)(d *= 256.0)) >= 256) k = 255;
    >> packet[NTP_TRANSMIT+i] = k;
    >> d -= k;
    >> }

    >
    >>
    >> in this what is happening after deviding by NTP_SCALE and what is
    >> happening inside for loop.


    Shouldn't there be a comment near the code saying what it does?

    David



  5. Re: sntp code: what pack_ntp is doung,

    David J Taylor wrote:
    >> Pg wrote:
    >>
    >>> for (i = 0; i < 8; ++i) {
    >>> if ((k = (int)(d *= 256.0)) >= 256) k = 255;
    >>> packet[NTP_TRANSMIT+i] = k;
    >>> d -= k;
    >>> }
    >>> in this what is happening after deviding by NTP_SCALE and what is
    >>> happening inside for loop.

    >
    > Shouldn't there be a comment near the code saying what it does?
    >
    > David
    >
    >


    If I had written it there would be a comment. But some people believe
    that the code says what it's doing!

    It's not easy to wrap your mind around 70,000 (give or take a few
    thousand) lines of code! It's less easy when the code is not well
    commented.

    Perhaps it's obvious to "old hands" what is being done and, especially,
    why. People looking at the code for the first time might perhaps figure
    out that the order of two bytes is being reversed but probably would
    have more difficulty understanding why. It costs only a few seconds to
    type "/* Put it in network byte order. */" and it saves far more of the
    reader's time than it costs the writer! It also makes the code easier
    to maintain.

    "A program is not just a set of instructions to be executed by a
    computer; it is also a document that must be understood by human beings!"

  6. Re: sntp code: what pack_ntp is doung,

    On Apr 11, 5:45*pm, "Richard B. Gilbert"
    wrote:
    > David J Taylor wrote:
    > >> Pg wrote:

    >
    > >>> * * for (i = 0; i < 8; ++i) {
    > >>> * * * * if ((k = (int)(d *= 256.0)) >= 256) k = 255;
    > >>> * * * * packet[NTP_TRANSMIT+i] = k;
    > >>> * * * * d -= k;
    > >>> * * }
    > >>> in this what is happening after deviding by NTP_SCALE and what is
    > >>> happening inside for loop.

    >
    > > Shouldn't there be a comment near the code saying what it does?

    >
    > > David

    >
    > If I had written it there would be a comment. *But some people believe
    > that the code says what it's doing!
    >
    > It's not easy to wrap your mind around 70,000 (give or take a few
    > thousand) lines of code! *It's less easy when the code is not well
    > commented.
    >
    > Perhaps it's obvious to "old hands" what is being done and, especially,
    > why. *People looking at the code for the first time might perhaps figure
    > out that the order of two bytes is being reversed but probably would
    > have more difficulty understanding why. *It costs only a few seconds to
    > type "/* Put it in network byte order. */" and it saves far more of the
    > reader's time than it costs the writer! *It also makes the code easier
    > to maintain.
    >
    > "A program is not just a set of instructions to be executed by a
    > computer; it is also a document that must be understood by human beings!"-Hide quoted text -
    >
    > - Show quoted text -


    thanks to all,
    arun

+ Reply to Thread