xref: /original-bsd/sbin/routed/trace/trace.c (revision d272e02a)
1 /*
2  * Copyright (c) 1983, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)trace.c	5.8 (Berkeley) 02/28/91";
26 #endif /* not lint */
27 
28 #include <sys/param.h>
29 #include <sys/protosw.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <protocols/routed.h>
33 #include <arpa/inet.h>
34 #include <netdb.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 struct	sockaddr_in myaddr;
40 char	packet[MAXPACKETSIZE];
41 
42 main(argc, argv)
43 	int argc;
44 	char **argv;
45 {
46 	int size, s;
47 	struct sockaddr from;
48 	struct sockaddr_in router;
49 	register struct rip *msg = (struct rip *)packet;
50 	struct hostent *hp;
51 	struct servent *sp;
52 
53 	if (argc < 3) {
54 usage:
55 		printf("usage: trace cmd machines,\n");
56 		printf("cmd either \"on filename\", or \"off\"\n");
57 		exit(1);
58 	}
59 	s = socket(AF_INET, SOCK_DGRAM, 0);
60 	if (s < 0) {
61 		perror("socket");
62 		exit(2);
63 	}
64 	myaddr.sin_family = AF_INET;
65 	myaddr.sin_port = htons(IPPORT_RESERVED-1);
66 	if (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
67 		perror("bind");
68 		exit(2);
69 	}
70 
71 	argv++, argc--;
72 	msg->rip_cmd = strcmp(*argv, "on") == 0 ?
73 		RIPCMD_TRACEON : RIPCMD_TRACEOFF;
74 	msg->rip_vers = RIPVERSION;
75 	argv++, argc--;
76 	size = sizeof (int);
77 	if (msg->rip_cmd == RIPCMD_TRACEON) {
78 		strcpy(msg->rip_tracefile, *argv);
79 		size += strlen(*argv);
80 		argv++, argc--;
81 	}
82 	if (argc == 0)
83 		goto usage;
84 	bzero((char *)&router, sizeof (router));
85 	router.sin_family = AF_INET;
86 	sp = getservbyname("router", "udp");
87 	if (sp == 0) {
88 		printf("udp/router: service unknown\n");
89 		exit(1);
90 	}
91 	router.sin_port = sp->s_port;
92 	while (argc > 0) {
93 		router.sin_family = AF_INET;
94 		router.sin_addr.s_addr = inet_addr(*argv);
95 		if (router.sin_addr.s_addr == -1) {
96 			hp = gethostbyname(*argv);
97 			if (hp == NULL) {
98 				fprintf(stderr, "trace: %s: ", *argv);
99 				herror((char *)NULL);
100 				continue;
101 			}
102 			bcopy(hp->h_addr, &router.sin_addr, hp->h_length);
103 		}
104 		if (sendto(s, packet, size, 0,
105 		    (struct sockaddr *)&router, sizeof(router)) < 0)
106 			perror(*argv);
107 		argv++, argc--;
108 	}
109 }
110