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.

299 lines
8.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: E V E N T S R C . C P P
  7. //
  8. // Contents: Functions dealing with UPnP event sources.
  9. //
  10. // Notes:
  11. //
  12. // Author: danielwe 28 Oct 1999
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include <oleauto.h>
  18. #include "ncbase.h"
  19. #include "updiagp.h"
  20. #include "ncinet.h"
  21. BOOL DoListEventSources(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  22. {
  23. DWORD ies;
  24. Assert(g_ctx.ectx == CTX_CD);
  25. _tprintf(TEXT("Listing all Event Sources\n"));
  26. _tprintf(TEXT("------------------------------\n"));
  27. for (ies = 0; ies < PDevCur()->cSvcs; ies++)
  28. {
  29. _tprintf(TEXT("%d) %s\n"), ies + 1,
  30. PDevCur()->rgSvcs[ies]->szEvtUrl);
  31. }
  32. _tprintf(TEXT("------------------------------\n\n"));
  33. return FALSE;
  34. }
  35. BOOL DoSwitchEs(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  36. {
  37. Assert(g_ctx.ectx == CTX_CD);
  38. if (cArgs == 2)
  39. {
  40. DWORD idev;
  41. idev = _tcstoul(rgArgs[1], NULL, 10);
  42. if (idev <= PDevCur()->cSvcs && PDevCur()->rgSvcs[idev - 1])
  43. {
  44. g_ctx.psvcCur = PDevCur()->rgSvcs[idev - 1];
  45. g_ctx.ectx = CTX_EVTSRC;
  46. }
  47. else
  48. {
  49. _tprintf(TEXT("%d is not a valid event source index!\n"), idev);
  50. }
  51. }
  52. else
  53. {
  54. Usage(iCmd);
  55. }
  56. return FALSE;
  57. }
  58. BOOL DoSubmitEvent(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  59. {
  60. DWORD iArg;
  61. DWORD iProp;
  62. UPNPSVC * psvc = g_ctx.psvcCur;
  63. UPNP_PROPERTY * rgProps;
  64. DWORD cProps = cArgs - 1;
  65. Assert(g_ctx.ectx == CTX_EVTSRC);
  66. if (cArgs < 2)
  67. {
  68. Usage(iCmd);
  69. return FALSE;
  70. }
  71. rgProps = new UPNP_PROPERTY[cProps];
  72. Assert(rgProps);
  73. ::ZeroMemory(rgProps, sizeof(UPNP_PROPERTY) * cProps);
  74. for (iArg = 1; iArg < cArgs; iArg++)
  75. {
  76. LPTSTR szProp;
  77. LPTSTR szValue;
  78. szProp = _tcstok(rgArgs[iArg], TEXT(":"));
  79. if (szProp)
  80. {
  81. iProp = _tcstoul(szProp, NULL, 10);
  82. if (iProp > 0)
  83. {
  84. iProp--; // make it 0-based
  85. szValue = _tcstok(NULL, TEXT(":"));
  86. if (szValue)
  87. {
  88. rgProps[iArg - 1].szName = SzFromTsz(psvc->sst.rgRows[iProp].szPropName);
  89. rgProps[iArg - 1].szValue = SzFromTsz(szValue);
  90. }
  91. else
  92. {
  93. _tprintf(TEXT("'%s' is not a valid property!\n\n"), szProp);
  94. goto cleanup;
  95. }
  96. }
  97. else
  98. {
  99. _tprintf(TEXT("'%s' is not a valid property!\n\n"), szProp);
  100. goto cleanup;
  101. }
  102. }
  103. else
  104. {
  105. _tprintf(TEXT("'%s' is not a valid property!\n\n"), rgArgs[iArg]);
  106. goto cleanup;
  107. }
  108. }
  109. CHAR szUri[INTERNET_MAX_URL_LENGTH];
  110. LPSTR pszEvtUrl;
  111. HRESULT hr;
  112. pszEvtUrl = SzFromTsz(psvc->szEvtUrl);
  113. if (pszEvtUrl)
  114. {
  115. hr = HrGetRequestUriA(pszEvtUrl, INTERNET_MAX_URL_LENGTH, szUri);
  116. if (SUCCEEDED(hr))
  117. {
  118. if (SubmitUpnpPropertyEvent(szUri, 0, cProps, rgProps))
  119. {
  120. TraceTag(ttidUpdiag, "Successfully submitted event to %s.", psvc->szEvtUrl);
  121. }
  122. else
  123. {
  124. TraceTag(ttidUpdiag, "Failed to submit event to %s! Error %d.",
  125. psvc->szEvtUrl, GetLastError());
  126. }
  127. }
  128. else
  129. {
  130. TraceTag(ttidUpdiag, "Failed to crack URL %s! Error %d.",
  131. psvc->szEvtUrl, GetLastError());
  132. }
  133. delete [] pszEvtUrl;
  134. }
  135. else
  136. {
  137. TraceTag(ttidUpdiag, "DoSubmitEvent: SzFromTsz failed");
  138. }
  139. cleanup:
  140. iProp = 0;
  141. for ( ; iProp < cProps; ++iProp)
  142. {
  143. if (rgProps[iProp].szName)
  144. {
  145. delete [] rgProps[iProp].szName;
  146. }
  147. if (rgProps[iProp].szValue)
  148. {
  149. delete [] rgProps[iProp].szValue;
  150. }
  151. }
  152. delete [] rgProps;
  153. return FALSE;
  154. }
  155. BOOL DoListProps(DWORD iCmd, DWORD cArgs, LPTSTR *rgArgs)
  156. {
  157. DWORD iProp;
  158. UPNPSVC * psvc = g_ctx.psvcCur;
  159. Assert(g_ctx.ectx == CTX_EVTSRC);
  160. _tprintf(TEXT("Listing properties for event source %s...\n"),
  161. g_ctx.psvcCur->szEvtUrl);
  162. _tprintf(TEXT("----------------------------------------------------\n"));
  163. for (iProp = 0; iProp < psvc->sst.cRows; iProp++)
  164. {
  165. VARIANT varDest;
  166. VariantClear(&varDest);
  167. VariantChangeType(&varDest, &psvc->sst.rgRows[iProp].varValue, 0, VT_BSTR);
  168. _tprintf(TEXT("%d) %s = %S\n"), iProp + 1,
  169. psvc->sst.rgRows[iProp].szPropName, varDest.bstrVal);
  170. }
  171. _tprintf(TEXT("----------------------------------------------------\n\n"));
  172. return FALSE;
  173. }
  174. DWORD CsecDiffFileTime(FILETIME * pft1, FILETIME *pft2)
  175. {
  176. ULONGLONG qwResult1;
  177. ULONGLONG qwResult2;
  178. LONG lDiff;
  179. // Copy the time into a quadword.
  180. qwResult1 = (((ULONGLONG) pft1->dwHighDateTime) << 32) + pft1->dwLowDateTime;
  181. qwResult2 = (((ULONGLONG) pft2->dwHighDateTime) << 32) + pft2->dwLowDateTime;
  182. lDiff = (LONG) (((LONGLONG)(qwResult2 - qwResult1)) / _SECOND);
  183. lDiff = max(0, lDiff);
  184. return lDiff;
  185. }
  186. VOID DoEvtSrcInfo()
  187. {
  188. UPNPSVC * psvc = g_ctx.psvcCur;
  189. CHAR szUri[INTERNET_MAX_URL_LENGTH];
  190. HRESULT hr;
  191. EVTSRC_INFO info = {0};
  192. EVTSRC_INFO * pinfo = &info;
  193. LPSTR pszEvtUrl;
  194. pszEvtUrl = SzFromTsz(psvc->szEvtUrl);
  195. if (pszEvtUrl)
  196. {
  197. hr = HrGetRequestUriA(pszEvtUrl, INTERNET_MAX_URL_LENGTH, szUri);
  198. if (SUCCEEDED(hr))
  199. {
  200. if (GetEventSourceInfo(szUri, &pinfo))
  201. {
  202. DWORD iSub;
  203. _tprintf(TEXT("Listing information for event source %s:\n"),
  204. psvc->szEvtUrl);
  205. _tprintf(TEXT("------------------------------------------------------\n"));
  206. for (iSub = 0; iSub < pinfo->cSubs; iSub++)
  207. {
  208. SYSTEMTIME st;
  209. FILETIME ftCur;
  210. TCHAR szLocalDate[255];
  211. TCHAR szLocalTime[255];
  212. _tprintf(TEXT("Notify to : %s\n"), pinfo->rgSubs[iSub].szDestUrl);
  213. _tprintf(TEXT("Seq # : %d\n"), pinfo->rgSubs[iSub].iSeq);
  214. FileTimeToSystemTime(&pinfo->rgSubs[iSub].ftTimeout, &st);
  215. GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
  216. szLocalDate, 255);
  217. GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL,
  218. szLocalTime, 255);
  219. GetSystemTimeAsFileTime(&ftCur);
  220. FileTimeToLocalFileTime(&ftCur, &ftCur);
  221. _tprintf(TEXT("Expires : %s %s (in %d seconds)\n"),
  222. szLocalDate, szLocalTime,
  223. CsecDiffFileTime(&ftCur,
  224. &pinfo->rgSubs[iSub].ftTimeout));
  225. _tprintf(TEXT("Timeout : %d\n"), pinfo->rgSubs[iSub].csecTimeout);
  226. _tprintf(TEXT("SID : %s\n\n"), pinfo->rgSubs[iSub].szSid);
  227. free(pinfo->rgSubs[iSub].szDestUrl);
  228. free(pinfo->rgSubs[iSub].szSid);
  229. }
  230. free(pinfo->rgSubs);
  231. _tprintf(TEXT("------------------------------------------------------\n\n"));
  232. }
  233. else
  234. {
  235. TraceTag(ttidUpdiag, "Failed to get event source info for %s! Error %d.",
  236. psvc->szEvtUrl, GetLastError());
  237. }
  238. }
  239. else
  240. {
  241. TraceTag(ttidUpdiag, "Failed to crack URL %s! Error %d.",
  242. psvc->szEvtUrl, GetLastError());
  243. }
  244. delete [] pszEvtUrl;
  245. }
  246. else
  247. {
  248. TraceTag(ttidUpdiag, "DoEvtSrcInfo: SzFromTsz failed");
  249. }
  250. }