/PkFunction.cs

http://httpwatch.codeplex.com · C# · 455 lines · 351 code · 80 blank · 24 comment · 53 complexity · 02802acc3ba7680353a1f7c93640c78f MD5 · raw file

  1. using SECURITY_DESCRIPTOR = System.Int32;
  2. using System.Runtime.InteropServices;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. using System.Net;
  7. using System;
  8. namespace CnCerT.Net.Packet
  9. {
  10. /// <summary>
  11. /// ??????? ??set4bytes
  12. /// </summary>
  13. public class PkFunction
  14. {
  15. public const int NORMAL = 0;
  16. public const int VALUE = 1;
  17. /// <summary>
  18. /// ?????????
  19. /// </summary>
  20. /// <param name="datastr"></param>
  21. /// <returns></returns>
  22. public static byte[] StrToByte(string datastr)
  23. {
  24. if (datastr.Length % 2 == 1) datastr += "00";
  25. byte[] ret = new byte[datastr.Length / 2];
  26. for (int i = 0; i < datastr.Length; i += 2)
  27. {
  28. if (i < datastr.Length) ret[i / 2] = (byte)(System.Uri.FromHex(datastr[i]) * 16 + System.Uri.FromHex(datastr[i + 1]));
  29. }
  30. return ret;
  31. }
  32. public static bool Islocalip(string ip)//????IP
  33. {
  34. if (ip.Substring(0, ip.IndexOf(".")) == "10" || ip.Substring(0, ip.IndexOf(".")) == "192" || ip.Substring(0, ip.IndexOf(".")) == "172")
  35. {
  36. return true;
  37. }
  38. else
  39. {
  40. return false;
  41. }
  42. }
  43. public static string GetIpAddress(string SrvName)
  44. {
  45. IPHostEntry ipEntry = new IPHostEntry();
  46. IPAddress Ip = new IPAddress(0);
  47. if (SrvName.Trim() == "")
  48. {
  49. //ipEntry = Dns.Resolve( Dns.GetHostName() );
  50. ipEntry = Dns.GetHostEntry(Dns.GetHostName());
  51. }
  52. else
  53. {
  54. ipEntry = Dns.GetHostEntry(SrvName);
  55. }
  56. Ip = ipEntry.AddressList[0];
  57. return Ip.ToString();
  58. }
  59. public static string GetHostName(string IpAddr)
  60. {
  61. IPHostEntry iphEntry = new IPHostEntry();
  62. if (IpAddr.Trim() == "")
  63. {
  64. return Dns.GetHostName();
  65. }
  66. else
  67. {
  68. //iphEntry = Dns.GetHostByAddress(IpAddr );
  69. iphEntry = Dns.GetHostEntry(IpAddr);
  70. }
  71. return iphEntry.HostName;
  72. }
  73. public static ushort Get2Bytes(byte[] ptr, ref int Index, int Type)
  74. {
  75. ushort u = 0;
  76. if (Type == NORMAL)
  77. {
  78. u = (ushort)ptr[Index++];
  79. u *= 256;
  80. u += (ushort)ptr[Index++];
  81. }
  82. else if (Type == VALUE)
  83. {
  84. u = (ushort)ptr[++Index];
  85. u *= 256; Index--;
  86. u += (ushort)ptr[Index++]; Index++;
  87. }
  88. return u;
  89. }
  90. public static void KillProcess(string processName)
  91. {
  92. System.Diagnostics.Process myproc = new System.Diagnostics.Process();
  93. try
  94. {
  95. foreach (Process thisproc in Process.GetProcessesByName(processName))
  96. {
  97. if (!thisproc.CloseMainWindow())
  98. {
  99. thisproc.Kill();
  100. }
  101. }
  102. }
  103. catch {
  104. }
  105. }
  106. public static bool CheckProcess(string processName)
  107. {
  108. System.Diagnostics.Process myproc = new System.Diagnostics.Process();
  109. try
  110. {
  111. int i = 0;
  112. foreach (Process thisproc in Process.GetProcessesByName(processName))
  113. {
  114. i++;
  115. }
  116. if (i > 1) return true;
  117. return false;
  118. }
  119. catch
  120. {
  121. return false ;
  122. }
  123. }
  124. public static void Set2Bytes(ref byte[] ptr, ref int Index, ushort NewValue, int Type)
  125. {
  126. if (Type == NORMAL)
  127. {
  128. ptr[Index] = (byte)(NewValue >> 8);
  129. ptr[Index + 1] = (byte)NewValue;
  130. }
  131. else if (Type == VALUE)
  132. {
  133. ptr[Index + 0] = (byte)NewValue;
  134. ptr[Index + 1] = (byte)(NewValue >> 8);
  135. Index += 2;
  136. }
  137. }
  138. public static uint Get3Bytes(byte[] ptr, ref int Index, int Type)
  139. {
  140. uint ui = 0;
  141. if (Type == NORMAL)
  142. {
  143. ui = ((uint)ptr[Index++]) << 16;
  144. ui += ((uint)ptr[Index++]) << 8;
  145. ui += (uint)ptr[Index++];
  146. }
  147. return ui;
  148. }
  149. public static uint Get4Bytes(byte[] ptr, ref int Index, int Type)
  150. {
  151. uint ui = 0;
  152. if (Type == NORMAL)
  153. {
  154. ui = ((uint)ptr[Index++]) << 24;
  155. ui += ((uint)ptr[Index++]) << 16;
  156. ui += ((uint)ptr[Index++]) << 8;
  157. ui += (uint)ptr[Index++];
  158. }
  159. else if (Type == VALUE)
  160. {
  161. ui = ((uint)ptr[Index + 3]) << 24;
  162. ui += ((uint)ptr[Index + 2]) << 16;
  163. ui += ((uint)ptr[Index + 1]) << 8;
  164. ui += (uint)ptr[Index]; Index += 4;
  165. }
  166. return ui;
  167. }
  168. public static void Set4Bytes(ref byte[] ptr,ref int Index, uint NewValue, int Type)
  169. {
  170. if (Type == NORMAL)
  171. {
  172. ptr[Index ] = (byte)(NewValue >> 24);
  173. ptr[Index + 1] = (byte)(NewValue >> 16);
  174. ptr[Index + 2] = (byte)(NewValue >> 8);
  175. ptr[Index + 3] = (byte)NewValue;
  176. }
  177. else if (Type == VALUE)
  178. {
  179. ptr[Index] = (byte)NewValue;
  180. ptr[Index + 1] = (byte)(NewValue >> 8);
  181. ptr[Index + 2] = (byte)(NewValue >> 16);
  182. ptr[Index + 3] = (byte)(NewValue >> 24);
  183. }
  184. Index += 4;
  185. }
  186. public static ulong Get8Bytes(byte[] ptr, ref int Index, int Type)
  187. {
  188. ulong ui = 0;
  189. if (Type == NORMAL)
  190. {
  191. ui = ((uint)ptr[Index++]) << 56;
  192. ui += ((uint)ptr[Index++]) << 48;
  193. ui += ((uint)ptr[Index++]) << 40;
  194. ui += ((uint)ptr[Index++]) << 32;
  195. ui += ((uint)ptr[Index++]) << 24;
  196. ui += ((uint)ptr[Index++]) << 16;
  197. ui += ((uint)ptr[Index++]) << 8;
  198. ui += (uint)ptr[Index++];
  199. }
  200. else if (Type == VALUE)
  201. {
  202. ui = ((uint)ptr[Index + 7]) << 56;
  203. ui += ((uint)ptr[Index + 6]) << 48;
  204. ui += ((uint)ptr[Index + 5]) << 40;
  205. ui += ((uint)ptr[Index + 4]) << 32;
  206. ui += ((uint)ptr[Index + 3]) << 24;
  207. ui += ((uint)ptr[Index + 2]) << 16;
  208. ui += ((uint)ptr[Index + 1]) << 8;
  209. ui += (uint)ptr[Index]; Index += 8;
  210. }
  211. return ui;
  212. }
  213. public static void Set8Bytes(ref byte[] ptr,ref int Index, ulong NewValue, int Type)
  214. {
  215. if (Type == NORMAL)
  216. {
  217. ptr[Index] = (byte)(NewValue >> 56);
  218. ptr[Index + 1] = (byte)(NewValue >> 48);
  219. ptr[Index + 2] = (byte)(NewValue >> 40);
  220. ptr[Index + 3] = (byte)(NewValue >> 32);
  221. ptr[Index + 4] = (byte)(NewValue >> 24);
  222. ptr[Index + 5] = (byte)(NewValue >> 16);
  223. ptr[Index + 6] = (byte)(NewValue >> 8);
  224. ptr[Index + 7] = (byte)NewValue;
  225. }
  226. else if (Type == VALUE)
  227. {
  228. ptr[Index + 7] = (byte)(NewValue >> 56);
  229. ptr[Index + 6] = (byte)(NewValue >> 48);
  230. ptr[Index + 5] = (byte)(NewValue >> 40);
  231. ptr[Index + 4] = (byte)(NewValue >> 32);
  232. ptr[Index + 3] = (byte)(NewValue >> 24);
  233. ptr[Index + 2] = (byte)(NewValue >> 16);
  234. ptr[Index + 1] = (byte)(NewValue >> 8);
  235. ptr[Index] = (byte)NewValue;
  236. Index += 8;
  237. }
  238. }
  239. public static string GetIpAddress(byte[] ptr, ref int Index)
  240. {
  241. string str = "";
  242. str += ptr[Index++].ToString() + ".";
  243. str += ptr[Index++].ToString() + ".";
  244. str += ptr[Index++].ToString() + ".";
  245. str += ptr[Index++].ToString();
  246. return str;
  247. }
  248. public static string GetIpAddress(byte[] ptr, ref int Index, int Length)
  249. {
  250. string str = "";
  251. int i = 0;
  252. for (i = 0; i < Length - 1; i++)
  253. str += ptr[Index++].ToString() + ".";
  254. str += ptr[Index++].ToString();
  255. return str;
  256. }
  257. public static short Swap(short s)
  258. {
  259. short b1 = 0, b2 = 0;
  260. short rs;
  261. b1 = (short)((s >> 8) & 0x00ff);
  262. b2 = (short)((s & 0x00ff) << 8);
  263. rs = (short)(b1 + b2);
  264. return rs;
  265. }
  266. public static ushort Swap(ushort us)
  267. {
  268. ushort b1 = 0, b2 = 0;
  269. ushort rus;
  270. b1 = (ushort)((us >> 8) & 0x00ff);
  271. b2 = (ushort)((us & 0x00ff) << 8);
  272. rus = (ushort)(b1 + b2);
  273. return rus;
  274. }
  275. public static int Swap(int i)
  276. {
  277. int b1 = 0, b2 = 0, b3 = 0, b4 = 0;
  278. int ri;
  279. b1 = (i >> 24) & 0x000000ff;
  280. b2 = ((i >> 16) & 0x000000ff) << 8;
  281. b3 = ((i >> 8) & 0x000000ff) << 16;
  282. b4 = (i & 0x000000ff) << 24;
  283. ri = b1 + b2 + b3 + b4;
  284. return ri;
  285. }
  286. public static uint Swap(uint ui)
  287. {
  288. uint b1 = 0, b2 = 0, b3 = 0, b4 = 0;
  289. uint rui;
  290. b1 = (ui >> 24) & 0x000000ff;
  291. b2 = ((ui >> 16) & 0x000000ff) << 8;
  292. b3 = ((ui >> 8) & 0x000000ff) << 16;
  293. b4 = (ui & 0x000000ff) << 24;
  294. rui = b1 + b2 + b3 + b4;
  295. return rui;
  296. }
  297. public static long Swap(long l)
  298. {
  299. long b1 = 0, b2 = 0, b3 = 0, b4 = 0;
  300. long b5 = 0, b6 = 0, b7 = 0, b8 = 0;
  301. long rl;
  302. b1 = ((l >> 56) & 0x00000000000000ff);
  303. b2 = ((l >> 48) & 0x00000000000000ff) << 8;
  304. b3 = ((l >> 40) & 0x00000000000000ff) << 16;
  305. b4 = ((l >> 32) & 0x00000000000000ff) << 24;
  306. b5 = ((l >> 24) & 0x00000000000000ff) << 32;
  307. b6 = ((l >> 16) & 0x00000000000000ff) << 40;
  308. b7 = ((l >> 8) & 0x00000000000000ff) << 48;
  309. b8 = (l & 0x00000000000000ff) << 56;
  310. rl = b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8;
  311. return rl;
  312. }
  313. public static ulong Swap(ulong ul)
  314. {
  315. ulong b1 = 0, b2 = 0, b3 = 0, b4 = 0;
  316. ulong b5 = 0, b6 = 0, b7 = 0, b8 = 0;
  317. ulong url;
  318. b1 = ((ul >> 56) & 0x00000000000000ff);
  319. b2 = ((ul >> 48) & 0x00000000000000ff) << 8;
  320. b3 = ((ul >> 40) & 0x00000000000000ff) << 16;
  321. b4 = ((ul >> 32) & 0x00000000000000ff) << 24;
  322. b5 = ((ul >> 24) & 0x00000000000000ff) << 32;
  323. b6 = ((ul >> 16) & 0x00000000000000ff) << 40;
  324. b7 = ((ul >> 8) & 0x00000000000000ff) << 48;
  325. b8 = (ul & 0x00000000000000ff) << 56;
  326. url = b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8;
  327. return url;
  328. }
  329. /// <summary>
  330. /// FF:FF:FF
  331. /// </summary>
  332. /// <param name="PacketData"></param>
  333. /// <param name="Index"></param>
  334. /// <returns></returns>
  335. public static string GetMACAddress(byte[] PacketData, ref int Index)
  336. {
  337. string Tmp = "";
  338. int i = 0;
  339. for (i = 0; i < 5; i++)//?5?HEX
  340. {
  341. Tmp += PacketData[Index++].ToString("x02") + ":";
  342. }
  343. Tmp += PacketData[Index++].ToString("x02");//????HEX
  344. // MessageBox.Show(Tmp.ToUpper());
  345. return Tmp.ToUpper();
  346. }
  347. /// <summary>
  348. /// ?????? FFFFF
  349. /// </summary>
  350. /// <param name="PacketData"></param>
  351. /// <param name="Index"></param>
  352. /// <returns></returns>
  353. public static string GetMACAddress2(byte[] PacketData, ref int Index)
  354. {
  355. string Tmp = "";
  356. int i = 0;
  357. for (i = 0; i < 5; i++)//?5?HEX
  358. {
  359. Tmp += PacketData[Index++].ToString("x02");
  360. }
  361. Tmp += PacketData[Index++].ToString("x02");//????HEX
  362. // MessageBox.Show(Tmp.ToUpper());
  363. return Tmp.ToUpper();
  364. }
  365. public static void Setipaddres(ref byte[] PacketDate, string ip, ref int Index)//??IP
  366. {
  367. if (ip != null)
  368. {
  369. byte[] _ipaddr = IPAddress.Parse(ip).GetAddressBytes();
  370. Array.Copy(_ipaddr, 0, PacketDate, Index, 4);
  371. Index += _ipaddr.Length;
  372. }
  373. }
  374. }
  375. }