Annotation of embedaddon/libnet/sample/ntp.c, revision 1.1.1.2
1.1 misho 1: /*
2: * $Id: ntp.c,v 1.3 2004/02/20 18:53:49 mike Exp $
3: *
4: * libnet 1.1
5: * Build an NTP Packet
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: #include "./libnet_test.h"
37:
38: int
39: main(int argc, char **argv)
40: {
41: int c;
42: libnet_t *l;
43: u_long dst_ip;
44: libnet_ptag_t t;
45: char errbuf[LIBNET_ERRBUF_SIZE];
46:
47: printf("libnet 1.1 NTP packet shaping[raw -- autobuilding IP]\n");
48:
49: /*
50: * Initialize the library. Root priviledges are required.
51: */
52: l = libnet_init(
53: LIBNET_RAW4, /* injection type */
54: NULL, /* network interface */
55: errbuf); /* error buf */
56:
57: if (l == NULL)
58: {
59: fprintf(stderr, "libnet_init() failed: %s", errbuf);
60: exit(EXIT_FAILURE);
61: }
62:
63: dst_ip = 0;
64: while((c = getopt(argc, argv, "d:")) != EOF)
65: {
66: switch (c)
67: {
68: /*
69: * We expect the input to be of the form `ip.ip.ip.ip.port`. We
70: * point cp to the last dot of the IP address/port string and
71: * then seperate them with a NULL byte. The optarg now points to
72: * just the IP address, and cp points to the port.
73: */
74: case 'd':
75: if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
76: {
77: fprintf(stderr, "Bad destination IP address: %s\n", optarg);
78: exit(1);
79: }
80: break;
81: }
82: }
83: if (!dst_ip)
84: {
85: usage(argv[0]);
86: exit(EXIT_FAILURE);
87: }
88:
89: /*
90: * Build the packet, remmebering that order IS important. We must
91: * build the packet from lowest protocol type on up as it would
92: * appear on the wire. So for our NTP packet:
93: *
94: * --------------------------------------------------------------------
95: * | IP | UDP | NTP |
96: * --------------------------------------------------------------------
97: * ^ ^ ^
98: * |-------------- | |
99: * libnet_build_ipv4()--| | |
100: * | |
101: * libnet_build_udp()-------| |
102: * |
103: * libnet_build_ntp()------------------------------|
104: */
105: t = libnet_build_ntp(
106: LIBNET_NTP_LI_AC, /* leap indicator */
107: LIBNET_NTP_VN_4, /* version */
108: LIBNET_NTP_MODE_S, /* mode */
109: LIBNET_NTP_STRATUM_PRIMARY, /* stratum */
110: 4, /* poll interval */
111: 1, /* precision */
112: 0xffff, /* delay interval */
113: 0xffff, /* delay fraction */
114: 0xffff, /* dispersion interval */
115: 0xffff, /* dispersion fraction */
116: LIBNET_NTP_REF_PPS, /* reference id */
117: 0x11, /* reference ts int */
118: 0x22, /* reference ts frac */
119: 0x33, /* originate ts int */
120: 0x44, /* originate ts frac */
121: 0x55, /* receive ts int */
122: 0x66, /* receive ts frac */
123: 0x77, /* transmit ts interval */
124: 0x88, /* transmit ts fraction */
125: NULL, /* payload */
126: 0, /* payload size */
127: l, /* libnet handle */
128: 0); /* libnet id */
129: if (t == -1)
130: {
131: fprintf(stderr, "Can't build NTP header: %s\n", libnet_geterror(l));
132: goto bad;
133: }
134:
135: libnet_seed_prand(l);
136: t = libnet_build_udp(
137: libnet_get_prand(LIBNET_PRu16), /* source port */
138: 123, /* NTP port */
139: LIBNET_UDP_H + LIBNET_NTP_H, /* UDP packet length */
140: 0, /* checksum */
141: NULL, /* payload */
142: 0, /* payload size */
143: l, /* libnet handle */
144: 0); /* libnet id */
145: if (t == -1)
146: {
147: fprintf(stderr, "Can't build UDP header: %s\n", libnet_geterror(l));
148: goto bad;
149: }
150:
151: t = libnet_autobuild_ipv4(
152: LIBNET_IPV4_H + LIBNET_UDP_H + LIBNET_NTP_H,/* packet length */
153: IPPROTO_UDP,
154: dst_ip,
155: l);
156: if (t == -1)
157: {
158: fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
159: goto bad;
160: }
161:
162: /*
163: * Write it to the wire.
164: */
165:
166: fprintf(stderr, "l contains a %d byte packet\n", libnet_getpacket_size(l));
167: c = libnet_write(l);
168: if (c == -1)
169: {
170: fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
171: goto bad;
172: }
173: else
174: {
175: fprintf(stderr, "Wrote %d byte NTP packet; check the wire.\n", c);
176: }
177: libnet_destroy(l);
178: return (EXIT_SUCCESS);
179: bad:
180: libnet_destroy(l);
181: return (EXIT_FAILURE);
182: }
183:
184:
185: void
186: usage(char *name)
187: {
188: fprintf(stderr,
189: "usage: %s -d destination_ip\n",
190: name);
191: }
192:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>