2 * parsenfsfh.c - portable parser for NFS file handles
3 * uses all sorts of heuristics
6 * Digital Equipment Corporation
7 * Western Research Laboratory
13 static const char rcsid[] =
14 "@(#) $Header: /tcpdump/master/tcpdump/parsenfsfh.c,v 1.16 1999/11/21 09:36:47 fenner Exp $ (LBL)";
21 #include <sys/types.h>
31 #include "interface.h"
35 * This routine attempts to parse a file handle (in network byte order),
36 * using heuristics to guess what kind of format it is in. See the
37 * file "fhandle_layouts" for a detailed description of the various
38 * patterns we know about.
40 * The file handle is parsed into our internal representation of a
41 * file-system id, and an internal representation of an inode-number.
58 /* Nasty hack to keep the Ultrix C compiler from emitting bogus warnings */
59 #define XFF(x) ((u_int32_t)(x))
64 #define make_uint32(msb,b,c,lsb)\
65 (XFF(lsb) + (XFF(c)<<8) + (XFF(b)<<16) + (XFF(msb)<<24))
67 #define make_uint24(msb,b, lsb)\
68 (XFF(lsb) + (XFF(b)<<8) + (XFF(msb)<<16))
70 #define make_uint16(msb,lsb)\
71 (XFF(lsb) + (XFF(msb)<<8))
74 /* or other 64-bit systems */
75 #define make_uint48(msb,b,c,d,e,lsb)\
76 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24) + ((b)<<32) + ((msb)<<40))
78 /* on 32-bit systems ignore high-order bits */
79 #define make_uint48(msb,b,c,d,e,lsb)\
80 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24))
83 static int is_UCX(unsigned char *);
86 Parse_fh(fh, len, fsidp, inop, osnamep, fsnamep, ourself)
91 char **osnamep; /* if non-NULL, return OS name here */
92 char **fsnamep; /* if non-NULL, return server fs name here (for VMS) */
93 int ourself; /* true if file handle was generated on this host */
95 register unsigned char *fhp = (unsigned char *)fh;
97 int fhtype = FHT_UNKNOWN;
100 /* File handle generated on this host, no need for guessing */
124 * This is basically a big decision tree
126 else if ((fhp[0] == 0) && (fhp[1] == 0)) {
127 /* bytes[0,1] == (0,0); rules out Ultrix, IRIX5, SUNOS5 */
128 /* probably rules out HP-UX, AIX unless they allow major=0 */
129 if ((fhp[2] == 0) && (fhp[3] == 0)) {
130 /* bytes[2,3] == (0,0); must be Auspex */
131 /* XXX or could be Ultrix+MASSBUS "hp" disk? */
136 * bytes[2,3] != (0,0); rules out Auspex, could be
137 * DECOSF, SUNOS4, or IRIX4
139 if ((fhp[4] != 0) && (fhp[5] == 0) &&
140 (fhp[8] == 12) && (fhp[9] == 0)) {
141 /* seems to be DECOSF, with minor == 0 */
145 /* could be SUNOS4 or IRIX4 */
146 /* XXX the test of fhp[5] == 8 could be wrong */
147 if ((fhp[4] == 0) && (fhp[5] == 8) && (fhp[6] == 0) &&
149 /* looks like a length, not a file system typecode */
161 * bytes[0,1] != (0,0); rules out Auspex, IRIX4, SUNOS4
162 * could be IRIX5, DECOSF, UCX, Ultrix, SUNOS5
163 * could be AIX, HP-UX
165 if ((fhp[2] == 0) && (fhp[3] == 0)) {
167 * bytes[2,3] == (0,0); rules out OSF, probably not UCX
168 * (unless the exported device name is just one letter!),
169 * could be Ultrix, IRIX5, AIX, or SUNOS5
170 * might be HP-UX (depends on their values for minor devs)
172 /*XXX we probably only need to test of these two bytes */
173 if ((fhp[21] == 0) && (fhp[23] == 0)) {
177 /* Could be SUNOS5/IRIX5, maybe AIX */
178 /* XXX no obvious difference between SUNOS5 and IRIX5 */
181 /* XXX what about AIX? */
186 * bytes[2,3] != (0,0); rules out Ultrix, could be
187 * DECOSF, SUNOS5, IRIX5, AIX, HP-UX, or UCX
189 if ((fhp[8] == 12) && (fhp[9] == 0)) {
192 else if ((fhp[8] == 0) && (fhp[9] == 10)) {
193 /* could be SUNOS5/IRIX5, AIX, HP-UX */
194 if ((fhp[7] == 0) && (fhp[6] == 0) &&
195 (fhp[5] == 0) && (fhp[4] == 0)) {
196 /* XXX is this always true of HP-UX? */
199 else if (fhp[7] == 2) {
200 /* This would be MNT_NFS on AIX, which is impossible */
201 fhtype = FHT_SUNOS5; /* or maybe IRIX5 */
205 * XXX Could be SUNOS5/IRIX5 or AIX. I don't
206 * XXX see any way to disambiguate these, so
207 * XXX I'm going with the more likely guess.
208 * XXX Sorry, Big Blue.
210 fhtype = FHT_SUNOS5; /* or maybe IRIX5 */
218 fhtype = FHT_UNKNOWN;
224 /* XXX still needs to handle SUNOS3 */
228 fsidp->Fsid_dev.Minor = fhp[7];
229 fsidp->Fsid_dev.Major = fhp[6];
230 fsidp->fsid_code = 0;
232 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
240 fsidp->fsid_code = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
241 /* XXX could ignore 3 high-order bytes */
243 temp = make_uint32(fhp[3], fhp[2], fhp[1], fhp[0]);
244 fsidp->Fsid_dev.Minor = temp & 0xFFFFF;
245 fsidp->Fsid_dev.Major = (temp>>20) & 0xFFF;
247 temp = make_uint32(fhp[15], fhp[14], fhp[13], fhp[12]);
254 fsidp->Fsid_dev.Minor = fhp[3];
255 fsidp->Fsid_dev.Major = fhp[2];
256 fsidp->fsid_code = 0;
258 temp = make_uint32(fhp[8], fhp[9], fhp[10], fhp[11]);
266 fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
267 fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
268 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
270 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
283 fsidp->Fsid_dev.Minor = fhp[3];
284 fsidp->Fsid_dev.Major = fhp[2];
285 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
287 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
295 temp = make_uint16(fhp[0], fhp[1]);
296 fsidp->Fsid_dev.Major = (temp>>2) & 0x3FFF;
297 temp = make_uint24(fhp[1], fhp[2], fhp[3]);
298 fsidp->Fsid_dev.Minor = temp & 0x3FFFF;
299 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
301 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
309 fsidp->fsid_code = 0;
310 fsidp->Fsid_dev.Minor = fhp[0];
311 fsidp->Fsid_dev.Major = fhp[1];
313 temp = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
320 /* No numeric file system ID, so hash on the device-name */
321 if (sizeof(*fsidp) >= 14) {
322 if (sizeof(*fsidp) > 14)
323 memset((char *)fsidp, 0, sizeof(*fsidp));
324 /* just use the whole thing */
325 memcpy((char *)fsidp, (char *)fh, 14);
328 u_int32_t tempa[4]; /* at least 16 bytes, maybe more */
330 memset((char *)tempa, 0, sizeof(tempa));
331 memcpy((char *)tempa, (char *)fh, 14); /* ensure alignment */
332 fsidp->Fsid_dev.Minor = tempa[0] + (tempa[1]<<1);
333 fsidp->Fsid_dev.Major = tempa[2] + (tempa[3]<<1);
334 fsidp->fsid_code = 0;
337 /* VMS file ID is: (RVN, FidHi, FidLo) */
338 *inop = make_uint32(fhp[26], fhp[27], fhp[23], fhp[22]);
340 /* Caller must save (and null-terminate?) this value */
342 *fsnamep = (char *)&(fhp[1]);
349 fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
350 fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
351 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
353 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
361 fsidp->Fsid_dev.Major = fhp[0];
362 temp = make_uint24(fhp[1], fhp[2], fhp[3]);
363 fsidp->Fsid_dev.Minor = temp;
364 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
366 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
378 for (i = 0; i < 32; i++)
379 (void)fprintf(stderr, "%x.", fhp[i]);
380 (void)fprintf(stderr, "\n");
383 /* XXX for now, give "bogus" values to aid debugging */
384 fsidp->fsid_code = 0;
385 fsidp->Fsid_dev.Minor = 257;
386 fsidp->Fsid_dev.Major = 257;
389 /* display will show this string instead of (257,257) */
391 *fsnamep = "Unknown";
394 *osnamep = "Unknown";
401 * Is this a VMS UCX file handle?
403 * (1) leading code byte [XXX not yet]
404 * (2) followed by string of printing chars & spaces
405 * (3) followed by string of nulls
414 for (i = 1; i < 14; i++) {
415 if (isprint(fhp[i])) {
421 else if (fhp[i] == 0) {