Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <netdb.h>
- #include <time.h>
- #include <poll.h>
- #include <sys/timerfd.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <arpa/inet.h>
- #include <sys/time.h>
- #include <sys/poll.h>
- #include <openssl/md5.h>
- #define buffer_gets 114688
- typedef struct
- {
- unsigned char md5[MD5_DIGEST_LENGTH];
- } md5struct;
- void printMD5(unsigned char* md5)
- {
- for(int i=0; i<MD5_DIGEST_LENGTH; i++){
- printf("%02x", md5[i]);
- }
- printf("\n");
- }
- char* Parameters(int argc, char* argv[], int* count, float* r_delay, float* s_delay, long* port);
- int change_to_int(char* number);
- float change_to_float(char* number);
- int hostname_to_ip(char* hostname, char* ip);
- struct itimerspec value;
- int main(int argc, char* argv[])
- {
- int count = 0;
- long port = 0;
- float r_delay = 0;
- float s_delay = 0;
- char* address = NULL;
- address = Parameters(argc, argv, &count, &r_delay, &s_delay, &port);
- //printf("Count = %d\nr_delay = %f\ns_delay = %f\nAddres & Port = %s:%ld\n", count, r_delay, s_delay, address, port);
- int len, fd, sfd;
- char send_buf[5] = "abcd";
- char recv_buf[buffer_gets];
- struct sockaddr_in addr;
- char server[25];
- struct sockaddr_in* saddr = (struct sockaddr_in*)&addr;
- inet_ntop(AF_INET, &saddr->sin_addr, server, INET6_ADDRSTRLEN);
- int createClock = timerfd_create(CLOCK_REALTIME, 0);
- if(createClock == -1)
- {
- printf("Problem with clock.\n");
- exit(1);
- }
- if(r_delay){
- value.it_value.tv_sec = (long)r_delay;
- value.it_value.tv_nsec = (r_delay-(float)value.it_value.tv_sec)*1000000000;
- value.it_interval.tv_sec = (long)r_delay;
- value.it_interval.tv_nsec = (r_delay-(float)value.it_value.tv_sec)*1000000000;
- }
- else{
- value.it_value.tv_sec = (long)s_delay;
- value.it_value.tv_nsec = (s_delay-(float)value.it_value.tv_sec)*1000000000;
- value.it_interval.tv_sec = 0;
- value.it_interval.tv_nsec = 0;
- }
- if(timerfd_settime(createClock, 0, &value, NULL) == -1)
- {
- printf("problem with timerfd_settime\n");
- exit(1);
- }
- if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
- {
- printf("Problem with socket()\n");
- exit(-1);
- }
- hostname_to_ip(address, server);
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = inet_addr(server);
- addr.sin_port = htons(port);
- fd = connect(sfd, (struct sockaddr*)&addr, sizeof(addr));
- if(fd < 0)
- {
- printf("fd = %d", fd);
- printf("Problem with connect");
- exit(-1);
- }
- struct pollfd fds[10];
- memset(fds, 0, sizeof(fds));
- fds[0].fd = sfd;
- fds[0].events = POLLIN;
- fds[1].fd = createClock;
- fds[1].events = POLLIN;
- int extra;
- int writeCount = 0;
- int readCount = 0;
- MD5_CTX CTX;
- md5struct blocks[count];
- int timeout = (300000);
- while(1){
- extra = poll(fds, 2, timeout);
- if(extra < 0){
- printf("Problem with poll\n");
- break;
- }
- else if(extra == 0){
- printf("End a program\n");
- break;
- }
- if(fds[0].fd == sfd && fds[0].revents == POLLIN){
- MD5_Init(&CTX);
- int tsiz = 0;
- //Gettime tab[readCount].secoundTS
- for(int i=0; i<buffer_gets/1024; i++){
- len = recv(sfd, recv_buf+(i*1024), 1024, 0);
- tsiz += len;
- MD5_Update(&CTX, recv_buf+(i*1024), len);
- }
- //Gettime tab[readCount].thirdTS
- MD5_Final(blocks[readCount].md5, &CTX);
- readCount++;
- printf("%d -- %d\n", readCount, count);
- if(readCount != count){
- if(s_delay){
- if(timerfd_settime(createClock, 0, &value, NULL) == -1)
- {
- printf("problem with timerfd_settime\n");
- exit(1);
- }
- }
- }
- else{
- value.it_value.tv_sec = 0;
- value.it_value.tv_nsec = 0;
- value.it_interval.tv_sec = 0;
- value.it_interval.tv_nsec = 0;
- if(timerfd_settime(createClock, 0, &value, NULL) == -1)
- {
- printf("problem with timerfd_settime\n");
- exit(1);
- }
- }
- }
- else if(fds[1].fd == createClock && fds[1].revents == POLLIN){
- char buf[8];
- read(createClock, buf, 8);
- len = send(sfd, send_buf, sizeof(send_buf), 0);
- if(len != sizeof(send_buf))
- printf("Problem with size of send\n");
- //Gettime tab[writeCount].firstTS
- writeCount++;
- if(writeCount >= count){
- fds[1].fd = -1;
- }
- }
- if(readCount == count){
- struct timespec ts;
- clock_gettime(CLOCK_REALTIME, &ts);
- long ts1 = ts.tv_sec;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- long ts2 = ts.tv_sec;
- fprintf(stderr, "WallTime: %ld\nMonotonic: %ld\nPID of a process: %d\nAddress IP: %s\nMD5:\n", ts1, ts2, getpid(), server);
- for(int i=0; i<count; i++){
- printMD5(blocks[i].md5);
- }
- close(sfd);
- exit(0);
- }
- }
- return 0;
- }
- char* Parameters(int argc, char* argv[], int* count, float* r_delay, float* s_delay, long* port)
- {
- char* address;
- int c;
- char* count_pom = NULL;
- char* r_pom = NULL;
- char* s_pom = NULL;
- while((c = getopt(argc, argv, "#:r:s:")) != -1)
- {
- switch(c)
- {
- case '#':
- count_pom = optarg;
- *count = change_to_int(count_pom);
- break;
- case 'r':
- r_pom = optarg;
- *r_delay = change_to_float(r_pom);
- break;
- case 's':
- s_pom = optarg;
- *s_delay = change_to_float(s_pom);
- break;
- }
- }
- if(*count == 0)
- {
- printf("-# is mandatory..\n");
- exit(1);
- }
- if((*r_delay == 0 && *s_delay == 0) || (*r_delay != 0 && *s_delay != 0))
- {
- printf("You can\'t use -s and -r together\n");
- exit(1);
- }
- if(optind != (argc - 1))
- {
- printf("Not enough parameters\n");
- exit(1);
- }
- char* pEnd;
- char* tmp = argv[optind];
- address = strtok(tmp, "[]:");
- tmp = strtok(NULL, "[]:");
- if(tmp == NULL)
- {
- *port = strtol(address, &pEnd, 10);
- if(*port == 0)
- {
- printf("Wrong port...\n");
- exit(1);
- }
- strcpy(address, "localhost");
- }
- else
- {
- *port = strtol(tmp, &pEnd, 10);
- if(*port == 0)
- {
- printf("Wrong port...\n");
- exit(1);
- }
- }
- return address;
- }
- int change_to_int(char* tmp)
- {
- char tmp_copy[10];
- strcpy(tmp_copy, tmp);
- int l1 = 0;
- int l2 = 0;
- char* pEnd;
- char* tmp1 = strtok(tmp_copy, ":");
- char* tmp2 = strtok(NULL, ":");
- char* tmp3 = strtok(NULL, ":");
- l1 = strtol(tmp1, &pEnd, 0);
- if(tmp3 != NULL)
- {
- printf("L1:L2:L3.\tYou can\'t have more than two parameters here.\n");
- exit(1);
- }
- if(l1 <= 0)
- {
- printf("L1 in -#.\tThis parameter can\'t be lower than 0.\n");
- exit(1);
- }
- if(tmp2 == NULL)
- return l1;
- else
- {
- l2 = strtol(tmp2, &pEnd, 0);
- if(l2 <= 0)
- {
- printf("L2 in -#.\tThis parameter can\'t be lower than 0.\n");
- exit(1);
- }
- if(l2 < l1)
- {
- printf("L1 and L2 in -#.\tFirst parameter has to be lower than the second one.\n");
- exit(1);
- }
- if(l1 == l2)
- return l1;
- int t;
- time_t tt;
- t = time(&tt);
- srand(t);
- return (rand() % (l2 - l1) + 1) + l1;
- }
- }
- float change_to_float(char* tmp)
- {
- char tmp_copy[50];
- strcpy(tmp_copy, tmp);
- float l1 = 0;
- float l2 = 0;
- char* pEnd;
- char* tmp1 = strtok(tmp_copy, ":");
- char* tmp2 = strtok(NULL, ":");
- char* tmp3 = strtok(NULL, ":");
- l1 = strtof(tmp1, &pEnd);
- if(tmp3 != NULL)
- {
- printf("Problem with tmp3\n");
- exit(1);
- }
- if(l1 <= 0)
- {
- printf("L1 in -r or -s.\tThis parameter has to be bigger than 0.\n");
- exit(1);
- }
- if(tmp2 == NULL)
- return l1;
- else
- {
- l2 = strtof(tmp2, &pEnd);
- if(l2 <= 0)
- {
- printf("L2 in -r or -s.\tThis parameter has to be bigger than 0.\n");
- exit(1);
- }
- int tim;
- time_t tt;
- tim = time(&tt);
- srand(tim);
- if(l1 == l2)
- return l1;
- else if(l1 > l2)
- {
- printf("L1 and L2 in -r or -s.\tFirst parameter has to be lower than the second one.\n");
- exit(1);
- }
- else
- return (((float)rand() / RAND_MAX) * (l2 - l1 + 1) + l1);
- }
- return -1;
- }
- int hostname_to_ip(char* hostname, char* ip)
- {
- struct hostent *he;
- struct in_addr **addr_list;
- if((he = gethostbyname(hostname)) == NULL)
- {
- printf("Problem with gethostbyname\n");
- exit(1);
- }
- addr_list = (struct in_addr**)he->h_addr_list;
- for(int i = 0; addr_list[i] != NULL; i++)
- {
- strcpy(ip, inet_ntoa(*addr_list[i]));
- return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement