Esempio n. 1
0
int generate_route_on_file_packet(u_char* packetOut, char * payload, int size, int type, int seqNum) {

	if (size < MIN_APP_PKT_LEN) {
		fprintf(stderr, "ERROR: size should > 60\n");
		return -1;
	}
	memset(packetOut, 0, sizeof(u_char) * PACKET_BUF_SIZE);
	struct rthdr* rth = (struct rthdr*)packetOut;
	rth->saddr = (u_int16_t)(my_addr & 0xffff);
	rth->daddr = (u_int16_t)(dest_addr & 0xffff);
	rth->ttl = (u_int8_t)(0x10 & 0xff);
	rth->protocol = (u_int8_t)type;
	rth->size = htons((u_int16_t)size);
	rth->dummy[0] = 0x00;
	rth->dummy[1] = 0x00;
	rth->dummy[2] = 0x80;
	rth->dummy[3] = 0x00;
	rth->check = htons(rthdr_chk_gen(rth));

  	//memcpy(&rth, packetOut, sizeof(struct rthdr));
	//print_data(stdout, packetOut, size);
	int i, hdrlen, payload_size;

	switch(type) {
		case ROUTE_ON_CONTROL:
			fprintf(stdout, "WARNING: generate_route_on_packet: does not support ROUTE_ON_CONTROL\n");
			return -1;
		case ROUTE_ON_UNRELIABLE:
			//printf("generating unreliable packets...\n");

            hdrlen = sizeof(struct rthdr) + sizeof(struct urhdr);
            payload_size = size - hdrlen;
            //printf("Size: %d, hdrlen: %d, payload size: %d\n", size, hdrlen, payload_size);
			struct urhdr* urh = (struct urhdr*)(packetOut + sizeof(struct rthdr));
			urh->port = (u_int8_t)(port & 0xff);
			//print_data(stdout, packetOut, size);
			memcpy(packetOut + hdrlen, payload, payload_size);
			urh->check = htons(packet_chk_gen(packetOut, size));
			printf("Sending a UNRELIABLE packet of size: %d\n" , size);
			break;
		case ROUTE_ON_RELIABLE:
		    //printf("generating reliable packets...\n");
			hdrlen = sizeof(struct rthdr) + sizeof(struct rlhdr);
			payload_size = size - hdrlen;
			//printf("Size :%d, Header len: %d, Payload size : %d SeqNo: %d\n", size, hdrlen, payload_size, seqNum);
			struct rlhdr* rlh = (struct rlhdr*)(packetOut + sizeof(struct rthdr));
			rlh->port = (u_int8_t)(port & 0xff);
			rlh->seq = (u_int32_t)(seqNum & 0xffffffff);
			//rlh->dummy = (u_int8_t)(0xff);
            memcpy(packetOut + hdrlen, payload, payload_size);

            rlh->check = htons(packet_chk_gen(packetOut, size));
            //fprintf(stdout, "Send R_Seq #: %d of size %d to %d\n", rlh->seq, size, rth->daddr   );
			break;
		default:
			fprintf(stderr, "ERROR: protocol not supported\n");
			return -1;
	}
	return size;
}
Esempio n. 2
0
int generate_route_on_file_packet(u_char* packetOut, char * payload, int size, int type, int seqNum, int port, int node) {

	if (size < MIN_APP_PKT_LEN) {
		fprintf(stderr, "ERROR: size should > 60\n");
		return -1;
	}
	memset(packetOut, 0, sizeof(u_char) * PACKET_BUF_SIZE);
	struct ethhdr* eth = (struct ethhdr*)packetOut;
	struct rthdr* rth = (struct rthdr*)(packetOut + sizeof(struct ethhdr));
	memcpy(eth->h_source, my_mac, ETH_ALEN);
	if(node == 1){
        memcpy(eth->h_dest, node1_mac, ETH_ALEN);
        rth->daddr = (u_int16_t)(0x0011 & 0xffff);
	}
	else if (node == 2){
      memcpy(eth->h_dest, node2_mac, ETH_ALEN);
      rth->daddr = (u_int16_t)(0x0022 & 0xffff);
	}
	else if (node == 3){
        memcpy(eth->h_dest, node3_mac, ETH_ALEN);
        rth->daddr = (u_int16_t)(0x0033 & 0xffff);
	}
	else {
        printf("Wrong node \n");
        exit(1);
	}

	eth->h_proto = ETH_P_IP;
	rth->saddr = (u_int16_t)(my_addr & 0xffff);

	rth->ttl = (u_int8_t)(0x10 & 0xff);
	rth->protocol = (u_int8_t)type;
	rth->size = htons((u_int16_t)size);
	rth->check = htons(rthdr_chk_gen(rth));

	int i, hdrlen, payload_size;

	switch(type) {
		case ROUTE_ON_RELIABLE:
		    //printf("generating reliable packets...\n");
			hdrlen = sizeof(struct ethhdr) + sizeof(struct rthdr) + sizeof(struct rlhdr);
			payload_size = size - hdrlen;
			//printf("Size :%d, Header len: %d, Payload size : %d SeqNo: %d\n", size, hdrlen, payload_size, seqNum);
			struct rlhdr* rlh = (struct rlhdr*)(packetOut + sizeof(struct ethhdr) + sizeof(struct rthdr));
			rlh->port = (u_int8_t)(port & 0xff);
			rlh->seq = (u_int32_t)(seqNum & 0xffffffff);
			//rlh->dummy = (u_int8_t)(0xff);
            memcpy(packetOut + hdrlen, payload, payload_size);

            rlh->check = htons(packet_chk_gen(packetOut, size));
            //fprintf(stdout, "Send R_Seq #: %d of size %d to %d\n", rlh->seq, size, rth->daddr   );
			break;
		default:
			fprintf(stderr, "ERROR: protocol not supported\n");
			return -1;
	}
	return size;
}
void print_rthdr(FILE* logfile, struct rthdr* hdr) {
	// TODO: test if we need any ntohs(); or htons();
	fprintf(logfile, "Routing Header:\n");
	fprintf(logfile, "\t|-source:             %04x\n", hdr->saddr);
	fprintf(logfile, "\t|-destination:        %04x\n", hdr->daddr);
	fprintf(logfile, "\t|-ttl:                %d\n", (unsigned int)(hdr->ttl));
	fprintf(logfile, "\t|-protocol:           %d\n", (unsigned int)(hdr->protocol));
	fprintf(logfile, "\t|-size:               %d\n", (unsigned int)ntohs((hdr->size)));
	fprintf(logfile, "\t|-checksum:           %04x\n", ntohs(hdr->check));
	fprintf(logfile, "checksum test: %04x\n", rthdr_chk_gen(hdr));
	fprintf(logfile, "\n");
}