tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / dhcp / common / ctrace.c
bloba4a6fe84829b4b408424f9f808c333e7a0f49265
1 /* trace.c
3 Subroutines that support dhcp tracing... */
5 /*
6 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 2001-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
22 * 950 Charter Street
23 * Redwood City, CA 94063
24 * <info@isc.org>
25 * http://www.isc.org/
27 * This software has been written for Internet Systems Consortium
28 * by Ted Lemon, as part of a project for Nominum, Inc. To learn more
29 * about Internet Systems Consortium, see http://www.isc.org/. To
30 * learn more about Nominum, Inc., see ``http://www.nominum.com''.
33 #if 0
34 static char copyright[] =
35 "$Id$ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
36 #endif
38 #include "dhcpd.h"
40 #if defined (TRACING)
41 void trace_interface_register (trace_type_t *ttype, struct interface_info *ip)
43 trace_interface_packet_t tipkt;
45 if (trace_record ()) {
46 memset (&tipkt, 0, sizeof tipkt);
47 memcpy (&tipkt.hw_address,
48 &ip -> hw_address, sizeof ip -> hw_address);
49 memcpy (&tipkt.primary_address,
50 &ip -> primary_address, sizeof ip -> primary_address);
51 memcpy (tipkt.name, ip -> name, sizeof ip -> name);
52 tipkt.index = htonl (ip -> index);
54 trace_write_packet (ttype, sizeof tipkt, (char *)&tipkt, MDL);
58 void trace_interface_input (trace_type_t *ttype, unsigned len, char *buf)
60 trace_interface_packet_t *tipkt;
61 struct interface_info *ip;
62 struct sockaddr_in *sin;
63 struct iaddr addr;
64 isc_result_t status;
66 if (len != sizeof *tipkt) {
67 log_error ("trace interface packet size mismatch: %ld != %d",
68 (long)(sizeof *tipkt), len);
69 return;
71 tipkt = (trace_interface_packet_t *)buf;
73 ip = (struct interface_info *)0;
74 status = interface_allocate (&ip, MDL);
75 if (status != ISC_R_SUCCESS) {
76 foo:
77 log_error ("trace_interface_input: %s.",
78 isc_result_totext (status));
79 return;
81 ip -> ifp = dmalloc (sizeof *(ip -> ifp), MDL);
82 if (!ip -> ifp) {
83 interface_dereference (&ip, MDL);
84 status = ISC_R_NOMEMORY;
85 goto foo;
88 memcpy (&ip -> hw_address, &tipkt -> hw_address,
89 sizeof ip -> hw_address);
90 memcpy (&ip -> primary_address, &tipkt -> primary_address,
91 sizeof ip -> primary_address);
92 memcpy (ip -> name, tipkt -> name, sizeof ip -> name);
93 ip -> index = ntohl (tipkt -> index);
95 interface_snorf (ip, 0);
96 if (dhcp_interface_discovery_hook)
97 (*dhcp_interface_discovery_hook) (ip);
99 /* Fake up an ifp. */
100 memcpy (ip -> ifp -> ifr_name, ip -> name, sizeof ip -> name);
101 #ifdef HAVE_SA_LEN
102 ip -> ifp -> ifr_addr.sa_len = sizeof (struct sockaddr_in);
103 #endif
104 sin = (struct sockaddr_in *)&ip -> ifp -> ifr_addr;
105 sin -> sin_addr = ip -> primary_address;
107 addr.len = 4;
108 memcpy (addr.iabuf, &sin -> sin_addr.s_addr, addr.len);
109 if (dhcp_interface_setup_hook)
110 (*dhcp_interface_setup_hook) (ip, &addr);
111 interface_stash (ip);
113 if (!quiet_interface_discovery) {
114 log_info ("Listening on Trace/%s/%s%s%s",
115 ip -> name,
116 print_hw_addr (ip -> hw_address.hbuf [0],
117 ip -> hw_address.hlen - 1,
118 &ip -> hw_address.hbuf [1]),
119 (ip -> shared_network ? "/" : ""),
120 (ip -> shared_network ?
121 ip -> shared_network -> name : ""));
122 if (strcmp (ip -> name, "fallback")) {
123 log_info ("Sending on Trace/%s/%s%s%s",
124 ip -> name,
125 print_hw_addr (ip -> hw_address.hbuf [0],
126 ip -> hw_address.hlen - 1,
127 &ip -> hw_address.hbuf [1]),
128 (ip -> shared_network ? "/" : ""),
129 (ip -> shared_network ?
130 ip -> shared_network -> name : ""));
133 interface_dereference (&ip, MDL);
136 void trace_interface_stop (trace_type_t *ttype) {
137 /* XXX */
140 void trace_inpacket_stash (struct interface_info *interface,
141 struct dhcp_packet *packet,
142 unsigned len,
143 unsigned int from_port,
144 struct iaddr from,
145 struct hardware *hfrom)
147 trace_inpacket_t tip;
148 trace_iov_t iov [2];
150 if (!trace_record ())
151 return;
152 tip.from_port = from_port;
153 tip.from = from;
154 tip.from.len = htonl (tip.from.len);
155 if (hfrom) {
156 tip.hfrom = *hfrom;
157 tip.havehfrom = 1;
158 } else {
159 memset (&tip.hfrom, 0, sizeof tip.hfrom);
160 tip.havehfrom = 0;
162 tip.index = htonl (interface -> index);
164 iov [0].buf = (char *)&tip;
165 iov [0].len = sizeof tip;
166 iov [1].buf = (char *)packet;
167 iov [1].len = len;
168 trace_write_packet_iov (inpacket_trace, 2, iov, MDL);
171 void trace_inpacket_input (trace_type_t *ttype, unsigned len, char *buf)
173 trace_inpacket_t *tip;
174 int index;
176 if (len < sizeof *tip) {
177 log_error ("trace_input_packet: too short - %d", len);
178 return;
180 tip = (trace_inpacket_t *)buf;
181 index = ntohl (tip -> index);
182 tip -> from.len = ntohl (tip -> from.len);
184 if (index > interface_count ||
185 index < 0 ||
186 !interface_vector [index]) {
187 log_error ("trace_input_packet: unknown interface index %d",
188 index);
189 return;
192 if (!bootp_packet_handler) {
193 log_error ("trace_input_packet: no bootp packet handler.");
194 return;
197 (*bootp_packet_handler) (interface_vector [index],
198 (struct dhcp_packet *)(tip + 1),
199 len - sizeof *tip,
200 tip -> from_port,
201 tip -> from,
202 (tip -> havehfrom ?
203 &tip -> hfrom
204 : (struct hardware *)0));
207 void trace_inpacket_stop (trace_type_t *ttype) { }
209 ssize_t trace_packet_send (struct interface_info *interface,
210 struct packet *packet,
211 struct dhcp_packet *raw,
212 size_t len,
213 struct in_addr from,
214 struct sockaddr_in *to,
215 struct hardware *hto)
217 trace_outpacket_t tip;
218 trace_iov_t iov [2];
220 if (trace_record ()) {
221 if (hto) {
222 tip.hto = *hto;
223 tip.havehto = 1;
224 } else {
225 memset (&tip.hto, 0, sizeof tip.hto);
226 tip.havehto = 0;
228 tip.from.len = 4;
229 memcpy (tip.from.iabuf, &from, 4);
230 tip.to.len = 4;
231 memcpy (tip.to.iabuf, &to -> sin_addr, 4);
232 tip.to_port = to -> sin_port;
233 tip.index = htonl (interface -> index);
235 iov [0].buf = (char *)&tip;
236 iov [0].len = sizeof tip;
237 iov [1].buf = (char *)raw;
238 iov [1].len = len;
239 trace_write_packet_iov (outpacket_trace, 2, iov, MDL);
241 if (!trace_playback ()) {
242 return send_packet (interface, packet, raw, len,
243 from, to, hto);
245 return len;
248 void trace_outpacket_input (trace_type_t *ttype, unsigned len, char *buf)
250 trace_outpacket_t *tip;
251 int index;
253 if (len < sizeof *tip) {
254 log_error ("trace_input_packet: too short - %d", len);
255 return;
257 tip = (trace_outpacket_t *)buf;
258 index = ntohl (tip -> index);
260 if (index > interface_count ||
261 index < 0 ||
262 !interface_vector [index]) {
263 log_error ("trace_input_packet: unknown interface index %d",
264 index);
265 return;
268 /* XXX would be nice to somehow take notice of these. */
271 void trace_outpacket_stop (trace_type_t *ttype) { }
273 void trace_seed_stash (trace_type_t *ttype, unsigned seed)
275 u_int32_t outseed;
276 if (!trace_record ())
277 return;
278 outseed = htonl (seed);
279 trace_write_packet (ttype, sizeof outseed, (char *)&outseed, MDL);
280 return;
283 void trace_seed_input (trace_type_t *ttype, unsigned length, char *buf)
285 u_int32_t *seed;
287 if (length != sizeof seed) {
288 log_error ("trace_seed_input: wrong size (%d)", length);
290 seed = (u_int32_t *)buf;
291 srandom (ntohl (*seed));
294 void trace_seed_stop (trace_type_t *ttype) { }
295 #endif /* TRACING */