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