home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tcpdumpb.zip / print-rip.c < prev    next >
C/C++ Source or Header  |  1997-02-14  |  6KB  |  162 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991, 1993, 1994, 1996
  3.  *      The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static char rcsid[] =
  24.     "@(#) $Header: print-rip.c,v 1.34 96/07/23 14:17:26 leres Exp $ (LBL)";
  25. #endif
  26.  
  27. #include <sys/param.h>
  28. #include <sys/time.h>
  29. #include <sys/socket.h>
  30.  
  31. #include <netinet/in.h>
  32. #include <netinet/in_systm.h>
  33. #include <netinet/ip.h>
  34. #ifndef __EMX__
  35. #include <netinet/ip_var.h>
  36. #endif
  37. #include <netinet/udp.h>
  38. #include <netinet/udp_var.h>
  39.  
  40. #include <stdio.h>
  41.  
  42. #include "interface.h"
  43. #include "addrtoname.h"
  44. #include "extract.h"                    /* must come after interface.h */
  45.  
  46. struct rip {
  47.         u_char rip_cmd;                 /* request/response */
  48.         u_char rip_vers;                /* protocol version # */
  49.         u_short rip_zero2;              /* unused */
  50. };
  51. #define RIPCMD_REQUEST          1       /* want info */
  52. #define RIPCMD_RESPONSE         2       /* responding to request */
  53. #define RIPCMD_TRACEON          3       /* turn tracing on */
  54. #define RIPCMD_TRACEOFF         4       /* turn it off */
  55. #define RIPCMD_POLL             5       /* want info from everybody */
  56. #define RIPCMD_POLLENTRY        6       /* poll for entry */
  57.  
  58. struct rip_netinfo {
  59.         u_short rip_family;
  60.         u_short rip_tag;
  61.         u_int32_t rip_dest;
  62.         u_int32_t rip_dest_mask;
  63.         u_int32_t rip_router;
  64.         u_int32_t rip_metric;           /* cost of route */
  65. };
  66.  
  67. static void
  68. rip_entry_print(register int vers, register const struct rip_netinfo *ni)
  69. {
  70.         register u_char *cp, *ep;
  71.  
  72.         if (EXTRACT_16BITS(&ni->rip_family) != AF_INET) {
  73.  
  74.                 printf(" [family %d:", EXTRACT_16BITS(&ni->rip_family));
  75.                 cp = (u_char *)&ni->rip_tag;
  76.                 ep = (u_char *)&ni->rip_metric + sizeof(ni->rip_metric);
  77.                 for (; cp < ep; cp += 2)
  78.                         printf(" %04x", EXTRACT_16BITS(cp));
  79.                 printf("]");
  80.         } else if (vers < 2) {
  81.                 /* RFC 1058 */
  82.                 printf(" %s", ipaddr_string(&ni->rip_dest));
  83.         } else {
  84.                 /* RFC 1723 */
  85.                 printf(" {%s", ipaddr_string(&ni->rip_dest));
  86.                 if (ni->rip_dest_mask)
  87.                         printf("/%s", ipaddr_string(&ni->rip_dest_mask));
  88.                 if (ni->rip_router)
  89.                         printf("->%s", ipaddr_string(&ni->rip_router));
  90.                 if (ni->rip_tag)
  91.                         printf(" tag %04x", EXTRACT_16BITS(&ni->rip_tag));
  92.                 printf("}");
  93.         }
  94.         printf("(%d)", EXTRACT_32BITS(&ni->rip_metric));
  95. }
  96.  
  97. void
  98. rip_print(const u_char *dat, u_int length)
  99. {
  100.         register const struct rip *rp;
  101.         register const struct rip_netinfo *ni;
  102.         register int i, j, trunc;
  103.  
  104.         i = min(length, snapend - dat) - (sizeof(*rp) - sizeof(*ni));
  105.         if (i < 0)
  106.                 return;
  107.  
  108.         rp = (struct rip *)dat;
  109.         switch (rp->rip_cmd) {
  110.  
  111.         case RIPCMD_REQUEST:
  112.                 printf(" rip-req %d", length);
  113.                 break;
  114.  
  115.         case RIPCMD_RESPONSE:
  116.                 j = length / sizeof(*ni);
  117.                 if (j * sizeof(*ni) != length - 4)
  118.                         printf(" rip-resp %d[%d]:", j, length);
  119.                 else
  120.                         printf(" rip-resp %d:", j);
  121.                 trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
  122.                 ni = (struct rip_netinfo *)(rp + 1);
  123.                 for (; (i -= sizeof(*ni)) >= 0; ++ni)
  124.                         rip_entry_print(rp->rip_vers, ni);
  125.                 if (trunc)
  126.                         printf("[|rip]");
  127.                 break;
  128.  
  129.         case RIPCMD_TRACEON:
  130.                 printf(" rip-traceon %d: \"", length);
  131.                 (void)fn_print((const u_char *)(rp + 1), snapend);
  132.                 fputs("\"\n", stdout);
  133.                 break;
  134.  
  135.         case RIPCMD_TRACEOFF:
  136.                 printf(" rip-traceoff %d", length);
  137.                 break;
  138.  
  139.         case RIPCMD_POLL:
  140.                 printf(" rip-poll %d", length);
  141.                 break;
  142.  
  143.         case RIPCMD_POLLENTRY:
  144.                 printf(" rip-pollentry %d", length);
  145.                 break;
  146.  
  147.         default:
  148.                 printf(" rip-#%d %d", rp->rip_cmd, length);
  149.                 break;
  150.         }
  151.         switch (rp->rip_vers) {
  152.  
  153.         case 1:
  154.         case 2:
  155.                 break;
  156.  
  157.         default:
  158.                 printf(" [vers %d]", rp->rip_vers);
  159.                 break;
  160.         }
  161. }
  162.