main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include <stdio.h>
  2. #include <arpa/inet.h>
  3. #include <assert.h>
  4. #include <netdb.h> /* getprotobyname */
  5. #include <netinet/in.h>
  6. #include <stdbool.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/socket.h>
  11. #include <sys/time.h>
  12. #include <unistd.h>
  13. #define HOST_DNS_NAME "ifconfig.me"
  14. #define LENGHT_OF_SUBTRACT_STRING 14
  15. #define _XOPEN_SOURCE 255
  16. #define TIMEOUT_SECONDS 0.01
  17. #define DEBUG_MODE false
  18. #define ERROR_MODE false
  19. #define TIME_IN_SLEEP 0.01
  20. #define TIME_LIVE_LOOP_SLEEP 0.01
  21. void printStringWithOffset(const char* str, int start, int end) {
  22. int i = 0;
  23. for (i = start; i < end; i++) {
  24. write(STDERR_FILENO, &str[i], 1);
  25. }
  26. write(STDERR_FILENO, "\n", 2);
  27. }
  28. int
  29. request(char *hostname_arg) {
  30. char buffer[BUFSIZ];
  31. enum CONSTEXPR { MAX_REQUEST_LEN = 1024 };
  32. char request[MAX_REQUEST_LEN];
  33. char request_template[] = "GET / HTTP/1.1\r\nHost: %s\r\n\r\n";
  34. struct protoent *protoent;
  35. char *hostname = hostname_arg; // HOST_DNS_NAME
  36. in_addr_t in_addr;
  37. int request_len;
  38. int socket_file_descriptor;
  39. ssize_t nbytes_total, nbytes_last;
  40. struct hostent *hostent;
  41. struct sockaddr_in sockaddr_in;
  42. unsigned short server_port = 80;
  43. #if 0
  44. if (argc > 1)
  45. hostname = argv[1];
  46. if (argc > 2)
  47. server_port = strtoul(argv[2], NULL, 10);
  48. #endif
  49. request_len = snprintf(request, MAX_REQUEST_LEN, request_template, hostname);
  50. if (request_len >= MAX_REQUEST_LEN) {
  51. #if ERROR_MODE
  52. fprintf(stderr, "request length large: %d\n", request_len);
  53. #endif
  54. exit(EXIT_FAILURE);
  55. }
  56. // Set timeout for the socket
  57. struct timeval timeout;
  58. timeout.tv_sec = TIMEOUT_SECONDS;
  59. timeout.tv_usec = 0;
  60. setsockopt(socket_file_descriptor, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
  61. sizeof(timeout));
  62. /* Build the socket. */
  63. protoent = getprotobyname("tcp");
  64. if (protoent == NULL) {
  65. perror("getprotobyname");
  66. return -3;
  67. // exit(EXIT_FAILURE);
  68. }
  69. socket_file_descriptor = socket(AF_INET, SOCK_STREAM, protoent->p_proto);
  70. if (socket_file_descriptor == -1) {
  71. perror("socket");
  72. return -3;
  73. // exit(EXIT_FAILURE);
  74. }
  75. /* Build the address. */
  76. hostent = gethostbyname(hostname);
  77. if (hostent == NULL) {
  78. #if ERROR_MODE
  79. fprintf(stderr, "error: gethostbyname(\"%s\")\n", hostname);
  80. #endif
  81. return -3;
  82. // exit(EXIT_FAILURE);
  83. }
  84. in_addr = inet_addr(inet_ntoa(*(struct in_addr *)*(hostent->h_addr_list)));
  85. if (in_addr == (in_addr_t)-1) {
  86. #if ERROR_MODE
  87. fprintf(stderr, "error: inet_addr(\"%s\")\n", *(hostent->h_addr_list));
  88. #endif
  89. return -3;
  90. // exit(EXIT_FAILURE);
  91. }
  92. sockaddr_in.sin_addr.s_addr = in_addr;
  93. sockaddr_in.sin_family = AF_INET;
  94. sockaddr_in.sin_port = htons(server_port);
  95. /* Actually connect. */
  96. if (connect(socket_file_descriptor, (struct sockaddr *)&sockaddr_in,
  97. sizeof(sockaddr_in)) == -1) {
  98. perror("connect");
  99. return -3;
  100. // exit(EXIT_FAILURE);
  101. }
  102. /* Send HTTP request. */
  103. nbytes_total = 0;
  104. while (nbytes_total < request_len) {
  105. nbytes_last = write(socket_file_descriptor, request + nbytes_total,
  106. request_len - nbytes_total);
  107. if (nbytes_last == -1) {
  108. perror("write");
  109. return -3;
  110. // exit(EXIT_FAILURE);
  111. }
  112. nbytes_total += nbytes_last;
  113. }
  114. /* Read the response. */
  115. #if DEBUG_MODE
  116. fprintf(stderr, "debug: before first read\n");
  117. #endif
  118. // while ((nbytes_total = read(socket_file_descriptor, buffer, BUFSIZ)) > 0) {
  119. while (true) {
  120. fd_set read_fds;
  121. FD_ZERO(&read_fds);
  122. FD_SET(socket_file_descriptor, &read_fds);
  123. int select_result =
  124. select(socket_file_descriptor + 1, &read_fds, NULL, NULL, NULL);
  125. if (select_result == -1) {
  126. perror("Error in select");
  127. break;
  128. } else if (select_result == 0) {
  129. // Timeout or no data available
  130. break;
  131. }
  132. // Read from the socket
  133. nbytes_total = read(socket_file_descriptor, buffer, BUFSIZ);
  134. if (nbytes_total > 0) {
  135. #if DEBUG_MODE
  136. fprintf(stderr, "debug: after a read\n");
  137. #endif
  138. // write(STDOUT_FILENO, buffer, nbytes_total);
  139. printStringWithOffset(buffer, (nbytes_total-LENGHT_OF_SUBTRACT_STRING),nbytes_total);
  140. write(STDOUT_FILENO, "\n", 2);
  141. break;
  142. } else if (nbytes_total == 0) {
  143. // End of file reached
  144. break;
  145. } else {
  146. #if ERROR_MODE
  147. perror("Failed to read from the socket");
  148. #endif
  149. break;
  150. }
  151. // fprintf(stderr, "debug: after a read\n");
  152. // write(STDOUT_FILENO, buffer, nbytes_total);
  153. }
  154. #if DEBUG_MODE
  155. fprintf(stderr, "debug: after last read\n");
  156. #endif
  157. if (nbytes_total == -1) {
  158. perror("read");
  159. return 0;
  160. // exit(EXIT_FAILURE);
  161. }
  162. close(socket_file_descriptor);
  163. return 0;
  164. // exit(EXIT_SUCCESS);
  165. }
  166. int
  167. in_live_loop() {
  168. int i = 0;
  169. while(true) {
  170. request(HOST_DNS_NAME);
  171. sleep((unsigned int)TIME_IN_SLEEP);
  172. }
  173. return 0;
  174. }
  175. int
  176. live_loop() {
  177. int i = 0;
  178. for (i = 0; i < 10; i++) {
  179. request(HOST_DNS_NAME);
  180. sleep((unsigned int)TIME_LIVE_LOOP_SLEEP);
  181. }
  182. return 0;
  183. }
  184. int
  185. main () {
  186. in_live_loop();
  187. // live_loop();
  188. return 0;
  189. }