Esempio n. 1
0
void aggregatePackets(char* info,Packet* p)
{
	char* t,*data,*tokenPacotes;
	int Npackets = size(info),i = 0;
//	Packet* p = malloc( Npackets * sizeof(Packet));
	int pid,length;
  
	
	t = strtok(info,"#;");
	
	while(t && i < Npackets)
	{

	  if(i>0) t = strtok(NULL,"#;");
	  pid = atoi(t);
	  
	
	  t = strtok(NULL,";");
	  length = atoi(t);

	
	  t = strtok(NULL,";#");
	  data = t;
	

	  p[i] = initPacket(pid,length,data);
          i++; 
        }
	
  	p[i] = initPacket(-1,-1,NULL);
	
}
Esempio n. 2
0
IPv6RAImpl::IPv6RAImpl(SwSwitch* sw,
                       const SwitchState* state,
                       const Interface* intf)
  : AsyncTimeout(sw->getBackgroundEVB()),
    sw_(sw) {
  std::chrono::seconds raInterval(
      intf->getNdpConfig().routerAdvertisementSeconds);
  interval_ = raInterval;
  initPacket(intf);
}
Esempio n. 3
0
void* makePackets(void * data) {
   int fragment = 1;

   char* s = malloc(PACKET_SIZE);

   for(;;){
      if (!isFull(ph)) {
         s = fgets(s, PACKET_SIZE, file);

         if (s == NULL) {
            getLock(ph);
            addPacket(ph, initPacket(NULL, DUMMY_FRAG_NUM));
            unlock(ph);
            pthread_exit(0);
         } else {
            getLock(ph);
            addPacket(ph, initPacket(s, fragment++));
            unlock(ph);
         }	         
      }
   }  
}
Esempio n. 4
0
/*
 * encode the string into multiply packets and put into queue
 *
 */
void encodeString(char* str, struct Queue* q) {
	if(str == NULL || q == NULL) return;
	int i;
	int length = strlen(str);
	char* strBuf = (char*)malloc(sizeof(char)*length);
	strncpy(strBuf, str, length);
	int packet_size = length / 100;
	if(length % 100 >0) {
		packet_size+=1;
	}
	int* num_packets = (int*)malloc(sizeof(int));
	*num_packets = packet_size;
	enqueue(com.pendingPacketSize, (void*)num_packets);
	struct Packet* result;
	for(i = 0; i < packet_size; i++) {
		if(i == packet_size - 1) {
			result = initPacket(length, (unsigned char*)strBuf);
			setHeader(result, 0, 1, STRING); //last packet
		}
		else {
			char* temp = strnsep_(&strBuf, 100);
			result = initPacket(100, (unsigned char*)temp);
			free(temp);
			temp = NULL;
			length -= 100;
		}
		if(i == 0)
			setHeader(result, 1, 0, STRING); //first packet
		else if(i != packet_size - 1)
			setHeader(result, 0, 0, STRING);
		enqueue(q, (void*)result);
	}
	result = NULL;
	free(strBuf);
	strBuf = NULL;
}
Esempio n. 5
0
    void	sendPacket(std::vector<void*> &headers, std::vector<unsigned int> &sizes) {
        int				index = 0;
        struct Ethernet::header_s	*eth = NULL;
        bool			isFirst = true;

        total_size = 0;
        bzero(buffer, 65535);
        for (std::vector<void*>::iterator it = headers.begin();
                it != headers.end(); ++it) {
            if (isFirst) {
                isFirst = false;
                eth = (struct Ethernet::header_s*)(*it);
            }
            memcpy(buffer + total_size, *it, sizes.at(index));
            total_size += sizes.at(index);
            ++index;
        }
        openSocket();
        initPacket(eth->src_mac_address);
        start();
    }
