Beispiel #1
0
void* send_fold(void *params)
{
	char *tmp=params;
	if(strlen(tmp)<1)
	{
		/* Error 7: Worng parameters */
		consoleErrorLog(7);
		consoleUsageCmd(&SEND_FOLD_CMD);
		return;
	}
	packet p;
	p.IP_src=client_address.sin_addr;
	p.IP_dst=server_address.sin_addr;
	strcpy(p.login_src,user_login);
	strcpy(p.login_dst,SERVER_ADMIN_NAME);
	strcpy(p.data_type,FILE_TYPE);
	
	char fold[strlen(tmp)+1];
	strcpy(fold,tmp);
	strcat(fold,"/");
	
	path_content path;
	getListFolder(fold,&path);
	int i;
	for(i=0;i<path.nb_elmt;i++)
	{
		if(strcmp(path.content[i],"..")==0)
		{
			continue;
		}
		FILE *f=NULL;
		char file_path[strlen(fold)+strlen(path.content[i])];
		strcpy(file_path,fold);
		strcat(file_path,path.content[i]);
		f=fopen(file_path,"r");
		if(f!=NULL)
		{
			fclose(f);
			packet *packet_file;
			packet_file=calloc(1,sizeof(packet));
			memcpy(packet_file,&p,sizeof(packet));
			send_file_packet(file_path,socket_descriptor,packet_file);
			free(packet_file);
		}
		else
		{
			char sub_fold[strlen(path.content[i])];
			strcpy(sub_fold,path.content[i]);
			strcpy(sub_fold,substr(sub_fold,1,strlen(sub_fold)));
			strtok(sub_fold,"]");
			char new_path[strlen(fold)+1+strlen(sub_fold)];
			strcpy(new_path,fold);
			strcat(new_path,sub_fold);
			send_fold(new_path);
		}
	}
	sprintf(logC,"Folder \"%s\" sent\n",fold);
	consoleLog(logC);
	memset(&logC,0,strlen(logC));
}
Beispiel #2
0
void* send_file(void *params)
{
	char *tmp=params;
	if(strlen(tmp)<1)
	{
		/* Error 7: Worng parameters */
		consoleErrorLog(7);
		consoleUsageCmd(&SEND_FILE_CMD);
		return;
	}
	packet p;
	p.IP_src=client_address.sin_addr;
	p.IP_dst=server_address.sin_addr;
	strcpy(p.login_src,user_login);
	strcpy(p.login_dst,SERVER_ADMIN_NAME);
	strcpy(p.data_type,FILE_TYPE);
	
	send_file_packet(tmp,socket_descriptor,&p);
}
int main (int argc, char** argv) {
	if (argc < 3) {
		printf("Usage: sudo ./fsender filepath source dest1 dest2\n");
		exit(1);
	}
	int src, dest1, dest2;
	pcap_if_t *device_list = NULL;		// Linked list of all devices discovered
	pcap_if_t *device_ptr = NULL;		// Pointer to a single device
	//pcap_t *handle_sniffed = NULL;

	char err[128];						// Holds the error
	char *device_name = NULL;
	char devices[10][64];				// For holding all available
	int count = 0;
	int ret = 0;
	int n = 0;

	srand(time(NULL));

	printf("Scanning available devices ... ");
	if ( (ret = pcap_findalldevs(&device_list, err)) != 0 ) {
		fprintf(stderr, "Error scanning devices, with error code %d, and error message %s\n", ret, err);
		exit(1);
	}
	printf("DONE\n");

	printf("Here are the available devices:\n");
	for (device_ptr = device_list; device_ptr != NULL; device_ptr = device_ptr->next) {
		printf("%d. %s\t-\t%s\n", count, device_ptr->name, device_ptr->description);
		if (device_ptr->name != NULL) {
			strcpy(devices[count], device_ptr->name);
		}
		count++;
	}

	printf("Which device do you want to sniff? Enter the number:\n");
	scanf("%d", &n);
	device_name = devices[n];

	printf("Trying to open device %s to send ... ", device_name);
	if ( (handle_sniffed = pcap_open_live(device_name, BUFSIZ, 1, 100, err)) == NULL ) {
		fprintf(stderr, "Error opening device %s, with error message %s\n", device_name, err);
		exit(1);
	}
	printf( "DONE\n");


    //	unsigned char key_p[4];
//    key_p[0] = 0x33; key_p[1] = 0x13; key_p[2]= 0x3f; key_p[3] = 0xe4;
//	initDes(key_p, &schedule_p);

    u_char payload[PAYLOAD_SIZE];
    char node_name[8];
    int payload_size;
    long offset = 0;
    mapfile(argv[1]);

    switch(atoi(argv[2])){
    case 1:
        src = NODE1_RTR1;
        strcpy(node_name, "node1");
//        key_h[0] = 0x99; key_h[1] = 0xa1; key_h[2]= 0x10; key_h[3] = 0x7b;
//        key_h[4] = 0xeb; key_h[5] = 0x17; key_h[6]= 0x6a; key_h[7] = 0x2a;
        break;
    case 2:
        src = NODE2_RTR1;
        strcpy(node_name, "node2");
//        key_h[0] = 0x13; key_h[1] = 0x33; key_h[2]= 0xe4; key_h[3] = 0x3f;
//        key_h[4] = 0x13; key_h[5] = 0x33; key_h[6]= 0xe4; key_h[7] = 0x3f;
        break;
    case 3:
        src = NODE3_RTR1;
        strcpy(node_name, "node3");
//        key_h[0] = 0xe4; key_h[1] = 0x3f; key_h[2]= 0x13; key_h[3] = 0x33;
//        key_h[4] = 0xe4; key_h[5] = 0x3f; key_h[6]= 0x13; key_h[7] = 0x33;
        break;
    default:
        break;
    }

    switch(atoi(argv[3])){
    case 1:
        dest1 = NODE1_RTR1;
        break;
    case 2:
        dest1 = NODE2_RTR1;
        break;
    case 3:
        dest1 = NODE3_RTR1;
        break;
    default:
        break;
    }

    switch(atoi(argv[4])){
    case 1:
        dest2 = NODE1_RTR1;
        break;
    case 2:
        dest2 = NODE2_RTR1;
        break;
    case 3:
        dest2 = NODE3_RTR1;
        break;
    default:
        break;
    }

    //printKey(key_h, 8);
    if((ret = initDes(node_name, &schedule_h)) < 0){
        printf("Failed with %d \n", ret);
        }

    //printKey(&schedule_h, 8);
//    fp_read = fopen(argv[1], "r");
//    if(fp_read == NULL){
//        fprintf(stderr, "File open failed");
//        exit(1);
//    }
//    fseek(fp_read, 0L, SEEK_END);
//    filesize = ftell(fp_read);
    if((filesize % PAYLOAD_SIZE) != 0)
        no_of_packets = (filesize/PAYLOAD_SIZE) + 1;
    else
        no_of_packets = (filesize/PAYLOAD_SIZE);

    //initialize keys

    int seqNum = 0; int i;

    char str[20];
    fprintf(stdout, "Please enter Y/y to start \n");
    scanf("%s", &str);

    if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) { perror( "clock gettime" );}
    printTime();
    printf("Sending ... \n");
    for(i = 0; i < 1; i++){
        seqNum = 0;
        offset = 0;
        while (seqNum < no_of_packets) {
            //printf("Seq : %d\n", seqNum);
            //fseek(fp_read, offset, SEEK_SET);
            if((seqNum == (no_of_packets-1)) && ((filesize % PAYLOAD_SIZE) != 0))
                payload_size = filesize % PAYLOAD_SIZE;
            else
                payload_size = PAYLOAD_SIZE;
            memcpy(payload, data + offset, payload_size);
            //fread(payload, 1, payload_size, fp_read);
            offset = offset + payload_size;
            // send to 1 destination
            send_file_packet(payload, payload_size, src, dest1, 11, seqNum);
            usleep(200);
            // send to 2nd destination
            send_file_packet(payload, payload_size, src, dest2, 12, seqNum);
            usleep(100);
            seqNum++;
        }
    }
    printf("\n");
    //pthread_join(resend_thread, NULL);
    pcap_close(handle_sniffed);
    //pcap_close(handle_sniffed_nack);
    munmap(data, filesize);
    close(fp);
    printf( "ALL DONE\n");
    return 1;
}