Mercurial > hg
annotate hgext/zeroconf/Zeroconf.py @ 51701:97d013f48cae
zeroconf: fix boolean return value
This was (wrongly) flagged by Pytype as being undefined since it doesn't
seem to understand `try` blocks. However, the caller is expecting a boolean
and the fix to appease Pytype is simple, so we do both.
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Thu, 18 Jul 2024 12:02:01 +0200 |
parents | 7f0cb9ee0534 |
children | 454feddab720 |
rev | line source |
---|---|
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 """ Multicast DNS Service Discovery for Python, v0.12 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 Copyright (C) 2003, Paul Scott-Murphy |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
3 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
4 This module provides a framework for the use of DNS Service Discovery |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
5 using IP multicast. It has been tested against the JRendezvous |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
6 implementation from <a href="http://strangeberry.com">StrangeBerry</a>, |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 and against the mDNSResponder from Mac OS X 10.3.8. |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
8 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
9 This library is free software; you can redistribute it and/or |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
10 modify it under the terms of the GNU Lesser General Public |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
11 License as published by the Free Software Foundation; either |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
12 version 2.1 of the License, or (at your option) any later version. |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
13 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 This library is distributed in the hope that it will be useful, |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
17 Lesser General Public License for more details. |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
18 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
19 You should have received a copy of the GNU Lesser General Public |
15782
7de7630053cb
Remove FSF mailing address from GPL headers
Martin Geisler <mg@aragost.com>
parents:
14494
diff
changeset
|
20 License along with this library; if not, see |
7de7630053cb
Remove FSF mailing address from GPL headers
Martin Geisler <mg@aragost.com>
parents:
14494
diff
changeset
|
21 <http://www.gnu.org/licenses/>. |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
22 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
23 """ |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
24 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 """0.12 update - allow selection of binding interface |
28298 | 26 typo fix - Thanks A. M. Kuchlingi |
27 removed all use of word 'Rendezvous' - this is an API change""" | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
28 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
29 """0.11 update - correction to comments for addListener method |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
30 support for new record types seen from OS X |
28298 | 31 - IPv6 address |
32 - hostinfo | |
33 ignore unknown DNS record types | |
34 fixes to name decoding | |
28299 | 35 works alongside other processes using port 5353 (e.g. Mac OS X) |
28298 | 36 tested against Mac OS X 10.3.2's mDNSResponder |
37 corrections to removal of list entries for service browser""" | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
39 """0.10 update - Jonathon Paisley contributed these corrections: |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
40 always multicast replies, even when query is unicast |
28298 | 41 correct a pointer encoding problem |
42 can now write records in any order | |
43 traceback shown on failure | |
44 better TXT record parsing | |
45 server is now separate from name | |
46 can cancel a service browser | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 |
28298 | 48 modified some unit tests to accommodate these changes""" |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
50 """0.09 update - remove all records on service unregistration |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
51 fix DOS security problem with readName""" |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
52 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
53 """0.08 update - changed licensing to LGPL""" |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
55 """0.07 update - faster shutdown on engine |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
56 pointer encoding of outgoing names |
28298 | 57 ServiceBrowser now works |
58 new unit tests""" | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
59 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
60 """0.06 update - small improvements with unit tests |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
61 added defined exception types |
28298 | 62 new style objects |
63 fixed hostname/interface problem | |
64 fixed socket timeout problem | |
65 fixed addServiceListener() typo bug | |
66 using select() for socket reads | |
67 tested on Debian unstable with Python 2.2.2""" | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 |
17424
e7cfe3587ea4
fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents:
15782
diff
changeset
|
69 """0.05 update - ensure case insensitivity on domain names |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
70 support for unicast DNS queries""" |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 """0.04 update - added some unit tests |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
73 added __ne__ adjuncts where required |
28298 | 74 ensure names end in '.local.' |
75 timeout on receiving socket for clean shutdown""" | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
76 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
77 __author__ = b"Paul Scott-Murphy" |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
78 __email__ = b"paul at scott dash murphy dot com" |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
79 __version__ = b"0.12" |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
80 |
34447
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
81 import errno |
28422
e2c6092ad422
zeroconf: replace reduce+add with itertools.chain
timeless <timeless@mozdev.org>
parents:
28421
diff
changeset
|
82 import itertools |
28296
a73394e7b47c
zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents:
28295
diff
changeset
|
83 import select |
a73394e7b47c
zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents:
28295
diff
changeset
|
84 import socket |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
85 import struct |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
86 import threading |
28296
a73394e7b47c
zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents:
28295
diff
changeset
|
87 import time |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
88 import traceback |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
89 |
42551
8306b6c29add
py3: hack around inconsistency of type of name passed to DNSQuestion
Pulkit Goyal <7895pulkit@gmail.com>
parents:
42550
diff
changeset
|
90 from mercurial import pycompat |
8306b6c29add
py3: hack around inconsistency of type of name passed to DNSQuestion
Pulkit Goyal <7895pulkit@gmail.com>
parents:
42550
diff
changeset
|
91 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
92 __all__ = [b"Zeroconf", b"ServiceInfo", b"ServiceBrowser"] |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
93 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
94 # hook for threads |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
95 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
96 globals()[b'_GLOBAL_DONE'] = 0 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
97 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
98 # Some timing constants |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
99 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
100 _UNREGISTER_TIME = 125 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
101 _CHECK_TIME = 175 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
102 _REGISTER_TIME = 225 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
103 _LISTENER_TIME = 200 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
104 _BROWSER_TIME = 500 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
105 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
106 # Some DNS constants |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
107 |
41519
a36f462cf533
zeroconf: Python 3 porting of vendored library
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
108 _MDNS_ADDR = r'224.0.0.251' |
28297 | 109 _MDNS_PORT = 5353 |
110 _DNS_PORT = 53 | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
111 _DNS_TTL = 60 * 60 # one hour default TTL |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
112 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
113 _MAX_MSG_TYPICAL = 1460 # unused |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
114 _MAX_MSG_ABSOLUTE = 8972 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
115 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
116 _FLAGS_QR_MASK = 0x8000 # query response mask |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
117 _FLAGS_QR_QUERY = 0x0000 # query |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
118 _FLAGS_QR_RESPONSE = 0x8000 # response |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
119 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
120 _FLAGS_AA = 0x0400 # Authoritative answer |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
121 _FLAGS_TC = 0x0200 # Truncated |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
122 _FLAGS_RD = 0x0100 # Recursion desired |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
123 _FLAGS_RA = 0x8000 # Recursion available |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
124 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
125 _FLAGS_Z = 0x0040 # Zero |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
126 _FLAGS_AD = 0x0020 # Authentic data |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
127 _FLAGS_CD = 0x0010 # Checking disabled |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
128 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
129 _CLASS_IN = 1 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
130 _CLASS_CS = 2 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
131 _CLASS_CH = 3 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
132 _CLASS_HS = 4 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
133 _CLASS_NONE = 254 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
134 _CLASS_ANY = 255 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
135 _CLASS_MASK = 0x7FFF |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
136 _CLASS_UNIQUE = 0x8000 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
137 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
138 _TYPE_A = 1 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
139 _TYPE_NS = 2 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
140 _TYPE_MD = 3 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
141 _TYPE_MF = 4 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
142 _TYPE_CNAME = 5 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
143 _TYPE_SOA = 6 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
144 _TYPE_MB = 7 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
145 _TYPE_MG = 8 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
146 _TYPE_MR = 9 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
147 _TYPE_NULL = 10 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
148 _TYPE_WKS = 11 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
149 _TYPE_PTR = 12 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
150 _TYPE_HINFO = 13 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
151 _TYPE_MINFO = 14 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
152 _TYPE_MX = 15 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
153 _TYPE_TXT = 16 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
154 _TYPE_AAAA = 28 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
155 _TYPE_SRV = 33 |
27637
b502138f5faa
cleanup: remove superfluous space after space after equals (python)
timeless <timeless@mozdev.org>
parents:
17537
diff
changeset
|
156 _TYPE_ANY = 255 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
157 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
158 # Mapping constants to names |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
159 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
160 _CLASSES = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
161 _CLASS_IN: b"in", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
162 _CLASS_CS: b"cs", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
163 _CLASS_CH: b"ch", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
164 _CLASS_HS: b"hs", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
165 _CLASS_NONE: b"none", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
166 _CLASS_ANY: b"any", |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
167 } |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
168 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
169 _TYPES = { |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
170 _TYPE_A: b"a", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
171 _TYPE_NS: b"ns", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
172 _TYPE_MD: b"md", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
173 _TYPE_MF: b"mf", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
174 _TYPE_CNAME: b"cname", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
175 _TYPE_SOA: b"soa", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
176 _TYPE_MB: b"mb", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
177 _TYPE_MG: b"mg", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
178 _TYPE_MR: b"mr", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
179 _TYPE_NULL: b"null", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
180 _TYPE_WKS: b"wks", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
181 _TYPE_PTR: b"ptr", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
182 _TYPE_HINFO: b"hinfo", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
183 _TYPE_MINFO: b"minfo", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
184 _TYPE_MX: b"mx", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
185 _TYPE_TXT: b"txt", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
186 _TYPE_AAAA: b"quada", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
187 _TYPE_SRV: b"srv", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
188 _TYPE_ANY: b"any", |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
189 } |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
190 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
191 # utility functions |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
192 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
193 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
194 def currentTimeMillis(): |
28298 | 195 """Current system time in milliseconds""" |
196 return time.time() * 1000 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
197 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
198 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
199 # Exceptions |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
200 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
201 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
202 class NonLocalNameException(Exception): |
28298 | 203 pass |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
204 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
205 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
206 class NonUniqueNameException(Exception): |
28298 | 207 pass |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
208 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
209 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
210 class NamePartTooLongException(Exception): |
28298 | 211 pass |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
212 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
213 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
214 class AbstractMethodException(Exception): |
28298 | 215 pass |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
216 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
217 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
218 class BadTypeInNameException(Exception): |
28298 | 219 pass |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
220 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
221 |
11435
7c58cde598fe
zeroconf: Use BadDomainName exception instead of string exceptions
Javi Merino <cibervicho@gmail.com>
parents:
10514
diff
changeset
|
222 class BadDomainName(Exception): |
28298 | 223 def __init__(self, pos): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
224 Exception.__init__(self, b"at position %s" % pos) |
11435
7c58cde598fe
zeroconf: Use BadDomainName exception instead of string exceptions
Javi Merino <cibervicho@gmail.com>
parents:
10514
diff
changeset
|
225 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
226 |
11435
7c58cde598fe
zeroconf: Use BadDomainName exception instead of string exceptions
Javi Merino <cibervicho@gmail.com>
parents:
10514
diff
changeset
|
227 class BadDomainNameCircular(BadDomainName): |
28298 | 228 pass |
11435
7c58cde598fe
zeroconf: Use BadDomainName exception instead of string exceptions
Javi Merino <cibervicho@gmail.com>
parents:
10514
diff
changeset
|
229 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
230 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
231 # implementation classes |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
232 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
233 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
234 class DNSEntry: |
28298 | 235 """A DNS entry""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
236 |
28298 | 237 def __init__(self, name, type, clazz): |
41519
a36f462cf533
zeroconf: Python 3 porting of vendored library
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41365
diff
changeset
|
238 self.key = name.lower() |
28298 | 239 self.name = name |
240 self.type = type | |
241 self.clazz = clazz & _CLASS_MASK | |
242 self.unique = (clazz & _CLASS_UNIQUE) != 0 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
243 |
28298 | 244 def __eq__(self, other): |
245 """Equality test on name, type, and class""" | |
246 if isinstance(other, DNSEntry): | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
247 return ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
248 self.name == other.name |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
249 and self.type == other.type |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
250 and self.clazz == other.clazz |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
251 ) |
28298 | 252 return 0 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
253 |
28298 | 254 def __ne__(self, other): |
255 """Non-equality test""" | |
256 return not self.__eq__(other) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
257 |
28298 | 258 def getClazz(self, clazz): |
259 """Class accessor""" | |
260 try: | |
261 return _CLASSES[clazz] | |
262 except KeyError: | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
263 return b"?(%s)" % clazz |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
264 |
28298 | 265 def getType(self, type): |
266 """Type accessor""" | |
267 try: | |
268 return _TYPES[type] | |
269 except KeyError: | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
270 return b"?(%s)" % type |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
271 |
28298 | 272 def toString(self, hdr, other): |
273 """String representation with additional information""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
274 result = b"%s[%s,%s" % ( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
275 hdr, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
276 self.getType(self.type), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
277 self.getClazz(self.clazz), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
278 ) |
28298 | 279 if self.unique: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
280 result += b"-unique," |
28298 | 281 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
282 result += b"," |
28298 | 283 result += self.name |
284 if other is not None: | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
285 result += b",%s]" % other |
28298 | 286 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
287 result += b"]" |
28298 | 288 return result |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
289 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
290 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
291 class DNSQuestion(DNSEntry): |
28298 | 292 """A DNS question entry""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
293 |
28298 | 294 def __init__(self, name, type, clazz): |
49293
e3143ab9dadb
zeroconf: constant-fold a `pycompat.ispy3`
Manuel Jacob <me@manueljacob.de>
parents:
49053
diff
changeset
|
295 if isinstance(name, str): |
42551
8306b6c29add
py3: hack around inconsistency of type of name passed to DNSQuestion
Pulkit Goyal <7895pulkit@gmail.com>
parents:
42550
diff
changeset
|
296 name = name.encode('ascii') |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
297 if not name.endswith(b".local."): |
28298 | 298 raise NonLocalNameException(name) |
299 DNSEntry.__init__(self, name, type, clazz) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
300 |
28298 | 301 def answeredBy(self, rec): |
302 """Returns true if the question is answered by the record""" | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
303 return ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
304 self.clazz == rec.clazz |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
305 and (self.type == rec.type or self.type == _TYPE_ANY) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
306 and self.name == rec.name |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
307 ) |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
308 |
28298 | 309 def __repr__(self): |
310 """String representation""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
311 return DNSEntry.toString(self, b"question", None) |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
312 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
313 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
314 class DNSRecord(DNSEntry): |
28298 | 315 """A DNS record - like a DNS entry, but has a TTL""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
316 |
28298 | 317 def __init__(self, name, type, clazz, ttl): |
318 DNSEntry.__init__(self, name, type, clazz) | |
319 self.ttl = ttl | |
320 self.created = currentTimeMillis() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
321 |
28298 | 322 def __eq__(self, other): |
323 """Tests equality as per DNSRecord""" | |
324 if isinstance(other, DNSRecord): | |
325 return DNSEntry.__eq__(self, other) | |
326 return 0 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
327 |
28298 | 328 def suppressedBy(self, msg): |
329 """Returns true if any answer in a message can suffice for the | |
330 information held in this record.""" | |
331 for record in msg.answers: | |
332 if self.suppressedByAnswer(record): | |
333 return 1 | |
334 return 0 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
335 |
28298 | 336 def suppressedByAnswer(self, other): |
337 """Returns true if another record has same name, type and class, | |
338 and if its TTL is at least half of this record's.""" | |
339 if self == other and other.ttl > (self.ttl / 2): | |
340 return 1 | |
341 return 0 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
342 |
28298 | 343 def getExpirationTime(self, percent): |
344 """Returns the time at which this record will have expired | |
345 by a certain percentage.""" | |
346 return self.created + (percent * self.ttl * 10) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
347 |
28298 | 348 def getRemainingTTL(self, now): |
349 """Returns the remaining TTL in seconds.""" | |
350 return max(0, (self.getExpirationTime(100) - now) / 1000) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
351 |
28298 | 352 def isExpired(self, now): |
353 """Returns true if this record has expired.""" | |
354 return self.getExpirationTime(100) <= now | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
355 |
28298 | 356 def isStale(self, now): |
357 """Returns true if this record is at least half way expired.""" | |
358 return self.getExpirationTime(50) <= now | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
359 |
28298 | 360 def resetTTL(self, other): |
361 """Sets this record's TTL and created time to that of | |
362 another record.""" | |
363 self.created = other.created | |
364 self.ttl = other.ttl | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
365 |
28298 | 366 def write(self, out): |
367 """Abstract method""" | |
368 raise AbstractMethodException | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
369 |
28298 | 370 def toString(self, other): |
371 """String representation with additional information""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
372 arg = b"%s/%s,%s" % ( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
373 self.ttl, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
374 self.getRemainingTTL(currentTimeMillis()), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
375 other, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
376 ) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
377 return DNSEntry.toString(self, b"record", arg) |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
378 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
379 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
380 class DNSAddress(DNSRecord): |
28298 | 381 """A DNS address record""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
382 |
28298 | 383 def __init__(self, name, type, clazz, ttl, address): |
384 DNSRecord.__init__(self, name, type, clazz, ttl) | |
385 self.address = address | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
386 |
28298 | 387 def write(self, out): |
388 """Used in constructing an outgoing packet""" | |
389 out.writeString(self.address, len(self.address)) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
390 |
28298 | 391 def __eq__(self, other): |
392 """Tests equality on address""" | |
393 if isinstance(other, DNSAddress): | |
394 return self.address == other.address | |
395 return 0 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
396 |
28298 | 397 def __repr__(self): |
398 """String representation""" | |
399 try: | |
400 return socket.inet_ntoa(self.address) | |
401 except Exception: | |
402 return self.address | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
403 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
404 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
405 class DNSHinfo(DNSRecord): |
28298 | 406 """A DNS host information record""" |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
407 |
28298 | 408 def __init__(self, name, type, clazz, ttl, cpu, os): |
409 DNSRecord.__init__(self, name, type, clazz, ttl) | |
410 self.cpu = cpu | |
411 self.os = os | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
412 |
28298 | 413 def write(self, out): |
414 """Used in constructing an outgoing packet""" | |
415 out.writeString(self.cpu, len(self.cpu)) | |
416 out.writeString(self.os, len(self.os)) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
417 |
28298 | 418 def __eq__(self, other): |
419 """Tests equality on cpu and os""" | |
420 if isinstance(other, DNSHinfo): | |
421 return self.cpu == other.cpu and self.os == other.os | |
422 return 0 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
423 |
28298 | 424 def __repr__(self): |
425 """String representation""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
426 return self.cpu + b" " + self.os |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
427 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
428 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
429 class DNSPointer(DNSRecord): |
28298 | 430 """A DNS pointer record""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
431 |
28298 | 432 def __init__(self, name, type, clazz, ttl, alias): |
433 DNSRecord.__init__(self, name, type, clazz, ttl) | |
434 self.alias = alias | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
435 |
28298 | 436 def write(self, out): |
437 """Used in constructing an outgoing packet""" | |
438 out.writeName(self.alias) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
439 |
28298 | 440 def __eq__(self, other): |
441 """Tests equality on alias""" | |
442 if isinstance(other, DNSPointer): | |
443 return self.alias == other.alias | |
444 return 0 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
445 |
28298 | 446 def __repr__(self): |
447 """String representation""" | |
448 return self.toString(self.alias) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
449 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
450 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
451 class DNSText(DNSRecord): |
28298 | 452 """A DNS text record""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
453 |
28298 | 454 def __init__(self, name, type, clazz, ttl, text): |
455 DNSRecord.__init__(self, name, type, clazz, ttl) | |
456 self.text = text | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
457 |
28298 | 458 def write(self, out): |
459 """Used in constructing an outgoing packet""" | |
460 out.writeString(self.text, len(self.text)) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
461 |
28298 | 462 def __eq__(self, other): |
463 """Tests equality on text""" | |
464 if isinstance(other, DNSText): | |
465 return self.text == other.text | |
466 return 0 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
467 |
28298 | 468 def __repr__(self): |
469 """String representation""" | |
470 if len(self.text) > 10: | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
471 return self.toString(self.text[:7] + b"...") |
28298 | 472 else: |
473 return self.toString(self.text) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
474 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
475 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
476 class DNSService(DNSRecord): |
28298 | 477 """A DNS service record""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
478 |
28298 | 479 def __init__(self, name, type, clazz, ttl, priority, weight, port, server): |
480 DNSRecord.__init__(self, name, type, clazz, ttl) | |
481 self.priority = priority | |
482 self.weight = weight | |
483 self.port = port | |
484 self.server = server | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
485 |
28298 | 486 def write(self, out): |
487 """Used in constructing an outgoing packet""" | |
488 out.writeShort(self.priority) | |
489 out.writeShort(self.weight) | |
490 out.writeShort(self.port) | |
491 out.writeName(self.server) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
492 |
28298 | 493 def __eq__(self, other): |
494 """Tests equality on priority, weight, port and server""" | |
495 if isinstance(other, DNSService): | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
496 return ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
497 self.priority == other.priority |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
498 and self.weight == other.weight |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
499 and self.port == other.port |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
500 and self.server == other.server |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
501 ) |
28298 | 502 return 0 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
503 |
28298 | 504 def __repr__(self): |
505 """String representation""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
506 return self.toString(b"%s:%s" % (self.server, self.port)) |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
507 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
508 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
509 class DNSIncoming: |
28298 | 510 """Object representation of an incoming DNS packet""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
511 |
28298 | 512 def __init__(self, data): |
513 """Constructor from string holding bytes of packet""" | |
514 self.offset = 0 | |
515 self.data = data | |
516 self.questions = [] | |
517 self.answers = [] | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
518 self.numquestions = 0 |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
519 self.numanswers = 0 |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
520 self.numauthorities = 0 |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
521 self.numadditionals = 0 |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
522 |
28298 | 523 self.readHeader() |
524 self.readQuestions() | |
525 self.readOthers() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
526 |
28298 | 527 def readHeader(self): |
528 """Reads header portion of packet""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
529 format = b'!HHHHHH' |
28298 | 530 length = struct.calcsize(format) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
531 info = struct.unpack( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
532 format, self.data[self.offset : self.offset + length] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
533 ) |
28298 | 534 self.offset += length |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
535 |
28298 | 536 self.id = info[0] |
537 self.flags = info[1] | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
538 self.numquestions = info[2] |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
539 self.numanswers = info[3] |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
540 self.numauthorities = info[4] |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
541 self.numadditionals = info[5] |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
542 |
28298 | 543 def readQuestions(self): |
544 """Reads questions section of packet""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
545 format = b'!HH' |
28298 | 546 length = struct.calcsize(format) |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
547 for i in range(0, self.numquestions): |
28298 | 548 name = self.readName() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
549 info = struct.unpack( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
550 format, self.data[self.offset : self.offset + length] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
551 ) |
28298 | 552 self.offset += length |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
553 |
28298 | 554 try: |
555 question = DNSQuestion(name, info[0], info[1]) | |
556 self.questions.append(question) | |
557 except NonLocalNameException: | |
558 pass | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
559 |
28298 | 560 def readInt(self): |
561 """Reads an integer from the packet""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
562 format = b'!I' |
28298 | 563 length = struct.calcsize(format) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
564 info = struct.unpack( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
565 format, self.data[self.offset : self.offset + length] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
566 ) |
28298 | 567 self.offset += length |
568 return info[0] | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
569 |
28298 | 570 def readCharacterString(self): |
571 """Reads a character string from the packet""" | |
572 length = ord(self.data[self.offset]) | |
573 self.offset += 1 | |
574 return self.readString(length) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
575 |
28298 | 576 def readString(self, len): |
577 """Reads a string of a given length from the packet""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
578 format = b'!%ds' % len |
28298 | 579 length = struct.calcsize(format) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
580 info = struct.unpack( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
581 format, self.data[self.offset : self.offset + length] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
582 ) |
28298 | 583 self.offset += length |
584 return info[0] | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
585 |
28298 | 586 def readUnsignedShort(self): |
587 """Reads an unsigned short from the packet""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
588 format = b'!H' |
28298 | 589 length = struct.calcsize(format) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
590 info = struct.unpack( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
591 format, self.data[self.offset : self.offset + length] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
592 ) |
28298 | 593 self.offset += length |
594 return info[0] | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
595 |
28298 | 596 def readOthers(self): |
28299 | 597 """Reads answers, authorities and additionals section of the packet""" |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
598 format = b'!HHiH' |
28298 | 599 length = struct.calcsize(format) |
28504
3c90090320ad
zeroconf: remove leftover camelcase identifier
Martin von Zweigbergk <martinvonz@google.com>
parents:
28422
diff
changeset
|
600 n = self.numanswers + self.numauthorities + self.numadditionals |
28298 | 601 for i in range(0, n): |
602 domain = self.readName() | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
603 info = struct.unpack( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
604 format, self.data[self.offset : self.offset + length] |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
605 ) |
28298 | 606 self.offset += length |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
607 |
28298 | 608 rec = None |
609 if info[0] == _TYPE_A: | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
610 rec = DNSAddress( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
611 domain, info[0], info[1], info[2], self.readString(4) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
612 ) |
28298 | 613 elif info[0] == _TYPE_CNAME or info[0] == _TYPE_PTR: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
614 rec = DNSPointer( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
615 domain, info[0], info[1], info[2], self.readName() |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
616 ) |
28298 | 617 elif info[0] == _TYPE_TXT: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
618 rec = DNSText( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
619 domain, info[0], info[1], info[2], self.readString(info[3]) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
620 ) |
28298 | 621 elif info[0] == _TYPE_SRV: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
622 rec = DNSService( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
623 domain, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
624 info[0], |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
625 info[1], |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
626 info[2], |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
627 self.readUnsignedShort(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
628 self.readUnsignedShort(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
629 self.readUnsignedShort(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
630 self.readName(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
631 ) |
28298 | 632 elif info[0] == _TYPE_HINFO: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
633 rec = DNSHinfo( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
634 domain, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
635 info[0], |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
636 info[1], |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
637 info[2], |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
638 self.readCharacterString(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
639 self.readCharacterString(), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
640 ) |
28298 | 641 elif info[0] == _TYPE_AAAA: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
642 rec = DNSAddress( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
643 domain, info[0], info[1], info[2], self.readString(16) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
644 ) |
28298 | 645 else: |
646 # Try to ignore types we don't know about | |
647 # this may mean the rest of the name is | |
648 # unable to be parsed, and may show errors | |
649 # so this is left for debugging. New types | |
650 # encountered need to be parsed properly. | |
651 # | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
652 # print "UNKNOWN TYPE = " + str(info[0]) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
653 # raise BadTypeInNameException |
28298 | 654 self.offset += info[3] |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
655 |
28298 | 656 if rec is not None: |
657 self.answers.append(rec) | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
658 |
28298 | 659 def isQuery(self): |
660 """Returns true if this is a query""" | |
661 return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
662 |
28298 | 663 def isResponse(self): |
664 """Returns true if this is a response""" | |
665 return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
666 |
28298 | 667 def readUTF(self, offset, len): |
668 """Reads a UTF-8 string of a given length from the packet""" | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
669 return self.data[offset : offset + len].decode('utf-8') |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
670 |
28298 | 671 def readName(self): |
672 """Reads a domain name from the packet""" | |
42550
683aeef12830
py3: add r'' prefixes and do ('%d' % int) instead of str(int)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
41519
diff
changeset
|
673 result = r'' |
28298 | 674 off = self.offset |
675 next = -1 | |
676 first = off | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
677 |
28298 | 678 while True: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
679 len = ord(self.data[off : off + 1]) |
28298 | 680 off += 1 |
681 if len == 0: | |
682 break | |
683 t = len & 0xC0 | |
684 if t == 0x00: | |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43077
diff
changeset
|
685 result = ''.join((result, self.readUTF(off, len) + '.')) |
28298 | 686 off += len |
687 elif t == 0xC0: | |
688 if next < 0: | |
689 next = off + 1 | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
690 off = ((len & 0x3F) << 8) | ord(self.data[off : off + 1]) |
28298 | 691 if off >= first: |
692 raise BadDomainNameCircular(off) | |
693 first = off | |
694 else: | |
695 raise BadDomainName(off) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
696 |
28298 | 697 if next >= 0: |
698 self.offset = next | |
699 else: | |
700 self.offset = off | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
701 |
28298 | 702 return result |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
703 |
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
704 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
705 class DNSOutgoing: |
28298 | 706 """Object representation of an outgoing packet""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
707 |
28302
e96a3ae025ed
zeroconf: remove whitespace around = for named parameters
timeless <timeless@mozdev.org>
parents:
28301
diff
changeset
|
708 def __init__(self, flags, multicast=1): |
28298 | 709 self.finished = 0 |
710 self.id = 0 | |
711 self.multicast = multicast | |
712 self.flags = flags | |
713 self.names = {} | |
714 self.data = [] | |
715 self.size = 12 | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
716 |
28298 | 717 self.questions = [] |
718 self.answers = [] | |
719 self.authorities = [] | |
720 self.additionals = [] | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
721 |
28298 | 722 def addQuestion(self, record): |
723 """Adds a question""" | |
724 self.questions.append(record) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
725 |
28298 | 726 def addAnswer(self, inp, record): |
727 """Adds an answer""" | |
728 if not record.suppressedBy(inp): | |
729 self.addAnswerAtTime(record, 0) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
730 |
28298 | 731 def addAnswerAtTime(self, record, now): |
732 """Adds an answer if if does not expire by a certain time""" | |
733 if record is not None: | |
734 if now == 0 or not record.isExpired(now): | |
735 self.answers.append((record, now)) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
736 |
28298 | 737 def addAuthoritativeAnswer(self, record): |
738 """Adds an authoritative answer""" | |
739 self.authorities.append(record) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
740 |
28298 | 741 def addAdditionalAnswer(self, record): |
742 """Adds an additional answer""" | |
743 self.additionals.append(record) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
744 |
28298 | 745 def writeByte(self, value): |
746 """Writes a single byte to the packet""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
747 format = b'!c' |
28298 | 748 self.data.append(struct.pack(format, chr(value))) |
749 self.size += 1 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
750 |
28298 | 751 def insertShort(self, index, value): |
752 """Inserts an unsigned short in a certain position in the packet""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
753 format = b'!H' |
28298 | 754 self.data.insert(index, struct.pack(format, value)) |
755 self.size += 2 | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
756 |
28298 | 757 def writeShort(self, value): |
758 """Writes an unsigned short to the packet""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
759 format = b'!H' |
28298 | 760 self.data.append(struct.pack(format, value)) |
761 self.size += 2 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
762 |
28298 | 763 def writeInt(self, value): |
764 """Writes an unsigned integer to the packet""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
765 format = b'!I' |
28298 | 766 self.data.append(struct.pack(format, int(value))) |
767 self.size += 4 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
768 |
28298 | 769 def writeString(self, value, length): |
770 """Writes a string to the packet""" | |
47864
ad2c37075f46
zeroconf: fix an issue concatenating bytes and str
Matt Harbison <matt_harbison@yahoo.com>
parents:
44956
diff
changeset
|
771 format = '!' + str(length) + 's' |
28298 | 772 self.data.append(struct.pack(format, value)) |
773 self.size += length | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
774 |
28298 | 775 def writeUTF(self, s): |
776 """Writes a UTF-8 string of a given length to the packet""" | |
777 utfstr = s.encode('utf-8') | |
778 length = len(utfstr) | |
779 if length > 64: | |
780 raise NamePartTooLongException | |
781 self.writeByte(length) | |
782 self.writeString(utfstr, length) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
783 |
28298 | 784 def writeName(self, name): |
785 """Writes a domain name to the packet""" | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
786 |
28298 | 787 try: |
788 # Find existing instance of this name in packet | |
789 # | |
790 index = self.names[name] | |
791 except KeyError: | |
792 # No record of this name already, so write it | |
793 # out as normal, recording the location of the name | |
794 # for future pointers to it. | |
795 # | |
796 self.names[name] = self.size | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
797 parts = name.split(b'.') |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
798 if parts[-1] == b'': |
28298 | 799 parts = parts[:-1] |
800 for part in parts: | |
801 self.writeUTF(part) | |
802 self.writeByte(0) | |
803 return | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
804 |
28298 | 805 # An index was found, so write a pointer to it |
806 # | |
807 self.writeByte((index >> 8) | 0xC0) | |
808 self.writeByte(index) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
809 |
28298 | 810 def writeQuestion(self, question): |
811 """Writes a question to the packet""" | |
812 self.writeName(question.name) | |
813 self.writeShort(question.type) | |
814 self.writeShort(question.clazz) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
815 |
28298 | 816 def writeRecord(self, record, now): |
817 """Writes a record (answer, authoritative answer, additional) to | |
818 the packet""" | |
819 self.writeName(record.name) | |
820 self.writeShort(record.type) | |
821 if record.unique and self.multicast: | |
822 self.writeShort(record.clazz | _CLASS_UNIQUE) | |
823 else: | |
824 self.writeShort(record.clazz) | |
825 if now == 0: | |
826 self.writeInt(record.ttl) | |
827 else: | |
828 self.writeInt(record.getRemainingTTL(now)) | |
829 index = len(self.data) | |
830 # Adjust size for the short we will write before this record | |
831 # | |
832 self.size += 2 | |
833 record.write(self) | |
834 self.size -= 2 | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
835 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
836 length = len(b''.join(self.data[index:])) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
837 self.insertShort(index, length) # Here is the short we adjusted for |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
838 |
28298 | 839 def packet(self): |
840 """Returns a string containing the packet's bytes | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
841 |
28298 | 842 No further parts should be added to the packet once this |
843 is done.""" | |
844 if not self.finished: | |
845 self.finished = 1 | |
846 for question in self.questions: | |
847 self.writeQuestion(question) | |
848 for answer, time_ in self.answers: | |
849 self.writeRecord(answer, time_) | |
850 for authority in self.authorities: | |
851 self.writeRecord(authority, 0) | |
852 for additional in self.additionals: | |
853 self.writeRecord(additional, 0) | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
854 |
28298 | 855 self.insertShort(0, len(self.additionals)) |
856 self.insertShort(0, len(self.authorities)) | |
857 self.insertShort(0, len(self.answers)) | |
858 self.insertShort(0, len(self.questions)) | |
859 self.insertShort(0, self.flags) | |
860 if self.multicast: | |
861 self.insertShort(0, 0) | |
862 else: | |
863 self.insertShort(0, self.id) | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
864 return b''.join(self.data) |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
865 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
866 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
867 class DNSCache: |
28298 | 868 """A cache of DNS entries""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
869 |
28298 | 870 def __init__(self): |
871 self.cache = {} | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
872 |
28298 | 873 def add(self, entry): |
874 """Adds an entry""" | |
875 try: | |
876 list = self.cache[entry.key] | |
877 except KeyError: | |
878 list = self.cache[entry.key] = [] | |
879 list.append(entry) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
880 |
28298 | 881 def remove(self, entry): |
882 """Removes an entry""" | |
883 try: | |
884 list = self.cache[entry.key] | |
885 list.remove(entry) | |
886 except KeyError: | |
887 pass | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
888 |
28298 | 889 def get(self, entry): |
890 """Gets an entry by key. Will return None if there is no | |
891 matching entry.""" | |
892 try: | |
893 list = self.cache[entry.key] | |
894 return list[list.index(entry)] | |
895 except (KeyError, ValueError): | |
896 return None | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
897 |
28298 | 898 def getByDetails(self, name, type, clazz): |
899 """Gets an entry by details. Will return None if there is | |
900 no matching entry.""" | |
901 entry = DNSEntry(name, type, clazz) | |
902 return self.get(entry) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
903 |
28298 | 904 def entriesWithName(self, name): |
905 """Returns a list of entries whose key matches the name.""" | |
906 try: | |
907 return self.cache[name] | |
908 except KeyError: | |
909 return [] | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
910 |
28298 | 911 def entries(self): |
912 """Returns a list of all entries""" | |
913 try: | |
28422
e2c6092ad422
zeroconf: replace reduce+add with itertools.chain
timeless <timeless@mozdev.org>
parents:
28421
diff
changeset
|
914 return list(itertools.chain.from_iterable(self.cache.values())) |
28298 | 915 except Exception: |
916 return [] | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
917 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
918 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
919 class Engine(threading.Thread): |
28298 | 920 """An engine wraps read access to sockets, allowing objects that |
921 need to receive data from sockets to be called back when the | |
922 sockets are ready. | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
923 |
28298 | 924 A reader needs a handle_read() method, which is called when the socket |
925 it is interested in is ready for reading. | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
926 |
28298 | 927 Writers are not implemented here, because we only send short |
928 packets. | |
929 """ | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
930 |
28298 | 931 def __init__(self, zeroconf): |
932 threading.Thread.__init__(self) | |
933 self.zeroconf = zeroconf | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
934 self.readers = {} # maps socket to reader |
28298 | 935 self.timeout = 5 |
936 self.condition = threading.Condition() | |
937 self.start() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
938 |
28298 | 939 def run(self): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
940 while not globals()[b'_GLOBAL_DONE']: |
28298 | 941 rs = self.getReaders() |
942 if len(rs) == 0: | |
943 # No sockets to manage, but we wait for the timeout | |
944 # or addition of a socket | |
945 # | |
946 self.condition.acquire() | |
947 self.condition.wait(self.timeout) | |
948 self.condition.release() | |
949 else: | |
950 try: | |
951 rr, wr, er = select.select(rs, [], [], self.timeout) | |
952 for sock in rr: | |
953 try: | |
954 self.readers[sock].handle_read() | |
955 except Exception: | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
956 if not globals()[b'_GLOBAL_DONE']: |
28298 | 957 traceback.print_exc() |
958 except Exception: | |
959 pass | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
960 |
28298 | 961 def getReaders(self): |
962 self.condition.acquire() | |
963 result = self.readers.keys() | |
964 self.condition.release() | |
965 return result | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
966 |
28298 | 967 def addReader(self, reader, socket): |
968 self.condition.acquire() | |
969 self.readers[socket] = reader | |
970 self.condition.notify() | |
971 self.condition.release() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
972 |
28298 | 973 def delReader(self, socket): |
974 self.condition.acquire() | |
28301
fd8a4d2d6541
zeroconf: del is not a function
timeless <timeless@mozdev.org>
parents:
28300
diff
changeset
|
975 del self.readers[socket] |
28298 | 976 self.condition.notify() |
977 self.condition.release() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
978 |
28298 | 979 def notify(self): |
980 self.condition.acquire() | |
981 self.condition.notify() | |
982 self.condition.release() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
983 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
984 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
985 class Listener: |
28298 | 986 """A Listener is used by this module to listen on the multicast |
987 group to which DNS messages are sent, allowing the implementation | |
988 to cache information as it arrives. | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
989 |
28298 | 990 It requires registration with an Engine object in order to have |
991 the read() method called when a socket is available for reading.""" | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
992 |
28298 | 993 def __init__(self, zeroconf): |
994 self.zeroconf = zeroconf | |
995 self.zeroconf.engine.addReader(self, self.zeroconf.socket) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
996 |
28298 | 997 def handle_read(self): |
34447
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
998 sock = self.zeroconf.socket |
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
999 try: |
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
1000 data, (addr, port) = sock.recvfrom(_MAX_MSG_ABSOLUTE) |
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
1001 except socket.error as e: |
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
1002 if e.errno == errno.EBADF: |
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
1003 # some other thread may close the socket |
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
1004 return |
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
1005 else: |
5385b76fd1fd
zeroconf: do not crash if socket being read is closed by another thread
Jun Wu <quark@fb.com>
parents:
28504
diff
changeset
|
1006 raise |
28298 | 1007 self.data = data |
1008 msg = DNSIncoming(data) | |
1009 if msg.isQuery(): | |
1010 # Always multicast responses | |
1011 # | |
1012 if port == _MDNS_PORT: | |
1013 self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) | |
1014 # If it's not a multicast query, reply via unicast | |
1015 # and multicast | |
1016 # | |
1017 elif port == _DNS_PORT: | |
1018 self.zeroconf.handleQuery(msg, addr, port) | |
1019 self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT) | |
1020 else: | |
1021 self.zeroconf.handleResponse(msg) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1022 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1023 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1024 class Reaper(threading.Thread): |
28298 | 1025 """A Reaper is used by this module to remove cache entries that |
1026 have expired.""" | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1027 |
28298 | 1028 def __init__(self, zeroconf): |
1029 threading.Thread.__init__(self) | |
1030 self.zeroconf = zeroconf | |
1031 self.start() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1032 |
28298 | 1033 def run(self): |
1034 while True: | |
1035 self.zeroconf.wait(10 * 1000) | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1036 if globals()[b'_GLOBAL_DONE']: |
28298 | 1037 return |
1038 now = currentTimeMillis() | |
1039 for record in self.zeroconf.cache.entries(): | |
1040 if record.isExpired(now): | |
1041 self.zeroconf.updateRecord(now, record) | |
1042 self.zeroconf.cache.remove(record) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1043 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1044 |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1045 class ServiceBrowser(threading.Thread): |
28298 | 1046 """Used to browse for a service of a specific type. |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1047 |
28298 | 1048 The listener object will have its addService() and |
1049 removeService() methods called when this browser | |
1050 discovers changes in the services availability.""" | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1051 |
28298 | 1052 def __init__(self, zeroconf, type, listener): |
1053 """Creates a browser for a specific type""" | |
1054 threading.Thread.__init__(self) | |
1055 self.zeroconf = zeroconf | |
1056 self.type = type | |
1057 self.listener = listener | |
1058 self.services = {} | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1059 self.nexttime = currentTimeMillis() |
28298 | 1060 self.delay = _BROWSER_TIME |
1061 self.list = [] | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1062 |
28298 | 1063 self.done = 0 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1064 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1065 self.zeroconf.addListener( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1066 self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1067 ) |
28298 | 1068 self.start() |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1069 |
28298 | 1070 def updateRecord(self, zeroconf, now, record): |
1071 """Callback invoked by Zeroconf when new information arrives. | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1072 |
28298 | 1073 Updates information required by browser in the Zeroconf cache.""" |
1074 if record.type == _TYPE_PTR and record.name == self.type: | |
1075 expired = record.isExpired(now) | |
1076 try: | |
1077 oldrecord = self.services[record.alias.lower()] | |
1078 if not expired: | |
1079 oldrecord.resetTTL(record) | |
1080 else: | |
28301
fd8a4d2d6541
zeroconf: del is not a function
timeless <timeless@mozdev.org>
parents:
28300
diff
changeset
|
1081 del self.services[record.alias.lower()] |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1082 callback = lambda x: self.listener.removeService( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1083 x, self.type, record.alias |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1084 ) |
28298 | 1085 self.list.append(callback) |
1086 return | |
1087 except Exception: | |
1088 if not expired: | |
1089 self.services[record.alias.lower()] = record | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1090 callback = lambda x: self.listener.addService( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1091 x, self.type, record.alias |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1092 ) |
28298 | 1093 self.list.append(callback) |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1094 |
28298 | 1095 expires = record.getExpirationTime(75) |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1096 if expires < self.nexttime: |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1097 self.nexttime = expires |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1098 |
28298 | 1099 def cancel(self): |
1100 self.done = 1 | |
1101 self.zeroconf.notifyAll() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1102 |
28298 | 1103 def run(self): |
1104 while True: | |
1105 event = None | |
1106 now = currentTimeMillis() | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1107 if len(self.list) == 0 and self.nexttime > now: |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1108 self.zeroconf.wait(self.nexttime - now) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1109 if globals()[b'_GLOBAL_DONE'] or self.done: |
28298 | 1110 return |
1111 now = currentTimeMillis() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1112 |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1113 if self.nexttime <= now: |
28298 | 1114 out = DNSOutgoing(_FLAGS_QR_QUERY) |
1115 out.addQuestion(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN)) | |
1116 for record in self.services.values(): | |
1117 if not record.isExpired(now): | |
1118 out.addAnswerAtTime(record, now) | |
1119 self.zeroconf.send(out) | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1120 self.nexttime = now + self.delay |
28298 | 1121 self.delay = min(20 * 1000, self.delay * 2) |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1122 |
28298 | 1123 if len(self.list) > 0: |
1124 event = self.list.pop(0) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1125 |
28298 | 1126 if event is not None: |
1127 event(self.zeroconf) | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1128 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1129 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
1130 class ServiceInfo: |
28298 | 1131 """Service information""" |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1132 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1133 def __init__( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1134 self, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1135 type, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1136 name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1137 address=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1138 port=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1139 weight=0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1140 priority=0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1141 properties=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1142 server=None, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1143 ): |
28298 | 1144 """Create a service description. |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1145 |
28298 | 1146 type: fully qualified service type name |
1147 name: fully qualified service name | |
1148 address: IP address as unsigned short, network byte order | |
1149 port: port that the service runs on | |
1150 weight: weight of the service | |
1151 priority: priority of the service | |
28299 | 1152 properties: dictionary of properties (or a string holding the bytes for |
1153 the text field) | |
28298 | 1154 server: fully qualified name for service host (defaults to name)""" |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1155 |
28298 | 1156 if not name.endswith(type): |
1157 raise BadTypeInNameException | |
1158 self.type = type | |
1159 self.name = name | |
1160 self.address = address | |
1161 self.port = port | |
1162 self.weight = weight | |
1163 self.priority = priority | |
1164 if server: | |
1165 self.server = server | |
1166 else: | |
1167 self.server = name | |
1168 self.setProperties(properties) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1169 |
28298 | 1170 def setProperties(self, properties): |
1171 """Sets properties and text of this info from a dictionary""" | |
1172 if isinstance(properties, dict): | |
1173 self.properties = properties | |
1174 list = [] | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1175 result = b'' |
28298 | 1176 for key in properties: |
1177 value = properties[key] | |
1178 if value is None: | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1179 suffix = b'' |
28298 | 1180 elif isinstance(value, str): |
1181 suffix = value | |
1182 elif isinstance(value, int): | |
1183 if value: | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1184 suffix = b'true' |
28298 | 1185 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1186 suffix = b'false' |
28298 | 1187 else: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1188 suffix = b'' |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1189 list.append(b'='.join((key, suffix))) |
28298 | 1190 for item in list: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1191 result = b''.join( |
43677
0f82b29f7494
zeroconf: fix traceback under py3
Kim Alvefur <zash@zash.se>
parents:
43077
diff
changeset
|
1192 ( |
0f82b29f7494
zeroconf: fix traceback under py3
Kim Alvefur <zash@zash.se>
parents:
43077
diff
changeset
|
1193 result, |
0f82b29f7494
zeroconf: fix traceback under py3
Kim Alvefur <zash@zash.se>
parents:
43077
diff
changeset
|
1194 struct.pack(b'!c', pycompat.bytechr(len(item))), |
0f82b29f7494
zeroconf: fix traceback under py3
Kim Alvefur <zash@zash.se>
parents:
43077
diff
changeset
|
1195 item, |
0f82b29f7494
zeroconf: fix traceback under py3
Kim Alvefur <zash@zash.se>
parents:
43077
diff
changeset
|
1196 ) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1197 ) |
28298 | 1198 self.text = result |
1199 else: | |
1200 self.text = properties | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1201 |
28298 | 1202 def setText(self, text): |
1203 """Sets properties and text given a text field""" | |
1204 self.text = text | |
1205 try: | |
1206 result = {} | |
1207 end = len(text) | |
1208 index = 0 | |
1209 strs = [] | |
1210 while index < end: | |
1211 length = ord(text[index]) | |
1212 index += 1 | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1213 strs.append(text[index : index + length]) |
28298 | 1214 index += length |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1215 |
28298 | 1216 for s in strs: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1217 eindex = s.find(b'=') |
28298 | 1218 if eindex == -1: |
1219 # No equals sign at all | |
1220 key = s | |
1221 value = 0 | |
1222 else: | |
1223 key = s[:eindex] | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1224 value = s[eindex + 1 :] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1225 if value == b'true': |
28298 | 1226 value = 1 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1227 elif value == b'false' or not value: |
28298 | 1228 value = 0 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1229 |
28298 | 1230 # Only update non-existent properties |
28420
d03b7800672c
zeroconf: compare singleton using is
timeless <timeless@mozdev.org>
parents:
28419
diff
changeset
|
1231 if key and result.get(key) is None: |
28298 | 1232 result[key] = value |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1233 |
28298 | 1234 self.properties = result |
1235 except Exception: | |
1236 traceback.print_exc() | |
1237 self.properties = None | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1238 |
28298 | 1239 def getType(self): |
1240 """Type accessor""" | |
1241 return self.type | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1242 |
28298 | 1243 def getName(self): |
1244 """Name accessor""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1245 if self.type is not None and self.name.endswith(b"." + self.type): |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1246 return self.name[: len(self.name) - len(self.type) - 1] |
28298 | 1247 return self.name |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1248 |
28298 | 1249 def getAddress(self): |
1250 """Address accessor""" | |
1251 return self.address | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1252 |
28298 | 1253 def getPort(self): |
1254 """Port accessor""" | |
1255 return self.port | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1256 |
28298 | 1257 def getPriority(self): |
1258 """Priority accessor""" | |
1259 return self.priority | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1260 |
28298 | 1261 def getWeight(self): |
1262 """Weight accessor""" | |
1263 return self.weight | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1264 |
28298 | 1265 def getProperties(self): |
1266 """Properties accessor""" | |
1267 return self.properties | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1268 |
28298 | 1269 def getText(self): |
1270 """Text accessor""" | |
1271 return self.text | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1272 |
28298 | 1273 def getServer(self): |
1274 """Server accessor""" | |
1275 return self.server | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1276 |
28298 | 1277 def updateRecord(self, zeroconf, now, record): |
1278 """Updates service information from a DNS record""" | |
1279 if record is not None and not record.isExpired(now): | |
1280 if record.type == _TYPE_A: | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1281 # if record.name == self.name: |
28298 | 1282 if record.name == self.server: |
1283 self.address = record.address | |
1284 elif record.type == _TYPE_SRV: | |
1285 if record.name == self.name: | |
1286 self.server = record.server | |
1287 self.port = record.port | |
1288 self.weight = record.weight | |
1289 self.priority = record.priority | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1290 # self.address = None |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1291 self.updateRecord( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1292 zeroconf, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1293 now, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1294 zeroconf.cache.getByDetails( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1295 self.server, _TYPE_A, _CLASS_IN |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1296 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1297 ) |
28298 | 1298 elif record.type == _TYPE_TXT: |
1299 if record.name == self.name: | |
1300 self.setText(record.text) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1301 |
28298 | 1302 def request(self, zeroconf, timeout): |
1303 """Returns true if the service could be discovered on the | |
1304 network, and updates this object with details discovered. | |
1305 """ | |
1306 now = currentTimeMillis() | |
1307 delay = _LISTENER_TIME | |
1308 next = now + delay | |
1309 last = now + timeout | |
51701
97d013f48cae
zeroconf: fix boolean return value
Raphaël Gomès <rgomes@octobus.net>
parents:
51700
diff
changeset
|
1310 result = False |
28298 | 1311 try: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1312 zeroconf.addListener( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1313 self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1314 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1315 while ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1316 self.server is None or self.address is None or self.text is None |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1317 ): |
28298 | 1318 if last <= now: |
1319 return 0 | |
1320 if next <= now: | |
1321 out = DNSOutgoing(_FLAGS_QR_QUERY) | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1322 out.addQuestion( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1323 DNSQuestion(self.name, _TYPE_SRV, _CLASS_IN) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1324 ) |
28299 | 1325 out.addAnswerAtTime( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1326 zeroconf.cache.getByDetails( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1327 self.name, _TYPE_SRV, _CLASS_IN |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1328 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1329 now, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1330 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1331 out.addQuestion( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1332 DNSQuestion(self.name, _TYPE_TXT, _CLASS_IN) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1333 ) |
28299 | 1334 out.addAnswerAtTime( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1335 zeroconf.cache.getByDetails( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1336 self.name, _TYPE_TXT, _CLASS_IN |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1337 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1338 now, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1339 ) |
28298 | 1340 if self.server is not None: |
28299 | 1341 out.addQuestion( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1342 DNSQuestion(self.server, _TYPE_A, _CLASS_IN) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1343 ) |
28299 | 1344 out.addAnswerAtTime( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1345 zeroconf.cache.getByDetails( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1346 self.server, _TYPE_A, _CLASS_IN |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1347 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1348 now, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1349 ) |
28298 | 1350 zeroconf.send(out) |
1351 next = now + delay | |
1352 delay = delay * 2 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1353 |
28298 | 1354 zeroconf.wait(min(next, last) - now) |
1355 now = currentTimeMillis() | |
51701
97d013f48cae
zeroconf: fix boolean return value
Raphaël Gomès <rgomes@octobus.net>
parents:
51700
diff
changeset
|
1356 result = True |
28298 | 1357 finally: |
1358 zeroconf.removeListener(self) | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1359 |
28298 | 1360 return result |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1361 |
28298 | 1362 def __eq__(self, other): |
1363 """Tests equality of service name""" | |
1364 if isinstance(other, ServiceInfo): | |
1365 return other.name == self.name | |
1366 return 0 | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1367 |
28298 | 1368 def __ne__(self, other): |
1369 """Non-equality test""" | |
1370 return not self.__eq__(other) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1371 |
28298 | 1372 def __repr__(self): |
1373 """String representation""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1374 result = b"service[%s,%s:%s," % ( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1375 self.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1376 socket.inet_ntoa(self.getAddress()), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1377 self.port, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1378 ) |
28298 | 1379 if self.text is None: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1380 result += b"None" |
28298 | 1381 else: |
1382 if len(self.text) < 20: | |
1383 result += self.text | |
1384 else: | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1385 result += self.text[:17] + b"..." |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1386 result += b"]" |
28298 | 1387 return result |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1388 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1389 |
48946
642e31cb55f0
py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents:
48875
diff
changeset
|
1390 class Zeroconf: |
28298 | 1391 """Implementation of Zeroconf Multicast DNS Service Discovery |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1392 |
28298 | 1393 Supports registration, unregistration, queries and browsing. |
1394 """ | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1395 |
28298 | 1396 def __init__(self, bindaddress=None): |
1397 """Creates an instance of the Zeroconf class, establishing | |
1398 multicast communications, listening and reaping threads.""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1399 globals()[b'_GLOBAL_DONE'] = 0 |
28298 | 1400 if bindaddress is None: |
1401 self.intf = socket.gethostbyname(socket.gethostname()) | |
1402 else: | |
1403 self.intf = bindaddress | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1404 self.group = (b'', _MDNS_PORT) |
28298 | 1405 self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
1406 try: | |
1407 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
1408 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) | |
1409 except Exception: | |
1410 # SO_REUSEADDR should be equivalent to SO_REUSEPORT for | |
1411 # multicast UDP sockets (p 731, "TCP/IP Illustrated, | |
1412 # Volume 2"), but some BSD-derived systems require | |
1413 # SO_REUSEPORT to be specified explicitly. Also, not all | |
1414 # versions of Python have SO_REUSEPORT available. So | |
1415 # if you're on a BSD-based system, and haven't upgraded | |
1416 # to Python 2.3 yet, you may find this library doesn't | |
1417 # work as expected. | |
1418 # | |
1419 pass | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1420 self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, b"\xff") |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1421 self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, b"\x01") |
28298 | 1422 try: |
1423 self.socket.bind(self.group) | |
1424 except Exception: | |
1425 # Some versions of linux raise an exception even though | |
1426 # SO_REUSEADDR and SO_REUSEPORT have been set, so ignore it | |
1427 pass | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1428 self.socket.setsockopt( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1429 socket.SOL_IP, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1430 socket.IP_ADD_MEMBERSHIP, |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43077
diff
changeset
|
1431 socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1432 ) |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1433 |
28298 | 1434 self.listeners = [] |
1435 self.browsers = [] | |
1436 self.services = {} | |
1437 self.servicetypes = {} | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1438 |
28298 | 1439 self.cache = DNSCache() |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1440 |
28298 | 1441 self.condition = threading.Condition() |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1442 |
28298 | 1443 self.engine = Engine(self) |
1444 self.listener = Listener(self) | |
1445 self.reaper = Reaper(self) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1446 |
28298 | 1447 def isLoopback(self): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1448 return self.intf.startswith(b"127.0.0.1") |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1449 |
28298 | 1450 def isLinklocal(self): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1451 return self.intf.startswith(b"169.254.") |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1452 |
28298 | 1453 def wait(self, timeout): |
1454 """Calling thread waits for a given number of milliseconds or | |
1455 until notified.""" | |
1456 self.condition.acquire() | |
28300
15c5f50e7e13
zeroconf: add whitespace around operator
timeless <timeless@mozdev.org>
parents:
28299
diff
changeset
|
1457 self.condition.wait(timeout / 1000) |
28298 | 1458 self.condition.release() |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1459 |
28298 | 1460 def notifyAll(self): |
1461 """Notifies all waiting threads""" | |
1462 self.condition.acquire() | |
49053
7d1daa1ef286
zeroconf: fix deprecation warning with python 3.10
Julien Cristau <jcristau@debian.org>
parents:
48946
diff
changeset
|
1463 self.condition.notify_all() |
28298 | 1464 self.condition.release() |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1465 |
28298 | 1466 def getServiceInfo(self, type, name, timeout=3000): |
1467 """Returns network's service information for a particular | |
1468 name and type, or None if no service matches by the timeout, | |
1469 which defaults to 3 seconds.""" | |
1470 info = ServiceInfo(type, name) | |
1471 if info.request(self, timeout): | |
1472 return info | |
1473 return None | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1474 |
28298 | 1475 def addServiceListener(self, type, listener): |
1476 """Adds a listener for a particular service type. This object | |
1477 will then have its updateRecord method called when information | |
1478 arrives for that type.""" | |
1479 self.removeServiceListener(listener) | |
1480 self.browsers.append(ServiceBrowser(self, type, listener)) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1481 |
28298 | 1482 def removeServiceListener(self, listener): |
1483 """Removes a listener from the set that is currently listening.""" | |
1484 for browser in self.browsers: | |
1485 if browser.listener == listener: | |
1486 browser.cancel() | |
28301
fd8a4d2d6541
zeroconf: del is not a function
timeless <timeless@mozdev.org>
parents:
28300
diff
changeset
|
1487 del browser |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1488 |
28298 | 1489 def registerService(self, info, ttl=_DNS_TTL): |
1490 """Registers service information to the network with a default TTL | |
1491 of 60 seconds. Zeroconf will then respond to requests for | |
1492 information for that service. The name of the service may be | |
1493 changed if needed to make it unique on the network.""" | |
1494 self.checkService(info) | |
1495 self.services[info.name.lower()] = info | |
28421
6d72cc613fc4
zeroconf: replace has_key with in
timeless <timeless@mozdev.org>
parents:
28420
diff
changeset
|
1496 if info.type in self.servicetypes: |
28300
15c5f50e7e13
zeroconf: add whitespace around operator
timeless <timeless@mozdev.org>
parents:
28299
diff
changeset
|
1497 self.servicetypes[info.type] += 1 |
28298 | 1498 else: |
28300
15c5f50e7e13
zeroconf: add whitespace around operator
timeless <timeless@mozdev.org>
parents:
28299
diff
changeset
|
1499 self.servicetypes[info.type] = 1 |
28298 | 1500 now = currentTimeMillis() |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1501 nexttime = now |
28298 | 1502 i = 0 |
1503 while i < 3: | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1504 if now < nexttime: |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1505 self.wait(nexttime - now) |
28298 | 1506 now = currentTimeMillis() |
1507 continue | |
1508 out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1509 out.addAnswerAtTime( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1510 DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, ttl, info.name), 0 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1511 ) |
28299 | 1512 out.addAnswerAtTime( |
1513 DNSService( | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1514 info.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1515 _TYPE_SRV, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1516 _CLASS_IN, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1517 ttl, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1518 info.priority, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1519 info.weight, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1520 info.port, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1521 info.server, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1522 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1523 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1524 ) |
28299 | 1525 out.addAnswerAtTime( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1526 DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1527 ) |
28298 | 1528 if info.address: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1529 out.addAnswerAtTime( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1530 DNSAddress( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1531 info.server, _TYPE_A, _CLASS_IN, ttl, info.address |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1532 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1533 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1534 ) |
28298 | 1535 self.send(out) |
1536 i += 1 | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1537 nexttime += _REGISTER_TIME |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1538 |
28298 | 1539 def unregisterService(self, info): |
1540 """Unregister a service.""" | |
1541 try: | |
28301
fd8a4d2d6541
zeroconf: del is not a function
timeless <timeless@mozdev.org>
parents:
28300
diff
changeset
|
1542 del self.services[info.name.lower()] |
28300
15c5f50e7e13
zeroconf: add whitespace around operator
timeless <timeless@mozdev.org>
parents:
28299
diff
changeset
|
1543 if self.servicetypes[info.type] > 1: |
15c5f50e7e13
zeroconf: add whitespace around operator
timeless <timeless@mozdev.org>
parents:
28299
diff
changeset
|
1544 self.servicetypes[info.type] -= 1 |
28298 | 1545 else: |
1546 del self.servicetypes[info.type] | |
1547 except KeyError: | |
1548 pass | |
1549 now = currentTimeMillis() | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1550 nexttime = now |
28298 | 1551 i = 0 |
1552 while i < 3: | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1553 if now < nexttime: |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1554 self.wait(nexttime - now) |
28298 | 1555 now = currentTimeMillis() |
1556 continue | |
1557 out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) | |
28299 | 1558 out.addAnswerAtTime( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1559 DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1560 ) |
28299 | 1561 out.addAnswerAtTime( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1562 DNSService( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1563 info.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1564 _TYPE_SRV, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1565 _CLASS_IN, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1566 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1567 info.priority, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1568 info.weight, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1569 info.port, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1570 info.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1571 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1572 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1573 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1574 out.addAnswerAtTime( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1575 DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1576 ) |
28298 | 1577 if info.address: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1578 out.addAnswerAtTime( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1579 DNSAddress( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1580 info.server, _TYPE_A, _CLASS_IN, 0, info.address |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1581 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1582 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1583 ) |
28298 | 1584 self.send(out) |
1585 i += 1 | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1586 nexttime += _UNREGISTER_TIME |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1587 |
28298 | 1588 def unregisterAllServices(self): |
1589 """Unregister all registered services.""" | |
1590 if len(self.services) > 0: | |
1591 now = currentTimeMillis() | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1592 nexttime = now |
28298 | 1593 i = 0 |
1594 while i < 3: | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1595 if now < nexttime: |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1596 self.wait(nexttime - now) |
28298 | 1597 now = currentTimeMillis() |
1598 continue | |
1599 out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) | |
1600 for info in self.services.values(): | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1601 out.addAnswerAtTime( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1602 DNSPointer( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1603 info.type, _TYPE_PTR, _CLASS_IN, 0, info.name |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1604 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1605 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1606 ) |
28299 | 1607 out.addAnswerAtTime( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1608 DNSService( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1609 info.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1610 _TYPE_SRV, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1611 _CLASS_IN, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1612 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1613 info.priority, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1614 info.weight, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1615 info.port, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1616 info.server, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1617 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1618 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1619 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1620 out.addAnswerAtTime( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1621 DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1622 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1623 ) |
28298 | 1624 if info.address: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1625 out.addAnswerAtTime( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1626 DNSAddress( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1627 info.server, _TYPE_A, _CLASS_IN, 0, info.address |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1628 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1629 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1630 ) |
28298 | 1631 self.send(out) |
1632 i += 1 | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1633 nexttime += _UNREGISTER_TIME |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1634 |
28298 | 1635 def checkService(self, info): |
1636 """Checks the network for a unique service name, modifying the | |
1637 ServiceInfo passed in if it is not unique.""" | |
1638 now = currentTimeMillis() | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1639 nexttime = now |
28298 | 1640 i = 0 |
1641 while i < 3: | |
1642 for record in self.cache.entriesWithName(info.type): | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1643 if ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1644 record.type == _TYPE_PTR |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1645 and not record.isExpired(now) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1646 and record.alias == info.name |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1647 ): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1648 if info.name.find(b'.') < 0: |
44956
ba7eda4fcf8e
zeroconf: fix non existant formatting in the vendored zeroconf module
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
43735
diff
changeset
|
1649 info.name = b"%s.[%s:%d].%s" % ( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1650 info.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1651 info.address, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1652 info.port, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1653 info.type, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1654 ) |
28298 | 1655 self.checkService(info) |
1656 return | |
1657 raise NonUniqueNameException | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1658 if now < nexttime: |
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1659 self.wait(nexttime - now) |
28298 | 1660 now = currentTimeMillis() |
1661 continue | |
1662 out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA) | |
1663 self.debug = out | |
1664 out.addQuestion(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN)) | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1665 out.addAuthoritativeAnswer( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1666 DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, info.name) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1667 ) |
28298 | 1668 self.send(out) |
1669 i += 1 | |
28419
eb9d0e828c30
zeroconf: remove camelcase in identifiers
timeless <timeless@mozdev.org>
parents:
28302
diff
changeset
|
1670 nexttime += _CHECK_TIME |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1671 |
28298 | 1672 def addListener(self, listener, question): |
1673 """Adds a listener for a given question. The listener will have | |
1674 its updateRecord method called when information is available to | |
1675 answer the question.""" | |
1676 now = currentTimeMillis() | |
1677 self.listeners.append(listener) | |
1678 if question is not None: | |
1679 for record in self.cache.entriesWithName(question.name): | |
1680 if question.answeredBy(record) and not record.isExpired(now): | |
1681 listener.updateRecord(self, now, record) | |
1682 self.notifyAll() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1683 |
28298 | 1684 def removeListener(self, listener): |
1685 """Removes a listener.""" | |
1686 try: | |
1687 self.listeners.remove(listener) | |
1688 self.notifyAll() | |
1689 except Exception: | |
1690 pass | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1691 |
28298 | 1692 def updateRecord(self, now, rec): |
1693 """Used to notify listeners of new information that has updated | |
1694 a record.""" | |
1695 for listener in self.listeners: | |
1696 listener.updateRecord(self, now, rec) | |
1697 self.notifyAll() | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1698 |
28298 | 1699 def handleResponse(self, msg): |
1700 """Deal with incoming response packets. All answers | |
1701 are held in the cache, and listeners are notified.""" | |
1702 now = currentTimeMillis() | |
1703 for record in msg.answers: | |
1704 expired = record.isExpired(now) | |
1705 if record in self.cache.entries(): | |
1706 if expired: | |
1707 self.cache.remove(record) | |
1708 else: | |
1709 entry = self.cache.get(record) | |
1710 if entry is not None: | |
1711 entry.resetTTL(record) | |
1712 record = entry | |
1713 else: | |
1714 self.cache.add(record) | |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1715 |
28298 | 1716 self.updateRecord(now, record) |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1717 |
28298 | 1718 def handleQuery(self, msg, addr, port): |
1719 """Deal with incoming query packets. Provides a response if | |
1720 possible.""" | |
1721 out = None | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1722 |
28298 | 1723 # Support unicast client responses |
1724 # | |
1725 if port != _MDNS_PORT: | |
1726 out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, 0) | |
1727 for question in msg.questions: | |
1728 out.addQuestion(question) | |
7874
d812029cda85
cleanup: drop variables for unused return values
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7071
diff
changeset
|
1729 |
28298 | 1730 for question in msg.questions: |
1731 if question.type == _TYPE_PTR: | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1732 if question.name == b"_services._dns-sd._udp.local.": |
28298 | 1733 for stype in self.servicetypes.keys(): |
1734 if out is None: | |
1735 out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1736 out.addAnswer( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1737 msg, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1738 DNSPointer( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1739 b"_services._dns-sd._udp.local.", |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1740 _TYPE_PTR, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1741 _CLASS_IN, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1742 _DNS_TTL, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1743 stype, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1744 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1745 ) |
28298 | 1746 for service in self.services.values(): |
1747 if question.name == service.type: | |
1748 if out is None: | |
1749 out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1750 out.addAnswer( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1751 msg, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1752 DNSPointer( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1753 service.type, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1754 _TYPE_PTR, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1755 _CLASS_IN, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1756 _DNS_TTL, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1757 service.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1758 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1759 ) |
28298 | 1760 else: |
1761 try: | |
1762 if out is None: | |
1763 out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA) | |
7874
d812029cda85
cleanup: drop variables for unused return values
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7071
diff
changeset
|
1764 |
28298 | 1765 # Answer A record queries for any service addresses we know |
1766 if question.type == _TYPE_A or question.type == _TYPE_ANY: | |
1767 for service in self.services.values(): | |
1768 if service.server == question.name.lower(): | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1769 out.addAnswer( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1770 msg, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1771 DNSAddress( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1772 question.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1773 _TYPE_A, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1774 _CLASS_IN | _CLASS_UNIQUE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1775 _DNS_TTL, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1776 service.address, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1777 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1778 ) |
7874
d812029cda85
cleanup: drop variables for unused return values
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7071
diff
changeset
|
1779 |
28298 | 1780 service = self.services.get(question.name.lower(), None) |
35629
31451f3f4b56
style: remove multiple statement on a single line in zeroconf
Boris Feld <boris.feld@octobus.net>
parents:
34447
diff
changeset
|
1781 if not service: |
31451f3f4b56
style: remove multiple statement on a single line in zeroconf
Boris Feld <boris.feld@octobus.net>
parents:
34447
diff
changeset
|
1782 continue |
7874
d812029cda85
cleanup: drop variables for unused return values
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7071
diff
changeset
|
1783 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1784 if question.type == _TYPE_SRV or question.type == _TYPE_ANY: |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1785 out.addAnswer( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1786 msg, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1787 DNSService( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1788 question.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1789 _TYPE_SRV, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1790 _CLASS_IN | _CLASS_UNIQUE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1791 _DNS_TTL, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1792 service.priority, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1793 service.weight, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1794 service.port, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1795 service.server, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1796 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1797 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1798 if question.type == _TYPE_TXT or question.type == _TYPE_ANY: |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1799 out.addAnswer( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1800 msg, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1801 DNSText( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1802 question.name, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1803 _TYPE_TXT, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1804 _CLASS_IN | _CLASS_UNIQUE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1805 _DNS_TTL, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1806 service.text, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1807 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1808 ) |
28298 | 1809 if question.type == _TYPE_SRV: |
28299 | 1810 out.addAdditionalAnswer( |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1811 DNSAddress( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1812 service.server, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1813 _TYPE_A, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1814 _CLASS_IN | _CLASS_UNIQUE, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1815 _DNS_TTL, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1816 service.address, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1817 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1818 ) |
28298 | 1819 except Exception: |
1820 traceback.print_exc() | |
7874
d812029cda85
cleanup: drop variables for unused return values
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7071
diff
changeset
|
1821 |
28298 | 1822 if out is not None and out.answers: |
1823 out.id = msg.id | |
1824 self.send(out, addr, port) | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1825 |
28302
e96a3ae025ed
zeroconf: remove whitespace around = for named parameters
timeless <timeless@mozdev.org>
parents:
28301
diff
changeset
|
1826 def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT): |
28298 | 1827 """Sends an outgoing packet.""" |
1828 # This is a quick test to see if we can parse the packets we generate | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1829 # temp = DNSIncoming(out.packet()) |
28298 | 1830 try: |
1831 self.socket.sendto(out.packet(), 0, (addr, port)) | |
1832 except Exception: | |
1833 # Ignore this, it may be a temporary loss of network connection | |
1834 pass | |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1835 |
28298 | 1836 def close(self): |
1837 """Ends the background threads, and prevent this instance from | |
1838 servicing further queries.""" | |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1839 if globals()[b'_GLOBAL_DONE'] == 0: |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1840 globals()[b'_GLOBAL_DONE'] = 1 |
28298 | 1841 self.notifyAll() |
1842 self.engine.notify() | |
1843 self.unregisterAllServices() | |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1844 self.socket.setsockopt( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1845 socket.SOL_IP, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1846 socket.IP_DROP_MEMBERSHIP, |
43506
9f70512ae2cf
cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents:
43077
diff
changeset
|
1847 socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1848 ) |
28298 | 1849 self.socket.close() |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1850 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1851 |
7071
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1852 # Test a few module features, including service registration, service |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1853 # query (for Zoe), and service unregistration. |
643c751e60b2
zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1854 |
7877
eba7f12b0c51
cleanup: whitespace cleanup
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
7874
diff
changeset
|
1855 if __name__ == '__main__': |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1856 print(b"Multicast DNS Service Discovery for Python, version", __version__) |
28298 | 1857 r = Zeroconf() |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1858 print(b"1. Testing registration of a service...") |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1859 desc = {b'version': b'0.10', b'a': b'test value', b'b': b'another value'} |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1860 info = ServiceInfo( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1861 b"_http._tcp.local.", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1862 b"My Service Name._http._tcp.local.", |
51167
b79f13d6ef25
zeroconf: give inet_aton() str instead of bytes
Anton Shestakov <av6@dwimlabs.net>
parents:
49293
diff
changeset
|
1863 socket.inet_aton("127.0.0.1"), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1864 1234, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1865 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1866 0, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1867 desc, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1868 ) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1869 print(b" Registering service...") |
28298 | 1870 r.registerService(info) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1871 print(b" Registration done.") |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1872 print(b"2. Testing query of service information...") |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1873 print( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1874 b" Getting ZOE service:", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1875 str(r.getServiceInfo(b"_http._tcp.local.", b"ZOE._http._tcp.local.")), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1876 ) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1877 print(b" Query done.") |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1878 print(b"3. Testing query of own service...") |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1879 print( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1880 b" Getting self:", |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1881 str( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1882 r.getServiceInfo( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1883 b"_http._tcp.local.", b"My Service Name._http._tcp.local." |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1884 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1885 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
42551
diff
changeset
|
1886 ) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1887 print(b" Query done.") |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1888 print(b"4. Testing unregister of service information...") |
28298 | 1889 r.unregisterService(info) |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
1890 print(b" Unregister done.") |
28298 | 1891 r.close() |