• Home
  • 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 <unistd.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <time.h>
11 #include <netdb.h>
12 
13 #define	MAXBUFSIZ	8096
14 
15 static char buf[MAXBUFSIZ];
16 static int Num_Loops = 100;
17 
main(int argc,char * argv[])18 int main(int argc, char *argv[])
19 {
20 	struct ip_mreq imr;
21 	struct sockaddr_in sin, mcast_out;
22 	int i = 0, s = 0, n = 0;
23 	unsigned i1, i2, i3, i4, g1, g2, g3, g4;
24 	struct hostent *hp, *gethostbyname();
25 	char myname[64];
26 	char ttl = 0;
27 
28 	if (argc < 4) {
29 		fprintf(stderr,
30 			"usage: %s g.g.g.g interface_name (or i.i.i.i) port [ttl]\n",
31 			argv[0]);
32 		exit(1);
33 	}
34 
35 	/* Get local host name */
36 	if (gethostname(myname, sizeof(myname) - 1) < 0) {
37 		perror("gethostname");
38 		exit(1);
39 	}
40 
41 	/* set up multicast membership structure */
42 	if ((n = sscanf(argv[1], "%u.%u.%u.%u", &g1, &g2, &g3, &g4)) != 4) {
43 		fprintf(stderr, "bad group address\n");
44 		exit(1);
45 	}
46 	imr.imr_multiaddr.s_addr =
47 	    htonl((g1 << 24) | (g2 << 16) | (g3 << 8) | g4);
48 
49 	if ((hp = gethostbyname(argv[2]))) {
50 		memcpy(&imr.imr_interface.s_addr, hp->h_addr, hp->h_length);
51 	} else
52 	    if ((n = sscanf(argv[2], "%u.%u.%u.%u", &i1, &i2, &i3, &i4)) != 4) {
53 		fprintf(stderr, "Bad interface address\n");
54 		exit(1);
55 	} else
56 		imr.imr_interface.s_addr =
57 		    htonl((i1 << 24) | (i2 << 16) | (i3 << 8) | i4);
58 
59 	/* Set up socket structure for sendto */
60 	memset(&mcast_out, 0x00, sizeof(mcast_out));
61 	memset(&sin, 0x00, sizeof(sin));
62 	mcast_out.sin_family = sin.sin_family = AF_INET;
63 	mcast_out.sin_port = sin.sin_port = htons(atoi(argv[3]));
64 
65 	mcast_out.sin_addr.s_addr = imr.imr_multiaddr.s_addr;
66 
67 	/* Create socket */
68 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
69 		perror("can not open socket");
70 		exit(1);
71 	}
72 
73 	/* Set socket for multicasting */
74 	if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
75 		       &imr.imr_interface.s_addr,
76 		       sizeof(imr.imr_interface.s_addr)) != 0) {
77 		fprintf(stderr,
78 			"Error: unable to set socket option IP_MULTICAST_IF\n");
79 		exit(1);
80 	}
81 
82 	/* With an even port number the loopback will be disabled */
83 /*      loop = atoi(argv[3])&1;
84         if (setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(char))!= 0) {
85            fprintf (stderr,
86                     "Error: unable to set socket option IP_MULTICAST_LOOP\n");
87            exit (1);
88         }
89 */
90 	ttl = atoi(argv[4]);
91 	if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
92 		perror("can not set ttl");
93 		exit(1);
94 	}
95 
96 	/* Send datagrams */
97 	for (i = 1; i < Num_Loops; i++) {
98 		sprintf(buf, "%s %d %d", argv[2], i, (int)time(0));
99 		if ((n =
100 		     sendto(s, buf, sizeof(buf), 0,
101 			    (struct sockaddr *)&mcast_out,
102 			    sizeof(mcast_out))) < 0) {
103 			perror("setsockopt");
104 			exit(1);
105 		}
106 		sleep(1);
107 	}
108 
109 	/* Tell recevier to close */
110 	sprintf(buf, "quit");
111 	if ((n = sendto(s, buf, sizeof(buf), 0, (struct sockaddr *)&mcast_out,
112 			sizeof(mcast_out))) < 0) {
113 		perror("setsockopt");
114 		exit(1);
115 	}
116 
117 	close(s);
118 	exit(0);
119 }
120