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