Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <fcntl.h>
  6. #include <netdb.h>
  7. #include <time.h>
  8. #include <poll.h>
  9.  
  10. #include <sys/timerfd.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <arpa/inet.h>
  16. #include <sys/time.h>
  17. #include <sys/poll.h>
  18. #include <openssl/md5.h>
  19.  
  20. #define buffer_gets 114688
  21.  
  22. typedef struct
  23. {
  24. unsigned char md5[MD5_DIGEST_LENGTH];
  25. } md5struct;
  26.  
  27. void printMD5(unsigned char* md5)
  28. {
  29. for(int i=0; i<MD5_DIGEST_LENGTH; i++){
  30. printf("%02x", md5[i]);
  31. }
  32. printf("\n");
  33. }
  34.  
  35. char* Parameters(int argc, char* argv[], int* count, float* r_delay, float* s_delay, long* port);
  36. int change_to_int(char* number);
  37. float change_to_float(char* number);
  38. int hostname_to_ip(char* hostname, char* ip);
  39.  
  40. struct itimerspec value;
  41.  
  42. int main(int argc, char* argv[])
  43. {
  44. int count = 0;
  45. long port = 0;
  46. float r_delay = 0;
  47. float s_delay = 0;
  48. char* address = NULL;
  49. address = Parameters(argc, argv, &count, &r_delay, &s_delay, &port);
  50.  
  51. //printf("Count = %d\nr_delay = %f\ns_delay = %f\nAddres & Port = %s:%ld\n", count, r_delay, s_delay, address, port);
  52. int len, fd, sfd;
  53. char send_buf[5] = "abcd";
  54. char recv_buf[buffer_gets];
  55. struct sockaddr_in addr;
  56. char server[25];
  57.  
  58. struct sockaddr_in* saddr = (struct sockaddr_in*)&addr;
  59. inet_ntop(AF_INET, &saddr->sin_addr, server, INET6_ADDRSTRLEN);
  60.  
  61. int createClock = timerfd_create(CLOCK_REALTIME, 0);
  62. if(createClock == -1)
  63. {
  64. printf("Problem with clock.\n");
  65. exit(1);
  66. }
  67. if(r_delay){
  68. value.it_value.tv_sec = (long)r_delay;
  69. value.it_value.tv_nsec = (r_delay-(float)value.it_value.tv_sec)*1000000000;
  70. value.it_interval.tv_sec = (long)r_delay;
  71. value.it_interval.tv_nsec = (r_delay-(float)value.it_value.tv_sec)*1000000000;
  72. }
  73. else{
  74. value.it_value.tv_sec = (long)s_delay;
  75. value.it_value.tv_nsec = (s_delay-(float)value.it_value.tv_sec)*1000000000;
  76. value.it_interval.tv_sec = 0;
  77. value.it_interval.tv_nsec = 0;
  78. }
  79. if(timerfd_settime(createClock, 0, &value, NULL) == -1)
  80. {
  81. printf("problem with timerfd_settime\n");
  82. exit(1);
  83. }
  84.  
  85. if((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  86. {
  87. printf("Problem with socket()\n");
  88. exit(-1);
  89. }
  90.  
  91. hostname_to_ip(address, server);
  92.  
  93. memset(&addr, 0, sizeof(addr));
  94. addr.sin_family = AF_INET;
  95. addr.sin_addr.s_addr = inet_addr(server);
  96. addr.sin_port = htons(port);
  97.  
  98. fd = connect(sfd, (struct sockaddr*)&addr, sizeof(addr));
  99. if(fd < 0)
  100. {
  101. printf("fd = %d", fd);
  102. printf("Problem with connect");
  103. exit(-1);
  104. }
  105.  
  106. struct pollfd fds[10];
  107. memset(fds, 0, sizeof(fds));
  108. fds[0].fd = sfd;
  109. fds[0].events = POLLIN;
  110. fds[1].fd = createClock;
  111. fds[1].events = POLLIN;
  112. int extra;
  113. int writeCount = 0;
  114. int readCount = 0;
  115. MD5_CTX CTX;
  116.  
  117. md5struct blocks[count];
  118. int timeout = (300000);
  119. while(1){
  120. extra = poll(fds, 2, timeout);
  121. if(extra < 0){
  122. printf("Problem with poll\n");
  123. break;
  124. }
  125. else if(extra == 0){
  126. printf("End a program\n");
  127. break;
  128. }
  129. if(fds[0].fd == sfd && fds[0].revents == POLLIN){
  130. MD5_Init(&CTX);
  131. int tsiz = 0;
  132. //Gettime tab[readCount].secoundTS
  133. for(int i=0; i<buffer_gets/1024; i++){
  134. len = recv(sfd, recv_buf+(i*1024), 1024, 0);
  135. tsiz += len;
  136. MD5_Update(&CTX, recv_buf+(i*1024), len);
  137. }
  138. //Gettime tab[readCount].thirdTS
  139. MD5_Final(blocks[readCount].md5, &CTX);
  140. readCount++;
  141. printf("%d -- %d\n", readCount, count);
  142. if(readCount != count){
  143. if(s_delay){
  144. if(timerfd_settime(createClock, 0, &value, NULL) == -1)
  145. {
  146. printf("problem with timerfd_settime\n");
  147. exit(1);
  148. }
  149. }
  150. }
  151. else{
  152. value.it_value.tv_sec = 0;
  153. value.it_value.tv_nsec = 0;
  154. value.it_interval.tv_sec = 0;
  155. value.it_interval.tv_nsec = 0;
  156. if(timerfd_settime(createClock, 0, &value, NULL) == -1)
  157. {
  158. printf("problem with timerfd_settime\n");
  159. exit(1);
  160. }
  161. }
  162. }
  163. else if(fds[1].fd == createClock && fds[1].revents == POLLIN){
  164. char buf[8];
  165. read(createClock, buf, 8);
  166.  
  167. len = send(sfd, send_buf, sizeof(send_buf), 0);
  168. if(len != sizeof(send_buf))
  169. printf("Problem with size of send\n");
  170. //Gettime tab[writeCount].firstTS
  171. writeCount++;
  172. if(writeCount >= count){
  173. fds[1].fd = -1;
  174. }
  175. }
  176. if(readCount == count){
  177. struct timespec ts;
  178. clock_gettime(CLOCK_REALTIME, &ts);
  179. long ts1 = ts.tv_sec;
  180. clock_gettime(CLOCK_MONOTONIC, &ts);
  181. long ts2 = ts.tv_sec;
  182. fprintf(stderr, "WallTime: %ld\nMonotonic: %ld\nPID of a process: %d\nAddress IP: %s\nMD5:\n", ts1, ts2, getpid(), server);
  183. for(int i=0; i<count; i++){
  184. printMD5(blocks[i].md5);
  185. }
  186. close(sfd);
  187. exit(0);
  188. }
  189. }
  190.  
  191. return 0;
  192. }
  193.  
  194. char* Parameters(int argc, char* argv[], int* count, float* r_delay, float* s_delay, long* port)
  195. {
  196. char* address;
  197. int c;
  198. char* count_pom = NULL;
  199. char* r_pom = NULL;
  200. char* s_pom = NULL;
  201.  
  202. while((c = getopt(argc, argv, "#:r:s:")) != -1)
  203. {
  204. switch(c)
  205. {
  206. case '#':
  207. count_pom = optarg;
  208. *count = change_to_int(count_pom);
  209. break;
  210. case 'r':
  211. r_pom = optarg;
  212. *r_delay = change_to_float(r_pom);
  213. break;
  214. case 's':
  215. s_pom = optarg;
  216. *s_delay = change_to_float(s_pom);
  217. break;
  218. }
  219. }
  220. if(*count == 0)
  221. {
  222. printf("-# is mandatory..\n");
  223. exit(1);
  224. }
  225. if((*r_delay == 0 && *s_delay == 0) || (*r_delay != 0 && *s_delay != 0))
  226. {
  227. printf("You can\'t use -s and -r together\n");
  228. exit(1);
  229. }
  230. if(optind != (argc - 1))
  231. {
  232. printf("Not enough parameters\n");
  233. exit(1);
  234. }
  235.  
  236. char* pEnd;
  237. char* tmp = argv[optind];
  238. address = strtok(tmp, "[]:");
  239. tmp = strtok(NULL, "[]:");
  240. if(tmp == NULL)
  241. {
  242. *port = strtol(address, &pEnd, 10);
  243. if(*port == 0)
  244. {
  245. printf("Wrong port...\n");
  246. exit(1);
  247. }
  248. strcpy(address, "localhost");
  249. }
  250. else
  251. {
  252. *port = strtol(tmp, &pEnd, 10);
  253. if(*port == 0)
  254. {
  255. printf("Wrong port...\n");
  256. exit(1);
  257. }
  258. }
  259.  
  260. return address;
  261. }
  262.  
  263. int change_to_int(char* tmp)
  264. {
  265. char tmp_copy[10];
  266. strcpy(tmp_copy, tmp);
  267. int l1 = 0;
  268. int l2 = 0;
  269. char* pEnd;
  270.  
  271. char* tmp1 = strtok(tmp_copy, ":");
  272. char* tmp2 = strtok(NULL, ":");
  273. char* tmp3 = strtok(NULL, ":");
  274.  
  275. l1 = strtol(tmp1, &pEnd, 0);
  276. if(tmp3 != NULL)
  277. {
  278. printf("L1:L2:L3.\tYou can\'t have more than two parameters here.\n");
  279. exit(1);
  280. }
  281. if(l1 <= 0)
  282. {
  283. printf("L1 in -#.\tThis parameter can\'t be lower than 0.\n");
  284. exit(1);
  285. }
  286. if(tmp2 == NULL)
  287. return l1;
  288. else
  289. {
  290. l2 = strtol(tmp2, &pEnd, 0);
  291. if(l2 <= 0)
  292. {
  293. printf("L2 in -#.\tThis parameter can\'t be lower than 0.\n");
  294. exit(1);
  295. }
  296.  
  297. if(l2 < l1)
  298. {
  299. printf("L1 and L2 in -#.\tFirst parameter has to be lower than the second one.\n");
  300. exit(1);
  301. }
  302. if(l1 == l2)
  303. return l1;
  304. int t;
  305. time_t tt;
  306. t = time(&tt);
  307. srand(t);
  308. return (rand() % (l2 - l1) + 1) + l1;
  309.  
  310. }
  311. }
  312.  
  313. float change_to_float(char* tmp)
  314. {
  315. char tmp_copy[50];
  316. strcpy(tmp_copy, tmp);
  317. float l1 = 0;
  318. float l2 = 0;
  319.  
  320. char* pEnd;
  321.  
  322. char* tmp1 = strtok(tmp_copy, ":");
  323. char* tmp2 = strtok(NULL, ":");
  324. char* tmp3 = strtok(NULL, ":");
  325.  
  326. l1 = strtof(tmp1, &pEnd);
  327. if(tmp3 != NULL)
  328. {
  329. printf("Problem with tmp3\n");
  330. exit(1);
  331. }
  332. if(l1 <= 0)
  333. {
  334. printf("L1 in -r or -s.\tThis parameter has to be bigger than 0.\n");
  335. exit(1);
  336. }
  337. if(tmp2 == NULL)
  338. return l1;
  339. else
  340. {
  341. l2 = strtof(tmp2, &pEnd);
  342. if(l2 <= 0)
  343. {
  344. printf("L2 in -r or -s.\tThis parameter has to be bigger than 0.\n");
  345. exit(1);
  346. }
  347.  
  348. int tim;
  349. time_t tt;
  350. tim = time(&tt);
  351. srand(tim);
  352. if(l1 == l2)
  353. return l1;
  354. else if(l1 > l2)
  355. {
  356. printf("L1 and L2 in -r or -s.\tFirst parameter has to be lower than the second one.\n");
  357. exit(1);
  358. }
  359. else
  360. return (((float)rand() / RAND_MAX) * (l2 - l1 + 1) + l1);
  361. }
  362. return -1;
  363. }
  364. int hostname_to_ip(char* hostname, char* ip)
  365. {
  366. struct hostent *he;
  367. struct in_addr **addr_list;
  368.  
  369. if((he = gethostbyname(hostname)) == NULL)
  370. {
  371. printf("Problem with gethostbyname\n");
  372. exit(1);
  373. }
  374. addr_list = (struct in_addr**)he->h_addr_list;
  375. for(int i = 0; addr_list[i] != NULL; i++)
  376. {
  377. strcpy(ip, inet_ntoa(*addr_list[i]));
  378. return 0;
  379. }
  380. return 1;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement