2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * afs_util.c - miscellaneous AFS client utility functions
15 #include <afsconfig.h>
16 #include "afs/param.h"
20 #include "afs/sysincludes.h" /* Standard vendor system headers */
23 #if !defined(AFS_LINUX20_ENV)
26 #include <netinet/in.h>
29 #include "h/hashing.h"
31 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV)
32 #include <netinet/in_var.h>
33 #endif /* ! AFS_HPUX110_ENV */
34 #endif /* !defined(UKERNEL) */
36 #include "afsincludes.h" /* Afs-based standard headers */
37 #include "afs/afs_stats.h" /* afs statistics */
38 #include "hcrypto/md5.h"
40 #if defined(AFS_AIX_ENV)
41 #include <sys/fp_io.h>
44 afs_int32 afs_md5inum = 0;
48 afs_cv2string(char *ttp, afs_uint32 aval)
54 AFS_STATCNT(afs_cv2string);
70 /* not a generic strtoul replacement. for vol/vno/uniq, portable */
73 afs_strtoi_r(const char *str, char **endptr, afs_uint32 *ret)
78 *endptr = (char *)str;
83 for (x = (char *)str; *x >= '0' && *x <= '9'; x++) {
84 /* Check for impending overflow */
85 if (*ret > 429496729) { /* ULONG_MAX/10 */
87 *endptr = (char *)str;
91 *ret = (*ret * 10) + (*x - '0');
98 #ifndef afs_strcasecmp
100 afs_strcasecmp(const char *s1, const char *s2)
107 if (c1 >= 'A' && c1 <= 'Z')
109 if (c2 >= 'A' && c2 <= 'Z')
121 afs_strcat(char *s1, char *s2)
128 while ((*s1++ = *s2++));
133 #ifdef AFS_OBSD34_ENV
135 afs_strcpy(char *s1, char *s2)
140 while ((*s1++ = *s2++) != '\0');
147 afs_strchr(char *s, int c)
159 afs_strrchr(char *s, int c)
178 n = afs_osi_Alloc(cc);
186 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
187 int flag, int code, struct rx_connection *rxconn)
189 struct server *aserver = sa->server;
193 AFS_STATCNT(print_internet_address);
194 address = ntohl(sa->sa_ip);
195 if (aserver->flags & SRVR_MULTIHOMED) {
196 if (flag == 1) { /* server down mesg */
197 if (!(aserver->flags & SRVR_ISDOWN))
199 " (multi-homed address; other same-host interfaces maybe up)\n";
201 ptr = " (all multi-homed ip addresses down for the server)\n";
202 } else if (flag == 2) { /* server up mesg */
204 " (multi-homed address; other same-host interfaces may still be down)\n";
207 afs_warnall("%s%d.%d.%d.%d in cell %s%s (code %d)%s", preamble, (address >> 24),
208 (address >> 16) & 0xff, (address >> 8) & 0xff, (address) & 0xff,
209 aserver->cell->cellName, postamble, code, ptr);
211 if (flag == 1 && rxconn) {
212 /* server was marked down, check Rx to see if this was possibly due to
213 * an ICMP error received from the network */
214 int errorigin, errtype, errcode;
216 if (rx_GetNetworkError(rxconn, &errorigin, &errtype, &errcode, &errmsg) == 0) {
217 const char *str1 = " (";
218 const char *str2 = ")";
220 errmsg = str1 = str2 = "";
222 afs_warnall("afs: network error for %d.%d.%d.%d:%d: origin %d type %d code %d%s%s%s\n",
224 (address >> 16) & 0xff,
225 (address >> 8) & 0xff,
227 (int)ntohs(sa->sa_portal),
228 errorigin, errtype, errcode, str1, errmsg, str2);
231 } /*print_internet_address */
236 AFS_STATCNT(afs_noop);
243 AFS_STATCNT(afs_badop);
244 osi_Panic("afs bad vnode op");
245 AFS_UNREACHED(return 0);
249 * afs_data_pointer_to_int32() - returns least significant afs_int32 of the
250 * given data pointer, without triggering "cast truncates pointer"
251 * warnings. We use this where we explicitly don't care whether a
252 * pointer is truncated -- it loses information where a pointer is
253 * larger than an afs_int32.
257 afs_data_pointer_to_int32(const void *p)
260 afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
264 int i32_sub; /* subscript of least significant afs_int32 in ip.i32[] */
269 /* used to determine the byte order of the system */
272 char c[sizeof(int) / sizeof(char)];
278 /* little-endian system */
281 /* big-endian system */
282 i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
287 return ip.i32[i32_sub];
290 static_inline afs_int32
291 afs_calc_inum_md5(afs_int32 cell, afs_int32 volume, afs_int32 vnode)
293 afs_int32 ino = 0, vno = vnode;
300 MD5_Update(&ct, &cell, 4);
301 MD5_Update(&ct, &volume, 4);
302 MD5_Update(&ct, &vnode, 4);
303 MD5_Final(digest, &ct);
305 /* Userspace may react oddly to an inode number of 0 or 1, so keep
306 * reading more of the md5 digest if we get back one of those.
307 * Make sure not to read beyond the end of the digest; if we somehow
308 * still have a 0, we will fall through to the non-md5 calculation. */
310 (ino == 0 || ino == 1) &&
311 offset + sizeof(ino) <= sizeof(digest);
314 memcpy(&ino, &digest[offset], sizeof(ino));
315 ino ^= (ino ^ vno) & 1;
316 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
323 afs_calc_inum(afs_int32 cell, afs_int32 volume, afs_int32 vnode)
327 ino = afs_calc_inum_md5(cell, volume, vnode);
329 if (ino == 0 || ino == 1) {
330 ino = (volume << 16) + vnode;
332 ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */