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