Beispiel #1
0
int* Router::forward_rxed_data_packet_to_port(Packet* p){
	PacketHdr *hdr = p->accessHeader();
	char p_type = hdr->getOctet(PACKET_TYPE_INDEX);
	if(p_type != 'm'){ //check for packet type
		std::cout << "This function is supposed to be given DATA packet. This has another type !\n";
		exit(1);
	}
	//Extract necessary info for giving the forwarding decision
	char num_addr = hdr->getOctet(NUM_ADDR_INDEX);
	int int_num_addr = (int)(num_addr-'0');
	int* d_p = new int[int_num_addr];
	char dest_addrs_buffer[int_num_addr+1];
	for(int i=0; i < num_addr; i++){
		dest_addrs_buffer[i]=hdr->getOctet(NUM_ADDR_INDEX+i+1);
	}
	dest_addrs_buffer[num_addr] = (char)'/0';
	/*
	std::cout << "=============================================\n";
	std::cout << "int_num_addr: " << int_num_addr << " dest_addrs_buffer: " 
					  << dest_addrs_buffer << std::endl;
	std::cout << "=============================================\n";
	*/
	if(int_num_addr == 1){//unicast packet
		*d_p = ftf_p->forward_to_port(int_num_addr, (char*)dest_addrs_buffer, &d_p);
	}
	else{ //MC packet
		ftf_p->forward_to_port(int_num_addr, (char*)dest_addrs_buffer, &d_p);
	}	
	return d_p;
}
Beispiel #2
0
void Router::send_ls_to_outgoing_ports(char* incoming_TxP, Packet* p){
	PacketHdr *hdr = p->accessHeader();
	std::cout << "LS packed rxed from node_id" << hdr->getOctet(2) << " is sent to outgoing ports" 
						<< " with TTL:" << hdr->getOctet(1) << std::endl;
	int counter = 0;
	for(int i=0; i<num_neighbors; i++){
		if(strcmp(nbr_tbl.at(i).TxP, incoming_TxP) != 0){ //Do not send it to incoming port
			try{
				++counter;
				txing_ports[i]->sendPacket(p);
			}catch(const char *reason){
				cerr << "Exception:" << reason << endl;
			}
		}
		std::cout << "Incoming LS packet is forwarded to " << counter
							<< " outgoin ports\n";
	}
}
Beispiel #3
0
void Router::print_data_packet(Packet* p){
	//First print the header
	PacketHdr *hdr = p->accessHeader();
	
	std::cout << "PRINT PACKET with Header size: " <<hdr->getSize() << "\n";
	std::cout << "packet_type: " <<hdr->getOctet(PACKET_TYPE_INDEX) << "\n";
	std::cout << "ttl: " << hdr->getOctet(TTL_INDEX) << "\n";
	std::cout << "src_addr: " << hdr->getOctet(SRC_ADDR_INDEX) << "\n";
	char num_addr = hdr->getOctet(NUM_ADDR_INDEX);
	std::cout << "num_addr: " << num_addr << "\n";
	
	int int_num_addr = (int)(num_addr-'0');
	char dest_addrs_buffer[int_num_addr+1];
	for(int i=0; i < int_num_addr; i++){
		dest_addrs_buffer[i]=hdr->getOctet(NUM_ADDR_INDEX+i+1);
	}
	dest_addrs_buffer[int_num_addr]='\0';
	std::cout << "dst_addrs: " << dest_addrs_buffer << "\n";
	//Second print the payload
	//std::cout << "payload_size: " << hdr->getOctet(PAYLOAD_SIZE_INDEX(int_num_addr)) << "\n";
	std::cout << "packet_payload: " << p->getPayload() << "\n\n";
}
Beispiel #4
0
void Router::extract_ls_info(Packet* lsp){
	PacketHdr *hdr = lsp->accessHeader();
	char id = hdr->getOctet(2);
	if(std::find(graph_name.begin(), graph_name.end(), id) != graph_name.end()){
		std::cout << "Connectivity info of node_id: " << id << " is already in graph_name\n";
		std::cout << "here comes the graph_name: ";
		for( std::vector<char>::const_iterator i = graph_name.begin(); i != graph_name.end(); ++i)
  	  std::cout << *i << '-';
		std::cout << std::endl;
		return;
	}
	graph_name.push_back(id);
	
	std::cout << "here comes the graph_name: ";
	for( std::vector<char>::const_iterator i = graph_name.begin(); i != graph_name.end(); ++i)
    std::cout << *i << '-';
	std::cout << std::endl;
	
	//Extract the connectivity info
	/*
	//Check the seq_num
	hdr->getOctet(1);
	*/
	int num_nhops = hdr->getOctet(3);
	char* nhops = new char[num_nhops+1];
	for(int i=0; i<num_nhops; i++){
		nhops[i] = hdr->getOctet(3+i+1);
	}
	nhops[num_nhops] = (char)'\0';
	netnode_nhops.insert (std::pair<char,char*>(id, nhops));
	
	
	if(graph_name.size() == num_of_total_nodes){
		std::cout << "CONNECTIVITY INFO FROM EACH NODE IS EXTRACTED YAY !\n";
		build_map();
	}
}	
Beispiel #5
0
void Router::send_data_packet(Packet* p){
	//std::cout << "The following packet ll be sent:" << std::endl;
	//print_data_packet(p);
			
	PacketHdr *hdr = p->accessHeader();
	char num_addr = hdr->getOctet(NUM_ADDR_INDEX);
	int int_num_addr = (int)(num_addr-'0');
	
	
	if(int_num_addr == 1 && hdr->getOctet(NUM_ADDR_INDEX+1) == id){ //if the packet is uni and targeted to me
		std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
		std::cout << "DATA PACKET TARGETED TO ME IS RXED: \n";
		print_data_packet(p);
		std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
		return;
	}
	int* f_ports = forward_rxed_data_packet_to_port(p);
	//Use the "goodness" of at most 3 MC receivers
	std::vector <int> ports_done;
	std::vector <char> temp_dests;
	
	for(int i=0; i<int_num_addr; i++){
		int curr_port = f_ports[i];
		temp_dests.clear();
		if(std::find(ports_done.begin(), ports_done.end(), curr_port) == ports_done.end()){ //not contained
			temp_dests.push_back(hdr->getOctet(NUM_ADDR_INDEX+i+1));
			for(int j=i+1; j<int_num_addr; j++){
				if(curr_port == f_ports[j])
					temp_dests.push_back(hdr->getOctet(NUM_ADDR_INDEX+j+1));
			}
			char* dest_addrs = new char[temp_dests.size()+1];
			for(int k=0; k<temp_dests.size(); k++){
				dest_addrs[k] = temp_dests.at(k);
			}
			dest_addrs[temp_dests.size()] = (char)'\0';
			//hdr->getOctet(PAYLOAD_SIZE_INDEX(temp_dests.size()))
			int payload_size = strlen(p->getPayload());
			char* payload = new char[payload_size];
			strcpy(payload, p->getPayload());
			Packet* new_p = create_data_packet('m', (char)((int)(hdr->getOctet(TTL_INDEX)-'0')-1), 
																			hdr->getOctet(SRC_ADDR_INDEX),
																			'0'+temp_dests.size(), dest_addrs,
																			payload_size,	payload);
			send_packet_over_port(new_p, curr_port);
			//std::cout << "The following packet ll be sent over port:" << curr_port << std::endl;
			//print_data_packet(p);
			//
			ports_done.push_back(curr_port);
		}
	}
	
}
void *interface4(void *arg){
	printf("Starting interface4 thread\n");
	struct cShared *sh = (struct cShared *)arg;
	cout<<"This is interface4!"<<endl;

	Packet *recvPacket;
	PacketHdr *hdr;

	char type;
	int cid;
	int hid;
	int hopnum;
	int interface;

	while(1){
		// sleep(0.4);
		recvPacket = sh->recv_port4->receivePacket();
		if(recvPacket!=NULL){
            
		hdr = recvPacket->accessHeader();
		type = hdr->getOctet(0);


		//receive update packet: [type+cid+#hops]
		if(type==(char)0X02){
			int k=0;

			struct routing_Table *temp=new routing_Table;
			struct routing_Table *ptr=NULL;
			cid=hdr->getIntegerInfo(1);
			hopnum=hdr->getIntegerInfo(5);
			temp->CID=cid;
			temp->interf=4;
			temp->hopNum=hopnum;
			temp->nextrow=NULL;
			temp->previous=NULL;
			// updated in 0903 start
			// time_t t=time(0);
			int t=0;
			// updated in 0903 end
			temp->TtoE=t+TTL;

			ptr=sh->rtable;

			//update the routing table
				while(ptr!=NULL){
					if(ptr->CID==temp->CID){
						k=1;
						if(ptr->hopNum >= temp->hopNum){

							ptr->interf=4;
							ptr->hopNum=temp->hopNum;
							ptr->TtoE=temp->TtoE;

							//broadcast updated packet
							hdr->setIntegerInfo(hopnum+1,5);
							sh->send_port2->sendPacket(recvPacket);
							sh->send_port3->sendPacket(recvPacket);
							sh->send_port1->sendPacket(recvPacket);
						}
						else if(ptr->hopNum==temp->hopNum&&ptr->interf==temp->interf){
							ptr->TtoE=temp->TtoE;
						}

					}
						ptr=ptr->nextrow;
				}
				if(k==0){
					temp->nextrow=sh->rtable;
					sh->rtable->previous=temp;
					sh->rtable=temp;

					//broadcast updated packet
					hdr->setIntegerInfo(hopnum+1,5);
					sh->send_port2->sendPacket(recvPacket);
					sh->send_port3->sendPacket(recvPacket);
					sh->send_port1->sendPacket(recvPacket);
				}
			//}
			//delete ptr;

		}
		//receive response packet: [type+cid+hid+payload]
		else if(type==(char)0x01){
			int k=0;
			struct pending_Table *ptr=NULL;
			ptr=sh->ptable;
			cid=hdr->getIntegerInfo(1);
			hid=hdr->getIntegerInfo(5);
			//find the path in pending table
			while(ptr!=NULL){
				if(ptr->rCID==cid && ptr->HID==hid){
					interface=ptr->interf;
					k=1;
					//decided which interface should send the packet
					switch(interface){
						case 1:
							sh->send_port1->sendPacket(recvPacket);
							break;
						case 2:
							sh->send_port2->sendPacket(recvPacket);
							break;
						case 3:
							sh->send_port3->sendPacket(recvPacket);
							break;
						case 4:
							sh->send_port4->sendPacket(recvPacket);
							break;
						default:
							cout<<"Cannot found path!"<<endl;
							break;
					}
				}
				if(k==1)
					break;
			}
			//cout<<"Delivery Error!"<<endl;
			//delete ptr;
		}

		//receive request packet: [type+cid+hid]
		else{
			int k=0;
			int k1=0;

			struct pending_Table *ptemp=new pending_Table;
			struct pending_Table *pptr=NULL;
			struct routing_Table *rptr=NULL;

			cid=hdr->getIntegerInfo(1);
			hid=hdr->getIntegerInfo(5);


			pptr=sh->ptable;
			rptr=sh->rtable;

			ptemp->rCID=cid;
			ptemp->HID=hid;
			ptemp->interf=4;
			// updated in 0903 start
			// time_t t=time(0);
			int t=0;
			// updated in 0903 end
			ptemp->TtoE=t+rTTL;
			ptemp->nextrow=NULL;
			ptemp->previous=NULL;


			//update the pending table
			while(pptr!=NULL){
				if(pptr->rCID==ptemp->rCID && pptr->HID==ptemp->HID){
					k=1;
					//reset TtoE
					break;
				}
				pptr=pptr->nextrow;
			}
			if(k==0){
				ptemp->nextrow=sh->ptable;
				sh->ptable->previous=ptemp;
				sh->ptable=ptemp;
			}
			//figure the path out with routing table


			while(rptr!=NULL){
				if(rptr->CID==ptemp->rCID){
					k1=1;
					interface=rptr->interf;

					//decided which interface should send the packet
					switch(interface){
						case 1:
							sh->send_port1->sendPacket(recvPacket);
							break;
						case 2:
							sh->send_port2->sendPacket(recvPacket);
							break;
						case 3:
							sh->send_port3->sendPacket(recvPacket);
							break;
						case 4:
							sh->send_port4->sendPacket(recvPacket);
							break;
						default:
							cout<<"Cannot found path!"<<endl;
							break;
					}
				}
				if(k1==1)
					break;
				rptr=rptr->nextrow;
			}
			if(k1==0)
				cout<<"Cannot found such content!"<<endl;
			//delete pptr;
			//delete rptr;
			//delete ptemp;
		}
		//delete recvPacket;

		struct routing_Table *p=NULL;
		p=sh->rtable;
		cout<<"The Routing Table is:"<<endl;
		cout<<"ContentID"<<setw(10)<<"Interface"<<setw(6)<<"#Hops"<<setw(5)<<"TtoE"<<endl;
		while(p->nextrow!=NULL){
			cout<<setw(7)<<p->CID<<setw(10)<<p->interf;
			// cout<<setw(5)<<p->hopNum<<setw(7)<<p->TtoE-time(0)<<endl;
			cout<<setw(5)<<p->hopNum<<setw(7)<<p->TtoE-0<<endl;
			p=p->nextrow;
		}

		struct pending_Table *t=NULL;
		t=sh->ptable;
		cout<<"The Pending Table is:"<<endl;
		cout<<"RequstCID"<<setw(7)<<"HostID"<<setw(10)<<"Interface"<<setw(10)<<"TimeToExp"<<endl;
		while(t->nextrow!=NULL){
			cout<<setw(7)<<t->rCID<<setw(7)<<t->HID;
			// cout<<setw(9)<<t->interf<<setw(10)<<t->TtoE-time(0)<<endl;
			cout<<setw(9)<<t->interf<<setw(10)<<t->TtoE-0<<endl;
			t=t->nextrow;
		}
		}
	}
	return NULL;
}
int Message::get_packet_type(Packet *p){
	PacketHdr *hdr = p->accessHeader();
	return (int) hdr->getOctet(POS_TYPE);
}
int Message::get_packet_HOPS(Packet *p){
	PacketHdr *hdr = p->accessHeader();
	return (int) hdr->getOctet(POS_HOPS);
}
void *ReceiveProcedure(void *arg){
    
    cout << "Start: Receive procedure." << endl;
    struct RPShared *rp = (struct RPShared *)arg;
    Packet *recvPacket;
    PacketHdr *hdr;
    Packet *data_packet;
    PacketHdr *data_packet_hdr;
    unsigned char packet_id;
    unsigned char content_id;
    unsigned char host_id_requesting;
    while(1){
        
        recvPacket = rp->recv_port->receivePacket();
        hdr = recvPacket->accessHeader();
        packet_id = hdr->getOctet(0);
        if(packet_id == '0'){
            cout<<"\n---------------------------------------------------------"<<endl;
            cout << "Message: Serving a request." << endl;
            int i;
            content_id = hdr->getOctet(1);
            host_id_requesting = hdr->getOctet(2);
            printf("Message: Content ID requested is: %d\n",content_id);
            for(i=0; i<ListOfContents.size(); i++){
                if(ListOfContents.at(i) == content_id){
                    data_packet = new Packet();
                    data_packet_hdr = data_packet->accessHeader();
                    data_packet_hdr->setOctet('1',0);
                    data_packet_hdr->setOctet(content_id,1);
                    data_packet_hdr->setOctet(host_id_requesting,2);
                    //Get the file and store as payload
                    string x = "Content" + to_string((int)content_id) + ".txt";
                    
                    cout<<x<<endl;
                    char *filename = const_cast<char*>(x.c_str());
                    //char *filename="Content4.txt";
                    char buffer[MAX_BUFFER];
                    ifstream f;
                    f.open(filename);
                    int iterator = 0;
                    while(!f.eof()){
                        buffer[iterator] = f.get();
                        iterator++;
                    }
                    f.close();
                    data_packet_hdr->setIntegerInfo(iterator,3);
                    data_packet->fillPayload(iterator,buffer);
                    rp->send_port->sendPacket(data_packet);
                    cout << "Message: Requested content sent." << endl;
                    
                }
                else{
                    cout << "Message: Content not present, drop packet." << endl;
                }
            }
           cout<<"\n---------------------------------------------------------"<<endl; 
        }
        else if(packet_id == '1'){
            // Case when a data packet is received
            cout<<"\n---------------------------------------------------------"<<endl;
            cout << "Message: Data packet received." << endl;
            unsigned char id_requested;
            char* temp;
            
            id_requested = hdr->getOctet(1);
            if(id_requested == ind->content_requested_id){
                cout << "Message:  Data packet is correct." << endl;
                ind->receive_ind = '1';
                temp = recvPacket->getPayload();
                string y = "Content" + to_string((int)id_requested) + ".txt";
                char *fname = const_cast<char*>(y.c_str());
                ofstream f;
                f.open(fname);
                f << temp;
                f.close();
                rp->send_port->setACKflag(true);
                rp->send_port->timer_.stopTimer();
                ListOfContents.push_back(id_requested);
            }
            cout<<"\n---------------------------------------------------------"<<endl;   
            
        }
        else if(packet_id == '2'){
                    
            // Drop packet because hosts don't accept packet announcements
           
        }
        else{
            cout << "Packet ID not proper" << endl;
        }
    }
}
Beispiel #10
0
void* Router::listen_on_port(void* thread_id){
	int* tid = (int *)(thread_id);
	pthread_mutex_lock( &mutex_lop );
	int tlocal_tid = *tid;
	pthread_mutex_unlock( &mutex_lop );
  cout<<endl<<"Starting to listen on port " << nbr_tbl.at(tlocal_tid).RxP<<"......\n";
  PacketHdr *hdr;

  while(1){
  	Packet* rxed_p = rxing_ports[tlocal_tid]->receivePacket();
  	hdr = rxed_p->accessHeader();
  	if(hdr->getOctet(0) == 'd'){ //Hello packet is rxed
  		cout << "Disc packet is rxed from node_id: " << hdr->getOctet(1)
  				 << " over rx_port:" << nbr_tbl.at(tlocal_tid).RxP << endl;
  		//Send Hello Ack
    	try{
	  		txing_ports[tlocal_tid]->sendPacket(create_control_packet('1'));
		  }catch(const char *reason){
  			cerr << "Exception:" << reason << endl;
  		}
  	}
  	else if(hdr->getOctet(0) == '1'){ //Hello Ack is rxed
  		cout << "Hello Ack is rxed from node_id: " << hdr->getOctet(1)
  				 << " over rx_port:" << nbr_tbl.at(tlocal_tid).RxP << endl;
  		//Set the nhop_hello_ack_rxed map correctly to indicate the ACK is rxed from the nhop
  		nhop_hello_ack_rxed.find(hdr->getOctet(1))->second = 1;
  		/*
  		//print nhop_hello_ack_rxed
  		std::map<char,int>::iterator it;
  		std::cout << "nhop_hello_ack_rxed:\n";
  		for (it=nhop_hello_ack_rxed.begin(); it!=nhop_hello_ack_rxed.end(); ++it){
			if(it->second == 0){
					std::cout << it->first << " -> " << it->second << std::endl;
				}
			}
			*/
  	}
  	else if(hdr->getOctet(0) == 'l'){ //LSP packet is rxed
  		cout << "LSP packet is rxed from node_id: " << hdr->getOctet(2)
  				 << " over rx_port:" << nbr_tbl.at(tlocal_tid).RxP << endl;
  		//Extract the connectivity info
  		extract_ls_info(rxed_p);
  		//Send it to outgoing ports
  		int ttl = (int)(hdr->getOctet(1)-'0');
  		
  		//std::cout << "----------------------------------------\n";
  		//std::cout << "LS TTL: " << hdr->getOctet(1) << std::endl;
  		if( ttl != 0){ //check ttl if it is 0 then discard:dont forward further
  			--ttl;
  			hdr->setOctet( (char)('0'+ttl),1);
  			//std::cout << "UPDATED LS TTL: " << hdr->getOctet(1) << std::endl;	
	  		send_ls_to_outgoing_ports(nbr_tbl.at(tlocal_tid).TxP, rxed_p);
	  	}
	  	//std::cout << "----------------------------------------\n";
	  	
		}
		else if(hdr->getOctet(0) == 'm'){ //Data packet is rxed
			cout << "\nDATA packet is rxed from node_id: " << hdr->getOctet(2)
  				 << " over rx_port:" << nbr_tbl.at(tlocal_tid).RxP << endl;
			forward_incoming_data_packet(rxed_p);
		}
  	else{
  		cout << "Some packet with unrecognized type is rxed\n";
  	}
  }
}