Mercurial > hg
comparison hgext/zeroconf/Zeroconf.py @ 41519:a36f462cf533
zeroconf: Python 3 porting of vendored library
A quick glance through this module reveals that most of it "just works"
on Python 3 with the source transformer active. There are a few
places where we need to ensure we're using str.
Differential Revision: https://phab.mercurial-scm.org/D5804
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 02 Feb 2019 11:49:26 -0800 |
parents | 876494fd967d |
children | 683aeef12830 |
comparison
equal
deleted
inserted
replaced
41518:530d211ae9a8 | 41519:a36f462cf533 |
---|---|
82 | 82 |
83 import errno | 83 import errno |
84 import itertools | 84 import itertools |
85 import select | 85 import select |
86 import socket | 86 import socket |
87 import string | |
88 import struct | 87 import struct |
89 import threading | 88 import threading |
90 import time | 89 import time |
91 import traceback | 90 import traceback |
92 | 91 |
104 _LISTENER_TIME = 200 | 103 _LISTENER_TIME = 200 |
105 _BROWSER_TIME = 500 | 104 _BROWSER_TIME = 500 |
106 | 105 |
107 # Some DNS constants | 106 # Some DNS constants |
108 | 107 |
109 _MDNS_ADDR = '224.0.0.251' | 108 _MDNS_ADDR = r'224.0.0.251' |
110 _MDNS_PORT = 5353 | 109 _MDNS_PORT = 5353 |
111 _DNS_PORT = 53 | 110 _DNS_PORT = 53 |
112 _DNS_TTL = 60 * 60 # one hour default TTL | 111 _DNS_TTL = 60 * 60 # one hour default TTL |
113 | 112 |
114 _MAX_MSG_TYPICAL = 1460 # unused | 113 _MAX_MSG_TYPICAL = 1460 # unused |
219 | 218 |
220 class DNSEntry(object): | 219 class DNSEntry(object): |
221 """A DNS entry""" | 220 """A DNS entry""" |
222 | 221 |
223 def __init__(self, name, type, clazz): | 222 def __init__(self, name, type, clazz): |
224 self.key = string.lower(name) | 223 self.key = name.lower() |
225 self.name = name | 224 self.name = name |
226 self.type = type | 225 self.type = type |
227 self.clazz = clazz & _CLASS_MASK | 226 self.clazz = clazz & _CLASS_MASK |
228 self.unique = (clazz & _CLASS_UNIQUE) != 0 | 227 self.unique = (clazz & _CLASS_UNIQUE) != 0 |
229 | 228 |
618 off = self.offset | 617 off = self.offset |
619 next = -1 | 618 next = -1 |
620 first = off | 619 first = off |
621 | 620 |
622 while True: | 621 while True: |
623 len = ord(self.data[off]) | 622 len = ord(self.data[off:off + 1]) |
624 off += 1 | 623 off += 1 |
625 if len == 0: | 624 if len == 0: |
626 break | 625 break |
627 t = len & 0xC0 | 626 t = len & 0xC0 |
628 if t == 0x00: | 627 if t == 0x00: |
629 result = ''.join((result, self.readUTF(off, len) + '.')) | 628 result = ''.join((result, self.readUTF(off, len) + '.')) |
630 off += len | 629 off += len |
631 elif t == 0xC0: | 630 elif t == 0xC0: |
632 if next < 0: | 631 if next < 0: |
633 next = off + 1 | 632 next = off + 1 |
634 off = ((len & 0x3F) << 8) | ord(self.data[off]) | 633 off = ((len & 0x3F) << 8) | ord(self.data[off:off + 1]) |
635 if off >= first: | 634 if off >= first: |
636 raise BadDomainNameCircular(off) | 635 raise BadDomainNameCircular(off) |
637 first = off | 636 first = off |
638 else: | 637 else: |
639 raise BadDomainName(off) | 638 raise BadDomainName(off) |
1331 except Exception: | 1330 except Exception: |
1332 # Some versions of linux raise an exception even though | 1331 # Some versions of linux raise an exception even though |
1333 # SO_REUSEADDR and SO_REUSEPORT have been set, so ignore it | 1332 # SO_REUSEADDR and SO_REUSEPORT have been set, so ignore it |
1334 pass | 1333 pass |
1335 self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, | 1334 self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, |
1336 socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) | 1335 socket.inet_aton(_MDNS_ADDR) + socket.inet_aton(r'0.0.0.0')) |
1337 | 1336 |
1338 self.listeners = [] | 1337 self.listeners = [] |
1339 self.browsers = [] | 1338 self.browsers = [] |
1340 self.services = {} | 1339 self.services = {} |
1341 self.servicetypes = {} | 1340 self.servicetypes = {} |
1655 globals()['_GLOBAL_DONE'] = 1 | 1654 globals()['_GLOBAL_DONE'] = 1 |
1656 self.notifyAll() | 1655 self.notifyAll() |
1657 self.engine.notify() | 1656 self.engine.notify() |
1658 self.unregisterAllServices() | 1657 self.unregisterAllServices() |
1659 self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, | 1658 self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, |
1660 socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0')) | 1659 socket.inet_aton(_MDNS_ADDR) + socket.inet_aton(r'0.0.0.0')) |
1661 self.socket.close() | 1660 self.socket.close() |
1662 | 1661 |
1663 # Test a few module features, including service registration, service | 1662 # Test a few module features, including service registration, service |
1664 # query (for Zoe), and service unregistration. | 1663 # query (for Zoe), and service unregistration. |
1665 | 1664 |