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
|
/*
* ShapDHCPServer (C) 2010, Cluster
* http://clusterrr.com
* http://code.google.com/p/sharpdhcpserver/
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar
* 14 rue de Plaisance, 75014 Paris, France
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Cluster.SharpDHCPServer;
namespace Cluster.SharpDHCPServer_Sample
{
class Program
{
const string LOCAL_INTERFACE = "0.0.0.0";
static byte nextIP = 10;
static Dictionary<string, IPAddress> leases = new Dictionary<string, IPAddress>();
static void Main(string[] args)
{
var server = new DHCPServer();
server.ServerName = "SharpDHCPServer";
server.OnDataReceived += Request;
Console.WriteLine("Running DHCP server. Press enter to stop it.");
Console.ReadLine();
server.Dispose();
}
static void Request(DHCPRequest dhcpRequest)
{
var type = dhcpRequest.GetMsgType();
var mac = ByteArrayToString(dhcpRequest.GetChaddr());
// IP for client
IPAddress ip;
if (!leases.TryGetValue(mac, out ip))
{
ip = new IPAddress(new byte[] { 10, 0, 0, nextIP++ });
leases[mac] = ip;
}
Console.WriteLine(type.ToString() + " request from " + mac + ", it will be " + ip.ToString());
var options = dhcpRequest.GetAllOptions();
Console.Write("Options:");
foreach (DHCPOption option in options.Keys)
{
Console.WriteLine(option.ToString() + ": " + ByteArrayToString(options[option]));
}
// Lets show some request info
var requestedOptions = dhcpRequest.GetRequestedOptionsList();
if (requestedOptions != null)
{
Console.Write("Requested options:");
foreach (DHCPOption option in requestedOptions) Console.Write(" " + option.ToString());
Console.WriteLine();
}
// Option 82 info
var relayInfoN = dhcpRequest.GetRelayInfo();
if (relayInfoN != null)
{
var relayInfo = (RelayInfo)relayInfoN;
if (relayInfo.AgentCircuitID != null) Console.WriteLine("Relay agent circuit ID: " + ByteArrayToString(relayInfo.AgentCircuitID));
if (relayInfo.AgentRemoteID != null) Console.WriteLine("Relay agent remote ID: " + ByteArrayToString(relayInfo.AgentRemoteID));
}
Console.WriteLine();
var replyOptions = new DHCPReplyOptions();
// Options should be filled with valid data. Only requested options will be sent.
replyOptions.SubnetMask = IPAddress.Parse("255.255.255.0");
replyOptions.DomainName = "SharpDHCPServer";
replyOptions.ServerIdentifier = IPAddress.Parse("10.0.0.1");
replyOptions.RouterIP = IPAddress.Parse("10.0.0.1");
replyOptions.DomainNameServers = new IPAddress[] { IPAddress.Parse("192.168.100.2"), IPAddress.Parse("192.168.100.3") };
// Some static routes
replyOptions.StaticRoutes = new NetworkRoute[] {
new NetworkRoute(IPAddress.Parse("10.0.0.0"), IPAddress.Parse("255.0.0.0"), IPAddress.Parse("10.0.0.1")),
new NetworkRoute(IPAddress.Parse("192.168.0.0"), IPAddress.Parse("255.255.0.0"), IPAddress.Parse("10.0.0.1")),
new NetworkRoute(IPAddress.Parse("172.16.0.0"), IPAddress.Parse("255.240.0.0"), IPAddress.Parse("10.0.0.1")),
new NetworkRoute(IPAddress.Parse("80.252.130.248"), IPAddress.Parse("255.255.255.248"), IPAddress.Parse("10.0.0.1")),
new NetworkRoute(IPAddress.Parse("80.252.128.88"), IPAddress.Parse("255.255.255.248"), IPAddress.Parse("10.0.0.1")),
};
// Lets send reply to client!
if (type == DHCPMsgType.DHCPDISCOVER)
dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPOFFER, ip, replyOptions);
if (type == DHCPMsgType.DHCPREQUEST)
dhcpRequest.SendDHCPReply(DHCPMsgType.DHCPACK, ip, replyOptions);
}
static string ByteArrayToString(byte[] ar)
{
var res = new StringBuilder();
foreach (var b in ar)
{
res.Append(b.ToString("X2"));
}
res.Append(" (");
foreach (var b in ar)
{
if ((b >= 32) && (b <127))
res.Append(Encoding.ASCII.GetString(new byte[] { b }));
else res.Append(" ");
}
res.Append(")");
return res.ToString();
}
}
}
|