1 /*-
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1983, 1988, 1993\n\
11 The Regents of the University of California. All rights reserved.\n";
12 #endif /* not lint */
13
14 #ifndef lint
15 static char sccsid[] = "@(#)trace.c 8.2 (Berkeley) 04/28/95";
16 #endif /* not lint */
17
18 #include <sys/param.h>
19 #include <sys/protosw.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <protocols/routed.h>
23 #include <arpa/inet.h>
24 #include <netdb.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 struct sockaddr_in myaddr;
30 char packet[MAXPACKETSIZE];
31
main(argc,argv)32 main(argc, argv)
33 int argc;
34 char **argv;
35 {
36 int size, s;
37 struct sockaddr from;
38 struct sockaddr_in router;
39 register struct rip *msg = (struct rip *)packet;
40 struct hostent *hp;
41 struct servent *sp;
42
43 if (argc < 3) {
44 usage:
45 printf("usage: trace cmd machines,\n");
46 printf("cmd either \"on filename\", or \"off\"\n");
47 exit(1);
48 }
49 s = socket(AF_INET, SOCK_DGRAM, 0);
50 if (s < 0) {
51 perror("socket");
52 exit(2);
53 }
54 myaddr.sin_family = AF_INET;
55 myaddr.sin_port = htons(IPPORT_RESERVED-1);
56 if (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
57 perror("bind");
58 exit(2);
59 }
60
61 argv++, argc--;
62 msg->rip_cmd = strcmp(*argv, "on") == 0 ?
63 RIPCMD_TRACEON : RIPCMD_TRACEOFF;
64 msg->rip_vers = RIPVERSION;
65 argv++, argc--;
66 size = sizeof (int);
67 if (msg->rip_cmd == RIPCMD_TRACEON) {
68 strcpy(msg->rip_tracefile, *argv);
69 size += strlen(*argv);
70 argv++, argc--;
71 }
72 if (argc == 0)
73 goto usage;
74 memset(&router, 0, sizeof (router));
75 router.sin_family = AF_INET;
76 sp = getservbyname("router", "udp");
77 if (sp == 0) {
78 printf("udp/router: service unknown\n");
79 exit(1);
80 }
81 router.sin_port = sp->s_port;
82 while (argc > 0) {
83 router.sin_family = AF_INET;
84 router.sin_addr.s_addr = inet_addr(*argv);
85 if (router.sin_addr.s_addr == -1) {
86 hp = gethostbyname(*argv);
87 if (hp == NULL) {
88 fprintf(stderr, "trace: %s: ", *argv);
89 herror((char *)NULL);
90 continue;
91 }
92 memmove(&router.sin_addr, hp->h_addr, hp->h_length);
93 }
94 if (sendto(s, packet, size, 0,
95 (struct sockaddr *)&router, sizeof(router)) < 0)
96 perror(*argv);
97 argv++, argc--;
98 }
99 }
100