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.

293 lines
7.2 KiB

  1. /*
  2. File pnp.c
  3. Handles pnp notifications such as lan interfaces coming up and down.
  4. */
  5. #define UNICODE
  6. #define _UNICODE
  7. #include <nt.h>
  8. #include <ntrtl.h>
  9. #include <nturtl.h>
  10. #include <stdlib.h>
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <wchar.h>
  14. #include <winsock.h>
  15. #include <npapi.h>
  16. #include <ipexport.h>
  17. #include <ras.h>
  18. #include <rasman.h>
  19. #include <acd.h>
  20. #include <tapi.h>
  21. #include <ndisguid.h>
  22. #include <wmium.h>
  23. #include "radebug.h"
  24. extern HANDLE hPnpEventG;
  25. //**
  26. //
  27. // Call: PnpMediaSenseCb
  28. //
  29. // Returns: None
  30. //
  31. // Description:
  32. //
  33. VOID
  34. WINAPI
  35. PnpMediaSenseCb(
  36. PWNODE_HEADER pWnodeHeader,
  37. UINT_PTR NotificationContext
  38. )
  39. {
  40. PWNODE_SINGLE_INSTANCE pWnode = (PWNODE_SINGLE_INSTANCE)pWnodeHeader;
  41. LPWSTR lpwsName = (LPWSTR)RtlOffsetToPointer(
  42. pWnode,
  43. pWnode->OffsetInstanceName );
  44. //
  45. // Get the information for the media disconnect.
  46. //
  47. if ( memcmp( &(pWnodeHeader->Guid),
  48. &GUID_NDIS_STATUS_MEDIA_DISCONNECT,
  49. sizeof( GUID ) ) == 0 )
  50. {
  51. RASAUTO_TRACE1(
  52. "PnpMediaSenseCb [disconnect] called for %ws",
  53. lpwsName );
  54. if (hPnpEventG)
  55. {
  56. SetEvent(hPnpEventG);
  57. }
  58. }
  59. else
  60. {
  61. //
  62. // Get the information for the media connect.
  63. //
  64. if ( memcmp( &(pWnodeHeader->Guid),
  65. &GUID_NDIS_STATUS_MEDIA_CONNECT,
  66. sizeof( GUID ) ) == 0 )
  67. {
  68. RASAUTO_TRACE1(
  69. "PnpMediaSenseCb [connect] called for %ws",
  70. lpwsName );
  71. if (hPnpEventG)
  72. {
  73. SetEvent(hPnpEventG);
  74. }
  75. }
  76. }
  77. }
  78. //**
  79. //
  80. // Call: PnpMediaSenseRegister
  81. //
  82. // Returns: NO_ERROR - Success
  83. // Non-zero returns - Failure
  84. //
  85. // Description:
  86. //
  87. DWORD
  88. PnpMediaSenseRegister(
  89. IN BOOL fRegister
  90. )
  91. {
  92. DWORD dwRetCode = NO_ERROR;
  93. PVOID pvDeliveryInfo = PnpMediaSenseCb;
  94. dwRetCode = WmiNotificationRegistration(
  95. (LPGUID)(&GUID_NDIS_STATUS_MEDIA_CONNECT),
  96. (BOOLEAN)fRegister,
  97. pvDeliveryInfo,
  98. (ULONG_PTR)NULL,
  99. NOTIFICATION_CALLBACK_DIRECT );
  100. if ( dwRetCode != NO_ERROR )
  101. {
  102. return( dwRetCode );
  103. }
  104. dwRetCode = WmiNotificationRegistration(
  105. (LPGUID)(&GUID_NDIS_STATUS_MEDIA_DISCONNECT),
  106. (BOOLEAN)fRegister,
  107. pvDeliveryInfo,
  108. (ULONG_PTR)NULL,
  109. NOTIFICATION_CALLBACK_DIRECT );
  110. if ( dwRetCode != NO_ERROR )
  111. {
  112. return( dwRetCode );
  113. }
  114. return( NO_ERROR );
  115. }
  116. //**
  117. //
  118. // Call: PnpBindingsNotificationsCb
  119. //
  120. // Returns: None
  121. //
  122. // Description:
  123. //
  124. VOID
  125. WINAPI
  126. PnpBindingsNotificationsCb(
  127. PWNODE_HEADER pWnodeHeader,
  128. UINT_PTR NotificationContext
  129. )
  130. {
  131. LPWSTR lpwszGUIDStart;
  132. LPWSTR lpwszGUIDEnd;
  133. LPWSTR lpwszGUID;
  134. WCHAR wchGUIDSaveLast;
  135. PWNODE_SINGLE_INSTANCE pWnode = (PWNODE_SINGLE_INSTANCE)pWnodeHeader;
  136. LPWSTR lpwsName = (LPWSTR)RtlOffsetToPointer(
  137. pWnode,
  138. pWnode->OffsetInstanceName );
  139. LPWSTR lpwsTransportName = (LPWSTR)RtlOffsetToPointer(
  140. pWnode,
  141. pWnode->DataBlockOffset );
  142. //
  143. // Extract GUID from the \device\GUID name
  144. //
  145. lpwszGUID = lpwsTransportName + wcslen( lpwsTransportName ) + 1;
  146. lpwszGUIDStart = wcsrchr( lpwszGUID, L'{' );
  147. lpwszGUIDEnd = wcsrchr( lpwszGUID, L'}' );
  148. if ( (lpwszGUIDStart != NULL )
  149. && (lpwszGUIDEnd != NULL ))
  150. {
  151. BOOL fBind, fUnbind;
  152. // Only signal when something happens with IP. This will prevent
  153. // us from handling too many notifications
  154. //
  155. if ( _wcsicmp( L"TCPIP", lpwsTransportName ) == 0 )
  156. {
  157. fBind = ( memcmp(
  158. &(pWnodeHeader->Guid),
  159. &GUID_NDIS_NOTIFY_BIND,
  160. sizeof( GUID ) ) == 0);
  161. fUnbind = (memcmp(
  162. &(pWnodeHeader->Guid),
  163. &GUID_NDIS_NOTIFY_UNBIND,
  164. sizeof(GUID))==0);
  165. if (fBind || fUnbind)
  166. {
  167. RASAUTO_TRACE4(
  168. "PnpBindingsNotificationsCb %d %d if=%ws, trans=%ws",
  169. fBind,
  170. fUnbind,
  171. lpwsName,
  172. lpwsTransportName );
  173. if (hPnpEventG)
  174. {
  175. SetEvent(hPnpEventG);
  176. }
  177. }
  178. }
  179. else
  180. {
  181. RASAUTO_TRACE2(
  182. "PnpBindingsNotificationsCb non-tcp: if=%ws, trans=%ws",
  183. lpwsName,
  184. lpwsTransportName );
  185. }
  186. }
  187. }
  188. //**
  189. //
  190. // Call: PnpBindingsNotificationsRegister
  191. //
  192. // Returns: NO_ERROR - Success
  193. // Non-zero returns - Failure
  194. //
  195. // Description:
  196. //
  197. DWORD
  198. PnpBindingsNotificationsRegister(
  199. IN BOOL fRegister
  200. )
  201. {
  202. DWORD dwRetCode = NO_ERROR;
  203. PVOID pvDeliveryInfo = PnpBindingsNotificationsCb;
  204. dwRetCode = WmiNotificationRegistration(
  205. (LPGUID)(&GUID_NDIS_NOTIFY_BIND),
  206. (BOOLEAN)fRegister,
  207. pvDeliveryInfo,
  208. (ULONG_PTR)NULL,
  209. NOTIFICATION_CALLBACK_DIRECT );
  210. if ( dwRetCode != NO_ERROR )
  211. {
  212. return( dwRetCode );
  213. }
  214. dwRetCode = WmiNotificationRegistration(
  215. (LPGUID)(&GUID_NDIS_NOTIFY_UNBIND),
  216. (BOOLEAN)fRegister,
  217. pvDeliveryInfo,
  218. (ULONG_PTR)NULL,
  219. NOTIFICATION_CALLBACK_DIRECT );
  220. if ( dwRetCode != NO_ERROR )
  221. {
  222. return( dwRetCode );
  223. }
  224. return( NO_ERROR );
  225. }
  226. DWORD
  227. PnpRegister(
  228. IN BOOL fRegister)
  229. {
  230. DWORD dwErr = NO_ERROR;
  231. RASAUTO_TRACE1("PnpRegister: %d", !!fRegister);
  232. dwErr = PnpBindingsNotificationsRegister(fRegister);
  233. if (dwErr == NO_ERROR)
  234. {
  235. dwErr = PnpMediaSenseRegister(fRegister);
  236. if (dwErr == NO_ERROR)
  237. {
  238. RASAUTO_TRACE("PnpRegister: success.");
  239. }
  240. else
  241. {
  242. if (fRegister)
  243. {
  244. PnpBindingsNotificationsRegister(FALSE);
  245. }
  246. RASAUTO_TRACE1("PnpRegister: MSense reg failure 0x%x", dwErr);
  247. }
  248. }
  249. else
  250. {
  251. RASAUTO_TRACE1("PnpRegister: Bingings reg failure 0x%x", dwErr);
  252. }
  253. return dwErr;
  254. }