Python module to check if a given IP (IPv4 or IPv6) string exists in a given network. Implementation in C.

CYBERDEViL d34481cf36 Don't allocate stuff on the heap when not needed. 2 years ago
src d34481cf36 Don't allocate stuff on the heap when not needed. 2 years ago
.editorconfig db3deff1fc Initial 3 years ago
.gitignore 5f38ca0499 Initial commit 3 years ago
LICENSE 5f38ca0499 Initial commit 3 years ago
README.md db3deff1fc Initial 3 years ago
setup.py 6e1094d0ce The initial code wasn't accurate at all, this should fix it. 3 years ago
test.py 6e1094d0ce The initial code wasn't accurate at all, this should fix it. 3 years ago

README.md

ipinnet

Python module to check if a given IP (IPv4 or IPv6) string exists in a given network. Implementation in C.

Methods

The ipinnet module exposes 2 methods; ipv4_in_net and ipv6_in_net.

ipv4_in_net(ip_address, network_address, netmask)

  @param ip_address: A valid IPv4 address (e.g. 192.168.1.10)
  @type  ip_address: str

  @param network_address: A valid IPv4 network address (e.g. 192.168.1.0)
  @type  network_address: str

  @param netmask: Netmask, a number between 1 and 32. (DO NOT GO OUT THIS
                  RANGE!)
  @type  netmask: uint8_t


ipv6_in_net(ip_address, network_address, netmask)

  @param ip_address: A valid IPv6 address (e.g.
                     2001:0db8:85a3:0000:0000:8a2e:0370:7334)
  @type  ip_address: str

  @param network_address: A valid IPv6 network address (e.g. 2001:0db8::)
  @type  network_address: str

  @param netmask: Netmask, a number between 1 and 128. (DO NOT GO OUT
                  THIS RANGE!)
  @type  netmask: uint8_t

Example

import ipinnet

answers = {
    ipinnet.MATCH: "Yes",
    ipinnet.NO_MATCH: "No",
    ipinnet.INVALID_IP: "Invalid IP",
    ipinnet.INVALID_NET: "Invalid Net"
}

result = ipinnet.ipv4_in_net("192.168.10.1", "192.168.10.0", 24)
print(answers[result])

result = ipinnet.ipv4_in_net("192.168.10.1", "192.168.11.0", 24)
print(answers[result])

result = ipinnet.ipv6_in_net("2001:0db8:85a3:0000:0000:8a2e:0370:7334",
                             "2001:0db8::", 32)
print(answers[result])

result = ipinnet.ipv6_in_net("2001:0db8:85a3:0000:0000:8a2e:0370:7334",
                             "2001:0db8::", 33)
print(answers[result])

Output:

Yes
No
Yes
No