Skip to content

Commit

Permalink
Multiple changes and refactoring.
Browse files Browse the repository at this point in the history
* Python 3 compatible
* Add tests
* Improve docstrings
* Update documentation and diagrams
  • Loading branch information
juga0 committed Jun 26, 2017
1 parent 53346d2 commit a2dc108
Show file tree
Hide file tree
Showing 38 changed files with 1,793 additions and 9,522 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ docs/build
docs/source/images/*.pdf
.cache
.tox
.coverage
.coverage*
htmlcov

Empty file added CHANGELOG.rst
Empty file.
45 changes: 0 additions & 45 deletions INSTALL.md

This file was deleted.

26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,16 @@ From [Wikipedia](https://en.wikipedia.org/wiki/DHCP):
> network needs to be statically (ie., manually) assigned to an
> IP address.
Current status
Documentation
--------------

WIP, still not recommended for end users. Testers welcomed.

See [TODO](./TODO.md)
A more extensive online documentation is available in [Read the docs](https://dhcpcanon.readthedocs.io/).
The documentation source is in [this repository](docs/source/)

Installation
------------

See [Installation](INSTALL.md)
See [Installation](docs/source/install.rst)

Download
--------
Expand All @@ -86,6 +85,13 @@ If you wish to signal a bug or report a feature request, please fill-in
an issue on the [dhcpcanon issue
tracker](https://github.com/juga0/dhcpcanon/issues).

Current status
--------------

WIP, still not recommended for end users. Testers welcomed.

See [TODO](./docs/source/todo.rst)

License
-------

Expand All @@ -95,6 +101,10 @@ licensed by the terms of the MIT license.
Acknowledgments
---------------

To the persons that push me to implement yet another DHCP client,
the person that told me about the anonymity profile RFC, the authors of the RFC
and all the ones that give ideas and suggestions on how to improve.
To all the persons that have given suggestions and comments about this
implementation, the authors of the
[RFC 7844](https://tools.ietf.org/html/rfc7844)),
the [Prototype Fund Project] (https://prototypefund.de) of the
the [Open Knowledge Foundation Germany](https://okfn.de/) and the
[Federal Ministry of Education and Research](https://www.bmbf.de/)
for funding partially this project.
17 changes: 0 additions & 17 deletions TODO.md

This file was deleted.

8 changes: 5 additions & 3 deletions dhcpcanon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:expandtab 2
# Copyright 2016, 2017 juga (juga at riseup dot net), MIT license.
"""."""
"""__init__ for the DHCP client implementation of the Anonymity Profile
([:rfc:`7844`])."""
from __future__ import absolute_import
# from . import (clientscript, conflog, constants, dhcpcap, dhcpcapfsm,
# dhcpcaplease, dhcpcaputils, timers)
try:
from dhcpcanon._version import version
from ._version import version
except ImportError:
try:
from setuptools_scm import get_version
version = get_version()
except (ImportError, LookupError):
version = '0.2.0'
version = '0.3.0'

__version__ = version
__author__ = "juga"
Expand Down
2 changes: 1 addition & 1 deletion dhcpcanon/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# vim:ts=4:sw=4:expandtab 2
# Copyright 2016, 2017 juga (juga at riseup dot net), MIT license.

version = "0.2.1"
version = "0.3.0"
39 changes: 26 additions & 13 deletions dhcpcanon/clientscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,69 @@
# vim:ts=4:sw=4:expandtab 2
# Copyright 2016, 2017 juga (juga at riseup dot net), MIT license.
"""Class to Initialize and call external script."""
from __future__ import unicode_literals
from __future__ import absolute_import, unicode_literals

import logging
import os
import subprocess

import attr
from dhcpcanon.constants import (LEASEATTRS2ENVKEYS,
LEASEATTRS_SAMEAS_ENVKEYS, SCRIPT_ENV_KEYS,
STATES2REASONS)

from .constants import (LEASEATTRS2ENVKEYS, LEASEATTRS_SAMEAS_ENVKEYS,
SCRIPT_ENV_KEYS, STATES2REASONS)

logger = logging.getLogger('dhcpcanon')


@attr.s
class ClientScript(object):
"""Simulates the behaviour of isc-dhcp client-script or nm-dhcp-helper."""
"""Simulates the behaviour of the isc-dhcp client-script or nm-dhcp-helper.
`client-script
<https://anonscm.debian.org/cgit/pkg-dhcp/isc-dhcp.git/tree/client/scripts/linux>`_
or `nm-dhcp-helper
<https://github.com/NetworkManager/NetworkManager/tree/master/src/dhcp>`_.
"""

scriptname = attr.ib(default=None)
env = attr.ib(default=attr.Factory(dict))

def __attrs_post_init__(self):
def __attrs_post_init__(self, env=None):
"""."""
self.env = dict.fromkeys(SCRIPT_ENV_KEYS, '')
logger.debug('Modifying ClientScript obj after creating it.')
if env is None:
self.env = dict.fromkeys(SCRIPT_ENV_KEYS, str(''))
else:
self.env = env

def script_init(self, lease, state, prefix='', medium=''):
"""Initialize environment to pass to the external script."""
if self.scriptname is not None:
logger.debug('Modifying ClientScript obj, setting env.')
if isinstance(state, int):
reason = STATES2REASONS[state]
else:
reason = state

self.env['medium'] = medium
self.env['client'] = 'dhcpcanon'
self.env['reason'] = str(reason)
self.env['medium'] = str(medium)
self.env['client'] = str('dhcpcanon')
self.env['pid'] = str(os.getpid())
self.env['reason'] = reason

for k in LEASEATTRS_SAMEAS_ENVKEYS:
self.env[k] = lease.__getattribute__(k)
self.env[k] = str(lease.__getattribute__(k))

for k, v in LEASEATTRS2ENVKEYS.items():
self.env[k] = str(lease.__getattribute__(v))
else:
logger.debug('There is not script path.')

def script_go(self, scriptname=None, env=None):
"""Run the external script."""
scriptname = self.scriptname or scriptname
if scriptname is not None:
env = self.env or env
logger.debug('calling script %s with env %s', scriptname, env)
logger.debug('Calling script %s', scriptname)
sp = subprocess.Popen([scriptname], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env,
close_fds=True, shell=False)
Expand Down
9 changes: 7 additions & 2 deletions dhcpcanon/conflog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:expandtab
# Copyright 2016, 2017 juga (juga at riseup dot net), MIT license.
"""."""
"""Logging configuration."""
import logging
import sys

Expand Down Expand Up @@ -49,9 +49,14 @@
'level': logging.INFO,
'propagate': False
},
"scapy": {
'handlers': ['stdoutscapy'],
'level': logging.DEBUG,
'propagate': False
},
"scapy.interactive": {
'handlers': ['stdoutscapy'],
'level': logging.INFO,
'level': logging.DEBUG,
'propagate': False
}
}
Expand Down
17 changes: 6 additions & 11 deletions dhcpcanon/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:expandtab 2
# Copyright 2016, 2017 juga (juga at riseup dot net), MIT license.
"""."""
"""Constants for the DHCP client implementation of the Anonymity Profile
([:rfc:`7844`])."""
import logging

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -45,16 +46,6 @@
'router', 'domain', 'name_server', 'lease_time', 'renewal_time',
'rebinding_time']

# NOTE: 3.6. The choice of option numbers and the specific ordering of option
# numbers in the PRL can be used to fingerprint the client
# SHOULD only request a
# minimal number of options in the PRL and SHOULD also randomly shuffle
# the ordering of option codes in the PRL
# PARAM_REQ_LIST = '\x01\x03\x06\x0fw\xfc'# \x1c3
PARAM_REQ_LIST = '\x01\x1c\x02\x03\x0f\x06\x77\x0c\x2c\x2f\x1a\x79\x2a'
XID_MIN = 1
XID_MAX = 900000000

# DHCP FSM
#############
STATE_ERROR = -1
Expand Down Expand Up @@ -93,6 +84,7 @@
STATES2REASONS = {
STATE_PREINIT: 'PREINIT',
STATE_INIT: 'INIT',
STATE_SELECTING: 'SELECTING',
STATE_BOUND: 'BOUND',
STATE_END: 'END',
STATE_REBINDING: 'REBIND',
Expand Down Expand Up @@ -135,3 +127,6 @@
'expire': 'expiry',
'dhcp_server_identifier': 'server_id',
}

FSM_ATTRS = ['request_attempts', 'discover_attempts', 'script',
'time_sent_request', 'current_state', 'client']

0 comments on commit a2dc108

Please sign in to comment.