macinfo.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * TAP-Windows -- A kernel driver to provide virtual tap
  3. * device functionality on Windows.
  4. *
  5. * This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
  6. *
  7. * This source code is Copyright (C) 2002-2014 OpenVPN Technologies, Inc.,
  8. * and is released under the GPL version 2 (see below).
  9. *
  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.
  13. *
  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.
  18. *
  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
  23. */
  24. #include "tap.h"
  25. int
  26. HexStringToDecimalInt (const int p_Character)
  27. {
  28. int l_Value = 0;
  29. if (p_Character >= 'A' && p_Character <= 'F')
  30. l_Value = (p_Character - 'A') + 10;
  31. else if (p_Character >= 'a' && p_Character <= 'f')
  32. l_Value = (p_Character - 'a') + 10;
  33. else if (p_Character >= '0' && p_Character <= '9')
  34. l_Value = p_Character - '0';
  35. return l_Value;
  36. }
  37. BOOLEAN
  38. ParseMAC (MACADDR dest, const char *src)
  39. {
  40. int c;
  41. int mac_index = 0;
  42. BOOLEAN high_digit = FALSE;
  43. int delim_action = 1;
  44. ASSERT (src);
  45. ASSERT (dest);
  46. CLEAR_MAC (dest);
  47. while (c = *src++)
  48. {
  49. if (IsMacDelimiter (c))
  50. {
  51. mac_index += delim_action;
  52. high_digit = FALSE;
  53. delim_action = 1;
  54. }
  55. else if (IsHexDigit (c))
  56. {
  57. const int digit = HexStringToDecimalInt (c);
  58. if (mac_index < sizeof (MACADDR))
  59. {
  60. if (!high_digit)
  61. {
  62. dest[mac_index] = (char)(digit);
  63. high_digit = TRUE;
  64. delim_action = 1;
  65. }
  66. else
  67. {
  68. dest[mac_index] = (char)(dest[mac_index] * 16 + digit);
  69. ++mac_index;
  70. high_digit = FALSE;
  71. delim_action = 0;
  72. }
  73. }
  74. else
  75. return FALSE;
  76. }
  77. else
  78. return FALSE;
  79. }
  80. return (mac_index + delim_action) >= sizeof (MACADDR);
  81. }
  82. /*
  83. * Generate a MAC using the GUID in the adapter name.
  84. *
  85. * The mac is constructed as 00:FF:xx:xx:xx:xx where
  86. * the Xs are taken from the first 32 bits of the GUID in the
  87. * adapter name. This is similar to the Linux 2.4 tap MAC
  88. * generator, except linux uses 32 random bits for the Xs.
  89. *
  90. * In general, this solution is reasonable for most
  91. * applications except for very large bridged TAP networks,
  92. * where the probability of address collisions becomes more
  93. * than infintesimal.
  94. *
  95. * Using the well-known "birthday paradox", on a 1000 node
  96. * network the probability of collision would be
  97. * 0.000116292153. On a 10,000 node network, the probability
  98. * of collision would be 0.01157288998621678766.
  99. */
  100. VOID
  101. GenerateRandomMac(
  102. __in MACADDR mac,
  103. __in const unsigned char *adapter_name
  104. )
  105. {
  106. unsigned const char *cp = adapter_name;
  107. unsigned char c;
  108. unsigned int i = 2;
  109. unsigned int byte = 0;
  110. int brace = 0;
  111. int state = 0;
  112. CLEAR_MAC (mac);
  113. mac[0] = 0x00;
  114. mac[1] = 0xFF;
  115. while (c = *cp++)
  116. {
  117. if (i >= sizeof (MACADDR))
  118. break;
  119. if (c == '{')
  120. brace = 1;
  121. if (IsHexDigit (c) && brace)
  122. {
  123. const unsigned int digit = HexStringToDecimalInt (c);
  124. if (state)
  125. {
  126. byte <<= 4;
  127. byte |= digit;
  128. mac[i++] = (unsigned char) byte;
  129. state = 0;
  130. }
  131. else
  132. {
  133. byte = digit;
  134. state = 1;
  135. }
  136. }
  137. }
  138. }
  139. VOID
  140. GenerateRelatedMAC(
  141. __in MACADDR dest,
  142. __in const MACADDR src,
  143. __in const int delta
  144. )
  145. {
  146. ETH_COPY_NETWORK_ADDRESS (dest, src);
  147. dest[2] += (UCHAR) delta;
  148. }