summaryrefslogtreecommitdiff
path: root/tftpy/TftpShared.py
blob: 5603ba6543f1681e55885489ee1a07276119b6b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# vim: ts=4 sw=4 et ai:
"""This module holds all objects shared by all other modules in tftpy."""


MIN_BLKSIZE = 8
DEF_BLKSIZE = 512
MAX_BLKSIZE = 65536
SOCK_TIMEOUT = 5
MAX_DUPS = 20
DEF_TIMEOUT_RETRIES = 3
DEF_TFTP_PORT = 69

# A hook for deliberately introducing delay in testing.
DELAY_BLOCK = 0
# A hook to simulate a bad network
NETWORK_UNRELIABILITY = 0
# 0 is disabled, anything positive is the inverse of the percentage of
# dropped traffic. For example, 1000 would cause 0.1% of DAT packets to
# be skipped to simulate lost packets.


def tftpassert(condition, msg):
    """This function is a simple utility that will check the condition
    passed for a false state. If it finds one, it throws a TftpException
    with the message passed. This just makes the code throughout cleaner
    by refactoring."""
    if not condition:
        raise TftpException(msg)


class TftpErrors:
    """This class is a convenience for defining the common tftp error codes,
    and making them more readable in the code."""

    NotDefined = 0
    FileNotFound = 1
    AccessViolation = 2
    DiskFull = 3
    IllegalTftpOp = 4
    UnknownTID = 5
    FileAlreadyExists = 6
    NoSuchUser = 7
    FailedNegotiation = 8


class TftpException(Exception):
    """This class is the parent class of all exceptions regarding the handling
    of the TFTP protocol."""

    pass


class TftpTimeout(TftpException):
    """This class represents a timeout error waiting for a response from the
    other end."""

    pass


class TftpTimeoutExpectACK(TftpTimeout):
    """This class represents a timeout error when waiting for ACK of the current block
    and receiving duplicate ACK for previous block from the other end."""

    pass


class TftpFileNotFoundError(TftpException):
    """This class represents an error condition where we received a file
    not found error."""

    pass