File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / libnet / src / libnet_build_ntp.c
Revision 1.1.1.3 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Sep 27 11:11:38 2023 UTC (9 months, 1 week ago) by misho
Branches: libnet, MAIN
CVS tags: v1_2p1, HEAD
Version 1.2p1

    1: /*
    2:  *  $Id: libnet_build_ntp.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $
    3:  *
    4:  *  libnet
    5:  *  libnet_build_ntp.c - NTP packet assembler
    6:  *
    7:  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
    8:  *  All rights reserved.
    9:  *
   10:  * Redistribution and use in source and binary forms, with or without
   11:  * modification, are permitted provided that the following conditions
   12:  * are met:
   13:  * 1. Redistributions of source code must retain the above copyright
   14:  *    notice, this list of conditions and the following disclaimer.
   15:  * 2. Redistributions in binary form must reproduce the above copyright
   16:  *    notice, this list of conditions and the following disclaimer in the
   17:  *    documentation and/or other materials provided with the distribution.
   18:  *
   19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29:  * SUCH DAMAGE.
   30:  *
   31:  */
   32: 
   33: #include "common.h"
   34: 
   35: libnet_ptag_t
   36: libnet_build_ntp(uint8_t leap_indicator, uint8_t version, uint8_t mode,
   37: uint8_t stratum, uint8_t poll, uint8_t precision, uint16_t delay_int,
   38: uint16_t delay_frac, uint16_t dispersion_int, uint16_t dispersion_frac,
   39: uint32_t reference_id, uint32_t ref_ts_int, uint32_t ref_ts_frac,
   40: uint32_t orig_ts_int, uint32_t orig_ts_frac, uint32_t rec_ts_int,
   41: uint32_t rec_ts_frac, uint32_t xmt_ts_int, uint32_t xmt_ts_frac,
   42: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
   43: {
   44:     uint32_t n, h;
   45:     libnet_pblock_t *p;
   46:     struct libnet_ntp_hdr ntp_hdr;
   47: 
   48:     if (l == NULL)
   49:     { 
   50:         return (-1);
   51:     } 
   52: 
   53:     n = LIBNET_NTP_H + payload_s;
   54:     h = 0;
   55: 
   56:     /*
   57:      *  Find the existing protocol block if a ptag is specified, or create
   58:      *  a new one.
   59:      */
   60:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_NTP_H);
   61:     if (p == NULL)
   62:     {
   63:         return (-1);
   64:     }
   65: 
   66:     memset(&ntp_hdr, 0, sizeof(ntp_hdr));
   67:     ntp_hdr.ntp_li_vn_mode          = LIBNET_NTP_DO_LI_VN_MODE(
   68:                                       leap_indicator, version, mode);
   69:     ntp_hdr.ntp_stratum             = stratum;
   70:     ntp_hdr.ntp_poll                = poll;
   71:     ntp_hdr.ntp_precision           = precision;
   72:     ntp_hdr.ntp_delay.integer       = htons(delay_int);
   73:     ntp_hdr.ntp_delay.fraction      = htons(delay_frac);
   74:     ntp_hdr.ntp_dispersion.integer  = htons(dispersion_int);
   75:     ntp_hdr.ntp_dispersion.fraction = htons(dispersion_frac);
   76:     ntp_hdr.ntp_reference_id        = htonl(reference_id);
   77:     ntp_hdr.ntp_ref_ts.integer      = htonl(ref_ts_int);
   78:     ntp_hdr.ntp_ref_ts.fraction     = htonl(ref_ts_frac);
   79:     ntp_hdr.ntp_orig_ts.integer     = htonl(orig_ts_int);
   80:     ntp_hdr.ntp_orig_ts.fraction    = htonl(orig_ts_frac);
   81:     ntp_hdr.ntp_rec_ts.integer      = htonl(rec_ts_int);
   82:     ntp_hdr.ntp_rec_ts.fraction     = htonl(rec_ts_frac);
   83:     ntp_hdr.ntp_xmt_ts.integer      = htonl(xmt_ts_int);
   84:     ntp_hdr.ntp_xmt_ts.fraction     = htonl(xmt_ts_frac);
   85: 
   86:     n = libnet_pblock_append(l, p, (uint8_t *)&ntp_hdr, LIBNET_NTP_H);
   87:     if (n == -1)
   88:     {
   89:         goto bad;
   90:     }
   91: 
   92:     /* boilerplate payload sanity check / append macro */
   93:     LIBNET_DO_PAYLOAD(l, p);
   94: 
   95:     return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_NTP_H));
   96: bad:
   97:     libnet_pblock_delete(l, p);
   98:     return (-1);
   99: }
  100: 
  101: /**
  102:  * Local Variables:
  103:  *  indent-tabs-mode: nil
  104:  *  c-file-style: "stroustrup"
  105:  * End:
  106:  */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>