PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/models/IpAddress.py

https://github.com/moloch--/RootTheBox
Python | 105 lines | 97 code | 0 blank | 8 comment | 0 complexity | 42ae2bf36f8fb25543339f8f2021bfe1 MD5 | raw file
Possible License(s): Apache-2.0
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Created on Mar 11, 2012
  4. @author: moloch
  5. Copyright 2012 Root the Box
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. '''
  16. import xml.etree.cElementTree as ET
  17. from uuid import uuid4
  18. from netaddr import IPAddress
  19. from sqlalchemy import Column, ForeignKey
  20. from sqlalchemy.orm import synonym
  21. from sqlalchemy.types import Integer, String
  22. from models import dbsession
  23. from models.BaseModels import DatabaseObject
  24. from tornado import netutil
  25. class IpAddress(DatabaseObject):
  26. ''' Wraps the netaddr IPAddress class '''
  27. uuid = Column(String(36), unique=True, nullable=False, default=lambda: str(uuid4()))
  28. box_id = Column(Integer, ForeignKey('box.id'), nullable=False)
  29. _address = Column(String(40), unique=True)
  30. _ip_address = None
  31. @classmethod
  32. def all(cls):
  33. ''' Returns a list of all objects in the database '''
  34. return dbsession.query(cls).all()
  35. @classmethod
  36. def by_id(cls, _id):
  37. ''' Returns a the object with id of _id '''
  38. return dbsession.query(cls).filter_by(id=_id).first()
  39. @classmethod
  40. def by_uuid(cls, _uuid):
  41. ''' Return and object based on a _uuid '''
  42. return dbsession.query(cls).filter_by(uuid=_uuid).first()
  43. @classmethod
  44. def by_address(cls, address):
  45. ''' Return and object based on an address '''
  46. return dbsession.query(cls).filter_by(_address=address).first()
  47. @property
  48. def address(self):
  49. if self._ip_address is None:
  50. self._ip_address = IPAddress(self._address)
  51. return self._ip_address.format()
  52. @address.setter
  53. def address(self, value):
  54. ip = IPAddress(value)
  55. if ip.is_loopback():
  56. raise ValueError("You cannot use a loopback address")
  57. if ip.is_multicast():
  58. raise ValueError("You cannot use a multicast address")
  59. self._address = value
  60. @property
  61. def version(self):
  62. if self._ip_address is None:
  63. self._ip_address = IPAddress(self._address)
  64. return self._ip_address.version
  65. @property
  66. def is_private(self):
  67. if self._ip_address is None:
  68. self._ip_address = IPAddress(self._address)
  69. return self._ip_address.is_private()
  70. def to_xml(self, parent):
  71. ip_elem = ET.SubElement(parent, "ip")
  72. ip_elem.set("version", str(self.version))
  73. ET.SubElement(ip_elem, "address").text = self.address
  74. def __repr__(self):
  75. return "<IpAddress - %s>" % self.address
  76. def __str__(self):
  77. return self._address
  78. def __eq__(self, other):
  79. return self._address == other._address
  80. def __ne__(self, other):
  81. return not self == other