gluon-neighbour-info.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /*
  33. * a destination address with interface identifier
  34. * a port
  35. * a requeststring
  36. * a timeout
  37. * a filter
  38. */
  39. void usage() {
  40. puts("Usage: gluon-neighbour-info [-h] [-s] -d <dest> -p <port> -i <if0> -r <request>");
  41. puts(" -p <int> UDP port");
  42. puts(" -d <ip6> multicast group, e.g. ff02:0:0:0:0:0:2:1001");
  43. puts(" -i <string> interface, e.g. eth0 ");
  44. puts(" -r <string> request, e.g. nodeinfo");
  45. puts(" -s output as server-sent events");
  46. puts(" -h this help\n");
  47. }
  48. int request(const int sock, const struct sockaddr_in6 *client_addr, const char *request, bool sse) {
  49. ssize_t ret;
  50. struct sockaddr_in6 node_addr;
  51. socklen_t nodelen;
  52. char buffer[8192];
  53. ret = sendto(sock, request, strlen(request), 0, (struct sockaddr *)client_addr, sizeof(struct sockaddr_in6));
  54. if (ret < 0) {
  55. perror("Error in sendto()");
  56. exit(EXIT_FAILURE);
  57. }
  58. nodelen = sizeof(client_addr);
  59. struct timeval tv;
  60. tv.tv_sec = 2;
  61. tv.tv_usec = 0;
  62. if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv)) < 0) {
  63. perror("Error");
  64. }
  65. while (1) {
  66. ret = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&node_addr, &nodelen);
  67. if (ret < 0)
  68. break;
  69. if (sse)
  70. fputs("event: neighbour\ndata: ", stdout);
  71. fwrite(buffer, sizeof(char), ret, stdout);
  72. if (sse)
  73. fputs("\n\n", stdout);
  74. fflush(stdout);
  75. }
  76. return 0;
  77. }
  78. int main(int argc, char **argv) {
  79. int sock;
  80. struct sockaddr_in6 client_addr = {};
  81. char *request_string = NULL;
  82. struct in6_addr mgroup_addr;
  83. sock = socket(PF_INET6, SOCK_DGRAM, 0);
  84. if (sock < 0) {
  85. perror("creating socket");
  86. exit(EXIT_FAILURE);
  87. }
  88. client_addr.sin6_family = AF_INET6;
  89. client_addr.sin6_addr = in6addr_any;
  90. opterr = 0;
  91. int port_set = 0;
  92. int destination_set = 0;
  93. bool sse = false;
  94. int c;
  95. while ((c = getopt(argc, argv, "p:d:r:i:sh")) != -1)
  96. switch (c) {
  97. case 'p':
  98. client_addr.sin6_port = htons(atoi(optarg));
  99. break;
  100. case 'd':
  101. if (!inet_pton(AF_INET6, optarg, &client_addr.sin6_addr)) {
  102. perror("Invalid IPv6 address. This message will probably confuse you");
  103. exit(EXIT_FAILURE);
  104. }
  105. break;
  106. case 'i':
  107. client_addr.sin6_scope_id = if_nametoindex(optarg);
  108. if (client_addr.sin6_scope_id == 0) {
  109. perror("Can not use interface");
  110. exit(EXIT_FAILURE);
  111. }
  112. break;
  113. case 'r':
  114. request_string = optarg;
  115. break;
  116. case 's':
  117. sse = true;
  118. break;
  119. case 'h':
  120. usage();
  121. exit(EXIT_SUCCESS);
  122. break;
  123. default:
  124. fprintf(stderr, "Invalid parameter %c ignored.\n", c);
  125. }
  126. if (request_string == NULL)
  127. error(EXIT_FAILURE, 0, "No request string supplied");
  128. if (sse)
  129. fputs("Content-Type: text/event-stream\n\n", stdout);
  130. request(sock, &client_addr, request_string, sse);
  131. if (sse)
  132. fputs("event: eot\ndata: null\n\n", stdout);
  133. return EXIT_SUCCESS;
  134. }