Leaked source code of windows server 2003
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.

243 lines
5.5 KiB

  1. //THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. //ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. //THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright 1993-1995 Microsoft Corporation. All Rights Reserved.
  7. //
  8. // MODULE: ipaddr.c
  9. //
  10. // PURPOSE: IP address handler
  11. //
  12. // PLATFORMS: Windows 95
  13. //
  14. // FUNCTIONS:
  15. // CheckAddress()
  16. // ParseIPAddress()
  17. // ConvertIPAddress()
  18. // AssignIPAddress()
  19. //
  20. // SPECIAL INSTRUCTIONS: N/A
  21. //
  22. #include "proj.h" // includes common header files and global declarations
  23. #include <rnap.h>
  24. #define MAXNUMBER 80
  25. #define MAX_IP_FIELDS 4
  26. #define MIN_FIELD1 1 // min allowed value for field 1
  27. #define MAX_FIELD1 223 // max allowed value for field 1
  28. #define MIN_FIELD2 0 // min for field 2
  29. #define MAX_FIELD2 255 // max for field 2
  30. #define MIN_FIELD3 0 // min for field 3
  31. #define MAX_FIELD3 254 // max for field 3
  32. #define MIN_FIELD4 1 // 0 is reserved for broadcast
  33. #define MIN_IP_VALUE 0 /* default minimum allowable field value */
  34. #define MAX_IP_VALUE 255 /* default maximum allowable field value */
  35. typedef struct tagIPaddr {
  36. DWORD cField;
  37. BYTE bIP[MAX_IP_FIELDS];
  38. } IPADDR, *PIPADDR;
  39. static int atoi (LPCSTR szBuf)
  40. {
  41. int iRet = 0;
  42. // Find the first nonspace
  43. //
  44. while ((*szBuf == ' ') || (*szBuf == '\t'))
  45. szBuf++;
  46. while ((*szBuf >= '0') && (*szBuf <= '9'))
  47. {
  48. iRet = (iRet*10)+(int)(*szBuf-'0');
  49. szBuf++;
  50. };
  51. return iRet;
  52. }
  53. //
  54. //
  55. // FUNCTION: CheckAddress (DWORD)
  56. //
  57. // PURPOSE: Check an address to see if its valid.
  58. //
  59. // RETURN: The first field that has an invalid value,
  60. // or (WORD)-1 if the address is okay.
  61. //
  62. DWORD CheckAddress(DWORD ip)
  63. {
  64. BYTE b;
  65. b = HIBYTE(HIWORD(ip));
  66. if (b < MIN_FIELD1 || b > MAX_FIELD1 || b == 127) return 0;
  67. b = LOBYTE(LOWORD(ip));
  68. if (b > MAX_FIELD3) return 3;
  69. return (DWORD)-1;
  70. }
  71. //
  72. //
  73. // FUNCTION: ParseIPAddress (PIPADDR, LPCSTR)
  74. //
  75. // PURPOSE: parse the IP address string
  76. //
  77. DWORD NEAR PASCAL ParseIPAddress (PIPADDR pIPAddr, LPCSTR szIPAddress)
  78. {
  79. LPCSTR szNextIP, szNext;
  80. char szNumber[MAXNUMBER+1];
  81. int cField, cb, iValue;
  82. szNext = szNextIP = szIPAddress;
  83. cField = 0;
  84. while ((*szNext) && (cField < MAX_IP_FIELDS))
  85. {
  86. // Check address separator
  87. //
  88. if (*szNext == '.')
  89. {
  90. // We have a new number
  91. //
  92. cb = (DWORD)(szNext-szNextIP);
  93. if ((cb > 0) && (cb <= MAXNUMBER))
  94. {
  95. lstrcpyn(szNumber, szNextIP, cb+1);
  96. iValue = atoi(szNumber);
  97. if ((iValue >= MIN_IP_VALUE) && (iValue <= MAX_IP_VALUE))
  98. {
  99. pIPAddr->bIP[cField] = (UCHAR)iValue;
  100. cField++;
  101. };
  102. };
  103. szNextIP = szNext+1;
  104. };
  105. szNext++;
  106. };
  107. // Get the last number
  108. //
  109. if (cField < MAX_IP_FIELDS)
  110. {
  111. cb = (int) (szNext-szNextIP);
  112. if ((cb > 0) && (cb <= MAXNUMBER))
  113. {
  114. lstrcpyn(szNumber, szNextIP, cb+1);
  115. iValue = atoi(szNumber);
  116. if ((iValue >= MIN_IP_VALUE) && (iValue <= MAX_IP_VALUE))
  117. {
  118. pIPAddr->bIP[cField] = (UCHAR) iValue;
  119. cField++;
  120. };
  121. };
  122. }
  123. else
  124. {
  125. // Not a valid IP address
  126. //
  127. return ERROR_INVALID_ADDRESS;
  128. };
  129. pIPAddr->cField = cField;
  130. return ERROR_SUCCESS;
  131. }
  132. //
  133. //
  134. // FUNCTION: ConvertIPAddress (LPDWORD, LPCSTR)
  135. //
  136. // PURPOSE: convert the IP address string to a number
  137. //
  138. DWORD NEAR PASCAL ConvertIPAddress (LPDWORD lpdwAddr, LPCSTR szIPAddress)
  139. {
  140. IPADDR ipAddr;
  141. DWORD dwIPAddr;
  142. DWORD dwRet;
  143. DWORD i;
  144. // Parse the IP address string
  145. //
  146. if ((dwRet = ParseIPAddress(&ipAddr, szIPAddress)) == ERROR_SUCCESS)
  147. {
  148. // Validate the number fields
  149. //
  150. if (ipAddr.cField == MAX_IP_FIELDS)
  151. {
  152. // Conver the IP address into one number
  153. //
  154. dwIPAddr = 0;
  155. for (i = 0; i < ipAddr.cField; i++)
  156. {
  157. dwIPAddr = (dwIPAddr << 8) + ipAddr.bIP[i];
  158. };
  159. // Validate the address
  160. //
  161. if (CheckAddress(dwIPAddr) > MAX_IP_FIELDS)
  162. {
  163. *lpdwAddr = dwIPAddr;
  164. dwRet = ERROR_SUCCESS;
  165. }
  166. else
  167. {
  168. dwRet = ERROR_INVALID_ADDRESS;
  169. };
  170. }
  171. else
  172. {
  173. dwRet = ERROR_INVALID_ADDRESS;
  174. };
  175. };
  176. return dwRet;
  177. }
  178. //
  179. //
  180. // FUNCTION: AssignIPAddress (LPCSTR, LPCSTR)
  181. //
  182. // PURPOSE: assign an IP address to the connection
  183. //
  184. DWORD NEAR PASCAL AssignIPAddress (LPCSTR szEntryName, LPCSTR szIPAddress)
  185. {
  186. IPDATA ipData;
  187. DWORD dwIPAddr;
  188. DWORD dwRet;
  189. // Validate and convert IP address string into a number
  190. //
  191. if ((dwRet = ConvertIPAddress(&dwIPAddr, szIPAddress))
  192. == ERROR_SUCCESS)
  193. {
  194. // Get the current IP settings for the connection
  195. //
  196. ipData.dwSize = sizeof(ipData);
  197. #ifndef WINNT_RAS
  198. //
  199. // WINNT_RAS: the functions RnaGetIPInfo and RnaSetIPInfo don't exist on NT
  200. //
  201. if ((dwRet = RnaGetIPInfo((LPSTR)szEntryName, &ipData, FALSE)) == ERROR_SUCCESS)
  202. {
  203. // We want to specify the IP address
  204. //
  205. ipData.fdwTCPIP |= IPF_IP_SPECIFIED;
  206. // Set the IP address
  207. //
  208. ipData.dwIPAddr = dwIPAddr;
  209. // Set the IP settings for the connection
  210. //
  211. dwRet = RnaSetIPInfo((LPSTR)szEntryName, &ipData);
  212. };
  213. #endif // WINNT_RAS
  214. };
  215. return dwRet;
  216. }