2 * State machine for text import
3 * November 2010, Jaap Keuter <jaap.keuter@xs4all.nl>
4 * Modified March 2021, Paul Weiß <paulniklasweiss@gmail.com>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * Based on text2pcap.c by Ashok Narayanan <ashokn@cisco.com>
12 * SPDX-License-Identifier: GPL-2.0-or-later
15 /*******************************************************************************
17 * This code reads in an ASCII hexdump of this common format:
19 * 00000000 00 E0 1E A7 05 6F 00 10 5A A0 B9 12 08 00 46 00 .....o..Z.....F.
20 * 00000010 03 68 00 00 00 00 0A 2E EE 33 0F 19 08 7F 0F 19 .h.......3......
21 * 00000020 03 80 94 04 00 00 10 01 16 A2 0A 00 03 50 00 0C .............P..
22 * 00000030 01 01 0F 19 03 80 11 01 1E 61 00 0C 03 01 0F 19 .........a......
24 * Each bytestring line consists of an offset, one or more bytes, and
25 * text at the end. An offset is defined as a hex string of more than
26 * two characters. A byte is defined as a hex string of exactly two
27 * characters. The text at the end is ignored, as is any text before
28 * the offset. Bytes read from a bytestring line are added to the
29 * current packet only if all the following conditions are satisfied:
31 * - No text appears between the offset and the bytes (any bytes appearing after
32 * such text would be ignored)
34 * - The offset must be arithmetically correct, i.e. if the offset is 00000020,
35 * then exactly 32 bytes must have been read into this packet before this.
36 * If the offset is wrong, the packet is immediately terminated
38 * A packet start is signaled by a zero offset.
40 * Lines starting with #TEXT2PCAP are directives. These allow the user
41 * to embed instructions into the capture file which allows text2pcap
42 * to take some actions (e.g. specifying the encapsulation
43 * etc.). Currently no directives are implemented.
45 * Lines beginning with # which are not directives are ignored as
46 * comments. Currently all non-hexdump text is ignored by text2pcap;
47 * in the future, text processing may be added, but lines prefixed
48 * with '#' will still be ignored.
50 * The output is a libpcap packet containing Ethernet frames by
51 * default. This program takes options which allow the user to add
52 * dummy Ethernet, IP and UDP, TCP or SCTP headers to the packets in order
53 * to allow dumps of L3 or higher protocols to be decoded.
55 * Considerable flexibility is built into this code to read hexdumps
56 * of slightly different formats. For example, any text prefixing the
57 * hexdump line is dropped (including mail forwarding '>'). The offset
58 * can be any hex number of four digits or greater.
60 * This converter cannot read a single packet greater than
61 * WTAP_MAX_PACKET_SIZE_STANDARD. The snapshot length is automatically
62 * set to WTAP_MAX_PACKET_SIZE_STANDARD.
65 /*******************************************************************************
66 * Alternatively this parses a Textfile based on a prel regex containing named
67 * capturing groups like so:
68 * (?<seqno>\d+)\s*(?<dir><|>)\s*(?<time>\d+:\d\d:\d\d.\d+)\s+(?<data>[0-9a-fA-F]+)\\s+
70 * Fields are decoded using a leanient parser, but only one attempt is made.
71 * Except for in data invalid values will be replaced by default ones.
72 * data currently only accepts plain HEX, OCT or BIN encoded data.
73 * common field separators are ignored. Note however that 0x or 0b prefixing is
74 * not supported and no automatic format detection is attempted.
78 #include "text_import.h"
83 #include <wsutil/file_util.h>
84 #include <ws_exit_codes.h>
92 #include <epan/tvbuff.h>
93 #include <wsutil/crc32.h>
94 #include <epan/in_cksum.h>
96 #include <wsutil/report_message.h>
97 #include <wsutil/exported_pdu_tlvs.h>
99 #include <wsutil/nstime.h>
100 #include <wsutil/time_util.h>
101 #include <wsutil/ws_strptime.h>
103 #include <wsutil/version_info.h>
104 #include <wsutil/cpu_info.h>
105 #include <wsutil/os_version_info.h>
107 #include "text_import_scanner.h"
108 #include "text_import_scanner_lex.h"
109 #include "text_import_regex.h"
111 /*--- Options --------------------------------------------------------------------*/
113 /* maximum time precision we can handle = 10^(-SUBSEC_PREC) */
114 #define SUBSEC_PREC 9
116 static text_import_info_t
*info_p
;
118 /* Dummy Ethernet header */
119 static bool hdr_ethernet
;
120 static uint8_t hdr_eth_dest_addr
[6] = {0x20, 0x52, 0x45, 0x43, 0x56, 0x00};
121 static uint8_t hdr_eth_src_addr
[6] = {0x20, 0x53, 0x45, 0x4E, 0x44, 0x00};
122 static uint32_t hdr_ethernet_proto
;
124 /* Dummy IP header */
126 static bool hdr_ipv6
;
127 static unsigned hdr_ip_proto
;
129 /* Destination and source addresses for IP header */
130 static ws_in6_addr NO_IPv6_ADDRESS
= {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
131 /* These IPv6 default addresses are unique local addresses generated using
132 * the pseudo-random method from Section 3.2.2 of RFC 4193
134 static ws_in6_addr IPv6_SRC
= {{0xfd, 0xce, 0xd8, 0x62, 0x14, 0x1b, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}};
135 static ws_in6_addr IPv6_DST
= {{0xfd, 0xce, 0xd8, 0x62, 0x14, 0x1b, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}};
137 /* Dummy UDP header */
140 /* Dummy TCP header */
143 /* TCP sequence numbers when has_direction is true */
144 static uint32_t tcp_in_seq_num
;
145 static uint32_t tcp_out_seq_num
;
147 /* Dummy SCTP header */
148 static bool hdr_sctp
;
150 /* Dummy DATA chunk header */
151 static bool hdr_data_chunk
;
152 static uint8_t hdr_data_chunk_type
;
153 static uint8_t hdr_data_chunk_bits
;
154 static uint32_t hdr_data_chunk_tsn
;
155 static uint16_t hdr_data_chunk_sid
;
156 static uint16_t hdr_data_chunk_ssn
;
158 /* Dummy ExportPdu header */
159 static bool hdr_export_pdu
;
161 /* Hex+ASCII text dump identification, to handle an edge case where
162 * the ASCII representation contains patterns that look like bytes. */
163 static uint8_t* pkt_lnstart
;
165 static bool has_direction
;
166 static uint32_t direction
= PACK_FLAGS_RECEPTION_TYPE_UNSPECIFIED
;
167 static bool has_seqno
;
168 static uint64_t seqno
;
169 /*--- Local data -----------------------------------------------------------------*/
171 /* This is where we store the packet currently being built */
172 static uint8_t *packet_buf
;
173 static uint32_t curr_offset
;
174 static uint32_t packet_start
;
175 static bool offset_warned
;
176 static import_status_t
start_new_packet(bool);
178 /* This buffer contains strings present before the packet offset 0 */
179 #define PACKET_PREAMBLE_MAX_LEN 2048
180 static uint8_t packet_preamble
[PACKET_PREAMBLE_MAX_LEN
+1];
181 static int packet_preamble_len
;
183 /* Time code of packet, derived from packet_preamble */
184 static time_t ts_sec
;
185 static uint32_t ts_nsec
;
186 static bool ts_fmt_iso
;
187 static struct tm timecode_default
;
188 static bool timecode_warned
;
189 /* The time delta to add to packets without a valid time code.
190 * This can be no smaller than the time resolution of the dump
191 * file, so the default is 1000 nanoseconds, or 1 microsecond.
192 * XXX: We should at least get this from the resolution of the file we're
193 * writing to, and possibly allow the user to set a different value.
195 static uint32_t ts_tick
= 1000;
197 /* HDR_ETH Offset base to parse */
198 static uint32_t offset_base
= 16;
200 /* ----- State machine -----------------------------------------------------------*/
202 /* Current state of parser */
204 INIT
, /* Waiting for start of new packet */
205 START_OF_LINE
, /* Starting from beginning of line */
206 READ_OFFSET
, /* Just read the offset */
207 READ_BYTE
, /* Just read a byte */
208 READ_TEXT
/* Just read text - ignore until EOL */
210 static parser_state_t state
= INIT
;
212 static const char *state_str
[] = {"Init",
219 static const char *token_str
[] = {"",
228 /* ----- Skeleton Packet Headers --------------------------------------------------*/
231 uint8_t dest_addr
[6];
236 static hdr_ethernet_t HDR_ETHERNET
;
241 uint16_t packet_length
;
242 uint16_t identification
;
247 uint16_t hdr_checksum
;
252 /* Default IPv4 addresses if none supplied */
253 #if G_BYTE_ORDER == G_BIG_ENDIAN
255 #define IP_SRC 0x0a010101
256 #define IP_DST 0x0a020202
259 #define IP_SRC 0x0101010a
260 #define IP_DST 0x0202020a
263 static hdr_ip_t HDR_IP
=
264 {0x45, 0, 0, IP_ID
, 0, 0, 0xff, 0, 0, IP_SRC
, IP_DST
};
266 static struct { /* pseudo header for checksum calculation */
275 /* headers taken from glibc */
280 uint32_t ip6_un1_flow
; /* 24 bits of flow-ID */
281 uint16_t ip6_un1_plen
; /* payload length */
282 uint8_t ip6_un1_nxt
; /* next header */
283 uint8_t ip6_un1_hlim
; /* hop limit */
285 uint8_t ip6_un2_vfc
; /* 4 bits version, 4 bits priority */
287 ws_in6_addr ip6_src
; /* source address */
288 ws_in6_addr ip6_dst
; /* destination address */
291 static hdr_ipv6_t HDR_IPv6
;
293 /* https://tools.ietf.org/html/rfc2460#section-8.1 */
294 static struct { /* pseudo header ipv6 for checksum calculation */
295 struct e_in6_addr src_addr6
;
296 struct e_in6_addr dst_addr6
;
304 uint16_t source_port
;
310 static hdr_udp_t HDR_UDP
= {0, 0, 0, 0};
313 uint16_t source_port
;
324 static hdr_tcp_t HDR_TCP
= {0, 0, 0, 0, 0x50, 0, 0, 0, 0};
333 static hdr_sctp_t HDR_SCTP
= {0, 0, 0, 0};
345 static hdr_data_chunk_t HDR_DATA_CHUNK
= {0, 0, 0, 0, 0, 0, 0};
349 uint16_t payload_len
;
352 static hdr_export_pdu_t HDR_EXPORT_PDU
= {0, 0};
354 #define EXPORT_PDU_END_OF_OPTIONS_SIZE 4
356 /*----------------------------------------------------------------------
357 * Parse a single hex number
358 * Will abort the program if it can't parse the number
359 * Pass in true if this is an offset, false if not
361 static import_status_t
362 parse_num(const char *str
, int offset
, uint32_t* num
)
367 report_failure("FATAL ERROR: str is NULL");
368 return IMPORT_FAILURE
;
372 unsigned long ulnum
= strtoul(str
, &c
, offset
? offset_base
: 16);
374 report_failure("Unable to convert %s to base %u: %s", str
,
375 offset
? offset_base
: 16, g_strerror(errno
));
376 return IMPORT_FAILURE
;
379 report_failure("Unable to convert %s to base %u", str
,
380 offset
? offset_base
: 16);
381 return IMPORT_FAILURE
;
383 if (ulnum
> UINT32_MAX
) {
384 report_failure("%s too large", str
);
385 return IMPORT_FAILURE
;
387 *num
= (uint32_t) ulnum
;
388 return IMPORT_SUCCESS
;
391 /*----------------------------------------------------------------------
392 * Write this byte into current packet
394 static import_status_t
395 write_byte(const char *str
)
399 if (parse_num(str
, false, &num
) != IMPORT_SUCCESS
)
400 return IMPORT_FAILURE
;
402 packet_buf
[curr_offset
] = (uint8_t) num
;
404 if (curr_offset
>= info_p
->max_frame_length
) /* packet full */
405 if (start_new_packet(true) != IMPORT_SUCCESS
)
406 return IMPORT_FAILURE
;
408 return IMPORT_SUCCESS
;
411 /*----------------------------------------------------------------------
412 * Remove bytes from the current packet
415 unwrite_bytes (uint32_t nbytes
)
417 curr_offset
-= nbytes
;
420 /*----------------------------------------------------------------------
421 * Determine SCTP chunk padding length
424 number_of_padding_bytes (uint32_t length
)
428 remainder
= length
% 4;
433 return 4 - remainder
;
436 /*----------------------------------------------------------------------
437 * Write current packet out
439 * @param cont [IN] true if a packet is being written because the max frame
440 * length was reached, and the original packet from the input file is
441 * continued in a later frame. Used to set fragmentation fields in dummy
442 * headers (currently only implemented for SCTP; IPv4 could be added later.)
444 static import_status_t
445 write_current_packet(bool cont
)
447 int prefix_length
= 0;
448 int proto_length
= 0;
450 int eth_trailer_length
= 0;
451 int prefix_index
= 0;
452 int i
, padding_length
;
454 if (curr_offset
> 0) {
455 /* Write the packet */
457 /* Is direction indication on with an inbound packet? */
458 bool isOutbound
= has_direction
&& (direction
== PACK_FLAGS_DIRECTION_OUTBOUND
);
460 /* Compute packet length */
462 if (hdr_export_pdu
) {
463 prefix_length
+= (int)sizeof(HDR_EXPORT_PDU
) + (int)strlen(info_p
->payload
) + EXPORT_PDU_END_OF_OPTIONS_SIZE
;
464 proto_length
= prefix_length
+ curr_offset
;
466 if (hdr_data_chunk
) { prefix_length
+= (int)sizeof(HDR_DATA_CHUNK
); }
467 if (hdr_sctp
) { prefix_length
+= (int)sizeof(HDR_SCTP
); }
468 if (hdr_udp
) { prefix_length
+= (int)sizeof(HDR_UDP
); proto_length
= prefix_length
+ curr_offset
; }
469 if (hdr_tcp
) { prefix_length
+= (int)sizeof(HDR_TCP
); proto_length
= prefix_length
+ curr_offset
; }
471 prefix_length
+= (int)sizeof(HDR_IP
);
472 ip_length
= prefix_length
+ curr_offset
+ ((hdr_data_chunk
) ? number_of_padding_bytes(curr_offset
) : 0);
473 } else if (hdr_ipv6
) {
474 ip_length
= prefix_length
+ curr_offset
+ ((hdr_data_chunk
) ? number_of_padding_bytes(curr_offset
) : 0);
475 /* IPv6 payload length field does not include the header itself.
476 * It does include extension headers, but we don't put any
477 * (if we later do fragments, that would change.)
479 prefix_length
+= (int)sizeof(HDR_IPv6
);
481 if (hdr_ethernet
) { prefix_length
+= (int)sizeof(HDR_ETHERNET
); }
483 /* Make room for dummy header */
484 memmove(&packet_buf
[prefix_length
], packet_buf
, curr_offset
);
488 if (prefix_length
+ curr_offset
< 60) {
489 eth_trailer_length
= 60 - (prefix_length
+ curr_offset
);
493 /* Write Ethernet header */
497 memcpy(HDR_ETHERNET
.dest_addr
, hdr_eth_src_addr
, 6);
498 memcpy(HDR_ETHERNET
.src_addr
, hdr_eth_dest_addr
, 6);
500 memcpy(HDR_ETHERNET
.dest_addr
, hdr_eth_dest_addr
, 6);
501 memcpy(HDR_ETHERNET
.src_addr
, hdr_eth_src_addr
, 6);
503 HDR_ETHERNET
.ethertype
= g_htons(hdr_ethernet_proto
);
504 memcpy(&packet_buf
[prefix_index
], &HDR_ETHERNET
, sizeof(HDR_ETHERNET
));
505 prefix_index
+= (int)sizeof(HDR_ETHERNET
);
508 /* Write IP header */
510 vec_t cksum_vector
[1];
513 HDR_IP
.src_addr
= info_p
->ip_dest_addr
.ipv4
? info_p
->ip_dest_addr
.ipv4
: IP_DST
;
514 HDR_IP
.dest_addr
= info_p
->ip_src_addr
.ipv4
? info_p
->ip_src_addr
.ipv4
: IP_SRC
;
517 HDR_IP
.src_addr
= info_p
->ip_src_addr
.ipv4
? info_p
->ip_src_addr
.ipv4
: IP_SRC
;
518 HDR_IP
.dest_addr
= info_p
->ip_dest_addr
.ipv4
? info_p
->ip_dest_addr
.ipv4
: IP_DST
;
520 HDR_IP
.packet_length
= g_htons(ip_length
);
521 HDR_IP
.protocol
= (uint8_t) hdr_ip_proto
;
522 HDR_IP
.hdr_checksum
= 0;
523 cksum_vector
[0].ptr
= (uint8_t *)&HDR_IP
; cksum_vector
[0].len
= sizeof(HDR_IP
);
524 HDR_IP
.hdr_checksum
= in_cksum(cksum_vector
, 1);
526 memcpy(&packet_buf
[prefix_index
], &HDR_IP
, sizeof(HDR_IP
));
527 prefix_index
+= (int)sizeof(HDR_IP
);
529 /* initialize pseudo header for checksum calculation */
530 pseudoh
.src_addr
= HDR_IP
.src_addr
;
531 pseudoh
.dest_addr
= HDR_IP
.dest_addr
;
533 pseudoh
.protocol
= (uint8_t) hdr_ip_proto
;
534 pseudoh
.length
= g_htons(proto_length
);
535 } else if (hdr_ipv6
) {
536 if (memcmp(&info_p
->ip_dest_addr
.ipv6
, &NO_IPv6_ADDRESS
, sizeof(ws_in6_addr
))) {
537 memcpy(isOutbound
? &HDR_IPv6
.ip6_src
: &HDR_IPv6
.ip6_dst
, &info_p
->ip_dest_addr
.ipv6
, sizeof(ws_in6_addr
));
539 memcpy(isOutbound
? &HDR_IPv6
.ip6_src
: &HDR_IPv6
.ip6_dst
, &IPv6_DST
, sizeof(ws_in6_addr
));
541 if (memcmp(&info_p
->ip_src_addr
.ipv6
, &NO_IPv6_ADDRESS
, sizeof(ws_in6_addr
))) {
542 memcpy(isOutbound
? &HDR_IPv6
.ip6_dst
: &HDR_IPv6
.ip6_src
, &info_p
->ip_src_addr
.ipv6
, sizeof(ws_in6_addr
));
544 memcpy(isOutbound
? &HDR_IPv6
.ip6_dst
: &HDR_IPv6
.ip6_src
, &IPv6_SRC
, sizeof(ws_in6_addr
));
547 HDR_IPv6
.ip6_ctlun
.ip6_un2_vfc
&= 0x0F;
548 HDR_IPv6
.ip6_ctlun
.ip6_un2_vfc
|= (6<< 4);
549 HDR_IPv6
.ip6_ctlun
.ip6_un1
.ip6_un1_plen
= g_htons(ip_length
);
550 HDR_IPv6
.ip6_ctlun
.ip6_un1
.ip6_un1_nxt
= (uint8_t) hdr_ip_proto
;
551 HDR_IPv6
.ip6_ctlun
.ip6_un1
.ip6_un1_hlim
= 32;
553 memcpy(&packet_buf
[prefix_index
], &HDR_IPv6
, sizeof(HDR_IPv6
));
554 prefix_index
+= (int)sizeof(HDR_IPv6
);
556 /* initialize pseudo ipv6 header for checksum calculation */
557 pseudoh6
.src_addr6
= HDR_IPv6
.ip6_src
;
558 pseudoh6
.dst_addr6
= HDR_IPv6
.ip6_dst
;
559 memset(pseudoh6
.zero
, 0, sizeof(pseudoh6
.zero
));
560 pseudoh6
.next_header
= (uint8_t) hdr_ip_proto
;
561 pseudoh6
.length
= g_htons(proto_length
);
564 /* Write UDP header */
566 vec_t cksum_vector
[3];
568 HDR_UDP
.source_port
= isOutbound
? g_htons(info_p
->dst_port
): g_htons(info_p
->src_port
);
569 HDR_UDP
.dest_port
= isOutbound
? g_htons(info_p
->src_port
) : g_htons(info_p
->dst_port
);
570 HDR_UDP
.length
= g_htons(proto_length
);
572 HDR_UDP
.checksum
= 0;
574 cksum_vector
[0].ptr
= (uint8_t *)&pseudoh6
; cksum_vector
[0].len
= sizeof(pseudoh6
);
576 cksum_vector
[0].ptr
= (uint8_t *)&pseudoh
; cksum_vector
[0].len
= sizeof(pseudoh
);
578 cksum_vector
[1].ptr
= (uint8_t *)&HDR_UDP
; cksum_vector
[1].len
= sizeof(HDR_UDP
);
579 cksum_vector
[2].ptr
= &packet_buf
[prefix_length
]; cksum_vector
[2].len
= curr_offset
;
580 HDR_UDP
.checksum
= in_cksum(cksum_vector
, 3);
582 memcpy(&packet_buf
[prefix_index
], &HDR_UDP
, sizeof(HDR_UDP
));
583 prefix_index
+= (int)sizeof(HDR_UDP
);
586 /* Write TCP header */
588 vec_t cksum_vector
[3];
590 HDR_TCP
.source_port
= isOutbound
? g_htons(info_p
->dst_port
): g_htons(info_p
->src_port
);
591 HDR_TCP
.dest_port
= isOutbound
? g_htons(info_p
->src_port
) : g_htons(info_p
->dst_port
);
592 /* set ack number if we have direction */
594 HDR_TCP
.flags
= 0x10;
595 HDR_TCP
.ack_num
= g_ntohl(isOutbound
? tcp_out_seq_num
: tcp_in_seq_num
);
596 HDR_TCP
.ack_num
= g_htonl(HDR_TCP
.ack_num
);
602 HDR_TCP
.seq_num
= isOutbound
? tcp_in_seq_num
: tcp_out_seq_num
;
603 HDR_TCP
.window
= g_htons(0x2000);
605 HDR_TCP
.checksum
= 0;
607 cksum_vector
[0].ptr
= (uint8_t *)&pseudoh6
; cksum_vector
[0].len
= sizeof(pseudoh6
);
609 cksum_vector
[0].ptr
= (uint8_t *)&pseudoh
; cksum_vector
[0].len
= sizeof(pseudoh
);
611 cksum_vector
[1].ptr
= (uint8_t *)&HDR_TCP
; cksum_vector
[1].len
= sizeof(HDR_TCP
);
612 cksum_vector
[2].ptr
= &packet_buf
[prefix_length
]; cksum_vector
[2].len
= curr_offset
;
613 HDR_TCP
.checksum
= in_cksum(cksum_vector
, 3);
615 memcpy(&packet_buf
[prefix_index
], &HDR_TCP
, sizeof(HDR_TCP
));
616 prefix_index
+= (int)sizeof(HDR_TCP
);
618 tcp_in_seq_num
= g_ntohl(tcp_in_seq_num
) + curr_offset
;
619 tcp_in_seq_num
= g_htonl(tcp_in_seq_num
);
622 tcp_out_seq_num
= g_ntohl(tcp_out_seq_num
) + curr_offset
;
623 tcp_out_seq_num
= g_htonl(tcp_out_seq_num
);
627 /* Compute DATA chunk header and append padding */
628 if (hdr_data_chunk
) {
629 hdr_data_chunk_bits
= 0;
630 if (packet_start
== 0) {
631 hdr_data_chunk_bits
|= 0x02;
634 hdr_data_chunk_bits
|= 0x01;
636 HDR_DATA_CHUNK
.type
= hdr_data_chunk_type
;
637 HDR_DATA_CHUNK
.bits
= hdr_data_chunk_bits
;
638 HDR_DATA_CHUNK
.length
= g_htons(curr_offset
+ sizeof(HDR_DATA_CHUNK
));
639 HDR_DATA_CHUNK
.tsn
= g_htonl(hdr_data_chunk_tsn
);
640 HDR_DATA_CHUNK
.sid
= g_htons(hdr_data_chunk_sid
);
641 HDR_DATA_CHUNK
.ssn
= g_htons(hdr_data_chunk_ssn
);
642 HDR_DATA_CHUNK
.ppid
= g_htonl(info_p
->ppi
);
643 hdr_data_chunk_tsn
++;
645 hdr_data_chunk_ssn
++;
647 padding_length
= number_of_padding_bytes(curr_offset
);
648 for (i
=0; i
<padding_length
; i
++)
649 packet_buf
[prefix_length
+curr_offset
+i
] = 0;
650 curr_offset
+= padding_length
;
653 /* Write SCTP header */
655 HDR_SCTP
.src_port
= isOutbound
? g_htons(info_p
->dst_port
): g_htons(info_p
->src_port
);
656 HDR_SCTP
.dest_port
= isOutbound
? g_htons(info_p
->src_port
) : g_htons(info_p
->dst_port
);
657 HDR_SCTP
.tag
= g_htonl(info_p
->tag
);
658 HDR_SCTP
.checksum
= g_htonl(0);
660 HDR_SCTP
.checksum
= crc32c_calculate(&HDR_SCTP
, sizeof(HDR_SCTP
), CRC32C_PRELOAD
);
662 HDR_SCTP
.checksum
= crc32c_calculate(&HDR_DATA_CHUNK
, sizeof(HDR_DATA_CHUNK
), HDR_SCTP
.checksum
);
663 HDR_SCTP
.checksum
= g_htonl(~crc32c_calculate(&packet_buf
[prefix_length
], curr_offset
, HDR_SCTP
.checksum
));
665 memcpy(&packet_buf
[prefix_index
], &HDR_SCTP
, sizeof(HDR_SCTP
));
666 prefix_index
+= (int)sizeof(HDR_SCTP
);
669 /* Write DATA chunk header */
670 if (hdr_data_chunk
) {
671 memcpy(&packet_buf
[prefix_index
], &HDR_DATA_CHUNK
, sizeof(HDR_DATA_CHUNK
));
672 /*prefix_index += (int)sizeof(HDR_DATA_CHUNK);*/
675 /* Write ExportPDU header */
676 if (hdr_export_pdu
) {
677 unsigned payload_len
= (unsigned)strlen(info_p
->payload
);
678 HDR_EXPORT_PDU
.tag_type
= g_htons(EXP_PDU_TAG_DISSECTOR_NAME
);
679 HDR_EXPORT_PDU
.payload_len
= g_htons(payload_len
);
680 memcpy(&packet_buf
[prefix_index
], &HDR_EXPORT_PDU
, sizeof(HDR_EXPORT_PDU
));
681 prefix_index
+= sizeof(HDR_EXPORT_PDU
);
682 memcpy(&packet_buf
[prefix_index
], info_p
->payload
, payload_len
);
683 prefix_index
+= payload_len
;
684 /* Add end-of-options tag */
685 memset(&packet_buf
[prefix_index
], 0x00, 4);
688 /* Write Ethernet trailer */
689 if (hdr_ethernet
&& eth_trailer_length
> 0) {
690 memset(&packet_buf
[prefix_length
+curr_offset
], 0, eth_trailer_length
);
693 HDR_TCP
.seq_num
= g_ntohl(HDR_TCP
.seq_num
) + curr_offset
;
694 HDR_TCP
.seq_num
= g_htonl(HDR_TCP
.seq_num
);
696 /* Write the packet */
701 memset(&rec
, 0, sizeof rec
);
703 if (info_p
->encapsulation
== WTAP_ENCAP_SYSTEMD_JOURNAL
) {
704 rec
.rec_type
= REC_TYPE_SYSTEMD_JOURNAL_EXPORT
;
705 rec
.block
= wtap_block_create(WTAP_BLOCK_SYSTEMD_JOURNAL_EXPORT
);
706 rec
.rec_header
.systemd_journal_export_header
.record_len
= prefix_length
+ curr_offset
+ eth_trailer_length
;
707 rec
.presence_flags
= WTAP_HAS_CAP_LEN
|WTAP_HAS_TS
;
708 /* XXX: Ignore our direction, packet id, and timestamp. For a
709 * systemd Journal Export Block the timestamp comes from the
710 * __REALTIME_TIMESTAMP= field. We don't check to see if that
711 * field is there (it MUST be, but we don't check whether our
712 * input is malformed in general), but since the presence flags
713 * aren't really used when writing, it doesn't matter.
716 rec
.rec_type
= REC_TYPE_PACKET
;
717 rec
.block
= wtap_block_create(WTAP_BLOCK_PACKET
);
718 rec
.rec_header
.packet_header
.caplen
= rec
.rec_header
.packet_header
.len
= prefix_length
+ curr_offset
+ eth_trailer_length
;
719 rec
.ts
.secs
= ts_sec
;
720 rec
.ts
.nsecs
= ts_nsec
;
721 rec
.rec_header
.packet_header
.pkt_encap
= info_p
->encapsulation
;
722 rec
.presence_flags
= WTAP_HAS_CAP_LEN
|WTAP_HAS_INTERFACE_ID
|WTAP_HAS_TS
;
724 wtap_block_add_uint32_option(rec
.block
, OPT_PKT_FLAGS
, direction
);
727 wtap_block_add_uint64_option(rec
.block
, OPT_PKT_PACKETID
, seqno
);
731 if (!wtap_dump(info_p
->wdh
, &rec
, packet_buf
, &err
, &err_info
)) {
732 report_cfile_write_failure(info_p
->import_text_filename
,
733 info_p
->output_filename
, err
, err_info
,
734 info_p
->num_packets_read
,
735 wtap_dump_file_type_subtype(info_p
->wdh
));
736 wtap_block_unref(rec
.block
);
737 return IMPORT_FAILURE
;
739 wtap_block_unref(rec
.block
);
740 info_p
->num_packets_written
++;
743 packet_start
+= curr_offset
;
745 return IMPORT_SUCCESS
;
749 /*----------------------------------------------------------------------
750 * Append a token to the packet preamble.
752 static import_status_t
753 append_to_preamble(char *str
)
757 if (packet_preamble_len
!= 0) {
758 if (packet_preamble_len
== PACKET_PREAMBLE_MAX_LEN
)
759 return IMPORT_SUCCESS
; /* no room to add more preamble */
760 /* XXX: Just keep going? This is probably not a problem, unless
761 * someone had >2000 bytes of whitespace before the timestamp... */
762 /* Add a blank separator between the previous token and this token. */
763 packet_preamble
[packet_preamble_len
++] = ' ';
766 report_failure("FATAL ERROR: str is NULL");
767 return IMPORT_FAILURE
;
769 toklen
= strlen(str
);
771 if (packet_preamble_len
+ toklen
> PACKET_PREAMBLE_MAX_LEN
)
772 return IMPORT_SUCCESS
; /* no room to add token to the preamble */
773 /* XXX: Just keep going? This is probably not a problem, as above.*/
774 (void) g_strlcpy(&packet_preamble
[packet_preamble_len
], str
, PACKET_PREAMBLE_MAX_LEN
);
775 packet_preamble_len
+= (int) toklen
;
776 if (ws_log_get_level() >= LOG_LEVEL_NOISY
) {
778 char xs
[PACKET_PREAMBLE_MAX_LEN
];
779 (void) g_strlcpy(xs
, packet_preamble
, PACKET_PREAMBLE_MAX_LEN
);
780 while ((c
= strchr(xs
, '\r')) != NULL
) *c
=' ';
781 ws_noisy("[[append_to_preamble: \"%s\"]]", xs
);
785 return IMPORT_SUCCESS
;
788 #define INVALID_VALUE (-1)
790 #define WHITESPACE_VALUE (-2)
793 * Information on how to parse any plainly encoded binary data
795 * one Unit is least_common_mmultiple(bits_per_char, 8) bits.
797 struct plain_decoding_data
{
799 unsigned chars_per_unit
;
800 unsigned bytes_per_unit
: 3; /* Internally a uint64_t is used to hold units */
801 unsigned bits_per_char
: 6;
805 #define _INVALID_INIT2 INVALID_VALUE, INVALID_VALUE
806 #define _INVALID_INIT4 _INVALID_INIT2, _INVALID_INIT2
807 #define _INVALID_INIT8 _INVALID_INIT4, _INVALID_INIT4
808 #define _INVALID_INIT16 _INVALID_INIT8, _INVALID_INIT8
809 #define _INVALID_INIT32 _INVALID_INIT16, _INVALID_INIT16
810 #define _INVALID_INIT64 _INVALID_INIT32, _INVALID_INIT32
811 #define _INVALID_INIT128 _INVALID_INIT64, _INVALID_INIT64
812 #define _INVALID_INIT256 _INVALID_INIT128, _INVALID_INIT128
814 #define INVALID_INIT _INVALID_INIT256
815 // this is a gcc/clang extension:
816 // [0 ... 255] = INVALID_VALUE
818 #define WHITESPACE_INIT \
819 [' '] = WHITESPACE_VALUE, \
820 ['\t'] = WHITESPACE_VALUE, \
821 ['\n'] = WHITESPACE_VALUE, \
822 ['\v'] = WHITESPACE_VALUE, \
823 ['\f'] = WHITESPACE_VALUE, \
824 ['\r'] = WHITESPACE_VALUE
828 * Some compilers warn about initializing the same subobject
829 * more than once with designated initializers.
831 * We're doing that - INVALID_INIT iniitalizes everything to
832 * INVALID_VALUE, but then we override selected elements -
833 * but we know what we're doing, so just suppress that
838 const struct plain_decoding_data hex_decode_info
= {
845 [':'] = WHITESPACE_VALUE
,
846 ['0'] = 0,1,2,3,4,5,6,7,8,9,
847 ['A'] = 10,11,12,13,14,15,
848 ['a'] = 10,11,12,13,14,15
852 const struct plain_decoding_data bin_decode_info
= {
863 const struct plain_decoding_data oct_decode_info
= {
870 ['0'] = 0,1,2,3,4,5,6,7
874 const struct plain_decoding_data base64_decode_info
= {
881 ['A'] = 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
882 ['a'] = 26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
883 ['0'] = 52,53,54,55,56,57,58,59,60,61,
886 ['='] = WHITESPACE_VALUE
/* padding at the end, the decoder doesn't need this, so just ignores it */
892 /*******************************************************************************
893 * The modularized part of this mess, used by the wrapper around the regex
894 * engine in text_import_regex.c to hook into this state-machine backend.
896 * Should the rest be modularized as well? Maybe, but then start with pcap2text.c
900 * This function parses encoded data according to <encoding> into binary data.
901 * It will continue until one of the following conditions is met:
903 * - dest cannot hold another full unit of data
904 * - an invalid character is read
905 * When this happens any complete bytes will be recovered from the remaining
906 * possibly incomplete unit and stored to dest (there will be no incomplete unit
907 * if dest is full). Any remaining bits will be discarded.
908 * src and dest will be advanced to where parsing including this last incomplete
910 * If you want to continue parsing (meaning incomplete units were due to call
911 * fragmentation and not actually due to EOT) you have to resume the parser at
912 * *src_last_unit and dest - result % bytes_per_unit
914 static int parse_plain_data(unsigned char** src
, const unsigned char* src_end
,
915 uint8_t** dest
, const uint8_t* dest_end
, const struct plain_decoding_data
* encoding
,
916 unsigned char** src_last_unit
) {
921 unsigned c_chars
= 0;
923 * Src data |- - -|- - -|- - -|- - -|- - -|- - -|- - -|- - -|
924 * Bytes |- - - - - - - -|- - - - - - - -|- - - - - - - -|
925 * Units |- - - - - - - - - - - - - - - - - - - - - - - -|
929 if (ws_log_get_level() >= LOG_LEVEL_NOISY
) {
930 char* debug_str
= wmem_strndup(NULL
, *src
, (src_end
-*src
));
931 ws_noisy("parsing data: %s", debug_str
);
932 wmem_free(NULL
, debug_str
);
934 while (*src
< src_end
&& *dest
+ encoding
->bytes_per_unit
<= dest_end
) {
935 val
= encoding
->table
[**src
];
940 case WHITESPACE_VALUE
:
941 ws_warning("Unexpected char %d in data", **src
);
944 c_val
= c_val
<< encoding
->bits_per_char
| val
;
946 /* another full unit */
947 if (c_chars
== encoding
->chars_per_unit
) {
950 *src_last_unit
= *src
;
952 for (j
= encoding
->bytes_per_unit
; j
> 0; --j
) {
953 **dest
= (char) (c_val
>> (j
* 8 - 8));
961 for (j
= c_chars
* encoding
->bits_per_char
; j
>= 8; j
-= 8) {
962 **dest
= (char) (c_val
>> (j
- 8));
965 return status
* units
;
968 void parse_data(unsigned char* start_field
, unsigned char* end_field
, enum data_encoding encoding
) {
969 uint8_t* dest
= &packet_buf
[curr_offset
];
970 uint8_t* dest_end
= &packet_buf
[info_p
->max_frame_length
];
972 const struct plain_decoding_data
* table
; /* should be further down */
974 case ENCODING_PLAIN_HEX
:
975 case ENCODING_PLAIN_OCT
:
976 case ENCODING_PLAIN_BIN
:
977 case ENCODING_BASE64
:
978 /* const struct plain_decoding_data* table; // This can't be here because gcc says no */
980 case ENCODING_PLAIN_HEX
:
981 table
= &hex_decode_info
;
983 case ENCODING_PLAIN_OCT
:
984 table
= &oct_decode_info
;
986 case ENCODING_PLAIN_BIN
:
987 table
= &bin_decode_info
;
989 case ENCODING_BASE64
:
990 table
= &base64_decode_info
;
995 info_p
->num_packets_read
++;
997 parse_plain_data(&start_field
, end_field
, &dest
, dest_end
, table
, NULL
);
998 curr_offset
= (int) (dest
- packet_buf
);
999 if (curr_offset
== info_p
->max_frame_length
) {
1000 write_current_packet(true);
1001 dest
= &packet_buf
[curr_offset
];
1007 ws_critical("not implemented/invalid encoding type");
1012 #define setFlags(VAL, MASK, FLAGS) \
1013 ((VAL) & ~(MASK)) | ((FLAGS) & (MASK))
1015 static void _parse_dir(const unsigned char* start_field
, const unsigned char* end_field _U_
, const char* in_indicator
, const char* out_indicator
, uint32_t* dir
) {
1017 for (; *in_indicator
&& *start_field
!= *in_indicator
; ++in_indicator
);
1018 if (*in_indicator
) {
1019 *dir
= setFlags(*dir
, PACK_FLAGS_DIRECTION_MASK
<< PACK_FLAGS_DIRECTION_SHIFT
, PACK_FLAGS_DIRECTION_INBOUND
);
1022 for (; *out_indicator
&& *start_field
!= *out_indicator
; ++out_indicator
);
1023 if (*out_indicator
) {
1024 *dir
= setFlags(*dir
, PACK_FLAGS_DIRECTION_MASK
<< PACK_FLAGS_DIRECTION_SHIFT
, PACK_FLAGS_DIRECTION_OUTBOUND
);
1027 *dir
= setFlags(*dir
, PACK_FLAGS_DIRECTION_MASK
<< PACK_FLAGS_DIRECTION_SHIFT
, PACK_FLAGS_DIRECTION_UNKNOWN
);
1030 void parse_dir(const unsigned char* start_field
, const unsigned char* end_field
, const char* in_indicator
, const char* out_indicator
) {
1031 _parse_dir(start_field
, end_field
, in_indicator
, out_indicator
, &direction
);
1034 #define PARSE_BUF 64
1036 /* Attempt to parse a time according to the given format. If the conversion
1037 * succeeds, set sec and nsec appropriately and return true. If it fails,
1038 * leave sec and nsec unchanged and return false.
1041 _parse_time(const unsigned char* start_field
, const unsigned char* end_field
, const char* _format
, time_t* sec
, int* nsec
) {
1046 char field
[PARSE_BUF
];
1047 char format
[PARSE_BUF
];
1056 (void) g_strlcpy(field
, start_field
, MIN(end_field
- start_field
+ 1, PARSE_BUF
));
1059 if (!iso8601_to_nstime(&ts_iso
, field
, ISO8601_DATETIME_AUTO
)) {
1063 *nsec
= ts_iso
.nsecs
;
1065 (void) g_strlcpy(format
, _format
, PARSE_BUF
);
1068 * Initialize to today, local time, just in case not all fields
1069 * of the date and time are specified.
1071 timecode
= timecode_default
;
1075 * %f is for fractions of seconds not supported by strptime
1076 * BTW: what is this function name? is this some russian joke?
1078 subsecs_fmt
= g_strrstr(format
, "%f");
1083 cursor
= ws_strptime_p(cursor
, format
, &timecode
);
1085 if (cursor
== NULL
) {
1089 if (subsecs_fmt
!= NULL
) {
1091 * Parse subsecs and any following format
1093 nsec_buf
= (unsigned) strtol(cursor
, &p
, 10);
1098 subseclen
= (int) (p
- cursor
);
1100 cursor
= ws_strptime_p(cursor
, subsecs_fmt
+ 2, &timecode
);
1101 if (cursor
== NULL
) {
1106 if (subseclen
> 0) {
1108 * Convert that number to a number
1109 * of nanoseconds; if it's N digits
1110 * long, it's in units of 10^(-N) seconds,
1111 * so, to convert it to units of
1112 * 10^-9 seconds, we multiply by
1115 if (subseclen
> SUBSEC_PREC
) {
1117 * *More* than 9 digits; 9-N is
1118 * negative, so we divide by
1121 for (i
= subseclen
- SUBSEC_PREC
; i
!= 0; i
--)
1123 } else if (subseclen
< SUBSEC_PREC
) {
1124 for (i
= SUBSEC_PREC
- subseclen
; i
!= 0; i
--)
1129 if ( -1 == (sec_buf
= mktime(&timecode
)) ) {
1137 ws_noisy("parsed time %s Format(%s), time(%u), subsecs(%u)\n", field
, _format
, (uint32_t)*sec
, (uint32_t)*nsec
);
1142 void parse_time(const unsigned char* start_field
, const unsigned char* end_field
, const char* format
) {
1143 if (format
== NULL
|| !_parse_time(start_field
, end_field
, format
, &ts_sec
, &ts_nsec
)) {
1148 void parse_seqno(const unsigned char* start_field
, const unsigned char* end_field
) {
1149 char* buf
= (char*) g_alloca(end_field
- start_field
+ 1);
1150 (void) g_strlcpy(buf
, start_field
, end_field
- start_field
+ 1);
1151 seqno
= g_ascii_strtoull(buf
, NULL
, 10);
1154 void flush_packet(void) {
1155 write_current_packet(false);
1158 /*----------------------------------------------------------------------
1159 * Parse the preamble to get the timecode.
1163 parse_preamble (void)
1166 bool got_time
= false;
1169 * Null-terminate the preamble.
1171 packet_preamble
[packet_preamble_len
] = '\0';
1173 if (has_direction
) {
1174 _parse_dir(&packet_preamble
[0], &packet_preamble
[1], "iI", "oO", &direction
);
1175 i
= (direction
== PACK_FLAGS_DIRECTION_UNKNOWN
) ? 0 : 1;
1176 while (packet_preamble
[i
] == ' ' ||
1177 packet_preamble
[i
] == '\r' ||
1178 packet_preamble
[i
] == '\t') {
1181 packet_preamble_len
-= i
;
1182 /* Also move the trailing '\0'. */
1183 memmove(packet_preamble
, packet_preamble
+ i
, packet_preamble_len
+ 1);
1187 * If no time stamp format was specified, don't attempt to parse
1188 * the packet preamble to extract a time stamp.
1191 /* Ensure preamble has more than two chars before attempting to parse.
1192 * This should cover line breaks etc that get counted.
1194 if ( info_p
->timestamp_format
!= NULL
&& strlen(packet_preamble
) > 2 ) {
1195 got_time
= _parse_time(packet_preamble
, packet_preamble
+ strlen(packet_preamble
), info_p
->timestamp_format
, &ts_sec
, &ts_nsec
);
1197 /* Let's only have a possible GUI popup once, other messages to log
1199 if (!timecode_warned
) {
1200 report_warning("Time conversions (%s) failed, advancing time by %d ns from previous packet on failure. First failure was for %s on input packet %d.", info_p
->timestamp_format
, ts_tick
, packet_preamble
, info_p
->num_packets_read
);
1201 timecode_warned
= true;
1203 ws_warning("Time conversion (%s) failed for %s on input packet %d.", info_p
->timestamp_format
, packet_preamble
, info_p
->num_packets_read
);
1206 if (ws_log_get_level() >= LOG_LEVEL_NOISY
) {
1208 while ((c
= strchr(packet_preamble
, '\r')) != NULL
) *c
=' ';
1209 ws_noisy("[[parse_preamble: \"%s\"]]", packet_preamble
);
1210 ws_noisy("Format(%s), time(%u), subsecs(%u)", info_p
->timestamp_format
, (uint32_t)ts_sec
, ts_nsec
);
1217 /* Clear Preamble */
1218 packet_preamble_len
= 0;
1221 /*----------------------------------------------------------------------
1222 * Start a new packet
1224 * @param cont [IN] true if a new packet is starting because the max frame
1225 * length was reached on the current packet, and the original packet from the
1226 * input file is continued in a later frame. Passed to write_current_packet,
1227 * where it is used to set fragmentation fields in dummy headers (currently
1228 * only implemented for SCTP; IPv4 could be added later.)
1230 static import_status_t
1231 start_new_packet(bool cont
)
1233 ws_debug("Start new packet (cont = %s).", cont
? "true" : "false");
1235 /* Write out the current packet, if required */
1236 if (write_current_packet(cont
) != IMPORT_SUCCESS
)
1237 return IMPORT_FAILURE
;
1238 info_p
->num_packets_read
++;
1240 /* Ensure we parse the packet preamble as it may contain the time */
1241 /* THIS IMPLIES A STATE TRANSITION OUTSIDE THE STATE MACHINE */
1244 return IMPORT_SUCCESS
;
1247 /*----------------------------------------------------------------------
1248 * Process a directive
1251 process_directive (char *str _U_
)
1254 tokens
= g_strsplit_set(str
+10, "\r\n", 2);
1255 ws_message("--- Directive [%s] currently unsupported ---", tokens
[0]);
1259 /*----------------------------------------------------------------------
1260 * Parse a single token (called from the scanner)
1263 parse_token(token_t token
, char *str
)
1266 /* Variables for the hex+ASCII identification / lookback */
1276 * This is implemented as a simple state machine of five states.
1277 * State transitions are caused by tokens being received from the
1278 * scanner. The code should be self_documenting.
1281 if (ws_log_get_level() >= LOG_LEVEL_NOISY
) {
1282 /* Sanitize - remove all '\r' */
1284 if (str
!=NULL
) { while ((c
= strchr(str
, '\r')) != NULL
) *c
=' '; }
1286 ws_noisy("(%s, %s \"%s\") -> (",
1287 state_str
[state
], token_str
[token
], str
? str
: "");
1292 /* ----- Waiting for new packet -------------------------------------------*/
1296 append_to_preamble(str
);
1299 process_directive(str
);
1302 if (offset_base
== 0) {
1303 append_to_preamble(str
);
1304 /* If we're still in the INIT state, maybe there's something
1305 * odd like a time format with no separators. That wouldn't
1306 * work in a mode with an offset, but give it a try.
1308 tokens
= g_strsplit_set(str
, ": \t\r\n", 2);
1309 if (!offset_warned
) {
1310 report_warning("Running in no offset mode but read offset (%s) at start of file, treating as preamble", tokens
[0]);
1311 offset_warned
= true;
1313 ws_warning("Running in no offset mode but read offset (%s) at start of file, treating as preamble", tokens
[0]);
1317 if (parse_num(str
, true, &num
) != IMPORT_SUCCESS
)
1318 return IMPORT_FAILURE
;
1320 /* New packet starts here */
1321 if (start_new_packet(false) != IMPORT_SUCCESS
)
1322 return IMPORT_FAILURE
;
1323 state
= READ_OFFSET
;
1324 pkt_lnstart
= packet_buf
+ num
;
1328 if (offset_base
== 0) {
1329 if (start_new_packet(false) != IMPORT_SUCCESS
)
1330 return IMPORT_FAILURE
;
1331 if (write_byte(str
) != IMPORT_SUCCESS
)
1332 return IMPORT_FAILURE
;
1334 pkt_lnstart
= packet_buf
;
1338 if (write_current_packet(false) != IMPORT_SUCCESS
)
1339 return IMPORT_FAILURE
;
1346 /* ----- Processing packet, start of new line -----------------------------*/
1350 append_to_preamble(str
);
1353 process_directive(str
);
1356 if (offset_base
== 0) {
1357 /* After starting the packet there's no point adding it to
1358 * the preamble in this mode (we only do one packet.)
1359 * Use a generic warning message to suppress the many
1360 * expected duplicates. */
1361 tokens
= g_strsplit_set(str
, ": \t\r\n", 2);
1362 if (!offset_warned
) {
1363 report_warning("Running in no offset mode but read offset (%s) at start of line, ignoring", tokens
[0]);
1364 offset_warned
= true;
1366 ws_warning("Running in no offset mode but read offset (%s) at start of line, ignoring.", tokens
[0]);
1370 if (parse_num(str
, true, &num
) != IMPORT_SUCCESS
)
1371 return IMPORT_FAILURE
;
1373 /* New packet starts here */
1374 if (start_new_packet(false) != IMPORT_SUCCESS
)
1375 return IMPORT_FAILURE
;
1377 state
= READ_OFFSET
;
1378 } else if ((num
- packet_start
) != curr_offset
) {
1380 * The offset we read isn't the one we expected.
1381 * This may only mean that we mistakenly interpreted
1382 * some text as byte values (e.g., if the text dump
1383 * of packet data included a number with spaces around
1384 * it). If the offset is less than what we expected,
1385 * assume that's the problem, and throw away the putative
1386 * extra byte values.
1388 if (num
< curr_offset
) {
1389 unwrite_bytes(curr_offset
- num
);
1390 state
= READ_OFFSET
;
1392 /* Bad offset; switch to INIT state */
1393 ws_message("Inconsistent offset. Expecting %0X, got %0X. Ignoring rest of packet",
1395 if (write_current_packet(false) != IMPORT_SUCCESS
)
1396 return IMPORT_FAILURE
;
1400 state
= READ_OFFSET
;
1402 pkt_lnstart
= packet_buf
+ num
;
1405 if (offset_base
== 0) {
1406 if (write_byte(str
) != IMPORT_SUCCESS
)
1407 return IMPORT_FAILURE
;
1409 pkt_lnstart
= packet_buf
;
1413 if (write_current_packet(false) != IMPORT_SUCCESS
)
1414 return IMPORT_FAILURE
;
1421 /* ----- Processing packet, read offset -----------------------------------*/
1425 /* Record the byte */
1427 if (write_byte(str
) != IMPORT_SUCCESS
)
1428 return IMPORT_FAILURE
;
1436 state
= START_OF_LINE
;
1439 if (write_current_packet(false) != IMPORT_SUCCESS
)
1440 return IMPORT_FAILURE
;
1447 /* ----- Processing packet, read byte -------------------------------------*/
1451 /* Record the byte */
1452 if (write_byte(str
) != IMPORT_SUCCESS
)
1453 return IMPORT_FAILURE
;
1461 if (token
== T_EOL
) {
1463 state
= START_OF_LINE
;
1465 if (info_p
->hexdump
.identify_ascii
) {
1466 /* Here a line of pkt bytes reading is finished
1467 compare the ascii and hex to avoid such situation:
1468 "61 62 20 ab ", when ab is ascii dump then it should
1469 not be treat as byte */
1471 /* s2 is the ASCII string, s1 is the HEX string, e.g, when
1472 s2 = "ab ", s1 = "616220"
1473 we should find out the largest tail of s1 matches the head
1474 of s2, it means the matched part in tail is the ASCII dump
1475 of the head byte. These matched should be rollback */
1476 line_size
= curr_offset
-(int)(pkt_lnstart
-packet_buf
);
1477 s2
= (char*)g_malloc((line_size
+1)/4+1);
1478 /* gather the possible pattern */
1479 for (i
= 0; i
< (line_size
+1)/4; i
++) {
1480 tmp_str
[0] = pkt_lnstart
[i
*3];
1481 tmp_str
[1] = pkt_lnstart
[i
*3+1];
1483 /* it is a valid convertible string */
1484 if (!g_ascii_isxdigit(tmp_str
[0]) || !g_ascii_isxdigit(tmp_str
[1])) {
1487 s2
[i
] = (char)strtoul(tmp_str
, (char **)NULL
, 16);
1489 /* the 3rd entry is not a delimiter, so the possible byte pattern will not shown */
1490 if (!(pkt_lnstart
[i
*3+2] == ' ')) {
1496 /* If packet line start contains possible byte pattern, the line end
1497 should contain the matched pattern if the user open the -a flag.
1498 The packet will be possible invalid if the byte pattern cannot find
1499 a matched one in the line of packet buffer.*/
1501 if (strncmp(pkt_lnstart
+line_size
-rollback
, s2
, rollback
) == 0) {
1502 unwrite_bytes(rollback
);
1504 /* Not matched. This line contains invalid packet bytes, so
1505 discard the whole line */
1507 unwrite_bytes(line_size
);
1514 if (write_current_packet(false) != IMPORT_SUCCESS
)
1515 return IMPORT_FAILURE
;
1522 /* ----- Processing packet, read text -------------------------------------*/
1526 state
= START_OF_LINE
;
1529 if (write_current_packet(false) != IMPORT_SUCCESS
)
1530 return IMPORT_FAILURE
;
1538 report_failure("FATAL ERROR: Bad state (%d)", state
);
1539 return IMPORT_FAILURE
;
1542 ws_noisy(", %s)", state_str
[state
]);
1544 return IMPORT_SUCCESS
;
1547 /*----------------------------------------------------------------------
1548 * Import a text file.
1551 text_import(text_import_info_t
* const info
)
1553 import_status_t status
;
1557 /* Lets start from the beginning */
1561 packet_preamble_len
= 0;
1562 direction
= PACK_FLAGS_DIRECTION_UNKNOWN
;
1563 ts_sec
= time(0); /* initialize to current time */
1564 now_tm
= localtime(&ts_sec
);
1565 if (now_tm
== NULL
) {
1567 * This shouldn't happen - on UN*X, this should Just Work, and
1568 * on 32 bit Windows built with 32 bit time_t, it won't work if ts_sec
1569 * is before the Epoch, but it's long after 1970 (and even 32 bit
1570 * Windows builds with 64 bit time_t by default now), so....
1572 report_failure("localtime(right now) failed");
1573 return WS_EXIT_INIT_FAILED
;
1575 timecode_default
= *now_tm
;
1576 timecode_default
.tm_isdst
= -1; /* Unknown for now, depends on time given to the strptime() function */
1579 /* Get input parameters. */
1583 hdr_ethernet
= false;
1588 hdr_data_chunk
= false;
1589 hdr_export_pdu
= false;
1591 if (info
->mode
== TEXT_IMPORT_HEXDUMP
) {
1592 switch (info
->hexdump
.offset_type
)
1607 has_direction
= info
->hexdump
.has_direction
;
1609 } else if (info
->mode
== TEXT_IMPORT_REGEX
) {
1610 has_direction
= g_regex_get_string_number(info
->regex
.format
, "dir") >= 0;
1611 has_seqno
= g_regex_get_string_number(info
->regex
.format
, "seqno") >= 0;
1614 if (info
->timestamp_format
== NULL
|| g_ascii_strcasecmp(info
->timestamp_format
, "ISO")) {
1619 offset_warned
= false;
1620 timecode_warned
= false;
1622 /* XXX: It would be good to know the time precision of the file,
1623 * to use for the time delta for packets without timestamps. (ts_tick)
1624 * That could either be added to text_import_info_t or a method
1625 * added to get it from wtap_dumper (which is opaque.)
1628 switch (info
->dummy_header_type
)
1631 hdr_ethernet
= true;
1632 hdr_ethernet_proto
= info
->pid
;
1637 hdr_ip_proto
= info
->protocol
;
1660 case HEADER_SCTP_DATA
:
1662 hdr_data_chunk
= true;
1667 case HEADER_EXPORT_PDU
:
1668 hdr_export_pdu
= true;
1679 hdr_ethernet_proto
= 0x86DD;
1681 hdr_ethernet_proto
= 0x0800;
1684 switch (info
->encapsulation
) {
1686 case (WTAP_ENCAP_ETHERNET
):
1687 hdr_ethernet
= true;
1690 case (WTAP_ENCAP_RAW_IP
):
1693 case (WTAP_ENCAP_RAW_IP4
):
1695 report_failure("Encapsulation %s only supports IPv4 headers, not IPv6", wtap_encap_name(info
->encapsulation
));
1696 return WS_EXIT_INVALID_OPTION
;
1700 case (WTAP_ENCAP_RAW_IP6
):
1702 report_failure("Encapsulation %s only supports IPv6 headers, not IPv4", wtap_encap_name(info
->encapsulation
));
1703 return WS_EXIT_INVALID_OPTION
;
1708 report_failure("Dummy IP header not supported with encapsulation: %s (%s)", wtap_encap_name(info
->encapsulation
), wtap_encap_description(info
->encapsulation
));
1709 return WS_EXIT_INVALID_OPTION
;
1713 info
->num_packets_read
= 0;
1714 info
->num_packets_written
= 0;
1716 packet_buf
= (uint8_t *)g_malloc(sizeof(HDR_ETHERNET
) + sizeof(HDR_IP
) +
1717 sizeof(HDR_SCTP
) + sizeof(HDR_DATA_CHUNK
) +
1718 sizeof(HDR_EXPORT_PDU
) + WTAP_MAX_PACKET_SIZE_STANDARD
);
1722 /* XXX: This doesn't happen, because g_malloc aborts the program on
1723 * error, unlike malloc or g_try_malloc.
1725 report_failure("FATAL ERROR: no memory for packet buffer");
1726 return WS_EXIT_INIT_FAILED
;
1729 if (info
->mode
== TEXT_IMPORT_HEXDUMP
) {
1730 status
= text_import_scan(info
->hexdump
.import_text_FILE
);
1732 case (IMPORT_SUCCESS
):
1735 case (IMPORT_FAILURE
):
1736 ret
= WS_EXIT_INVALID_FILE
;
1738 case (IMPORT_INIT_FAILED
):
1739 report_failure("Can't initialize scanner: %s", g_strerror(errno
));
1740 ret
= WS_EXIT_INIT_FAILED
;
1745 } else if (info
->mode
== TEXT_IMPORT_REGEX
) {
1746 ret
= text_import_regex(info
);
1748 info
->num_packets_read
= ret
;
1750 } else if (ret
< 0) {
1751 ret
= WS_EXIT_INVALID_FILE
;
1754 ret
= WS_EXIT_INVALID_OPTION
;
1760 /* Write the SHB and IDB to the wtap_dump_params before opening the wtap dump
1761 * file. While dummy headers can be written automatically, this writes out
1762 * some extra information including an optional interface name.
1765 text_import_pre_open(wtap_dump_params
* const params
, int file_type_subtype
, const char* const input_filename
, const char* const interface_name
)
1767 wtap_block_t shb_hdr
;
1768 wtap_block_t int_data
;
1769 wtapng_if_descr_mandatory_t
*int_data_mand
;
1773 if (wtap_file_type_subtype_supports_block(file_type_subtype
, WTAP_BLOCK_SECTION
) != BLOCK_NOT_SUPPORTED
&&
1774 wtap_file_type_subtype_supports_option(file_type_subtype
, WTAP_BLOCK_SECTION
, OPT_COMMENT
) != OPTION_NOT_SUPPORTED
) {
1776 shb_hdr
= wtap_block_create(WTAP_BLOCK_SECTION
);
1778 comment
= ws_strdup_printf("Generated from input file %s.", input_filename
);
1779 wtap_block_add_string_option(shb_hdr
, OPT_COMMENT
, comment
, strlen(comment
));
1782 info_str
= g_string_new("");
1783 get_cpu_info(info_str
);
1784 if (info_str
->str
) {
1785 wtap_block_add_string_option(shb_hdr
, OPT_SHB_HARDWARE
, info_str
->str
, info_str
->len
);
1787 g_string_free(info_str
, TRUE
);
1789 info_str
= g_string_new("");
1790 get_os_version_info(info_str
);
1791 if (info_str
->str
) {
1792 wtap_block_add_string_option(shb_hdr
, OPT_SHB_OS
, info_str
->str
, info_str
->len
);
1794 g_string_free(info_str
, TRUE
);
1796 wtap_block_add_string_option_format(shb_hdr
, OPT_SHB_USERAPPL
, "%s", get_appname_and_version());
1798 params
->shb_hdrs
= g_array_new(false, false, sizeof(wtap_block_t
));
1799 g_array_append_val(params
->shb_hdrs
, shb_hdr
);
1802 /* wtap_dump_init_dumper() will create a interface block if the file type
1803 * supports it and one isn't created already, but since we have the
1804 * option of including the interface name, create it ourself.
1806 * (XXX: IDBs should be optional for wtap_dump_init_dumper(), e.g. if
1807 * the encap type is WTAP_ENCAP_SYSTEMD_JOURNAL, which doesn't use
1808 * interfaces. But it's not, so always create it here.)
1810 if (wtap_file_type_subtype_supports_block(file_type_subtype
, WTAP_BLOCK_IF_ID_AND_INFO
) != BLOCK_NOT_SUPPORTED
) {
1811 int_data
= wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO
);
1812 int_data_mand
= (wtapng_if_descr_mandatory_t
*)wtap_block_get_mandatory_data(int_data
);
1814 int_data_mand
->wtap_encap
= params
->encap
;
1815 int_data_mand
->time_units_per_second
= 1000000000;
1816 int_data_mand
->snap_len
= params
->snaplen
;
1818 if (interface_name
!= NULL
) {
1819 wtap_block_add_string_option(int_data
, OPT_IDB_NAME
, interface_name
, strlen(interface_name
));
1821 wtap_block_add_string_option(int_data
, OPT_IDB_NAME
, "Fake IF, text2pcap", strlen("Fake IF, text2pcap"));
1823 if (params
->tsprec
>= 0 && params
->tsprec
<= WS_TSPREC_MAX
) {
1825 * This is a valid time precision.
1829 * Compute 10^{params->tsprec}.
1831 int_data_mand
->time_units_per_second
= 1;
1832 for (int i
= 0; i
< params
->tsprec
; i
++)
1833 int_data_mand
->time_units_per_second
*= 10;
1835 if (params
->tsprec
!= WTAP_TSPREC_USEC
) {
1837 * Microsecond precision is the default, so we only
1838 * add an option if the precision isn't microsecond
1841 wtap_block_add_uint8_option(int_data
, OPT_IDB_TSRESOL
, params
->tsprec
);
1845 * Either WTAP_TSPREC_PER_PACKET, WTAP_TSPREC_UNKNOWN,
1846 * or not a valid precision.
1850 ws_assert_not_reached();
1853 params
->idb_inf
= g_new(wtapng_iface_descriptions_t
,1);
1854 params
->idb_inf
->interface_data
= g_array_new(false, false, sizeof(wtap_block_t
));
1855 g_array_append_val(params
->idb_inf
->interface_data
, int_data
);
1859 return EXIT_SUCCESS
;