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