Drop ".i0", it is an undefined macro.
[netbsd-mini2440.git] / dist / tcpdump / parsenfsfh.c
blob2b8a312daa0ead9c1dabeace750a5e88a808cbc6
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1993, 1994 Jeffrey C. Mogul, Digital Equipment Corporation,
5 * Western Research Laboratory. All rights reserved.
6 * Copyright (c) 2001 Compaq Computer Corporation. All rights reserved.
8 * Permission to use, copy, and modify this software and its
9 * documentation is hereby granted only under the following terms and
10 * conditions. Both the above copyright notice and this permission
11 * notice must appear in all copies of the software, derivative works
12 * or modified versions, and any portions thereof, and both notices
13 * must appear in supporting documentation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS" AND COMPAQ COMPUTER CORPORATION
26 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
27 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
28 * EVENT SHALL COMPAQ COMPUTER CORPORATION BE LIABLE FOR ANY
29 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
30 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
31 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
32 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
33 * SOFTWARE.
37 * parsenfsfh.c - portable parser for NFS file handles
38 * uses all sorts of heuristics
40 * Jeffrey C. Mogul
41 * Digital Equipment Corporation
42 * Western Research Laboratory
45 #include <sys/cdefs.h>
46 #ifndef lint
47 #if 0
48 static const char rcsid[] _U_ =
49 "@(#) Header: /tcpdump/master/tcpdump/parsenfsfh.c,v 1.28.2.1 2007/06/15 19:15:04 guy Exp (LBL)";
50 #else
51 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
52 #endif
53 #endif
55 #ifdef HAVE_CONFIG_H
56 #include "config.h"
57 #endif
59 #include <tcpdump-stdinc.h>
61 #include <stdio.h>
62 #include <string.h>
64 #include "interface.h"
65 #include "nfsfh.h"
68 * This routine attempts to parse a file handle (in network byte order),
69 * using heuristics to guess what kind of format it is in. See the
70 * file "fhandle_layouts" for a detailed description of the various
71 * patterns we know about.
73 * The file handle is parsed into our internal representation of a
74 * file-system id, and an internal representation of an inode-number.
77 #define FHT_UNKNOWN 0
78 #define FHT_AUSPEX 1
79 #define FHT_DECOSF 2
80 #define FHT_IRIX4 3
81 #define FHT_IRIX5 4
82 #define FHT_SUNOS3 5
83 #define FHT_SUNOS4 6
84 #define FHT_ULTRIX 7
85 #define FHT_VMSUCX 8
86 #define FHT_SUNOS5 9
87 #define FHT_AIX32 10
88 #define FHT_HPUX9 11
89 #define FHT_BSD44 12
91 #ifdef ultrix
92 /* Nasty hack to keep the Ultrix C compiler from emitting bogus warnings */
93 #define XFF(x) ((u_int32_t)(x))
94 #else
95 #define XFF(x) (x)
96 #endif
98 #define make_uint32(msb,b,c,lsb)\
99 (XFF(lsb) + (XFF(c)<<8) + (XFF(b)<<16) + (XFF(msb)<<24))
101 #define make_uint24(msb,b, lsb)\
102 (XFF(lsb) + (XFF(b)<<8) + (XFF(msb)<<16))
104 #define make_uint16(msb,lsb)\
105 (XFF(lsb) + (XFF(msb)<<8))
107 #ifdef __alpha
108 /* or other 64-bit systems */
109 #define make_uint48(msb,b,c,d,e,lsb)\
110 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24) + ((b)<<32) + ((msb)<<40))
111 #else
112 /* on 32-bit systems ignore high-order bits */
113 #define make_uint48(msb,b,c,d,e,lsb)\
114 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24))
115 #endif
117 static int is_UCX(const unsigned char *);
119 void
120 Parse_fh(fh, len, fsidp, inop, osnamep, fsnamep, ourself)
121 register const unsigned char *fh;
122 int len _U_;
123 my_fsid *fsidp;
124 ino_t *inop;
125 const char **osnamep; /* if non-NULL, return OS name here */
126 const char **fsnamep; /* if non-NULL, return server fs name here (for VMS) */
127 int ourself; /* true if file handle was generated on this host */
129 register const unsigned char *fhp = fh;
130 u_int32_t temp;
131 int fhtype = FHT_UNKNOWN;
132 int i;
134 if (ourself) {
135 /* File handle generated on this host, no need for guessing */
136 #if defined(IRIX40)
137 fhtype = FHT_IRIX4;
138 #endif
139 #if defined(IRIX50)
140 fhtype = FHT_IRIX5;
141 #endif
142 #if defined(IRIX51)
143 fhtype = FHT_IRIX5;
144 #endif
145 #if defined(SUNOS4)
146 fhtype = FHT_SUNOS4;
147 #endif
148 #if defined(SUNOS5)
149 fhtype = FHT_SUNOS5;
150 #endif
151 #if defined(ultrix)
152 fhtype = FHT_ULTRIX;
153 #endif
154 #if defined(__osf__)
155 fhtype = FHT_DECOSF;
156 #endif
157 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__) \
158 || defined(__OpenBSD__)
159 fhtype = FHT_BSD44;
160 #endif
163 * This is basically a big decision tree
165 else if ((fhp[0] == 0) && (fhp[1] == 0)) {
166 /* bytes[0,1] == (0,0); rules out Ultrix, IRIX5, SUNOS5 */
167 /* probably rules out HP-UX, AIX unless they allow major=0 */
168 if ((fhp[2] == 0) && (fhp[3] == 0)) {
169 /* bytes[2,3] == (0,0); must be Auspex */
170 /* XXX or could be Ultrix+MASSBUS "hp" disk? */
171 fhtype = FHT_AUSPEX;
173 else {
175 * bytes[2,3] != (0,0); rules out Auspex, could be
176 * DECOSF, SUNOS4, or IRIX4
178 if ((fhp[4] != 0) && (fhp[5] == 0) &&
179 (fhp[8] == 12) && (fhp[9] == 0)) {
180 /* seems to be DECOSF, with minor == 0 */
181 fhtype = FHT_DECOSF;
183 else {
184 /* could be SUNOS4 or IRIX4 */
185 /* XXX the test of fhp[5] == 8 could be wrong */
186 if ((fhp[4] == 0) && (fhp[5] == 8) && (fhp[6] == 0) &&
187 (fhp[7] == 0)) {
188 /* looks like a length, not a file system typecode */
189 fhtype = FHT_IRIX4;
191 else {
192 /* by elimination */
193 fhtype = FHT_SUNOS4;
198 else {
200 * bytes[0,1] != (0,0); rules out Auspex, IRIX4, SUNOS4
201 * could be IRIX5, DECOSF, UCX, Ultrix, SUNOS5
202 * could be AIX, HP-UX
204 if ((fhp[2] == 0) && (fhp[3] == 0)) {
206 * bytes[2,3] == (0,0); rules out OSF, probably not UCX
207 * (unless the exported device name is just one letter!),
208 * could be Ultrix, IRIX5, AIX, or SUNOS5
209 * might be HP-UX (depends on their values for minor devs)
211 if ((fhp[6] == 0) && (fhp[7] == 0)) {
212 fhtype = FHT_BSD44;
214 /*XXX we probably only need to test of these two bytes */
215 else if ((fhp[21] == 0) && (fhp[23] == 0)) {
216 fhtype = FHT_ULTRIX;
218 else {
219 /* Could be SUNOS5/IRIX5, maybe AIX */
220 /* XXX no obvious difference between SUNOS5 and IRIX5 */
221 if (fhp[9] == 10)
222 fhtype = FHT_SUNOS5;
223 /* XXX what about AIX? */
226 else {
228 * bytes[2,3] != (0,0); rules out Ultrix, could be
229 * DECOSF, SUNOS5, IRIX5, AIX, HP-UX, or UCX
231 if ((fhp[8] == 12) && (fhp[9] == 0)) {
232 fhtype = FHT_DECOSF;
234 else if ((fhp[8] == 0) && (fhp[9] == 10)) {
235 /* could be SUNOS5/IRIX5, AIX, HP-UX */
236 if ((fhp[7] == 0) && (fhp[6] == 0) &&
237 (fhp[5] == 0) && (fhp[4] == 0)) {
238 /* XXX is this always true of HP-UX? */
239 fhtype = FHT_HPUX9;
241 else if (fhp[7] == 2) {
242 /* This would be MNT_NFS on AIX, which is impossible */
243 fhtype = FHT_SUNOS5; /* or maybe IRIX5 */
245 else {
247 * XXX Could be SUNOS5/IRIX5 or AIX. I don't
248 * XXX see any way to disambiguate these, so
249 * XXX I'm going with the more likely guess.
250 * XXX Sorry, Big Blue.
252 fhtype = FHT_SUNOS5; /* or maybe IRIX5 */
255 else {
256 if (is_UCX(fhp)) {
257 fhtype = FHT_VMSUCX;
259 else {
260 fhtype = FHT_UNKNOWN;
266 /* XXX still needs to handle SUNOS3 */
268 switch (fhtype) {
269 case FHT_AUSPEX:
270 fsidp->Fsid_dev.Minor = fhp[7];
271 fsidp->Fsid_dev.Major = fhp[6];
272 fsidp->fsid_code = 0;
274 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
275 *inop = temp;
277 if (osnamep)
278 *osnamep = "Auspex";
279 break;
281 case FHT_BSD44:
282 fsidp->Fsid_dev.Minor = fhp[0];
283 fsidp->Fsid_dev.Major = fhp[1];
284 fsidp->fsid_code = 0;
286 temp = make_uint32(fhp[15], fhp[14], fhp[13], fhp[12]);
287 *inop = temp;
289 if (osnamep)
290 *osnamep = "BSD 4.4";
291 break;
293 case FHT_DECOSF:
294 fsidp->fsid_code = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
295 /* XXX could ignore 3 high-order bytes */
297 temp = make_uint32(fhp[3], fhp[2], fhp[1], fhp[0]);
298 fsidp->Fsid_dev.Minor = temp & 0xFFFFF;
299 fsidp->Fsid_dev.Major = (temp>>20) & 0xFFF;
301 temp = make_uint32(fhp[15], fhp[14], fhp[13], fhp[12]);
302 *inop = temp;
303 if (osnamep)
304 *osnamep = "OSF";
305 break;
307 case FHT_IRIX4:
308 fsidp->Fsid_dev.Minor = fhp[3];
309 fsidp->Fsid_dev.Major = fhp[2];
310 fsidp->fsid_code = 0;
312 temp = make_uint32(fhp[8], fhp[9], fhp[10], fhp[11]);
313 *inop = temp;
315 if (osnamep)
316 *osnamep = "IRIX4";
317 break;
319 case FHT_IRIX5:
320 fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
321 fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
322 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
324 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
325 *inop = temp;
327 if (osnamep)
328 *osnamep = "IRIX5";
329 break;
331 #ifdef notdef
332 case FHT_SUNOS3:
334 * XXX - none of the heuristics above return this.
335 * Are there any SunOS 3.x systems around to care about?
337 if (osnamep)
338 *osnamep = "SUNOS3";
339 break;
340 #endif
342 case FHT_SUNOS4:
343 fsidp->Fsid_dev.Minor = fhp[3];
344 fsidp->Fsid_dev.Major = fhp[2];
345 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
347 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
348 *inop = temp;
350 if (osnamep)
351 *osnamep = "SUNOS4";
352 break;
354 case FHT_SUNOS5:
355 temp = make_uint16(fhp[0], fhp[1]);
356 fsidp->Fsid_dev.Major = (temp>>2) & 0x3FFF;
357 temp = make_uint24(fhp[1], fhp[2], fhp[3]);
358 fsidp->Fsid_dev.Minor = temp & 0x3FFFF;
359 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
361 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
362 *inop = temp;
364 if (osnamep)
365 *osnamep = "SUNOS5";
366 break;
368 case FHT_ULTRIX:
369 fsidp->fsid_code = 0;
370 fsidp->Fsid_dev.Minor = fhp[0];
371 fsidp->Fsid_dev.Major = fhp[1];
373 temp = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
374 *inop = temp;
375 if (osnamep)
376 *osnamep = "Ultrix";
377 break;
379 case FHT_VMSUCX:
380 /* No numeric file system ID, so hash on the device-name */
381 if (sizeof(*fsidp) >= 14) {
382 if (sizeof(*fsidp) > 14)
383 memset((char *)fsidp, 0, sizeof(*fsidp));
384 /* just use the whole thing */
385 memcpy((char *)fsidp, (char *)fh, 14);
387 else {
388 u_int32_t tempa[4]; /* at least 16 bytes, maybe more */
390 memset((char *)tempa, 0, sizeof(tempa));
391 memcpy((char *)tempa, (char *)fh, 14); /* ensure alignment */
392 fsidp->Fsid_dev.Minor = tempa[0] + (tempa[1]<<1);
393 fsidp->Fsid_dev.Major = tempa[2] + (tempa[3]<<1);
394 fsidp->fsid_code = 0;
397 /* VMS file ID is: (RVN, FidHi, FidLo) */
398 *inop = make_uint32(fhp[26], fhp[27], fhp[23], fhp[22]);
400 /* Caller must save (and null-terminate?) this value */
401 if (fsnamep)
402 *fsnamep = (char *)&(fhp[1]);
404 if (osnamep)
405 *osnamep = "VMS";
406 break;
408 case FHT_AIX32:
409 fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
410 fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
411 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
413 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
414 *inop = temp;
416 if (osnamep)
417 *osnamep = "AIX32";
418 break;
420 case FHT_HPUX9:
421 fsidp->Fsid_dev.Major = fhp[0];
422 temp = make_uint24(fhp[1], fhp[2], fhp[3]);
423 fsidp->Fsid_dev.Minor = temp;
424 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
426 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
427 *inop = temp;
429 if (osnamep)
430 *osnamep = "HPUX9";
431 break;
433 case FHT_UNKNOWN:
434 #ifdef DEBUG
435 /* XXX debugging */
436 for (i = 0; i < 32; i++)
437 (void)fprintf(stderr, "%x.", fhp[i]);
438 (void)fprintf(stderr, "\n");
439 #endif
440 /* Save the actual handle, so it can be display with -u */
441 for (i = 0; i < 32; i++)
442 (void)snprintf(&(fsidp->Opaque_Handle[i*2]), 3, "%.2X", fhp[i]);
444 /* XXX for now, give "bogus" values to aid debugging */
445 fsidp->fsid_code = 0;
446 fsidp->Fsid_dev.Minor = 257;
447 fsidp->Fsid_dev.Major = 257;
448 *inop = 1;
450 /* display will show this string instead of (257,257) */
451 if (fsnamep)
452 *fsnamep = "Unknown";
454 if (osnamep)
455 *osnamep = "Unknown";
456 break;
462 * Is this a VMS UCX file handle?
463 * Check for:
464 * (1) leading code byte [XXX not yet]
465 * (2) followed by string of printing chars & spaces
466 * (3) followed by string of nulls
468 static int
469 is_UCX(fhp)
470 const unsigned char *fhp;
472 register int i;
473 int seen_null = 0;
475 for (i = 1; i < 14; i++) {
476 if (isprint(fhp[i])) {
477 if (seen_null)
478 return(0);
479 else
480 continue;
482 else if (fhp[i] == 0) {
483 seen_null = 1;
484 continue;
486 else
487 return(0);
490 return(1);