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