1 /*
2  * Copyright (c) 1988-1990 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: (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: /usr/staff/martinh/tcpview/RCS/detail-rip.c,v 1.1 1993/04/22 20:17:03 martinh Exp $ (UW)";
25 #endif
26 
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <netinet/ip_var.h>
34 #include <netinet/udp.h>
35 #include <netinet/udp_var.h>
36 #include <protocols/routed.h>
37 
38 #include <errno.h>
39 
40 #include "tcpview.h"
41 #include "interface.h"
42 #include "addrtoname.h"
43 
44 #ifdef __STDC__
45 static void do_version(register struct rip *rp);
46 #else
47 static void do_version();
48 #endif
49 
rip_entry_print(ni,metric)50 static void rip_entry_print(ni,metric)
51      register struct netinfo *ni;
52      int metric;
53 {
54   char *str1, *str2;
55 
56   if (ntohs(ni->rip_dst.sa_family) != AF_INET) {
57     register int i;
58 
59     printf(" [family %d:", ntohs(ni->rip_dst.sa_family));
60     for (i = 0; i < 14; i += 2)
61       printf(" %02x%02x", ni->rip_dst.sa_data[i],
62 	     ni->rip_dst.sa_data[i+1]);
63     printf("] ");
64   } else {
65     register struct sockaddr_in *sin = (struct sockaddr_in *)&ni->rip_dst;
66     str1 = ipaddr_str(&sin->sin_addr,ADDR_NUMERICAL);
67     str2 = ipaddr_str(&sin->sin_addr,0);
68     if( strcmp(str1,str2) )
69       printf("%s [%s] ", str2, str1);
70     else
71       printf("%-16s ",str2);
72     if (sin->sin_port)
73       printf("[port %d] ", sin->sin_port);
74   }
75   if( metric )
76     printf("\t%d\n", ntohl(ni->rip_metric));
77   else
78     printf("\n");
79   hex(0,19);
80   Offset += 20;
81 }
82 
83 /* dat points to beginning of rip message */
84 /* amt = Phdr->caplen - Offset is number of bytes left to process */
85 /* length is number of bytes in message (may not be all captured) */
86 
detail_rip(dat,length)87 void detail_rip(dat, length)
88      u_char *dat;
89      int length;
90 {
91   register struct rip *rp = (struct rip *)dat;
92   register struct netinfo *ni;
93   register int amt = Phdr->caplen - Offset;
94   register int i = min(length, amt) -
95     (sizeof(struct rip) - sizeof(struct netinfo));
96 
97   int j;
98   int trunc;
99 
100   if (i < 0) /* no nets to print */
101     return;
102 
103   j = length / sizeof(*ni);  /* number of nets in message */
104   trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i); /* truncated net? */
105 
106   printf("----- RIP Header -----\n\n");
107   hex(0, 1);
108   hex(-1,-1);
109 
110   switch (rp->rip_cmd) {
111   case RIPCMD_REQUEST:
112     printf("REQUEST (%d nets)\n",j);
113     hex(0,0);
114     do_version(rp);
115     Offset += 4; /* skip over RIP header */
116     for (ni = rp->rip_nets; (i -= sizeof(*ni)) >= 0; ++ni) {
117       rip_entry_print(ni,0);
118     }
119     if (trunc) {
120       printf("-truncated-\n");
121       hex(-1,-1);
122     }
123     break;
124   case RIPCMD_RESPONSE:
125     printf("RESPONSE (%d nets)\n",j);
126     hex(0,0);
127     do_version(rp);
128     Offset += 4; /* skip over RIP header */
129     for (ni = rp->rip_nets; (i -= sizeof(*ni)) >= 0; ++ni) {
130       rip_entry_print(ni,1);
131     }
132     if (trunc) {
133       printf("-truncated-\n");
134       hex(-1,-1);
135     }
136     break;
137   case RIPCMD_TRACEON:
138     printf("TRACEON  (This is an obsolete command)\n");
139     do_version(rp);
140     hex(0,0);
141     break;
142   case RIPCMD_TRACEOFF:
143     printf("TRACEOFF  (This is an obsolete command)\n");
144     do_version(rp);
145     hex(0,0);
146     break;
147   case RIPCMD_POLL:
148     printf("POLL\n");
149     do_version(rp);
150     hex(0,0);
151     break;
152   case RIPCMD_POLLENTRY:
153     printf("POLLENTRY\n");
154     do_version(rp);
155     hex(0,0);
156     break;
157   default:
158     printf("unknown command %d\n", rp->rip_cmd);
159     do_version(rp);
160     hex(0,0);
161     break;
162   }
163 }
164 
do_version(rp)165 static void do_version(rp)
166      register struct rip *rp;
167 {
168   if( rp->rip_vers == 1 )
169     printf("VERSION: 1\n");
170   else
171     printf("VERSION: %d ( WARNING: should be 1 )\n",rp->rip_vers);
172   hex(1,1);
173 }
174