
#include <stdio.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include        <errno.h>
#include        <signal.h>

double  PR;               /* global variable indicating the probability of packet loss or corruption...
				     this will be given as a parameter... */

#define MAX_INT  2147483647

struct s_pkt {            /* packet format that will be exchanged between sender and receiver */    
    char flags; /* 0 normal operation 1: last packet */
    int  seqnum;
    int  acknum;
    int  checksum;
    char payload[21];
};

void udt_sendto(int s,  const  void  *msg,  size_t  len,  int
		flags, const struct sockaddr *to, int  tolen)
{
    int n;
    char netmsg[sizeof(struct s_pkt)];
    
    memcpy(netmsg,msg,sizeof(struct s_pkt));
    
    sleep(1); /* transmission time */
    if( ((double) random(0)/MAX_INT) < PR) { 
	netmsg[11]='x'; netmsg[12]='x';
	printf("    Network: some bits in pkt are curropted \n");
    } 
    if( ((double) random(0)/MAX_INT) > PR) {  
	n=sendto(s, netmsg,len,flags,to, tolen);
    } else {
	printf("   Network: a paket is lost\n");
    }
    
}
