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).
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 HexStringToDecimalInt (const int p_Character
)
32 if (p_Character
>= 'A' && p_Character
<= 'F')
33 l_Value
= (p_Character
- 'A') + 10;
34 else if (p_Character
>= 'a' && p_Character
<= 'f')
35 l_Value
= (p_Character
- 'a') + 10;
36 else if (p_Character
>= '0' && p_Character
<= '9')
37 l_Value
= p_Character
- '0';
43 ParseMAC (MACADDR dest
, const char *src
)
47 BOOLEAN high_digit
= FALSE
;
57 if (IsMacDelimiter (c
))
59 mac_index
+= delim_action
;
63 else if (IsHexDigit (c
))
65 const int digit
= HexStringToDecimalInt (c
);
66 if (mac_index
< sizeof (MACADDR
))
70 dest
[mac_index
] = (char)(digit
);
76 dest
[mac_index
] = (char)(dest
[mac_index
] * 16 + digit
);
89 return (mac_index
+ delim_action
) >= sizeof (MACADDR
);
93 * Generate a MAC using the GUID in the adapter name.
95 * The mac is constructed as 00:FF:xx:xx:xx:xx where
96 * the Xs are taken from the first 32 bits of the GUID in the
97 * adapter name. This is similar to the Linux 2.4 tap MAC
98 * generator, except linux uses 32 random bits for the Xs.
100 * In general, this solution is reasonable for most
101 * applications except for very large bridged TAP networks,
102 * where the probability of address collisions becomes more
105 * Using the well-known "birthday paradox", on a 1000 node
106 * network the probability of collision would be
107 * 0.000116292153. On a 10,000 node network, the probability
108 * of collision would be 0.01157288998621678766.
111 VOID
GenerateRandomMac (MACADDR mac
, const unsigned char *adapter_name
)
113 unsigned const char *cp
= adapter_name
;
116 unsigned int byte
= 0;
127 if (i
>= sizeof (MACADDR
))
131 if (IsHexDigit (c
) && brace
)
133 const unsigned int digit
= HexStringToDecimalInt (c
);
138 mac
[i
++] = (unsigned char) byte
;
150 VOID
GenerateRelatedMAC (MACADDR dest
, const MACADDR src
, const int delta
)
152 COPY_MAC (dest
, src
);
153 dest
[2] += (UCHAR
) delta
;