]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/netstat/atalk.c
This commit was generated by cvs2svn to compensate for changes in r110018,
[FreeBSD/FreeBSD.git] / usr.bin / netstat / atalk.c
1 /*
2  * Copyright (c) 1983, 1988, 1993
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 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 /*
36 static char sccsid[] = "@(#)atalk.c     1.1 (Whistle) 6/6/96";
37 */
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif /* not lint */
41
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
47
48 #include <arpa/inet.h>
49 #include <net/route.h>
50
51 #include <netatalk/at.h>
52 #include <netatalk/ddp_var.h>
53
54 #include <errno.h>
55 #include <nlist.h>
56 #include <netdb.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include "netstat.h"
60
61 struct  ddpcb ddpcb;
62 struct  socket sockb;
63
64 static  int first = 1;
65
66 /*
67  * Print a summary of connections related to a Network Systems
68  * protocol.  For XXX, also give state of connection.
69  * Listening processes (aflag) are suppressed unless the
70  * -a (all) flag is specified.
71  */
72
73 static const char *
74 at_pr_net(struct sockaddr_at *sat, int numeric)
75 {
76 static  char mybuf[50];
77
78         if (!numeric) {
79                 switch(sat->sat_addr.s_net) {
80                 case 0xffff:
81                         return "????";
82                 case ATADDR_ANYNET:
83                         return("*");
84                 }
85         }
86         sprintf(mybuf,"%hu",ntohs(sat->sat_addr.s_net));
87         return mybuf;
88 }
89
90 static const char *
91 at_pr_host(struct sockaddr_at *sat, int numeric)
92 {
93 static  char mybuf[50];
94
95         if (!numeric) {
96                 switch(sat->sat_addr.s_node) {
97                 case ATADDR_BCAST:
98                         return "bcast";
99                 case ATADDR_ANYNODE:
100                         return("*");
101                 }
102         }
103         sprintf(mybuf,"%d",(unsigned int)sat->sat_addr.s_node);
104         return mybuf;
105 }
106
107 static const char *
108 at_pr_port(struct sockaddr_at *sat)
109 {
110 static  char mybuf[50];
111         struct servent *serv;
112
113         switch(sat->sat_port) {
114         case ATADDR_ANYPORT:
115                 return("*");
116         case 0xff:
117                 return "????";
118         default:
119                 if (numeric_port) {
120                         (void)snprintf(mybuf, sizeof(mybuf), "%d",
121                             (unsigned int)sat->sat_port);
122                 } else {
123                         serv = getservbyport(sat->sat_port, "ddp");
124                         if (serv == NULL)
125                                 (void)snprintf(mybuf, sizeof(mybuf), "%d",
126                                     (unsigned int) sat->sat_port);
127                         else
128                                 (void) snprintf(mybuf, sizeof(mybuf), "%s",
129                                     serv->s_name);
130                 }
131         }
132         return mybuf;
133 }
134
135 static char *
136 at_pr_range(struct sockaddr_at *sat)
137 {
138 static  char mybuf[50];
139
140         if(sat->sat_range.r_netrange.nr_firstnet
141            != sat->sat_range.r_netrange.nr_lastnet) {
142                 sprintf(mybuf,"%d-%d",
143                         ntohs(sat->sat_range.r_netrange.nr_firstnet),
144                         ntohs(sat->sat_range.r_netrange.nr_lastnet));
145         } else {
146                 sprintf(mybuf,"%d",
147                         ntohs(sat->sat_range.r_netrange.nr_firstnet));
148         }
149         return mybuf;
150 }
151
152
153 /* what == 0 for addr only == 3 */
154 /*         1 for net */
155 /*         2 for host */
156 /*         4 for port */
157 /*         8 for numeric only */
158 char *
159 atalk_print(struct sockaddr *sa, int what)
160 {
161         struct sockaddr_at *sat = (struct sockaddr_at *)sa;
162         static  char mybuf[50];
163         int numeric = (what & 0x08);
164
165         mybuf[0] = 0;
166         switch (what & 0x13) {
167         case 0:
168                 mybuf[0] = 0;
169                 break;
170         case 1:
171                 sprintf(mybuf,"%s",at_pr_net(sat, numeric));
172                 break;
173         case 2:
174                 sprintf(mybuf,"%s",at_pr_host(sat, numeric));
175                 break;
176         case 3:
177                 sprintf(mybuf,"%s.%s",
178                                 at_pr_net(sat, numeric),
179                                 at_pr_host(sat, numeric));
180                 break;
181         case 0x10:
182                 sprintf(mybuf,"%s", at_pr_range(sat));
183         }
184         if (what & 4) {
185                 sprintf(mybuf+strlen(mybuf),".%s",at_pr_port(sat));
186         }
187         return mybuf;
188 }
189
190 char *
191 atalk_print2(struct sockaddr *sa, struct sockaddr *mask, int what)
192 {
193   int n;
194   static char buf[100];
195   struct sockaddr_at *sat1, *sat2;
196   struct sockaddr_at thesockaddr;
197   struct sockaddr *sa2;
198
199   sat1 = (struct sockaddr_at *)sa;
200   sat2 = (struct sockaddr_at *)mask;
201   sa2 = (struct sockaddr *)&thesockaddr;
202
203   thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net & sat2->sat_addr.s_net;
204   snprintf(buf, sizeof(buf), "%s", atalk_print(sa2, 1 |(what & 8)));
205   if(sat2->sat_addr.s_net != 0xFFFF) {
206     thesockaddr.sat_addr.s_net = sat1->sat_addr.s_net | ~sat2->sat_addr.s_net;
207     n = strlen(buf);
208     snprintf(buf + n, sizeof(buf) - n, "-%s", atalk_print(sa2, 1 |(what & 8)));
209   }
210   if(what & 2) {
211     n = strlen(buf);
212     snprintf(buf + n, sizeof(buf) - n, ".%s", atalk_print(sa, what & (~1)));
213   }
214   return(buf);
215 }
216
217 void
218 atalkprotopr(u_long off __unused, const char *name, int af1 __unused)
219 {
220         struct ddpcb *this, *next;
221
222         if (off == 0)
223                 return;
224         kread(off, (char *)&this, sizeof (struct ddpcb *));
225         for ( ; this != NULL; this = next) {
226                 kread((u_long)this, (char *)&ddpcb, sizeof (ddpcb));
227                 next = ddpcb.ddp_next;
228 #if 0
229                 if (!aflag && atalk_nullhost(ddpcb.ddp_lsat) ) {
230                         continue;
231                 }
232 #endif
233                 kread((u_long)ddpcb.ddp_socket, (char *)&sockb, sizeof (sockb));
234                 if (first) {
235                         printf("Active ATALK connections");
236                         if (aflag)
237                                 printf(" (including servers)");
238                         putchar('\n');
239                         if (Aflag)
240                                 printf("%-8.8s ", "PCB");
241                         printf(Aflag ?
242                                 "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
243                                 "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
244                                 "Proto", "Recv-Q", "Send-Q",
245                                 "Local Address", "Foreign Address", "(state)");
246                         first = 0;
247                 }
248                 if (Aflag)
249                         printf("%8lx ", (u_long) this);
250                 printf("%-5.5s %6u %6u ", name, sockb.so_rcv.sb_cc,
251                         sockb.so_snd.sb_cc);
252                 printf(Aflag?" %-18.18s":" %-22.22s", atalk_print(
253                                         (struct sockaddr *)&ddpcb.ddp_lsat,7));
254                 printf(Aflag?" %-18.18s":" %-22.22s", atalk_print(
255                                         (struct sockaddr *)&ddpcb.ddp_fsat,7));
256                 putchar('\n');
257         }
258 }
259
260 #define ANY(x,y,z) if (x || sflag <= 1) \
261         printf("\t%lu %s%s%s\n",x,y,plural(x),z)
262
263 /*
264  * Dump DDP statistics structure.
265  */
266 void
267 ddp_stats(u_long off __unused, const char *name, int af1 __unused)
268 {
269         struct ddpstat ddpstat;
270
271         if (off == 0)
272                 return;
273         kread(off, (char *)&ddpstat, sizeof (ddpstat));
274         printf("%s:\n", name);
275         ANY(ddpstat.ddps_short, "packet", " with short headers ");
276         ANY(ddpstat.ddps_long, "packet", " with long headers ");
277         ANY(ddpstat.ddps_nosum, "packet", " with no checksum ");
278         ANY(ddpstat.ddps_tooshort, "packet", " too short ");
279         ANY(ddpstat.ddps_badsum, "packet", " with bad checksum ");
280         ANY(ddpstat.ddps_toosmall, "packet", " with not enough data ");
281         ANY(ddpstat.ddps_forward, "packet", " forwarded ");
282         ANY(ddpstat.ddps_encap, "packet", " encapsulated ");
283         ANY(ddpstat.ddps_cantforward, "packet", " rcvd for unreachable dest ");
284         ANY(ddpstat.ddps_nosockspace, "packet", " dropped due to no socket space ");
285 }