Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions iptc/ip4tc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,9 +1064,6 @@ def set_src(self, src):
saddr = _a_to_i(socket.inet_pton(socket.AF_INET, addr))
except socket.error:
raise ValueError("invalid address %s" % (addr))
ina = in_addr()
ina.s_addr = ct.c_uint32(saddr)
self.entry.ip.src = ina

if not netm.isdigit():
try:
Expand All @@ -1080,8 +1077,11 @@ def set_src(self, src):
nmask = socket.htonl((2 ** imask - 1) << (32 - imask))
neta = in_addr()
neta.s_addr = ct.c_uint32(nmask)

self.entry.ip.smsk = neta
# Apply subnet mask to IP address
ina = in_addr()
ina.s_addr = ct.c_uint32(saddr & nmask)
self.entry.ip.src = ina

src = property(get_src, set_src)
"""This is the source network address with an optional network mask in
Expand Down Expand Up @@ -1125,9 +1125,6 @@ def set_dst(self, dst):
daddr = _a_to_i(socket.inet_pton(socket.AF_INET, addr))
except socket.error:
raise ValueError("invalid address %s" % (addr))
ina = in_addr()
ina.s_addr = ct.c_uint32(daddr)
self.entry.ip.dst = ina

if not netm.isdigit():
try:
Expand All @@ -1142,6 +1139,10 @@ def set_dst(self, dst):
neta = in_addr()
neta.s_addr = ct.c_uint32(nmask)
self.entry.ip.dmsk = neta
# Apply subnet mask to IP address
ina = in_addr()
ina.s_addr = ct.c_uint32(daddr & nmask)
self.entry.ip.dst = ina

dst = property(get_dst, set_dst)
"""This is the destination network address with an optional network mask
Expand Down
14 changes: 7 additions & 7 deletions tests/test_iptc.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@ def tearDown(self):
def test_rule_address(self):
# valid addresses
rule = iptc.Rule()
for addr in [("127.0.0.1/255.255.255.0", "127.0.0.1/255.255.255.0"),
("!127.0.0.1/255.255.255.0", "!127.0.0.1/255.255.255.0"),
("127.0.0.1/255.255.128.0", "127.0.0.1/255.255.128.0"),
("127.0.0.1/16", "127.0.0.1/255.255.0.0"),
("127.0.0.1/24", "127.0.0.1/255.255.255.0"),
("127.0.0.1/17", "127.0.0.1/255.255.128.0"),
("!127.0.0.1/17", "!127.0.0.1/255.255.128.0")]:
for addr in [("127.0.0.1/255.255.255.0", "127.0.0.0/255.255.255.0"),
("!127.0.0.1/255.255.255.0", "!127.0.0.0/255.255.255.0"),
("127.0.0.1/255.255.128.0", "127.0.0.0/255.255.128.0"),
("127.0.0.1/16", "127.0.0.0/255.255.0.0"),
("127.0.0.1/24", "127.0.0.0/255.255.255.0"),
("127.0.0.1/17", "127.0.0.0/255.255.128.0"),
("!127.0.0.1/17", "!127.0.0.0/255.255.128.0")]:
rule.src = addr[0]
self.assertEquals(rule.src, addr[1])
rule.dst = addr[0]
Expand Down