xref: /csrg-svn/sbin/routed/trace/trace.c (revision 33488)
122009Sdist /*
222009Sdist  * Copyright (c) 1983 Regents of the University of California.
3*33488Sbostic  * All rights reserved.
4*33488Sbostic  *
5*33488Sbostic  * Redistribution and use in source and binary forms are permitted
6*33488Sbostic  * provided that this notice is preserved and that due credit is given
7*33488Sbostic  * to the University of California at Berkeley. The name of the University
8*33488Sbostic  * may not be used to endorse or promote products derived from this
9*33488Sbostic  * software without specific prior written permission. This software
10*33488Sbostic  * is provided ``as is'' without express or implied warranty.
1122009Sdist  */
1222009Sdist 
137214Ssam #ifndef lint
1422009Sdist char copyright[] =
1522009Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1622009Sdist  All rights reserved.\n";
17*33488Sbostic #endif /* not lint */
187214Ssam 
1922009Sdist #ifndef lint
20*33488Sbostic static char sccsid[] = "@(#)trace.c	5.4 (Berkeley) 02/16/88";
21*33488Sbostic #endif /* not lint */
2222009Sdist 
237214Ssam #include <sys/param.h>
247214Ssam #include <sys/protosw.h>
257214Ssam #include <sys/socket.h>
2615760Skarels #include <netinet/in.h>
277214Ssam #include <errno.h>
287214Ssam #include <stdio.h>
298340Ssam #include <netdb.h>
3025382Skarels #include <protocols/routed.h>
317214Ssam 
327214Ssam struct	sockaddr_in myaddr = { AF_INET, IPPORT_RESERVED-1 };
3315760Skarels char	packet[MAXPACKETSIZE];
347214Ssam 
357214Ssam main(argc, argv)
367214Ssam 	int argc;
377214Ssam 	char *argv[];
387214Ssam {
397214Ssam 	int size, s;
407214Ssam 	struct sockaddr from;
417214Ssam 	struct sockaddr_in router;
427214Ssam 	register struct rip *msg = (struct rip *)packet;
438340Ssam 	struct hostent *hp;
448340Ssam 	struct servent *sp;
457214Ssam 
467214Ssam 	if (argc < 3) {
477214Ssam usage:
487214Ssam 		printf("usage: trace cmd machines,\n");
497214Ssam 		printf("cmd either \"on filename\", or \"off\"\n");
507214Ssam 		exit(1);
517214Ssam 	}
5215760Skarels 	s = socket(AF_INET, SOCK_DGRAM, 0);
5315760Skarels 	if (s < 0) {
5415760Skarels 		perror("socket");
5515760Skarels 		exit(2);
5615760Skarels 	}
577214Ssam #ifdef vax || pdp11
587214Ssam 	myaddr.sin_port = htons(myaddr.sin_port);
597214Ssam #endif
6015760Skarels 	if (bind(s, &myaddr, sizeof(myaddr)) < 0) {
6115760Skarels 		perror("bind");
627214Ssam 		exit(2);
637214Ssam 	}
6415760Skarels 
657214Ssam 	argv++, argc--;
667214Ssam 	msg->rip_cmd = strcmp(*argv, "on") == 0 ?
677214Ssam 		RIPCMD_TRACEON : RIPCMD_TRACEOFF;
6815760Skarels 	msg->rip_vers = RIPVERSION;
697214Ssam 	argv++, argc--;
707214Ssam 	size = sizeof (int);
717214Ssam 	if (msg->rip_cmd == RIPCMD_TRACEON) {
727214Ssam 		strcpy(msg->rip_tracefile, *argv);
737214Ssam 		size += strlen(*argv);
747214Ssam 		argv++, argc--;
757214Ssam 	}
767214Ssam 	if (argc == 0)
777214Ssam 		goto usage;
787214Ssam 	bzero((char *)&router, sizeof (router));
797214Ssam 	router.sin_family = AF_INET;
808340Ssam 	sp = getservbyname("router", "udp");
818340Ssam 	if (sp == 0) {
828340Ssam 		printf("udp/router: service unknown\n");
838340Ssam 		exit(1);
848340Ssam 	}
8515760Skarels 	router.sin_port = sp->s_port;
867214Ssam 	while (argc > 0) {
8726146Skarels 		router.sin_family = AF_INET;
8826146Skarels 		router.sin_addr.s_addr = inet_addr(*argv);
8926146Skarels 		if (router.sin_addr.s_addr == -1) {
9026146Skarels 			hp = gethostbyname(*argv);
9126146Skarels 			if (hp == 0) {
9226146Skarels 				printf("%s: unknown\n", *argv);
9326146Skarels 				exit(1);
9426146Skarels 			}
9526146Skarels 			bcopy(hp->h_addr, &router.sin_addr, hp->h_length);
967214Ssam 		}
9715760Skarels 		if (sendto(s, packet, size, 0, &router, sizeof(router)) < 0)
987214Ssam 			perror(*argv);
997214Ssam 		argv++, argc--;
1007214Ssam 	}
1017214Ssam }
102