]>
Commit | Line | Data |
---|---|---|
223bb49e PL |
1 | .. _scripting-netmasks: |
2 | ||
3 | Netmasks and NetMaskGroups | |
4 | ========================== | |
5 | ||
6 | There are two classes in the PowerDNS Recursor that can be used to match IP addresses against. | |
7 | ||
8 | Netmask class | |
9 | ------------- | |
54c79a88 | 10 | The :class:`Netmask` class represents an IP netmask. |
223bb49e | 11 | |
54c79a88 PL |
12 | .. code-block:: Lua |
13 | ||
14 | mask = newNetmask("192.0.2.1/24") | |
15 | mask:isIPv4() -- true | |
16 | mask:match("192.0.2.8") -- true | |
17 | ||
18 | .. function:: newNetmask(mask) -> Netmask | |
223bb49e | 19 | |
54c79a88 | 20 | Creates a new :class:`Netmask`. |
223bb49e | 21 | |
54c79a88 PL |
22 | :param str mask: The mask to convert. |
23 | ||
24 | .. class:: Netmask | |
223bb49e | 25 | |
54c79a88 | 26 | Represents a netmask. |
223bb49e | 27 | |
54c79a88 | 28 | .. method:: Netmask:empty() -> bool |
223bb49e | 29 | |
54c79a88 | 30 | True if the netmask doesn't contain a valid address. |
223bb49e | 31 | |
54c79a88 | 32 | .. method:: Netmask:getBits() -> int |
223bb49e | 33 | |
54c79a88 | 34 | The number of bits in the address. |
223bb49e | 35 | |
54c79a88 | 36 | .. method:: Netmask:getNetwork() -> ComboAddress |
223bb49e | 37 | |
54c79a88 | 38 | Returns a :class:`ComboAddress` representing the network (no mask applied). |
223bb49e | 39 | |
54c79a88 | 40 | .. method:: Netmask:getMaskedNetwork() -> ComboAddress |
223bb49e | 41 | |
54c79a88 | 42 | Returns a :class:`ComboAddress` representing the network (truncating according to the mask). |
223bb49e | 43 | |
54c79a88 | 44 | .. method:: Netmask:isIpv4() -> bool |
223bb49e | 45 | |
14075d06 AT |
46 | .. deprecated:: v4.3.0 |
47 | ||
48 | True if the netmask is an IPv4 netmask. | |
49 | ||
50 | .. method:: Netmask:isIPv4() -> bool | |
51 | ||
52 | .. versionadded:: v4.3.0 | |
53 | ||
54c79a88 | 54 | True if the netmask is an IPv4 netmask. |
223bb49e | 55 | |
54c79a88 | 56 | .. method:: Netmask:isIpv6() -> bool |
223bb49e | 57 | |
14075d06 AT |
58 | .. deprecated:: v4.3.0 |
59 | ||
60 | True if the netmask is an IPv6 netmask. | |
61 | ||
62 | .. method:: Netmask:isIPv6() -> bool | |
63 | ||
64 | .. deprecated:: v4.3.0 | |
65 | ||
54c79a88 | 66 | True if the netmask is an IPv6 netmask. |
223bb49e | 67 | |
54c79a88 | 68 | .. method:: Netmask:match(address) -> bool |
223bb49e | 69 | |
54c79a88 | 70 | True if the address passed in address matches |
223bb49e | 71 | |
54c79a88 | 72 | :param str address: IP Address to match against. |
223bb49e | 73 | |
54c79a88 | 74 | .. method:: Netmask:toString() -> str |
223bb49e | 75 | |
54c79a88 | 76 | Returns a human-friendly representation. |
223bb49e PL |
77 | |
78 | NetMaskGroup class | |
79 | ------------------ | |
80 | ||
81 | NetMaskGroups are more powerful than plain Netmasks. | |
54c79a88 | 82 | They can be matched against netmasks objects: |
223bb49e | 83 | |
54c79a88 PL |
84 | .. code-block:: lua |
85 | ||
86 | nmg = newNMG() | |
87 | nmg:addMask("127.0.0.0/8") | |
88 | nmg:addMasks({"213.244.168.0/24", "130.161.0.0/16"}) | |
89 | nmg:addMasks(dofile("bad.ips")) -- contains return {"ip1","ip2"..} | |
223bb49e | 90 | |
54c79a88 PL |
91 | if nmg:match(dq.remoteaddr) then |
92 | print("Intercepting query from ", dq.remoteaddr) | |
93 | end | |
223bb49e | 94 | |
54c79a88 | 95 | Prefixing a mask with ``!`` excludes that mask from matching. |
223bb49e | 96 | |
54c79a88 | 97 | .. function:: newNMG() -> NetMaskGroup |
223bb49e | 98 | |
54c79a88 PL |
99 | Returns a new, empty :class:`NetMaskGroup`. |
100 | ||
101 | .. class:: NetMaskGroup | |
223bb49e | 102 | |
54c79a88 | 103 | IP addresses are passed to Lua in native format. |
223bb49e | 104 | |
54c79a88 | 105 | .. method:: NetMaskGroup:addMask(mask) |
223bb49e | 106 | |
54c79a88 | 107 | Adds ``mask`` to the NetMaskGroup. |
223bb49e | 108 | |
54c79a88 | 109 | :param str mask: The mask to add. |
223bb49e | 110 | |
54c79a88 | 111 | .. method:: NetMaskGroup:addMasks(masks) |
223bb49e | 112 | |
54c79a88 | 113 | Adds ``masks`` to the NetMaskGroup. |
223bb49e | 114 | |
54c79a88 | 115 | :param {str} mask: The masks to add. |
223bb49e | 116 | |
54c79a88 | 117 | .. method:: NetMaskGroup:match(address) -> bool |
223bb49e | 118 | |
54c79a88 | 119 | Returns true if ``address`` matches any of the masks in the group. |
223bb49e | 120 | |
ef2ea4bf | 121 | :param ComboAddress address: The IP address to match the netmasks against. |