Annotation of embedaddon/libnet/src/libnet_build_ntp.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  *  $Id: libnet_build_ntp.c,v 1.9 2004/03/01 20:26:12 mike 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: #if (HAVE_CONFIG_H)
                     34: #include "../include/config.h"
                     35: #endif
                     36: #if (!(_WIN32) || (__CYGWIN__)) 
                     37: #include "../include/libnet.h"
                     38: #else
                     39: #include "../include/win32/libnet.h"
                     40: #endif
                     41: 
                     42: libnet_ptag_t
                     43: libnet_build_ntp(u_int8_t leap_indicator, u_int8_t version, u_int8_t mode,
                     44: u_int8_t stratum, u_int8_t poll, u_int8_t precision, u_int16_t delay_int,
                     45: u_int16_t delay_frac, u_int16_t dispersion_int, u_int16_t dispersion_frac,
                     46: u_int32_t reference_id, u_int32_t ref_ts_int, u_int32_t ref_ts_frac,
                     47: u_int32_t orig_ts_int, u_int32_t orig_ts_frac, u_int32_t rec_ts_int,
                     48: u_int32_t rec_ts_frac, u_int32_t xmt_ts_int, u_int32_t xmt_ts_frac,
                     49: u_int8_t *payload, u_int32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
                     50: {
                     51:     u_int32_t n, h;
                     52:     libnet_pblock_t *p;
                     53:     struct libnet_ntp_hdr ntp_hdr;
                     54: 
                     55:     if (l == NULL)
                     56:     { 
                     57:         return (-1);
                     58:     } 
                     59: 
                     60:     n = LIBNET_NTP_H + payload_s;
                     61:     h = 0;
                     62: 
                     63:     /*
                     64:      *  Find the existing protocol block if a ptag is specified, or create
                     65:      *  a new one.
                     66:      */
                     67:     p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_NTP_H);
                     68:     if (p == NULL)
                     69:     {
                     70:         return (-1);
                     71:     }
                     72: 
                     73:     memset(&ntp_hdr, 0, sizeof(ntp_hdr));
                     74:     ntp_hdr.ntp_li_vn_mode          = LIBNET_NTP_DO_LI_VN_MODE(
                     75:                                       leap_indicator, version, mode);
                     76:     ntp_hdr.ntp_stratum             = stratum;
                     77:     ntp_hdr.ntp_poll                = poll;
                     78:     ntp_hdr.ntp_precision           = precision;
                     79:     ntp_hdr.ntp_delay.integer       = htons(delay_int);
                     80:     ntp_hdr.ntp_delay.fraction      = htons(delay_frac);
                     81:     ntp_hdr.ntp_dispersion.integer  = htons(dispersion_int);
                     82:     ntp_hdr.ntp_dispersion.fraction = htons(dispersion_frac);
                     83:     ntp_hdr.ntp_reference_id        = htonl(reference_id);
                     84:     ntp_hdr.ntp_ref_ts.integer      = htonl(ref_ts_int);
                     85:     ntp_hdr.ntp_ref_ts.fraction     = htonl(ref_ts_frac);
                     86:     ntp_hdr.ntp_orig_ts.integer     = htonl(orig_ts_int);
                     87:     ntp_hdr.ntp_orig_ts.fraction    = htonl(orig_ts_frac);
                     88:     ntp_hdr.ntp_rec_ts.integer      = htonl(orig_ts_int);
                     89:     ntp_hdr.ntp_rec_ts.fraction     = htonl(orig_ts_frac);
                     90:     ntp_hdr.ntp_xmt_ts.integer      = htonl(xmt_ts_int);
                     91:     ntp_hdr.ntp_xmt_ts.fraction     = htonl(xmt_ts_frac);
                     92: 
                     93:     n = libnet_pblock_append(l, p, (u_int8_t *)&ntp_hdr, LIBNET_NTP_H);
                     94:     if (n == -1)
                     95:     {
                     96:         goto bad;
                     97:     }
                     98: 
                     99:     if ((payload && !payload_s) || (!payload && payload_s))
                    100:     {
                    101:         snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
                    102:                 "%s(): payload inconsistency\n", __func__);
                    103:         goto bad;
                    104:     }
                    105: 
                    106:     if (payload && payload_s)
                    107:     {
                    108:         n = libnet_pblock_append(l, p, payload, payload_s);
                    109:         if (n == -1)
                    110:         {
                    111:             goto bad;
                    112:         }
                    113:     }
                    114: 
                    115:     return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_NTP_H));
                    116: bad:
                    117:     libnet_pblock_delete(l, p);
                    118:     return (-1);
                    119: }
                    120: 
                    121: /* EOF */

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