OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / example / libnet-example-1.c
1 /*
2  *  $Id: libnet-example-1.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet example code
5  *  example 1:  raw socket api / TCP packet
6  *
7  *  Copyright (c) 1998 - 2001 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 <libnet.h>
34
35 void usage(char *);
36
37 int
38 main(int argc, char **argv)
39 {
40     int network, packet_size, c;
41     u_long src_ip, dst_ip;
42     u_short src_prt, dst_prt;
43     u_char *cp, *packet;
44
45     printf("libnet example code:\tmodule 1\n\n");
46     printf("packet injection interface:\traw socket\n");
47     printf("packet type:\t\t\tTCP [no payload]\n");
48
49     src_ip  = 0;
50     dst_ip  = 0;
51     src_prt = 0;
52     dst_prt = 0;
53
54     while((c = getopt(argc, argv, "d:s:")) != EOF)
55     {      
56         switch (c)
57         {
58             /*
59              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
60              *  point cp to the last dot of the IP address/port string and
61              *  then seperate them with a NULL byte.  The optarg now points to
62              *  just the IP address, and cp points to the port.
63              */
64             case 'd':
65                 if (!(cp = strrchr(optarg, '.')))
66                 {
67                     usage(argv[0]);
68                 }
69                 *cp++ = 0;
70                 dst_prt = (u_short)atoi(cp);
71                 if (!(dst_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
72                 {
73                     libnet_error(LIBNET_ERR_FATAL, "Bad destination IP address: %s\n", optarg);
74                 }
75                 break;
76             case 's':
77                 if (!(cp = strrchr(optarg, '.')))
78                 {
79                     usage(argv[0]);
80                 }
81                 *cp++ = 0;
82                 src_prt = (u_short)atoi(cp);
83                 if (!(src_ip = libnet_name_resolve(optarg, LIBNET_RESOLVE)))
84                 {
85                     libnet_error(LIBNET_ERR_FATAL, "Bad source IP address: %s\n", optarg);
86                 }
87                 break;
88         }
89     }
90     if (!src_ip || !src_prt || !dst_ip || !dst_prt)
91     {
92         usage(argv[0]);
93         exit(EXIT_FAILURE);
94     }
95
96     /*
97      *  We're just going to build a TCP packet with no payload using the
98      *  raw sockets API, so we only need memory for a TCP header and an IP
99      *  header.
100      */
101     packet_size = LIBNET_IP_H + LIBNET_TCP_H;
102
103     /*
104      *  Step 1: Memory initialization (interchangable with step 2).
105      */
106     libnet_init_packet(packet_size, &packet);
107     if (packet == NULL)
108     {
109         libnet_error(LIBNET_ERR_FATAL, "libnet_init_packet failed\n");
110     }
111
112     /*
113      *  Step 2: Network initialization (interchangable with step 1).
114      */
115     network = libnet_open_raw_sock(IPPROTO_RAW);
116     if (network == -1)
117     {
118         libnet_error(LIBNET_ERR_FATAL, "Can't open network.\n");
119     }
120     
121
122     /*
123      *  Step 3: Packet construction (IP header).
124      */
125     libnet_build_ip(LIBNET_TCP_H,   /* size of the packet sans IP header */
126             IPTOS_LOWDELAY,         /* IP tos */
127             242,                    /* IP ID */
128             0,                      /* frag stuff */
129             48,                     /* TTL */
130             IPPROTO_TCP,            /* transport protocol */
131             src_ip,                 /* source IP */
132             dst_ip,                 /* destination IP */
133             NULL,                   /* payload (none) */
134             0,                      /* payload length */
135             packet);                /* packet header memory */
136
137
138     /*
139      *  Step 3: Packet construction (TCP header).
140      */
141     libnet_build_tcp(src_prt,       /* source TCP port */
142             dst_prt,                /* destination TCP port */
143             0xa1d95,                /* sequence number */
144             0x53,                   /* acknowledgement number */
145             TH_SYN,                 /* control flags */
146             1024,                   /* window size */
147             0,                      /* urgent pointer */
148             NULL,                   /* payload (none) */
149             0,                      /* payload length */
150             packet + LIBNET_IP_H);  /* packet header memory */
151
152
153     /*
154      *  Step 4: Packet checksums (TCP header only).
155      */
156     if (libnet_do_checksum(packet, IPPROTO_TCP, LIBNET_TCP_H) == -1)
157     {
158         libnet_error(LIBNET_ERR_FATAL, "libnet_do_checksum failed\n");
159     }
160
161
162     /*
163      *  Step 5: Packet injection.
164      */
165     c = libnet_write_ip(network, packet, packet_size);
166     if (c < packet_size)
167     {
168         libnet_error(LN_ERR_WARNING, "libnet_write_ip only wrote %d bytes\n", c);
169     }
170     else
171     {
172         printf("construction and injection completed, wrote all %d bytes\n", c);
173     }
174
175     /*
176      *  Shut down the interface.
177      */
178     if (libnet_close_raw_sock(network) == -1)
179     {
180         libnet_error(LN_ERR_WARNING, "libnet_close_raw_sock couldn't close the interface");
181     }
182
183
184     /*
185      *  Free packet memory.
186      */
187     libnet_destroy_packet(&packet);
188
189     return (c == -1 ? EXIT_FAILURE : EXIT_SUCCESS);
190 }
191
192
193 void
194 usage(char *name)
195 {
196     fprintf(stderr, "usage: %s -s s_ip.s_port -d d_ip.d_port\n", name);
197 }
198
199
200 /* EOF */