Example #1
0
    def _from_string(self, a):
        """Initalize a new instance from a string."""
        addr_re = "(?P<neg>!)?\s*"                      + \
                  "(?P<address>(?P<ipv4>[0-9.]+)|"      + \
                              "(?P<ipv6>[0-9a-f:]+))"   + \
                              "(?:/(?P<mask>\d+))?\s*"

        m = re.compile(addr_re).match(a)
        if not m:
            raise ValueError("Could not parse address: '{}'".format(a))

        self.neg = bool(m.group("neg"))

        if m.group("ipv4"):
            self.af = AF_INET
            self.addr = m.group("ipv4")
        elif m.group("ipv6"):
            self.af = AF_INET6
            self.addr = m.group("ipv6")

        net = m.group("mask") or {AF_INET: 32, AF_INET6: 128}[self.af]
        self.mask = ctonm(int(net), self.af)

        self.fback = 0
        self.ifname = ""               # ?
        self.type   = PFRKE_PLAIN      # ?
        self.states = 0
        self.weight = 0
Example #2
0
    def _from_string(self, a):
        """Initalize a new instance from a string."""
        ipv4_re = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"
        ipv6_re = "[0-9a-f:]+"
        addr_re = "(?P<neg>!)?\s*"                            + \
                  "(?P<address>(?P<ipv4>{})|".format(ipv4_re) + \
                              "(?P<ipv6>{}))".format(ipv6_re) + \
                              "(?:/(?P<mask>\d+))?\s*"

        m = re.compile(addr_re).match(a)
        if not m:
            raise ValueError("Could not parse address: '{}'".format(a))

        self.neg = bool(m.group("neg"))

        if m.group("ipv4"):
            self.af = AF_INET
            self.addr = m.group("ipv4")
        elif m.group("ipv6"):
            self.af = AF_INET6
            self.addr = m.group("ipv6")

        net = m.group("mask") or {AF_INET: 32, AF_INET6: 128}[self.af]
        self.mask = ctonm(int(net), self.af)

        self.fback = 0
        self.ifname = ""  # ?
        self.type = PFRKE_PLAIN  # ?
        self.states = 0
        self.weight = 0
Example #3
0
    def _from_struct(self, a):
        """Initialize class attributes from a pfr_addr structure"""
        l = {AF_INET: 4, AF_INET6: 16}[a.pfra_af]

        self.af     = a.pfra_af
        self.addr   = inet_ntop(self.af, string_at(addressof(a.pfra_u), l))
        self.mask   = ctonm(a.pfra_net, self.af)
        self.neg    = bool(a.pfra_not)
        self.fback  = a.pfra_fback
        self.ifname = a.pfra_ifname    # ?
        self.type   = a.pfra_type      # ?
Example #4
0
    def _from_struct(self, a):
        """Initialize class attributes from a pfr_addr structure."""
        l = {AF_INET: 4, AF_INET6: 16}[a.pfra_af]

        self.af = a.pfra_af
        self.addr = inet_ntop(self.af, string_at(addressof(a.pfra_u), l))
        self.mask = ctonm(a.pfra_net, self.af)
        self.neg = bool(a.pfra_not)
        self.fback = a.pfra_fback
        self.ifname = a.pfra_ifname
        self.type = a.pfra_type
        self.states = a.pfra_states
        self.weight = a.pfra_weight