xref: /386bsd/usr/src/sbin/routed/trace/trace.c (revision a2142627)
1 /*-
2  * Copyright (c) 1983, 1988 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1983, 1988 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)trace.c	5.9 (Berkeley) 4/16/91";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <protocols/routed.h>
49 #include <arpa/inet.h>
50 #include <netdb.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 
55 struct	sockaddr_in myaddr;
56 char	packet[MAXPACKETSIZE];
57 
main(argc,argv)58 main(argc, argv)
59 	int argc;
60 	char **argv;
61 {
62 	int size, s;
63 	struct sockaddr from;
64 	struct sockaddr_in router;
65 	register struct rip *msg = (struct rip *)packet;
66 	struct hostent *hp;
67 	struct servent *sp;
68 
69 	if (argc < 3) {
70 usage:
71 		printf("usage: trace cmd machines,\n");
72 		printf("cmd either \"on filename\", or \"off\"\n");
73 		exit(1);
74 	}
75 	s = socket(AF_INET, SOCK_DGRAM, 0);
76 	if (s < 0) {
77 		perror("socket");
78 		exit(2);
79 	}
80 	myaddr.sin_family = AF_INET;
81 	myaddr.sin_port = htons(IPPORT_RESERVED-1);
82 	if (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
83 		perror("bind");
84 		exit(2);
85 	}
86 
87 	argv++, argc--;
88 	msg->rip_cmd = strcmp(*argv, "on") == 0 ?
89 		RIPCMD_TRACEON : RIPCMD_TRACEOFF;
90 	msg->rip_vers = RIPVERSION;
91 	argv++, argc--;
92 	size = sizeof (int);
93 	if (msg->rip_cmd == RIPCMD_TRACEON) {
94 		strcpy(msg->rip_tracefile, *argv);
95 		size += strlen(*argv);
96 		argv++, argc--;
97 	}
98 	if (argc == 0)
99 		goto usage;
100 	bzero((char *)&router, sizeof (router));
101 	router.sin_family = AF_INET;
102 	sp = getservbyname("router", "udp");
103 	if (sp == 0) {
104 		printf("udp/router: service unknown\n");
105 		exit(1);
106 	}
107 	router.sin_port = sp->s_port;
108 	while (argc > 0) {
109 		router.sin_family = AF_INET;
110 		router.sin_addr.s_addr = inet_addr(*argv);
111 		if (router.sin_addr.s_addr == -1) {
112 			hp = gethostbyname(*argv);
113 			if (hp == NULL) {
114 				fprintf(stderr, "trace: %s: ", *argv);
115 				herror((char *)NULL);
116 				continue;
117 			}
118 			bcopy(hp->h_addr, &router.sin_addr, hp->h_length);
119 		}
120 		if (sendto(s, packet, size, 0,
121 		    (struct sockaddr *)&router, sizeof(router)) < 0)
122 			perror(*argv);
123 		argv++, argc--;
124 	}
125 }
126