6
6
import time
7
7
import sys
8
8
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
14
14
MAX_MSG_SIZE = 1024
15
15
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
-
28
16
client_socket = socket .socket (socket .AF_INET , socket .SOCK_DGRAM , socket .IPPROTO_UDP )
29
17
client_socket .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEPORT , 1 )
30
18
client_socket .setsockopt (socket .SOL_SOCKET , socket .SO_BROADCAST , 1 )
@@ -45,55 +33,80 @@ def handle(signum, frame):
45
33
chaddr = MAC_ADDRESS ,
46
34
sname = b'' ,
47
35
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
+ ])
49
40
)
50
41
51
- time_out_interval = 10
52
- signal .signal (signal .SIGALRM , handle )
53
- signal .alarm (time_out_interval )
54
42
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 :
57
59
try :
60
+ print ('Sent discover message...' )
61
+ client_socket .sendto (discover .asbytes , ('<broadcast>' , 67 ))
62
+
58
63
offer , _ = client_socket .recvfrom (MAX_MSG_SIZE )
59
64
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 )
60
68
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 ))
85
95
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 :
86
99
ack , _ = client_socket .recvfrom (MAX_MSG_SIZE )
87
100
ack = dhcppython .packet .DHCPPacket .from_bytes (ack )
88
101
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' ]
93
106
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...' )
95
108
96
109
except Exception as e :
97
110
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 )
0 commit comments