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