示例#1
0
int main(int argc, char** argv)
{
	uint32_t send_sec;
	uint32_t packet_size;
	struct lorcon_packet *packet;
	uint32_t i;
	int32_t ret;
	uint32_t mode;
	uint32_t delay_us;
	struct timespec start, now;
	int32_t diff;

	/* Parse arguments */
	if (argc > 5) {
		printf("Usage: random_packets <number> <length> <mode: 0=my MAC, 1=injection MAC> <delay in us>\n");
		return 1;
	}
	if (argc < 5 || (1 != sscanf(argv[4], "%u", &delay_us))) {
		delay_us = 0;
	}
	if (argc < 4 || (1 != sscanf(argv[3], "%u", &mode))) {
		mode = 0;
		printf("Usage: random_packets <number> <length> <mode: 0=my MAC, 1=injection MAC> <delay in us>\n");
	} else if (mode > 1) {
		printf("Usage: random_packets <number> <length> <mode: 0=my MAC, 1=injection MAC> <delay in us>\n");
		return 1;
	}
	if (argc < 3 || (1 != sscanf(argv[2], "%u", &packet_size)))
		packet_size = 2200;
	if (argc < 2 || (1 != sscanf(argv[1], "%u", &send_sec)))
		send_sec = 10000;

	/* Generate packet payloads */
	//printf("Generating packet payloads \n");
	payload_buffer = malloc(PAYLOAD_SIZE);
	if (payload_buffer == NULL) {
		perror("malloc payload buffer");
		exit(1);
	}
	generate_payloads(payload_buffer, PAYLOAD_SIZE);

	/* Setup the interface for lorcon */
	//printf("Initializing LORCON\n");
	init_lorcon();

	/* Allocate packet */
	packet = malloc(sizeof(*packet) + packet_size);
	if (!packet) {
		perror("malloc packet");
		exit(1);
	}
	packet->fc = (0x08 /* Data frame */
				| (0x0 << 8) /* Not To-DS */);
	packet->dur = 0xffff;
	if (mode == 0) {
		memcpy(packet->addr1, "\x00\x16\xea\x12\x34\x56", 6);
		get_mac_address(packet->addr2, "mon1");
		memcpy(packet->addr3, "\x00\x16\xea\x12\x34\x56", 6);
	} else if (mode == 1) {
		memcpy(packet->addr1, "\x00\x16\xea\x12\x34\x56", 6);
		memcpy(packet->addr2, "\x00\x16\xea\x12\x34\x56", 6);
		memcpy(packet->addr3, "\xff\xff\xff\xff\xff\xff", 6);
	}
	packet->seq = 0;
	tx_packet.packet = (uint8_t *)packet;
	tx_packet.plen = sizeof(*packet) + packet_size;

	/* Send packets */
	//printf("Sending packets of size %u for %d sec...\n", packet_size, send_sec);
	if (delay_us) {
		/* Get start time */
		clock_gettime(CLOCK_MONOTONIC, &start);
	}

	i = 0;
	int cur_sec = 0;
	double cur_pkt_msec = 0;
	clock_gettime(CLOCK_MONOTONIC, &start);
	clock_gettime(CLOCK_MONOTONIC, &now);
	//for (i = 0; i < send_sec; ++i) {
	while(cur_sec < send_sec) {
		payload_memcpy(packet->payload, packet_size,
				(i*packet_size) % PAYLOAD_SIZE);
	
		clock_gettime(CLOCK_MONOTONIC, &now);
		int sub_time = (int)(now.tv_nsec * 1e-9 + now.tv_sec - start.tv_nsec * 1e-9 - start.tv_sec);
		if (sub_time > cur_sec) {
			cur_sec = sub_time;
			//printf("time: %d\n", (now.tv_nsec - start.tv_nsec) * 1e-9);
			printf("time now: %d\n", cur_sec);
			
		}	
		/*	
		double sub_pkt_msec = (now.tv_nsec - start.tv_nsec) / 1e6 + (now.tv_sec - start.tv_sec) * 1e3;
		if (sub_pkt_msec > cur_pkt_msec + 0.25) {
			cur_pkt_msec = sub_pkt_msec;
		} else {
			continue;
		}
		*/

		if (delay_us) {
			clock_gettime(CLOCK_MONOTONIC, &now);
			diff = (now.tv_sec - start.tv_sec) * 1000000 +
			       (now.tv_nsec - start.tv_nsec + 500) / 1000;
			diff = delay_us*i - diff;
			if (diff > 0 && diff < delay_us)
				usleep(diff);
		}

		ret = tx80211_txpacket(&tx, &tx_packet);
		if (ret < 0) {
			fprintf(stderr, "Unable to transmit packet: %s\n",
					tx.errstr);
			exit(1);
		}
		i++;

		if (((i+1) % 1000) == 0) {
			//printf(".");
			fflush(stdout);
		}
		if (((i+1) % 50000) == 0) {
			//printf("%dk\n", (i+1)/1000);
			fflush(stdout);
		}
	}
	printf("total packet sent: %d\n", i);

	return 0;
}
示例#2
0
int main(int argc, char** argv)
{
	const char *filepath;

	FILE *file;
	//FILE *fout;

	size_t cnt;

	__le16 seq = 0;

	if(argc != 2){
		printf("Usage: ./tx filepath\n");
		return -1;
	}
	
	filepath = argv[1];
	printf("%s\n",filepath);


	//1.open file for write, byte format
	file=fopen(filepath,"rb");
	if(file==NULL){
		printf("open file error\n");
		return -1;
	}

	/*
	fout=fopen("test.dat","w+");
	if(fout==NULL){
		printf("open file error\n");
		return -1;
	}
	*/


	//2. init lorcon
	printf("Initializing LORCON\n");
	init_lorcon();

	//3. allocat packet and set header
	packet = malloc(sizeof(*packet) + packet_size);
	if (!packet) {
		perror("malloc packet");
		exit(1);
	}
	packet->fc = (0x08 
				| (0x0 << 8) );
	packet->dur = 0xffff;

	memcpy(packet->addr1, "\x00\x16\xea\x12\x34\x56", 6);
	memcpy(packet->addr2, "\x00\x16\xea\x12\x34\x56", 6);
	memcpy(packet->addr3, "\xff\xff\xff\xff\xff\xff", 6);

	packet->seq = seq;
	tx_packet.packet = (uint8_t *)packet;
	tx_packet.plen = sizeof(*packet) + packet_size;


	//4. read file and send
	//size and nmemb!
	//differ cnt == packet_size and cnt < packet_size
	while((cnt = fread(packet->payload,1,packet_size,file)) == packet_size){
		printf("%d\n",cnt);
		//fwrite(packet->payload,1,cnt,fout);
		send_beacon(seq++);		
	}
	if(cnt > 0){
		printf("%d\n",cnt);
		//fwrite(packet->payload,1,cnt,fout);
		tx_packet.plen = sizeof(*packet) + cnt;
		send_beacon(seq++);
	}

	free(packet);
	fclose(file);
	//fclose(fout);
	return 0;
}
示例#3
0
int main(int argc, char** argv)
{
	uint8_t *payload_buffer;
	uint32_t num_packets = 0;
	uint32_t packet_size = 0;
	uint32_t payload_size = 0;
	char *file_path;
	struct lorcon_packet *packet;
	uint32_t i;
	int32_t ret;
	uint32_t mode;
	uint32_t delay_us;
	struct timespec start, now;
	int32_t diff;

	/* Parse arguments */
	if (argc > 4) {
		printf("Usage: random_packets <file_name> <mode: 0=my MAC, 1=injection MAC> <delay in us>\n");
		return 1;
	}
	if (argc < 4 || (1 != sscanf(argv[3], "%u", &delay_us))) {
		delay_us = 0;
	}
	if (argc < 3 || (1 != sscanf(argv[2], "%u", &mode))) {
		mode = 0;
	} else if (mode > 1) {
		printf("Usage: random_packets <file_name> <mode: 0=my MAC, 1=injection MAC> <delay in us>\n");
		return 1;
	}
	if (argc < 2 || strlen(argv[1]) == 0) {
		printf("Usage: random_packets <file_name> <mode: 0=my MAC, 1=injection MAC> <delay in us>\n");
		return 1;
	} else {
		file_path = argv[1];
	}
	
	/* Generate packet payloads */
	printf("Generating packet payload from file: %s \n", file_path);
	generate_payload_from_file(file_path, &payload_buffer, &payload_size);
	printf("Read payload, size is: %d\n", payload_size);
	
	// Set packet size and number of packets
	if (payload_size > MAX_PACKET_SIZE) {
		packet_size = MAX_PACKET_SIZE;
		num_packets = ceil((float) payload_size / (float) MAX_PACKET_SIZE);
	} else {
		packet_size = payload_size;
		num_packets = 1;
	}

	/* Setup the interface for lorcon */
	printf("Initializing LORCON\n");
	init_lorcon();

	/* Allocate packet */
	packet = malloc(sizeof(*packet) + packet_size);
	if (!packet) {
		perror("malloc packet");
		exit(1);
	}
	packet->fc = (0x08 /* Data frame */
				| (0x0 << 8) /* Not To-DS */);
	packet->dur = 0xffff;
	if (mode == 0) {
		memcpy(packet->addr1, "\x00\x16\xea\x12\x34\x56", 6);
		get_mac_address(packet->addr2, "mon0");
		memcpy(packet->addr3, "\x00\x16\xea\x12\x34\x56", 6);
	} else if (mode == 1) {
		memcpy(packet->addr1, "\x00\x16\xea\x12\x34\x56", 6);
		memcpy(packet->addr2, "\x00\x16\xea\x12\x34\x56", 6);
		memcpy(packet->addr3, "\xff\xff\xff\xff\xff\xff", 6);
	}
	packet->seq = 0;
	tx_packet.packet = (uint8_t *)packet;
	tx_packet.plen = sizeof(*packet) + packet_size;

	/* Send packets */
	printf("Sending %u packets of size %u (. every thousand)\n", num_packets, packet_size);
	if (delay_us) {
		/* Get start time */
		clock_gettime(CLOCK_MONOTONIC, &start);
	}
	for (i = 0; i < num_packets; ++i) {
		if (i == (num_packets - 1)) {
			printf("final packet size: %d\n", (payload_size - i * packet_size));
			memcpy(packet->payload, (payload_buffer + (i * packet_size)), (payload_size - i * packet_size));
			tx_packet.plen = sizeof(*packet) + (payload_size - i * packet_size);
		} else {
			memcpy(packet->payload, (payload_buffer + (i * packet_size)), packet_size);
		}
		if (delay_us) {
			clock_gettime(CLOCK_MONOTONIC, &now);
			diff = (now.tv_sec - start.tv_sec) * 1000000 +
			       (now.tv_nsec - start.tv_nsec + 500) / 1000;
			diff = delay_us*i - diff;
			if (diff > 0 && diff < delay_us)
				usleep(diff);
		}

		ret = tx80211_txpacket(&tx, &tx_packet);
		if (ret < 0) {
			fprintf(stderr, "Unable to transmit packet: %s\n",
					tx.errstr);
			exit(1);
		}

		if (((i+1) % 1000) == 0) {
			printf(".");
			fflush(stdout);
		}
		if (((i+1) % 50000) == 0) {
			printf("%dk\n", (i+1)/1000);
			fflush(stdout);
		}
	}

	return 0;
}