]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
12e41d03 | 2 | /* |
896014f4 DL |
3 | * PIM for Quagga |
4 | * Copyright (C) 2008 Everton da Silva Marques | |
896014f4 | 5 | */ |
12e41d03 | 6 | |
8f500a1c RW |
7 | #include <zebra.h> |
8 | ||
12e41d03 DL |
9 | #include <stdlib.h> |
10 | #include <stdio.h> | |
11 | #include <errno.h> | |
12 | #include <string.h> | |
13 | #include <unistd.h> | |
14 | #include <sys/types.h> | |
15 | #include <sys/socket.h> | |
16 | #include <net/if.h> | |
17 | #include <arpa/inet.h> | |
18 | ||
b892f1dd | 19 | #include "if.h" |
12e41d03 DL |
20 | #include "pim_igmp_join.h" |
21 | ||
22 | const char *prog_name = 0; | |
23 | ||
24 | static int iface_solve_index(const char *ifname) | |
25 | { | |
d62a17ae | 26 | struct if_nameindex *ini; |
27 | ifindex_t ifindex = -1; | |
28 | int i; | |
29 | ||
30 | if (!ifname) | |
31 | return -1; | |
32 | ||
33 | ini = if_nameindex(); | |
34 | if (!ini) { | |
35 | int err = errno; | |
36 | fprintf(stderr, | |
37 | "%s: interface=%s: failure solving index: errno=%d: %s\n", | |
38 | prog_name, ifname, err, strerror(err)); | |
39 | errno = err; | |
40 | return -1; | |
41 | } | |
42 | ||
43 | for (i = 0; ini[i].if_index; ++i) { | |
d62a17ae | 44 | if (!strcmp(ini[i].if_name, ifname)) { |
45 | ifindex = ini[i].if_index; | |
46 | break; | |
47 | } | |
48 | } | |
12e41d03 | 49 | |
d62a17ae | 50 | if_freenameindex(ini); |
12e41d03 | 51 | |
d62a17ae | 52 | return ifindex; |
12e41d03 DL |
53 | } |
54 | ||
55 | int main(int argc, const char *argv[]) | |
56 | { | |
bd2c824a A |
57 | pim_addr group_addr; |
58 | pim_addr source_addr; | |
d62a17ae | 59 | const char *ifname; |
60 | const char *group; | |
61 | const char *source; | |
62 | ifindex_t ifindex; | |
63 | int result; | |
64 | int fd; | |
65 | ||
66 | prog_name = argv[0]; | |
67 | ||
68 | fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | |
69 | if (fd < 0) { | |
70 | fprintf(stderr, | |
71 | "%s: could not create socket: socket(): errno=%d: %s\n", | |
72 | prog_name, errno, strerror(errno)); | |
73 | exit(1); | |
74 | } | |
75 | ||
76 | if (argc != 4) { | |
77 | fprintf(stderr, | |
78 | "usage: %s interface group source\n" | |
79 | "example: %s eth0 232.1.1.1 1.1.1.1\n", | |
80 | prog_name, prog_name); | |
81 | exit(1); | |
82 | } | |
83 | ||
84 | ifname = argv[1]; | |
85 | group = argv[2]; | |
86 | source = argv[3]; | |
87 | ||
88 | ifindex = iface_solve_index(ifname); | |
89 | if (ifindex < 0) { | |
90 | fprintf(stderr, "%s: could not find interface: %s\n", prog_name, | |
91 | ifname); | |
92 | exit(1); | |
93 | } | |
94 | ||
95 | result = inet_pton(AF_INET, group, &group_addr); | |
96 | if (result <= 0) { | |
97 | fprintf(stderr, "%s: bad group address: %s\n", prog_name, | |
98 | group); | |
99 | exit(1); | |
100 | } | |
101 | ||
102 | result = inet_pton(AF_INET, source, &source_addr); | |
103 | if (result <= 0) { | |
104 | fprintf(stderr, "%s: bad source address: %s\n", prog_name, | |
105 | source); | |
106 | exit(1); | |
107 | } | |
108 | ||
bd2c824a | 109 | result = pim_gm_join_source(fd, ifindex, group_addr, source_addr); |
d62a17ae | 110 | if (result) { |
111 | fprintf(stderr, | |
112 | "%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s\n", | |
113 | prog_name, fd, group, source, ifindex, ifname, errno, | |
114 | strerror(errno)); | |
115 | exit(1); | |
116 | } | |
117 | ||
118 | printf("%s: joined channel (S,G)=(%s,%s) on interface %s\n", prog_name, | |
119 | source, group, ifname); | |
120 | ||
121 | printf("%s: waiting...\n", prog_name); | |
122 | ||
767a7788 | 123 | if (getchar() == EOF) |
15fa39c5 | 124 | fprintf(stderr, "getchar failure\n"); |
d62a17ae | 125 | |
126 | close(fd); | |
127 | ||
128 | printf("%s: left channel (S,G)=(%s,%s) on interface %s\n", prog_name, | |
129 | source, group, ifname); | |
130 | ||
131 | exit(0); | |
12e41d03 | 132 | } |