1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| public static class SnmpServer { public static void StartListening(Dictionary<string, string> dictionary, int port) { while (true) { var receiver = new UdpClient(port); IPEndPoint remoteIp = null;
try { while (true) { var data = receiver.Receive(ref remoteIp); int requestIdBytesLength = data[16]; var requestIdBytes = new byte[requestIdBytesLength]; Buffer.BlockCopy(data, 17, requestIdBytes, 0, requestIdBytesLength); int inputOidLength = data[32]; var inputOidBytes = new byte[inputOidLength]; Buffer.BlockCopy(data, 33, inputOidBytes, 0, inputOidLength); var inputOid = ConvertBytesToStringOid(inputOidBytes); if (dictionary.TryGetValue(inputOid, out var oidValue)) { var sender = new UdpClient(remoteIp.Address.ToString(), remoteIp.Port); var oidValueBytes = Encoding.ASCII.GetBytes(oidValue); var response = GenerateOidBytesResponse( requestIdBytes, inputOidBytes, oidValueBytes ); sender.Send(response, response.Length); sender.Close(); sender.Dispose(); } } } catch (Exception) { receiver.Close(); } } }
private static byte[] GenerateOidBytesResponse( byte[] requestIdBytes, byte[] inputOidBytes, byte[] oidValueBytes ) { var result1 = new List<byte>(oidValueBytes); result1.Insert(0, (byte)oidValueBytes.Length); result1.Insert(0, 0x04); var result2 = new List<byte>(inputOidBytes); result2.AddRange(result1); result2.Insert(0, (byte)inputOidBytes.Length); result2.Insert(0, 0x06); result2.Insert(0, (byte)result2.Count); result2.Insert(0, 0x30); result2.Insert(0, (byte)result2.Count); result2.Insert(0, 0x30); Magic210(result2); Magic210(result2);
var result3 = new List<byte>(requestIdBytes); result3.AddRange(result2); result3.Insert(0, (byte)requestIdBytes.Length); result3.Insert(0, 0x02); result3.Insert(0, (byte)result3.Count); result3.Insert(0, 0xa2);
CommunityPublic(result3); Magic210(result3); result3.Insert(0, (byte)result3.Count); result3.Insert(0, 0x30);
return result3.ToArray(); }
private static void CommunityPublic(List<byte> list) { list.Insert(0, 0x63); list.Insert(0, 0x69); list.Insert(0, 0x6c); list.Insert(0, 0x62); list.Insert(0, 0x75); list.Insert(0, 0x70); list.Insert(0, 0x06); list.Insert(0, 0x04); }
private static void Magic210(List<byte> list) { list.Insert(0, 0x00); list.Insert(0, 0x01); list.Insert(0, 0x02); }
private static string ConvertBytesToStringOid(byte[] oidBytes) { var builder = new StringBuilder(); var result = new List<uint> { (uint)(oidBytes[0] / 40), (uint)(oidBytes[0] % 40) }; uint buffer = 0; for (var i = 1; i < oidBytes.Length; i++) { if ((oidBytes[i] & 0x80) == 0) { result.Add(oidBytes[i] + (buffer << 7)); buffer = 0; } else { buffer <<= 7; buffer += (uint)(oidBytes[i] & 0x7F); } }
for (var i = 0; i < result.Count; i++) { builder.Append(result[i]); if (i != result.Count - 1) builder.Append('.'); }
return builder.ToString(); } }
|