Permalink
Browse files

Move to old-code

svn path=/old-code/; revision=156238
  • Loading branch information...
1 parent dde4013 commit 953cc8ea9c23e546e3dcc91a6165a9d2f15c24eb @migueldeicaza migueldeicaza committed Apr 27, 2010
View
@@ -0,0 +1,3 @@
+2005-01-30 Zac Bowling <zac@zacbowling.com>
+
+ * * : Intial commit of Mono.Net.Snmp
View
@@ -0,0 +1,21 @@
+Copyright (c) 2001, 2002, 2003 Ximian, Inc and the individuals listed
+on the ChangeLog entries.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,12 @@
+CSC=mcs
+#PROGFILES=`cygpath \`cygpath -m -s "$(PROGRAMFILES)"\``
+#CSC=$(PROGFILES)/Mono-1.1.3/bin/mcs.bat /d:WIN32
+#ROOT=/cygdrive/$(subst \,/,$(subst :\,/,$(SYSTEMROOT)))
+#CSC=$(ROOT)/microsoft.net/framework/v1.1.4322/csc.exe /d:WIN32 /optimize+
+
+default:
+ $(CSC) /target:library ./src/SNMP.cs ./src/AssemblyInfo.cs /out:Mono.Net.Snmp.dll
+ $(CSC) /target:exe ./samples/SimpleSNMP.cs /r:Mono.Net.Snmp.dll /out:./samples/SimpleSNMP.exe
+ $(CSC) /target:exe ./samples/MacAddress.cs /r:Mono.Net.Snmp.dll /out:./samples/MacAddress.exe
+clean:
+ rm Mono.Net.Snmp.dll ./samples/SimpleSNMP.exe ./samples/MacAddress.exe
Binary file not shown.
View
@@ -0,0 +1,24 @@
+Mono.Net.Snmp
+Copyright 2005(C) Zac Bowling
+zac@zacbowling.com
+http://zacbowling.com
+
+Licenced under the terms of the MIT X11 licence.
+Read the MIT.X11 for more information.
+
+Known issues:
+* No MIB support. All values need to be passed as the raw OID.
+* No OID values larger 32,768
+* No async support...
+
+How to build:
+1. if necissary, put mcs or csc path into makefile
+2. run `make`
+
+SAMPLES:
+
+SimpleSNMP - Read basic SNMP device information:
+> mono samples/SimpleSNMP.exe <ipaddress> <community>
+
+MacAddress - Query a remote Mac Address:
+> mono samples/MacAddress.exe <ipaddress> <community>
@@ -0,0 +1,8 @@
+all:
+ csc.exe /d:WIN32 /optimize+ /target:library src\SNMP.cs src\AssemblyInfo.cs /out:Mono.Net.Snmp.dll
+ csc.exe /d:WIN32 /optimize+ /target:exe samples\SimpleSNMP.cs /r:Mono.Net.Snmp.dll /out:samples\SimpleSNMP.exe
+ csc.exe /d:WIN32 /optimize+ /target:exe samples\MacAddress.cs /r:Mono.Net.Snmp.dll /out:samples\MacAddress.exe
+clean:
+ del Mono.Net.Snmp.dll
+ del samples\SimpleSNMP.exe
+ del samples\MacAddress.exe
@@ -0,0 +1,35 @@
+using System;
+using Mono.Net.Snmp;
+
+namespace Mono.Net.Snmp.Samples
+{
+class MacAddress
+{
+ public static void Main(string[] argv)
+ {
+ int commlength, miblength, datastart, datalength;
+ string nextmib, value;
+ SNMP conn = new SNMP();
+ string mib = "1.3.6.1.2.1.17.4.3.1.1";
+ int orgmiblength = mib.Length;
+ byte[] response = new byte[1024];
+
+ nextmib = mib;
+
+ while (true)
+ {
+ response = conn.get("getnext", argv[0], argv[1], nextmib);
+ commlength = Convert.ToInt16(response[6]);
+ miblength = Convert.ToInt16(response[23 + commlength]);
+ datalength = Convert.ToInt16(response[25 + commlength + miblength]);
+ datastart = 26 + commlength + miblength;
+ value = BitConverter.ToString(response, datastart, datalength);
+ nextmib = conn.getnextMIB(response);
+ if (!(nextmib.Substring(0, orgmiblength) == mib))
+ break;
+
+ Console.WriteLine("{0} = {1}", nextmib, value);
+ }
+ }
+}
+}
@@ -0,0 +1,108 @@
+using System;
+using System.Text;
+using Mono.Net.Snmp;
+
+
+namespace Mono.Net.Snmp.Samples
+{
+class SimpleSNMP
+{
+ public static void Main(string[] argv)
+ {
+ int commlength, miblength, datatype, datalength, datastart;
+ int uptime = 0;
+ string output;
+ SNMP conn = new SNMP();
+ byte[] response = new byte[1024];
+
+ Console.WriteLine("Device SNMP information:");
+
+ // Send sysName SNMP request
+ response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.5.0");
+ if (response[0] == 0xff)
+ {
+ Console.WriteLine("No response from {0}", argv[0]);
+ return;
+ }
+
+ // If response, get the community name and MIB lengths
+ commlength = Convert.ToInt16(response[6]);
+ miblength = Convert.ToInt16(response[23 + commlength]);
+
+ // Extract the MIB data from the SNMP response
+ datatype = Convert.ToInt16(response[24 + commlength + miblength]);
+ datalength = Convert.ToInt16(response[25 + commlength + miblength]);
+ datastart = 26 + commlength + miblength;
+ output = Encoding.ASCII.GetString(response, datastart, datalength);
+ Console.WriteLine(" sysName - Datatype: {0}, Value: {1}",
+ datatype, output);
+
+ // Send a sysLocation SNMP request
+ response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.6.0");
+ if (response[0] == 0xff)
+ {
+ Console.WriteLine("No response from {0}", argv[0]);
+ return;
+ }
+
+ // If response, get the community name and MIB lengths
+ commlength = Convert.ToInt16(response[6]);
+ miblength = Convert.ToInt16(response[23 + commlength]);
+
+ // Extract the MIB data from the SNMP response
+ datatype = Convert.ToInt16(response[24 + commlength + miblength]);
+ datalength = Convert.ToInt16(response[25 + commlength + miblength]);
+ datastart = 26 + commlength + miblength;
+ output = Encoding.ASCII.GetString(response, datastart, datalength);
+ Console.WriteLine(" sysLocation - Datatype: {0}, Value: {1}", datatype, output);
+
+ // Send a sysContact SNMP request
+ response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.4.0");
+ if (response[0] == 0xff)
+ {
+ Console.WriteLine("No response from {0}", argv[0]);
+ return;
+ }
+
+ // Get the community and MIB lengths
+ commlength = Convert.ToInt16(response[6]);
+ miblength = Convert.ToInt16(response[23 + commlength]);
+
+ // Extract the MIB data from the SNMP response
+ datatype = Convert.ToInt16(response[24 + commlength + miblength]);
+ datalength = Convert.ToInt16(response[25 + commlength + miblength]);
+ datastart = 26 + commlength + miblength;
+ output = Encoding.ASCII.GetString(response, datastart, datalength);
+ Console.WriteLine(" sysContact - Datatype: {0}, Value: {1}",
+ datatype, output);
+
+ // Send a SysUptime SNMP request
+ response = conn.get("get", argv[0], argv[1], "1.3.6.1.2.1.1.3.0");
+ if (response[0] == 0xff)
+ {
+ Console.WriteLine("No response from {0}", argv[0]);
+ return;
+ }
+
+ // Get the community and MIB lengths of the response
+ commlength = Convert.ToInt16(response[6]);
+ miblength = Convert.ToInt16(response[23 + commlength]);
+
+ // Extract the MIB data from the SNMp response
+ datatype = Convert.ToInt16(response[24 + commlength + miblength]);
+ datalength = Convert.ToInt16(response[25 + commlength + miblength]);
+ datastart = 26 + commlength + miblength;
+
+ // The sysUptime value may by a multi-byte integer
+ // Each byte read must be shifted to the higher byte order
+ while(datalength > 0)
+ {
+ uptime = (uptime << 8) + response[datastart++];
+ datalength--;
+ }
+ Console.WriteLine(" sysUptime - Datatype: {0}, Value: {1}",
+ datatype, uptime);
+
+ }
+}
+}
@@ -0,0 +1,6 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+[assembly:AssemblyVersion("1.0.0.0")]
+[assembly:AssemblyDelaySign(false)]
+[assembly:AssemblyKeyFile("Mono.Net.Snmp.snk")]
View
@@ -0,0 +1,166 @@
+/// COPYRIGHT 2005(C) ZACBOWLING
+/// LICENCE: X11
+///
+/// I got a lot from the book "C# Networking"
+/// the ASN.1 docs.
+///
+using System;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+
+namespace Mono.Net.Snmp
+{
+public class SNMP
+{
+ public SNMP()
+ {
+
+ }
+
+ public byte[] get(string request, string host, string community, string OIDstring)
+ {
+ byte[] oid, packet;
+ int snmplen, comlen, OIDlen, cnt = 0, temp, i, pos = 0, orgoidlen;
+ string[] OIDvals;
+
+ packet = new byte[1024]; //if its bigger too bad :-)
+ oid = new byte[1024]; //to store our OID
+
+ comlen = community.Length;
+ OIDvals = OIDstring.Split('.');
+ OIDlen = OIDvals.Length;
+ orgoidlen = OIDvals.Length;
+
+ // Convert the OID string into a byte array of integer values
+ // Values over 128 require multiple bytes which also increases the
+ // OID length because its ASN.1.. no way to know before to well before
+ for (i = 0; i < orgoidlen; i++)
+ {
+ temp = Convert.ToInt16(OIDvals[i]);
+ if (temp > 127)
+ {
+ oid[cnt] = Convert.ToByte(128 + (temp / 128));
+ oid[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128));
+ cnt += 2;
+ OIDlen++;
+ } else
+ {
+ oid[cnt] = Convert.ToByte(temp);
+ cnt++;
+ }
+ }
+ snmplen = 29 + comlen + OIDlen - 1; //Length of entire SNMP packet
+
+ //The SNMP sequence start
+ packet[pos++] = 0x30; //Sequence start
+ packet[pos++] = Convert.ToByte(snmplen - 2); //sequence size
+
+ //SNMP version
+ packet[pos++] = 0x02; //Integer type
+ packet[pos++] = 0x01; //length
+ packet[pos++] = 0x00; //SNMP version 1
+
+ //Community name
+ packet[pos++] = 0x04; // String type
+ packet[pos++] = Convert.ToByte(comlen); //length
+ //Convert community name to byte array
+ byte[] data = Encoding.ASCII.GetBytes(community);
+ for (i = 0; i < data.Length; i++)
+ {
+ packet[pos++] = data[i];
+ }
+
+ //Add GetRequest or GetNextRequest value
+ if (request == "get")
+ packet[pos++] = 0xA0;
+ else
+ packet[pos++] = 0xA1;
+
+ packet[pos++] = Convert.ToByte(20 + OIDlen - 1); //Size of total OID
+
+ //Request ID
+ packet[pos++] = 0x02; //Integer type
+ packet[pos++] = 0x04; //length
+ packet[pos++] = 0x00; //SNMP request ID
+ packet[pos++] = 0x00;
+ packet[pos++] = 0x00;
+ packet[pos++] = 0x01;
+
+ //Error status
+ packet[pos++] = 0x02; //Integer type
+ packet[pos++] = 0x01; //length
+ packet[pos++] = 0x00; //SNMP error status
+
+ //Error index
+ packet[pos++] = 0x02; //Integer type
+ packet[pos++] = 0x01; //length
+ packet[pos++] = 0x00; //SNMP error index
+
+ //Start of variable bindings
+ packet[pos++] = 0x30; //Start of variable bindings sequence
+
+ packet[pos++] = Convert.ToByte(6 + OIDlen - 1); // Size of variable binding
+
+ packet[pos++] = 0x30; //Start of first variable bindings sequence
+ packet[pos++] = Convert.ToByte(6 + OIDlen - 1 - 2); // size
+ packet[pos++] = 0x06; //Object type
+ packet[pos++] = Convert.ToByte(OIDlen - 1); //length
+
+ //Start of MIB
+ packet[pos++] = 0x2b;
+ //Place MIB array in packet
+ for(i = 2; i < OIDlen; i++)
+ packet[pos++] = Convert.ToByte(oid[i]);
+ packet[pos++] = 0x05; //Null object value
+ packet[pos++] = 0x00; //Null
+
+ //Send packet to destination
+ Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
+ ProtocolType.Udp);
+ sock.SetSocketOption(SocketOptionLevel.Socket,
+ SocketOptionName.ReceiveTimeout, 5000);
+ IPHostEntry ihe = Dns.Resolve(host);
+ IPEndPoint iep = new IPEndPoint(ihe.AddressList[0], 161);
+ EndPoint ep = (EndPoint)iep;
+ sock.SendTo(packet, snmplen, SocketFlags.None, iep);
+
+ //Now we check for the response :-)
+ //TODO: Maybe make this an async or not necissary. Blocking...
+ try
+ {
+ int recv = sock.ReceiveFrom(packet, ref ep);
+ } catch (SocketException)
+ {
+ packet[0] = 0xff;
+ }
+ return packet;
+ }
+
+ public string getnextMIB(byte[] OID)
+ {
+ string outputStr;
+ int commlength, OIDstart, OIDlength, OIDvalue;
+
+ commlength = OID[6];
+ OIDstart = 6 + commlength + 17; //we don't care, just jump to the oid
+ outputStr = "1.3"; //Prepend the starting OID
+
+ //The MIB length is the length-1 trimming the .0
+ OIDlength = OID[OIDstart] - 1;
+ OIDstart += 2; //skip over the length and 0x2b values
+
+ for(int i = OIDstart; i < OIDstart + OIDlength; i++)
+ {
+ OIDvalue = Convert.ToInt16(OID[i]);
+ if (OIDvalue > 128)
+ {
+ OIDvalue = (OIDvalue/128)*128 + Convert.ToInt16(OID[i+1]);
+ i++;
+ }
+ outputStr += "." + OIDvalue;
+ }
+ return outputStr;
+ }
+}
+}

0 comments on commit 953cc8e

Please sign in to comment.