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