BSD 4_3_Net_2 release
[unix-history] / usr / src / sbin / routed / trace / trace.c
CommitLineData
0fc6e47b
KB
1/*-
2 * Copyright (c) 1983, 1988 The Regents of the University of California.
0eb85d71
KB
3 * All rights reserved.
4 *
af359dea
C
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.
5ff67f98
DF
32 */
33
34#ifndef lint
35char copyright[] =
0fc6e47b 36"@(#) Copyright (c) 1983, 1988 The Regents of the University of California.\n\
5ff67f98 37 All rights reserved.\n";
0eb85d71 38#endif /* not lint */
5ff67f98 39
7eaf7b1f 40#ifndef lint
af359dea 41static char sccsid[] = "@(#)trace.c 5.9 (Berkeley) 4/16/91";
0eb85d71 42#endif /* not lint */
7eaf7b1f
SL
43
44#include <sys/param.h>
45#include <sys/protosw.h>
46#include <sys/socket.h>
603811e3 47#include <netinet/in.h>
49001a0c 48#include <protocols/routed.h>
49dac2b7
KB
49#include <arpa/inet.h>
50#include <netdb.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
7eaf7b1f 54
7c1c85bb 55struct sockaddr_in myaddr;
603811e3 56char packet[MAXPACKETSIZE];
7eaf7b1f
SL
57
58main(argc, argv)
59 int argc;
49dac2b7 60 char **argv;
7eaf7b1f
SL
61{
62 int size, s;
63 struct sockaddr from;
64 struct sockaddr_in router;
7eaf7b1f 65 register struct rip *msg = (struct rip *)packet;
f64bc556
SL
66 struct hostent *hp;
67 struct servent *sp;
7eaf7b1f
SL
68
69 if (argc < 3) {
70usage:
71 printf("usage: trace cmd machines,\n");
72 printf("cmd either \"on filename\", or \"off\"\n");
73 exit(1);
74 }
603811e3
MK
75 s = socket(AF_INET, SOCK_DGRAM, 0);
76 if (s < 0) {
77 perror("socket");
78 exit(2);
79 }
7c1c85bb
MK
80 myaddr.sin_family = AF_INET;
81 myaddr.sin_port = htons(IPPORT_RESERVED-1);
49dac2b7 82 if (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
603811e3 83 perror("bind");
7eaf7b1f
SL
84 exit(2);
85 }
603811e3 86
7eaf7b1f
SL
87 argv++, argc--;
88 msg->rip_cmd = strcmp(*argv, "on") == 0 ?
89 RIPCMD_TRACEON : RIPCMD_TRACEOFF;
603811e3 90 msg->rip_vers = RIPVERSION;
7eaf7b1f
SL
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;
f64bc556
SL
102 sp = getservbyname("router", "udp");
103 if (sp == 0) {
104 printf("udp/router: service unknown\n");
105 exit(1);
106 }
603811e3 107 router.sin_port = sp->s_port;
7eaf7b1f 108 while (argc > 0) {
fdd611b4
MK
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);
561e9652
KB
113 if (hp == NULL) {
114 fprintf(stderr, "trace: %s: ", *argv);
115 herror((char *)NULL);
7c1c85bb 116 continue;
fdd611b4
MK
117 }
118 bcopy(hp->h_addr, &router.sin_addr, hp->h_length);
7eaf7b1f 119 }
49dac2b7
KB
120 if (sendto(s, packet, size, 0,
121 (struct sockaddr *)&router, sizeof(router)) < 0)
7eaf7b1f
SL
122 perror(*argv);
123 argv++, argc--;
124 }
125}