Exemple #1
0
int main(int argc, const char * argv[]) {
    
    int i;
    char *arg_interface = NULL;
    char *token, *dup;
    
    int num_input_interfaces = 0;
    struct interface *input_interface;
    
    if (argc <= 1) {
        print_usage();
        exit(1);
    }
    
    // Read command line arguments
    if (strcmp("-i", argv[1]) == 0) {
        arg_interface = strdup(argv[2]);
    }
   
    /*
     * CPU Prioritization
     *
     */
    // Increase the priority of the process (max priority is -20, min is 19)
    if (setpriority(PRIO_PROCESS, 0, -20) < 0) {
        fprintf(stderr, "** It is recommend to run as a superuser! **\n");
    }
    
    /*
     * Listening Interfaces - defined in command line arguments
     *
     */
    
    // Realloc doesn't work so we count commas to approx the number of interfaces
    for (i=0; i<strlen(arg_interface); i++) {
        if (arg_interface[i] == ',') num_input_interfaces++;
    }
    num_input_interfaces++;
    
    // Allocate input interface array
    input_interface = (struct interface *) malloc(num_input_interfaces * sizeof(struct interface));
    
    // Parse input interfaces
    num_input_interfaces = 0;
    dup = strdup(arg_interface);
    while ((token = strtok(dup, ",")) != NULL) {
        
        strcpy(input_interface[num_input_interfaces].interface_name, token);
        fill_interface_info(&input_interface[num_input_interfaces]);
        
        // Interface name is valid
        if (input_interface[num_input_interfaces].interface_index != -1) {
            num_input_interfaces++;
        }
        
        dup = NULL;
        
    }
    
    free(arg_interface);
    
    // Print listening interfaces information
    #ifdef _VERBOSE
    fprintf(stderr, "[LISTENING INTERFACES]\n");
    fprintf(stderr, "   Number of listening interfaces: %d\n", num_input_interfaces);
    fprintf(stderr, "   %-5s %-6s %-19s %-15s\n", "Dev", "DevId", "Interface MAC addr", "Inf IP addr");
    for(i=0; i<num_input_interfaces; i++) {
        fprintf(stderr, "%2d ", i+1);
        fprintf_interface(stderr, &input_interface[i]);
    }
    fprintf(stderr, "\n");
    #endif

    
    /*
     * Start packet sniffing threads
     *
     */
    pthread_t thread[num_input_interfaces];
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    
    struct sniffer_thread_parameter params[num_input_interfaces];
    
    for (i=0; i<num_input_interfaces; i++) {
        
        fprintf(stderr, "[Start sniffer #%d - %s]\n", i+1, input_interface[i].interface_name);
        
        params[i].interfaces = &input_interface;
        params[i].num_interfaces = num_input_interfaces;
        params[i].sniff_interface = i;
        
        if (pthread_create(&thread[i], &attr, sniffer_thread, (void *) &params[i]) < 0) {
            fprintf(stderr, "Error: Can not create a thread for the sniffer_thread in main()\n");
        }
        
    }

    for (i=0; i<num_input_interfaces; i++) {
        pthread_join(thread[i], NULL);
    }
    
    return 0;
    
}
Exemple #2
0
int main(int argc, const char * argv[]) {
    
    FILE *file;
    long file_size = -1;
    char *file_path, *file_name;
    struct stat file_stat;
    unsigned char required_acks = FSCP_DEFAULT_NUMBER_OF_ACKS;
    
    uchar_t  *packet;
    uint16_t recvfrom_addr;
    struct layer2 *l2;
    struct layer3 *l3;
    struct layer4_udp *l4;
    int i;
    char *dup, *token;
    uint8_t port;
    struct interface output_interface;
    
    size_t bandwidth = ~0;
    
    if (argc <= 1) {
        print_usage();
        exit(1);
    }
    
    // Increase the priority of the process (max priority is -20, min is 19)
    if (setpriority(PRIO_PROCESS, 0, -15) < 0) {
        fprintf(stderr, "** It is recommend to run as a superuser! **\n");
    }
    
    // Initializing packet and its header
    packet = (uchar_t *) malloc(MTU);
    memset(packet, 0, MTU);
    l2 = (struct layer2 *) packet;
    l3 = (struct layer3 *) (packet + sizeof(struct layer2));
    l4 = (struct layer4_udp *) (packet + sizeof(struct layer2) + sizeof(struct layer3));
    l3->type = TYPE_UDP;

    if (strcmp("-r", argv[1]) == 0) {
        
        //Begin receiving the file
        if (argc >= 11) {
            
            if (strcmp("-src", argv[2]) == 0) {
                l2->original_source_addr = htons((uint16_t) atoi(argv[3]));
            }
            else {
                print_usage();
                exit(1);
            }
            
            if (strcmp("-from", argv[4]) == 0) {
                recvfrom_addr = (uint16_t) atoi(argv[5]);
            }
            else {
                print_usage();
                exit(1);
            }
            
            if (strcmp("-path", argv[6]) == 0) {
                
                i = 0;
                dup = strdup(argv[7]);
                while ((token = strtok(dup, ",")) != NULL) {
                    
                    l3->source_routing[i] = atoi(token);
                    
                    i++;
                    dup = NULL;
                    
                    if (i > MAX_HOPS-1) break;
                }
                
                free(dup);
            }
            else {
                print_usage();
                exit(1);
            }
            
            if (strcmp("-port", argv[8]) == 0) {
                port = (uint8_t) atoi(argv[9]);
                l4->sport = port;
                l4->dport = port;
                l4->len = htons(FSCP_UDP_ID_BYTES);
            }
            else {
                print_usage();
                exit(1);
            }
            
            if (strcmp("-dev", argv[10]) == 0) {
                strcpy(output_interface.interface_name, argv[11]);
                fill_interface_info(&output_interface);
            }
            else {
                print_usage();
                exit(1);
            }
            
            if ((argc >= 13) && (strcmp("-f", argv[12]) == 0)) {
                if (argc >= 14) {
                    file_name = (char *) malloc(sizeof(char) * (strlen(argv[13]) + 1));
                    strcpy(file_name, argv[13]);
                }
                else {
                    print_usage();
                    exit(1);
                }
            }
            else {
                file_name = strdup("");
            }

            init_receiver(packet, recvfrom_addr, port, &output_interface, file_name);
            
        }
        else {
            print_usage();
            exit(1);
        }
        
    }
    else if (strcmp("-s", argv[1]) == 0) {
        if (argc >= 14) {

            if (strcmp("-src", argv[2]) == 0) {
                l2->original_source_addr = htons((uint16_t) atoi(argv[3]));
            }
            else {
                print_usage();
                exit(1);
            }
            
            if (strcmp("-dest", argv[4]) == 0) {
                recvfrom_addr = (uint16_t) atoi(argv[5]);
            }
            else {
                print_usage();
                exit(1);
            }

            if (strcmp("-path", argv[6]) == 0) {
                
                i = 0;
                dup = strdup(argv[7]);
                while ((token = strtok(dup, ",")) != NULL) {
                    
                    l3->source_routing[i] = atoi(token);
                    
                    i++;
                    dup = NULL;
                    
                    if (i > MAX_HOPS-1) break;
                }
                
                free(dup);
            }
            else {
                print_usage();
                exit(1);
            }
            
            if (strcmp("-port", argv[8]) == 0) {
                port = (uint8_t) atoi(argv[9]);
                l4->sport = port;
                l4->dport = port;
            }
            else {
                print_usage();
                exit(1);
            }
            
            if (strcmp("-dev", argv[10]) == 0) {
                strcpy(output_interface.interface_name, argv[11]);
                fill_interface_info(&output_interface);
            }
            else {
                print_usage();
                exit(1);
            }
            
            if (strcmp("-f", argv[12]) == 0) {
                file = fopen(argv[13], "r");
                if(!file) {
                    fprintf(stderr, "Error: cannot read the file %s\n", argv[13]);
                    exit(1);
                }
                
                stat(argv[13], &file_stat);
                file_size = file_stat.st_size;
                file_path = strdup(argv[13]);
                file_name = strdup(basename(file_path));
                
            }
            else {
                print_usage();
                exit(1);
            }
            
            if (argc >= 15) {
                if ((strcmp("-ack", argv[14]) == 0) && (argc >= 16)) {
                    required_acks = atoi(argv[15]);
                    if (required_acks == 0) {
                        required_acks = FSCP_DEFAULT_NUMBER_OF_ACKS;
                    }
                }
                #ifdef _THROTTLING_ENABLED
                else if ((strcmp("-bw", argv[14]) == 0) && (argc >= 16)) {
                    bandwidth = atoi(argv[15]);
                    bandwidth *= 125 * 1000;
                }
                #endif
                else {
                    print_usage();
                    exit(1);
                }
            }
            
            #ifdef _THROTTLING_ENABLED
            if (argc >= 17) {
                if ((strcmp("-bw", argv[16]) == 0) && (argc >= 18)) {
                    bandwidth = atoi(argv[17]);
                    bandwidth *= 125 * 1000;
                }
                else {
                    print_usage();
                    exit(1);
                }
            }
            #endif

            init_sender(packet, recvfrom_addr, port, port, &output_interface, file, file_size, file_name, required_acks, bandwidth);
            
        }
        else {
            print_usage();
            exit(1);
        }
        
    }
    else {
        print_usage();
        exit(1);
    }
    
    return 0;
}
Exemple #3
0
int main(int argc, const char * argv[]) {
    
    char *keystoredir;
    char *arg_interface = NULL;
    char *token, *dup;
    
    int i;
    int num_input_interfaces = 0;
    
    uint16_t src;
    
    struct stat s;
    struct interface *input_interface;
    
    if (argc <= 5) {
        print_usage();
        exit(1);
    }
    
    if (!((strcmp("-s", argv[1]) == 0) || (strcmp("-c", argv[1]) == 0))) {
        print_usage();
        exit(1);
    }
    
    if (strcmp("-src", argv[2]) != 0) {
        print_usage();
        exit(1);
    }
    
    if (strcmp("-d", argv[4]) != 0) {
        print_usage();
        exit(1);
    }
    
    if (strcmp("-i", argv[6]) != 0) {
        print_usage();
        exit(1);
    }
    
    src = (uint16_t) atoi(argv[3]);
    keystoredir = strdup(argv[5]);
    
    
    // Remove / if the dirname ends with a slash
    if (keystoredir[strlen(keystoredir)-1] == '/') {
        keystoredir[strlen(keystoredir)-1] = '\0';
    }
    
    // Check if the keystore directory exists
    if (stat(keystoredir, &s) == -1) {
        fprintf(stderr, "Error: keystore %s does not exist\n", keystoredir);
        exit(1);
    }
    else {
        if(!S_ISDIR(s.st_mode)) {
            fprintf(stderr, "Error: keystore %s is not a directory\n", keystoredir);
            exit(1);
        }
    }
    
    arg_interface = strdup(argv[7]);
    
    /*
     * Network Interfaces
     *
     */
    // Count commas to get the upper bound of the number of interfaces
    for (i=0; i<strlen(arg_interface); i++) {
        if (arg_interface[i] == ',') num_input_interfaces++;
    }
    num_input_interfaces++;
    
    // Allocate input interface array
    input_interface = (struct interface *) malloc(num_input_interfaces * sizeof(struct interface));
    
    // Parse input interfaces
    num_input_interfaces = 0;
    dup = strdup(arg_interface);
    while ((token = strtok(dup, ",")) != NULL) {
        
        strcpy(input_interface[num_input_interfaces].interface_name, token);
        fill_interface_info(&input_interface[num_input_interfaces]);
        
        // Interface name is valid
        if (input_interface[num_input_interfaces].interface_index != -1) {
            num_input_interfaces++;
        }
        
        dup = NULL;
        
    }
    free(arg_interface);
    free(token);
    
    // Check if the number of interfaces is at least 1
    if (num_input_interfaces <= 0) {
        fprintf(stderr, "Error: no valid network interfaces\n");
        exit(1);
    }
    
    // Print listening interfaces information
    #ifdef _VERBOSE
    fprintf(stderr, "[NETWORK INTERFACES]\n");
    fprintf(stderr, "   Number of network interfaces: %d\n", num_input_interfaces);
    fprintf(stderr, "   %-5s %-6s %-19s %-15s\n", "Dev", "DevId", "Interface MAC addr", "Inf IP addr");
    for(i=0; i<num_input_interfaces; i++) {
        fprintf(stderr, "%2d ", i+1);
        fprintf_interface(stderr, &input_interface[i]);
    }
    fprintf(stderr, "\n");
    #endif
    
    
    
    if (strcmp("-s", argv[1]) == 0) {
        server(src, keystoredir, num_input_interfaces, &input_interface);
    }
    else if (strcmp("-c", argv[1]) == 0) {
        client(src, keystoredir, num_input_interfaces, &input_interface);
    }
    
    return 0;
    
}