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