2 * $Id: libnet_build_icmp.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * libnet_build_icmp.c - ICMP packet assembler
7 * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
34 #include "../include/config.h"
36 #include "../include/libnet.h"
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)
42 struct libnet_icmp_hdr icmp_hdr;
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 */
54 if (payload && payload_s)
57 * Unchecked runtime error for buf + LIBNET_ICMP_ECHO_H payload
58 * to be greater than the allocated heap memory.
60 memcpy(buf + LIBNET_ICMP_ECHO_H, payload, payload_s);
62 memcpy(buf, &icmp_hdr, LIBNET_ICMP_ECHO_H);
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)
71 struct libnet_icmp_hdr icmp_hdr;
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 */
84 if (payload && payload_s)
87 * Unchecked runtime error for buf + ICMP_H payload to begreater than
88 * the allocated heap memory.
90 memcpy(buf + LIBNET_ICMP_MASK_H, payload, payload_s);
92 memcpy(buf, &icmp_hdr, LIBNET_ICMP_MASK_H);
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)
103 struct libnet_icmp_hdr icmp_hdr;
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 */
116 * How convenient! We can use our build_ip function to tack on the
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);
123 memcpy(buf, &icmp_hdr, LIBNET_ICMP_UNREACH_H);
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)
134 struct libnet_icmp_hdr icmp_hdr;
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 */
147 * How convenient! We can use our build_ip function to tack on the
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);
154 memcpy(buf, &icmp_hdr, LIBNET_ICMP_TIMXCEED_H);
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)
164 struct libnet_icmp_hdr icmp_hdr;
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 */
179 if (payload && payload_s)
182 * Unchecked runtime error for buf + ICMP_TS_H payload to be greater
183 * than the allocated heap memory.
185 memcpy(buf + LIBNET_ICMP_TS_H, payload, payload_s);
187 memcpy(buf, &icmp_hdr, LIBNET_ICMP_TS_H);
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)
199 struct libnet_icmp_hdr icmp_hdr;
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 */
211 * How convenient! We can use our build_ip function to tack on the
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);
218 memcpy(buf, &icmp_hdr, LIBNET_ICMP_REDIRECT_H);