Exemplo n.º 1
0
 def __init__(self, **kw):
     """<method internal="yes">
     </method>
     """
     self.bindto = []
     self.bindto.append(DBSockAddr(SockAddrInet("127.0.0.1", 0), ZD_PROTO_TCP))
     self.bindto.append(DBSockAddr(SockAddrInet("127.0.0.1", 0), ZD_PROTO_UDP))
     self.bindto.append(DBSockAddr(SockAddrInet6("::1", 0), ZD_PROTO_TCP))
     self.bindto.append(DBSockAddr(SockAddrInet6("::1", 0), ZD_PROTO_UDP))
     kw['transparent'] = TRUE
     super(RuleDispatcher, self).__init__(Globals.instance_name, self.bindto, **kw)
Exemplo n.º 2
0
    def resolve(self, host, port):
        """
		<method internal="yes">
		</method>
		"""
        try:
            ip_list = gethostbyname_ex(host)[2]
            if self.multi:
                return map(lambda ip: SockAddrInet(ip, port), ip_list)
            else:
                return (SockAddrInet(ip_list[0], port), )
        except IOError:
            return None
Exemplo n.º 3
0
 def performTranslation(self, session, addrs, nat_type):
     """
     <method internal="yes">
     </method>
     """
     try:
         ip = self.ip_hash[addrs[nat_type].ip_s]
         if nat_type == NAT_SNAT:
             return SockAddrInet(ip, 0)
         else:
             return SockAddrInet(ip, addr.port)
     except KeyError:
         if self.default_reject:
             raise DACException, 'IP not within the required range.'
         else:
             return addr
Exemplo n.º 4
0
def create_sockaddr(addrinfo, port):
    """
    <function internal="yes"/>
    """
    addr = addrinfo[4][0]
    family = addrinfo[0]
    if (family == socket.AF_INET):
        return SockAddrInet(addr, port)
    elif (family == socket.AF_INET6):
        return SockAddrInet6(addr, port)
Exemplo n.º 5
0
 def mapAddress(self, addr, from_domain, to_domain, nat_type):
     """
     <method internal="yes">
       <summary>
         Function to map an address to another subnet.
       </summary>
       <description>
         <para>
           This function maps the address 'addr' in the domain
           'from_domain' to another domain 'to_domain'.
           Returns a SockAddrInet in the destination domain or None
         </para>
       </description>
       <metainfo>
         <arguments>
           <argument maturity="stable">
             <name>from_domain</name>
             <type></type>
             <description>source domain</description>
           </argument>
           <argument maturity="stable">
             <name>to_domain</name>
             <type></type>
             <description>destination domain</description>
           </argument>
           <argument maturity="stable">
             <name>nat_type</name>
             <type></type>
             <description>specifies the NAT type</description>
           </argument>
         </arguments>
       </metainfo>
     </method>
     """
     if addr < from_domain:
         ip = (addr.ip & ~to_domain.netmask()) + (to_domain.netaddr()
                                                  & to_domain.netmask())
         if nat_type == NAT_SNAT:
             return SockAddrInet(inet_ntoa(ip), 0)
         elif nat_type == NAT_DNAT:
             return SockAddrInet(inet_ntoa(ip), addr.port)
Exemplo n.º 6
0
    def resolve(self, host, port):
        """
		<method internal="yes">
		</method>
		"""
        try:
            ip_list = self.mapping[host]
            if type(ip_list) == types.StringType:
                ip_list = (ip_list, )
            return map(lambda ip: SockAddrInet(ip, port), ip_list)
        except KeyError:
            return None
Exemplo n.º 7
0
class NAT64(AbstractNAT):
    """
    <class maturity="stable">
      <summary>
       Class that performs translation from IPv6 to IPv4 addresses (NAT64)
      </summary>
      <description>
       NAT64 maps specific bits of the IPv6 address to IPv4 addresses according to the
       NAT64 specification as described in RFC6052
       (http://tools.ietf.org/html/rfc6052#section-2.2).
      </description>
      <metainfo>
        <attributes/>
      </metainfo>
    </class>
    """
    def __init__(self, prefix_mask=96):
        """
        <method maturity="stable">
          <summary>
            Constructor to initialize a NAT64 instance.
          </summary>
          <description>
            <para>
              This constructor initializes a NAT64 instance.
            </para>
          </description>
          <metainfo>
            <arguments>
              <argument maturity="stable">
                <name>prefix_mask</name>
                <type>
                  <integer display_name="Prefix netmask"/>
                </type>
                <default>96</default>
                <description>This parameter specifies the length of the IPv6 address to consider and must
                be one of 32, 40, 48, 56, 64, or 96.</description>
              </argument>
            </arguments>
          </metainfo>
        </method>
        """
        super(NAT64, self).__init__()

        permitted_masks = (32, 40, 48, 56, 64, 96)
        if prefix_mask not in permitted_masks:
            raise UserException, "Prefix mask must be one of: %s; mask='%s'" % (
                permitted_masks, prefix_mask)
        self.prefix_mask = prefix_mask

    def performTranslation(self, session, addrs, nat_type):
        """
        <method internal="yes">
        </method>
        """
        if addrs[nat_type].family != socket.AF_INET6:
            raise UserException, "NAT64 might only be used to translate IPv6 addresses; family='%s'" % addrs[
                nat_type].family

        v6addr = addrs[nat_type].pack()
        translate = {
            96: lambda mask: v6addr[-4:],
            64: lambda mask: v6addr[-7:-3],
            56: lambda mask: v6addr[-9:-8] + v6addr[-7:-4],
            48: lambda mask: v6addr[-10:-8] + v6addr[-7:-5],
            40: lambda mask: v6addr[-11:-8] + v6addr[-7],
            32: lambda mask: v6addr[-12:-8],
        }

        v4addr = translate[self.prefix_mask](self)
        try:
            unwrapped = socket.inet_ntop(socket.AF_INET, v4addr)
        except socket.error, e:
            log(None, CORE_ERROR, 2,
                "Unable to perform NAT mapping; error='%s'", (e, ))
            return None

        return SockAddrInet(unwrapped, addrs[nat_type].port)