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.

main.cs 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 = "..\\..\\snf_engine.xml"; // For "Any CPU" platform.
  82. const string ConfigurationPath = "..\\..\\..\\snf_engine.xml"; // For "x86" or "x64" platforms.
  83. const uint IPToTest = 0x0c22384e; // Same as IP 12.34.56.78
  84. const string SampleMessage =
  85. "Received: from mx-out.example.com [12.34.56.78] (HELO Somebody)\r\n" +
  86. " by mx-in.example.com (nosuchserver v1.0) for nobody@example.com\r\n" +
  87. "From: <somebody@example.com>\r\n" +
  88. "To: <nobody@example.com>\r\n" +
  89. "Subject: Nothing to see here\r\n" +
  90. "\r\n" +
  91. "So this is the big thing that's not here to see.\r\n" +
  92. "I thought it would be more interesting than this.\r\n" +
  93. "\r\n" +
  94. "_M\r\n" +
  95. ".\r\n";
  96. //// Here is a simple example. Startup, exercise the API, shut down.
  97. //// Note that we're doing this in a very "C" style becuase the DLL API is C
  98. int Result = 0;
  99. Result = startupSNF(ConfigurationPath);
  100. //Result = startupSNFAuthenticated(
  101. // ConfigurationPath,
  102. // LicenseID,
  103. // Authentication);
  104. System.Console.WriteLine("Started with config " + ConfigurationPath + " Result: " + Result);
  105. // IP tests can be done asynchrounously - they do not have to be part of any particular scan.
  106. Result = testIP(IPToTest);
  107. System.Console.WriteLine("IP test result: " + Result);
  108. double IPReputation = getIPReputation(IPToTest);
  109. System.Console.WriteLine("IP Reputation: " + IPReputation);
  110. // Messgae scans happen in a scan, read, close cycle as shown inside this loop.
  111. const int NumberOfScans = 10;
  112. for(int i = 0; i < NumberOfScans; i++) {
  113. // Show how the IP reputation changes over time.
  114. IPReputation = getIPReputation(IPToTest);
  115. System.Console.WriteLine("IP Reputation: " + IPReputation);
  116. // Scan a message from a buffer.
  117. int SetupTime = 12;
  118. int ScanHandle = 0;
  119. ScanHandle = scanBuffer(SampleMessage, SampleMessage.Length, "TestMessage", SetupTime);
  120. System.Console.WriteLine("Scan Handle: " + ScanHandle);
  121. // Retrieve the X-Headers for the scan.
  122. IntPtr XHeadersPtr = IntPtr.Zero;
  123. int XHeadersLength = 0;
  124. int ScanResult = 0;
  125. ScanResult = getScanXHeaders(ScanHandle, out XHeadersPtr, out XHeadersLength);
  126. string XHeaders;
  127. XHeaders = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(XHeadersPtr, XHeadersLength);
  128. // Close the scan.
  129. Result = closeScan(ScanHandle);
  130. System.Console.WriteLine("Scan Close Result: " + Result);
  131. // X- headers were captured in a string BEFORE closing the scan so we can
  132. // use them here.
  133. System.Console.WriteLine("Scan result code: " + ScanResult);
  134. System.Console.WriteLine("Scan X- headers: " + XHeaders);
  135. }
  136. // Now that all scanning is done we shut down.
  137. Result = shutdownSNF();
  138. }
  139. }
  140. }