Advertisement
Guest User

Symmetria

a guest
Sep 21st, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.27 KB | None | 0 0
  1. #include <arpa/inet.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <sys/timeb.h>
  10.  
  11. #define PORT 7890
  12. #define PACKET_DELAY 50
  13.  
  14. void diep(char *stringz)
  15. {
  16.    perror(stringz);
  17.    exit(-1);
  18. }
  19.  
  20. struct s_timestamp {
  21.         int sequence;
  22.         int tseconds;
  23.         int tmilli;
  24.         int total_packets; /* Tell the server how many packets we're sending */
  25.         int padding_size;
  26.         char *padding;
  27. };
  28.  
  29. void delay_time(int PACKETDELAY) {
  30.    int t_diff = 0;
  31.    struct timeb t_delay_s, t_delay_e;
  32.  
  33.    memset(&t_delay_s, sizeof(struct timeb), 0);
  34.    memset(&t_delay_e, sizeof(struct timeb), 0);
  35.    ftime(&t_delay_s);
  36.    while(t_diff < PACKETDELAY) {
  37.      ftime(&t_delay_e);
  38.      t_diff=(1000 * (t_delay_e.time - t_delay_s.time) + (t_delay_e.millitm - t_delay_s.millitm));
  39.    }
  40. }
  41.  
  42. int main(int argc, char *argv[]) {
  43.    struct sockaddr_in udp_socket;
  44.    char payload[sizeof(struct timeb)];
  45.    int u_sock, counter, NUM_PACKETS, socklen=sizeof(udp_socket);
  46.    struct s_timestamp p_data;
  47.    struct timeb t_current;
  48.    char IP_ADDRESS[20];
  49.    int p_size;
  50.  
  51.    if(argc < 4) {
  52.      printf("Usage: %s IP_ADDRESS NUM_PACKETS PAD_AMOUNT\n", argv[0]);
  53.      exit(-1);
  54.    }
  55.  
  56.    memset(&IP_ADDRESS, '\0', 20);
  57.    memcpy((char *)&IP_ADDRESS, argv[1], strlen(argv[1]));
  58.    printf("Found IP address %s\n",&IP_ADDRESS);
  59.  
  60.    if(!(NUM_PACKETS = atoi(argv[2])))  {
  61.      printf("Invalid number of packets specified\n");
  62.      exit(-1);
  63.  
  64.    }
  65.    else printf("Sending %d packets...\n", NUM_PACKETS);
  66.  
  67.    if(!(atoi(argv[3]))) {
  68.      printf("Invalid padding specified\n");
  69.      exit(-1);
  70.    }
  71.    else {
  72.      p_data.padding = malloc(atoi(argv[3]));
  73.      p_size=atoi(argv[3]);
  74.    }
  75.  
  76.    if((u_sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
  77.         diep("socket");
  78.  
  79.    memset((char *)&udp_socket, 0, sizeof(udp_socket));
  80.  
  81.    udp_socket.sin_family = AF_INET;
  82.    udp_socket.sin_port = htons(PORT);
  83.  
  84.     if (inet_aton((char *)&IP_ADDRESS, &udp_socket.sin_addr)==0) {
  85.        fprintf(stderr, "inet_aton() failed\n");
  86.        exit(1);
  87.    }
  88.  
  89.  
  90. //   memset(&p_data, sizeof(struct s_timestamp),0);
  91.    memset(&p_data, p_size + sizeof(struct s_timestamp) ,0);
  92.  
  93.    p_data.padding_size = p_size;
  94.  
  95.    p_size = p_size + sizeof(struct s_timestamp);
  96.  
  97.    p_data.total_packets = NUM_PACKETS;
  98.    p_data.padding_size = p_size - sizeof(struct s_timestamp);;
  99.    if (sendto(u_sock, (void *)&p_data, p_size, 0, (struct sockaddr *)&udp_socket, socklen) == -1)
  100.       diep("sendto()");
  101.  
  102.    for(counter = 0; counter < NUM_PACKETS; counter++) {
  103.       memset(&p_data, p_size ,0);
  104.       p_data.padding_size = p_size - sizeof(struct s_timestamp);
  105.       printf("Sending packet %d\n", counter);
  106.       ftime(&t_current);
  107.       p_data.tseconds = t_current.time;
  108.       p_data.tmilli = t_current.millitm;
  109.       p_data.sequence=counter;
  110.       p_data.total_packets = NUM_PACKETS;
  111.       printf("Transmitting packet with sequence number %d\n",p_data.sequence);
  112.       if (sendto(u_sock, (void *)&p_data, p_size, 0, (struct sockaddr *)&udp_socket, socklen) == -1)
  113.         diep("sendto()");
  114.       delay_time(PACKET_DELAY);
  115.       }
  116.    printf("Size of struct s_timestamp is %d\n",p_size);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement