int main() { init_sender(); init_processor(); start_collector(); return EXIT_SUCCESS; }
int rstsys(void) { fflush(stdout);fflush(stderr); init_processor(); if(usecmmu) cmmu_init(); return 0; }
int main(int argc, char** argv) { /* options */ int opt_disasm = 0, opt_regdump = 0, opt_interactive = 0; /* the architectural state of the CPU */ processor_t p; /* parse the command-line args */ int c; while ((c = getopt(argc, argv, "drit")) != -1) { switch (c) { case 'd': opt_disasm = 1; break; case 'r': opt_regdump = 1; break; case 'i': opt_interactive = 1; break; case 't': opt_interactive = 2; break; default: fprintf(stderr, "Bad option %c\n", c); return -1; } } /* make sure we got an executable filename on the command line */ if (argc <= optind) { fprintf(stderr, "Give me an executable file to run!\n"); return -1; } /* load the executable into memory */ load_program(init_mem(), MEM_SIZE, argv[optind], opt_disasm); /* if we're just disassembling, exit here */ if (opt_disasm) { return 0; } /* initialize the CPU */ init_processor(&p); /* simulate forever! */ while (1) { execute_one_inst(&p, opt_interactive, opt_regdump); } return 0; }
void sniff(char *in_if, char *out_if, char *in_file, char *out_file, TableStateMachine *machine, int num_workers, int no_report, int batch) { pcap_t *hpcap[2]; char errbuf[PCAP_ERRBUF_SIZE]; char *device_in = NULL, *device_out = NULL; pcap_if_t *devices, *next_device; int res; struct bpf_program bpf; ProcessorData *processor; int linktype[2], linkHdrLen, i; char *mode; memset(errbuf, 0, PCAP_ERRBUF_SIZE); if (in_if || out_if) { // Find available interfaces if (pcap_findalldevs(&devices, errbuf)) { fprintf(stderr, "[Sniffer] ERROR: Cannot find network interface (pcap_findalldevs error: %s)\n", errbuf); exit(1); } // Find requested interface next_device = devices; while (next_device) { printf("[Sniffer] Found network interface: %s\n", next_device->name); if (in_if && (strcmp(in_if, next_device->name) == 0)) { device_in = in_if; } if (out_if && (strcmp(out_if, next_device->name) == 0)) { device_out = out_if; } next_device = next_device->next; } pcap_freealldevs(devices); if (in_if && (strcmp(in_if, STR_ANY) == 0)) { device_in = STR_ANY; } if (in_if && !device_in) { fprintf(stderr, "[Sniffer] ERROR: Cannot find input network interface\n"); exit(1); } if (out_if && !device_out) { fprintf(stderr, "[Sniffer] ERROR: Cannot find output network interface\n"); exit(1); } } if (in_if) { printf("[Sniffer] Sniffer is capturing from device: %s\n", device_in); } else { printf("[Sniffer] Sniffer is reading packets from file: %s\n", in_file); } if (out_if) { printf("[Sniffer] Packets are sent on device: %s\n", device_out); } else { printf("[Sniffer] Packets written to file: %s\n", out_file); } if (in_if) { hpcap[0] = pcap_create(device_in, errbuf); } else { hpcap[0] = pcap_open_offline(in_file, errbuf); } if (out_if) { hpcap[1] = pcap_create(device_out, errbuf); } else { //hpcap[1] = pcap_dump_fopen(out_file, errbuf); } for (i = 0; i < 2; i++) { mode = (i == 0) ? "input" : "output"; // Check pcap handle if (!hpcap[i]) { fprintf(stderr, "[Sniffer] ERROR: Cannot create %s pcap handle (pcap_create/pcap_open_offline error: %s)\n", mode ,errbuf); exit(1); } if (in_if && i == 0) { // Set promiscuous mode pcap_set_promisc(hpcap[0], 1); } // Activate PCAP if ((in_if && i == 0) || (out_if && i == 1)) { res = pcap_activate(hpcap[i]); switch (res) { case 0: // Success break; case PCAP_WARNING_PROMISC_NOTSUP: fprintf(stderr, "[Sniffer] WARNING: Promiscuous mode is not supported\n"); exit(1); break; case PCAP_WARNING: fprintf(stderr, "[Sniffer] WARNING: Unknown (%s)\n", pcap_geterr(hpcap[i])); exit(1); break; case PCAP_ERROR_NO_SUCH_DEVICE: fprintf(stderr, "[Sniffer] ERROR: Device not found\n"); exit(1); break; case PCAP_ERROR_PERM_DENIED: fprintf(stderr, "[Sniffer] ERROR: Permission denied\n"); exit(1); break; case PCAP_ERROR_PROMISC_PERM_DENIED: fprintf(stderr, "[Sniffer] ERROR: Permission denied for promiscuous mode\n"); exit(1); break; case PCAP_ERROR_RFMON_NOTSUP: fprintf(stderr, "[Sniffer] ERROR: Monitor mode is not supported\n"); exit(1); break; case PCAP_ERROR_IFACE_NOT_UP: fprintf(stderr, "[Sniffer] ERROR: Interface %s is not available\n", (i == 0) ? device_in : device_out); exit(1); break; default: fprintf(stderr, "[Sniffer] ERROR: Unknown (%s)\n", pcap_geterr(hpcap[i])); exit(1); break; } } if (i == 0) { if (in_if) { // Set capture direction (ingress only) res = pcap_setdirection(hpcap[0], PCAP_D_IN); if (res) { fprintf(stderr, "[Sniffer] ERROR: Cannot set capture direction (pcap_setdirection error: %s, return value: %d)\n", pcap_geterr(hpcap[0]), res); exit(1); } } // Compile PCAP filter (IP packets) res = pcap_compile(hpcap[0], &bpf, STR_FILTER, 0, PCAP_NETMASK_UNKNOWN); if (res) { fprintf(stderr, "[Sniffer] ERROR: Cannot compile packet filter (pcap_compile error: %s)\n", pcap_geterr(hpcap[0])); exit(1); } res = pcap_setfilter(hpcap[0], &bpf); if (res) { fprintf(stderr, "[Sniffer] ERROR: Cannot set packet filter (pcap_setfilter error: %s, return value: %d)\n", pcap_geterr(hpcap[0]), res); exit(1); } pcap_freecode(&bpf); } // Find data link type if ((linktype[i] = pcap_datalink(hpcap[i])) < 0) { fprintf(stderr, "[Sniffer] Cannot determine data link type (pcap_datalink error: %s)\n", pcap_geterr(hpcap[i])); exit(1); } if (i == 1 && linktype[0] != linktype[1]) { fprintf(stderr, "[Sniffer] Incompatible link types (input=%d, output=%d)\n", linktype[0], linktype[1]); exit(1); } } linkHdrLen = 0; switch (linktype[0]) { case DLT_NULL: linkHdrLen = 4; break; case DLT_EN10MB: linkHdrLen = 14; break; case DLT_SLIP: case DLT_PPP: linkHdrLen = 24; break; default: fprintf(stderr, "[Sniffer] Unsupported data link type: %d\n", linktype[0]); exit(1); } // Prepare processor processor = init_processor(machine, hpcap[0], hpcap[1], linkHdrLen, num_workers, no_report, batch); _global_processor = processor; // Set signal handler signal(SIGINT, stop); signal(SIGTERM, stop); signal(SIGQUIT, stop); // Run sniffer gettimeofday(&(processor->start), NULL); printf("[Sniffer] Sniffer is running (input: %s, outout: %s)...\n", in_if, out_if); res = pcap_loop(hpcap[0], -1, process_packet, (unsigned char *)(processor)); stop(res); }