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