您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // main.cs SNF-SDK-WIN C# OEM Demonstration Code
  2. // Copyright (C) 2009 ARM Research Labs, LLC
  3. //
  4. // This app simply exercises the API provided by snfmultidll.
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.IO;
  10. using System.Collections;
  11. using System.Runtime.InteropServices;
  12. namespace SNFMultiDLLExampleCsharp
  13. {
  14. class SNFMultiDLLExample
  15. {
  16. #region DLL Imports
  17. // Location of SNFMulti.dll.
  18. //const string SNFMULTI_DLL = "..\\..\\..\\64bitDll\\SNFMulti.dll"; // Set CPU type to "Any CPU"
  19. //const string SNFMULTI_DLL = "..\\..\\..\\..\\64bitDll\\SNFMulti.dll"; // Set CPU type to "x64
  20. const string SNFMULTI_DLL = "..\\..\\..\\32bitDll\\SNFMulti.dll"; // Set CPU type to "x86"
  21. // int setThrottle(int Threads); /* Set scan thread limit. */
  22. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "setThrottle", CallingConvention = CallingConvention.Winapi)]
  23. public static extern int setThrottle(int throttle);
  24. // int startupSNF(char* Path); /* Start SNF with configuration. */
  25. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "startupSNF", CallingConvention = CallingConvention.Winapi)]
  26. public static extern int startupSNF([MarshalAs(UnmanagedType.LPStr)] string Path);
  27. // EXP int startupSNFAuthenticated(char* Path, char* Lic, char* Auth); /* Start SNF with conf & auth. */
  28. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "startupSNFAuthenticated",
  29. CallingConvention = CallingConvention.Winapi)]
  30. public static extern int startupSNFAuthenticated(
  31. [MarshalAs(UnmanagedType.LPStr)] string Path,
  32. [MarshalAs(UnmanagedType.LPStr)] string Lic,
  33. [MarshalAs(UnmanagedType.LPStr)] string Auth);
  34. // int shutdownSNF(); /* Shutdown SNF. */
  35. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "shutdownSNF", CallingConvention = CallingConvention.Winapi)]
  36. public static extern int shutdownSNF();
  37. // int testIP(unsigned long int IPToCheck); /* Test the IP for a GBUdb range. */
  38. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "testIP", CallingConvention = CallingConvention.Winapi)]
  39. public static extern int testIP(ulong IPToCheck);
  40. // double getIPReputation(unsigned long int IPToCheck); /* Get reputation figure for IP. */
  41. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "getIPReputation", CallingConvention = CallingConvention.Winapi)]
  42. public static extern double getIPReputation(ulong IPToCheck);
  43. // int scanBuffer(unsigned char* Bfr, int Length, char* Name, int Setup);/* Scan msgBuffer, name, setup time. */
  44. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "scanBuffer", CallingConvention = CallingConvention.Winapi)]
  45. public static extern int scanBuffer(
  46. [MarshalAs(UnmanagedType.LPStr)] string Bfr,
  47. int Length,
  48. [MarshalAs(UnmanagedType.LPStr)] string Name,
  49. int Setup);
  50. // int scanFile(char* FilePath, int Setup); /* Scan msgFile, setup time. */
  51. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "scanFile", CallingConvention = CallingConvention.Winapi)]
  52. public static extern int scanFile([MarshalAs(UnmanagedType.LPStr)] string Path, int Setup);
  53. // int getScanXHeaders(int ScanHandle, char** Bfr, int* Length); /* Get result & XHeaders. */
  54. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "getScanXHeaders", CallingConvention = CallingConvention.Winapi)]
  55. public static extern int getScanXHeaders(int ScanHandle,
  56. out IntPtr Bfr,
  57. out int Length);
  58. // int getScanXMLLog(int ScanHandle, char** Bfr, int* Length); /* Get result & XML Log. */
  59. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "getScanXMLLog", CallingConvention = CallingConvention.Winapi)]
  60. public static extern int getScanXMLLog(int ScanHandle,
  61. out IntPtr Bfr,
  62. out int Length);
  63. // int getScanClassicLog(int ScanHandle, char** Bfr, int* Length); /* Get result & Classic Log. */
  64. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "getScanClassicLog", CallingConvention = CallingConvention.Winapi)]
  65. public static extern int getScanClassicLog(int ScanHandle,
  66. out IntPtr Bfr,
  67. out int Length);
  68. // int getScanResult(int ScanHandle); /* Get just the scan result. */
  69. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "getScanResult", CallingConvention = CallingConvention.Winapi)]
  70. public static extern int getScanResult(int ScanHandle);
  71. // int closeScan(int ScanHandle); /* Close the scan result. */
  72. [DllImport(SNFMULTI_DLL, CharSet = CharSet.Auto, EntryPoint = "closeScan", CallingConvention = CallingConvention.Winapi)]
  73. public static extern int closeScan(int ScanHandle);
  74. #endregion
  75. static void Main(string[] args)
  76. {
  77. //// Setup the basics we need to run this test.
  78. // const string LicenseID = "licensid"; // SNF License ID can be passed
  79. // const string Authentication = "authenticationxx"; // directly or read from the
  80. // configuration. OEMs go direct!
  81. const string ConfigurationPath = "c:\\SNF\\snf_engine.xml";
  82. const uint IPToTest = 0x0c22384e; // Same as IP 12.34.56.78
  83. const string SampleMessage =
  84. "Received: from mx-out.example.com [12.34.56.78] (HELO Somebody)\r\n" +
  85. " by mx-in.example.com (nosuchserver v1.0) for nobody@example.com\r\n" +
  86. "From: <somebody@example.com>\r\n" +
  87. "To: <nobody@example.com>\r\n" +
  88. "Subject: Nothing to see here\r\n" +
  89. "\r\n" +
  90. "So this is the big thing that's not here to see.\r\n" +
  91. "I thought it would be more interesting than this.\r\n" +
  92. "\r\n" +
  93. "_M\r\n" +
  94. ".\r\n";
  95. //// Here is a simple example. Startup, exercise the API, shut down.
  96. //// Note that we're doing this in a very "C" style becuase the DLL API is C
  97. int Result = 0;
  98. Result = startupSNF(ConfigurationPath);
  99. //Result = startupSNFAuthenticated(
  100. // ConfigurationPath,
  101. // LicenseID,
  102. // Authentication);
  103. System.Console.WriteLine("Started with config " + ConfigurationPath + " Result: " + Result);
  104. // IP tests can be done asynchrounously - they do not have to be part of any particular scan.
  105. Result = testIP(IPToTest);
  106. System.Console.WriteLine("IP test result: " + Result);
  107. double IPReputation = getIPReputation(IPToTest);
  108. System.Console.WriteLine("IP Reputation: " + IPReputation);
  109. // Messgae scans happen in a scan, read, close cycle as shown inside this loop.
  110. const int NumberOfScans = 10;
  111. for(int i = 0; i < NumberOfScans; i++) {
  112. // Show how the IP reputation changes over time.
  113. IPReputation = getIPReputation(IPToTest);
  114. System.Console.WriteLine("IP Reputation: " + IPReputation);
  115. // Scan a message from a buffer.
  116. int SetupTime = 12;
  117. int ScanHandle = 0;
  118. ScanHandle = scanBuffer(SampleMessage, SampleMessage.Length, "TestMessage", SetupTime);
  119. System.Console.WriteLine("Scan Handle: " + ScanHandle);
  120. // Retrieve the X-Headers for the scan.
  121. IntPtr XHeadersPtr = IntPtr.Zero;
  122. int XHeadersLength = 0;
  123. int ScanResult = 0;
  124. ScanResult = getScanXHeaders(ScanHandle, out XHeadersPtr, out XHeadersLength);
  125. string XHeaders;
  126. XHeaders = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(XHeadersPtr, XHeadersLength);
  127. // Close the scan.
  128. Result = closeScan(ScanHandle);
  129. System.Console.WriteLine("Scan Close Result: " + Result);
  130. // X- headers were captured in a string BEFORE closing the scan so we can
  131. // use them here.
  132. System.Console.WriteLine("Scan result code: " + ScanResult);
  133. System.Console.WriteLine("Scan X- headers: " + XHeaders);
  134. }
  135. // Now that all scanning is done we shut down.
  136. Result = shutdownSNF();
  137. }
  138. }
  139. }