void printFlow(std::multimap<T, flow> data, const char* filename) { FILE* pFile = fopen(filename, "w"); for (typename std::multimap<T, flow>::reverse_iterator ii = data.rbegin(); ii != data.rend(); ++ii) { printFlow(&ii->second, pFile); } fclose(pFile); }
static void node_print_known_proto_walker(const void *node, ndpi_VISIT which, int depth, void *user_data) { struct ndpi_flow *flow = *(struct ndpi_flow**)node; if(flow->detected_protocol == 0 /* UNKNOWN */) return; if((which == ndpi_preorder) || (which == ndpi_leaf)) /* Avoid walking the same node multiple times */ printFlow(flow); }
int main () { FINISH = clock () + (CLOCKS_PER_SEC * 1800) / 1000; freopen ("omax.in", "rt", stdin); freopen ("omax.out", "wt", stdout); #ifdef _DBG_ freopen ("omax.err", "wt", stderr); #endif scanf ("%d", &X); Y = X; n = X + Y + 2; for (int i = 0; i < X; i++) add_edge (0, i + 1, 1); for (int i = 0; i < Y; i++) add_edge (i + X + 1, n - 1, 1); for (int i = 0; i < X; i++) { int c; scanf ("%d", &c); for (int j = 0; j < c; j++) { int b; scanf ("%d", &b); add_edge (i + 1, b + X, 1); } } int flow = getFlow (); if (flow < X) noQuit (); #ifdef _DBG_ printf ("%d\n", flow); #endif memset (vis, 0, sizeof vis); for (int i = 0; i < n; i++) { dfs (i); } printFlow (); return 0; }
int main(int argc, char *argv[]) { int opt; char *filename; if (argc < 3) { std::cerr << "Usage: " << argv[0] << "[-f filename]" << std::endl; return (EXIT_FAILURE); } uint8_t aggr, sort, prefix = 0; while ((opt = getopt(argc, argv, "f:a:s:")) != -1) { std::string arg(optarg); switch (opt) { // filename case 'f': filename = optarg; break; // aggregation type case 'a': for (aggr = SRC_IP; aggr < MAX_AGGR; ++aggr) { if (arg.compare(AGGR_ARGS[aggr]) == 0) { if (aggr == SRC_IPV4 || aggr == SRC_IPV6 || aggr == DST_IPV4 || aggr == DST_IPV6) { std::vector<std::string> tokens = split(arg, '/'); if (tokens[0].size() == AGGR_ARGS[aggr].size() && tokens.size() == 2) { if (aggr == SRC_IPV6 || aggr == DST_IPV6) { prefix = atoi(tokens[1].c_str()); if (prefix > 1 && prefix <= 128) break; } else if (aggr == SRC_IPV4 || aggr == DST_IPV4) { prefix = atoi(tokens[1].c_str()); if (prefix > 1 && prefix <= 32) break; } } } else { if (arg.size() == AGGR_ARGS[aggr].size()) break; } } } if (aggr == MAX_AGGR) { std::cerr << "Usage: " << argv[0] << "[-f filename]" << std::endl; return (EXIT_FAILURE); } break; // sort type case 's': for (sort = PACKETS; sort < MAX_SORT; ++sort) { if (compare(arg, SORT_ARGS[sort]) == 0) break; } if (sort == MAX_SORT) { std::cerr << "Usage: " << argv[0] << "[-f filename]" << std::endl; return (EXIT_FAILURE); } break; // other options default: std::cerr << "Usage: " << argv[0] << "[-f filename]" << std::endl; return (EXIT_FAILURE); } } // reading file DIR* dir = opendir(filename); if (dir != NULL) { struct dirent* dp; while ((dp = readdir(dir)) != NULL) { std::string fn(dp->d_name); std::string type(".bin"); size_t pos = fn.find(type); if (pos != std::string::npos && pos + type.length() == fn.length()) { // also check if ext is really at the end std::cout << dp->d_name << std::endl; } } } closedir(dir); std::ifstream f(filename, std::ifstream::in | std::ifstream::binary); if (!f) { std::cerr << "Unable to open file (filename: " << filename << ")." << std::endl; return EXIT_FAILURE; } std::map<in6_addr, flow> ipdata; std::map<uint16_t, flow> portdata; switch (aggr) { case SRC_IP: { while (f.good()) { flow fl; f.read(reinterpret_cast<char*> (&fl), sizeof (flow)); fl.bytes = __builtin_bswap64(fl.bytes); fl.packets = __builtin_bswap64(fl.packets); std::pair < std::map<in6_addr, flow>::iterator, bool> el = ipdata.insert(std::make_pair(fl.src_addr, fl)); if (!el.second) { // element already existed el.first->second.packets += fl.packets; el.first->second.bytes += fl.bytes; } } break; } case SRC_IPV4: { while (f.good()) { flow fl; f.read(reinterpret_cast<char*> (&fl), sizeof (flow)); fl.bytes = __builtin_bswap64(fl.bytes); fl.packets = __builtin_bswap64(fl.packets); std::pair < std::map<in6_addr, flow>::iterator, bool> el = ipdata.insert(std::make_pair(getMask(fl.src_addr, prefix, IP_V4), fl)); if (!el.second) { // element already existed el.first->second.packets += fl.packets; el.first->second.bytes += fl.bytes; } } break; } case SRC_IPV6: { while (f.good()) { flow fl; f.read(reinterpret_cast<char*> (&fl), sizeof (flow)); fl.bytes = __builtin_bswap64(fl.bytes); fl.packets = __builtin_bswap64(fl.packets); std::pair < std::map<in6_addr, flow>::iterator, bool> el = ipdata.insert(std::make_pair(getMask(fl.src_addr, prefix, IP_V6), fl)); if (!el.second) { // element already existed el.first->second.packets += fl.packets; el.first->second.bytes += fl.bytes; } } break; } case DST_IP: { while (f.good()) { flow fl; f.read(reinterpret_cast<char*> (&fl), sizeof (flow)); fl.bytes = __builtin_bswap64(fl.bytes); fl.packets = __builtin_bswap64(fl.packets); std::pair < std::map<in6_addr, flow>::iterator, bool> el = ipdata.insert(std::make_pair(fl.dst_addr, fl)); if (!el.second) { // element already existed el.first->second.packets += fl.packets; el.first->second.bytes += fl.bytes; } } break; } case DST_IPV4: { while (f.good()) { flow fl; f.read(reinterpret_cast<char*> (&fl), sizeof (flow)); fl.bytes = __builtin_bswap64(fl.bytes); fl.packets = __builtin_bswap64(fl.packets); std::pair < std::map<in6_addr, flow>::iterator, bool> el = ipdata.insert(std::make_pair(getMask(fl.dst_addr, prefix, IP_V4), fl)); if (!el.second) { el.first->second.packets += fl.packets; el.first->second.bytes += fl.bytes; } } break; } case DST_IPV6: { while (f.good()) { flow fl; f.read(reinterpret_cast<char*> (&fl), sizeof (flow)); fl.bytes = __builtin_bswap64(fl.bytes); fl.packets = __builtin_bswap64(fl.packets); std::pair < std::map<in6_addr, flow>::iterator, bool> el = ipdata.insert(std::make_pair(getMask(fl.dst_addr, prefix, IP_V6), fl)); if (!el.second) { el.first->second.packets += fl.packets; el.first->second.bytes += fl.bytes; } } break; } case SRC_PORT: { while (f.good()) { flow fl; f.read(reinterpret_cast<char*> (&fl), sizeof (flow)); fl.bytes = __builtin_bswap64(fl.bytes); fl.packets = __builtin_bswap64(fl.packets); std::pair < std::map<uint16_t, flow>::iterator, bool> el = portdata.insert(std::make_pair(fl.src_port, fl)); if (!el.second) { el.first->second.packets += fl.packets; el.first->second.bytes += fl.bytes; } } break; } case DST_PORT: { while (f.good()) { flow fl; f.read(reinterpret_cast<char*> (&fl), sizeof (flow)); fl.bytes = __builtin_bswap64(fl.bytes); fl.packets = __builtin_bswap64(fl.packets); std::pair < std::map<uint16_t, flow>::iterator, bool> el = portdata.insert(std::make_pair(fl.dst_port, fl)); if (!el.second) { el.first->second.packets += fl.packets; el.first->second.bytes += fl.bytes; } } break; } } std::multimap<uint64_t, flow> sorted; switch (aggr) { case SRC_IP: case SRC_IPV4: case SRC_IPV6: case DST_IP: case DST_IPV4: case DST_IPV6: switch (sort) { case PACKETS: for (std::map<in6_addr, flow>::const_iterator ii = ipdata.begin(); ii != ipdata.end(); ++ii) sorted.insert(std::make_pair(ii->second.packets, ii->second)); break; case BYTES: for (std::map<in6_addr, flow>::const_iterator ii = ipdata.begin(); ii != ipdata.end(); ++ii) sorted.insert(std::make_pair(ii->second.bytes, ii->second)); break; } break; case SRC_PORT: case DST_PORT: switch (sort) { case PACKETS: for (std::map<uint16_t, flow>::const_iterator ii = portdata.begin(); ii != portdata.end(); ++ii) sorted.insert(std::make_pair(ii->second.packets, ii->second)); break; case BYTES: for (std::map<uint16_t, flow>::const_iterator ii = portdata.begin(); ii != portdata.end(); ++ii) sorted.insert(std::make_pair(ii->second.bytes, ii->second)); break; } break; } printFlow(sorted, "sorted.txt"); f.close(); return (EXIT_SUCCESS); }