void schedule_and_send(int link){ if(links[link].timeout_occurred == false) return; //printf("QUEUE SIZES for link %d: %d(a) %d(s) %d(f)\n", link, queue_nitems(links[link].ack_sender), queue_nitems(links[link].sender), queue_nitems(links[link].forwarding_queue)); //Send ack if it is there -- TOP PRIORITY if(queue_nitems(links[link].ack_sender) > 0){ send_acks(link); return; } //If ack queue is empty send packets from sender queue OR forwarding queue in priority basis if(queue_nitems(links[link].forwarding_queue) == 0 && queue_nitems(links[link].sender) > 0){ send_frames(link); } else if (queue_nitems(links[link].forwarding_queue) > 0 && queue_nitems(links[link].sender) == 0){ forward_frames(link); } else if (queue_nitems(links[link].forwarding_queue) == 0 && queue_nitems(links[link].sender) == 0){ return; } else { links[link].packet_to_send = (links[link].packet_to_send + 1) % (PRIORITY + 1); //printf("packet to send is ----------------------------------------- %d\n", links[link].packet_to_send); //if(links[link].packet_to_send == PRIORITY) if(queue_nitems(links[link].forwarding_queue) > queue_nitems(links[link].sender)) //send forwarding forward_frames(link); else //send normal send_frames(link); } }
static int process_haves_and_send_acks(struct upload_pack_data *data) { struct oid_array common = OID_ARRAY_INIT; struct strbuf response = STRBUF_INIT; int ret = 0; process_haves(&data->haves, &common); if (data->done) { ret = 1; } else if (send_acks(&common, &response)) { packet_buf_delim(&response); ret = 1; } else { /* Add Flush */ packet_buf_flush(&response); ret = 0; } /* Send response */ write_or_die(1, response.buf, response.len); strbuf_release(&response); oid_array_clear(&data->haves); oid_array_clear(&common); return ret; }
int main(int argc, char *argv[]) { unsigned int port; int opt; unsigned long delay; pid_t pid; char *interface, *endptr; char libnet_ebuf[LIBNET_ERRBUF_SIZE]; pcap_t *pcap; char pcap_expr[100]; libnet_t *libnet; u_int32_t target_addr, source_addr; struct sigaction sa; interface = NULL; while ((opt = getopt(argc, argv, "i:")) != -1) { switch (opt) { case 'i': interface = optarg; break; default: usage(); break; } } argc -= optind; argv += optind; if (argc != 3) { usage(); } port = strtoul(argv[1], &endptr, 10); if ((*endptr != '\0') || port < 1 || port > 65535) { errx(1, "Invalid port: %s", argv[1]); } errno = 0; delay = strtoul(argv[2], &endptr, 10); if (*endptr != '\0') { errx(1, "invalid delay: %s", argv[2]); } else if ((errno == ERANGE && (delay == LONG_MAX)) || (errno != 0 && delay == 0)) { errx(1, "invalid delay (%s): %s", strerror(errno), argv[2]); } /* Initialize libnet */ if ((libnet = libnet_init(LIBNET_RAW4, interface, libnet_ebuf)) == NULL) { errx(1, "couldn't initialize libnet: %s", libnet_ebuf); } if ((target_addr = libnet_name2addr4(libnet, argv[0], LIBNET_RESOLVE)) == -1) { errx(1, "could not resolve target %s", argv[0]); } if ((source_addr = libnet_get_ipaddr4(libnet)) == -1) { errx(1, "could not get local IP: %s", libnet_ebuf); } /* Initialize pcap */ snprintf(pcap_expr, 99, "tcp and tcp[tcpflags] == 18 and src host %s and port %s", argv[0], argv[1]); if ((pcap = pcap_init(interface, pcap_expr, 64)) == NULL) errx(1, "couldn't initialize sniffing"); if ((pcap_off = pcap_dloff(pcap)) < 0) errx(1, "couldn't determine link layer offset"); /* Fork */ pid = fork(); if (pid == -1) errx(1, "fork failed"); else if (pid == 0) send_syns(libnet, source_addr, target_addr, port, delay); else { child_pid = pid; sa.sa_handler = &handle_signal; sigaction(SIGINT, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); sigaction(SIGTERM, &sa, NULL); send_acks(pcap, libnet); } return EXIT_SUCCESS; }