Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #140 from psychomario/development
Browse files Browse the repository at this point in the history
Merge development for PyPI
  • Loading branch information
psychomario authored Dec 26, 2016
2 parents 11176d2 + 1353c85 commit a85bc35
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ The DHCP server class, __`DHCPD()`__, is constructed with the following __keywor
|__`subnet_mask`__|This specifies the subnet mask that the DHCP server will specify to clients.|`'255.255.255.0'`|_string_|
|__`router`__|This specifies the IP address of the router that the DHCP server will specify to clients.|`'192.168.2.1'`|_string_|
|__`dns_server`__|This specifies the DNS server that the DHCP server will specify to clients. Only one DNS server can be passed.|`'8.8.8.8'` ([Google Public DNS](https://developers.google.com/speed/public-dns/))|_string_|
|__`broadcast`__|This specifies the broadcast address the DHCP will broadcast packets to.|`'<broadcast>'`|_string_|
|__`broadcast`__|This specifies the broadcast address the DHCP will broadcast packets to. It is derived from __`ip`__ and __`subnet_mask`__, unless set explicitly in CLI or in JSON config file. This is necessary in selecting the right NIC for broadcast when you have multiple.|`''`|_string_|
|__`file_server`__|This is the IP address of the file server containing network boot files that the DHCP server will specify to clients.|`'192.168.2.2'`|_string_|
|__`file_name`__|This specifies the file name that the client should look for on the remote server.|`'pxelinux.0'`|_string_|
|__`use_ipxe`__|This indicates whether or not iPXE is being used and adjusts itself accordingly.|`False`|_bool_|
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ The following are arguments that can be passed to `pypxe.server` when running fr
|__`--dhcp-subnet DHCP_SUBNET`__|Specify DHCP subnet mask|`255.255.255.0`|
| __`--dhcp-router DHCP_ROUTER`__|Specify DHCP lease router|`192.168.2.1`|
|__`--dhcp-dns DHCP_DNS`__|Specify DHCP lease DNS server|`8.8.8.8`|
|__`--dhcp-broadcast DHCP_BROADCAST`__|Specify DHCP broadcast address|`'<broadcast>'`|
|__`--dhcp-broadcast DHCP_BROADCAST`__|Specify DHCP broadcast address|`''`|
|__`--dhcp-fileserver-ip DHCP_FILESERVER_IP`__|Specify DHCP file server IP address|`192.168.2.2`|
|__`--dhcp-whitelist`__|Only serve clients specified in the static lease file (`--static-config`)|`False`|

Expand Down
2 changes: 1 addition & 1 deletion example_cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"DHCP_SUBNET" : "255.255.255.0",
"DHCP_ROUTER" : "192.168.2.1",
"DHCP_DNS" : "8.8.8.8",
"DHCP_BROADCAST" : "<broadcast>",
"DHCP_BROADCAST" : "",
"DHCP_FILESERVER" : "192.168.2.2",
"SYSLOG_SERVER" : null,
"SYSLOG_PORT" : 514,
Expand Down
11 changes: 10 additions & 1 deletion pypxe/dhcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ def __init__(self, **server_settings):
self.subnet_mask = server_settings.get('subnet_mask', '255.255.255.0')
self.router = server_settings.get('router', '192.168.2.1')
self.dns_server = server_settings.get('dns_server', '8.8.8.8')
self.broadcast = server_settings.get('broadcast', '<broadcast>')

self.broadcast = server_settings.get('broadcast', '')
if not self.broadcast:
# calculate the broadcast address from ip and subnet_mask
nip = struct.unpack('!I', socket.inet_aton(self.ip))[0]
nmask = struct.unpack('!I', socket.inet_aton(self.subnet_mask))[0]
nbroadcast = (nip & nmask) | ((~ nmask) & 0xffffffff)
derived_broadcast = socket.inet_ntoa(struct.pack('!I', nbroadcast))
self.broadcast = derived_broadcast

self.file_server = server_settings.get('file_server', '192.168.2.2')
self.file_name = server_settings.get('file_name', '')
if not self.file_name:
Expand Down
16 changes: 16 additions & 0 deletions pypxe/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'''

import os.path
import logging

class PathTraversalException(Exception):
pass
Expand Down Expand Up @@ -33,3 +34,18 @@ def normalize_path(base, filename):
if normalized.startswith(os.path.join(abs_path, '')):
return normalized
raise PathTraversalException('Path Traversal detected')

def get_child_logger(logger, name):
'''
Get a descendant of an existing Logger.
This only exists because logger.getChild isn't in Python 2.6.
Args:
logger (Logger): Parent logger to create descendant of
name (str): Name to append to parent's name
Returns:
Logger: new Logger with `name` appended
'''
return logging.getLogger("{0}.{1}".format(logger.name, name))
7 changes: 4 additions & 3 deletions pypxe/nbd/writes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
from .. import helpers

class COW:
def basepages(self, offset, length):
Expand Down Expand Up @@ -86,7 +87,7 @@ def __init__(self, addr, imagefd, logger, seek_lock):
self.addr = addr
self.imagefd = imagefd
self.seek_lock = seek_lock
self.logger = logger.getChild('FS')
self.logger = helpers.get_child_logger(logger, 'FS')
self.logger.info('Copy-On-Write for {addr} in PyPXE_NBD_COW_{addr[0]}_{addr[1]}'.format(addr = addr))

# never want readonly cow, also definately creating file
Expand All @@ -101,7 +102,7 @@ def __init__(self, addr, imagefd, logger, seek_lock):
self.addr = addr
self.imagefd = imagefd
self.seek_lock = seek_lock
self.logger = logger.getChild('FS')
self.logger = helpers.get_child_logger(logger, 'FS')
self.logger.info('Copy-On-Write for {0} in Memory'.format(addr))

# BytesIO looks exactly the same as a file, perfect for in memory disk
Expand All @@ -116,7 +117,7 @@ def __init__(self, addr, imagefd, logger, seek_lock):
self.addr = addr
self.seek_lock = seek_lock
self.imagefd = imagefd
self.logger = logger.getChild('FS')
self.logger = helpers.get_child_logger(logger, 'FS')
self.logger.debug('File for {0}'.format(addr))

def read(self, offset, length):
Expand Down
12 changes: 6 additions & 6 deletions pypxe/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pypxe import dhcp # PyPXE DHCP service
from pypxe import http # PyPXE HTTP service
from pypxe import nbd # PyPXE NBD service

from pypxe import helpers
args = None
# default settings
SETTINGS = {'NETBOOT_DIR':'netboot',
Expand All @@ -28,7 +28,7 @@
'DHCP_SUBNET':'255.255.255.0',
'DHCP_DNS':'8.8.8.8',
'DHCP_ROUTER':'192.168.2.1',
'DHCP_BROADCAST':'<broadcast>',
'DHCP_BROADCAST':'',
'DHCP_FILESERVER':'192.168.2.2',
'SYSLOG_SERVER':None,
'SYSLOG_PORT':514,
Expand Down Expand Up @@ -202,7 +202,7 @@ def main():
if args.USE_TFTP:

# setup TFTP logger
tftp_logger = sys_logger.getChild('TFTP')
tftp_logger = helpers.get_child_logger(sys_logger, 'TFTP')
sys_logger.info('Starting TFTP server...')

# setup the thread
Expand All @@ -216,7 +216,7 @@ def main():
if args.USE_DHCP:

# setup DHCP logger
dhcp_logger = sys_logger.getChild('DHCP')
dhcp_logger = helpers.get_child_logger(sys_logger, 'DHCP')
if args.DHCP_MODE_PROXY:
sys_logger.info('Starting DHCP server in ProxyDHCP mode...')
else:
Expand Down Expand Up @@ -252,7 +252,7 @@ def main():
if args.USE_HTTP:

# setup HTTP logger
http_logger = sys_logger.getChild('HTTP')
http_logger = helpers.get_child_logger(sys_logger, 'HTTP')
sys_logger.info('Starting HTTP server...')

# setup the thread
Expand All @@ -265,7 +265,7 @@ def main():
# configure/start NBD server
if args.NBD_BLOCK_DEVICE:
# setup NBD logger
nbd_logger = sys_logger.getChild('NBD')
nbd_logger = helpers.get_child_logger(sys_logger, 'NBD')
sys_logger.info('Starting NBD server...')
nbd_server = nbd.NBD(
block_device = args.NBD_BLOCK_DEVICE,
Expand Down
2 changes: 1 addition & 1 deletion pypxe/tftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, mainsock, parent):
self.timeout = parent.timeout
self.ip = parent.ip
self.message, self.address = mainsock.recvfrom(1024)
self.logger = parent.logger.getChild('Client.{0}'.format(self.address))
self.logger = helpers.get_child_logger(parent.logger, 'Client.{0}'.format(self.address))
self.netboot_directory = parent.netboot_directory
self.logger.debug('Recieving request...')
self.retries = self.default_retries
Expand Down

0 comments on commit a85bc35

Please sign in to comment.