gluon-neighbour-info.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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] [-t <sec>] -d <dest> -p <port> -i <if0> -r <request>");
  35. puts(" -p <int> UDP port");
  36. puts(" -d <ip6> 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 output as server-sent events");
  41. puts(" -h this help\n");
  42. }
  43. void getclock(struct timeval *tv) {
  44. struct timespec ts;
  45. clock_gettime(CLOCK_MONOTONIC, &ts);
  46. tv->tv_sec = ts.tv_sec;
  47. tv->tv_usec = ts.tv_nsec / 1000;
  48. }
  49. /* Assumes a and b are normalized */
  50. void tv_subtract (struct timeval *r, struct timeval *a, struct timeval *b) {
  51. r->tv_usec = a->tv_usec - b->tv_usec;
  52. r->tv_sec = a->tv_sec - b->tv_sec;
  53. if (r->tv_usec < 0) {
  54. r->tv_usec += 1000000;
  55. r->tv_sec -= 1;
  56. }
  57. }
  58. ssize_t recvtimeout(int socket, void *buffer, size_t length, int flags, struct timeval *timeout, struct timeval *offset) {
  59. struct timeval now, delta;
  60. ssize_t ret;
  61. setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, timeout, sizeof(*timeout));
  62. ret = recv(socket, buffer, length, flags);
  63. getclock(&now);
  64. tv_subtract(&delta, &now, offset);
  65. tv_subtract(timeout, timeout, &delta);
  66. return ret;
  67. }
  68. int request(const int sock, const struct sockaddr_in6 *client_addr, const char *request, bool sse, int timeout) {
  69. ssize_t ret;
  70. char buffer[8192];
  71. ret = sendto(sock, request, strlen(request), 0, (struct sockaddr *)client_addr, sizeof(struct sockaddr_in6));
  72. if (ret < 0) {
  73. perror("Error in sendto()");
  74. exit(EXIT_FAILURE);
  75. }
  76. struct timeval tv_timeout, tv_offset;
  77. tv_timeout.tv_sec = timeout;
  78. tv_timeout.tv_usec = 0;
  79. getclock(&tv_offset);
  80. while (1) {
  81. ret = recvtimeout(sock, buffer, sizeof(buffer), 0, &tv_timeout, &tv_offset);
  82. if (ret < 0)
  83. break;
  84. if (sse)
  85. fputs("event: neighbour\ndata: ", stdout);
  86. fwrite(buffer, sizeof(char), ret, stdout);
  87. if (sse)
  88. fputs("\n\n", stdout);
  89. else
  90. fputs("\n", stdout);
  91. fflush(stdout);
  92. }
  93. return 0;
  94. }
  95. int main(int argc, char **argv) {
  96. int sock;
  97. struct sockaddr_in6 client_addr = {};
  98. char *request_string = NULL;
  99. struct in6_addr mgroup_addr;
  100. sock = socket(PF_INET6, SOCK_DGRAM, 0);
  101. if (sock < 0) {
  102. perror("creating socket");
  103. exit(EXIT_FAILURE);
  104. }
  105. client_addr.sin6_family = AF_INET6;
  106. client_addr.sin6_addr = in6addr_any;
  107. opterr = 0;
  108. int port_set = 0;
  109. int destination_set = 0;
  110. int timeout = 3;
  111. bool sse = false;
  112. int c;
  113. while ((c = getopt(argc, argv, "p:d:r:i:t:sh")) != -1)
  114. switch (c) {
  115. case 'p':
  116. client_addr.sin6_port = htons(atoi(optarg));
  117. break;
  118. case 'd':
  119. if (!inet_pton(AF_INET6, optarg, &client_addr.sin6_addr)) {
  120. perror("Invalid IPv6 address. This message will probably confuse you");
  121. exit(EXIT_FAILURE);
  122. }
  123. break;
  124. case 'i':
  125. client_addr.sin6_scope_id = if_nametoindex(optarg);
  126. if (client_addr.sin6_scope_id == 0) {
  127. perror("Can not use interface");
  128. exit(EXIT_FAILURE);
  129. }
  130. break;
  131. case 'r':
  132. request_string = optarg;
  133. break;
  134. case 't':
  135. timeout = atoi(optarg);
  136. break;
  137. case 's':
  138. sse = true;
  139. break;
  140. case 'h':
  141. usage();
  142. exit(EXIT_SUCCESS);
  143. break;
  144. default:
  145. fprintf(stderr, "Invalid parameter %c ignored.\n", c);
  146. }
  147. if (request_string == NULL)
  148. error(EXIT_FAILURE, 0, "No request string supplied");
  149. if (sse)
  150. fputs("Content-Type: text/event-stream\n\n", stdout);
  151. request(sock, &client_addr, request_string, sse, timeout);
  152. if (sse)
  153. fputs("event: eot\ndata: null\n\n", stdout);
  154. return EXIT_SUCCESS;
  155. }