]> git.saurik.com Git - apple/network_cmds.git/blob - routed.tproj/trace/trace.c
2d03ea26ee283fac09b920f10095c44ee5d54f22
[apple/network_cmds.git] / routed.tproj / trace / trace.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved
26 *
27 * Copyright (c) 1983, 1988, 1993
28 * The Regents of the University of California. All rights reserved.
29 *
30 * The NEXTSTEP Software License Agreement specifies the terms
31 * and conditions for redistribution.
32 *
33 */
34
35 #ifndef lint
36 static char copyright[] =
37 "@(#) Copyright (c) 1983, 1988, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #endif /* not lint */
40
41 #ifndef lint
42 static char sccsid[] = "@(#)trace.c 8.2 (Berkeley) 4/28/95";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <protocols/routed.h>
50 #include <arpa/inet.h>
51 #include <netdb.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55
56 struct sockaddr_in myaddr;
57 char packet[MAXPACKETSIZE];
58
59 main(argc, argv)
60 int argc;
61 char **argv;
62 {
63 int size, s;
64 struct sockaddr from;
65 struct sockaddr_in router;
66 register struct rip *msg = (struct rip *)packet;
67 struct hostent *hp;
68 struct servent *sp;
69
70 if (argc < 3) {
71 usage:
72 printf("usage: trace cmd machines,\n");
73 printf("cmd either \"on filename\", or \"off\"\n");
74 exit(1);
75 }
76 s = socket(AF_INET, SOCK_DGRAM, 0);
77 if (s < 0) {
78 perror("socket");
79 exit(2);
80 }
81 myaddr.sin_family = AF_INET;
82 myaddr.sin_port = htons(IPPORT_RESERVED-1);
83 if (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
84 perror("bind");
85 exit(2);
86 }
87
88 argv++, argc--;
89 msg->rip_cmd = strcmp(*argv, "on") == 0 ?
90 RIPCMD_TRACEON : RIPCMD_TRACEOFF;
91 msg->rip_vers = RIPVERSION;
92 argv++, argc--;
93 size = sizeof (int);
94 if (msg->rip_cmd == RIPCMD_TRACEON) {
95 strcpy(msg->rip_tracefile, *argv);
96 size += strlen(*argv);
97 argv++, argc--;
98 }
99 if (argc == 0)
100 goto usage;
101 memset(&router, 0, sizeof (router));
102 router.sin_family = AF_INET;
103 sp = getservbyname("router", "udp");
104 if (sp == 0) {
105 printf("udp/router: service unknown\n");
106 exit(1);
107 }
108 router.sin_port = sp->s_port;
109 while (argc > 0) {
110 router.sin_family = AF_INET;
111 router.sin_addr.s_addr = inet_addr(*argv);
112 if (router.sin_addr.s_addr == -1) {
113 hp = gethostbyname(*argv);
114 if (hp == NULL) {
115 fprintf(stderr, "trace: %s: ", *argv);
116 herror((char *)NULL);
117 continue;
118 }
119 memmove(&router.sin_addr, hp->h_addr, hp->h_length);
120 }
121 if (sendto(s, packet, size, 0,
122 (struct sockaddr *)&router, sizeof(router)) < 0)
123 perror(*argv);
124 argv++, argc--;
125 }
126 }