Csharp/C Sharp/Network/Socket Listener
Cisco Router
<source lang="csharp"> /* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765
- /
using System.Net; using System.Net.Sockets; using System; using System.Drawing; using System.IO; using System.Threading; using System.Windows.Forms; public class CiscoRouter : Form {
private TextBox host; private TextBox community; private ListBox results; private Thread monitor; private FileStream fs; private StreamWriter sw; public CiscoRouter() { Text = "Cisco Router Utilization"; Size = new Size(400, 380); Label label1 = new Label(); label1.Parent = this; label1.Text = "Host:"; label1.AutoSize = true; label1.Location = new Point(10, 30); host = new TextBox(); host.Parent = this; host.Size = new Size(170, 2 * Font.Height); host.Location = new Point(40, 27); Label label2 = new Label(); label2.Parent = this; label2.Text = "Community:"; label2.AutoSize = true; label2.Location = new Point(10, 60); community = new TextBox(); community.Parent = this; community.Size = new Size(170, 2 * Font.Height); community.Location = new Point(75, 57); results = new ListBox(); results.Parent = this; results.Location = new Point(10, 85); results.Size = new Size(360, 18 * Font.Height); Button start = new Button(); start.Parent = this; start.Text = "Start"; start.Location = new Point(250, 52); start.Size = new Size(5 * Font.Height, 2 * Font.Height); start.Click += new EventHandler(ButtonStartOnClick); Button stop = new Button(); stop.Parent = this; stop.Text = "Stop"; stop.Location = new Point(320, 52); stop.Size = new Size(5 * Font.Height, 2 * Font.Height); stop.Click += new EventHandler(ButtonStopOnClick); } void ButtonStartOnClick(Object obj, EventArgs ea) { monitor = new Thread(new ThreadStart(checkRouter)); monitor.IsBackground = true; monitor.Start(); } void ButtonStopOnClick(Object obj, EventArgs ea) { monitor.Abort(); sw.Close(); fs.Close(); } void checkRouter() { int commlength, miblength, datastart, cpuUtil; SNMP conn = new SNMP(); byte[] response = new byte[1024]; DateTime time; string logFile = "routerlog.txt"; fs = new FileStream(logFile, FileMode.OpenOrCreate, FileAccess.ReadWrite); sw = new StreamWriter(fs); while (true) { response = conn.get("get", host.Text, community.Text, "1.3.6.1.4.1.9.2.1.58.0"); if (response[0] == 0xff) { results.Items.Add("No reponse from host"); sw.WriteLine("No response from host"); sw.Flush(); break; } commlength = Convert.ToInt16(response[6]); miblength = Convert.ToInt16(response[23 + commlength]); datastart = 26 + commlength + miblength; cpuUtil = Convert.ToInt16(response[datastart]); time = DateTime.Now; results.Items.Add(time + " CPU Utilization: " + cpuUtil + "%"); sw.WriteLine("{0} CPU Utilization: {1}%", time, cpuUtil); sw.Flush(); Thread.Sleep(5 * 60000); } } public static void Main() { Application.Run(new CiscoRouter()); }
}
class SNMP {
public SNMP() { } public byte[] get(string request, string host, string community, string mibstring) { byte[] packet = new byte[1024]; byte[] mib = new byte[1024]; int snmplen; int comlen = community.Length; string[] mibvals = mibstring.Split("."); int miblen = mibvals.Length; int cnt = 0, temp, i; int orgmiblen = miblen; int pos = 0; // Convert the string MIB into a byte array of integer values // Unfortunately, values over 128 require multiple bytes // which also increases the MIB length for (i = 0; i < orgmiblen; i++) { temp = Convert.ToInt16(mibvals[i]); if (temp > 127) { mib[cnt] = Convert.ToByte(128 + (temp / 128)); mib[cnt + 1] = Convert.ToByte(temp - ((temp / 128) * 128)); cnt += 2; miblen++; } else { mib[cnt] = Convert.ToByte(temp); cnt++; } } snmplen = 29 + comlen + miblen - 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 + miblen - 1); //Size of total MIB //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 + miblen - 1); // Size of variable binding packet[pos++] = 0x30; //Start of first variable bindings sequence packet[pos++] = Convert.ToByte(6 + miblen - 1 - 2); // size packet[pos++] = 0x06; //Object type packet[pos++] = Convert.ToByte(miblen - 1); //length //Start of MIB packet[pos++] = 0x2b; //Place MIB array in packet for(i = 2; i < miblen; i++) packet[pos++] = Convert.ToByte(mib[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); //Receive response from packet try { int recv = sock.ReceiveFrom(packet, ref ep); } catch (SocketException) { packet[0] = 0xff; } return packet; } public string getnextMIB(byte[] mibin) { string output = "1.3"; int commlength = mibin[6]; int mibstart = 6 + commlength + 17; //find the start of the mib section //The MIB length is the length defined in the SNMP packet // minus 1 to remove the ending .0, which is not used int miblength = mibin[mibstart] - 1; mibstart += 2; //skip over the length and 0x2b values int mibvalue; for(int i = mibstart; i < mibstart + miblength; i++) { mibvalue = Convert.ToInt16(mibin[i]); if (mibvalue > 128) { mibvalue = (mibvalue/128)*128 + Convert.ToInt16(mibin[i+1]); i++; } output += "." + mibvalue; } return output; }
}
</source>
Listening For Sockets
<source lang="csharp"> /*
* C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: * Version: 1 */
using System; using System.Net; using System.Text; using System.Net.Sockets; namespace Client.Chapter_14___Networking_and_WWW {
public class ListeningForSockets { [STAThread] static void Main(string[] args) { int PortNumber = 10000; TcpListener MyListener = new TcpListener(PortNumber); MyListener.Start(); //Console.WriteLine("Waiting For Connection"); TcpClient MyClient = MyListener.AcceptTcpClient(); Console.WriteLine("Connection Accepted"); NetworkStream MyNetStream = MyClient.GetStream(); String Response = "Connection Has been accepted"; Byte[] SendTheseBytes = Encoding.ASCII.GetBytes(Response); MyNetStream.Write(SendTheseBytes, 0, SendTheseBytes.Length); MyClient.Close(); MyListener.Stop(); } }
}
</source>
Trace Route
<source lang="csharp"> /* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765
- /
using System; using System.Net; using System.Net.Sockets; using System.Text; public class TraceRoute {
public static void Main(string[] argv) { byte[] data = new byte[1024]; int recv, timestart, timestop; Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp); IPHostEntry iphe = Dns.Resolve(argv[0]); IPEndPoint iep = new IPEndPoint(iphe.AddressList[0], 0); EndPoint ep = (EndPoint)iep; ICMP packet = new ICMP(); packet.Type = 0x08; packet.Code = 0x00; packet.Checksum = 0; Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 0, 2); Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 2, 2); data = Encoding.ASCII.GetBytes("test packet"); Buffer.BlockCopy(data, 0, packet.Message, 4, data.Length); packet.MessageSize = data.Length + 4; int packetsize = packet.MessageSize + 4; UInt16 chcksum = packet.getChecksum(); packet.Checksum = chcksum; host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000); int badcount = 0; for (int i = 1; i < 50; i++) { host.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, i); timestart = Environment.TickCount; host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep); try { data = new byte[1024]; recv = host.ReceiveFrom(data, ref ep); timestop = Environment.TickCount; ICMP response = new ICMP(data, recv); if (response.Type == 11) Console.WriteLine("hop {0}: response from {1}, {2}ms", i, ep.ToString(), timestop-timestart); if (response.Type == 0) { Console.WriteLine("{0} reached in {1} hops, {2}ms.", ep.ToString(), i, timestop-timestart); break; } badcount = 0; } catch (SocketException) { Console.WriteLine("hop {0}: No response from remote host", i); badcount++; if (badcount == 5) { Console.WriteLine("Unable to contact remote host"); break; } } } host.Close(); }
} class ICMP {
public byte Type; public byte Code; public UInt16 Checksum; public int MessageSize; public byte[] Message = new byte[1024]; public ICMP() { } public ICMP(byte[] data, int size) { Type = data[20]; Code = data[21]; Checksum = BitConverter.ToUInt16(data, 22); MessageSize = size - 24; Buffer.BlockCopy(data, 24, Message, 0, MessageSize); } public byte[] getBytes() { byte[] data = new byte[MessageSize + 9]; Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1); Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1); Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2); Buffer.BlockCopy(Message, 0, data, 4, MessageSize); return data; } public UInt16 getChecksum() { UInt32 chcksm = 0; byte[] data = getBytes(); int packetsize = MessageSize + 8; int index = 0; while ( index < packetsize) { chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index)); index += 2; } chcksm = (chcksm >> 16) + (chcksm & 0xffff); chcksm += (chcksm >> 16); return (UInt16)(~chcksm); }
}
</source>