Skip to content

Commit 6904af3

Browse files
committed
Small changes
1 parent 9cb3bb2 commit 6904af3

File tree

4 files changed

+194
-57
lines changed

4 files changed

+194
-57
lines changed

Second Project/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ def connect_to_db():
7272
mydb = mysql.connector.connect(
7373
host='localhost',
7474
user='root',
75-
password='********',
75+
password='aa12345678',
7676
database='Telnet',
77-
port='****'
77+
port='3306'
7878
)
7979
return mydb
8080

Third Project/client.py

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,13 @@
66
import time
77
import sys
88

9-
MAC_ADDRESS = 'fe:0a:be:ef:1b:a0'
10-
CLIENT_PORT = 68
11-
GOT_IP = False
12-
CLIENT_IP = ''
13-
LEASE_TIME = 0
9+
MAC_ADDRESS = 'fe:0a:be:ef:1b:a0'
10+
CLIENT_PORT = 68
11+
GOT_IP = False
12+
CLIENT_IP = ''
13+
LEASE_TIME = 0
1414
MAX_MSG_SIZE = 1024
1515

16-
17-
# Generating next timeout based on last time-out and backoff-cutoff.
18-
# Excluding 0 from random numbers.
19-
def discover_timeout(last_timeout, backoff_cutoff=120):
20-
return min(2 * (1.0-random.random()) * last_timeout, backoff_cutoff) \
21-
if last_timeout != backoff_cutoff else backoff_cutoff
22-
23-
24-
def handle(signum, frame):
25-
raise Exception('Timeout...')
26-
27-
2816
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
2917
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
3018
client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
@@ -45,55 +33,80 @@ def handle(signum, frame):
4533
chaddr=MAC_ADDRESS,
4634
sname=b'',
4735
file=b'',
48-
options=dhcppython.options.OptionList([dhcppython.options.options.short_value_to_object(53, 'DHCPDISCOVER')])
36+
options=dhcppython.options.OptionList([
37+
dhcppython.options.options.short_value_to_object(12, 'iPhone 12'),
38+
dhcppython.options.options.short_value_to_object(53, 'DHCPDISCOVER')
39+
])
4940
)
5041

51-
time_out_interval = 10
52-
signal.signal(signal.SIGALRM, handle)
53-
signal.alarm(time_out_interval)
5442

55-
while True:
56-
client_socket.sendto(discover.asbytes, ('<broadcast>', 67))
43+
# Generating next timeout based on last time-out and backoff-cutoff.
44+
# Excluding 0 from random numbers.
45+
def discover_timeout(last_timeout, backoff_cutoff=120):
46+
return min(2 * random.random() * last_timeout, backoff_cutoff) \
47+
if last_timeout != backoff_cutoff else backoff_cutoff
48+
49+
50+
def alarm(signum, frame):
51+
raise TimeoutError('Timeout...')
52+
53+
54+
timeout_interval = 10
55+
signal.signal(signal.SIGALRM, alarm)
56+
signal.alarm(timeout_interval)
57+
58+
while not GOT_IP:
5759
try:
60+
print('Sent discover message...')
61+
client_socket.sendto(discover.asbytes, ('<broadcast>', 67))
62+
5863
offer, _ = client_socket.recvfrom(MAX_MSG_SIZE)
5964
offer = dhcppython.packet.DHCPPacket.from_bytes(offer)
65+
while offer.options.by_code(53).data != b'\x02' or offer.xid != discover.xid:
66+
offer, _ = client_socket.recvfrom(MAX_MSG_SIZE)
67+
offer = dhcppython.packet.DHCPPacket.from_bytes(offer)
6068

