]> git.proxmox.com Git - mirror_frr.git/blob - pimd/test_igmpv3_join.c
pimd: Remove unnecessary QuaggaId
[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
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 MA 02110-1301 USA
19
20 */
21
22 #include <zebra.h>
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <net/if.h>
32 #include <arpa/inet.h>
33
34 #include "if.h"
35 #include "pim_igmp_join.h"
36
37 const char *prog_name = 0;
38
39 static int iface_solve_index(const char *ifname)
40 {
41 struct if_nameindex *ini;
42 ifindex_t ifindex = -1;
43 int i;
44
45 if (!ifname)
46 return -1;
47
48 ini = if_nameindex();
49 if (!ini) {
50 int err = errno;
51 fprintf(stderr,
52 "%s: interface=%s: failure solving index: errno=%d: %s\n",
53 prog_name, ifname, err, strerror(err));
54 errno = err;
55 return -1;
56 }
57
58 for (i = 0; ini[i].if_index; ++i) {
59 #if 0
60 fprintf(stderr,
61 "%s: interface=%s matching against local ifname=%s ifindex=%d\n",
62 prog_name, ifname, ini[i].if_name, ini[i].if_index);
63 #endif
64 if (!strcmp(ini[i].if_name, ifname)) {
65 ifindex = ini[i].if_index;
66 break;
67 }
68 }
69
70 if_freenameindex(ini);
71
72 return ifindex;
73 }
74
75 int main(int argc, const char *argv[])
76 {
77 struct in_addr group_addr;
78 struct in_addr source_addr;
79 const char *ifname;
80 const char *group;
81 const char *source;
82 ifindex_t ifindex;
83 int result;
84 int fd;
85
86 prog_name = argv[0];
87
88 fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
89 if (fd < 0) {
90 fprintf(stderr,
91 "%s: could not create socket: socket(): errno=%d: %s\n",
92 prog_name, errno, strerror(errno));
93 exit(1);
94 }
95
96 if (argc != 4) {
97 fprintf(stderr,
98 "usage: %s interface group source\n"
99 "example: %s eth0 232.1.1.1 1.1.1.1\n",
100 prog_name, prog_name);
101 exit(1);
102 }
103
104 ifname = argv[1];
105 group = argv[2];
106 source = argv[3];
107
108 ifindex = iface_solve_index(ifname);
109 if (ifindex < 0) {
110 fprintf(stderr, "%s: could not find interface: %s\n",
111 prog_name, ifname);
112 exit(1);
113 }
114
115 result = inet_pton(AF_INET, group, &group_addr);
116 if (result <= 0) {
117 fprintf(stderr, "%s: bad group address: %s\n",
118 prog_name, group);
119 exit(1);
120 }
121
122 result = inet_pton(AF_INET, source, &source_addr);
123 if (result <= 0) {
124 fprintf(stderr, "%s: bad source address: %s\n",
125 prog_name, source);
126 exit(1);
127 }
128
129 result = pim_igmp_join_source(fd, ifindex, group_addr, source_addr);
130 if (result) {
131 fprintf(stderr,
132 "%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s\n",
133 prog_name, fd, group, source, ifindex, ifname,
134 errno, strerror(errno));
135 exit(1);
136 }
137
138 printf("%s: joined channel (S,G)=(%s,%s) on interface %s\n",
139 prog_name, source, group, ifname);
140
141 printf("%s: waiting...\n", prog_name);
142
143 getchar();
144
145 close(fd);
146
147 printf("%s: left channel (S,G)=(%s,%s) on interface %s\n",
148 prog_name, source, group, ifname);
149
150 exit(0);
151 }