1 # vim: set encoding=utf-8 et sw=4 sts=4 :
3 from pymin.seqtools import Sequence
4 from pymin.service.util import ListSubHandler
6 __all__ = ('SNatHandler',)
10 r"""SNat(dev, src[, src_net]) -> SNat instance.
12 dev - Netword device to use.
13 src - Source IP address.
14 src_net - Source network to apply the NAT (as IP/mask).
17 def __init__(self, dev, src, src_net=None):
18 r"Initialize object, see class documentation for details."
22 self.src_net = src_net
24 def update(self, dev=None, src=None, src_net=None):
25 r"update([dev[, ...]]) -> Update the values of a snat (see class doc)."
27 if dev is not None: self.dev = dev
28 if src is not None: self.src = src
29 if src_net is not None: self.src_net = src_net
31 def __cmp__(self, other):
32 r"Compares two SNat objects."
33 return cmp(self.as_tuple(), other.as_tuple())
36 r"Return a tuple representing the snat."
37 return (self.dev, self.src, self.src_net)
39 def as_call_list(self, index=None):
40 cmd = ['-t', 'nat', '-I', 'POSTROUTING']
42 cmd.append(str(index))
43 cmd.extend(('-o', self.dev, '-j', 'SNAT', '--to', self.src))
44 if self.src_net is not None:
45 cmd.extend(('-s', self.src_net))
48 class SNatHandler(ListSubHandler):
49 r"""SNatHandler(parent) -> SNatHandler instance.
51 This class is a helper for NatHandler to do all the work related to
54 parent - The parent service handler.
57 handler_help = u"Manage source NAT."
59 _cont_subhandler_attr = 'snats'
60 _cont_subhandler_class = SNat