1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * Shane Alcock |
---|
8 | * |
---|
9 | * All rights reserved. |
---|
10 | * |
---|
11 | * This code has been developed by the University of Waikato WAND |
---|
12 | * research group. For further information please see http://www.wand.net.nz/ |
---|
13 | * |
---|
14 | * libtrace is free software; you can redistribute it and/or modify |
---|
15 | * it under the terms of the GNU General Public License as published by |
---|
16 | * the Free Software Foundation; either version 2 of the License, or |
---|
17 | * (at your option) any later version. |
---|
18 | * |
---|
19 | * libtrace is distributed in the hope that it will be useful, |
---|
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
22 | * GNU General Public License for more details. |
---|
23 | * |
---|
24 | * You should have received a copy of the GNU General Public License |
---|
25 | * along with libtrace; if not, write to the Free Software |
---|
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
27 | * |
---|
28 | * $Id$ |
---|
29 | * |
---|
30 | */ |
---|
31 | |
---|
32 | #define _GNU_SOURCE |
---|
33 | |
---|
34 | #include "config.h" |
---|
35 | #include "common.h" |
---|
36 | #include "libtrace.h" |
---|
37 | #include "libtrace_int.h" |
---|
38 | #include "format_helper.h" |
---|
39 | #include "parse_cmd.h" |
---|
40 | #include "rt_protocol.h" |
---|
41 | |
---|
42 | #ifdef HAVE_INTTYPES_H |
---|
43 | # include <inttypes.h> |
---|
44 | #else |
---|
45 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
46 | #endif |
---|
47 | |
---|
48 | #ifdef HAVE_STDDEF_H |
---|
49 | # include <stddef.h> |
---|
50 | #else |
---|
51 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
52 | #endif |
---|
53 | #include <sys/types.h> |
---|
54 | #include <sys/socket.h> |
---|
55 | #include <sys/un.h> |
---|
56 | #include <sys/mman.h> |
---|
57 | #include <sys/stat.h> |
---|
58 | #include <unistd.h> |
---|
59 | #include <assert.h> |
---|
60 | #include <errno.h> |
---|
61 | #include <netdb.h> |
---|
62 | #include <fcntl.h> |
---|
63 | #include <getopt.h> |
---|
64 | #include <stdio.h> |
---|
65 | #include <string.h> |
---|
66 | #include <stdlib.h> |
---|
67 | |
---|
68 | #define RT_INFO ((struct rt_format_data_t*)libtrace->format_data) |
---|
69 | |
---|
70 | int reliability = 0; |
---|
71 | |
---|
72 | char *rt_deny_reason(uint8_t reason) { |
---|
73 | char *string = 0; |
---|
74 | |
---|
75 | switch(reason) { |
---|
76 | case RT_DENY_WRAPPER: |
---|
77 | string = "Rejected by TCP Wrappers"; |
---|
78 | break; |
---|
79 | case RT_DENY_FULL: |
---|
80 | string = "Max connections reached on server"; |
---|
81 | break; |
---|
82 | case RT_DENY_AUTH: |
---|
83 | string = "Authentication failed"; |
---|
84 | break; |
---|
85 | default: |
---|
86 | string = "Unknown reason"; |
---|
87 | } |
---|
88 | |
---|
89 | return string; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | struct rt_format_data_t { |
---|
94 | char *hostname; |
---|
95 | int port; |
---|
96 | int input_fd; |
---|
97 | int reliable; |
---|
98 | |
---|
99 | struct libtrace_t *dummy_erf; |
---|
100 | struct libtrace_t *dummy_pcap; |
---|
101 | struct libtrace_t *dummy_wag; |
---|
102 | }; |
---|
103 | |
---|
104 | static struct libtrace_format_t rt; |
---|
105 | |
---|
106 | static int rt_connect(struct libtrace_t *libtrace) { |
---|
107 | struct hostent *he; |
---|
108 | struct sockaddr_in remote; |
---|
109 | rt_header_t connect_msg; |
---|
110 | rt_deny_conn_t deny_hdr; |
---|
111 | rt_hello_t hello_opts; |
---|
112 | uint8_t reason; |
---|
113 | |
---|
114 | if ((he=gethostbyname(RT_INFO->hostname)) == NULL) { |
---|
115 | perror("gethostbyname"); |
---|
116 | return -1; |
---|
117 | } |
---|
118 | if ((RT_INFO->input_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
---|
119 | perror("socket"); |
---|
120 | return -1; |
---|
121 | } |
---|
122 | |
---|
123 | remote.sin_family = AF_INET; |
---|
124 | remote.sin_port = htons(RT_INFO->port); |
---|
125 | remote.sin_addr = *((struct in_addr *)he->h_addr); |
---|
126 | bzero(&(remote.sin_zero), 8); |
---|
127 | |
---|
128 | if (connect(RT_INFO->input_fd, (struct sockaddr *)&remote, |
---|
129 | sizeof(struct sockaddr)) == -1) { |
---|
130 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
131 | "Could not connect to host %s on port %d", |
---|
132 | RT_INFO->hostname, RT_INFO->port); |
---|
133 | return -1; |
---|
134 | } |
---|
135 | |
---|
136 | /* We are connected, now receive message from server */ |
---|
137 | |
---|
138 | if (recv(RT_INFO->input_fd, &connect_msg, sizeof(rt_header_t), 0) != sizeof(rt_header_t) ) { |
---|
139 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
140 | "Could not receive connection message from %s", |
---|
141 | RT_INFO->hostname); |
---|
142 | return -1; |
---|
143 | } |
---|
144 | |
---|
145 | switch (connect_msg.type) { |
---|
146 | case RT_DENY_CONN: |
---|
147 | |
---|
148 | if (recv(RT_INFO->input_fd, &deny_hdr, |
---|
149 | sizeof(rt_deny_conn_t), |
---|
150 | 0) != sizeof(rt_deny_conn_t)) { |
---|
151 | reason = 0; |
---|
152 | } |
---|
153 | reason = deny_hdr.reason; |
---|
154 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
155 | "Connection attempt is denied: %s", |
---|
156 | rt_deny_reason(reason)); |
---|
157 | return -1; |
---|
158 | case RT_HELLO: |
---|
159 | /* do something with options */ |
---|
160 | printf("Hello\n"); |
---|
161 | if (recv(RT_INFO->input_fd, &hello_opts, |
---|
162 | sizeof(rt_hello_t), 0) |
---|
163 | != sizeof(rt_hello_t)) { |
---|
164 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
165 | "Failed to receive RT_HELLO options"); |
---|
166 | return -1; |
---|
167 | } |
---|
168 | reliability = hello_opts.reliable; |
---|
169 | |
---|
170 | return 0; |
---|
171 | default: |
---|
172 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
173 | "Unknown message type received: %d", |
---|
174 | connect_msg.type); |
---|
175 | return -1; |
---|
176 | } |
---|
177 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, |
---|
178 | "Somehow you managed to reach this unreachable code"); |
---|
179 | return -1; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | static int rt_init_input(struct libtrace_t *libtrace) { |
---|
184 | char *scan; |
---|
185 | char *uridata = libtrace->uridata; |
---|
186 | libtrace->format_data = malloc(sizeof(struct rt_format_data_t)); |
---|
187 | |
---|
188 | RT_INFO->dummy_erf = NULL; |
---|
189 | RT_INFO->dummy_pcap = NULL; |
---|
190 | RT_INFO->dummy_wag = NULL; |
---|
191 | |
---|
192 | if (strlen(uridata) == 0) { |
---|
193 | RT_INFO->hostname = |
---|
194 | strdup("localhost"); |
---|
195 | RT_INFO->port = |
---|
196 | COLLECTOR_PORT; |
---|
197 | } else { |
---|
198 | if ((scan = strchr(uridata,':')) == NULL) { |
---|
199 | RT_INFO->hostname = |
---|
200 | strdup(uridata); |
---|
201 | RT_INFO->port = |
---|
202 | COLLECTOR_PORT; |
---|
203 | } else { |
---|
204 | RT_INFO->hostname = |
---|
205 | (char *)strndup(uridata, |
---|
206 | (scan - uridata)); |
---|
207 | RT_INFO->port = |
---|
208 | atoi(++scan); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | return rt_connect(libtrace); |
---|
213 | } |
---|
214 | |
---|
215 | static int rt_start_input(struct libtrace_t *libtrace) { |
---|
216 | rt_header_t start_msg; |
---|
217 | |
---|
218 | start_msg.type = RT_START; |
---|
219 | start_msg.length = sizeof(rt_start_t); |
---|
220 | |
---|
221 | printf("Sending start - len: %d\n", start_msg.length); |
---|
222 | |
---|
223 | /* Need to send start message to server */ |
---|
224 | if (send(RT_INFO->input_fd, &start_msg, sizeof(rt_header_t) + |
---|
225 | start_msg.length, 0) != sizeof(rt_header_t)) { |
---|
226 | printf("Failed to send start message to server\n"); |
---|
227 | return -1; |
---|
228 | } |
---|
229 | |
---|
230 | return 0; |
---|
231 | } |
---|
232 | |
---|
233 | static int rt_fin_input(struct libtrace_t *libtrace) { |
---|
234 | rt_header_t close_msg; |
---|
235 | |
---|
236 | close_msg.type = RT_CLOSE; |
---|
237 | close_msg.length = sizeof(rt_close_t); |
---|
238 | |
---|
239 | /* Send a close message to the server */ |
---|
240 | if (send(RT_INFO->input_fd, &close_msg, sizeof(rt_header_t) + |
---|
241 | close_msg.length, 0) != sizeof(rt_header_t)) { |
---|
242 | printf("Failed to send close message to server\n"); |
---|
243 | |
---|
244 | } |
---|
245 | if (RT_INFO->dummy_erf) |
---|
246 | trace_destroy_dead(RT_INFO->dummy_erf); |
---|
247 | |
---|
248 | if (RT_INFO->dummy_pcap) |
---|
249 | trace_destroy_dead(RT_INFO->dummy_pcap); |
---|
250 | |
---|
251 | if (RT_INFO->dummy_wag) |
---|
252 | trace_destroy_dead(RT_INFO->dummy_wag); |
---|
253 | close(RT_INFO->input_fd); |
---|
254 | free(libtrace->format_data); |
---|
255 | return 0; |
---|
256 | } |
---|
257 | |
---|
258 | static int rt_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
259 | int numbytes; |
---|
260 | |
---|
261 | while(1) { |
---|
262 | #ifndef MSG_NOSIGNAL |
---|
263 | # define MSG_NOSIGNAL 0 |
---|
264 | #endif |
---|
265 | if ((numbytes = recv(RT_INFO->input_fd, |
---|
266 | buffer, |
---|
267 | len, |
---|
268 | MSG_NOSIGNAL)) == -1) { |
---|
269 | if (errno == EINTR) { |
---|
270 | /* ignore EINTR in case |
---|
271 | * a caller is using signals |
---|
272 | */ |
---|
273 | continue; |
---|
274 | } |
---|
275 | perror("recv"); |
---|
276 | return -1; |
---|
277 | } |
---|
278 | break; |
---|
279 | |
---|
280 | } |
---|
281 | return numbytes; |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | static int rt_set_format(libtrace_t *libtrace, libtrace_packet_t *packet) |
---|
286 | { |
---|
287 | |
---|
288 | if (packet->type >= RT_DATA_PCAP) { |
---|
289 | if (!RT_INFO->dummy_pcap) { |
---|
290 | RT_INFO->dummy_pcap = trace_create_dead("pcap:-"); |
---|
291 | } |
---|
292 | packet->trace = RT_INFO->dummy_pcap; |
---|
293 | return; |
---|
294 | } |
---|
295 | |
---|
296 | switch (packet->type) { |
---|
297 | case RT_DATA_ERF: |
---|
298 | if (!RT_INFO->dummy_erf) { |
---|
299 | RT_INFO->dummy_erf = trace_create_dead("erf:-"); |
---|
300 | } |
---|
301 | packet->trace = RT_INFO->dummy_erf; |
---|
302 | break; |
---|
303 | case RT_DATA_WAG: |
---|
304 | if (!RT_INFO->dummy_wag) { |
---|
305 | RT_INFO->dummy_wag = trace_create_dead("wtf:-"); |
---|
306 | } |
---|
307 | packet->trace = RT_INFO->dummy_wag; |
---|
308 | break; |
---|
309 | case RT_DATA_LEGACY_ETH: |
---|
310 | case RT_DATA_LEGACY_ATM: |
---|
311 | case RT_DATA_LEGACY_POS: |
---|
312 | printf("Sending legacy over RT is currently not supported\n"); |
---|
313 | return -1; |
---|
314 | default: |
---|
315 | printf("Unrecognised format: %d\n", packet->type); |
---|
316 | return -1; |
---|
317 | } |
---|
318 | return 0; /* success */ |
---|
319 | } |
---|
320 | |
---|
321 | static void rt_set_payload(struct libtrace_packet_t *packet) { |
---|
322 | dag_record_t *erfptr; |
---|
323 | |
---|
324 | switch (packet->type) { |
---|
325 | case RT_DATA_ERF: |
---|
326 | erfptr = (dag_record_t *)packet->header; |
---|
327 | |
---|
328 | if (erfptr->flags.rxerror == 1) { |
---|
329 | packet->payload = NULL; |
---|
330 | break; |
---|
331 | } |
---|
332 | /* else drop into the default case */ |
---|
333 | default: |
---|
334 | packet->payload = (char *)packet->buffer + |
---|
335 | trace_get_framing_length(packet); |
---|
336 | break; |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | static int rt_send_ack(struct libtrace_t *libtrace, |
---|
341 | uint32_t seqno) { |
---|
342 | |
---|
343 | static char *ack_buffer = 0; |
---|
344 | char *buf_ptr; |
---|
345 | int numbytes = 0; |
---|
346 | int to_write = 0; |
---|
347 | rt_header_t *hdr; |
---|
348 | rt_ack_t *ack_hdr; |
---|
349 | |
---|
350 | if (!ack_buffer) { |
---|
351 | ack_buffer = malloc(sizeof(rt_header_t) + sizeof(rt_ack_t)); |
---|
352 | } |
---|
353 | |
---|
354 | hdr = (rt_header_t *) ack_buffer; |
---|
355 | ack_hdr = (rt_ack_t *) (ack_buffer + sizeof(rt_header_t)); |
---|
356 | |
---|
357 | hdr->type = RT_ACK; |
---|
358 | hdr->length = sizeof(rt_ack_t); |
---|
359 | |
---|
360 | ack_hdr->sequence = seqno; |
---|
361 | |
---|
362 | to_write = hdr->length + sizeof(rt_header_t); |
---|
363 | buf_ptr = ack_buffer; |
---|
364 | |
---|
365 | |
---|
366 | while (to_write > 0) { |
---|
367 | numbytes = send(RT_INFO->input_fd, buf_ptr, to_write, 0); |
---|
368 | if (numbytes == -1) { |
---|
369 | if (errno == EINTR || errno == EAGAIN) { |
---|
370 | continue; |
---|
371 | } |
---|
372 | else { |
---|
373 | printf("Error sending ack\n"); |
---|
374 | return -1; |
---|
375 | } |
---|
376 | } |
---|
377 | to_write = to_write - numbytes; |
---|
378 | buf_ptr = buf_ptr + to_write; |
---|
379 | |
---|
380 | } |
---|
381 | |
---|
382 | return 1; |
---|
383 | } |
---|
384 | |
---|
385 | static int rt_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
386 | |
---|
387 | rt_header_t pkt_hdr; |
---|
388 | int pkt_size = 0; |
---|
389 | |
---|
390 | void *buffer = 0; |
---|
391 | |
---|
392 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || !packet->buffer) { |
---|
393 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
394 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
395 | } |
---|
396 | |
---|
397 | buffer = packet->buffer; |
---|
398 | packet->header = packet->buffer; |
---|
399 | |
---|
400 | /* FIXME: Better error handling required */ |
---|
401 | if (rt_read(libtrace, &pkt_hdr, sizeof(rt_header_t)) != |
---|
402 | sizeof(rt_header_t)) { |
---|
403 | printf("Error receiving rt header\n"); |
---|
404 | return -1; |
---|
405 | } |
---|
406 | |
---|
407 | packet->type = pkt_hdr.type; |
---|
408 | pkt_size = pkt_hdr.length; |
---|
409 | packet->size = pkt_hdr.length; |
---|
410 | |
---|
411 | if (packet->type >= RT_DATA_SIMPLE) { |
---|
412 | if (rt_read(libtrace, buffer, pkt_size) != pkt_size) { |
---|
413 | printf("Error receiving packet\n"); |
---|
414 | return -1; |
---|
415 | } |
---|
416 | |
---|
417 | if (rt_set_format(libtrace, packet) < 0) { |
---|
418 | return -1; |
---|
419 | } |
---|
420 | rt_set_payload(packet); |
---|
421 | |
---|
422 | if (reliability > 0) { |
---|
423 | |
---|
424 | if (rt_send_ack(libtrace, pkt_hdr.sequence) |
---|
425 | == -1) |
---|
426 | { |
---|
427 | return -1; |
---|
428 | } |
---|
429 | } |
---|
430 | } else { |
---|
431 | switch(packet->type) { |
---|
432 | case RT_STATUS: |
---|
433 | case RT_DUCK: |
---|
434 | if (rt_read(libtrace, buffer, pkt_size) != |
---|
435 | pkt_size) { |
---|
436 | printf("Error receiving status packet\n"); |
---|
437 | return -1; |
---|
438 | } |
---|
439 | packet->header = 0; |
---|
440 | packet->payload = buffer; |
---|
441 | break; |
---|
442 | case RT_END_DATA: |
---|
443 | return 0; |
---|
444 | case RT_PAUSE_ACK: |
---|
445 | /* FIXME: Do something useful */ |
---|
446 | break; |
---|
447 | case RT_OPTION: |
---|
448 | /* FIXME: Do something useful here as well */ |
---|
449 | break; |
---|
450 | case RT_KEYCHANGE: |
---|
451 | break; |
---|
452 | default: |
---|
453 | printf("Bad rt type for client receipt: %d\n", |
---|
454 | pkt_hdr.type); |
---|
455 | } |
---|
456 | } |
---|
457 | /* Return the number of bytes read from the stream */ |
---|
458 | return sizeof(rt_header_t) + packet->size; |
---|
459 | } |
---|
460 | |
---|
461 | static int rt_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
462 | switch (packet->type) { |
---|
463 | case RT_DUCK: |
---|
464 | return sizeof(rt_duck_t); |
---|
465 | case RT_STATUS: |
---|
466 | return sizeof(rt_status_t); |
---|
467 | case RT_HELLO: |
---|
468 | return sizeof(rt_hello_t); |
---|
469 | case RT_START: |
---|
470 | return sizeof(rt_start_t); |
---|
471 | case RT_ACK: |
---|
472 | return sizeof(rt_ack_t); |
---|
473 | case RT_END_DATA: |
---|
474 | return sizeof(rt_end_data_t); |
---|
475 | case RT_CLOSE: |
---|
476 | return sizeof(rt_close_t); |
---|
477 | case RT_DENY_CONN: |
---|
478 | return sizeof(rt_deny_conn_t); |
---|
479 | case RT_PAUSE: |
---|
480 | return sizeof(rt_pause_t); |
---|
481 | case RT_PAUSE_ACK: |
---|
482 | return sizeof(rt_pause_ack_t); |
---|
483 | case RT_OPTION: |
---|
484 | return sizeof(rt_option_t); |
---|
485 | case RT_KEYCHANGE: |
---|
486 | return sizeof(rt_keychange_t); |
---|
487 | } |
---|
488 | printf("Unknown type: %d\n", packet->type); |
---|
489 | return 0; |
---|
490 | } |
---|
491 | |
---|
492 | static int rt_get_framing_length(const libtrace_packet_t *packet) { |
---|
493 | return 0; |
---|
494 | } |
---|
495 | |
---|
496 | |
---|
497 | static int rt_get_fd(const libtrace_t *trace) { |
---|
498 | return ((struct rt_format_data_t *)trace->format_data)->input_fd; |
---|
499 | } |
---|
500 | |
---|
501 | |
---|
502 | |
---|
503 | static void rt_help() { |
---|
504 | printf("rt format module\n"); |
---|
505 | printf("Supported input URIs:\n"); |
---|
506 | printf("\trt:hostname:port\n"); |
---|
507 | printf("\trt:hostname (connects on default port)\n"); |
---|
508 | printf("\n"); |
---|
509 | printf("\te.g.: rt:localhost\n"); |
---|
510 | printf("\te.g.: rt:localhost:32500\n"); |
---|
511 | printf("\n"); |
---|
512 | |
---|
513 | } |
---|
514 | |
---|
515 | |
---|
516 | static struct libtrace_format_t rt = { |
---|
517 | "rt", |
---|
518 | "$Id$", |
---|
519 | TRACE_FORMAT_RT, |
---|
520 | rt_init_input, /* init_input */ |
---|
521 | NULL, /* config_input */ |
---|
522 | rt_start_input, /* start_input */ |
---|
523 | NULL, /* init_output */ |
---|
524 | NULL, /* config_output */ |
---|
525 | NULL, /* start_output */ |
---|
526 | NULL, /* pause_output */ |
---|
527 | rt_fin_input, /* fin_input */ |
---|
528 | NULL, /* fin_output */ |
---|
529 | rt_read_packet, /* read_packet */ |
---|
530 | NULL, /* fin_packet */ |
---|
531 | NULL, /* write_packet */ |
---|
532 | NULL, /* get_link_type */ |
---|
533 | NULL, /* get_direction */ |
---|
534 | NULL, /* set_direction */ |
---|
535 | NULL, /* get_erf_timestamp */ |
---|
536 | NULL, /* get_timeval */ |
---|
537 | NULL, /* get_seconds */ |
---|
538 | NULL, /* seek_erf */ |
---|
539 | NULL, /* seek_timeval */ |
---|
540 | NULL, /* seek_seconds */ |
---|
541 | rt_get_capture_length, /* get_capture_length */ |
---|
542 | NULL, /* get_wire_length */ |
---|
543 | rt_get_framing_length, /* get_framing_length */ |
---|
544 | NULL, /* set_capture_length */ |
---|
545 | rt_get_fd, /* get_fd */ |
---|
546 | trace_event_device, /* trace_event */ |
---|
547 | rt_help, /* help */ |
---|
548 | NULL /* next pointer */ |
---|
549 | }; |
---|
550 | |
---|
551 | void __attribute__((constructor)) rt_constructor() { |
---|
552 | register_format(&rt); |
---|
553 | } |
---|