int perftest(int argc, char *argv[]) { struct PerfTest perftest[1]; struct ZoneFileParser *parser; struct Catalog *db; size_t i; perftest->loop_count = 10000000; /* * Create a pseudo-network subsystem for generating packets */ perftest->server.parent = perftest; perftest->server.adapter = adapter_create( perftest_alloc_packet, perftest_server_to_client_response, &perftest->server); adapter_add_ipv4(perftest->server.adapter, 0xC0A80101, 0xFFFFffff); /* create a catalog/database, this is where all the parsed zonefile * records will be put */ perftest->db = catalog_create(); perftest->thread->catalog = perftest->db; db = perftest->db; /* * Parse a sample zone */ parser = zonefile_begin( example_origin, /* origin */ 60, /* TTL */ 10000, /* filesize */ "<perftest>", /* filename */ zonefile_load, /* callback */ db, /* callback data */ 0 ); zonefile_set_singlestep(parser); for (i=0; perftest_zone[i]; i++) { zonefile_parse(parser, (const unsigned char*)perftest_zone[i], strlen(perftest_zone[i]) ); } zonefile_end(parser); /* * Send packets. This creates one thread per CPU processing requests. */ { unsigned threads_desired = pixie_cpu_get_count(); uint64_t start, stop; double requests_per_second; fprintf(stderr, "running %u threads\n", threads_desired); start = pixie_gettime(); for (i=0; i<threads_desired; i++) { __sync_fetch_and_add(&threads_running, 1); pixie_begin_thread((void(*)(void*))run_perf, 0, perftest); } while (threads_running) pixie_usleep(1000); stop = pixie_gettime(); requests_per_second = 1000000.0 * (1.0 * threads_desired * perftest->loop_count) / (stop - start); fprintf(stderr, "queries = %u\n", (unsigned)(threads_desired * perftest->loop_count)); fprintf(stderr, "seconds = %5.3f\n", (stop - start)/1000000.0); fprintf(stderr, "queries/second = %5.3f\n", requests_per_second); } exit(1); return 0; }
int pcap2zone(int argc, char *argv[]) { struct Pcap2Zone pcap2zone[1]; struct Catalog *catalog; int i; /* * Create a catalog/database */ catalog = catalog_create(); pcap2zone->db = catalog; /* * Initialize it with a pseudo-SOA record for the root zone */ { static const struct DomainPointer root = {(const unsigned char*)"\0",1}; struct ZoneFileParser *parser; parser = zonefile_begin( root, /* origin */ 60, /* TTL */ 10000, /* filesize */ "<pcap2zone>", /* filename */ zonefile_load, /* callback */ catalog /* callback data */ ); LOAD("$TTL 60\r\n" "@ IN SOA ns hostmaster (\r\n" " 2003080800 ; sn = serial number\r\n" " 172800 ; ref = refresh = 2d\r\n" " 15m ; ret = update retry = 15m\r\n" " 1209600 ; ex = expiry = 2w\r\n" " 1H ; nx = nxdomain ttl = 1h\r\n" " )\r\n", parser); zonefile_end(parser); } for (i=2; i<argc; i++) { const char *filename = argv[i]; struct Tracker tracker[1]; struct PcapFile *p; //uint64_t filesize; memset(tracker, 0, sizeof(tracker[0])); //filesize = tracker_get_filesize(tracker, filename); p = pcapfile_openread(filename); if (p == NULL) { perror(filename); continue; } for (;;) { unsigned char buf[65536]; int x; unsigned secs; unsigned usecs; unsigned original_length; unsigned bytes_read; x = pcapfile_readframe( p, &secs, &usecs, &original_length, &bytes_read, buf, sizeof(buf) ); if (x <= 0) break; { struct PreprocessedInfo info; unsigned x; x = preprocess_frame( buf, bytes_read, 1, &info); if (x && info.found == FOUND_DNS && info.port_src == 53) grab_dns_response(catalog, buf, info.found_offset, bytes_read); } tracker_report(tracker, bytes_read); } pcapfile_close(p); } return Success; }
static void conf_zonefiles_parse_thread(void *v) { struct XParseThread *p = (struct XParseThread *)v; struct Catalog *db = p->db_load; struct Configuration *cfg = p->cfg; struct ZoneFileParser *parser; static const struct DomainPointer root = {(const unsigned char*)"\0",1}; size_t directory_index; size_t file_index; size_t current_index; fflush(stderr); fflush(stdout); /* * Start the parsing */ parser = zonefile_begin( root, 60, 128, cfg->options.directory, zonefile_load, db, cfg->insertion_threads ); /* * Find the starting point. This converts the single * integer number into a [directory, file] index pair. */ current_index = 0; for (directory_index = 0; directory_index < cfg->zonedirs_length; directory_index++) { struct Cfg_ZoneDir *zonedir = cfg->zonedirs[directory_index]; current_index += zonedir->file_count; if (current_index >= p->start_index) break; } file_index = current_index - p->start_index; /* * 'for all zonefiles in this directory...' */ if (directory_index < cfg->zonedirs_length) while (current_index < p->end_index) { const char *filename; FILE *fp; int err; uint64_t filesize; struct Cfg_ZoneDir *zonedir; /* If we've gone past the end of this directory, * then start parsing the next directory */ zonedir = cfg->zonedirs[directory_index]; if (file_index >= zonedir->file_count) { file_index = 0; directory_index++; if (directory_index >= cfg->zonedirs_length) break; zonedir = cfg->zonedirs[directory_index]; } filename = zonedir->files[file_index].filename; filesize = zonedir->files[file_index].size; current_index++; file_index++; /* * Open the file */ fflush(stdout); fflush(stderr); err = fopen_s(&fp, filename, "rb"); if (err || fp == NULL) { perror(filename); p->status = Failure; return; } p->total_bytes += filesize; /* * Set parameters */ zonefile_begin_again( parser, root, /* . domain origin */ 60, /* one minute ttl */ filesize, filename); /* * Continue parsing the file until end, reporting progress as we * go along */ for (;;) { unsigned char buf[65536]; size_t bytes_read; bytes_read = fread((char*)buf, 1, sizeof(buf), fp); if (bytes_read == 0) break; zonefile_parse( parser, buf, bytes_read ); } fclose(fp); } /* We are done parsing the directories. Now let's parse * the individual zonefiles */ while (current_index < p->end_index) { const char *filename; FILE *fp; int err; uint64_t filesize; struct Cfg_Zone *zone; if (file_index >= cfg->zones_length) break; zone = cfg->zones[file_index]; filename = zone->file; filesize = zone->file_size; current_index++; file_index++; /* * Open the file */ fflush(stdout); fflush(stderr); err = fopen_s(&fp, filename, "rb"); if (err || fp == NULL) { perror(filename); p->status = Failure; return; } p->total_bytes += filesize; /* * Set parameters */ zonefile_begin_again( parser, root, /* . domain origin */ 60, /* one minute ttl */ filesize, filename); /* * Continue parsing the file until end, reporting progress as we * go along */ for (;;) { unsigned char buf[65536]; size_t bytes_read; bytes_read = fread((char*)buf, 1, sizeof(buf), fp); if (bytes_read == 0) break; zonefile_parse( parser, buf, bytes_read ); } fclose(fp); } if (zonefile_end(parser) == Success) { p->status = Success; } else { fprintf(stderr, "%s: failure\n", ""); p->status = Failure; } }