Revision 7df2c4f0

b/lib/netutils.py
362 362
      return False
363 363

  
364 364
  @classmethod
365
  def ValidateNetmask(cls, netmask):
366
    """Validate a netmask suffix in CIDR notation.
367

  
368
    @type netmask: int
369
    @param netmask: netmask suffix to validate
370
    @rtype: bool
371
    @return: True if valid, False otherwise
372

  
373
    """
374
    assert (isinstance(netmask, (int, long)))
375

  
376
    return 0 < netmask <= cls.iplen
377

  
378
  @classmethod
365 379
  def Own(cls, address):
366 380
    """Check if the current host has the the given IP address.
367 381

  
......
487 501

  
488 502
    raise errors.ProgrammerError("%s is not a valid IP version" % version)
489 503

  
504
  @staticmethod
505
  def GetClassFromIpVersion(version):
506
    """Return the IPAddress subclass for the given IP version.
507

  
508
    @type version: int
509
    @param version: IP version, one of L{constants.IP4_VERSION} or
510
                    L{constants.IP6_VERSION}
511
    @return: a subclass of L{netutils.IPAddress}
512
    @raise errors.ProgrammerError: for unknowo IP versions
513

  
514
    """
515
    if version == constants.IP4_VERSION:
516
      return IP4Address
517
    elif version == constants.IP6_VERSION:
518
      return IP6Address
519

  
520
    raise errors.ProgrammerError("%s is not a valid IP version" % version)
521

  
522
  @staticmethod
523
  def GetClassFromIpFamily(family):
524
    """Return the IPAddress subclass for the given IP family.
525

  
526
    @param family: IP family (one of C{socket.AF_INET} or C{socket.AF_INET6}
527
    @return: a subclass of L{netutils.IPAddress}
528
    @raise errors.ProgrammerError: for unknowo IP versions
529

  
530
    """
531
    return IPAddress.GetClassFromIpVersion(
532
              IPAddress.GetVersionFromAddressFamily(family))
533

  
490 534
  @classmethod
491 535
  def IsLoopback(cls, address):
492 536
    """Determine whether it is a loopback address.
b/test/ganeti.netutils_unittest.py
161 161
    self.assertEqual(fn("2001:db8::1"), socket.AF_INET6)
162 162
    self.assertRaises(errors.IPAddressError, fn, "0")
163 163

  
164
  def testValidateNetmask(self):
165
    for netmask in [0, 33]:
166
      self.assertFalse(netutils.IP4Address.ValidateNetmask(netmask))
167

  
168
    for netmask in [1, 32]:
169
      self.assertTrue(netutils.IP4Address.ValidateNetmask(netmask))
170

  
171
    for netmask in [0, 129]:
172
      self.assertFalse(netutils.IP6Address.ValidateNetmask(netmask))
173

  
174
    for netmask in [1, 128]:
175
      self.assertTrue(netutils.IP6Address.ValidateNetmask(netmask))
176

  
177
  def testGetClassFromX(self):
178
    self.assert_(
179
        netutils.IPAddress.GetClassFromIpVersion(constants.IP4_VERSION) ==
180
        netutils.IP4Address)
181
    self.assert_(
182
        netutils.IPAddress.GetClassFromIpVersion(constants.IP6_VERSION) ==
183
        netutils.IP6Address)
184
    self.assert_(
185
        netutils.IPAddress.GetClassFromIpFamily(socket.AF_INET) ==
186
        netutils.IP4Address)
187
    self.assert_(
188
        netutils.IPAddress.GetClassFromIpFamily(socket.AF_INET6) ==
189
        netutils.IP6Address)
190

  
164 191
  def testOwnLoopback(self):
165 192
    # FIXME: In a pure IPv6 environment this is no longer true
166 193
    self.assert_(netutils.IPAddress.Own("127.0.0.1"),

Also available in: Unified diff