trace.c revision 1.1 1 1.1 christos /* $NetBSD: trace.c,v 1.1 2018/04/07 22:34:27 christos Exp $ */
2 1.1 christos
3 1.1 christos /* trace.c
4 1.1 christos
5 1.1 christos Subroutines that support tracing of OMAPI wire transactions and
6 1.1 christos provide a mechanism for programs using OMAPI to trace their own
7 1.1 christos transactions... */
8 1.1 christos
9 1.1 christos /*
10 1.1 christos * Copyright (c) 2004-2017 by Internet Systems Consortium, Inc. ("ISC")
11 1.1 christos * Copyright (c) 2001-2003 by Internet Software Consortium
12 1.1 christos *
13 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
14 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
15 1.1 christos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
16 1.1 christos *
17 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
18 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
20 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
23 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 1.1 christos *
25 1.1 christos * Internet Systems Consortium, Inc.
26 1.1 christos * 950 Charter Street
27 1.1 christos * Redwood City, CA 94063
28 1.1 christos * <info (at) isc.org>
29 1.1 christos * https://www.isc.org/
30 1.1 christos *
31 1.1 christos */
32 1.1 christos
33 1.1 christos #include <sys/cdefs.h>
34 1.1 christos __RCSID("$NetBSD: trace.c,v 1.1 2018/04/07 22:34:27 christos Exp $");
35 1.1 christos
36 1.1 christos #include "dhcpd.h"
37 1.1 christos #include <omapip/omapip_p.h>
38 1.1 christos #include <errno.h>
39 1.1 christos
40 1.1 christos #if defined (TRACING)
41 1.1 christos void (*trace_set_time_hook) (TIME);
42 1.1 christos static int tracing_stopped;
43 1.1 christos static int traceoutfile;
44 1.1 christos static int traceindex;
45 1.1 christos static trace_type_t **trace_types;
46 1.1 christos static int trace_type_count;
47 1.1 christos static int trace_type_max;
48 1.1 christos static trace_type_t *new_trace_types;
49 1.1 christos static FILE *traceinfile;
50 1.1 christos static tracefile_header_t tracefile_header;
51 1.1 christos static int trace_playback_flag;
52 1.1 christos trace_type_t trace_time_marker;
53 1.1 christos
54 1.1 christos #if defined (DEBUG_MEMORY_LEAKAGE) || defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
55 1.1 christos extern omapi_array_t *trace_listeners;
56 1.1 christos extern omapi_array_t *omapi_connections;
57 1.1 christos
58 1.1 christos extern int errno;
59 1.1 christos
60 1.1 christos void trace_free_all ()
61 1.1 christos {
62 1.1 christos trace_type_t *tp;
63 1.1 christos int i;
64 1.1 christos tp = new_trace_types;
65 1.1 christos while (tp) {
66 1.1 christos new_trace_types = tp -> next;
67 1.1 christos if (tp -> name) {
68 1.1 christos dfree (tp -> name, MDL);
69 1.1 christos tp -> name = (char *)0;
70 1.1 christos }
71 1.1 christos dfree (tp, MDL);
72 1.1 christos tp = new_trace_types;
73 1.1 christos }
74 1.1 christos for (i = 0; i < trace_type_count; i++) {
75 1.1 christos if (trace_types [i]) {
76 1.1 christos if (trace_types [i] -> name)
77 1.1 christos dfree (trace_types [i] -> name, MDL);
78 1.1 christos dfree (trace_types [i], MDL);
79 1.1 christos }
80 1.1 christos }
81 1.1 christos dfree (trace_types, MDL);
82 1.1 christos trace_types = (trace_type_t **)0;
83 1.1 christos trace_type_count = trace_type_max = 0;
84 1.1 christos
85 1.1 christos omapi_array_free (&trace_listeners, MDL);
86 1.1 christos omapi_array_free (&omapi_connections, MDL);
87 1.1 christos }
88 1.1 christos #endif
89 1.1 christos
90 1.1 christos static isc_result_t trace_type_record (trace_type_t *,
91 1.1 christos unsigned, const char *, int);
92 1.1 christos
93 1.1 christos int trace_playback ()
94 1.1 christos {
95 1.1 christos return trace_playback_flag;
96 1.1 christos }
97 1.1 christos
98 1.1 christos int trace_record ()
99 1.1 christos {
100 1.1 christos if (traceoutfile && !tracing_stopped)
101 1.1 christos return 1;
102 1.1 christos return 0;
103 1.1 christos }
104 1.1 christos
105 1.1 christos isc_result_t trace_init (void (*set_time) (TIME),
106 1.1 christos const char *file, int line)
107 1.1 christos {
108 1.1 christos trace_type_t *root_type;
109 1.1 christos static int root_setup = 0;
110 1.1 christos
111 1.1 christos if (root_setup)
112 1.1 christos return ISC_R_SUCCESS;
113 1.1 christos
114 1.1 christos trace_set_time_hook = set_time;
115 1.1 christos
116 1.1 christos root_type = trace_type_register ("trace-index-mapping",
117 1.1 christos (void *)0, trace_index_map_input,
118 1.1 christos trace_index_stop_tracing, file, line);
119 1.1 christos if (!root_type)
120 1.1 christos return ISC_R_UNEXPECTED;
121 1.1 christos if (new_trace_types == root_type)
122 1.1 christos new_trace_types = new_trace_types -> next;
123 1.1 christos root_type -> index = 0;
124 1.1 christos trace_type_stash (root_type);
125 1.1 christos
126 1.1 christos root_setup = 1;
127 1.1 christos return ISC_R_SUCCESS;
128 1.1 christos }
129 1.1 christos
130 1.1 christos isc_result_t trace_begin (const char *filename,
131 1.1 christos const char *file, int line)
132 1.1 christos {
133 1.1 christos tracefile_header_t tfh;
134 1.1 christos int status;
135 1.1 christos trace_type_t *tptr, *next;
136 1.1 christos isc_result_t result;
137 1.1 christos
138 1.1 christos if (traceoutfile) {
139 1.1 christos log_error ("%s(%d): trace_begin called twice",
140 1.1 christos file, line);
141 1.1 christos return DHCP_R_INVALIDARG;
142 1.1 christos }
143 1.1 christos
144 1.1 christos traceoutfile = open (filename, O_CREAT | O_WRONLY | O_EXCL, 0600);
145 1.1 christos if (traceoutfile < 0 && errno == EEXIST) {
146 1.1 christos log_error ("WARNING: Overwriting trace file \"%s\"", filename);
147 1.1 christos traceoutfile = open (filename, O_WRONLY | O_EXCL | O_TRUNC,
148 1.1 christos 0600);
149 1.1 christos }
150 1.1 christos
151 1.1 christos if (traceoutfile < 0) {
152 1.1 christos log_error ("%s(%d): trace_begin: %s: %m",
153 1.1 christos file, line, filename);
154 1.1 christos return ISC_R_UNEXPECTED;
155 1.1 christos }
156 1.1 christos #if defined (HAVE_SETFD)
157 1.1 christos if (fcntl (traceoutfile, F_SETFD, 1) < 0)
158 1.1 christos log_error ("Can't set close-on-exec on %s: %m", filename);
159 1.1 christos #endif
160 1.1 christos
161 1.1 christos tfh.magic = htonl (TRACEFILE_MAGIC);
162 1.1 christos tfh.version = htonl (TRACEFILE_VERSION);
163 1.1 christos tfh.hlen = htonl (sizeof (tracefile_header_t));
164 1.1 christos tfh.phlen = htonl (sizeof (tracepacket_t));
165 1.1 christos
166 1.1 christos status = write (traceoutfile, &tfh, sizeof tfh);
167 1.1 christos if (status < 0) {
168 1.1 christos log_error ("%s(%d): trace_begin write failed: %m", file, line);
169 1.1 christos return ISC_R_UNEXPECTED;
170 1.1 christos } else if (status != sizeof tfh) {
171 1.1 christos log_error ("%s(%d): trace_begin: short write (%d:%ld)",
172 1.1 christos file, line, status, (long)(sizeof tfh));
173 1.1 christos trace_stop ();
174 1.1 christos return ISC_R_UNEXPECTED;
175 1.1 christos }
176 1.1 christos
177 1.1 christos /* Stash all the types that have already been set up. */
178 1.1 christos if (new_trace_types) {
179 1.1 christos next = new_trace_types;
180 1.1 christos new_trace_types = (trace_type_t *)0;
181 1.1 christos for (tptr = next; tptr; tptr = next) {
182 1.1 christos next = tptr -> next;
183 1.1 christos if (tptr -> index != 0) {
184 1.1 christos result = (trace_type_record
185 1.1 christos (tptr,
186 1.1 christos strlen (tptr -> name), file, line));
187 1.1 christos if (result != ISC_R_SUCCESS)
188 1.1 christos return status;
189 1.1 christos }
190 1.1 christos }
191 1.1 christos }
192 1.1 christos
193 1.1 christos return ISC_R_SUCCESS;
194 1.1 christos }
195 1.1 christos
196 1.1 christos isc_result_t trace_write_packet (trace_type_t *ttype, unsigned length,
197 1.1 christos const char *buf, const char *file, int line)
198 1.1 christos {
199 1.1 christos trace_iov_t iov;
200 1.1 christos
201 1.1 christos iov.buf = buf;
202 1.1 christos iov.len = length;
203 1.1 christos return trace_write_packet_iov (ttype, 1, &iov, file, line);
204 1.1 christos }
205 1.1 christos
206 1.1 christos isc_result_t trace_write_packet_iov (trace_type_t *ttype,
207 1.1 christos int count, trace_iov_t *iov,
208 1.1 christos const char *file, int line)
209 1.1 christos {
210 1.1 christos tracepacket_t tmp;
211 1.1 christos int status;
212 1.1 christos int i;
213 1.1 christos int length;
214 1.1 christos
215 1.1 christos /* Really shouldn't get called here, but it may be hard to turn off
216 1.1 christos tracing midstream if the trace file write fails or something. */
217 1.1 christos if (tracing_stopped)
218 1.1 christos return 0;
219 1.1 christos
220 1.1 christos if (!ttype) {
221 1.1 christos log_error ("%s(%d): trace_write_packet with null trace type",
222 1.1 christos file ? file : "<unknown file>", line);
223 1.1 christos return DHCP_R_INVALIDARG;
224 1.1 christos }
225 1.1 christos if (!traceoutfile) {
226 1.1 christos log_error ("%s(%d): trace_write_packet with no tracefile.",
227 1.1 christos file ? file : "<unknown file>", line);
228 1.1 christos return DHCP_R_INVALIDARG;
229 1.1 christos }
230 1.1 christos
231 1.1 christos /* Compute the total length of the iov. */
232 1.1 christos length = 0;
233 1.1 christos for (i = 0; i < count; i++)
234 1.1 christos length += iov [i].len;
235 1.1 christos
236 1.1 christos /* We have to swap out the data, because it may be read back on a
237 1.1 christos machine of different endianness. */
238 1.1 christos memset(&tmp, 0, sizeof(tmp));
239 1.1 christos tmp.type_index = htonl (ttype -> index);
240 1.1 christos tmp.when = htonl (time ((time_t *)0)); /* XXX */
241 1.1 christos tmp.length = htonl (length);
242 1.1 christos
243 1.1 christos status = write (traceoutfile, &tmp, sizeof tmp);
244 1.1 christos if (status < 0) {
245 1.1 christos log_error ("%s(%d): trace_write_packet write failed: %m",
246 1.1 christos file, line);
247 1.1 christos return ISC_R_UNEXPECTED;
248 1.1 christos } else if (status != sizeof tmp) {
249 1.1 christos log_error ("%s(%d): trace_write_packet: short write (%d:%ld)",
250 1.1 christos file, line, status, (long)(sizeof tmp));
251 1.1 christos trace_stop ();
252 1.1 christos }
253 1.1 christos
254 1.1 christos for (i = 0; i < count; i++) {
255 1.1 christos status = write (traceoutfile, iov [i].buf, iov [i].len);
256 1.1 christos if (status < 0) {
257 1.1 christos log_error ("%s(%d): %s write failed: %m",
258 1.1 christos file, line, "trace_write_packet");
259 1.1 christos return ISC_R_UNEXPECTED;
260 1.1 christos } else if (status != iov [i].len) {
261 1.1 christos log_error ("%s(%d): %s: short write (%d:%d)",
262 1.1 christos file, line,
263 1.1 christos "trace_write_packet", status, length);
264 1.1 christos trace_stop ();
265 1.1 christos }
266 1.1 christos }
267 1.1 christos
268 1.1 christos /* Write padding on the end of the packet to align the next
269 1.1 christos packet to an 8-byte boundary. This is in case we decide to
270 1.1 christos use mmap in some clever way later on. */
271 1.1 christos if (length % 8) {
272 1.1 christos static char zero [] = { 0, 0, 0, 0, 0, 0, 0 };
273 1.1 christos unsigned padl = 8 - (length % 8);
274 1.1 christos
275 1.1 christos status = write (traceoutfile, zero, padl);
276 1.1 christos if (status < 0) {
277 1.1 christos log_error ("%s(%d): trace_write_packet write failed: %m",
278 1.1 christos file, line);
279 1.1 christos return ISC_R_UNEXPECTED;
280 1.1 christos } else if (status != padl) {
281 1.1 christos log_error ("%s(%d): trace_write_packet: short write (%d:%d)",
282 1.1 christos file, line, status, padl);
283 1.1 christos trace_stop ();
284 1.1 christos }
285 1.1 christos }
286 1.1 christos
287 1.1 christos return ISC_R_SUCCESS;
288 1.1 christos }
289 1.1 christos
290 1.1 christos void trace_type_stash (trace_type_t *tptr)
291 1.1 christos {
292 1.1 christos trace_type_t **vec;
293 1.1 christos int delta;
294 1.1 christos if (trace_type_max <= tptr -> index) {
295 1.1 christos delta = tptr -> index - trace_type_max + 10;
296 1.1 christos vec = dmalloc (((trace_type_max + delta) *
297 1.1 christos sizeof (trace_type_t *)), MDL);
298 1.1 christos if (!vec)
299 1.1 christos return;
300 1.1 christos memset (&vec [trace_type_max], 0,
301 1.1 christos (sizeof (trace_type_t *)) * delta);
302 1.1 christos trace_type_max += delta;
303 1.1 christos if (trace_types) {
304 1.1 christos memcpy (vec, trace_types,
305 1.1 christos trace_type_count * sizeof (trace_type_t *));
306 1.1 christos dfree (trace_types, MDL);
307 1.1 christos }
308 1.1 christos trace_types = vec;
309 1.1 christos }
310 1.1 christos trace_types [tptr -> index] = tptr;
311 1.1 christos if (tptr -> index >= trace_type_count)
312 1.1 christos trace_type_count = tptr -> index + 1;
313 1.1 christos }
314 1.1 christos
315 1.1 christos trace_type_t *trace_type_register (const char *name,
316 1.1 christos void *baggage,
317 1.1 christos void (*have_packet) (trace_type_t *,
318 1.1 christos unsigned, char *),
319 1.1 christos void (*stop_tracing) (trace_type_t *),
320 1.1 christos const char *file, int line)
321 1.1 christos {
322 1.1 christos trace_type_t *ttmp;
323 1.1 christos unsigned slen = strlen (name);
324 1.1 christos isc_result_t status;
325 1.1 christos
326 1.1 christos ttmp = dmalloc (sizeof *ttmp, file, line);
327 1.1 christos if (!ttmp)
328 1.1 christos return ttmp;
329 1.1 christos ttmp -> index = -1;
330 1.1 christos ttmp -> name = dmalloc (slen + 1, file, line);
331 1.1 christos if (!ttmp -> name) {
332 1.1 christos dfree (ttmp, file, line);
333 1.1 christos return (trace_type_t *)0;
334 1.1 christos }
335 1.1 christos strcpy (ttmp -> name, name);
336 1.1 christos ttmp -> have_packet = have_packet;
337 1.1 christos ttmp -> stop_tracing = stop_tracing;
338 1.1 christos
339 1.1 christos if (traceoutfile) {
340 1.1 christos status = trace_type_record (ttmp, slen, file, line);
341 1.1 christos if (status != ISC_R_SUCCESS) {
342 1.1 christos dfree (ttmp -> name, file, line);
343 1.1 christos dfree (ttmp, file, line);
344 1.1 christos return (trace_type_t *)0;
345 1.1 christos }
346 1.1 christos } else {
347 1.1 christos ttmp -> next = new_trace_types;
348 1.1 christos new_trace_types = ttmp;
349 1.1 christos }
350 1.1 christos
351 1.1 christos return ttmp;
352 1.1 christos }
353 1.1 christos
354 1.1 christos static isc_result_t trace_type_record (trace_type_t *ttmp, unsigned slen,
355 1.1 christos const char *file, int line)
356 1.1 christos {
357 1.1 christos trace_index_mapping_t *tim;
358 1.1 christos isc_result_t status;
359 1.1 christos
360 1.1 christos tim = dmalloc (slen + TRACE_INDEX_MAPPING_SIZE, file, line);
361 1.1 christos if (!tim)
362 1.1 christos return ISC_R_NOMEMORY;
363 1.1 christos ttmp -> index = ++traceindex;
364 1.1 christos trace_type_stash (ttmp);
365 1.1 christos tim -> index = htonl (ttmp -> index);
366 1.1 christos memcpy (tim -> name, ttmp -> name, slen);
367 1.1 christos status = trace_write_packet (trace_types [0],
368 1.1 christos slen + TRACE_INDEX_MAPPING_SIZE,
369 1.1 christos (char *)tim, file, line);
370 1.1 christos dfree (tim, file, line);
371 1.1 christos return status;
372 1.1 christos }
373 1.1 christos
374 1.1 christos /* Stop all registered trace types from trying to trace. */
375 1.1 christos
376 1.1 christos void trace_stop (void)
377 1.1 christos {
378 1.1 christos int i;
379 1.1 christos
380 1.1 christos for (i = 0; i < trace_type_count; i++)
381 1.1 christos if (trace_types [i] -> stop_tracing)
382 1.1 christos (*(trace_types [i] -> stop_tracing))
383 1.1 christos (trace_types [i]);
384 1.1 christos tracing_stopped = 1;
385 1.1 christos }
386 1.1 christos
387 1.1 christos void trace_index_map_input (trace_type_t *ttype, unsigned length, char *buf)
388 1.1 christos {
389 1.1 christos trace_index_mapping_t *tmap;
390 1.1 christos unsigned len;
391 1.1 christos trace_type_t *tptr, **prev;
392 1.1 christos
393 1.1 christos if (length < TRACE_INDEX_MAPPING_SIZE) {
394 1.1 christos log_error ("short trace index mapping");
395 1.1 christos return;
396 1.1 christos }
397 1.1 christos tmap = (trace_index_mapping_t *)buf;
398 1.1 christos
399 1.1 christos prev = &new_trace_types;
400 1.1 christos for (tptr = new_trace_types; tptr; tptr = tptr -> next) {
401 1.1 christos len = strlen (tptr -> name);
402 1.1 christos if (len == length - TRACE_INDEX_MAPPING_SIZE &&
403 1.1 christos !memcmp (tptr -> name, tmap -> name, len)) {
404 1.1 christos tptr -> index = ntohl (tmap -> index);
405 1.1 christos trace_type_stash (tptr);
406 1.1 christos *prev = tptr -> next;
407 1.1 christos return;
408 1.1 christos }
409 1.1 christos prev = &tptr -> next;
410 1.1 christos }
411 1.1 christos
412 1.1 christos log_error ("No registered trace type for type name %.*s",
413 1.1 christos (int)length - TRACE_INDEX_MAPPING_SIZE, tmap -> name);
414 1.1 christos return;
415 1.1 christos }
416 1.1 christos
417 1.1 christos void trace_index_stop_tracing (trace_type_t *ttype) { }
418 1.1 christos
419 1.1 christos void trace_replay_init (void)
420 1.1 christos {
421 1.1 christos trace_playback_flag = 1;
422 1.1 christos }
423 1.1 christos
424 1.1 christos void trace_file_replay (const char *filename)
425 1.1 christos {
426 1.1 christos tracepacket_t *tpkt = NULL;
427 1.1 christos int status;
428 1.1 christos char *buf = NULL;
429 1.1 christos unsigned buflen;
430 1.1 christos unsigned bufmax = 0;
431 1.1 christos trace_type_t *ttype = NULL;
432 1.1 christos isc_result_t result;
433 1.1 christos int len;
434 1.1 christos
435 1.1 christos traceinfile = fopen (filename, "r");
436 1.1 christos if (!traceinfile) {
437 1.1 christos log_error("Can't open tracefile %s: %m", filename);
438 1.1 christos return;
439 1.1 christos }
440 1.1 christos #if defined (HAVE_SETFD)
441 1.1 christos if (fcntl (fileno(traceinfile), F_SETFD, 1) < 0)
442 1.1 christos log_error("Can't set close-on-exec on %s: %m", filename);
443 1.1 christos #endif
444 1.1 christos status = fread(&tracefile_header, 1,
445 1.1 christos sizeof tracefile_header, traceinfile);
446 1.1 christos if (status < sizeof tracefile_header) {
447 1.1 christos if (ferror(traceinfile))
448 1.1 christos log_error("Error reading trace file header: %m");
449 1.1 christos else
450 1.1 christos log_error("Short read on trace file header: %d %ld.",
451 1.1 christos status, (long)(sizeof tracefile_header));
452 1.1 christos goto out;
453 1.1 christos }
454 1.1 christos tracefile_header.magic = ntohl(tracefile_header.magic);
455 1.1 christos tracefile_header.version = ntohl(tracefile_header.version);
456 1.1 christos tracefile_header.hlen = ntohl(tracefile_header.hlen);
457 1.1 christos tracefile_header.phlen = ntohl(tracefile_header.phlen);
458 1.1 christos
459 1.1 christos if (tracefile_header.magic != TRACEFILE_MAGIC) {
460 1.1 christos log_error("%s: not a dhcp trace file.", filename);
461 1.1 christos goto out;
462 1.1 christos }
463 1.1 christos if (tracefile_header.version > TRACEFILE_VERSION) {
464 1.1 christos log_error ("tracefile version %ld > current %ld.",
465 1.1 christos (long int)tracefile_header.version,
466 1.1 christos (long int)TRACEFILE_VERSION);
467 1.1 christos goto out;
468 1.1 christos }
469 1.1 christos if (tracefile_header.phlen < sizeof *tpkt) {
470 1.1 christos log_error("tracefile packet size too small - %ld < %ld",
471 1.1 christos (long int)tracefile_header.phlen,
472 1.1 christos (long int)sizeof *tpkt);
473 1.1 christos goto out;
474 1.1 christos }
475 1.1 christos len = (sizeof tracefile_header) - tracefile_header.hlen;
476 1.1 christos if (len < 0) {
477 1.1 christos log_error("tracefile header size too small - %ld < %ld",
478 1.1 christos (long int)tracefile_header.hlen,
479 1.1 christos (long int)sizeof tracefile_header);
480 1.1 christos goto out;
481 1.1 christos }
482 1.1 christos if (len > 0) {
483 1.1 christos status = fseek(traceinfile, (long)len, SEEK_CUR);
484 1.1 christos if (status < 0) {
485 1.1 christos log_error("can't seek past header: %m");
486 1.1 christos goto out;
487 1.1 christos }
488 1.1 christos }
489 1.1 christos
490 1.1 christos tpkt = dmalloc((unsigned)tracefile_header.phlen, MDL);
491 1.1 christos if (tpkt == NULL) {
492 1.1 christos log_error ("can't allocate trace packet header.");
493 1.1 christos goto out;
494 1.1 christos }
495 1.1 christos
496 1.1 christos while ((result = trace_get_next_packet(&ttype, tpkt, &buf, &buflen,
497 1.1 christos &bufmax)) == ISC_R_SUCCESS) {
498 1.1 christos (*ttype->have_packet)(ttype, tpkt->length, buf);
499 1.1 christos ttype = NULL;
500 1.1 christos }
501 1.1 christos out:
502 1.1 christos fclose(traceinfile);
503 1.1 christos if (buf != NULL)
504 1.1 christos dfree(buf, MDL);
505 1.1 christos if (tpkt != NULL)
506 1.1 christos dfree(tpkt, MDL);
507 1.1 christos }
508 1.1 christos
509 1.1 christos /* Get the next packet from the file. If ttp points to a nonzero pointer
510 1.1 christos to a trace type structure, check the next packet to see if it's of the
511 1.1 christos expected type, and back off if not. */
512 1.1 christos
513 1.1 christos isc_result_t trace_get_next_packet (trace_type_t **ttp,
514 1.1 christos tracepacket_t *tpkt,
515 1.1 christos char **buf, unsigned *buflen,
516 1.1 christos unsigned *bufmax)
517 1.1 christos {
518 1.1 christos trace_type_t *ttype;
519 1.1 christos unsigned paylen;
520 1.1 christos int status, curposok = 0;
521 1.1 christos fpos_t curpos;
522 1.1 christos
523 1.1 christos while(1) {
524 1.1 christos curposok = 0;
525 1.1 christos status = fgetpos(traceinfile, &curpos);
526 1.1 christos if (status < 0) {
527 1.1 christos log_error("Can't save tracefile position: %m");
528 1.1 christos } else {
529 1.1 christos curposok = 1;
530 1.1 christos }
531 1.1 christos
532 1.1 christos status = fread(tpkt, 1, (size_t)tracefile_header.phlen,
533 1.1 christos traceinfile);
534 1.1 christos if (status < tracefile_header.phlen) {
535 1.1 christos if (ferror(traceinfile))
536 1.1 christos log_error("Error reading trace packet header: "
537 1.1 christos "%m");
538 1.1 christos else if (status == 0)
539 1.1 christos return ISC_R_EOF;
540 1.1 christos else
541 1.1 christos log_error ("Short read on trace packet header:"
542 1.1 christos " %ld %ld.",
543 1.1 christos (long int)status,
544 1.1 christos (long int)tracefile_header.phlen);
545 1.1 christos return DHCP_R_PROTOCOLERROR;
546 1.1 christos }
547 1.1 christos
548 1.1 christos /* Swap the packet. */
549 1.1 christos tpkt->type_index = ntohl(tpkt -> type_index);
550 1.1 christos tpkt->length = ntohl(tpkt -> length);
551 1.1 christos tpkt->when = ntohl(tpkt -> when);
552 1.1 christos
553 1.1 christos /* See if there's a handler for this packet type. */
554 1.1 christos if (tpkt->type_index < trace_type_count &&
555 1.1 christos trace_types[tpkt->type_index])
556 1.1 christos ttype = trace_types[tpkt->type_index];
557 1.1 christos else {
558 1.1 christos log_error ("Trace packet with unknown index %ld",
559 1.1 christos (long int)tpkt->type_index);
560 1.1 christos return DHCP_R_PROTOCOLERROR;
561 1.1 christos }
562 1.1 christos
563 1.1 christos /*
564 1.1 christos * Determine if we should try to expire any timer events.
565 1.1 christos * We do so if:
566 1.1 christos * we aren't looking for a specific type of packet
567 1.1 christos * we have a hook to use to update the timer
568 1.1 christos * the timestamp on the packet doesn't match the current time
569 1.1 christos * When we do so we rewind the file to the beginning of this
570 1.1 christos * packet and then try for a new packet. This allows
571 1.1 christos * any code triggered by a timeout to get the current packet
572 1.1 christos * while we get the next one.
573 1.1 christos */
574 1.1 christos
575 1.1 christos if ((ttp != NULL) && (*ttp == NULL) &&
576 1.1 christos (tpkt->when != cur_tv.tv_sec) &&
577 1.1 christos (trace_set_time_hook != NULL)) {
578 1.1 christos if (curposok == 0) {
579 1.1 christos log_error("no curpos for fsetpos in "
580 1.1 christos "tracefile");
581 1.1 christos return DHCP_R_PROTOCOLERROR;
582 1.1 christos }
583 1.1 christos
584 1.1 christos status = fsetpos(traceinfile, &curpos);
585 1.1 christos if (status < 0) {
586 1.1 christos log_error("fsetpos in tracefile failed: %m");
587 1.1 christos return DHCP_R_PROTOCOLERROR;
588 1.1 christos }
589 1.1 christos
590 1.1 christos (*trace_set_time_hook) (tpkt->when);
591 1.1 christos continue;
592 1.1 christos }
593 1.1 christos break;
594 1.1 christos }
595 1.1 christos
596 1.1 christos /* If we were supposed to get a particular kind of packet,
597 1.1 christos check to see that we got the right kind. */
598 1.1 christos if (ttp && *ttp && ttype != *ttp) {
599 1.1 christos log_error ("Read packet type %s when expecting %s",
600 1.1 christos ttype -> name, (*ttp) -> name);
601 1.1 christos status = fsetpos (traceinfile, &curpos);
602 1.1 christos if (status < 0) {
603 1.1 christos log_error ("fsetpos in tracefile failed: %m");
604 1.1 christos return DHCP_R_PROTOCOLERROR;
605 1.1 christos }
606 1.1 christos return ISC_R_UNEXPECTEDTOKEN;
607 1.1 christos }
608 1.1 christos
609 1.1 christos paylen = tpkt -> length;
610 1.1 christos if (paylen % 8)
611 1.1 christos paylen += 8 - (tpkt -> length % 8);
612 1.1 christos
613 1.1 christos /* allocate a buffer if we need one or current buffer is too small */
614 1.1 christos if ((*buf == NULL) || (paylen > (*bufmax))) {
615 1.1 christos if ((*buf))
616 1.1 christos dfree ((*buf), MDL);
617 1.1 christos (*bufmax) = ((paylen + 1023) & ~1023U);
618 1.1 christos (*buf) = dmalloc ((*bufmax), MDL);
619 1.1 christos if (!(*buf)) {
620 1.1 christos log_error ("Can't allocate input buffer sized %d",
621 1.1 christos (*bufmax));
622 1.1 christos return ISC_R_NOMEMORY;
623 1.1 christos }
624 1.1 christos }
625 1.1 christos
626 1.1 christos status = fread ((*buf), 1, paylen, traceinfile);
627 1.1 christos if (status < paylen) {
628 1.1 christos if (ferror (traceinfile))
629 1.1 christos log_error ("Error reading trace payload: %m");
630 1.1 christos else
631 1.1 christos log_error ("Short read on trace payload: %d %d.",
632 1.1 christos status, paylen);
633 1.1 christos return DHCP_R_PROTOCOLERROR;
634 1.1 christos }
635 1.1 christos
636 1.1 christos /* Store the actual length of the payload. */
637 1.1 christos *buflen = tpkt -> length;
638 1.1 christos
639 1.1 christos if (ttp)
640 1.1 christos *ttp = ttype;
641 1.1 christos return ISC_R_SUCCESS;
642 1.1 christos }
643 1.1 christos
644 1.1 christos isc_result_t trace_get_packet (trace_type_t **ttp,
645 1.1 christos unsigned *buflen, char **buf)
646 1.1 christos {
647 1.1 christos tracepacket_t *tpkt;
648 1.1 christos unsigned bufmax = 0;
649 1.1 christos isc_result_t status;
650 1.1 christos
651 1.1 christos if (!buf || *buf)
652 1.1 christos return DHCP_R_INVALIDARG;
653 1.1 christos
654 1.1 christos tpkt = dmalloc ((unsigned)tracefile_header.phlen, MDL);
655 1.1 christos if (!tpkt) {
656 1.1 christos log_error ("can't allocate trace packet header.");
657 1.1 christos return ISC_R_NOMEMORY;
658 1.1 christos }
659 1.1 christos
660 1.1 christos status = trace_get_next_packet (ttp, tpkt, buf, buflen, &bufmax);
661 1.1 christos
662 1.1 christos dfree (tpkt, MDL);
663 1.1 christos return status;
664 1.1 christos }
665 1.1 christos
666 1.1 christos /* Get a packet from the trace input file that contains a file with the
667 1.1 christos specified name. We don't hunt for the packet - it should be the next
668 1.1 christos packet in the tracefile. If it's not, or something else bad happens,
669 1.1 christos return an error code. */
670 1.1 christos
671 1.1 christos isc_result_t trace_get_file (trace_type_t *ttype,
672 1.1 christos const char *filename, unsigned *len, char **buf)
673 1.1 christos {
674 1.1 christos fpos_t curpos;
675 1.1 christos unsigned max = 0;
676 1.1 christos tracepacket_t *tpkt;
677 1.1 christos int status;
678 1.1 christos isc_result_t result;
679 1.1 christos
680 1.1 christos /* Disallow some obvious bogosities. */
681 1.1 christos if (!buf || !len || *buf)
682 1.1 christos return DHCP_R_INVALIDARG;
683 1.1 christos
684 1.1 christos /* Save file position in case of filename mismatch. */
685 1.1 christos status = fgetpos (traceinfile, &curpos);
686 1.1 christos if (status < 0)
687 1.1 christos log_error ("Can't save tracefile position: %m");
688 1.1 christos
689 1.1 christos tpkt = dmalloc ((unsigned)tracefile_header.phlen, MDL);
690 1.1 christos if (!tpkt) {
691 1.1 christos log_error ("can't allocate trace packet header.");
692 1.1 christos return ISC_R_NOMEMORY;
693 1.1 christos }
694 1.1 christos
695 1.1 christos result = trace_get_next_packet (&ttype, tpkt, buf, len, &max);
696 1.1 christos /* done with tpkt, free it */
697 1.1 christos dfree (tpkt, MDL);
698 1.1 christos if (result != ISC_R_SUCCESS) {
699 1.1 christos if (*buf) {
700 1.1 christos dfree (*buf, MDL);
701 1.1 christos *buf = NULL;
702 1.1 christos }
703 1.1 christos return result;
704 1.1 christos }
705 1.1 christos
706 1.1 christos /* Make sure the filename is right. */
707 1.1 christos if (strcmp (filename, *buf)) {
708 1.1 christos log_error ("Read file %s when expecting %s", *buf, filename);
709 1.1 christos dfree (*buf, MDL);
710 1.1 christos *buf = NULL;
711 1.1 christos
712 1.1 christos status = fsetpos (traceinfile, &curpos);
713 1.1 christos if (status < 0) {
714 1.1 christos log_error ("fsetpos in tracefile failed: %m");
715 1.1 christos return DHCP_R_PROTOCOLERROR;
716 1.1 christos }
717 1.1 christos return ISC_R_UNEXPECTEDTOKEN;
718 1.1 christos }
719 1.1 christos
720 1.1 christos return ISC_R_SUCCESS;
721 1.1 christos }
722 1.1 christos #endif /* TRACING */
723