test_igmpv3_join.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. PIM for Quagga
  3. Copyright (C) 2008 Everton da Silva Marques
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING; if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  15. MA 02110-1301 USA
  16. $QuaggaId: $Format:%an, %ai, %h$ $
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <net/if.h>
  26. #include <arpa/inet.h>
  27. #include "pim_igmp_join.h"
  28. const char *prog_name = 0;
  29. static int iface_solve_index(const char *ifname)
  30. {
  31. struct if_nameindex *ini;
  32. ifindex_t ifindex = -1;
  33. int i;
  34. if (!ifname)
  35. return -1;
  36. ini = if_nameindex();
  37. if (!ini) {
  38. int err = errno;
  39. fprintf(stderr,
  40. "%s: interface=%s: failure solving index: errno=%d: %s\n",
  41. prog_name, ifname, err, strerror(err));
  42. errno = err;
  43. return -1;
  44. }
  45. for (i = 0; ini[i].if_index; ++i) {
  46. #if 0
  47. fprintf(stderr,
  48. "%s: interface=%s matching against local ifname=%s ifindex=%d\n",
  49. prog_name, ifname, ini[i].if_name, ini[i].if_index);
  50. #endif
  51. if (!strcmp(ini[i].if_name, ifname)) {
  52. ifindex = ini[i].if_index;
  53. break;
  54. }
  55. }
  56. if_freenameindex(ini);
  57. return ifindex;
  58. }
  59. int main(int argc, const char *argv[])
  60. {
  61. struct in_addr group_addr;
  62. struct in_addr source_addr;
  63. const char *ifname;
  64. const char *group;
  65. const char *source;
  66. ifindex_t ifindex;
  67. int result;
  68. int fd;
  69. prog_name = argv[0];
  70. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  71. if (fd < 0) {
  72. fprintf(stderr,
  73. "%s: could not create socket: socket(): errno=%d: %s\n",
  74. prog_name, errno, strerror(errno));
  75. exit(1);
  76. }
  77. if (argc != 4) {
  78. fprintf(stderr,
  79. "usage: %s interface group source\n"
  80. "example: %s eth0 232.1.1.1 1.1.1.1\n",
  81. prog_name, prog_name);
  82. exit(1);
  83. }
  84. ifname = argv[1];
  85. group = argv[2];
  86. source = argv[3];
  87. ifindex = iface_solve_index(ifname);
  88. if (ifindex < 0) {
  89. fprintf(stderr, "%s: could not find interface: %s\n",
  90. prog_name, ifname);
  91. exit(1);
  92. }
  93. result = inet_pton(AF_INET, group, &group_addr);
  94. if (result <= 0) {
  95. fprintf(stderr, "%s: bad group address: %s\n",
  96. prog_name, group);
  97. exit(1);
  98. }
  99. result = inet_pton(AF_INET, source, &source_addr);
  100. if (result <= 0) {
  101. fprintf(stderr, "%s: bad source address: %s\n",
  102. prog_name, source);
  103. exit(1);
  104. }
  105. result = pim_igmp_join_source(fd, ifindex, group_addr, source_addr);
  106. if (result) {
  107. fprintf(stderr,
  108. "%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s\n",
  109. prog_name, fd, group, source, ifindex, ifname,
  110. errno, strerror(errno));
  111. exit(1);
  112. }
  113. printf("%s: joined channel (S,G)=(%s,%s) on interface %s\n",
  114. prog_name, source, group, ifname);
  115. printf("%s: waiting...\n", prog_name);
  116. getchar();
  117. close(fd);
  118. printf("%s: left channel (S,G)=(%s,%s) on interface %s\n",
  119. prog_name, source, group, ifname);
  120. exit(0);
  121. }