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.

329 lines
9.6 KiB

  1. /*
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. scope.cpp
  5. Abstract:
  6. Implementation of CMDhcpScope.
  7. Author:
  8. */
  9. #include "stdafx.h"
  10. #include <winsock2.h>
  11. #include "mdhcp.h"
  12. #include "scope.h"
  13. /////////////////////////////////////////////////////////////////////////////
  14. // Constructor
  15. CMDhcpScope::CMDhcpScope() : m_pFTM(NULL), m_fLocal(FALSE)
  16. {
  17. LOG((MSP_TRACE, "CMDhcpScope constructor: enter"));
  18. LOG((MSP_TRACE, "CMDhcpScope constructor: exit"));
  19. }
  20. /////////////////////////////////////////////////////////////////////////////
  21. // Called by our creator only -- not part of IMDhcpScope.
  22. HRESULT CMDhcpScope::Initialize(
  23. MCAST_SCOPE_ENTRY scope,
  24. BOOL fLocal
  25. )
  26. {
  27. LOG((MSP_TRACE, "CMDhcpScope::Initialize: enter"));
  28. HRESULT hr = CoCreateFreeThreadedMarshaler( GetControllingUnknown(),
  29. & m_pFTM );
  30. if ( FAILED(hr) )
  31. {
  32. LOG((MSP_INFO, "CMDhcpScope::Initialize - "
  33. "create FTM returned 0x%08x; exit", hr));
  34. return hr;
  35. }
  36. m_fLocal = fLocal;
  37. // elementwise copy...
  38. m_scope = scope;
  39. // except for wide character pointer, which points to a string that will
  40. // get deleted shortly. We need to make a copy of that string.
  41. // (We allocate too many bytes here -- better safe than sorry. :)
  42. m_scope.ScopeDesc.Buffer = new WCHAR[m_scope.ScopeDesc.MaximumLength + 1];
  43. if (m_scope.ScopeDesc.Buffer == NULL)
  44. {
  45. LOG((MSP_ERROR, "scope Initialize: out of memory for buffer copy"));
  46. return E_OUTOFMEMORY;
  47. }
  48. lstrcpynW(m_scope.ScopeDesc.Buffer,
  49. scope.ScopeDesc.Buffer,
  50. m_scope.ScopeDesc.MaximumLength);
  51. LOG((MSP_TRACE, "CMDhcpScope::Initialize: exit"));
  52. return S_OK;
  53. }
  54. /////////////////////////////////////////////////////////////////////////////
  55. // Destructors
  56. void CMDhcpScope::FinalRelease(void)
  57. {
  58. LOG((MSP_TRACE, "CMDhcpScope::FinalRelease: enter"));
  59. // this is our private copy of the string.
  60. delete m_scope.ScopeDesc.Buffer;
  61. if ( m_pFTM )
  62. {
  63. m_pFTM->Release();
  64. }
  65. LOG((MSP_TRACE, "CMDhcpScope::FinalRelease: exit"));
  66. }
  67. CMDhcpScope::~CMDhcpScope()
  68. {
  69. LOG((MSP_TRACE, "CMDhcpScope destructor: enter"));
  70. LOG((MSP_TRACE, "CMDhcpScope destructor: exit"));
  71. }
  72. //////////////////////////////////////////////////////////////////////////////
  73. //////////////////////////////////////////////////////////////////////////////
  74. // IMDhcpScope
  75. //
  76. // This interface is obtained by calling IMDhcp::EnumerateScopes or
  77. // IMDhcp::get_Scopes. It encapsulates all the properties of a multicast
  78. // scope. You can use the methods of this interface to get information about
  79. // the scope. This is a "read-only" interface in that it has "get" methods
  80. // but no "put" methods.
  81. //////////////////////////////////////////////////////////////////////////////
  82. //////////////////////////////////////////////////////////////////////////////
  83. //////////////////////////////////////////////////////////////////////////////
  84. // IMDhcpScope::get_ScopeID
  85. //
  86. // Parameters
  87. // pID [out] Pointer to a long that will receive the ScopeID of this
  88. // scope, which is the ID that was assigned to this scope
  89. // when it was configured on the MDHCP server.
  90. //
  91. // Return Values
  92. // S_OK Success
  93. // E_POINTER The caller passed in an invalid pointer argument
  94. //
  95. // Description
  96. // Use this method to obtain the ScopeID associated with this scope. The
  97. // ScopeID and ServerID are needed to select this scope in subsequent
  98. // calls to IMDhcp::RequestAddress, IMDhcp::RenewAddress, or
  99. // IMDhcp::ReleaseAddress.
  100. /////////////////////////////////////////////////////////////////////////////
  101. STDMETHODIMP CMDhcpScope::get_ScopeID(
  102. long *pID
  103. )
  104. {
  105. LOG((MSP_TRACE, "CMDhcpScope::get_ScopeID: enter"));
  106. if ( IsBadWritePtr(pID, sizeof(long)) )
  107. {
  108. LOG((MSP_ERROR, "get_ScopeID: bad pointer passed in"));
  109. return E_POINTER;
  110. }
  111. //
  112. // Stored in network byte order -- we convert to host byte order
  113. // here in order to be UI friendly
  114. //
  115. *pID = ntohl( m_scope.ScopeCtx.ScopeID.IpAddrV4 );
  116. LOG((MSP_TRACE, "CMDhcpScope::get_ScopeID: exit"));
  117. return S_OK;
  118. }
  119. //////////////////////////////////////////////////////////////////////////////
  120. // IMDhcpScope::get_ServerID
  121. //
  122. // Parameters
  123. // pID [out] Pointer to a long that will receive the ServerID of this
  124. // scope, which is the ID that was assigned to the MDHCP
  125. // server that published this scope at the time that the
  126. // MDHCP server was configured.
  127. //
  128. // Return Values
  129. // S_OK Success
  130. // E_POINTER The caller passed in an invalid pointer argument
  131. //
  132. // Description
  133. // Use this method to obtain the ServerID associated with this scope.
  134. // The ServerID is provided for informational purposes only; it is not
  135. // required as input to any of the methods in these interfaces.
  136. /////////////////////////////////////////////////////////////////////////////
  137. STDMETHODIMP CMDhcpScope::get_ServerID(
  138. long *pID
  139. )
  140. {
  141. LOG((MSP_TRACE, "CMDhcpScope::get_ServerID: enter"));
  142. if ( IsBadWritePtr(pID, sizeof(long)) )
  143. {
  144. LOG((MSP_ERROR, "get_ServerID: bad pointer passed in"));
  145. return E_POINTER;
  146. }
  147. *pID = m_scope.ScopeCtx.ServerID.IpAddrV4;
  148. LOG((MSP_TRACE, "CMDhcpScope::get_ServerID: exit"));
  149. return S_OK;
  150. }
  151. //////////////////////////////////////////////////////////////////////////////
  152. // IMDhcpScope::get_InterfaceID
  153. //
  154. // Parameters
  155. // pID [out] Pointer to a long that will receive the InterfaceID of this
  156. // scope, which identifies the interface on which the server
  157. // that published this scope resides. This is normally the
  158. // network address of the interface.
  159. //
  160. // Return Values
  161. // S_OK Success
  162. // E_POINTER The caller passed in an invalid pointer argument
  163. //
  164. // Description
  165. // Use this method to obtain the ServerID associated with this scope. The
  166. // InterfaceID is provided for informational purposes only; it is not
  167. // required as input to any of the methods in these interfaces. However,
  168. // it may factor into the application's (or the user's) decision as to
  169. // which scope to use when requesting an address. This is because, in a
  170. // multi-homed scenario, using a multicast address on one network that was
  171. // obtained from a server on another network may cause address conflicts.
  172. /////////////////////////////////////////////////////////////////////////////
  173. STDMETHODIMP CMDhcpScope::get_InterfaceID(
  174. long * pID
  175. )
  176. {
  177. LOG((MSP_TRACE, "CMDhcpScope::get_InterfaceID - enter"));
  178. if ( IsBadWritePtr(pID, sizeof(long)) )
  179. {
  180. LOG((MSP_ERROR, "CMDhcpScope::get_InterfaceID - "
  181. "bad pointer passed in"));
  182. return E_POINTER;
  183. }
  184. *pID = m_scope.ScopeCtx.Interface.IpAddrV4;
  185. LOG((MSP_TRACE, "CMDhcpScope::get_InterfaceID - exit"));
  186. return S_OK;
  187. }
  188. //////////////////////////////////////////////////////////////////////////////
  189. // IMDhcpScope::get_ScopeDescription
  190. //
  191. // Parameters
  192. // ppAddress [out] Pointer to a BSTR (size-tagged Unicode string pointer)
  193. // that will receive a description of this scope. The
  194. // description was established when this scope was
  195. // configured on the MDHCP server.
  196. //
  197. // Return Values
  198. // S_OK Success
  199. // E_POINTER The caller passed in an invalid pointer argument
  200. // E_OUTOFMEMORY Not enough memory to allocate the string
  201. //
  202. // Description
  203. // Use this method to obtain a textual description associated with this
  204. // scope. The description is used only for clarifying the purpose or
  205. // meaning of a scope and is not required as input to any of the methods
  206. // in these interfaces.
  207. /////////////////////////////////////////////////////////////////////////////
  208. STDMETHODIMP CMDhcpScope::get_ScopeDescription(
  209. BSTR *ppAddress
  210. )
  211. {
  212. LOG((MSP_TRACE, "CMDhcpScope::get_ScopeDescription: enter"));
  213. if ( IsBadWritePtr(ppAddress, sizeof(BSTR)) )
  214. {
  215. LOG((MSP_ERROR, "get_ScopeDescription: bad pointer passed in"));
  216. return E_POINTER;
  217. }
  218. // This allocates space on OLE's heap, copies the wide character string
  219. // to that space, fille in the BSTR length field, and returns a pointer
  220. // to the WCHAR array part of the BSTR.
  221. *ppAddress = SysAllocString(m_scope.ScopeDesc.Buffer);
  222. if ( *ppAddress == NULL )
  223. {
  224. LOG((MSP_ERROR, "get_ScopeDescription: out of memory in string "
  225. "allocation"));
  226. return E_OUTOFMEMORY;
  227. }
  228. LOG((MSP_TRACE, "CMDhcpScope::get_ScopeDescription: exit"));
  229. return S_OK;
  230. }
  231. /////////////////////////////////////////////////////////////////////////////
  232. //
  233. STDMETHODIMP CMDhcpScope::get_TTL(
  234. long * plTtl
  235. )
  236. {
  237. LOG((MSP_TRACE, "CMDhcpScope::get_TTL - enter"));
  238. if ( IsBadWritePtr( plTtl, sizeof(long) ) )
  239. {
  240. LOG((MSP_ERROR, "get_TTL: bad pointer passed in - exit E_POINTER"));
  241. return E_POINTER;
  242. }
  243. *plTtl = m_scope.TTL;
  244. LOG((MSP_TRACE, "CMDhcpScope::get_TTL - exit S_OK"));
  245. return S_OK;
  246. }
  247. /////////////////////////////////////////////////////////////////////////////
  248. // public method not on any interface
  249. //
  250. HRESULT CMDhcpScope::GetLocal(BOOL * pfLocal)
  251. {
  252. LOG((MSP_TRACE, "CMDhcpScope::GetLocal: enter"));
  253. _ASSERTE( ! IsBadWritePtr( pfLocal, sizeof(BOOL) ) );
  254. *pfLocal = m_fLocal;
  255. LOG((MSP_TRACE, "CMDhcpScope::GetLocal: exit S_OK"));
  256. return S_OK;
  257. }
  258. // eof