home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / routed / trace / trace.c
Encoding:
C/C++ Source or Header  |  1993-12-17  |  4.0 KB  |  127 lines

  1. /*-
  2.  * Copyright (c) 1983, 1988 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  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.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1983, 1988 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. /*static char sccsid[] = "from: @(#)trace.c    5.9 (Berkeley) 4/16/91";*/
  42. static char rcsid[] = "$Id: trace.c,v 1.4 1993/08/01 18:24:39 mycroft Exp $";
  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.     bzero((char *)&router, 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.             bcopy(hp->h_addr, &router.sin_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. }
  127.