void backtracking(node_t *currentNode, connection_t *currentSolution, connection_t *globalSolution) { //Move to the neighbors int i; for(i = 0; i < currentNode->n_neighbors; i++) { //Check if solution if(currentNode->neighbors[i] == currentSolution->destination && currentNode->n_connections[i]) { backtracking_compareSolutions(globalSolution, currentSolution); continue; } //If no more connections, skip to next neighbor if(!node_use_connection(currentNode, currentNode->neighbors[i]->name)) continue; //Move forward connection_push(currentSolution, currentNode->neighbors[i]); backtracking(currentNode->neighbors[i], currentSolution, globalSolution); //Move backwards connection_pop(currentSolution); node_free_connection(currentNode, currentNode->neighbors[i]->name); } }
/* process_packet: * Callback which processes a packet captured by libpcap. */ void process_packet(u_char *user, const struct pcap_pkthdr *hdr, const u_char *pkt) { struct tcphdr tcp; int off, len, delta; connection *C, c; struct sockaddr_storage src, dst; struct sockaddr *s, *d; uint8_t proto; s = (struct sockaddr *)&src; d = (struct sockaddr *)&dst; if (handle_link_layer(&datalink_info, pkt, hdr->caplen, &proto, &off)) return; if (layer3_find_tcp(pkt, proto, &off, s, d, &tcp)) return; len = hdr->caplen - off; /* XXX fragmented packets and other nasties. */ /* try to find the connection slot associated with this. */ C = find_connection(s, d); /* no connection at all, so we need to allocate one. */ if (!C) { log_msg(LOG_INFO, "new connection: %s", connection_string(s,d)); C = alloc_connection(); *C = connection_new(s, d); /* This might or might not be an entirely new connection (SYN flag * set). Either way we need a sequence number to start at. */ (*C)->isn = ntohl(tcp.th_seq); } /* Now we need to process this segment. */ c = *C; delta = 0;/*tcp.syn ? 1 : 0;*/ /* NB (STD0007): * SEG.LEN = the number of octets occupied by the data in the * segment (counting SYN and FIN) */ #if 0 if (tcp.syn) /* getting a new isn. */ c->isn = htonl(tcp.seq); #endif if (tcp.th_flags & TH_RST) { /* Looks like this connection is bogus, and so might be a * connection going the other way. */ log_msg(LOG_INFO, "connection reset: %s", connection_string(s, d)); connection_delete(c); *C = NULL; if ((C = find_connection(d, s))) { connection_delete(*C); *C = NULL; } return; } if (len > 0) { /* We have some data in the packet. If this data occurred after * the first data we collected for this connection, then save it * so that we can look for images. Otherwise, discard it. */ unsigned int offset; offset = ntohl(tcp.th_seq); /* Modulo 2**32 arithmetic; offset = seq - isn + delta. */ if (offset < (c->isn + delta)) offset = 0xffffffff - (c->isn + delta - offset); else offset -= c->isn + delta; if (offset > c->len + WRAPLEN) { /* Out-of-order packet. */ log_msg(LOG_INFO, "out of order packet: %s", connection_string(s, d)); } else { connection_push(c, pkt + off, offset, len); extract_media(c); } } if (tcp.th_flags & TH_FIN) { /* Connection closing; mark it as closed, but let sweep_connections * free it if appropriate. */ log_msg(LOG_INFO, "connection closing: %s, %d bytes transferred", connection_string(s, d), c->len); c->fin = 1; } /* sweep out old connections */ sweep_connections(); }