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
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
32 HexStringToDecimalInt (const int p_Character
)
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';
47 ParseMAC (MACADDR dest
, const char *src
)
51 BOOLEAN high_digit
= FALSE
;
61 if (IsMacDelimiter (c
))
63 mac_index
+= delim_action
;
67 else if (IsHexDigit (c
))
69 const int digit
= HexStringToDecimalInt (c
);
70 if (mac_index
< sizeof (MACADDR
))
74 dest
[mac_index
] = (char)(digit
);
80 dest
[mac_index
] = (char)(dest
[mac_index
] * 16 + digit
);
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
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
;
120 unsigned int byte
= 0;
131 if (i
>= sizeof (MACADDR
))
135 if (IsHexDigit (c
) && brace
)
137 const unsigned int digit
= HexStringToDecimalInt (c
);
142 mac
[i
++] = (unsigned char) byte
;
154 VOID
GenerateRelatedMAC (MACADDR dest
, const MACADDR src
, const int delta
)
156 COPY_MAC (dest
, src
);
157 dest
[2] += (UCHAR
) delta
;