1 /*
2  *  $Id: rpc_tcp.c,v 1.2 2004/01/03 20:31:01 mike Exp $
3  *
4  *  libnet 1.1
5  *  rpc_tcp.c - Build an RPC TCP 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
main(int argc,char ** argv)39 main(int argc, char **argv)
40 {
41     int c;
42     char *cp;
43     libnet_t *l;
44     libnet_ptag_t t;
45     char *payload;
46     u_short payload_s;
47     u_long src_ip, dst_ip;
48     u_short src_prt, dst_prt;
49     char errbuf[LIBNET_ERRBUF_SIZE];
50 
51     printf("libnet 1.1 packet shaping: RPC TCP[raw]\n");
52 
53     /*
54      *  Initialize the library.  Root priviledges are required.
55      */
56     l = libnet_init(
57             LIBNET_RAW4,                            /* injection type */
58             NULL,                                   /* network interface */
59             errbuf);                                /* errbuf */
60 
61     if (l == NULL)
62     {
63         fprintf(stderr, "libnet_init() failed: %s", errbuf);
64         exit(EXIT_FAILURE);
65     }
66 
67     src_ip  = 0;
68     dst_ip  = 0;
69     src_prt = 0;
70     dst_prt = 0;
71     payload = NULL;
72     payload_s = 0;
73     while((c = getopt(argc, argv, "d:s:p:")) != EOF)
74     {
75         switch (c)
76         {
77             /*
78              *  We expect the input to be of the form `ip.ip.ip.ip.port`.  We
79              *  point cp to the last dot of the IP address/port string and
80              *  then seperate them with a NULL byte.  The optarg now points to
81              *  just the IP address, and cp points to the port.
82              */
83             case 'd':
84                 if (!(cp = strrchr(optarg, '.')))
85                 {
86                     usage(argv[0]);
87                 }
88                 *cp++ = 0;
89                 dst_prt = (u_short)atoi(cp);
90                 if ((dst_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
91                 {
92                     fprintf(stderr, "Bad destination IP address: %s\n", optarg);
93                     exit(EXIT_FAILURE);
94                 }
95                 break;
96                 break;
97             case 'p':
98                 payload = optarg;
99                 payload_s = strlen(payload);
100                 break;
101             case 's':
102                 if (!(cp = strrchr(optarg, '.')))
103                 {
104                     usage(argv[0]);
105                 }
106                 *cp++ = 0;
107                 src_prt = (u_short)atoi(cp);
108                 if ((src_ip = libnet_name2addr4(l, optarg, LIBNET_RESOLVE)) == -1)
109                 {
110                     fprintf(stderr, "Bad source IP address: %s\n", optarg);
111                     exit(EXIT_FAILURE);
112                 }
113                 break;
114         }
115     }
116     if (!src_ip || !src_prt || !dst_ip || !dst_prt)
117     {
118         usage(argv[0]);
119         exit(EXIT_FAILURE);
120     }
121 
122     t = libnet_build_rpc_call(
123         LIBNET_RPC_LAST_FRAG | LIBNET_RPC_CALL_TCP_H,
124         0x10e70082,
125         LIBNET_PMAP_PROGRAM,
126         2,
127         LIBNET_PMAP_PROC_DUMP,
128         0,
129         0,
130         NULL,
131         0,
132         0,
133         NULL,
134         payload,
135         payload_s,
136         l,
137         0);
138     if (t == -1)
139     {
140         fprintf(stderr, "Can't build RPC header: %s\n", libnet_geterror(l));
141         goto bad;
142     }
143 
144     t = libnet_build_tcp(
145         src_prt,                                    /* source port */
146         dst_prt,                                    /* destination port */
147         0x01010101,                                 /* sequence number */
148         0x02020202,                                 /* acknowledgement num */
149         TH_ACK,                                     /* control flags */
150         32767,                                      /* window size */
151         0,                                          /* checksum */
152         0,                                          /* urgent pointer */
153         LIBNET_TCP_H + 44 + payload_s,              /* TCP packet size */
154         NULL,                                       /* payload */
155         0,                                          /* payload size */
156         l,                                          /* libnet handle */
157         0);                                         /* libnet id */
158     if (t == -1)
159     {
160         fprintf(stderr, "Can't build TCP header: %s\n", libnet_geterror(l));
161         goto bad;
162     }
163 
164     t = libnet_build_ipv4(
165         LIBNET_IPV4_H + LIBNET_TCP_H + 44 + payload_s,/* length */
166         0,                                          /* TOS */
167         242,                                        /* IP ID */
168         0,                                          /* IP Frag */
169         64,                                         /* TTL */
170         IPPROTO_TCP,                                /* protocol */
171         0,                                          /* checksum */
172         src_ip,                                     /* source IP */
173         dst_ip,                                     /* destination IP */
174         NULL,                                       /* payload */
175         0,                                          /* payload size */
176         l,                                          /* libnet handle */
177         0);                                         /* libnet id */
178     if (t == -1)
179     {
180         fprintf(stderr, "Can't build IP header: %s\n", libnet_geterror(l));
181         goto bad;
182     }
183 
184     /*
185      *  Write it to the wire.
186      */
187     c = libnet_write(l);
188     if (c == -1)
189     {
190         fprintf(stderr, "Write error: %s\n", libnet_geterror(l));
191         goto bad;
192     }
193     else
194     {
195         fprintf(stderr, "Wrote %d byte RPC TCP packet; check the wire.\n", c);
196     }
197     libnet_destroy(l);
198     return (EXIT_SUCCESS);
199 bad:
200     libnet_destroy(l);
201     return (EXIT_FAILURE);
202 }
203 
204 
205 void
usage(char * name)206 usage(char *name)
207 {
208     fprintf(stderr,
209         "usage: %s -s source_ip.source_port -d destination_ip.destination_port"
210         " [-p payload]\n",
211         name);
212 }
213 
214 /* EOF */
215