1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <arpa/inet.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <netdb.h>
11 
12 #define	MAXBUFSIZ	8096
13 
14 static int errors;
15 
main(int argc,char * argv[])16 int main(int argc, char *argv[])
17 {
18 	int s;
19 	struct in_addr gimr;
20 	struct ip_mreq simr;
21 
22 	unsigned i1, i2, i3, i4;
23 	struct hostent *hp, *gethostbyname();
24 
25 	char sintf[20], gintf[20];
26 	unsigned char ttl;
27 	char loop = 0;
28 	unsigned int len = 0;
29 
30 	if (argc != 2) {
31 		fprintf(stderr,
32 			"usage: %s interface_name (or i.i.i.i)\n", argv[0]);
33 		exit(1);
34 	}
35 	s = socket(AF_INET, SOCK_DGRAM, 0);
36 	if (s == -1) {
37 		perror("Error: can't open socket");
38 		exit(1);
39 	}
40 
41 	printf("agrv sub 1 is %s\n", argv[1]);
42 	hp = gethostbyname(argv[1]);
43 	if (hp)
44 		memcpy(&simr.imr_interface.s_addr, hp->h_addr, hp->h_length);
45 	else if (sscanf(argv[1], "%u.%u.%u.%u", &i1, &i2, &i3, &i4) != 4) {
46 		fprintf(stderr, "Bad interface address\n");
47 		exit(1);
48 	} else
49 		simr.imr_interface.s_addr =
50 		    htonl((i1 << 24) | (i2 << 16) | (i3 << 8) | i4);
51 	strcpy(sintf, inet_ntoa(simr.imr_interface));
52 
53 	/* verify socket options */
54 	if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
55 		       &simr.imr_interface.s_addr,
56 		       sizeof(simr.imr_interface.s_addr)) != 0) {
57 		perror("Error: Setting IP_MULTICAST_IF");
58 		errors++;
59 	} else
60 		printf("Set interface: %s for multicasting\n", sintf);
61 
62 	len = sizeof(gimr);
63 	if (getsockopt
64 	    (s, IPPROTO_IP, IP_MULTICAST_IF, &gimr, (socklen_t *)&len) != 0) {
65 		perror("Getting IP_MULTICAST_IF");
66 		errors++;
67 	} else {
68 		strcpy(gintf, inet_ntoa(gimr));
69 		printf("Got multicasting socket interface: %s\n", gintf);
70 	}
71 
72 	/* Verify that the multicastion for the interface was set */
73 	if (strcmp(sintf, gintf) != 0) {
74 		printf("Error: IP_MULTICAST_IF was not set\n");
75 		errors++;
76 	} else
77 		printf
78 		    ("Socket has been set for multicasting on interface: %s\n",
79 		     sintf);
80 
81 	len = sizeof(ttl);
82 	if (getsockopt
83 	    (s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, (socklen_t *)&len) != 0) {
84 		perror("Error: Gettting IP_MULTICAST_TTL");
85 		errors++;
86 	} else
87 		printf("getsockopt: got ttl = %i\n", ttl);
88 	if (ttl != 1)
89 		printf("Error: IP_MULTICAST_TTL not default value, ttl = %i\n",
90 		       ttl);
91 	ttl = 10;		/* Set ttl to 10 */
92 	len = sizeof(ttl);
93 	if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
94 		       &ttl, sizeof(ttl)) != 0) {
95 		perror("Error: Setting IP_MULTICAST_TTL");
96 		errors++;
97 	} else
98 		printf("TTL set on multicast socket\n");
99 	if (getsockopt
100 	    (s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, (socklen_t *)&len) != 0) {
101 		perror("Error: Getting IP_MULTICAST_TTL");
102 		errors++;
103 	}
104 	if (ttl != 10) {
105 		printf("Error: IP_MULTICAST_TTL not set, ttl = %i\n", ttl);
106 		errors++;
107 	}
108 
109 	len = sizeof(loop);
110 	if (getsockopt
111 	    (s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
112 	     (socklen_t *)&len) != 0) {
113 		perror("Error: Getting IP_MULTICAST_LOOP");
114 		errors++;
115 	} else
116 		printf("Got loopback setting\n");
117 	if (loop != 1) {
118 		printf("Error: IP_MULTICAST_LOOP not enabled, loop = %i\n",
119 		       loop);
120 		errors++;
121 	} else
122 		printf("IP_MULTICAST_LOOP is enabled\n");
123 
124 	loop = 0;		/* Disable IP_MULTICAST_LOOP */
125 	if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(char)) !=
126 	    0) {
127 		errors++;
128 		perror("Error: Setting IP_MULTICAST_LOOP");
129 	} else
130 		printf("Multicast loopback disabled\n");
131 	if (getsockopt
132 	    (s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
133 	     (socklen_t *)&len) != 0) {
134 		perror("Error: Getting IP_MULTICAST_LOOP");
135 		errors++;
136 	} else
137 		printf("Got multicast loopback value\n");
138 	if (loop != 0) {
139 		printf("Error: IP_MULTICAST_LOOP not disabled, loop = %i\n",
140 		       loop);
141 		errors++;
142 	} else
143 		printf("IP_MULTICAST_LOOP disabled\n");
144 
145 	close(s);
146 	if (errors)
147 		exit(1);
148 	exit(0);
149 }
150