OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / src / libnet_build_icmp.c
1 /*
2  *  $Id: libnet_build_icmp.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet
5  *  libnet_build_icmp.c - ICMP packet assembler
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 #if (HAVE_CONFIG_H)
34 #include "../include/config.h"
35 #endif
36 #include "../include/libnet.h"
37
38 int
39 libnet_build_icmp_echo(u_char type, u_char code, u_short id, u_short seq,
40             const u_char *payload, int payload_s, u_char *buf)
41 {
42     struct libnet_icmp_hdr icmp_hdr;
43
44     if (!buf)
45     {
46         return (-1);
47     }
48
49     icmp_hdr.icmp_type = type;         /* packet type */
50     icmp_hdr.icmp_code = code;         /* packet code */
51     icmp_hdr.icmp_id   = htons(id);    /* packet id */
52     icmp_hdr.icmp_seq  = htons(seq);   /* packet seq */
53
54     if (payload && payload_s)
55     {
56         /*
57          *  Unchecked runtime error for buf + LIBNET_ICMP_ECHO_H payload
58          *  to be greater than the allocated heap memory.
59          */
60         memcpy(buf + LIBNET_ICMP_ECHO_H, payload, payload_s);
61     }
62     memcpy(buf, &icmp_hdr, LIBNET_ICMP_ECHO_H);
63     return (1);
64 }
65
66
67 int
68 libnet_build_icmp_mask(u_char type, u_char code, u_short id, u_short seq,
69             u_long mask, const u_char *payload, int payload_s, u_char *buf)
70 {
71     struct libnet_icmp_hdr icmp_hdr;
72
73     if (!buf)
74     {
75         return (-1);
76     }
77
78     icmp_hdr.icmp_type = type;         /* packet type */
79     icmp_hdr.icmp_code = code;         /* packet code */
80     icmp_hdr.icmp_id   = htons(id);    /* packet id */
81     icmp_hdr.icmp_seq  = htons(seq);   /* packet seq */
82     icmp_hdr.icmp_mask = htonl(mask);  /* address mask */
83
84     if (payload && payload_s)
85     {
86         /*
87          *  Unchecked runtime error for buf + ICMP_H payload to begreater than
88          *  the allocated heap memory.
89          */
90         memcpy(buf + LIBNET_ICMP_MASK_H, payload, payload_s);
91     }
92     memcpy(buf, &icmp_hdr, LIBNET_ICMP_MASK_H);
93     return (1);
94 }
95
96
97 int
98 libnet_build_icmp_unreach(u_char type, u_char code, u_short orig_len,
99         u_char orig_tos, u_short orig_id, u_short orig_frag, u_char orig_ttl,
100         u_char orig_prot, u_long orig_src, u_long orig_dst, const u_char
101         *orig_payload, int payload_s, u_char *buf)
102 {
103     struct libnet_icmp_hdr icmp_hdr;
104
105     if (!buf)
106     {
107         return (-1);
108     }
109
110     icmp_hdr.icmp_type = type;          /* packet type */
111     icmp_hdr.icmp_code = code;          /* packet code */
112     icmp_hdr.icmp_id   = 0;             /* must be 0 */
113     icmp_hdr.icmp_seq  = 0;             /* must be 0 */
114
115     /*
116      *  How convenient!  We can use our build_ip function to tack on the
117      *  original header!
118      */
119     libnet_build_ip(0, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot,
120             orig_src, orig_dst, orig_payload, payload_s, buf +
121             LIBNET_ICMP_UNREACH_H);
122
123     memcpy(buf, &icmp_hdr, LIBNET_ICMP_UNREACH_H);
124     return (1);
125 }
126
127
128 int
129 libnet_build_icmp_timeexceed(u_char type, u_char code, u_short orig_len,
130         u_char orig_tos, u_short orig_id, u_short orig_frag, u_char orig_ttl,
131         u_char orig_prot, u_long orig_src, u_long orig_dst,
132         const u_char *orig_payload, int payload_s, u_char *buf)
133 {
134     struct libnet_icmp_hdr icmp_hdr;
135
136     if (!buf)
137     {
138         return (-1);
139     }
140
141     icmp_hdr.icmp_type = type;          /* packet type */
142     icmp_hdr.icmp_code = code;          /* packet code */
143     icmp_hdr.icmp_id   = 0;             /* must be 0 */
144     icmp_hdr.icmp_seq  = 0;             /* must be 0 */
145
146     /*
147      *  How convenient!  We can use our build_ip function to tack on the
148      *  original header!
149      */
150     libnet_build_ip(0, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot,
151             orig_src, orig_dst, orig_payload, payload_s, buf +
152             LIBNET_ICMP_TIMXCEED_H);
153
154     memcpy(buf, &icmp_hdr, LIBNET_ICMP_TIMXCEED_H);
155     return (1);
156 }
157
158
159 int
160 libnet_build_icmp_timestamp(u_char type, u_char code, u_short id, u_short seq,
161         n_time otime, n_time rtime, n_time ttime, const u_char *payload,
162         int payload_s, u_char *buf)
163 {
164     struct libnet_icmp_hdr icmp_hdr;
165
166     if (!buf)
167     {
168         return (-1);
169     }
170
171     icmp_hdr.icmp_type   = type;            /* packet type */
172     icmp_hdr.icmp_code   = code;            /* packet code */
173     icmp_hdr.icmp_id     = htons(id);       /* packet id */
174     icmp_hdr.icmp_seq    = htons(seq);      /* packet seq */
175     icmp_hdr.icmp_otime  = htonl(otime);    /* original timestamp */
176     icmp_hdr.icmp_rtime  = htonl(rtime);    /* receive timestamp */
177     icmp_hdr.icmp_ttime  = htonl(ttime);    /* transmit timestamp */
178
179     if (payload && payload_s)
180     {
181         /*
182          *  Unchecked runtime error for buf + ICMP_TS_H payload to be greater
183          *  than the allocated heap memory.
184          */
185         memcpy(buf + LIBNET_ICMP_TS_H, payload, payload_s);
186     }
187     memcpy(buf, &icmp_hdr, LIBNET_ICMP_TS_H);
188     return (1);
189 }
190
191
192 int
193 libnet_build_icmp_redirect(u_char type, u_char code, u_long gateway,
194             u_short orig_len, u_char orig_tos, u_short orig_id,
195             u_short orig_frag, u_char orig_ttl, u_char orig_prot,
196             u_long orig_src, u_long orig_dst, const u_char *orig_payload,
197             int payload_s, u_char *buf)
198 {
199     struct libnet_icmp_hdr icmp_hdr;
200
201     if (!buf)
202     {
203         return (-1);
204     }
205
206     icmp_hdr.icmp_type      = type;             /* packet type */
207     icmp_hdr.icmp_code      = code;             /* packet code */
208     icmp_hdr.hun.gateway    = htonl(gateway);   /* gateway address */
209
210     /*
211      *  How convenient!  We can use our build_ip function to tack on the
212      *  original header!
213      */
214     libnet_build_ip(0, orig_tos, orig_id, orig_frag, orig_ttl, orig_prot,
215             orig_src, orig_dst, orig_payload, payload_s, buf +
216             LIBNET_ICMP_REDIRECT_H);
217
218     memcpy(buf, &icmp_hdr, LIBNET_ICMP_REDIRECT_H);
219     return (1);
220 }
221
222 /* EOF */