1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Linux Test Project, 2001-2022
4 */
5
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <errno.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <netdb.h>
15
main(int argc,char * argv[])16 int main(int argc, char *argv[])
17 {
18 int s;
19 struct in_addr simr, gimr;
20
21 unsigned int i1, i2, i3, i4;
22 struct hostent *hp, *gethostbyname();
23
24 unsigned char ttl;
25 char no_loop = 0, do_loop = 1;
26 unsigned long len = 0;
27
28 if (argc != 2) {
29 fprintf(stderr,
30 "usage: %s interface_name (or i.i.i.i)\n", argv[0]);
31 exit(1);
32 }
33 s = socket(AF_INET, SOCK_DGRAM, 0);
34 if (s == -1) {
35 perror("can't open socket");
36 exit(1);
37 }
38
39 hp = gethostbyname(argv[1]);
40 if (hp)
41 memcpy(&simr.s_addr, hp->h_addr, hp->h_length);
42 else if (sscanf(argv[1], "%u.%u.%u.%u", &i1, &i2, &i3, &i4) != 4) {
43 fprintf(stderr, "Bad interface address\n");
44 exit(1);
45 } else
46 simr.s_addr = htonl((i1 << 24) | (i2 << 16) | (i3 << 8) | i4);
47
48 /* verify socket options error messages */
49 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &simr,
50 sizeof(simr)) != 0)
51 perror("Setting IP_MULTICAST_IF"), exit(1);
52 len = sizeof(gimr);
53 if (getsockopt
54 (s, IPPROTO_IP, IP_MULTICAST_IF, &gimr, (socklen_t *)&len) != 0)
55 perror("Getting IP_MULTICAST_IF"), exit(1);
56
57 len = sizeof(ttl);
58 if (getsockopt
59 (s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, (socklen_t *)&len) != 0)
60 perror("Getting IP_MULTICAST_TTL"), exit(1);
61
62 ttl = 10; /* Set ttl to 10 */
63 /* printf("setting ttl=10\n");*/
64 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) != 0)
65 perror("Setting IP_MULTICAST_TTL"), exit(1);
66
67 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &do_loop, sizeof(char))
68 != 0)
69 perror("Setting IP_MULTICAST_LOOP"), exit(1);
70 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &no_loop, sizeof(char))
71 != 0)
72 perror("Setting IP_MULTICAST_LOOP"), exit(1);
73 len = sizeof(no_loop);
74 if (getsockopt
75 (s, IPPROTO_IP, IP_MULTICAST_LOOP, &no_loop,
76 (socklen_t *)&len) != 0)
77 perror("Getting IP_MULTICAST_LOOP"), exit(1);
78
79 close(s);
80 exit(0);
81 }
82