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.

403 lines
14 KiB

  1. /***************************************************************************
  2. *
  3. * Copyright (C) 2001-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: dpnhupnpregport.h
  6. *
  7. * Content: Header for registered port object class.
  8. *
  9. * History:
  10. * Date By Reason
  11. * ======== ======== =========
  12. * 04/16/01 VanceO Split DPNATHLP into DPNHUPNP and DPNHPAST.
  13. *
  14. ***************************************************************************/
  15. //=============================================================================
  16. // Object flags
  17. //=============================================================================
  18. #define REGPORTOBJ_TCP DPNHREGISTERPORTS_TCP // TCP ports instead of UDP
  19. #define REGPORTOBJ_FIXEDPORTS DPNHREGISTERPORTS_FIXEDPORTS // the UPnP device should use same port numbers on public interface
  20. #define REGPORTOBJ_SHAREDPORTS DPNHREGISTERPORTS_SHAREDPORTS // the UPnP device should allow these UDP fixed ports to be shared with other clients
  21. #ifndef DPNBUILD_NOHNETFWAPI
  22. #define REGPORTOBJ_MAPPEDONHNETFIREWALL 0x80000000 // the port has been mapped with the local HomeNet API aware firewall
  23. #define REGPORTOBJ_PORTUNAVAILABLE_HNETFIREWALL 0x40000000 // the port is unavailable on the local HomeNet API aware firewall
  24. #define REGPORTOBJ_HNETFIREWALLMAPPINGBUILTIN 0x20000000 // the mapping on the HomeNet API aware firewall was already built-in
  25. #endif // ! DPNBUILD_NOHNETFWAPI
  26. #define REGPORTOBJ_PORTUNAVAILABLE_UPNP 0x10000000 // the port is unavailable on the UPnP device
  27. #define REGPORTOBJ_PERMANENTUPNPLEASE 0x08000000 // the port is permanently leased on the UPnP device
  28. #define REGPORTOBJ_REMOVINGUPNPLEASE 0x04000000 // the port is currently losing it's UPnP lease
  29. #define REGPORTOBJMASK_UPNP (REGPORTOBJ_TCP | REGPORTOBJ_FIXEDPORTS | REGPORTOBJ_SHAREDPORTS)
  30. #ifndef DPNBUILD_NOHNETFWAPI
  31. #define REGPORTOBJMASK_HNETFWAPI (REGPORTOBJ_TCP | REGPORTOBJ_FIXEDPORTS | REGPORTOBJ_SHAREDPORTS | REGPORTOBJ_HNETFIREWALLMAPPINGBUILTIN)
  32. #endif // ! DPNBUILD_NOHNETFWAPI
  33. //=============================================================================
  34. // Macros
  35. //=============================================================================
  36. #define REGPORT_FROM_GLOBAL_BILINK(b) (CONTAINING_OBJECT(b, CRegisteredPort, m_blGlobalList))
  37. #define REGPORT_FROM_DEVICE_BILINK(b) (CONTAINING_OBJECT(b, CRegisteredPort, m_blDeviceList))
  38. //=============================================================================
  39. // Registered port object class
  40. //=============================================================================
  41. class CRegisteredPort
  42. {
  43. public:
  44. #undef DPF_MODNAME
  45. #define DPF_MODNAME "CRegisteredPort::CRegisteredPort"
  46. CRegisteredPort(const DWORD dwRequestedLeaseTime,
  47. const DWORD dwFlags)
  48. {
  49. this->m_blGlobalList.Initialize();
  50. this->m_blDeviceList.Initialize();
  51. this->m_Sig[0] = 'R';
  52. this->m_Sig[1] = 'E';
  53. this->m_Sig[2] = 'G';
  54. this->m_Sig[3] = 'P';
  55. this->m_pOwningDevice = NULL;
  56. this->m_pasaddrinPrivateAddresses = NULL;
  57. this->m_dwNumAddresses = 0;
  58. this->m_dwRequestedLeaseTime = dwRequestedLeaseTime;
  59. this->m_dwFlags = dwFlags; // works because REGPORTOBJ_xxx == DPNHREGISTERPORTS_xxx.
  60. this->m_lUserRefs = 0;
  61. this->m_pasaddrinUPnPPublicAddresses = NULL;
  62. this->m_dwUPnPLeaseExpiration = 0;
  63. };
  64. #undef DPF_MODNAME
  65. #define DPF_MODNAME "CRegisteredPort::~CRegisteredPort"
  66. ~CRegisteredPort(void)
  67. {
  68. DNASSERT(this->m_blGlobalList.IsEmpty());
  69. DNASSERT(this->m_blDeviceList.IsEmpty());
  70. #ifndef DPNBUILD_NOHNETFWAPI
  71. DNASSERT(! (this->m_dwFlags & REGPORTOBJ_MAPPEDONHNETFIREWALL));
  72. #endif // ! DPNBUILD_NOHNETFWAPI
  73. DNASSERT(this->m_lUserRefs == 0);
  74. DNASSERT(this->m_pasaddrinPrivateAddresses == NULL);
  75. DNASSERT(this->m_pasaddrinUPnPPublicAddresses == NULL);
  76. //
  77. // For grins, change the signature before deleting the object.
  78. //
  79. this->m_Sig[3] = 'p';
  80. };
  81. inline BOOL IsValidObject(void)
  82. {
  83. if ((this == NULL) || (IsBadWritePtr(this, sizeof(CRegisteredPort))))
  84. {
  85. return FALSE;
  86. }
  87. if (*((DWORD*) (&this->m_Sig)) != 0x50474552) // 0x50 0x47 0x45 0x52 = 'PGER' = 'REGP' in Intel order
  88. {
  89. return FALSE;
  90. }
  91. return TRUE;
  92. };
  93. //
  94. // You must have global object lock to call this function.
  95. //
  96. #undef DPF_MODNAME
  97. #define DPF_MODNAME "CRegisteredPort::MakeDeviceOwner"
  98. inline void MakeDeviceOwner(CDevice * const pDevice)
  99. {
  100. DNASSERT(pDevice != NULL);
  101. DNASSERT(this->m_pOwningDevice == NULL);
  102. this->m_pOwningDevice = pDevice;
  103. this->m_blDeviceList.InsertAfter(&pDevice->m_blOwnedRegPorts);
  104. };
  105. //
  106. // You must have global object lock to call this function.
  107. //
  108. inline CDevice * GetOwningDevice(void) { return this->m_pOwningDevice; };
  109. //
  110. // You must have global object lock to call this function.
  111. //
  112. #undef DPF_MODNAME
  113. #define DPF_MODNAME "CRegisteredPort::ClearDeviceOwner"
  114. inline void ClearDeviceOwner(void)
  115. {
  116. DNASSERT(this->m_pOwningDevice != NULL);
  117. DNASSERT(this->m_blDeviceList.IsListMember(&this->m_pOwningDevice->m_blOwnedRegPorts));
  118. this->m_pOwningDevice = NULL;
  119. this->m_blDeviceList.RemoveFromList();
  120. };
  121. #undef DPF_MODNAME
  122. #define DPF_MODNAME "CRegisteredPort::SetPrivateAddresses"
  123. HRESULT SetPrivateAddresses(const SOCKADDR_IN * const asaddrinPrivateAddresses,
  124. const DWORD dwNumAddresses)
  125. {
  126. DNASSERT((this->m_pasaddrinPrivateAddresses == NULL) && (this->m_dwNumAddresses == 0));
  127. this->m_pasaddrinPrivateAddresses = (SOCKADDR_IN *) DNMalloc(dwNumAddresses * sizeof(SOCKADDR_IN));
  128. if (this->m_pasaddrinPrivateAddresses == NULL)
  129. {
  130. return DPNHERR_OUTOFMEMORY;
  131. }
  132. CopyMemory(this->m_pasaddrinPrivateAddresses, asaddrinPrivateAddresses, dwNumAddresses * sizeof(SOCKADDR_IN));
  133. this->m_dwNumAddresses = dwNumAddresses;
  134. return DPNH_OK;
  135. }
  136. inline void AddUserRef(void) { this->m_lUserRefs++; };
  137. #undef DPF_MODNAME
  138. #define DPF_MODNAME "CRegisteredPort::DecUserRef"
  139. inline LONG DecUserRef(void)
  140. {
  141. DNASSERT(this->m_lUserRefs >= 0);
  142. this->m_lUserRefs--;
  143. return this->m_lUserRefs;
  144. };
  145. #undef DPF_MODNAME
  146. #define DPF_MODNAME "CRegisteredPort::ClearAllUserRefs"
  147. inline void ClearAllUserRefs(void)
  148. {
  149. DNASSERT(this->m_lUserRefs > 0);
  150. this->m_lUserRefs = 0;
  151. };
  152. inline SOCKADDR_IN * GetPrivateAddressesArray(void) { return this->m_pasaddrinPrivateAddresses; };
  153. inline DWORD GetNumAddresses(void) const { return this->m_dwNumAddresses; };
  154. inline DWORD GetRequestedLeaseTime(void) const { return this->m_dwRequestedLeaseTime; };
  155. inline BOOL IsTCP(void) const { return ((this->m_dwFlags & REGPORTOBJ_TCP) ? TRUE : FALSE); };
  156. inline BOOL IsFixedPort(void) const { return ((this->m_dwFlags & REGPORTOBJ_FIXEDPORTS) ? TRUE : FALSE); };
  157. inline BOOL IsSharedPort(void) const { return ((this->m_dwFlags & REGPORTOBJ_SHAREDPORTS) ? TRUE : FALSE); };
  158. inline DWORD GetAddressesSize(void) const { return (this->GetNumAddresses() * sizeof(SOCKADDR_IN)); };
  159. inline void ClearPrivateAddresses(void)
  160. {
  161. if (this->m_pasaddrinPrivateAddresses != NULL)
  162. {
  163. DNFree(this->m_pasaddrinPrivateAddresses);
  164. this->m_pasaddrinPrivateAddresses = NULL;
  165. }
  166. };
  167. inline void UpdateRequestedLeaseTime(const DWORD dwRequestedLeaseTime)
  168. {
  169. this->m_dwRequestedLeaseTime = dwRequestedLeaseTime;
  170. };
  171. inline DWORD GetFlags(void) const { return this->m_dwFlags; };
  172. #ifndef DPNBUILD_NOHNETFWAPI
  173. inline BOOL IsMappedOnHNetFirewall(void) const { return ((this->m_dwFlags & REGPORTOBJ_MAPPEDONHNETFIREWALL) ? TRUE : FALSE); };
  174. inline BOOL IsHNetFirewallPortUnavailable(void) const { return ((this->m_dwFlags & REGPORTOBJ_PORTUNAVAILABLE_HNETFIREWALL) ? TRUE : FALSE); };
  175. inline BOOL IsHNetFirewallMappingBuiltIn(void) const { return ((this->m_dwFlags & REGPORTOBJ_HNETFIREWALLMAPPINGBUILTIN) ? TRUE : FALSE); };
  176. #endif // ! DPNBUILD_NOHNETFWAPI
  177. inline BOOL IsUPnPPortUnavailable(void) const { return ((this->m_dwFlags & REGPORTOBJ_PORTUNAVAILABLE_UPNP) ? TRUE : FALSE); };
  178. inline BOOL HasPermanentUPnPLease(void) const { return ((this->m_dwFlags & REGPORTOBJ_PERMANENTUPNPLEASE) ? TRUE : FALSE); };
  179. inline BOOL IsRemovingUPnPLease(void) const { return ((this->m_dwFlags & REGPORTOBJ_REMOVINGUPNPLEASE) ? TRUE : FALSE); };
  180. inline BOOL HasUPnPPublicAddresses(void) const { return ((this->m_pasaddrinUPnPPublicAddresses != NULL) ? TRUE : FALSE); };
  181. #undef DPF_MODNAME
  182. #define DPF_MODNAME "CRegisteredPort::GetUPnPLeaseExpiration"
  183. inline DWORD GetUPnPLeaseExpiration(void) const
  184. {
  185. DNASSERT(! (this->m_dwFlags & REGPORTOBJ_PERMANENTUPNPLEASE));
  186. return this->m_dwUPnPLeaseExpiration;
  187. };
  188. #ifndef DPNBUILD_NOHNETFWAPI
  189. #undef DPF_MODNAME
  190. #define DPF_MODNAME "CRegisteredPort::NoteMappedOnHNetFirewall"
  191. inline void NoteMappedOnHNetFirewall(void)
  192. {
  193. DNASSERT(! (this->m_dwFlags & REGPORTOBJ_MAPPEDONHNETFIREWALL));
  194. this->m_dwFlags |= REGPORTOBJ_MAPPEDONHNETFIREWALL;
  195. };
  196. #undef DPF_MODNAME
  197. #define DPF_MODNAME "CRegisteredPort::NoteHNetFirewallPortUnavailable"
  198. inline void NoteHNetFirewallPortUnavailable(void)
  199. {
  200. DNASSERT(! (this->m_dwFlags & REGPORTOBJ_PORTUNAVAILABLE_HNETFIREWALL));
  201. this->m_dwFlags |= REGPORTOBJ_PORTUNAVAILABLE_HNETFIREWALL;
  202. };
  203. #undef DPF_MODNAME
  204. #define DPF_MODNAME "CRegisteredPort::NoteHNetFirewallMappingBuiltIn"
  205. inline void NoteHNetFirewallMappingBuiltIn(void)
  206. {
  207. DNASSERT(! (this->m_dwFlags & REGPORTOBJ_HNETFIREWALLMAPPINGBUILTIN));
  208. this->m_dwFlags |= REGPORTOBJ_HNETFIREWALLMAPPINGBUILTIN;
  209. };
  210. #endif // ! DPNBUILD_NOHNETFWAPI
  211. #undef DPF_MODNAME
  212. #define DPF_MODNAME "CRegisteredPort::NoteUPnPPortUnavailable"
  213. inline void NoteUPnPPortUnavailable(void)
  214. {
  215. DNASSERT(! (this->m_dwFlags & REGPORTOBJ_PORTUNAVAILABLE_UPNP));
  216. this->m_dwFlags |= REGPORTOBJ_PORTUNAVAILABLE_UPNP;
  217. };
  218. #undef DPF_MODNAME
  219. #define DPF_MODNAME "CRegisteredPort::NotePermanentUPnPLease"
  220. inline void NotePermanentUPnPLease(void)
  221. {
  222. DNASSERT(! (this->m_dwFlags & REGPORTOBJ_PERMANENTUPNPLEASE));
  223. this->m_dwFlags |= REGPORTOBJ_PERMANENTUPNPLEASE;
  224. };
  225. #undef DPF_MODNAME
  226. #define DPF_MODNAME "CRegisteredPort::NoteRemovingUPnPLease"
  227. inline void NoteRemovingUPnPLease(void)
  228. {
  229. DNASSERT(! (this->m_dwFlags & REGPORTOBJ_REMOVINGUPNPLEASE));
  230. this->m_dwFlags |= REGPORTOBJ_REMOVINGUPNPLEASE;
  231. };
  232. #ifndef DPNBUILD_NOHNETFWAPI
  233. inline void NoteNotMappedOnHNetFirewall(void) { this->m_dwFlags &= ~REGPORTOBJ_MAPPEDONHNETFIREWALL; };
  234. inline void NoteNotHNetFirewallPortUnavailable(void) { this->m_dwFlags &= ~REGPORTOBJ_PORTUNAVAILABLE_HNETFIREWALL; };
  235. inline void NoteNotHNetFirewallMappingBuiltIn(void) { this->m_dwFlags &= ~REGPORTOBJ_HNETFIREWALLMAPPINGBUILTIN; };
  236. #endif // ! DPNBUILD_NOHNETFWAPI
  237. inline void NoteNotUPnPPortUnavailable(void) { this->m_dwFlags &= ~REGPORTOBJ_PORTUNAVAILABLE_UPNP; };
  238. inline void NoteNotPermanentUPnPLease(void) { this->m_dwFlags &= ~REGPORTOBJ_PERMANENTUPNPLEASE; };
  239. inline void NoteNotRemovingUPnPLease(void) { this->m_dwFlags &= ~REGPORTOBJ_REMOVINGUPNPLEASE; };
  240. inline void SetUPnPLeaseExpiration(const DWORD dwLeaseExpiration) { this->m_dwUPnPLeaseExpiration = dwLeaseExpiration; };
  241. #undef DPF_MODNAME
  242. #define DPF_MODNAME "CRegisteredPort::CreateUPnPPublicAddressesArray"
  243. inline HRESULT CreateUPnPPublicAddressesArray(void)
  244. {
  245. DNASSERT(! this->HasUPnPPublicAddresses());
  246. DNASSERT(this->GetNumAddresses() > 0);
  247. this->m_pasaddrinUPnPPublicAddresses = (SOCKADDR_IN*) DNMalloc(this->GetNumAddresses() * sizeof(SOCKADDR_IN));
  248. if (this->m_pasaddrinUPnPPublicAddresses == NULL)
  249. {
  250. return DPNHERR_OUTOFMEMORY;
  251. }
  252. return DPNH_OK;
  253. };
  254. #undef DPF_MODNAME
  255. #define DPF_MODNAME "CRegisteredPort::GetUPnPPublicAddressesArray"
  256. inline SOCKADDR_IN * GetUPnPPublicAddressesArray(void)
  257. {
  258. DNASSERT(this->HasUPnPPublicAddresses());
  259. return (this->m_pasaddrinUPnPPublicAddresses);
  260. };
  261. #undef DPF_MODNAME
  262. #define DPF_MODNAME "CRegisteredPort::SetUPnPPublicV4Address"
  263. inline void SetUPnPPublicV4Address(const DWORD dwAddressIndex,
  264. const DWORD dwAddressV4,
  265. const WORD wPort)
  266. {
  267. DNASSERT(this->HasUPnPPublicAddresses());
  268. this->m_pasaddrinUPnPPublicAddresses[dwAddressIndex].sin_family = AF_INET;
  269. this->m_pasaddrinUPnPPublicAddresses[dwAddressIndex].sin_addr.S_un.S_addr = dwAddressV4;
  270. this->m_pasaddrinUPnPPublicAddresses[dwAddressIndex].sin_port = wPort;
  271. };
  272. #undef DPF_MODNAME
  273. #define DPF_MODNAME "CRegisteredPort::UpdateUPnPPublicV4Addresses"
  274. inline void UpdateUPnPPublicV4Addresses(const DWORD dwAddressV4)
  275. {
  276. DWORD dwTemp;
  277. DNASSERT(this->HasUPnPPublicAddresses());
  278. for(dwTemp = 0; dwTemp < this->GetNumAddresses(); dwTemp++)
  279. {
  280. this->m_pasaddrinUPnPPublicAddresses[dwTemp].sin_addr.S_un.S_addr = dwAddressV4;
  281. }
  282. };
  283. #undef DPF_MODNAME
  284. #define DPF_MODNAME "CRegisteredPort::CopyUPnPPublicAddresses"
  285. inline void CopyUPnPPublicAddresses(SOCKADDR_IN * aPublicAddresses)
  286. {
  287. DNASSERT(this->HasUPnPPublicAddresses());
  288. CopyMemory(aPublicAddresses, this->m_pasaddrinUPnPPublicAddresses, this->GetAddressesSize());
  289. };
  290. inline void DestroyUPnPPublicAddressesArray(void)
  291. {
  292. if (this->m_pasaddrinUPnPPublicAddresses != NULL)
  293. {
  294. DNFree(this->m_pasaddrinUPnPPublicAddresses);
  295. this->m_pasaddrinUPnPPublicAddresses = NULL;
  296. }
  297. };
  298. CBilink m_blGlobalList; // list of all the ports registered
  299. CBilink m_blDeviceList; // list of ports registered for a particular device
  300. private:
  301. //
  302. // Note that all values here are protected by the global CNATHelpUPnP lock.
  303. //
  304. BYTE m_Sig[4]; // debugging signature ('REGP')
  305. CDevice * m_pOwningDevice; // pointer to owning device object
  306. SOCKADDR_IN * m_pasaddrinPrivateAddresses; // array of private addresses registered by this object
  307. DWORD m_dwNumAddresses; // number of private (and public, if available) addresses in use by this object
  308. DWORD m_dwRequestedLeaseTime; // requested lease time for object
  309. DWORD m_dwFlags; // flags for this object
  310. LONG m_lUserRefs; // number of user references for this port mapping
  311. SOCKADDR_IN * m_pasaddrinUPnPPublicAddresses; // array of public addresses UPnP device assigned for this mapping
  312. DWORD m_dwUPnPLeaseExpiration; // time when lease expires on UPnP device
  313. };