Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
5.5 KiB

  1. /*
  2. * File: utils.c
  3. * Description: This file contains the implementation of some utility
  4. * functions for the NLB KD extensions.
  5. * Author: Created by shouse, 1.4.01
  6. */
  7. #include "nlbkd.h"
  8. #include "utils.h"
  9. #include "print.h"
  10. /*
  11. * Function: ErrorCheckSymbols
  12. * Description: Prints an error message when the symbols are bad.
  13. * Author: Created by shouse, 1.4.01 - copied largely from ndiskd.dll
  14. */
  15. VOID ErrorCheckSymbols (CHAR * symbol) {
  16. dprintf("NLBKD: Error: Could not access %s - check symbols for wlbs.sys\n", symbol);
  17. }
  18. /*
  19. * Function: mystrtok
  20. * Description: Tokenizes a string via a configurable list of tokens.
  21. * Author: Created by shouse, 1.4.01 - copied largely from ndiskd.dll
  22. */
  23. char * mystrtok (char * string, char * control) {
  24. static unsigned char * str;
  25. CHAR * p;
  26. CHAR * s;
  27. if (string) str = string;
  28. if (!str || (*str == '\0')) return NULL;
  29. for (; *str; str++) {
  30. for (s = control; *s; s++)
  31. if (*str == *s) break;
  32. if (*s == '\0') break;
  33. }
  34. if (*str == '\0') {
  35. str = NULL;
  36. return NULL;
  37. }
  38. for (p = str + 1; *p; p++) {
  39. for (s = control; *s; s++) {
  40. if(*p == *s) {
  41. s = str;
  42. *p = '\0';
  43. str = p + 1;
  44. return s;
  45. }
  46. }
  47. }
  48. s = str;
  49. str = NULL;
  50. return s;
  51. }
  52. /*
  53. * Function: GetUlongFromAddress
  54. * Description: Returns a ULONG residing at a given memory location.
  55. * Author: Created by shouse, 1.4.01 - copied largely from ndiskd.dll
  56. */
  57. ULONG GetUlongFromAddress (ULONG64 Location) {
  58. ULONG result;
  59. ULONG Value;
  60. if ((!ReadMemory(Location, &Value, sizeof(ULONG), &result)) || (result < sizeof(ULONG))) {
  61. dprintf("unable to read from %08x\n", Location);
  62. return 0;
  63. }
  64. return Value;
  65. }
  66. /*
  67. * Function: GetPointerFromAddress
  68. * Description: Returns a memory address residing at a given memory location.
  69. * Author: Created by shouse, 1.4.01 - copied largely from ndiskd.dll
  70. */
  71. ULONG64 GetPointerFromAddress (ULONG64 Location) {
  72. ULONG64 Value;
  73. if (ReadPtr(Location,&Value)) {
  74. dprintf("unable to read from %p\n", Location);
  75. return 0;
  76. }
  77. return Value;
  78. }
  79. /*
  80. * Function: GetData
  81. * Description: Reads data from a memory location into a buffer.
  82. * Author: Created by shouse, 1.4.01 - copied largely from ndiskd.dll
  83. */
  84. BOOL GetData (IN LPVOID ptr, IN ULONG64 dwAddress, IN ULONG size, IN PCSTR type) {
  85. ULONG count = size;
  86. ULONG BytesRead;
  87. BOOL b;
  88. while (size > 0) {
  89. if (count >= 3000) count = 3000;
  90. b = ReadMemory(dwAddress, ptr, count, &BytesRead);
  91. if (!b || BytesRead != count) {
  92. dprintf("Unable to read %u bytes at %p, for %s\n", size, dwAddress, type);
  93. return FALSE;
  94. }
  95. dwAddress += count;
  96. size -= count;
  97. ptr = (LPVOID)((ULONG_PTR)ptr + count);
  98. }
  99. return TRUE;
  100. }
  101. /*
  102. * Function: GetString
  103. * Description: Copies a string from memory into a buffer.
  104. * Author: Created by shouse, 1.4.01 - copied largely from ndiskd.dll
  105. */
  106. BOOL GetString (IN ULONG64 dwAddress, IN LPWSTR buf, IN ULONG MaxChars) {
  107. do {
  108. if (!GetData(buf, dwAddress, sizeof(*buf), "Character"))
  109. return FALSE;
  110. dwAddress += sizeof(*buf);
  111. } while (--MaxChars && *buf++ != '\0');
  112. return TRUE;
  113. }
  114. /*
  115. * Function: GetMAC
  116. * Description: Copies an ethernet MAC address from memory into a buffer.
  117. * Author: Created by shouse, 1.14.01
  118. */
  119. BOOL GetMAC (IN ULONG64 dwAddress, IN UCHAR * buf, IN ULONG NumChars) {
  120. do {
  121. if (!GetData(buf, dwAddress, sizeof(*buf), "Character"))
  122. return FALSE;
  123. dwAddress += sizeof(*buf);
  124. buf++;
  125. } while (--NumChars);
  126. return TRUE;
  127. }
  128. /*
  129. * Function: TCPPacketTypeToString
  130. * Description: Returns a string corresponding to the enumerated TCP packet type.
  131. * Author: Created by shouse, 4.14.01
  132. */
  133. CHAR * TCPPacketTypeToString (TCP_PACKET_TYPE ePktType) {
  134. switch (ePktType) {
  135. case SYN:
  136. return "SYN";
  137. case DATA:
  138. return "DATA";
  139. case FIN:
  140. return "FIN";
  141. case RST:
  142. return "RST";
  143. default:
  144. return "Unknown";
  145. }
  146. }
  147. /*
  148. * Function: Map
  149. * Description: This IS the NLB hashing function.
  150. * Author: Created by shouse, 4.14.01
  151. */
  152. ULONG Map (ULONG v1, ULONG v2) {
  153. ULONG y = v1;
  154. ULONG z = v2;
  155. ULONG sum = 0;
  156. const ULONG a = 0x67; //key [0];
  157. const ULONG b = 0xdf; //key [1];
  158. const ULONG c = 0x40; //key [2];
  159. const ULONG d = 0xd3; //key [3];
  160. const ULONG delta = 0x9E3779B9;
  161. sum += delta;
  162. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  163. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  164. sum += delta;
  165. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  166. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  167. sum += delta;
  168. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  169. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  170. sum += delta;
  171. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  172. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  173. sum += delta;
  174. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  175. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  176. sum += delta;
  177. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  178. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  179. sum += delta;
  180. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  181. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  182. sum += delta;
  183. y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
  184. z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
  185. return y ^ z;
  186. }