Esempio n. 6
0
ICMPv4PacketImpl::ICMPv4PacketImpl(int dataSize)
	: ICMPPacketImpl(dataSize),
	_seq(0)
{
	initPacket();
}
Esempio n. 7
0
void cleanPackets(Packet* packets,int N)
{
	int i;
	for(i=0;i<N;i++)
		packets[i] = initPacket(-1,-1,NULL);
}
Esempio n. 8
0
int main(int argc, char *argv[]) {
    
    ///////////////*****SETUP****/////////////////
    int sock, port, nbytes, size, base;
    struct sockaddr_in serv_addr, client_addr;

    //initialize the alarm signal
    signal(SIGALRM, catch_alarm);
    
    if (argc < 5) {
         fprintf(stderr,"usage %s port CWind Pl Pc\n", argv[0]);
         exit(1);
    }

    int windowSize = atoi(argv[2]);
    struct packet packets[windowSize];
    int acks[windowSize];
    char fileName[DATA_SIZE];   
    int packetsSent = 0;

    int pCorrupt = atoi(argv[3]);
    int pLoss= atoi(argv[4]);

    sock = socket(AF_INET, SOCK_DGRAM, 0);

    //set socket to not block and set the buffer size
    int flags = fcntl(sock, F_GETFL);
    fcntl(sock, F_SETFL, flags | O_NONBLOCK);

    if (sock < 0) 
    error("ERROR opening socket");

    bzero((char *) &serv_addr, sizeof(serv_addr));
    port = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(port);

    //bind the socket
    size = sizeof(serv_addr);
    if ( bind(sock, (struct sockaddr*) &serv_addr, (socklen_t) size) < 0) {
        error("bind error");
    }

    ///////////////*****HANDSHAKE****/////////////////
    //wait for connections
    struct packet handshake;

    fprintf (stderr, "waiting for message \n");

    size = sizeof(client_addr);
    while(1) {
        if( nbytes = recvfrom(sock, &handshake, DATAGRAM_SIZE, 0, 
                                (struct sockaddr *) &client_addr,
                                 &size) < 0)
        {
            if( errno == EWOULDBLOCK ) {
                continue;
            }
            error("recvfrom failed");
        }
        break;
    }

    //print diagnostic to console
    char addr[256];
    inet_ntop(AF_INET, &(client_addr.sin_addr), addr, INET_ADDRSTRLEN);

    fprintf (stderr, "Sender: got message: %s From: %s : %d\n", handshake.data, addr, client_addr.sin_port);
    dump(&handshake);

    //get the requested filename
    strcpy(fileName, handshake.data);
    
    //init connection by sending an ack   
    //seq stays the same 
    handshake.ack = 1;  
 
    size = sizeof(client_addr);
    while(1) {
        if( nbytes = sendto (sock, &handshake, DATAGRAM_SIZE, 0,
                        (struct sockaddr *) &client_addr , size) < 0)
        {
                if( errno == EWOULDBLOCK ) {
                    continue;
                }
                printf("error: %d\n", errno);
                error("sendto failed");

        }    
        break;
    }


    //print diagnostic to console
    inet_ntop(AF_INET, &(client_addr.sin_addr), addr, INET_ADDRSTRLEN); 
    printf ("Sender: Sent ack for : %d To: %s : %d  ", handshake.seq, addr, client_addr.sin_port);
    dump(&handshake);

    /////// FILE IO /////////
    // open file
    FILE * fd = fopen(fileName,"r");

    if (fd == NULL) // open failed 
    {
        error("failed open file"); 
    }

    ////////////initial window of packets////////////
    base = 1;
    int k;
    for(k = 0; k < windowSize; k++) {

        // get the next chunk of the file
        struct packet p;
        initPacket(&p);

        p.seq = k+1;
        p.size = fread(p.data, 1, DATA_SIZE, fd);
        printf ("Sender: size is %d\n", p.size);
        packets[k] = p;

        //send the packet
        size = sizeof(client_addr);
        while(1) {
            if( nbytes = sendto (sock, &packets[k], DATAGRAM_SIZE, 0,
                (struct sockaddr *) &client_addr , size) < 0)
            {
                if( errno == EWOULDBLOCK ) {
                    continue;
                }
                error("sendto failed");
            }
            break;
        }
        packetsSent++;

        //print diagnostic to console
        inet_ntop(AF_INET, &(client_addr.sin_addr), addr, INET_ADDRSTRLEN); 
        printf ("Sender: Sent datagram to: %s : %d :\n", addr, client_addr.sin_port);
        dump(&packets[k]);

        if(feof(fd) || ferror(fd) ) {
            //done reading file
            break;
        }

   }
   //start the timer
   setTimeout(TIMEOUT);


    ////////****** MAIN LOOP *********///////////
    // wait for acks
    // when we receive an ack, update the packet window and send out the new packets
    while(1) {
        if(timeout) {
            //we timed out, retransmit the window
            printf("packet timed out Seq: \n");
            int k;
            for(k = 0; k < windowSize; k++) {
                //send the packet
                size = sizeof(client_addr);
                while(1) {
                    if( nbytes = sendto (sock, &packets[k], DATAGRAM_SIZE, 0,
                        (struct sockaddr *) &client_addr , size) < 0)
                    {
                        if( errno == EWOULDBLOCK ) {
                            continue;
                        }
                        error("sendto failed");
                    }
                    break;
                }
            }
            setTimeout(TIMEOUT);
        }

        //if file is empty or done reading, terminate connection
        if(feof(fd)) {
        }

        struct packet ack;
        initPacket(&ack);

        //wait for packet
        size = sizeof(client_addr);
        if( nbytes = recvfrom(sock, &ack, DATAGRAM_SIZE, 0, 
                             (struct sockaddr *) &client_addr,
                             &size) < 0)
        {
            if( errno == EWOULDBLOCK ) {
                continue;
            }
            error("recvfrom failed");
        }

        if (prob(pCorrupt) || prob(pLoss)) {
            fprintf(stderr, "packet was corrupted or lost\n");
            continue;
        }

        char addr[256];
        inet_ntop(AF_INET, &(client_addr.sin_addr), addr, INET_ADDRSTRLEN);
        
        /////********* PACKET IS ACK *********////
        if( ack.ack == 1)
        {
            fprintf (stderr, "Sender: got ack From: %s : %d\n", addr, client_addr.sin_port);
            dump(&ack);
            
            if( feof(fd) && ack.seq == packetsSent) {
                fclose(fd);
                struct packet terminate;
                initPacket(&terminate);
                terminate.fin = 1;
                //send the packet
                size = sizeof(client_addr);
                while(1) {    
                    if( nbytes = sendto (sock, &terminate, DATAGRAM_SIZE, 0,
                            (struct sockaddr *) &client_addr , size) < 0)
                    {
                        if( errno == EWOULDBLOCK ) {
                            continue;
                        }
                        error("sendto failed");
                    }
                    break;
                }
                //print diagnostic to console
                inet_ntop(AF_INET, &(client_addr.sin_addr), addr, INET_ADDRSTRLEN); 
                printf ("Sender: Sent termination: To: %s : %d \n", addr, client_addr.sin_port);
                dump(&terminate);
                break;
            }        

            // update the packets in the window and send new packets
            if(base <= ack.seq) {
                //stop the timeout alarm
                alarm(0);

                for(k=0; k < windowSize; k++) {
                    if(packets[k].seq <= ack.seq ) {
                        if(feof(fd) || ferror(fd) ) {
                            //done reading file
                            printf("done reading file\n");
                            break;
                        }

                        //get the next packet
                        struct packet p;
                        p.seq = base + windowSize;
                        //read the next chunk from the file
                        p.size = fread(p.data, 1, DATA_SIZE, fd);
                        packets[k] = p;

                        //send the packet
                        size = sizeof(client_addr);
                        while(1) {
                            if( nbytes = sendto (sock, &packets[k], DATAGRAM_SIZE, 0, (struct sockaddr *) &client_addr , size) < 0)
                            {
                                if( errno == EWOULDBLOCK ) {
                                    continue;
                                }
                                error("sendto failed");
                            }
                            break;
                        }
                        packetsSent++;

                        //print diagnostic to console
                        inet_ntop(AF_INET, &(client_addr.sin_addr), addr, INET_ADDRSTRLEN); 
                        printf ("Sender: Sent datagram To: %s : %d \n", addr, client_addr.sin_port);
                        dump(&packets[k]);

                        base++;
                    }
                }
                //reset the timer
                setTimeout(TIMEOUT);
            }


        }
        else
        {
            fprintf (stderr, "Sender: message wasn't an ack From: %s : %d\n", addr, client_addr.sin_port);
            dump(&ack);
        }
    
    }

}