예제 #1
0
파일: ftpc_cd.c 프로젝트: ryucd/ftp
void run_cd(int s, int argc, char* argv[])
{
	struct myftph header;
	char buf[HEADER_SIZE+1];
	bzero(&header, sizeof(header));
	bzero(buf, sizeof(buf));


	// check args
	if(argc != 2){
		fprintf(stderr, "Usage: cd <target>\n");
		return;
	}

	send_data_packet(s, FTP_TYPE_CMD_CWD, 0x00, strlen(argv[1]), argv[1]);

	if(recv(s, buf, HEADER_SIZE, 0) < 0){
		perror("recv");
		exit(1);
	}
	read_ftp_packet(&header, buf);
	
	if(header.type != FTP_TYPE_OK){
		output_error(&header);
	}
}
/* for every connection, send out packet */
void all_provider_connection_send_data(
        struct provider_connection_s* connection_head){
    struct provider_connection_s* temp = connection_head;
    while(temp != NULL){
        send_data_packet(temp);
        temp = temp -> next;
    }
}
예제 #3
0
void Router::forward_incoming_data_packet(Packet* p){
	if( p == NULL){ //check for null packet
		std::cout << "Rxed packet is NULL !\n";
		exit(1);
	}
	std::cout << "Rxed data packet which ll be forwarded is:\n";
	print_data_packet(p);
	//Correct packets will be sent to correct ports
	send_data_packet(p);
}
예제 #4
0
int main(int argc, char** argv){

	
	int s, i;
	slen = sizeof(si_other);
	char fileName[1000];
	struct sigaction act;
	act.sa_handler = sighandler;
	act.sa_flags = 0;


	strcpy(fileName, argv[1]);
	nameLength = strlen(fileName);
	if(argc != 2)
		diep("please enter filename");
	if(sigaction(SIGALRM, &act, 0) == -1)
		perror("sigaction\n");

	if((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
		diep("socket");

	memset((char *) &si_other, 0, sizeof(si_other));
	si_other.sin_family = AF_INET;
	si_other.sin_port = htons(PORT);
	if(inet_aton(SRV_IP, &si_other.sin_addr) == 0){
		fprintf(stderr, "inet_aton() failed\n");
		exit(1);
	}

	while(fin < 0){
		if(ackType == -1)
			send_connection_packet(fileName, s);
		else if(ackType == 1)
			send_data_packet(fileName, s);
		else
			send_end_packet(s);
			
	}
	printf("end the connection\n");
	close(s);
	return 0;
}
예제 #5
0
/* when an ack packet is received, invoke the function to process the pakcet */
void process_ack_packet(char* packet, bt_config_t* config,
                         struct sockaddr_in* from) {
    /* 1. parse ack packet */
    int ack_num = parse_ack(packet);

    // fix 0-1 problem
    ack_num--;

    DPRINTF(DEBUG_SOCKETS, "recv ack: %d\n", ack_num);

    /* 2. find the peer that send the ack packet */
    bt_peer_t* peer = find_peer(config->peers, from);

    if (peer->up_con == NULL){
        /* a "out-of-date" ack packet, ignore it*/
        return;
    }

    /* successfully receive a packet, reset successive_fail */
    peer->up_con->successive_fail = 0;

    /* 3. ack to window */
    int is_resend = window_ack_packet(peer->up_con, ack_num);

    if (is_resend)
        DPRINTF(DEBUG_SOCKETS, "resend data packet due to duplicate ack\n");

    /* 4. send (or resend) data packet */
    send_data_packet(is_resend, config, peer);

    /* 5. check if have received all acks (finish uploading) */
    if (window_finish_ack(peer->up_con)){
        /* CLR select set*/
        FD_CLR(peer->up_con->timer_fd, &config->readset);
        /* destroy connection with peer */
        destroy_connection(peer->up_con);
        peer->up_con = NULL;
        /* dec current upload number */
        config->cur_upload_num--;
    }
}
/*
 * ******************************************* 
 *  Function: send_data_packet_reliable 
 *
 *
 *  Description: 
 *    Sends data packet reliably , by trying 
 *    to send it multiple times before giving up
 *
 *  Parameters: 
 *      dest_id - destination id 
 *
 *      seq_n - sequence number 
 *
 *      cid - connection id 
 *
 *      send_id - sender id 
 *
 *      buffer - packet buffer 
 *
 *      mtu_p  - possible mtu
 *
 *
 *
 *
 * ******************************************* 
 */
int send_data_packet_reliable(int  dest_id , int  seq_n  ,int  cid, int send_id ,void * buffer , int mtu_p) 
{
 
    int retry_count = 0 ; 
   
    while ( retry_count < RETRY_COUNT )
    { 
          send_data_packet( dest_id , seq_n  , cid, send_id , buffer , mtu_p );
          usleep(WAIT_TIME_ACK);   
	  retry_count++; 
   
          if ( sender_buffer[send_id].response_code == CONTROL_ACK &&  sender_buffer[send_id].seq_no == seq_n)
          {
                break; 
                  
          } 
    }
    if ( retry_count >=   RETRY_COUNT )
    {
         return FALSE;
    
    }
    return TRUE;
}
예제 #7
0
void Router::build_map(){
	//Add the this router itself to graph_name and netnode_nhops and nhop_port
	graph_name.push_back(id);
	
	nhops = new char[nbr_tbl.size()];
	for(int i=0; i<nbr_tbl.size(); i++){
		nhops[i] = nbr_tbl.at(i).id[0];
		nhop_port.insert (std::pair<char,int>(nbr_tbl.at(i).id[0], i+1));
		port_nhop.insert (std::pair<int,char>(i+1, nbr_tbl.at(i).id[0] ));
	}
	netnode_nhops.insert (std::pair<char,char*>(id, nhops));
	
	std::map<char,int>::iterator it1;
	std::cout << "nhop - port\n";
	for (it1=nhop_port.begin(); it1!=nhop_port.end(); ++it1){
		std::cout << it1->first << "->" << it1->second << std::endl;
	}
	//put graph_name into char[]
	final_graph_name = new char[++num_of_total_nodes];
	for(int i=0; i<num_of_total_nodes; i++){
		final_graph_name[i] = graph_name.at(i);
	}

	std::map<char,int> vname_vindex;
	for(int i=0; i<strlen(final_graph_name); i++){
		vname_vindex.insert (std::pair<char,int>(final_graph_name[i], i));
	}
	//calculate the # of edges in the network
	num_edges = 0;
	std::map<char,char*>::iterator it;
	std::cout << "overall network topo:\n";
	for (it=netnode_nhops.begin(); it!=netnode_nhops.end(); ++it){
		std::cout << it->first << " -> " << it->second << std::endl;
		num_edges += strlen(it->second);
	}
	std::cout << "network num_edges: " << num_edges << std::endl;

	edge_array = new Edge[num_edges];
	int counter = 0;
	for (it=netnode_nhops.begin(); it!=netnode_nhops.end(); ++it){
		char* temp_array = it->second;
		for(int i=0; i<strlen(temp_array); i++){
			edge_array[counter] = Edge(vname_vindex.find(it->first)->second, 
																 vname_vindex.find(temp_array[i])->second);
			++counter;
		}
	}
	int weights[num_edges];
	for(int i=0; i<num_edges; i++){
		weights[i]=1;
	}
	/*
	std::map <char, int> nhop_port;
  nhop_port.insert (std::pair<char,int>('B', 1));
  nhop_port.insert (std::pair<char,int>('D', 2));
  */
	// declare the graph object
	//graph_t g(edge_array, edge_array+num_edges, weights, strlen(final_graph_name));
	g_p = new graph_t(edge_array, edge_array+num_edges, weights, strlen(final_graph_name));
	
	ftf_p = new ForwardingTableFiller <graph_t> ();
	ftf_p->set_all(vname_vindex.find(id)->second,*g_p, final_graph_name, vname_vindex, nhop_port);
  ftf_p->do_initial_job();
  //Everything is ready to rock
  
  //Some experiments
  /*
  if(id == 'W'){
  	int f_port = ftf_p->forward_to_port(1, "B", NULL);
		std::cout << "unicast to \"B\" forwarding_port: " << f_port << std::endl;
		int* f_ports = new int[3];
		f_port = ftf_p->forward_to_port(2, "BD", &f_ports);
		std::cout << "f_ports: " << f_ports[ 0] << " - "	<< f_ports[1] << " - " << f_ports[2] << std::endl;
		delete f_ports;
  }
  */
  Packet* trial_mc_pkt = create_data_packet('m', '9', id, '2',
														 			 					(char*)"XY", 6, (char*)"deneme");
	Packet* trial_u_pkt = create_data_packet('m', '9', id, '1', 
														 			 					(char*)"X", 6, (char*)"deneme");
	//std::cout << "///////////////////////////////////\n";
	//print_data_packet(trial_u_pkt);
	if(id == 'W')
  	send_data_packet(trial_mc_pkt);
}