148131Sbostic /*-
261543Sbostic * Copyright (c) 1983, 1988, 1993
361543Sbostic * The Regents of the University of California. All rights reserved.
433488Sbostic *
548131Sbostic * %sccs.include.redist.c%
622009Sdist */
722009Sdist
87214Ssam #ifndef lint
961543Sbostic static char copyright[] =
1061543Sbostic "@(#) Copyright (c) 1983, 1988, 1993\n\
1161543Sbostic The Regents of the University of California. All rights reserved.\n";
1233488Sbostic #endif /* not lint */
137214Ssam
1422009Sdist #ifndef lint
15*69004Sbostic static char sccsid[] = "@(#)trace.c 8.2 (Berkeley) 04/28/95";
1633488Sbostic #endif /* not lint */
1722009Sdist
187214Ssam #include <sys/param.h>
197214Ssam #include <sys/protosw.h>
207214Ssam #include <sys/socket.h>
2115760Skarels #include <netinet/in.h>
2246786Sbostic #include <protocols/routed.h>
2346786Sbostic #include <arpa/inet.h>
2446786Sbostic #include <netdb.h>
257214Ssam #include <stdio.h>
2646786Sbostic #include <stdlib.h>
2746786Sbostic #include <string.h>
287214Ssam
2936833Skarels struct sockaddr_in myaddr;
3015760Skarels char packet[MAXPACKETSIZE];
317214Ssam
main(argc,argv)327214Ssam main(argc, argv)
337214Ssam int argc;
3446786Sbostic char **argv;
357214Ssam {
367214Ssam int size, s;
377214Ssam struct sockaddr from;
387214Ssam struct sockaddr_in router;
397214Ssam register struct rip *msg = (struct rip *)packet;
408340Ssam struct hostent *hp;
418340Ssam struct servent *sp;
427214Ssam
437214Ssam if (argc < 3) {
447214Ssam usage:
457214Ssam printf("usage: trace cmd machines,\n");
467214Ssam printf("cmd either \"on filename\", or \"off\"\n");
477214Ssam exit(1);
487214Ssam }
4915760Skarels s = socket(AF_INET, SOCK_DGRAM, 0);
5015760Skarels if (s < 0) {
5115760Skarels perror("socket");
5215760Skarels exit(2);
5315760Skarels }
5436833Skarels myaddr.sin_family = AF_INET;
5536833Skarels myaddr.sin_port = htons(IPPORT_RESERVED-1);
5646786Sbostic if (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
5715760Skarels perror("bind");
587214Ssam exit(2);
597214Ssam }
6015760Skarels
617214Ssam argv++, argc--;
627214Ssam msg->rip_cmd = strcmp(*argv, "on") == 0 ?
637214Ssam RIPCMD_TRACEON : RIPCMD_TRACEOFF;
6415760Skarels msg->rip_vers = RIPVERSION;
657214Ssam argv++, argc--;
667214Ssam size = sizeof (int);
677214Ssam if (msg->rip_cmd == RIPCMD_TRACEON) {
687214Ssam strcpy(msg->rip_tracefile, *argv);
697214Ssam size += strlen(*argv);
707214Ssam argv++, argc--;
717214Ssam }
727214Ssam if (argc == 0)
737214Ssam goto usage;
74*69004Sbostic memset(&router, 0, sizeof (router));
757214Ssam router.sin_family = AF_INET;
768340Ssam sp = getservbyname("router", "udp");
778340Ssam if (sp == 0) {
788340Ssam printf("udp/router: service unknown\n");
798340Ssam exit(1);
808340Ssam }
8115760Skarels router.sin_port = sp->s_port;
827214Ssam while (argc > 0) {
8326146Skarels router.sin_family = AF_INET;
8426146Skarels router.sin_addr.s_addr = inet_addr(*argv);
8526146Skarels if (router.sin_addr.s_addr == -1) {
8626146Skarels hp = gethostbyname(*argv);
8735781Sbostic if (hp == NULL) {
8835781Sbostic fprintf(stderr, "trace: %s: ", *argv);
8935781Sbostic herror((char *)NULL);
9036833Skarels continue;
9126146Skarels }
92*69004Sbostic memmove(&router.sin_addr, hp->h_addr, hp->h_length);
937214Ssam }
9446786Sbostic if (sendto(s, packet, size, 0,
9546786Sbostic (struct sockaddr *)&router, sizeof(router)) < 0)
967214Ssam perror(*argv);
977214Ssam argv++, argc--;
987214Ssam }
997214Ssam }
100