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