Get rid of "Interrupted service call" messages in syslog
[tomato.git] / release / src / router / openvpn / tap-win32 / macinfo.c
blob49f21f95755de613d92cc85fa4b07b3bceba71b3
1 /*
2 * TAP-Win32/TAP-Win64 -- A kernel driver to provide virtual tap
3 * device functionality on Windows.
5 * This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
7 * This source code is Copyright (C) 2002-2009 OpenVPN Technologies, Inc.,
8 * and is released under the GPL version 2 (see below), however due
9 * to the extra costs of supporting Windows Vista, OpenVPN Solutions
10 * LLC reserves the right to change the terms of the TAP-Win32/TAP-Win64
11 * license for versions 9.1 and higher prior to the official release of
12 * OpenVPN 2.1.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2
16 * as published by the Free Software Foundation.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program (see the file COPYING included with this
25 * distribution); if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "macinfo.h"
31 int
32 HexStringToDecimalInt (const int p_Character)
34 int l_Value = 0;
36 if (p_Character >= 'A' && p_Character <= 'F')
37 l_Value = (p_Character - 'A') + 10;
38 else if (p_Character >= 'a' && p_Character <= 'f')
39 l_Value = (p_Character - 'a') + 10;
40 else if (p_Character >= '0' && p_Character <= '9')
41 l_Value = p_Character - '0';
43 return l_Value;
46 BOOLEAN
47 ParseMAC (MACADDR dest, const char *src)
49 int c;
50 int mac_index = 0;
51 BOOLEAN high_digit = FALSE;
52 int delim_action = 1;
54 MYASSERT (src);
55 MYASSERT (dest);
57 CLEAR_MAC (dest);
59 while (c = *src++)
61 if (IsMacDelimiter (c))
63 mac_index += delim_action;
64 high_digit = FALSE;
65 delim_action = 1;
67 else if (IsHexDigit (c))
69 const int digit = HexStringToDecimalInt (c);
70 if (mac_index < sizeof (MACADDR))
72 if (!high_digit)
74 dest[mac_index] = (char)(digit);
75 high_digit = TRUE;
76 delim_action = 1;
78 else
80 dest[mac_index] = (char)(dest[mac_index] * 16 + digit);
81 ++mac_index;
82 high_digit = FALSE;
83 delim_action = 0;
86 else
87 return FALSE;
89 else
90 return FALSE;
93 return (mac_index + delim_action) >= sizeof (MACADDR);
97 * Generate a MAC using the GUID in the adapter name.
99 * The mac is constructed as 00:FF:xx:xx:xx:xx where
100 * the Xs are taken from the first 32 bits of the GUID in the
101 * adapter name. This is similar to the Linux 2.4 tap MAC
102 * generator, except linux uses 32 random bits for the Xs.
104 * In general, this solution is reasonable for most
105 * applications except for very large bridged TAP networks,
106 * where the probability of address collisions becomes more
107 * than infintesimal.
109 * Using the well-known "birthday paradox", on a 1000 node
110 * network the probability of collision would be
111 * 0.000116292153. On a 10,000 node network, the probability
112 * of collision would be 0.01157288998621678766.
115 VOID GenerateRandomMac (MACADDR mac, const unsigned char *adapter_name)
117 unsigned const char *cp = adapter_name;
118 unsigned char c;
119 unsigned int i = 2;
120 unsigned int byte = 0;
121 int brace = 0;
122 int state = 0;
124 CLEAR_MAC (mac);
126 mac[0] = 0x00;
127 mac[1] = 0xFF;
129 while (c = *cp++)
131 if (i >= sizeof (MACADDR))
132 break;
133 if (c == '{')
134 brace = 1;
135 if (IsHexDigit (c) && brace)
137 const unsigned int digit = HexStringToDecimalInt (c);
138 if (state)
140 byte <<= 4;
141 byte |= digit;
142 mac[i++] = (unsigned char) byte;
143 state = 0;
145 else
147 byte = digit;
148 state = 1;
154 VOID GenerateRelatedMAC (MACADDR dest, const MACADDR src, const int delta)
156 COPY_MAC (dest, src);
157 dest[2] += (UCHAR) delta;