Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / address.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Address objects for network connections.
6 """
7
8 import warnings, os
9
10 from zope.interface import implements
11
12 from twisted.internet.interfaces import IAddress
13 from twisted.python import util
14
15
16 class _IPAddress(object, util.FancyEqMixin):
17     """
18     An L{_IPAddress} represents the address of an IP socket endpoint, providing
19     common behavior for IPv4 and IPv6.
20
21     @ivar type: A string describing the type of transport, either 'TCP' or
22         'UDP'.
23
24     @ivar host: A string containing the presentation format of the IP address;
25         for example, "127.0.0.1" or "::1".
26     @type host: C{str}
27
28     @ivar port: An integer representing the port number.
29     @type port: C{int}
30     """
31
32     implements(IAddress)
33
34     compareAttributes = ('type', 'host', 'port')
35
36     def __init__(self, type, host, port):
37         assert type in ('TCP', 'UDP')
38         self.type = type
39         self.host = host
40         self.port = port
41
42
43     def __repr__(self):
44         return '%s(%s, %r, %d)' % (
45             self.__class__.__name__, self.type, self.host, self.port)
46
47
48     def __hash__(self):
49         return hash((self.type, self.host, self.port))
50
51
52
53 class IPv4Address(_IPAddress):
54     """
55     An L{IPv4Address} represents the address of an IPv4 socket endpoint.
56
57     @ivar host: A string containing a dotted-quad IPv4 address; for example,
58         "127.0.0.1".
59     @type host: C{str}
60     """
61
62     def __init__(self, type, host, port, _bwHack=None):
63         _IPAddress.__init__(self, type, host, port)
64         if _bwHack is not None:
65             warnings.warn("twisted.internet.address.IPv4Address._bwHack "
66                           "is deprecated since Twisted 11.0",
67                           DeprecationWarning, stacklevel=2)
68
69
70
71 class IPv6Address(_IPAddress):
72     """
73     An L{IPv6Address} represents the address of an IPv6 socket endpoint.
74
75     @ivar host: A string containing a colon-separated, hexadecimal formatted
76         IPv6 address; for example, "::1".
77     @type host: C{str}
78     """
79
80
81
82 class UNIXAddress(object, util.FancyEqMixin):
83     """
84     Object representing a UNIX socket endpoint.
85
86     @ivar name: The filename associated with this socket.
87     @type name: C{str}
88     """
89
90     implements(IAddress)
91
92     compareAttributes = ('name', )
93
94     def __init__(self, name, _bwHack = None):
95         self.name = name
96         if _bwHack is not None:
97             warnings.warn("twisted.internet.address.UNIXAddress._bwHack is deprecated since Twisted 11.0",
98                     DeprecationWarning, stacklevel=2)
99
100
101     if getattr(os.path, 'samefile', None) is not None:
102         def __eq__(self, other):
103             """
104             overriding L{util.FancyEqMixin} to ensure the os level samefile
105             check is done if the name attributes do not match.
106             """
107             res = super(UNIXAddress, self).__eq__(other)
108             if not res and self.name and other.name:
109                 try:
110                     return os.path.samefile(self.name, other.name)
111                 except OSError:
112                     pass
113             return res
114
115
116     def __repr__(self):
117         return 'UNIXAddress(%r)' % (self.name,)
118
119
120     def __hash__(self):
121         if self.name is None:
122             return hash((self.__class__, None))
123         try:
124             s1 = os.stat(self.name)
125             return hash((s1.st_ino, s1.st_dev))
126         except OSError:
127             return hash(self.name)
128
129
130
131 # These are for buildFactory backwards compatability due to
132 # stupidity-induced inconsistency.
133
134 class _ServerFactoryIPv4Address(IPv4Address):
135     """Backwards compatability hack. Just like IPv4Address in practice."""
136
137     def __eq__(self, other):
138         if isinstance(other, tuple):
139             warnings.warn("IPv4Address.__getitem__ is deprecated.  Use attributes instead.",
140                           category=DeprecationWarning, stacklevel=2)
141             return (self.host, self.port) == other
142         elif isinstance(other, IPv4Address):
143             a = (self.type, self.host, self.port)
144             b = (other.type, other.host, other.port)
145             return a == b
146         return False