[PATCH] i386: Drop -traditional in arch/i386/boot
[pv_ops_mirror.git] / net / core / utils.c
blobadecfd281ae98d6b8239eada1b26de0dbe459890
1 /*
2 * Generic address resultion entity
4 * Authors:
5 * net_random Alan Cox
6 * net_ratelimit Andi Kleen
7 * in{4,6}_pton YOSHIFUJI Hideaki, Copyright (C)2006 USAGI/WIDE Project
9 * Created by Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/module.h>
18 #include <linux/jiffies.h>
19 #include <linux/kernel.h>
20 #include <linux/inet.h>
21 #include <linux/mm.h>
22 #include <linux/net.h>
23 #include <linux/string.h>
24 #include <linux/types.h>
25 #include <linux/random.h>
26 #include <linux/percpu.h>
27 #include <linux/init.h>
29 #include <asm/byteorder.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
33 int net_msg_cost __read_mostly = 5*HZ;
34 int net_msg_burst __read_mostly = 10;
35 int net_msg_warn __read_mostly = 1;
36 EXPORT_SYMBOL(net_msg_warn);
39 * All net warning printk()s should be guarded by this function.
41 int net_ratelimit(void)
43 return __printk_ratelimit(net_msg_cost, net_msg_burst);
45 EXPORT_SYMBOL(net_ratelimit);
48 * Convert an ASCII string to binary IP.
49 * This is outside of net/ipv4/ because various code that uses IP addresses
50 * is otherwise not dependent on the TCP/IP stack.
53 __be32 in_aton(const char *str)
55 unsigned long l;
56 unsigned int val;
57 int i;
59 l = 0;
60 for (i = 0; i < 4; i++)
62 l <<= 8;
63 if (*str != '\0')
65 val = 0;
66 while (*str != '\0' && *str != '.' && *str != '\n')
68 val *= 10;
69 val += *str - '0';
70 str++;
72 l |= val;
73 if (*str != '\0')
74 str++;
77 return(htonl(l));
80 EXPORT_SYMBOL(in_aton);
82 #define IN6PTON_XDIGIT 0x00010000
83 #define IN6PTON_DIGIT 0x00020000
84 #define IN6PTON_COLON_MASK 0x00700000
85 #define IN6PTON_COLON_1 0x00100000 /* single : requested */
86 #define IN6PTON_COLON_2 0x00200000 /* second : requested */
87 #define IN6PTON_COLON_1_2 0x00400000 /* :: requested */
88 #define IN6PTON_DOT 0x00800000 /* . */
89 #define IN6PTON_DELIM 0x10000000
90 #define IN6PTON_NULL 0x20000000 /* first/tail */
91 #define IN6PTON_UNKNOWN 0x40000000
93 static inline int digit2bin(char c, int delim)
95 if (c == delim || c == '\0')
96 return IN6PTON_DELIM;
97 if (c == '.')
98 return IN6PTON_DOT;
99 if (c >= '0' && c <= '9')
100 return (IN6PTON_DIGIT | (c - '0'));
101 return IN6PTON_UNKNOWN;
104 static inline int xdigit2bin(char c, int delim)
106 if (c == delim || c == '\0')
107 return IN6PTON_DELIM;
108 if (c == ':')
109 return IN6PTON_COLON_MASK;
110 if (c == '.')
111 return IN6PTON_DOT;
112 if (c >= '0' && c <= '9')
113 return (IN6PTON_XDIGIT | IN6PTON_DIGIT| (c - '0'));
114 if (c >= 'a' && c <= 'f')
115 return (IN6PTON_XDIGIT | (c - 'a' + 10));
116 if (c >= 'A' && c <= 'F')
117 return (IN6PTON_XDIGIT | (c - 'A' + 10));
118 if (delim == -1)
119 return IN6PTON_DELIM;
120 return IN6PTON_UNKNOWN;
123 int in4_pton(const char *src, int srclen,
124 u8 *dst,
125 int delim, const char **end)
127 const char *s;
128 u8 *d;
129 u8 dbuf[4];
130 int ret = 0;
131 int i;
132 int w = 0;
134 if (srclen < 0)
135 srclen = strlen(src);
136 s = src;
137 d = dbuf;
138 i = 0;
139 while(1) {
140 int c;
141 c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
142 if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM))) {
143 goto out;
145 if (c & (IN6PTON_DOT | IN6PTON_DELIM)) {
146 if (w == 0)
147 goto out;
148 *d++ = w & 0xff;
149 w = 0;
150 i++;
151 if (c & IN6PTON_DELIM) {
152 if (i != 4)
153 goto out;
154 break;
156 goto cont;
158 w = (w * 10) + c;
159 if ((w & 0xffff) > 255) {
160 goto out;
162 cont:
163 if (i >= 4)
164 goto out;
165 s++;
166 srclen--;
168 ret = 1;
169 memcpy(dst, dbuf, sizeof(dbuf));
170 out:
171 if (end)
172 *end = s;
173 return ret;
176 EXPORT_SYMBOL(in4_pton);
178 int in6_pton(const char *src, int srclen,
179 u8 *dst,
180 int delim, const char **end)
182 const char *s, *tok = NULL;
183 u8 *d, *dc = NULL;
184 u8 dbuf[16];
185 int ret = 0;
186 int i;
187 int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
188 int w = 0;
190 memset(dbuf, 0, sizeof(dbuf));
192 s = src;
193 d = dbuf;
194 if (srclen < 0)
195 srclen = strlen(src);
197 while (1) {
198 int c;
200 c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
201 if (!(c & state))
202 goto out;
203 if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
204 /* process one 16-bit word */
205 if (!(state & IN6PTON_NULL)) {
206 *d++ = (w >> 8) & 0xff;
207 *d++ = w & 0xff;
209 w = 0;
210 if (c & IN6PTON_DELIM) {
211 /* We've processed last word */
212 break;
215 * COLON_1 => XDIGIT
216 * COLON_2 => XDIGIT|DELIM
217 * COLON_1_2 => COLON_2
219 switch (state & IN6PTON_COLON_MASK) {
220 case IN6PTON_COLON_2:
221 dc = d;
222 state = IN6PTON_XDIGIT | IN6PTON_DELIM;
223 if (dc - dbuf >= sizeof(dbuf))
224 state |= IN6PTON_NULL;
225 break;
226 case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
227 state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
228 break;
229 case IN6PTON_COLON_1:
230 state = IN6PTON_XDIGIT;
231 break;
232 case IN6PTON_COLON_1_2:
233 state = IN6PTON_COLON_2;
234 break;
235 default:
236 state = 0;
238 tok = s + 1;
239 goto cont;
242 if (c & IN6PTON_DOT) {
243 ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s);
244 if (ret > 0) {
245 d += 4;
246 break;
248 goto out;
251 w = (w << 4) | (0xff & c);
252 state = IN6PTON_COLON_1 | IN6PTON_DELIM;
253 if (!(w & 0xf000)) {
254 state |= IN6PTON_XDIGIT;
256 if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
257 state |= IN6PTON_COLON_1_2;
258 state &= ~IN6PTON_DELIM;
260 if (d + 2 >= dbuf + sizeof(dbuf)) {
261 state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
263 cont:
264 if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
265 d + 4 == dbuf + sizeof(dbuf)) {
266 state |= IN6PTON_DOT;
268 if (d >= dbuf + sizeof(dbuf)) {
269 state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
271 s++;
272 srclen--;
275 i = 15; d--;
277 if (dc) {
278 while(d >= dc)
279 dst[i--] = *d--;
280 while(i >= dc - dbuf)
281 dst[i--] = 0;
282 while(i >= 0)
283 dst[i--] = *d--;
284 } else
285 memcpy(dst, dbuf, sizeof(dbuf));
287 ret = 1;
288 out:
289 if (end)
290 *end = s;
291 return ret;
294 EXPORT_SYMBOL(in6_pton);