61-
if offer.xid == discover.xid and offer.options.by_code(53) == b'\x01':
62-
request = dhcppython.packet.DHCPPacket(
63-
op=discover.op,
64-
htype=discover.htype,
65-
hlen=6,
66-
hops=0,
67-
xid=discover.xid,
68-
secs=0,
69-
flags=0,
70-
ciaddr=offer.yiaddr,
71-
yiaddr=ipaddress.IPv4Address(0),
72-
siaddr=offer.siaddr,
73-
giaddr=ipaddress.IPv4Address(0),
74-
chaddr=MAC_ADDRESS,
75-
sname=b'',
76-
file=b'',
77-
options=dhcppython.options.OptionList(
78-
[
79-
dhcppython.options.options.short_value_to_object(53, 'DHCPREQUEST'),
80-
dhcppython.options.options.short_value_to_object(50, offer.yiaddr),
81-
dhcppython.options.options.short_value_to_object(54, offer.siaddr)
82-
])
83-
)
84-
client_socket.sendto(request.asbytes, ('<broadcast>', 67))
69+
print('Got an offer...')
70+
request = dhcppython.packet.DHCPPacket(
71+
op=discover.op,
72+
htype=discover.htype,
73+
hlen=6,
74+
hops=0,
75+
xid=discover.xid,
76+
secs=0,
77+
flags=0,
78+
ciaddr=offer.yiaddr,
79+
yiaddr=ipaddress.IPv4Address(0),
80+
siaddr=offer.siaddr,
81+
giaddr=ipaddress.IPv4Address(0),
82+
chaddr=MAC_ADDRESS,
83+
sname=b'',
84+
file=b'',
85+
options=dhcppython.options.OptionList(
86+
[
87+
dhcppython.options.options.short_value_to_object(12, 'iPhone 12'),
88+
dhcppython.options.options.short_value_to_object(53, 'DHCPREQUEST'),
89+
dhcppython.options.options.short_value_to_object(50, offer.yiaddr),
90+
dhcppython.options.options.short_value_to_object(54, offer.siaddr)
91+
])
92+
)
93+
print('Sent request message...')
94+
client_socket.sendto(request.asbytes, ('<broadcast>', 67))
8595

96+
ack, _ = client_socket.recvfrom(MAX_MSG_SIZE)
97+
ack = dhcppython.packet.DHCPPacket.from_bytes(ack)
98+
while ack.options.by_code(53).data != b'\x05' or ack.xid != discover.xid:
8699
ack, _ = client_socket.recvfrom(MAX_MSG_SIZE)
87100
ack = dhcppython.packet.DHCPPacket.from_bytes(ack)
88101

89-
if ack.xid == ack.xid and ack.options.by_code(53) == b'\x05':
90-
GOT_IP = True
91-
CLIENT_IP = ack.yiaddr
92-
LEASE_TIME = ack.options.by_code(51)
102+
print('Got ack from server...')
103+
GOT_IP = True
104+
CLIENT_IP = ack.yiaddr
105+
LEASE_TIME = ack.options.by_code(51).value['lease_time']
93106

94-
print(f'Got ip address {CLIENT_IP} from server with ip address {ack.siaddr} for {LEASE_TIME} secs...')
107+
print(f'Got ip address {CLIENT_IP} from server with ip address {ack.siaddr} for {LEASE_TIME} secs...')
95108

96109
except Exception as e:
97110
print(e)
98-
time_out_interval = discover_timeout(time_out_interval)
99-
signal.alarm(time_out_interval)
111+
timeout_interval = int(discover_timeout(timeout_interval)) + 1
112+
signal.alarm(timeout_interval)

