diff options
Diffstat (limited to 'randpkt_core')
-rw-r--r-- | randpkt_core/CMakeLists.txt | 10 | ||||
-rw-r--r-- | randpkt_core/randpkt_core.c | 115 | ||||
-rw-r--r-- | randpkt_core/randpkt_core.h | 21 |
3 files changed, 86 insertions, 60 deletions
diff --git a/randpkt_core/CMakeLists.txt b/randpkt_core/CMakeLists.txt index 7f17f3dc37..61c50f47f9 100644 --- a/randpkt_core/CMakeLists.txt +++ b/randpkt_core/CMakeLists.txt @@ -27,12 +27,16 @@ add_library(randpkt_core STATIC set_target_properties(randpkt_core PROPERTIES LINK_FLAGS "${WS_LINK_FLAGS}" FOLDER "Libs") +if(MSVC) + set_target_properties(randpkt_core PROPERTIES LINK_FLAGS_DEBUG "${WS_MSVC_DEBUG_LINK_FLAGS}") +endif() + +target_link_libraries(randpkt_core PUBLIC ui) CHECKAPI( NAME randpkt_core-base SWITCHES - -g deprecated-gtk SOURCES ${RANDPKT_CORE_SRC} ${RANDPKT_CORE_HEADERS} @@ -41,14 +45,14 @@ CHECKAPI( NAME randpkt_core-todo SWITCHES - -M -g deprecated-gtk-todo + -M SOURCES ${RANDPKT_CORE_SRC} ${RANDPKT_CORE_HEADERS} ) # -# Editor modelines - http://www.wireshark.org/tools/modelines.html +# Editor modelines - https://www.wireshark.org/tools/modelines.html # # Local variables: # c-basic-offset: 8 diff --git a/randpkt_core/randpkt_core.c b/randpkt_core/randpkt_core.c index 628b941c23..ebb875fa4c 100644 --- a/randpkt_core/randpkt_core.c +++ b/randpkt_core/randpkt_core.c @@ -10,24 +10,24 @@ */ #include <config.h> +#define WS_LOG_DOMAIN "randpkt" #include "randpkt_core.h" #include <time.h> -#include <errno.h> #include <stdlib.h> #include <string.h> +#include <wsutil/array.h> #include <wsutil/file_util.h> +#include <wsutil/wslog.h> #include <wiretap/wtap_opttypes.h> #include "ui/failure_message.h" -#define array_length(x) (sizeof x / sizeof x[0]) - #define INVALID_LEN 1 #define WRITE_ERROR 2 -GRand *pkt_rand = NULL; +GRand *pkt_rand; /* Types of produceable packets */ enum { @@ -58,7 +58,7 @@ enum { }; /* Ethernet, indicating ARP */ -guint8 pkt_arp[] = { +uint8_t pkt_arp[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x32, 0x25, 0x0f, 0xff, @@ -66,7 +66,7 @@ guint8 pkt_arp[] = { }; /* Ethernet+IP+UDP, indicating DNS */ -guint8 pkt_dns[] = { +uint8_t pkt_dns[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -84,7 +84,7 @@ guint8 pkt_dns[] = { }; /* Ethernet+IP, indicating ICMP */ -guint8 pkt_icmp[] = { +uint8_t pkt_icmp[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -98,7 +98,7 @@ guint8 pkt_icmp[] = { }; /* Ethernet, indicating IP */ -guint8 pkt_ip[] = { +uint8_t pkt_ip[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -106,7 +106,7 @@ guint8 pkt_ip[] = { }; /* Ethernet, indicating IPv6 */ -guint8 pkt_ipv6[] = { +uint8_t pkt_ipv6[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -114,7 +114,7 @@ guint8 pkt_ipv6[] = { }; /* TR, indicating LLC */ -guint8 pkt_llc[] = { +uint8_t pkt_llc[] = { 0x10, 0x40, 0x68, 0x00, 0x19, 0x69, 0x95, 0x8b, 0x00, 0x01, 0xfa, 0x68, @@ -122,7 +122,7 @@ guint8 pkt_llc[] = { }; /* Ethernet, indicating WiMAX M2M */ -guint8 pkt_m2m[] = { +uint8_t pkt_m2m[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x32, 0x25, 0x0f, 0xff, @@ -130,7 +130,7 @@ guint8 pkt_m2m[] = { }; /* Ethernet+IP+UDP, indicating NBNS */ -guint8 pkt_nbns[] = { +uint8_t pkt_nbns[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -148,7 +148,7 @@ guint8 pkt_nbns[] = { }; /* Ethernet+IP+UDP, indicating syslog */ -guint8 pkt_syslog[] = { +uint8_t pkt_syslog[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -166,7 +166,7 @@ guint8 pkt_syslog[] = { }; /* TR+LLC+IP, indicating TCP */ -guint8 pkt_tcp[] = { +uint8_t pkt_tcp[] = { 0x10, 0x40, 0x68, 0x00, 0x19, 0x69, 0x95, 0x8b, 0x00, 0x01, 0xfa, 0x68, @@ -183,7 +183,7 @@ guint8 pkt_tcp[] = { }; /* Ethernet+IP, indicating UDP */ -guint8 pkt_udp[] = { +uint8_t pkt_udp[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -197,7 +197,7 @@ guint8 pkt_udp[] = { }; /* Ethernet+IP+UDP, indicating BVLC */ -guint8 pkt_bvlc[] = { +uint8_t pkt_bvlc[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -214,7 +214,7 @@ guint8 pkt_bvlc[] = { }; /* TR+LLC+IPX, indicating NCP, with NCP Type == 0x2222 */ -guint8 pkt_ncp2222[] = { +uint8_t pkt_ncp2222[] = { 0x10, 0x40, 0x00, 0x00, 0xf6, 0x7c, 0x9b, 0x70, 0x68, 0x00, 0x19, 0x69, @@ -231,7 +231,7 @@ guint8 pkt_ncp2222[] = { }; /* Ethernet+IP+TCP, indicating GIOP */ -guint8 pkt_giop[] = { +uint8_t pkt_giop[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -261,7 +261,7 @@ guint8 pkt_giop[] = { }; /* Ethernet+IP+TCP, indicating BGP */ -guint8 pkt_bgp[] = { +uint8_t pkt_bgp[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, @@ -289,7 +289,7 @@ guint8 pkt_bgp[] = { }; /* Ethernet+IP+TCP, indicating TDS NetLib */ -guint8 pkt_tds[] = { +uint8_t pkt_tds[] = { 0x00, 0x50, 0x8b, 0x0d, 0x7a, 0xed, 0x00, 0x08, 0xa3, 0x98, 0x39, 0x81, @@ -309,7 +309,7 @@ guint8 pkt_tds[] = { }; /* Ethernet+IP, indicating SCTP */ -guint8 pkt_sctp[] = { +uint8_t pkt_sctp[] = { 0x00, 0xa0, 0x80, 0x00, 0x5e, 0x46, 0x08, 0x00, 0x03, 0x4a, 0x00, 0x35, @@ -324,7 +324,7 @@ guint8 pkt_sctp[] = { /* Ethernet+IP+SCTP, indicating MEGACO */ -guint8 pkt_megaco[] = { +uint8_t pkt_megaco[] = { 0x00, 0xa0, 0x80, 0x00, 0x5e, 0x46, 0x08, 0x00, 0x03, 0x4a, 0x00, 0x35, @@ -533,7 +533,7 @@ static randpkt_example examples[] = { }; -guint randpkt_example_count(void) +unsigned randpkt_example_count(void) { return array_length(examples); } @@ -555,19 +555,19 @@ randpkt_example* randpkt_find_example(int type) return NULL; } -void randpkt_loop(randpkt_example* example, guint64 produce_count) +void randpkt_loop(randpkt_example* example, uint64_t produce_count, uint64_t packet_delay_ms) { - guint i, j; + unsigned i, j; int err; - guint len_random; - guint len_this_pkt; - gchar* err_info; + unsigned len_random; + unsigned len_this_pkt; + char* err_info; union wtap_pseudo_header* ps_header; - guint8* buffer; + uint8_t* buffer; wtap_rec* rec; rec = g_new0(wtap_rec, 1); - buffer = (guint8*)g_malloc0(65536); + buffer = (uint8_t*)g_malloc0(65536); rec->rec_type = REC_TYPE_PACKET; rec->presence_flags = WTAP_HAS_TS; @@ -593,13 +593,20 @@ void randpkt_loop(randpkt_example* example, guint64 produce_count) } len_this_pkt = example->sample_length + len_random; + if (len_this_pkt > WTAP_MAX_PACKET_SIZE_STANDARD) { + /* + * Wiretap will fail when trying to read packets + * bigger than WTAP_MAX_PACKET_SIZE_STANDARD. + */ + len_this_pkt = WTAP_MAX_PACKET_SIZE_STANDARD; + } rec->rec_header.packet_header.caplen = len_this_pkt; rec->rec_header.packet_header.len = len_this_pkt; rec->ts.secs = i; /* just for variety */ for (j = example->pseudo_length; j < (int) sizeof(*ps_header); j++) { - ((guint8*)ps_header)[j] = g_rand_int_range(pkt_rand, 0, 0x100); + ((uint8_t*)ps_header)[j] = g_rand_int_range(pkt_rand, 0, 0x100); } for (j = example->sample_length; j < len_this_pkt; j++) { @@ -613,9 +620,17 @@ void randpkt_loop(randpkt_example* example, guint64 produce_count) } if (!wtap_dump(example->dump, rec, buffer, &err, &err_info)) { - cfile_write_failure_message("randpkt", NULL, + cfile_write_failure_message(NULL, example->filename, err, err_info, 0, - WTAP_FILE_TYPE_SUBTYPE_PCAP); + wtap_dump_file_type_subtype(example->dump)); + } + if (packet_delay_ms) { + g_usleep(1000 * (unsigned long)packet_delay_ms); + if (!wtap_dump_flush(example->dump, &err)) { + cfile_write_failure_message(NULL, + example->filename, err, NULL, 0, + wtap_dump_file_type_subtype(example->dump)); + } } } @@ -623,14 +638,15 @@ void randpkt_loop(randpkt_example* example, guint64 produce_count) g_free(buffer); } -gboolean randpkt_example_close(randpkt_example* example) +bool randpkt_example_close(randpkt_example* example) { int err; - gboolean ok = TRUE; + char *err_info; + bool ok = true; - if (!wtap_dump_close(example->dump, &err)) { - cfile_close_failure_message(example->filename, err); - ok = FALSE; + if (!wtap_dump_close(example->dump, NULL, &err, &err_info)) { + cfile_close_failure_message(example->filename, err, err_info); + ok = false; } if (pkt_rand != NULL) { @@ -641,27 +657,32 @@ gboolean randpkt_example_close(randpkt_example* example) return ok; } -int randpkt_example_init(randpkt_example* example, char* produce_filename, int produce_max_bytes) +int randpkt_example_init(randpkt_example* example, char* produce_filename, int produce_max_bytes, int file_type_subtype) { int err; + char *err_info; if (pkt_rand == NULL) { pkt_rand = g_rand_new(); } + const wtap_dump_params params = { + .encap = example->sample_wtap_encap, + .snaplen = produce_max_bytes, + }; if (strcmp(produce_filename, "-") == 0) { /* Write to the standard output. */ - example->dump = wtap_dump_open_stdout(WTAP_FILE_TYPE_SUBTYPE_PCAP, - example->sample_wtap_encap, produce_max_bytes, FALSE /* compressed */, &err); + example->dump = wtap_dump_open_stdout(file_type_subtype, + WTAP_UNCOMPRESSED, ¶ms, &err, &err_info); example->filename = "the standard output"; } else { - example->dump = wtap_dump_open(produce_filename, WTAP_FILE_TYPE_SUBTYPE_PCAP, - example->sample_wtap_encap, produce_max_bytes, FALSE /* compressed */, &err); + example->dump = wtap_dump_open(produce_filename, file_type_subtype, + WTAP_UNCOMPRESSED, ¶ms, &err, &err_info); example->filename = produce_filename; } if (!example->dump) { - cfile_dump_open_failure_message("randpkt", produce_filename, - err, WTAP_FILE_TYPE_SUBTYPE_PCAP); + cfile_dump_open_failure_message(produce_filename, + err, err_info, file_type_subtype); return WRITE_ERROR; } @@ -696,7 +717,7 @@ int randpkt_parse_type(char *string) } /* Complain */ - g_error("randpkt: Type %s not known.\n", string); + ws_error("randpkt: Type %s not known.\n", string); return -1; } @@ -714,7 +735,7 @@ void randpkt_example_list(char*** abbrev_list, char*** longname_list) } /* - * Editor modelines - http://www.wireshark.org/tools/modelines.html + * Editor modelines - https://www.wireshark.org/tools/modelines.html * * Local variables: * c-basic-offset: 8 diff --git a/randpkt_core/randpkt_core.h b/randpkt_core/randpkt_core.h index 53dc042687..46f056d32d 100644 --- a/randpkt_core/randpkt_core.h +++ b/randpkt_core/randpkt_core.h @@ -1,4 +1,5 @@ -/* +/** @file + * * randpkt_core.h * --------- * Creates random packet traces. Useful for debugging sniffers by testing @@ -20,18 +21,18 @@ typedef struct { const char* longname; int produceable_type; int sample_wtap_encap; - guint8* sample_buffer; + uint8_t* sample_buffer; int sample_length; - guint8* pseudo_buffer; - guint pseudo_length; + uint8_t* pseudo_buffer; + unsigned pseudo_length; wtap_dumper* dump; const char* filename; - guint produce_max_bytes; + unsigned produce_max_bytes; } randpkt_example; /* Return the number of active examples */ -guint randpkt_example_count(void); +unsigned randpkt_example_count(void); /* Return the list of the active examples */ void randpkt_example_list(char*** abbrev_list, char*** longname_list); @@ -43,18 +44,18 @@ int randpkt_parse_type(char *string); randpkt_example* randpkt_find_example(int type); /* Init a new example */ -int randpkt_example_init(randpkt_example* example, char* produce_filename, int produce_max_bytes); +int randpkt_example_init(randpkt_example* example, char* produce_filename, int produce_max_bytes, int file_type_subtype); /* Loop the packet generation */ -void randpkt_loop(randpkt_example* example, guint64 produce_count); +void randpkt_loop(randpkt_example* example, uint64_t produce_count, uint64_t packet_delay_ms); /* Close the current example */ -gboolean randpkt_example_close(randpkt_example* example); +bool randpkt_example_close(randpkt_example* example); #endif /* - * Editor modelines - http://www.wireshark.org/tools/modelines.html + * Editor modelines - https://www.wireshark.org/tools/modelines.html * * Local variables: * c-basic-offset: 8 |