Annotation of embedaddon/libnet/src/libnet_build_ntp.c, revision 1.1.1.2
1.1 misho 1: /*
1.1.1.2 ! misho 2: * $Id: libnet_build_ntp.c,v 1.11 2004/11/09 07:05:07 mike Exp $
1.1 misho 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
1.1.1.2 ! misho 43: libnet_build_ntp(uint8_t leap_indicator, uint8_t version, uint8_t mode,
! 44: uint8_t stratum, uint8_t poll, uint8_t precision, uint16_t delay_int,
! 45: uint16_t delay_frac, uint16_t dispersion_int, uint16_t dispersion_frac,
! 46: uint32_t reference_id, uint32_t ref_ts_int, uint32_t ref_ts_frac,
! 47: uint32_t orig_ts_int, uint32_t orig_ts_frac, uint32_t rec_ts_int,
! 48: uint32_t rec_ts_frac, uint32_t xmt_ts_int, uint32_t xmt_ts_frac,
! 49: const uint8_t *payload, uint32_t payload_s, libnet_t *l, libnet_ptag_t ptag)
1.1 misho 50: {
1.1.1.2 ! misho 51: uint32_t n, h;
1.1 misho 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);
1.1.1.2 ! misho 88: ntp_hdr.ntp_rec_ts.integer = htonl(rec_ts_int);
! 89: ntp_hdr.ntp_rec_ts.fraction = htonl(rec_ts_frac);
1.1 misho 90: ntp_hdr.ntp_xmt_ts.integer = htonl(xmt_ts_int);
91: ntp_hdr.ntp_xmt_ts.fraction = htonl(xmt_ts_frac);
92:
1.1.1.2 ! misho 93: n = libnet_pblock_append(l, p, (uint8_t *)&ntp_hdr, LIBNET_NTP_H);
1.1 misho 94: if (n == -1)
95: {
96: goto bad;
97: }
98:
1.1.1.2 ! misho 99: /* boilerplate payload sanity check / append macro */
! 100: LIBNET_DO_PAYLOAD(l, p);
1.1 misho 101:
102: return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_NTP_H));
103: bad:
104: libnet_pblock_delete(l, p);
105: return (-1);
106: }
107:
108: /* EOF */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>