afs: Change VerifyVCache2 calls to VerifyVCache
[openafs.git] / src / afs / afs_util.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 /*
11  * afs_util.c - miscellaneous AFS client utility functions
12  *
13  * Implements:
14  */
15 #include <afsconfig.h>
16 #include "afs/param.h"
17
18
19 #include "afs/stds.h"
20 #include "afs/sysincludes.h"    /* Standard vendor system headers */
21
22 #if !defined(UKERNEL)
23 #if !defined(AFS_LINUX20_ENV)
24 #include <net/if.h>
25 #endif
26 #include <netinet/in.h>
27
28 #ifdef AFS_SGI62_ENV
29 #include "h/hashing.h"
30 #endif
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) */
35
36 #include "afsincludes.h"        /* Afs-based standard headers */
37 #include "afs/afs_stats.h"      /* afs statistics */
38 #include "hcrypto/md5.h"
39
40 #if     defined(AFS_AIX_ENV)
41 #include <sys/fp_io.h>
42 #endif
43
44 afs_int32 afs_md5inum = 0;
45
46 #ifndef afs_cv2string
47 char *
48 afs_cv2string(char *ttp, afs_uint32 aval)
49 {
50     char *tp = ttp;
51     int i;
52     int any;
53
54     AFS_STATCNT(afs_cv2string);
55     any = 0;
56     *(--tp) = 0;
57     while (aval != 0) {
58         i = aval % 10;
59         *(--tp) = '0' + i;
60         aval /= 10;
61         any = 1;
62     }
63     if (!any)
64         *(--tp) = '0';
65     return tp;
66
67 }                               /*afs_cv2string */
68 #endif
69
70 /* not a generic strtoul replacement. for vol/vno/uniq, portable */
71
72 afs_int32
73 afs_strtoi_r(const char *str, char **endptr, afs_uint32 *ret)
74 {
75     char *x;
76
77     *ret = 0;
78     *endptr = (char *)str;
79
80     if (!str)
81         return ERANGE;
82
83     for (x = (char *)str; *x >= '0' && *x <= '9'; x++) {
84         /* Check for impending overflow */
85         if (*ret > 429496729) { /* ULONG_MAX/10 */
86             *ret = 0;
87             *endptr = (char *)str;
88             return EINVAL;
89         }
90
91         *ret = (*ret * 10) + (*x - '0');
92     }
93
94     *endptr = x;
95     return 0;
96 }
97
98 #ifndef afs_strcasecmp
99 int
100 afs_strcasecmp(const char *s1, const char *s2)
101 {
102     while (*s1 && *s2) {
103         char c1, c2;
104
105         c1 = *s1++;
106         c2 = *s2++;
107         if (c1 >= 'A' && c1 <= 'Z')
108             c1 += 0x20;
109         if (c2 >= 'A' && c2 <= 'Z')
110             c2 += 0x20;
111         if (c1 != c2)
112             return c1 - c2;
113     }
114
115     return *s1 - *s2;
116 }
117 #endif
118
119 #ifndef afs_strcat
120 char *
121 afs_strcat(char *s1, char *s2)
122 {
123     char *os1;
124
125     os1 = s1;
126     while (*s1++);
127     --s1;
128     while ((*s1++ = *s2++));
129     return (os1);
130 }
131 #endif
132
133 #ifdef AFS_OBSD34_ENV
134 char *
135 afs_strcpy(char *s1, char *s2)
136 {
137     char *os1;
138
139     os1 = s1;
140     while ((*s1++ = *s2++) != '\0');
141     return os1;
142 }
143 #endif
144
145 #ifndef afs_strchr
146 char *
147 afs_strchr(char *s, int c)
148 {
149     char *p;
150
151     for (p = s; *p; p++)
152         if (*p == c)
153             return p;
154     return NULL;
155 }
156 #endif
157 #ifndef afs_strrchr
158 char *
159 afs_strrchr(char *s, int c)
160 {
161     char *p = NULL;
162
163     do {
164         if (*s == c)
165             p = (char*) s;
166     } while (*s++);
167     return p;
168 }
169 #endif
170
171 char *
172 afs_strdup(char *s)
173 {
174     char *n;
175     int cc;
176
177     cc = strlen(s) + 1;
178     n = afs_osi_Alloc(cc);
179     if (n)
180         memcpy(n, s, cc);
181
182     return n;
183 }
184
185 void
186 print_internet_address(char *preamble, struct srvAddr *sa, char *postamble,
187                        int flag, int code, struct rx_connection *rxconn)
188 {
189     struct server *aserver = sa->server;
190     char *ptr = "\n";
191     afs_uint32 address;
192
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))
198                 ptr =
199                     " (multi-homed address; other same-host interfaces maybe up)\n";
200             else
201                 ptr = " (all multi-homed ip addresses down for the server)\n";
202         } else if (flag == 2) { /* server up mesg */
203             ptr =
204                 " (multi-homed address; other same-host interfaces may still be down)\n";
205         }
206     }
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);
210
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;
215         const char *errmsg;
216         if (rx_GetNetworkError(rxconn, &errorigin, &errtype, &errcode, &errmsg) == 0) {
217             const char *str1 = " (";
218             const char *str2 = ")";
219             if (!errmsg) {
220                 errmsg = str1 = str2 = "";
221             }
222             afs_warnall("afs: network error for %d.%d.%d.%d:%d: origin %d type %d code %d%s%s%s\n",
223                      (address >> 24),
224                      (address >> 16) & 0xff,
225                      (address >> 8) & 0xff,
226                      (address) & 0xff,
227                      (int)ntohs(sa->sa_portal),
228                      errorigin, errtype, errcode, str1, errmsg, str2);
229         }
230     }
231 }                               /*print_internet_address */
232
233 int
234 afs_noop(void)
235 {
236     AFS_STATCNT(afs_noop);
237     return EINVAL;
238 }
239
240 int
241 afs_badop(void)
242 {
243     AFS_STATCNT(afs_badop);
244     osi_Panic("afs bad vnode op");
245     AFS_UNREACHED(return 0);
246 }
247
248 /*
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.
254  */
255
256 afs_int32
257 afs_data_pointer_to_int32(const void *p)
258 {
259     union {
260         afs_int32 i32[sizeof(void *) / sizeof(afs_int32)];
261         const void *p;
262     } ip;
263
264     int i32_sub;                /* subscript of least significant afs_int32 in ip.i32[] */
265
266     /* set i32_sub */
267
268     {
269         /* used to determine the byte order of the system */
270
271         union {
272             char c[sizeof(int) / sizeof(char)];
273             int i;
274         } ci;
275
276         ci.i = 1;
277         if (ci.c[0] == 1) {
278             /* little-endian system */
279             i32_sub = 0;
280         } else {
281             /* big-endian system */
282             i32_sub = (sizeof ip.i32 / sizeof ip.i32[0]) - 1;
283         }
284     }
285
286     ip.p = p;
287     return ip.i32[i32_sub];
288 }
289
290 static_inline afs_int32
291 afs_calc_inum_md5(afs_int32 cell, afs_int32 volume, afs_int32 vnode)
292 {
293     afs_int32 ino = 0, vno = vnode;
294     char digest[16];
295     struct md5 ct;
296
297     if (afs_md5inum) {
298         int offset;
299         MD5_Init(&ct);
300         MD5_Update(&ct, &cell, 4);
301         MD5_Update(&ct, &volume, 4);
302         MD5_Update(&ct, &vnode, 4);
303         MD5_Final(digest, &ct);
304
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. */
309         for (offset = 0;
310              (ino == 0 || ino == 1) &&
311               offset + sizeof(ino) <= sizeof(digest);
312              offset++) {
313
314             memcpy(&ino, &digest[offset], sizeof(ino));
315             ino ^= (ino ^ vno) & 1;
316             ino &= 0x7fffffff;      /* Assumes 32 bit ino_t ..... */
317         }
318     }
319     return ino;
320 }
321
322 afs_int32
323 afs_calc_inum(afs_int32 cell, afs_int32 volume, afs_int32 vnode)
324 {
325     afs_int32 ino;
326
327     ino = afs_calc_inum_md5(cell, volume, vnode);
328
329     if (ino == 0 || ino == 1) {
330         ino = (volume << 16) + vnode;
331     }
332     ino &= 0x7fffffff;      /* Assumes 32 bit ino_t ..... */
333     return ino;
334 }