mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-08 03:59:23 +01:00
Add utils.net.isIPV4, with utils.net.isIP checking v4 or v6
Signed-off-by: James Vega <jamessan@users.sourceforge.net>
This commit is contained in:
parent
7c14992fe8
commit
36eb3501cf
@ -1,6 +1,6 @@
|
|||||||
###
|
###
|
||||||
# Copyright (c) 2002-2005, Jeremiah Fincher
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
||||||
# Copyright (c) 2008-2009, James Vega
|
# Copyright (c) 2008-2009,2011, James Vega
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -1038,7 +1038,7 @@ registerGlobalValue(supybot, 'defaultIgnore',
|
|||||||
class IP(registry.String):
|
class IP(registry.String):
|
||||||
"""Value must be a valid IP."""
|
"""Value must be a valid IP."""
|
||||||
def setValue(self, v):
|
def setValue(self, v):
|
||||||
if v and not (utils.net.isIP(v) or utils.net.isIPV6(v)):
|
if v and not utils.net.isIP(v):
|
||||||
self.error()
|
self.error()
|
||||||
else:
|
else:
|
||||||
registry.String.setValue(self, v)
|
registry.String.setValue(self, v)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
###
|
###
|
||||||
# Copyright (c) 2002-2005, Jeremiah Fincher
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
||||||
# Copyright (c) 2009, James Vega
|
# Copyright (c) 2011, James Vega
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -187,7 +187,7 @@ def banmask(hostmask):
|
|||||||
"""
|
"""
|
||||||
assert isUserHostmask(hostmask)
|
assert isUserHostmask(hostmask)
|
||||||
host = hostFromHostmask(hostmask)
|
host = hostFromHostmask(hostmask)
|
||||||
if utils.net.isIP(host):
|
if utils.net.isIPV4(host):
|
||||||
L = host.split('.')
|
L = host.split('.')
|
||||||
L[-1] = '*'
|
L[-1] = '*'
|
||||||
return '*!*@' + '.'.join(L)
|
return '*!*@' + '.'.join(L)
|
||||||
@ -458,9 +458,9 @@ def replyTo(msg):
|
|||||||
return msg.nick
|
return msg.nick
|
||||||
|
|
||||||
def dccIP(ip):
|
def dccIP(ip):
|
||||||
"""Converts an IP string to the DCC integer form."""
|
"""Returns an IP in the proper form for DCC."""
|
||||||
assert utils.net.isIP(ip), \
|
assert utils.net.isIPV4(ip), \
|
||||||
'argument must be a string ip in xxx.xxx.xxx.xxx format.'
|
'argument must be a string ip in xxx.yyy.zzz.www format.'
|
||||||
i = 0
|
i = 0
|
||||||
x = 256**3
|
x = 256**3
|
||||||
for quad in ip.split('.'):
|
for quad in ip.split('.'):
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
###
|
###
|
||||||
# Copyright (c) 2002-2005, Jeremiah Fincher
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
||||||
|
# Copyright (c) 2011, James Vega
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -44,7 +45,7 @@ def getSocket(host):
|
|||||||
"""
|
"""
|
||||||
addrinfo = socket.getaddrinfo(host, None)
|
addrinfo = socket.getaddrinfo(host, None)
|
||||||
host = addrinfo[0][4][0]
|
host = addrinfo[0][4][0]
|
||||||
if isIP(host):
|
if isIPV4(host):
|
||||||
return socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
return socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
elif isIPV6(host):
|
elif isIPV6(host):
|
||||||
return socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
return socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
|
||||||
@ -52,16 +53,27 @@ def getSocket(host):
|
|||||||
raise socket.error, 'Something wonky happened.'
|
raise socket.error, 'Something wonky happened.'
|
||||||
|
|
||||||
def isIP(s):
|
def isIP(s):
|
||||||
"""Returns whether or not a given string is an IPV4 address.
|
"""Returns whether or not a given string is an IP address.
|
||||||
|
|
||||||
>>> isIP('255.255.255.255')
|
>>> isIP('255.255.255.255')
|
||||||
1
|
1
|
||||||
|
|
||||||
>>> isIP('abc.abc.abc.abc')
|
>>> isIP('::1')
|
||||||
|
0
|
||||||
|
"""
|
||||||
|
return isIPV4(s) or isIPV6(s)
|
||||||
|
|
||||||
|
def isIPV4(s):
|
||||||
|
"""Returns whether or not a given string is an IPV4 address.
|
||||||
|
|
||||||
|
>>> isIPV4('255.255.255.255')
|
||||||
|
1
|
||||||
|
|
||||||
|
>>> isIPV4('abc.abc.abc.abc')
|
||||||
0
|
0
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return bool(socket.inet_aton(s))
|
return bool(socket.inet_pton(socket.AF_INET, s))
|
||||||
except socket.error:
|
except socket.error:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
###
|
###
|
||||||
# Copyright (c) 2002-2005, Jeremiah Fincher
|
# Copyright (c) 2002-2005, Jeremiah Fincher
|
||||||
# Copyright (c) 2009, James Vega
|
# Copyright (c) 2009,2011, James Vega
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@ -501,11 +501,9 @@ class NetTest(SupyTestCase):
|
|||||||
isIP = utils.net.isIP
|
isIP = utils.net.isIP
|
||||||
self.failIf(isIP('a.b.c'))
|
self.failIf(isIP('a.b.c'))
|
||||||
self.failIf(isIP('256.0.0.0'))
|
self.failIf(isIP('256.0.0.0'))
|
||||||
self.failUnless(isIP('127.1'))
|
|
||||||
self.failUnless(isIP('0.0.0.0'))
|
self.failUnless(isIP('0.0.0.0'))
|
||||||
self.failUnless(isIP('100.100.100.100'))
|
self.failUnless(isIP('100.100.100.100'))
|
||||||
# This test is too flaky to bother with.
|
self.failUnless(isIP('255.255.255.255'))
|
||||||
# self.failUnless(utils.isIP('255.255.255.255'))
|
|
||||||
|
|
||||||
def testIsIPV6(self):
|
def testIsIPV6(self):
|
||||||
f = utils.net.isIPV6
|
f = utils.net.isIPV6
|
||||||
|
Loading…
Reference in New Issue
Block a user