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.

224 lines
10 KiB

  1. /***************************************************************************
  2. *
  3. * Copyright (C) 2001-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: dpnhupnpdevice.h
  6. *
  7. * Content: Header for device 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. #ifndef DPNBUILD_NOHNETFWAPI
  19. #define DEVICE_CHECKEDFORHNETFIREWALL 0x0001 // the check for a local HomeNet firewall has been performed for the device at least once
  20. #define DEVICE_HNETFIREWALLED 0x0002 // the device is firewalled, and can be controlled with the HomeNet APIs
  21. #define DEVICE_UPNPDISCOVERYSOCKETMAPPEDONHNETFIREWALL 0x0004 // the UPnP discovery socket for this device was mapped on the firewall
  22. #endif // ! DPNBUILD_NOHNETFWAPI
  23. #define DEVICE_PERFORMINGREMOTEUPNPDISCOVERY 0x0008 // the check for a remote UPnP gateway device is being performed
  24. #define DEVICE_PERFORMINGLOCALUPNPDISCOVERY 0x0010 // the check for a local UPnP gateway device is being performed
  25. #define DEVICE_GOTREMOTEUPNPDISCOVERYCONNRESET 0x0020 // the check for a remote UPnP gateway device generated a WSAECONNRESET error
  26. #define DEVICE_GOTLOCALUPNPDISCOVERYCONNRESET 0x0040 // the check for a local UPnP gateway device generated a WSAECONNRESET error
  27. #ifdef DBG
  28. #ifndef DPNBUILD_NOWINSOCK2
  29. #define DEVICE_PRIMARY 0x0080 // this device appears to be the primary adapter with which its gateway should be reached
  30. #define DEVICE_SECONDARY 0x0100 // this device appears to be a secondary adapter on a shared network
  31. #define DEVICE_NOGATEWAY 0x0200 // this device does not currently have a gateway
  32. #endif // ! DPNBUILD_NOWINSOCK2
  33. #endif // DBG
  34. //=============================================================================
  35. // Macros
  36. //=============================================================================
  37. #define DEVICE_FROM_BILINK(b) (CONTAINING_OBJECT(b, CDevice, m_blList))
  38. #define DEVICE_FROM_TEMP_BILINK(b) (CONTAINING_OBJECT(b, CDevice, m_blTempList))
  39. //=============================================================================
  40. // Typedefs
  41. //=============================================================================
  42. class CDevice;
  43. class CUPnPDevice;
  44. //=============================================================================
  45. // Device object class
  46. //=============================================================================
  47. class CDevice
  48. {
  49. public:
  50. #undef DPF_MODNAME
  51. #define DPF_MODNAME "CDevice::CDevice"
  52. CDevice(const DWORD dwLocalAddressV4)
  53. {
  54. this->m_blList.Initialize();
  55. this->m_blTempList.Initialize();
  56. this->m_blOwnedRegPorts.Initialize();
  57. this->m_Sig[0] = 'D';
  58. this->m_Sig[1] = 'E';
  59. this->m_Sig[2] = 'V';
  60. this->m_Sig[3] = 'I';
  61. this->m_dwFlags = 0;
  62. this->m_dwLocalAddressV4 = dwLocalAddressV4;
  63. this->m_wUPnPDiscoverySocketPort = 0;
  64. this->m_sUPnPDiscoverySocket = INVALID_SOCKET;
  65. this->m_pUPnPDevice = NULL;
  66. this->m_dwFirstUPnPDiscoveryTime = 0;
  67. #ifdef DBG
  68. this->m_dwNumUPnPDeviceFailures = 0;
  69. #endif // DBG
  70. };
  71. #undef DPF_MODNAME
  72. #define DPF_MODNAME "CDevice::~CDevice"
  73. ~CDevice(void)
  74. {
  75. #ifdef DBG
  76. DPFX(DPFPREP, 8, "(0x%p) NumUPnPDeviceFailures = %u",
  77. this, this->m_dwNumUPnPDeviceFailures);
  78. DNASSERT(this->m_blList.IsEmpty());
  79. DNASSERT(this->m_blTempList.IsEmpty());
  80. DNASSERT(this->m_blOwnedRegPorts.IsEmpty());
  81. #ifndef DPNBUILD_NOHNETFWAPI
  82. DNASSERT(! (this->m_dwFlags & DEVICE_UPNPDISCOVERYSOCKETMAPPEDONHNETFIREWALL));
  83. #endif // ! DPNBUILD_NOHNETFWAPI
  84. DNASSERT(this->m_sUPnPDiscoverySocket == INVALID_SOCKET);
  85. DNASSERT(this->m_pUPnPDevice == NULL);
  86. #endif // DBG
  87. };
  88. #ifndef DPNBUILD_NOHNETFWAPI
  89. inline BOOL HasCheckedForFirewallAvailability(void) const { return ((this->m_dwFlags & DEVICE_CHECKEDFORHNETFIREWALL) ? TRUE : FALSE); };
  90. inline BOOL IsHNetFirewalled(void) const { return ((this->m_dwFlags & DEVICE_HNETFIREWALLED) ? TRUE : FALSE); };
  91. inline BOOL IsUPnPDiscoverySocketMappedOnHNetFirewall(void) const { return ((this->m_dwFlags & DEVICE_UPNPDISCOVERYSOCKETMAPPEDONHNETFIREWALL) ? TRUE : FALSE); };
  92. #endif // ! DPNBUILD_NOHNETFWAPI
  93. inline BOOL IsOKToPerformRemoteUPnPDiscovery(void) const { return (((this->m_dwFlags & DEVICE_PERFORMINGREMOTEUPNPDISCOVERY) && (! (this->m_dwFlags & DEVICE_GOTREMOTEUPNPDISCOVERYCONNRESET)))? TRUE : FALSE); };
  94. inline BOOL IsOKToPerformLocalUPnPDiscovery(void) const { return (((this->m_dwFlags & DEVICE_PERFORMINGLOCALUPNPDISCOVERY) && (! (this->m_dwFlags & DEVICE_GOTLOCALUPNPDISCOVERYCONNRESET)))? TRUE : FALSE); };
  95. inline BOOL GotRemoteUPnPDiscoveryConnReset(void) const { return ((this->m_dwFlags & DEVICE_GOTREMOTEUPNPDISCOVERYCONNRESET) ? TRUE : FALSE); };
  96. inline BOOL GotLocalUPnPDiscoveryConnReset(void) const { return ((this->m_dwFlags & DEVICE_GOTLOCALUPNPDISCOVERYCONNRESET) ? TRUE : FALSE); };
  97. #ifdef DBG
  98. inline BOOL IsPerformingRemoteUPnPDiscovery(void) const { return ((this->m_dwFlags & DEVICE_PERFORMINGREMOTEUPNPDISCOVERY) ? TRUE : FALSE); };
  99. inline BOOL IsPerformingLocalUPnPDiscovery(void) const { return ((this->m_dwFlags & DEVICE_PERFORMINGLOCALUPNPDISCOVERY) ? TRUE : FALSE); };
  100. #ifndef DPNBUILD_NOWINSOCK2
  101. inline BOOL IsPrimaryDevice(void) const { return ((this->m_dwFlags & DEVICE_PRIMARY) ? TRUE : FALSE); };
  102. inline BOOL IsSecondaryDevice(void) const { return ((this->m_dwFlags & DEVICE_SECONDARY) ? TRUE : FALSE); };
  103. inline BOOL HasNoGateway(void) const { return ((this->m_dwFlags & DEVICE_NOGATEWAY) ? TRUE : FALSE); };
  104. #endif // ! DPNBUILD_NOWINSOCK2
  105. #endif // DBG
  106. inline DWORD GetLocalAddressV4(void) const { return this->m_dwLocalAddressV4; };
  107. inline WORD GetUPnPDiscoverySocketPort(void) const { return this->m_wUPnPDiscoverySocketPort; };
  108. inline SOCKET GetUPnPDiscoverySocket(void) const { return this->m_sUPnPDiscoverySocket; };
  109. //
  110. // This does not add a reference (when not NULL)!
  111. //
  112. inline CUPnPDevice * GetUPnPDevice(void) { return this->m_pUPnPDevice; };
  113. inline DWORD GetFirstUPnPDiscoveryTime(void) const { return this->m_dwFirstUPnPDiscoveryTime; };
  114. #ifndef DPNBUILD_NOHNETFWAPI
  115. inline void NoteCheckedForFirewallAvailability(void) { this->m_dwFlags |= DEVICE_CHECKEDFORHNETFIREWALL; };
  116. #undef DPF_MODNAME
  117. #define DPF_MODNAME "CDevice::NoteHNetFirewalled"
  118. inline void NoteHNetFirewalled(void)
  119. {
  120. DNASSERT(! (this->m_dwFlags & DEVICE_HNETFIREWALLED));
  121. this->m_dwFlags |= DEVICE_HNETFIREWALLED;
  122. };
  123. #undef DPF_MODNAME
  124. #define DPF_MODNAME "CDevice::NoteUPnPDiscoverySocketMappedOnHNetFirewall"
  125. inline void NoteUPnPDiscoverySocketMappedOnHNetFirewall(void)
  126. {
  127. DNASSERT(! (this->m_dwFlags & DEVICE_UPNPDISCOVERYSOCKETMAPPEDONHNETFIREWALL));
  128. this->m_dwFlags |= DEVICE_UPNPDISCOVERYSOCKETMAPPEDONHNETFIREWALL;
  129. };
  130. #endif // ! DPNBUILD_NOHNETFWAPI
  131. inline void NotePerformingRemoteUPnPDiscovery(void) { this->m_dwFlags |= DEVICE_PERFORMINGREMOTEUPNPDISCOVERY; };
  132. inline void NotePerformingLocalUPnPDiscovery(void) { this->m_dwFlags |= DEVICE_PERFORMINGLOCALUPNPDISCOVERY; };
  133. inline void NoteGotRemoteUPnPDiscoveryConnReset(void) { this->m_dwFlags |= DEVICE_GOTREMOTEUPNPDISCOVERYCONNRESET; };
  134. inline void NoteGotLocalUPnPDiscoveryConnReset(void) { this->m_dwFlags |= DEVICE_GOTLOCALUPNPDISCOVERYCONNRESET; };
  135. #ifndef DPNBUILD_NOHNETFWAPI
  136. inline void NoteNotHNetFirewalled(void) { this->m_dwFlags &= ~DEVICE_HNETFIREWALLED; };
  137. inline void NoteNotUPnPDiscoverySocketMappedOnHNetFirewall(void) { this->m_dwFlags &= ~DEVICE_UPNPDISCOVERYSOCKETMAPPEDONHNETFIREWALL; };
  138. #endif // ! DPNBUILD_NOHNETFWAPI
  139. inline void NoteNotPerformingRemoteUPnPDiscovery(void) { this->m_dwFlags &= ~DEVICE_PERFORMINGREMOTEUPNPDISCOVERY; };
  140. inline void NoteNotPerformingLocalUPnPDiscovery(void) { this->m_dwFlags &= ~DEVICE_PERFORMINGLOCALUPNPDISCOVERY; };
  141. inline void NoteNotGotRemoteUPnPDiscoveryConnReset(void) { this->m_dwFlags &= ~DEVICE_GOTREMOTEUPNPDISCOVERYCONNRESET; };
  142. inline void NoteNotGotLocalUPnPDiscoveryConnReset(void) { this->m_dwFlags &= ~DEVICE_GOTLOCALUPNPDISCOVERYCONNRESET; };
  143. #ifdef DBG
  144. #ifndef DPNBUILD_NOWINSOCK2
  145. inline void NotePrimaryDevice(void) { this->m_dwFlags |= DEVICE_PRIMARY; };
  146. inline void NoteSecondaryDevice(void) { this->m_dwFlags |= DEVICE_SECONDARY; };
  147. inline void NoteNoGateway(void) { this->m_dwFlags |= DEVICE_NOGATEWAY; };
  148. inline void ClearGatewayFlags(void) { this->m_dwFlags &= ~(DEVICE_PRIMARY | DEVICE_SECONDARY | DEVICE_NOGATEWAY); };
  149. #endif // ! DPNBUILD_NOWINSOCK2
  150. #endif // DBG
  151. inline void SetUPnPDiscoverySocketPort(const WORD wPort) { this->m_wUPnPDiscoverySocketPort = wPort; };
  152. inline void SetUPnPDiscoverySocket(const SOCKET sSocket) { this->m_sUPnPDiscoverySocket = sSocket; };
  153. inline void SetUPnPDevice(CUPnPDevice * const pUPnPDevice) { this->m_pUPnPDevice = pUPnPDevice; };
  154. inline void SetFirstUPnPDiscoveryTime(const DWORD dwTime) { this->m_dwFirstUPnPDiscoveryTime = dwTime; };
  155. #ifdef DBG
  156. inline void IncrementUPnPDeviceFailures(void) { this->m_dwNumUPnPDeviceFailures++; };
  157. #endif // DBG
  158. CBilink m_blList; // list of all the available devices
  159. CBilink m_blTempList; // temporary list of all the available devices
  160. CBilink m_blOwnedRegPorts; // list of all the ports registered using this device
  161. private:
  162. //
  163. // Note that all values here are protected by the global CNATHelpUPnP lock.
  164. //
  165. BYTE m_Sig[4]; // debugging signature ('DEVI')
  166. DWORD m_dwFlags; // flags describing this object
  167. DWORD m_dwLocalAddressV4; // address this object represents
  168. WORD m_wUPnPDiscoverySocketPort; // port being used by UPnP discovery socket
  169. SOCKET m_sUPnPDiscoverySocket; // socket opened for UPnP discovery communication on this device
  170. CUPnPDevice * m_pUPnPDevice; // pointer to UPnP Internet gateway for this device, if any
  171. DWORD m_dwFirstUPnPDiscoveryTime; // the time we first sent UPnP discovery traffic from this particular port, locally or remotely
  172. #ifdef DBG
  173. DWORD m_dwNumUPnPDeviceFailures; // how many times a UPnP device returned an error or stopped responding and had to be cleared
  174. #endif // DBG
  175. };