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.

168 lines
3.8 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. add2strt.h
  5. Abstract:
  6. Code for IP address-to-string translation routines.
  7. Author:
  8. Dave Thaler (dthaler) 3-28-2001
  9. Revision History:
  10. IPv6 conversion code originally by Rich Draves (richdr)
  11. --*/
  12. NTSTATUS
  13. RtlIpv6AddressToStringExT(
  14. IN const struct in6_addr *Address,
  15. IN ULONG ScopeId,
  16. IN USHORT Port,
  17. OUT LPTSTR AddressString,
  18. IN OUT PULONG AddressStringLength
  19. )
  20. /*++
  21. Routine Description:
  22. This is the extension routine which handles a full address conversion
  23. including address, scopeid and port (scopeid and port are optional).
  24. Arguments:
  25. Address - The address part to be translated.
  26. ScopeId - The Scope ID of the address (optional).
  27. Port - The port number of the address (optional).
  28. Port is in network byte order.
  29. AddressString - Pointer to output buffer where we will fill in address string.
  30. AddressStringLength - For input, it is the length of the input buffer; for
  31. output it is the length we actual returned.
  32. Return Value:
  33. STATUS_SUCCESS if the operation is successful, error code otherwise.
  34. --*/
  35. {
  36. TCHAR String[INET6_ADDRSTRLEN];
  37. LPTSTR S;
  38. ULONG Length;
  39. if ((Address == NULL) ||
  40. (AddressString == NULL) ||
  41. (AddressStringLength == NULL)) {
  42. return STATUS_INVALID_PARAMETER;
  43. }
  44. S = String;
  45. if (Port) {
  46. S += _stprintf(S, _T("["));
  47. }
  48. //
  49. // Now translate this address.
  50. //
  51. S = RtlIpv6AddressToStringT(Address, S);
  52. if (ScopeId != 0) {
  53. S += _stprintf(S, _T("%%%u"), ScopeId);
  54. }
  55. if (Port != 0) {
  56. S += _stprintf(S, _T("]:%u"), RtlUshortByteSwap(Port));
  57. }
  58. Length = (ULONG)(S - String + 1);
  59. if (*AddressStringLength < Length) {
  60. //
  61. // Before return, tell the caller how big
  62. // the buffer we need.
  63. //
  64. *AddressStringLength = Length;
  65. return STATUS_INVALID_PARAMETER;
  66. }
  67. *AddressStringLength = Length;
  68. RtlCopyMemory(AddressString, String, Length * sizeof(TCHAR));
  69. return STATUS_SUCCESS;
  70. }
  71. NTSTATUS
  72. RtlIpv4AddressToStringExT(
  73. IN const struct in_addr *Address,
  74. IN USHORT Port,
  75. OUT LPTSTR AddressString,
  76. IN OUT PULONG AddressStringLength
  77. )
  78. /*++
  79. Routine Description:
  80. This is the extension routine which handles a full address conversion
  81. including address and port (port is optional).
  82. Arguments:
  83. Address - The address part to translate.
  84. Port - Port number if there is any, otherwise 0. Port is in network
  85. byte order.
  86. AddressString - Receives the formatted address string.
  87. AddressStringLength - On input, contains the length of AddressString.
  88. On output, contains the number of characters actually written
  89. to AddressString.
  90. Return Value:
  91. STATUS_SUCCESS if the operation is successful, error code otherwise.
  92. --*/
  93. {
  94. TCHAR String[INET_ADDRSTRLEN];
  95. LPTSTR S;
  96. ULONG Length;
  97. //
  98. // Quick sanity checks.
  99. //
  100. if ((Address == NULL) ||
  101. (AddressString == NULL) ||
  102. (AddressStringLength == NULL)) {
  103. return STATUS_INVALID_PARAMETER;
  104. }
  105. S = String;
  106. //
  107. // Now translate this address.
  108. //
  109. S = RtlIpv4AddressToStringT(Address, S);
  110. if (Port != 0) {
  111. S += _stprintf(S, _T(":%u"), RtlUshortByteSwap(Port));
  112. }
  113. Length = (ULONG)(S - String + 1);
  114. if (*AddressStringLength < Length) {
  115. //
  116. // Before return, tell the caller how big
  117. // the buffer we need.
  118. //
  119. *AddressStringLength = Length;
  120. return STATUS_INVALID_PARAMETER;
  121. }
  122. RtlCopyMemory(AddressString, String, Length * sizeof(TCHAR));
  123. *AddressStringLength = Length;
  124. return STATUS_SUCCESS;
  125. }