gluon-neighbour-info.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. Copyright (c) 2014, Nils Schneider <nils@nilsschneider.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <stdbool.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <net/if.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #include <string.h>
  32. #include <time.h>
  33. void usage() {
  34. puts("Usage: gluon-neighbour-info [-h] [-s] [-l] [-c <count>] [-t <sec>] -d <dest> -p <port> -i <if0> -r <request>");
  35. puts(" -p <int> UDP port");
  36. puts(" -d <ip6> destination address (unicast ip6 or multicast group, e.g. ff02:0:0:0:0:0:2:1001)");
  37. puts(" -i <string> interface, e.g. eth0 ");
  38. puts(" -r <string> request, e.g. nodeinfo");
  39. puts(" -t <sec> timeout in seconds (default: 3)");
  40. puts(" -s <event> output as server-sent events of type <event>");
  41. puts(" or without type if <event> is the empty string");
  42. puts(" -c <count> only wait for at most <count> replies");
  43. puts(" -l after timeout (or <count> replies if -c is given),");
  44. puts(" send another request and loop forever");
  45. puts(" -h this help\n");
  46. }
  47. void getclock(struct timeval *tv) {
  48. struct timespec ts;
  49. clock_gettime(CLOCK_MONOTONIC, &ts);
  50. tv->tv_sec = ts.tv_sec;
  51. tv->tv_usec = ts.tv_nsec / 1000;
  52. }
  53. /* Assumes a and b are normalized */
  54. void tv_subtract (struct timeval *r, const struct timeval *a, const struct timeval *b) {
  55. r->tv_usec = a->tv_usec - b->tv_usec;
  56. r->tv_sec = a->tv_sec - b->tv_sec;
  57. if (r->tv_usec < 0) {
  58. r->tv_usec += 1000000;
  59. r->tv_sec -= 1;
  60. }
  61. }
  62. ssize_t recvtimeout(int socket, void *buffer, size_t length, int flags, const struct timeval *timeout) {
  63. struct timeval now, timeout_left;
  64. getclock(&now);
  65. tv_subtract(&timeout_left, timeout, &now);
  66. if (timeout_left.tv_sec < 0)
  67. return -1;
  68. setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout_left, sizeof(timeout_left));
  69. return recv(socket, buffer, length, flags);
  70. }
  71. int request(const int sock, const struct sockaddr_in6 *client_addr, const char *request, const char *sse, double timeout, unsigned int max_count) {
  72. ssize_t ret;
  73. char buffer[8192];
  74. unsigned int count = 0;
  75. ret = sendto(sock, request, strlen(request), 0, (struct sockaddr *)client_addr, sizeof(struct sockaddr_in6));
  76. if (ret < 0) {
  77. perror("Error in sendto()");
  78. exit(EXIT_FAILURE);
  79. }
  80. struct timeval tv_timeout;
  81. getclock(&tv_timeout);
  82. tv_timeout.tv_sec += (int) timeout;
  83. tv_timeout.tv_usec += ((int) (timeout * 1000000)) % 1000000;
  84. if (tv_timeout.tv_usec >= 1000000) {
  85. tv_timeout.tv_usec -= 1000000;
  86. tv_timeout.tv_sec += 1;
  87. }
  88. do {
  89. ret = recvtimeout(sock, buffer, sizeof(buffer), 0, &tv_timeout);
  90. if (ret < 0)
  91. break;
  92. if (sse) {
  93. if (sse[0] != '\0')
  94. fprintf(stdout, "event: %s\n", sse);
  95. fputs("data: ", stdout);
  96. }
  97. fwrite(buffer, sizeof(char), ret, stdout);
  98. if (sse)
  99. fputs("\n\n", stdout);
  100. else
  101. fputs("\n", stdout);
  102. fflush(stdout);
  103. count++;
  104. } while (max_count == 0 || count < max_count);
  105. if ((max_count == 0 && count == 0) || count < max_count)
  106. return EXIT_FAILURE;
  107. else
  108. return EXIT_SUCCESS;
  109. }
  110. int main(int argc, char **argv) {
  111. int sock;
  112. struct sockaddr_in6 client_addr = {};
  113. char *request_string = NULL;
  114. sock = socket(PF_INET6, SOCK_DGRAM, 0);
  115. if (sock < 0) {
  116. perror("creating socket");
  117. exit(EXIT_FAILURE);
  118. }
  119. client_addr.sin6_family = AF_INET6;
  120. opterr = 0;
  121. int max_count = 0;
  122. double timeout = 3.0;
  123. char *sse = NULL;
  124. bool loop = false;
  125. int ret = false;
  126. int c;
  127. while ((c = getopt(argc, argv, "p:d:r:i:t:s:c:lh")) != -1)
  128. switch (c) {
  129. case 'p':
  130. client_addr.sin6_port = htons(atoi(optarg));
  131. break;
  132. case 'd':
  133. if (!inet_pton(AF_INET6, optarg, &client_addr.sin6_addr)) {
  134. perror("Invalid IPv6 address. This message will probably confuse you");
  135. exit(EXIT_FAILURE);
  136. }
  137. break;
  138. case 'i':
  139. client_addr.sin6_scope_id = if_nametoindex(optarg);
  140. if (client_addr.sin6_scope_id == 0) {
  141. perror("Can not use interface");
  142. exit(EXIT_FAILURE);
  143. }
  144. break;
  145. case 'r':
  146. request_string = optarg;
  147. break;
  148. case 't':
  149. timeout = atof(optarg);
  150. if (timeout < 0) {
  151. perror("Negative timeout not supported");
  152. exit(EXIT_FAILURE);
  153. }
  154. break;
  155. case 's':
  156. sse = optarg;
  157. break;
  158. case 'l':
  159. loop = true;
  160. break;
  161. case 'c':
  162. max_count = atoi(optarg);
  163. if (max_count < 0) {
  164. perror("Negative count not supported");
  165. exit(EXIT_FAILURE);
  166. }
  167. break;
  168. case 'h':
  169. usage();
  170. exit(EXIT_SUCCESS);
  171. break;
  172. default:
  173. fprintf(stderr, "Invalid parameter %c ignored.\n", c);
  174. }
  175. if (request_string == NULL) {
  176. fprintf(stderr, "No request string supplied\n");
  177. exit(EXIT_FAILURE);
  178. }
  179. if (client_addr.sin6_port == htons(0)) {
  180. fprintf(stderr, "No port supplied\n");
  181. exit(EXIT_FAILURE);
  182. }
  183. if (IN6_IS_ADDR_UNSPECIFIED(&client_addr.sin6_addr)) {
  184. fprintf(stderr, "No destination address supplied\n");
  185. exit(EXIT_FAILURE);
  186. }
  187. if (sse) {
  188. fputs("Content-Type: text/event-stream\n\n", stdout);
  189. fflush(stdout);
  190. }
  191. do {
  192. ret = request(sock, &client_addr, request_string, sse, timeout, max_count);
  193. } while(loop);
  194. if (sse)
  195. fputs("event: eot\ndata: null\n\n", stdout);
  196. return ret;
  197. }