Skip to content

Commit

Permalink
FIX: #10 - Basic DHCP logging message are at info level now
Browse files Browse the repository at this point in the history
FIX: #16 - DHCP router and DNS are optional now
  • Loading branch information
Pietro Bertera committed Jun 23, 2015
1 parent 9769f3c commit 13e77cf
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions pypxe/dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ def __init__(self, **serverSettings):
raise Exception('DHCP pool start undefined')
if self.offerto == '':
raise Exception('DHCP pool end undefined')
if self.router == '':
raise Exception('DHCP router undefined')
if self.dnsserver == '':
raise Exception('DHCP DNS server undefined')
if self.broadcast == '':
raise Exception('DHCP broadcast undefined')

Expand All @@ -66,6 +62,11 @@ def __init__(self, **serverSettings):
if self.mode_debug:
self.logger.setLevel(logging.DEBUG)

if self.router == '':
self.logger.warning('DHCP router undefined: DHCP opt.3 will not be sent')
if self.dnsserver == '':
self.logger.warning('DHCP DNS server undefined: DHCP opt.6 will not be sent')

self.logger.info("NOTICE: DHCP server starting on %s:%d" % (self.ip, self.port))
self.logger.debug('DHCP server is using the following:')
self.logger.debug(' DHCP Server IP: {}'.format(self.ip))
Expand All @@ -86,6 +87,7 @@ def __init__(self, **serverSettings):

if os.path.isfile(self.leases_file):
try:
self.logger.info("Reading leases file: %s" % self.leases_file)
self.leases = pickle.load(open(self.leases_file, 'r'))
except Exception, e:
self.logger.error("Cannot load the leases file: %s" % self.leases_file)
Expand All @@ -94,7 +96,7 @@ def __init__(self, **serverSettings):
#key is mac
#self.leases = defaultdict(lambda: {'ip': '', 'expire': 0})
self.leases = defaultdict(default_lease)

def nextIP(self):
'''
This method returns the next unleased IP from range;
Expand Down Expand Up @@ -169,12 +171,15 @@ def craftHeader(self, message):
response += struct.pack('!HHI', 0, 0, 0) #secs, flags, ciaddr
if self.leases[clientmac]['ip']: #OFFER
offer = self.leases[clientmac]['ip']
self.logger.info('DHCP Assignment from leases file - MAC: {MAC} -> IP: {IP}'.format(MAC = self.printMAC(clientmac), IP = self.leases[clientmac]['ip']))
else: #ACK
offer = self.nextIP()
self.leases[clientmac]['ip'] = offer
self.leases[clientmac]['expire'] = time() + 86400
self.logger.info("Writing to the leases file: %s" % self.leases_file)
pickle.dump(self.leases, open(self.leases_file, "wb"))
self.logger.debug('New DHCP Assignment - MAC: {MAC} -> IP: {IP}'.format(MAC = self.printMAC(clientmac), IP = self.leases[clientmac]['ip']))
self.logger.info('New DHCP Assignment - MAC: {MAC} -> IP: {IP}'.format(MAC = self.printMAC(clientmac), IP = self.leases[clientmac]['ip']))

response += socket.inet_aton(offer) #yiaddr
response += socket.inet_aton(self.ip) #siaddr
response += socket.inet_aton('0.0.0.0') #giaddr
Expand All @@ -196,17 +201,22 @@ def craftOptions(self, opt53, clientmac):
response = self.tlvEncode(53, chr(opt53)) #message type, offer
response += self.tlvEncode(54, socket.inet_aton(self.ip)) #DHCP Server
response += self.tlvEncode(1, socket.inet_aton(self.subnetmask)) #SubnetMask
response += self.tlvEncode(3, socket.inet_aton(self.router)) #Router
response += self.tlvEncode(6, socket.inet_aton(self.dnsserver)) #DNS
if self.router != '':
response += self.tlvEncode(3, socket.inet_aton(self.router)) #Router
if self.dnsserver != '':
response += self.tlvEncode(6, socket.inet_aton(self.dnsserver)) #DNS
response += self.tlvEncode(51, struct.pack('!I', 86400)) #lease time

if self.fileserver != '':
#TFTP Server OR HTTP Server; if iPXE, need both
response += self.tlvEncode(66, self.fileserver)
self.logger.info("Encoded option 66: %s" % self.fileserver)

#filename null terminated
if self.filename != '':
response += self.tlvEncode(67, self.filename + chr(0))
self.logger.info("Encoded option 67: %s" % self.filename)

response += '\xff'
return response

Expand Down

0 comments on commit 13e77cf

Please sign in to comment.