[Pymilter] Recognizing dynamic IPs

Stuart D. Gathman stuart at bmsi.com
Wed Nov 24 09:31:59 EST 2004


I have 0.7.2 in production on 2 machines.  Seems pretty stable.  A very 
useful feature of 0.7.2 is recognizing dynamic PTR records.  You know, 
things like "adsl-57.76.154.info.com.ph".

I have a very heuristic function to do this.  Ideas on improving it
are welcome.

# examples we don't yet recognize:
#
# 1Cust65.tnt4.atl4.da.uu.net at ('67.192.40.65', 4588)
# 1Cust141.tnt30.rtm1.nld.da.uu.net at ('213.116.154.141', 2036)
# user64.net2045.mo.sprint-hsd.net at ('67.77.185.64', 3901)
# wiley-268-8196.roadrunner.nf.net at ('205.251.174.46', 4810)
# 221.fib163.satnet.net at ('200.69.163.221', 3301)
# BHE214126.res-com.wayinternet.com.br at ('200.159.214.126', 3528)
# ZR093223.ppp.dion.ne.jp at ('222.14.93.223', 3593)
# cpc2-ches1-4-0-cust8.lutn.cable.ntl.com at ('80.4.105.8', 61099)
# docsis8-179.sby.link.net.id at ('202.137.8.179', 1440)
# user239.res.openband.net at ('65.246.82.239', 1392)
# S010600e0299501e5.rd.shawcable.net at ('24.64.82.212', 1838)
# xdsl-2449.zgora.dialog.net.pl at ('81.168.237.145', 1238)

import re

ip3 = re.compile('([0-9]{1,3})[.-]([0-9]{1,3})[.-]([0-9]{1,3})')
rehmac = re.compile('h[0-9a-f]{12}[.]|pcp[0-9]{6,10}pcs[.]|no-reverse')

def is_dynip(host,addr):
  """Return True if hostname is for a dynamic ip.
  Examples:

  >>> is_dynip('post3.fabulousdealz.com','69.60.99.112')
  False
  >>> is_dynip('adsl-69-208-201-177.dsl.emhril.ameritech.net','69.208.201.177')
  True
  """
  if addr:
    if host.find(addr) >= 0: return True
    a = addr.split('.')
    m = ip3.search(host)
    if m:
      g = list(m.groups())
      if g == a[1:] or g == a[:3]: return True
      g.reverse()
      if g == a[1:] or g == a[:3]: return True
    if rehmac.search(host): return True
    if host.find("-%s." % '-'.join(a[2:])) >= 0: return True
    if host.find("w%s." % '-'.join(a[:2])) >= 0: return True
    if host.find(''.join(a[:3])) >= 0: return True
    if host.find(''.join(a[1:])) >= 0: return True
    x = "%02x%02x%02x%02x" % tuple(map(int,a))
    if host.lower().find(x) >= 0: return True
    z = [n.zfill(3) for n in a]
    if host.find('-'.join(z)) >= 0: return True
    if host.find("-%s." % '-'.join(z[2:])) >= 0: return True
    if host.find("%s." % ''.join(z[2:])) >= 0: return True
    if host.find(''.join(z)) >= 0: return True
  return False

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
    Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.




More information about the Pymilter mailing list