Third Project/server.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import time
2+
import json
3+
import socket
4+
import dhcppython
5+
import ipaddress
6+
import asyncio
7+
8+
with open('server_configuration.json') as json_file:
9+
server_config = json.load(json_file)
10+
11+
SERVER_PORT = 67
12+
SERVER_IP = server_config['server_ip']
13+
MAX_MSG_SIZE = 1024
14+
15+
16+
available_ips = []
17+
if server_config['pool_mode'] == 'range':
18+
start = server_config['range']['from'].split('.')
19+
start = ''.join(['{0:08b}'.format(int(octet)) for octet in start])
20+
21+
end = server_config['range']['to'].split('.')
22+
end = ''.join(['{0:08b}'.format(int(octet)) for octet in end])
23+
24+
for i in range(int(end, 2) - int(start, 2) + 1):
25+
address = '{0:032b}'.format(int(start, 2) + i)
26+
available_ips.append('.'.join([str(int(address[:8], 2)),
27+
str(int(address[8:16], 2)),
28+
str(int(address[16:24], 2)),
29+
str(int(address[24:], 2))]))
30+
elif server_config['pool_mode'] == 'subnet':
31+
ip_block = server_config['subnet']['ip_block'].split('.')
32+
ip_block = ''.join(['{0:08b}'.format(int(octet)) for octet in ip_block])
33+
34+
subnet_mask = server_config['subnet']['subnet_mask'].split('.')
35+
subnet_mask = ''.join(['{0:08b}'.format(int(octet)) for octet in subnet_mask])
36+
hosts = 2 ** (32 - subnet_mask.count('1'))
37+
38+
for i in range(1, hosts-1):
39+
address = '{0:032b}'.format(int(ip_block, 2) + i)
40+
available_ips.append('.'.join([str(int(address[:8], 2)),
41+
str(int(address[8:16], 2)),
42+
str(int(address[16:24], 2)),
43+
str(int(address[24:], 2))]))
44+
45+
lease_time = int(server_config['lease_time'])
46+
blocked_clients = server_config['black_list']
47+
reserved_ips = server_config['reservation_list']
48+
available_ips = [available_ip for available_ip in available_ips if available_ip != SERVER_IP and available_ip not in reserved_ips.values()]
49+
50+
given_ips = dict()
51+
client_states = dict()
52+
53+
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
54+
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
55+
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
56+
server_socket.bind(('', SERVER_PORT))
57+
58+
59+
while True:
60+
data, _ = server_socket.recvfrom(MAX_MSG_SIZE)
61+
data = dhcppython.packet.DHCPPacket.from_bytes(data)
62+
63+
if data.options.by_code(53).data == b'\x01':
64+
if data.chaddr not in blocked_clients:
65+
requesting_ip = ''
66+
if data.chaddr.lower() in reserved_ips:
67+
requesting_ip = reserved_ips[data.chaddr.lower()]
68+
elif available_ips:
69+
requesting_ip = available_ips[0]
70+
offer = dhcppython.packet.DHCPPacket(
71+
op=data.op,
72+
htype=data.htype,
73+
hlen=6,
74+
hops=0,
75+
xid=data.xid,
76+
secs=0,
77+
flags=0,
78+
ciaddr=ipaddress.IPv4Address(0),
79+
yiaddr=ipaddress.IPv4Address(requesting_ip),
80+
siaddr=ipaddress.IPv4Address(SERVER_IP),
81+
giaddr=ipaddress.IPv4Address(0),
82+
chaddr=data.chaddr,
83+
sname=b'',
84+
file=b'',
85+
options=dhcppython.options.OptionList(
86+
[
87+
dhcppython.options.options.short_value_to_object(51, lease_time),
88+
dhcppython.options.options.short_value_to_object(53, 'DHCPOFFER'),
89+
dhcppython.options.options.short_value_to_object(54, SERVER_IP)
90+
])
91+
)
92+
server_socket.sendto(offer.asbytes, ('<broadcast>', 68))
93+
client_states[data.xid] = 'offered'
94+
95+
elif data.xid in client_states and data.options.by_code(53).data == b'\x03' and client_states[data.xid] == 'offered':
96+
97+
ack = dhcppython.packet.DHCPPacket(
98+
op=data.op,
99+
htype=data.htype,
100+
hlen=6,
101+
hops=0,
102+
xid=data.xid,
103+
secs=0,
104+
flags=0,
105+
ciaddr=ipaddress.IPv4Address(0),
106+
yiaddr=data.ciaddr,
107+
siaddr=ipaddress.IPv4Address(SERVER_IP),
108+
giaddr=ipaddress.IPv4Address(0),
109+
chaddr=data.chaddr,
110+
sname=b'',
111+
file=b'',
112+
options=dhcppython.options.OptionList(
113+
[
114+
dhcppython.options.options.short_value_to_object(51, lease_time),
115+
dhcppython.options.options.short_value_to_object(53, 'DHCPACK'),
116+
dhcppython.options.options.short_value_to_object(54, SERVER_IP)
117+
])
118+
)
119+
if ack.yiaddr not in given_ips.values():
120+
given_ips[ack.chaddr] = ack.yiaddr
121+
if ack.chaddr.lower() not in reserved_ips:
122+
available_ips.remove(str(ack.yiaddr))
123+
server_socket.sendto(ack.asbytes, ('<broadcast>', 68))
124+
client_states[data.xid] = 'acked'

Third Project/server_configuration.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"lease_time": 10,
1313
"reservation_list": {
14-
"mac1": "ip1",
14+
"fe:0a:be:ef:1b:a0": "192.168.1.10",
1515
"mac2": "ip2"
1616
},
1717
"black_list": ["mac1", "mac2"]

0 commit comments

Comments
 (0)