1 /*
2 * Copyright (c) 1997-2012 Motonori Nakamura <motonori@wide.ad.jp>
3 * Copyright (c) 1997-2012 WIDE Project
4 * Copyright (c) 1997-2003 Kyoto University
5 * Copyright (c) 2012 National Institute of Informatics
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by WIDE Project and
20 * its contributors.
21 * 4. Neither the name of the Project, the University nor the names of
22 * its contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright (c) 1986, 1995-1997 Eric P. Allman
40 * Copyright (c) 1988, 1993
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72 #ifndef lint
73 static char *_id_ = "$Id: mxrand.c,v 1.9 2012/06/07 05:45:28 motonori Exp $";
74 static char sccsid[] = "@(#)domain.c 8.68 (Berkeley) 8/2/97 (with name server)";
75 #endif /* not lint */
76
77 # include "common.h"
78
79 /*
80 ** CURTIME -- return current time.
81 **
82 ** Parameters:
83 ** none.
84 **
85 ** Returns:
86 ** the current time.
87 **
88 ** Side Effects:
89 ** none.
90 */
91
92 time_t
curtime()93 curtime()
94 {
95 auto time_t t;
96
97 (void) time(&t);
98 return (t);
99 }
100
101 /*
102 ** MXRAND -- create a randomizer for equal MX preferences
103 **
104 ** If two MX hosts have equal preferences we want to randomize
105 ** the selection. But in order for signatures to be the same,
106 ** we need to randomize the same way each time. This function
107 ** computes a pseudo-random hash function from the host name.
108 **
109 ** Parameters:
110 ** host -- the name of the host.
111 **
112 ** Returns:
113 ** A random but repeatable value based on the host name.
114 **
115 ** Side Effects:
116 ** none.
117 */
118
119 int
mxrand(host1,host2)120 mxrand(host1, host2)
121 register char *host1, *host2;
122 {
123 int hfunc;
124 static unsigned int seed;
125
126 if (seed == 0)
127 {
128 seed = (int) curtime() & 0xffff;
129 if (seed == 0)
130 seed++;
131 }
132
133 #if 0
134 if (tTd(17, 9))
135 printf("mxrand(%s)", host);
136 #endif
137
138 hfunc = seed;
139
140 while (*host1 != '\0')
141 {
142 int c = *host1++;
143
144 if (isascii(c) && isupper(c))
145 c = tolower(c);
146 hfunc = ((hfunc << 1) ^ c) % 2003;
147 }
148
149 while (*host2 != '\0')
150 {
151 int c = *host2++;
152
153 if (isascii(c) && isupper(c))
154 c = tolower(c);
155 hfunc = ((hfunc << 1) ^ c) % 2003;
156 }
157
158 hfunc &= 0xff;
159 hfunc++;
160
161 #if 0
162 if (tTd(17, 9))
163 printf(" = %d\n", hfunc);
164 #endif
165 return hfunc;
166 }
167