Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/timeb.h>
- #define PORT 7890
- #define PACKET_DELAY 50
- void diep(char *stringz)
- {
- perror(stringz);
- exit(-1);
- }
- struct s_timestamp {
- int sequence;
- int tseconds;
- int tmilli;
- int total_packets; /* Tell the server how many packets we're sending */
- int padding_size;
- char *padding;
- };
- void delay_time(int PACKETDELAY) {
- int t_diff = 0;
- struct timeb t_delay_s, t_delay_e;
- memset(&t_delay_s, sizeof(struct timeb), 0);
- memset(&t_delay_e, sizeof(struct timeb), 0);
- ftime(&t_delay_s);
- while(t_diff < PACKETDELAY) {
- ftime(&t_delay_e);
- t_diff=(1000 * (t_delay_e.time - t_delay_s.time) + (t_delay_e.millitm - t_delay_s.millitm));
- }
- }
- int main(int argc, char *argv[]) {
- struct sockaddr_in udp_socket;
- char payload[sizeof(struct timeb)];
- int u_sock, counter, NUM_PACKETS, socklen=sizeof(udp_socket);
- struct s_timestamp p_data;
- struct timeb t_current;
- char IP_ADDRESS[20];
- int p_size;
- if(argc < 4) {
- printf("Usage: %s IP_ADDRESS NUM_PACKETS PAD_AMOUNT\n", argv[0]);
- exit(-1);
- }
- memset(&IP_ADDRESS, '\0', 20);
- memcpy((char *)&IP_ADDRESS, argv[1], strlen(argv[1]));
- printf("Found IP address %s\n",&IP_ADDRESS);
- if(!(NUM_PACKETS = atoi(argv[2]))) {
- printf("Invalid number of packets specified\n");
- exit(-1);
- }
- else printf("Sending %d packets...\n", NUM_PACKETS);
- if(!(atoi(argv[3]))) {
- printf("Invalid padding specified\n");
- exit(-1);
- }
- else {
- p_data.padding = malloc(atoi(argv[3]));
- p_size=atoi(argv[3]);
- }
- if((u_sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
- diep("socket");
- memset((char *)&udp_socket, 0, sizeof(udp_socket));
- udp_socket.sin_family = AF_INET;
- udp_socket.sin_port = htons(PORT);
- if (inet_aton((char *)&IP_ADDRESS, &udp_socket.sin_addr)==0) {
- fprintf(stderr, "inet_aton() failed\n");
- exit(1);
- }
- // memset(&p_data, sizeof(struct s_timestamp),0);
- memset(&p_data, p_size + sizeof(struct s_timestamp) ,0);
- p_data.padding_size = p_size;
- p_size = p_size + sizeof(struct s_timestamp);
- p_data.total_packets = NUM_PACKETS;
- p_data.padding_size = p_size - sizeof(struct s_timestamp);;
- if (sendto(u_sock, (void *)&p_data, p_size, 0, (struct sockaddr *)&udp_socket, socklen) == -1)
- diep("sendto()");
- for(counter = 0; counter < NUM_PACKETS; counter++) {
- memset(&p_data, p_size ,0);
- p_data.padding_size = p_size - sizeof(struct s_timestamp);
- printf("Sending packet %d\n", counter);
- ftime(&t_current);
- p_data.tseconds = t_current.time;
- p_data.tmilli = t_current.millitm;
- p_data.sequence=counter;
- p_data.total_packets = NUM_PACKETS;
- printf("Transmitting packet with sequence number %d\n",p_data.sequence);
- if (sendto(u_sock, (void *)&p_data, p_size, 0, (struct sockaddr *)&udp_socket, socklen) == -1)
- diep("sendto()");
- delay_time(PACKET_DELAY);
- }
- printf("Size of struct s_timestamp is %d\n",p_size);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement