util/Zeroconf.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fri, 22 Sep 2017 18:06:15 +0300
changeset 1829 a776ac02b079
parent 1828 396da88d7b5c
permissions -rw-r--r--
remove reimported modules
1783
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
     1
#  Multicast DNS Service Discovery for Python, v0.12
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
     2
#     Copyright (C) 2003, Paul Scott-Murphy
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
     3
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
     4
#     This module provides a framework for the use of DNS Service Discovery
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
     5
#     using IP multicast.  It has been tested against the JRendezvous
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
     6
#     implementation from <a href="http://strangeberry.com">StrangeBerry</a>,
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
     7
#     and against the mDNSResponder from Mac OS X 10.3.8.
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
     8
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
     9
#     This library is free software; you can redistribute it and/or
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    10
#     modify it under the terms of the GNU Lesser General Public
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    11
#     License as published by the Free Software Foundation; either
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    12
#     version 2.1 of the License, or (at your option) any later version.
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    13
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    14
#     This library is distributed in the hope that it will be useful,
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    15
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    16
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    17
#     Lesser General Public License for more details.
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    18
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    19
#     You should have received a copy of the GNU Lesser General Public
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    20
#     License along with this library; if not, write to the Free Software
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    21
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    22
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    23
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    24
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    25
# 0.12 update - allow selection of binding interface
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    26
#          typo fix - Thanks A. M. Kuchlingi
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    27
#          removed all use of word 'Rendezvous' - this is an API change
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    28
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    29
# 0.11 update - correction to comments for addListener method
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    30
#                  support for new record types seen from OS X
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    31
#                   - IPv6 address
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    32
#                   - hostinfo
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    33
#                  ignore unknown DNS record types
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    34
#                  fixes to name decoding
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    35
#                  works alongside other processes using port 5353 (e.g. on Mac OS X)
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    36
#                  tested against Mac OS X 10.3.2's mDNSResponder
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    37
#                  corrections to removal of list entries for service browser
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    38
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    39
# 0.10 update - Jonathon Paisley contributed these corrections:
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    40
#                  always multicast replies, even when query is unicast
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    41
#                  correct a pointer encoding problem
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    42
#                  can now write records in any order
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    43
#                  traceback shown on failure
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    44
#                  better TXT record parsing
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    45
#                  server is now separate from name
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    46
#                  can cancel a service browser
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    47
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    48
#                  modified some unit tests to accommodate these changes
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    49
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    50
# 0.09 update - remove all records on service unregistration
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    51
#                  fix DOS security problem with readName
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    52
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    53
# 0.08 update - changed licensing to LGPL
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    54
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    55
# 0.07 update - faster shutdown on engine
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    56
#                  pointer encoding of outgoing names
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    57
#                  ServiceBrowser now works
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    58
#                  new unit tests
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    59
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    60
# 0.06 update - small improvements with unit tests
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    61
#                  added defined exception types
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    62
#                  new style objects
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    63
#                  fixed hostname/interface problem
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    64
#                  fixed socket timeout problem
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    65
#                  fixed addServiceListener() typo bug
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    66
#                  using select() for socket reads
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    67
#                  tested on Debian unstable with Python 2.2.2
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    68
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    69
# 0.05 update - ensure case insensitivty on domain names
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    70
#                  support for unicast DNS queries
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    71
#
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    72
# 0.04 update - added some unit tests
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    73
#                  added __ne__ adjuncts where required
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    74
#                  ensure names end in '.local.'
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    75
#                  timeout on receiving socket for clean shutdown
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    76
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    77
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
    78
from __future__ import print_function
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    79
import string
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    80
import time
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    81
import struct
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    82
import socket
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    83
import threading
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    84
import select
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    85
import traceback
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    86
1783
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    87
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    88
__author__ = "Paul Scott-Murphy"
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    89
__email__ = "paul at scott dash murphy dot com"
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    90
__version__ = "0.12"
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    91
3311eea28d56 clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1782
diff changeset
    92
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    93
__all__ = ["Zeroconf", "ServiceInfo", "ServiceBrowser"]
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    94
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    95
# hook for threads
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    96
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    97
globals()['_GLOBAL_DONE'] = 0
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    98
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    99
# Some timing constants
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   100
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   101
_UNREGISTER_TIME = 125
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   102
_CHECK_TIME = 175
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   103
_REGISTER_TIME = 225
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   104
_LISTENER_TIME = 200
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   105
_BROWSER_TIME = 500
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   106
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   107
# Some DNS constants
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   108
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   109
_MDNS_ADDR = '224.0.0.251'
1752
d14ff9d7eb76 clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
   110
_MDNS_PORT = 5353
d14ff9d7eb76 clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
   111
_DNS_PORT = 53
d14ff9d7eb76 clean-up: fix PEP8 E703 statement ends with a semicolon
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
   112
_DNS_TTL = 60 * 60  # one hour default TTL
1737
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   113
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   114
_MAX_MSG_TYPICAL = 1460  # unused
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   115
_MAX_MSG_ABSOLUTE = 8972
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   116
1737
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   117
_FLAGS_QR_MASK = 0x8000  # query response mask
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   118
_FLAGS_QR_QUERY = 0x0000  # query
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   119
_FLAGS_QR_RESPONSE = 0x8000  # response
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   120
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   121
_FLAGS_AA = 0x0400  # Authorative answer
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   122
_FLAGS_TC = 0x0200  # Truncated
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   123
_FLAGS_RD = 0x0100  # Recursion desired
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   124
_FLAGS_RA = 0x8000  # Recursion available
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   125
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   126
_FLAGS_Z = 0x0040   # Zero
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   127
_FLAGS_AD = 0x0020  # Authentic data
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   128
_FLAGS_CD = 0x0010  # Checking disabled
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   129
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   130
_CLASS_IN = 1
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   131
_CLASS_CS = 2
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   132
_CLASS_CH = 3
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   133
_CLASS_HS = 4
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   134
_CLASS_NONE = 254
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   135
_CLASS_ANY = 255
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   136
_CLASS_MASK = 0x7FFF
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   137
_CLASS_UNIQUE = 0x8000
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   138
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   139
_TYPE_A = 1
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   140
_TYPE_NS = 2
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   141
_TYPE_MD = 3
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   142
_TYPE_MF = 4
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   143
_TYPE_CNAME = 5
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   144
_TYPE_SOA = 6
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   145
_TYPE_MB = 7
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   146
_TYPE_MG = 8
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   147
_TYPE_MR = 9
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   148
_TYPE_NULL = 10
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   149
_TYPE_WKS = 11
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   150
_TYPE_PTR = 12
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   151
_TYPE_HINFO = 13
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   152
_TYPE_MINFO = 14
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   153
_TYPE_MX = 15
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   154
_TYPE_TXT = 16
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   155
_TYPE_AAAA = 28
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   156
_TYPE_SRV = 33
1758
845ca626db09 clean-up: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1756
diff changeset
   157
_TYPE_ANY = 255
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   158
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   159
# Mapping constants to names
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   160
1747
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   161
_CLASSES = {
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   162
    _CLASS_IN:   "in",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   163
    _CLASS_CS:   "cs",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   164
    _CLASS_CH:   "ch",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   165
    _CLASS_HS:   "hs",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   166
    _CLASS_NONE: "none",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   167
    _CLASS_ANY:  "any"
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   168
}
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   169
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   170
_TYPES = {
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   171
    _TYPE_A:     "a",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   172
    _TYPE_NS:    "ns",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   173
    _TYPE_MD:    "md",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   174
    _TYPE_MF:    "mf",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   175
    _TYPE_CNAME: "cname",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   176
    _TYPE_SOA:   "soa",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   177
    _TYPE_MB:    "mb",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   178
    _TYPE_MG:    "mg",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   179
    _TYPE_MR:    "mr",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   180
    _TYPE_NULL:  "null",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   181
    _TYPE_WKS:   "wks",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   182
    _TYPE_PTR:   "ptr",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   183
    _TYPE_HINFO: "hinfo",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   184
    _TYPE_MINFO: "minfo",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   185
    _TYPE_MX:    "mx",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   186
    _TYPE_TXT:   "txt",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   187
    _TYPE_AAAA:  "quada",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   188
    _TYPE_SRV:   "srv",
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   189
    _TYPE_ANY:   "any"
6046ffa2280f clean-up: fix PEP8 E201 whitespace after '{'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1746
diff changeset
   190
}
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   191
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   192
# utility functions
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   193
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   194
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   195
def currentTimeMillis():
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   196
    """Current system time in milliseconds"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   197
    return time.time() * 1000
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   198
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   199
# Exceptions
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   200
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   201
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   202
class NonLocalNameException(Exception):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   203
    pass
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   204
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   205
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   206
class NonUniqueNameException(Exception):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   207
    pass
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   208
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   209
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   210
class NamePartTooLongException(Exception):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   211
    pass
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   212
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   213
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   214
class AbstractMethodException(Exception):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   215
    pass
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   216
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   217
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   218
class BadTypeInNameException(Exception):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   219
    pass
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   220
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   221
# implementation classes
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   222
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   223
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   224
class DNSEntry(object):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   225
    """A DNS entry"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   226
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   227
    def __init__(self, name, type, clazz):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   228
        self.key = string.lower(name)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   229
        self.name = name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   230
        self.type = type
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   231
        self.clazz = clazz & _CLASS_MASK
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   232
        self.unique = (clazz & _CLASS_UNIQUE) != 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   233
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   234
    def __eq__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   235
        """Equality test on name, type, and class"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   236
        if isinstance(other, DNSEntry):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   237
            return self.name == other.name and self.type == other.type and self.clazz == other.clazz
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   238
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   239
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   240
    def __ne__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   241
        """Non-equality test"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   242
        return not self.__eq__(other)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   243
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   244
    def getClazz(self, clazz):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   245
        """Class accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   246
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   247
            return _CLASSES[clazz]
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   248
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   249
            return "?(%s)" % (clazz)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   250
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   251
    def getType(self, type):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   252
        """Type accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   253
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   254
            return _TYPES[type]
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   255
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   256
            return "?(%s)" % (type)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   257
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   258
    def toString(self, hdr, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   259
        """String representation with additional information"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   260
        result = "%s[%s,%s" % (hdr, self.getType(self.type), self.getClazz(self.clazz))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   261
        if self.unique:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   262
            result += "-unique,"
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   263
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   264
            result += ","
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   265
        result += self.name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   266
        if other is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   267
            result += ",%s]" % (other)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   268
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   269
            result += "]"
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   270
        return result
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   271
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   272
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   273
class DNSQuestion(DNSEntry):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   274
    """A DNS question entry"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   275
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   276
    def __init__(self, name, type, clazz):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   277
        if not name.endswith(".local."):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   278
            raise NonLocalNameException
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   279
        DNSEntry.__init__(self, name, type, clazz)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   280
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   281
    def answeredBy(self, rec):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   282
        """Returns true if the question is answered by the record"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   283
        return self.clazz == rec.clazz and (self.type == rec.type or self.type == _TYPE_ANY) and self.name == rec.name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   284
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   285
    def __repr__(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   286
        """String representation"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   287
        return DNSEntry.toString(self, "question", None)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   288
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   289
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   290
class DNSRecord(DNSEntry):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   291
    """A DNS record - like a DNS entry, but has a TTL"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   292
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   293
    def __init__(self, name, type, clazz, ttl):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   294
        DNSEntry.__init__(self, name, type, clazz)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   295
        self.ttl = ttl
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   296
        self.created = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   297
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   298
    def __eq__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   299
        """Tests equality as per DNSRecord"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   300
        if isinstance(other, DNSRecord):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   301
            return DNSEntry.__eq__(self, other)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   302
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   303
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   304
    def suppressedBy(self, msg):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   305
        """Returns true if any answer in a message can suffice for the
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   306
        information held in this record."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   307
        for record in msg.answers:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   308
            if self.suppressedByAnswer(record):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   309
                return 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   310
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   311
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   312
    def suppressedByAnswer(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   313
        """Returns true if another record has same name, type and class,
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   314
        and if its TTL is at least half of this record's."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   315
        if self == other and other.ttl > (self.ttl / 2):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   316
            return 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   317
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   318
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   319
    def getExpirationTime(self, percent):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   320
        """Returns the time at which this record will have expired
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   321
        by a certain percentage."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   322
        return self.created + (percent * self.ttl * 10)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   323
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   324
    def getRemainingTTL(self, now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   325
        """Returns the remaining TTL in seconds."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   326
        return max(0, (self.getExpirationTime(100) - now) / 1000)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   327
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   328
    def isExpired(self, now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   329
        """Returns true if this record has expired."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   330
        return self.getExpirationTime(100) <= now
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   331
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   332
    def isStale(self, now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   333
        """Returns true if this record is at least half way expired."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   334
        return self.getExpirationTime(50) <= now
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   335
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   336
    def resetTTL(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   337
        """Sets this record's TTL and created time to that of
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   338
        another record."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   339
        self.created = other.created
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   340
        self.ttl = other.ttl
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   341
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   342
    def write(self, out):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   343
        """Abstract method"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   344
        raise AbstractMethodException
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   345
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   346
    def toString(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   347
        """String representation with addtional information"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   348
        arg = "%s/%s,%s" % (self.ttl, self.getRemainingTTL(currentTimeMillis()), other)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   349
        return DNSEntry.toString(self, "record", arg)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   350
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   351
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   352
class DNSAddress(DNSRecord):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   353
    """A DNS address record"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   354
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   355
    def __init__(self, name, type, clazz, ttl, address):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   356
        DNSRecord.__init__(self, name, type, clazz, ttl)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   357
        self.address = address
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   358
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   359
    def write(self, out):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   360
        """Used in constructing an outgoing packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   361
        out.writeString(self.address, len(self.address))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   362
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   363
    def __eq__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   364
        """Tests equality on address"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   365
        if isinstance(other, DNSAddress):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   366
            return self.address == other.address
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   367
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   368
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   369
    def __repr__(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   370
        """String representation"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   371
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   372
            return socket.inet_ntoa(self.address)
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   373
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   374
            return self.address
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   375
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   376
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   377
class DNSHinfo(DNSRecord):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   378
    """A DNS host information record"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   379
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   380
    def __init__(self, name, type, clazz, ttl, cpu, os):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   381
        DNSRecord.__init__(self, name, type, clazz, ttl)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   382
        self.cpu = cpu
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   383
        self.os = os
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   384
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   385
    def write(self, out):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   386
        """Used in constructing an outgoing packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   387
        out.writeString(self.cpu, len(self.cpu))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   388
        out.writeString(self.os, len(self.os))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   389
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   390
    def __eq__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   391
        """Tests equality on cpu and os"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   392
        if isinstance(other, DNSHinfo):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   393
            return self.cpu == other.cpu and self.os == other.os
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   394
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   395
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   396
    def __repr__(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   397
        """String representation"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   398
        return self.cpu + " " + self.os
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   399
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   400
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   401
class DNSPointer(DNSRecord):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   402
    """A DNS pointer record"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   403
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   404
    def __init__(self, name, type, clazz, ttl, alias):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   405
        DNSRecord.__init__(self, name, type, clazz, ttl)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   406
        self.alias = alias
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   407
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   408
    def write(self, out):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   409
        """Used in constructing an outgoing packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   410
        out.writeName(self.alias)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   411
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   412
    def __eq__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   413
        """Tests equality on alias"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   414
        if isinstance(other, DNSPointer):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   415
            return self.alias == other.alias
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   416
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   417
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   418
    def __repr__(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   419
        """String representation"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   420
        return self.toString(self.alias)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   421
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   422
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   423
class DNSText(DNSRecord):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   424
    """A DNS text record"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   425
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   426
    def __init__(self, name, type, clazz, ttl, text):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   427
        DNSRecord.__init__(self, name, type, clazz, ttl)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   428
        self.text = text
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   429
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   430
    def write(self, out):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   431
        """Used in constructing an outgoing packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   432
        out.writeString(self.text, len(self.text))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   433
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   434
    def __eq__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   435
        """Tests equality on text"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   436
        if isinstance(other, DNSText):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   437
            return self.text == other.text
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   438
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   439
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   440
    def __repr__(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   441
        """String representation"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   442
        if len(self.text) > 10:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   443
            return self.toString(self.text[:7] + "...")
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   444
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   445
            return self.toString(self.text)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   446
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   447
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   448
class DNSService(DNSRecord):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   449
    """A DNS service record"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   450
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   451
    def __init__(self, name, type, clazz, ttl, priority, weight, port, server):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   452
        DNSRecord.__init__(self, name, type, clazz, ttl)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   453
        self.priority = priority
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   454
        self.weight = weight
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   455
        self.port = port
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   456
        self.server = server
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   457
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   458
    def write(self, out):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   459
        """Used in constructing an outgoing packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   460
        out.writeShort(self.priority)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   461
        out.writeShort(self.weight)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   462
        out.writeShort(self.port)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   463
        out.writeName(self.server)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   464
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   465
    def __eq__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   466
        """Tests equality on priority, weight, port and server"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   467
        if isinstance(other, DNSService):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   468
            return self.priority == other.priority and self.weight == other.weight and self.port == other.port and self.server == other.server
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   469
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   470
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   471
    def __repr__(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   472
        """String representation"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   473
        return self.toString("%s:%s" % (self.server, self.port))
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   474
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   475
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   476
class DNSIncoming(object):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   477
    """Object representation of an incoming DNS packet"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   478
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   479
    def __init__(self, data):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   480
        """Constructor from string holding bytes of packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   481
        self.offset = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   482
        self.data = data
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   483
        self.questions = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   484
        self.answers = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   485
        self.numQuestions = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   486
        self.numAnswers = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   487
        self.numAuthorities = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   488
        self.numAdditionals = 0
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   489
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   490
        self.readHeader()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   491
        self.readQuestions()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   492
        self.readOthers()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   493
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   494
    def readHeader(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   495
        """Reads header portion of packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   496
        format = '!HHHHHH'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   497
        length = struct.calcsize(format)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   498
        info = struct.unpack(format, self.data[self.offset:self.offset+length])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   499
        self.offset += length
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   500
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   501
        self.id = info[0]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   502
        self.flags = info[1]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   503
        self.numQuestions = info[2]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   504
        self.numAnswers = info[3]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   505
        self.numAuthorities = info[4]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   506
        self.numAdditionals = info[5]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   507
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   508
    def readQuestions(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   509
        """Reads questions section of packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   510
        format = '!HH'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   511
        length = struct.calcsize(format)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   512
        for i in range(0, self.numQuestions):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   513
            name = self.readName()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   514
            info = struct.unpack(format, self.data[self.offset:self.offset+length])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   515
            self.offset += length
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   516
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   517
            question = DNSQuestion(name, info[0], info[1])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   518
            self.questions.append(question)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   519
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   520
    def readInt(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   521
        """Reads an integer from the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   522
        format = '!I'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   523
        length = struct.calcsize(format)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   524
        info = struct.unpack(format, self.data[self.offset:self.offset+length])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   525
        self.offset += length
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   526
        return info[0]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   527
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   528
    def readCharacterString(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   529
        """Reads a character string from the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   530
        length = ord(self.data[self.offset])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   531
        self.offset += 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   532
        return self.readString(length)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   533
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   534
    def readString(self, len):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   535
        """Reads a string of a given length from the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   536
        format = '!' + str(len) + 's'
1758
845ca626db09 clean-up: fix PEP8 E222 multiple spaces after operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1756
diff changeset
   537
        length = struct.calcsize(format)
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   538
        info = struct.unpack(format, self.data[self.offset:self.offset+length])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   539
        self.offset += length
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   540
        return info[0]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   541
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   542
    def readUnsignedShort(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   543
        """Reads an unsigned short from the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   544
        format = '!H'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   545
        length = struct.calcsize(format)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   546
        info = struct.unpack(format, self.data[self.offset:self.offset+length])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   547
        self.offset += length
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   548
        return info[0]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   549
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   550
    def readOthers(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   551
        """Reads the answers, authorities and additionals section of the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   552
        format = '!HHiH'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   553
        length = struct.calcsize(format)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   554
        n = self.numAnswers + self.numAuthorities + self.numAdditionals
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   555
        for i in range(0, n):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   556
            domain = self.readName()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   557
            info = struct.unpack(format, self.data[self.offset:self.offset+length])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   558
            self.offset += length
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   559
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   560
            rec = None
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   561
            if info[0] == _TYPE_A:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   562
                rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(4))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   563
            elif info[0] == _TYPE_CNAME or info[0] == _TYPE_PTR:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   564
                rec = DNSPointer(domain, info[0], info[1], info[2], self.readName())
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   565
            elif info[0] == _TYPE_TXT:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   566
                rec = DNSText(domain, info[0], info[1], info[2], self.readString(info[3]))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   567
            elif info[0] == _TYPE_SRV:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   568
                rec = DNSService(domain, info[0], info[1], info[2], self.readUnsignedShort(), self.readUnsignedShort(), self.readUnsignedShort(), self.readName())
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   569
            elif info[0] == _TYPE_HINFO:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   570
                rec = DNSHinfo(domain, info[0], info[1], info[2], self.readCharacterString(), self.readCharacterString())
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   571
            elif info[0] == _TYPE_AAAA:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   572
                rec = DNSAddress(domain, info[0], info[1], info[2], self.readString(16))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   573
            else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   574
                # Try to ignore types we don't know about
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   575
                # this may mean the rest of the name is
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   576
                # unable to be parsed, and may show errors
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   577
                # so this is left for debugging.  New types
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   578
                # encountered need to be parsed properly.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   579
                #
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
   580
                # print "UNKNOWN TYPE = " + str(info[0])
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
   581
                # raise BadTypeInNameException
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   582
                pass
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   583
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   584
            if rec is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   585
                self.answers.append(rec)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   586
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   587
    def isQuery(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   588
        """Returns true if this is a query"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   589
        return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_QUERY
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   590
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   591
    def isResponse(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   592
        """Returns true if this is a response"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   593
        return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   594
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   595
    def readUTF(self, offset, len):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   596
        """Reads a UTF-8 string of a given length from the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   597
        result = self.data[offset:offset+len].decode('utf-8')
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   598
        return result
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   599
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   600
    def readName(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   601
        """Reads a domain name from the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   602
        result = ''
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   603
        off = self.offset
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   604
        next = -1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   605
        first = off
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   606
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   607
        while 1:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   608
            len = ord(self.data[off])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   609
            off += 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   610
            if len == 0:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   611
                break
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   612
            t = len & 0xC0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   613
            if t == 0x00:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   614
                result = ''.join((result, self.readUTF(off, len) + '.'))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   615
                off += len
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   616
            elif t == 0xC0:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   617
                if next < 0:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   618
                    next = off + 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   619
                off = ((len & 0x3F) << 8) | ord(self.data[off])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   620
                if off >= first:
361
331d698e1118 Adding support for internationalization
laurent
parents: 223
diff changeset
   621
                    raise _("Bad domain name (circular) at ") + str(off)
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   622
                first = off
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   623
            else:
361
331d698e1118 Adding support for internationalization
laurent
parents: 223
diff changeset
   624
                raise _("Bad domain name at ") + str(off)
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   625
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   626
        if next >= 0:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   627
            self.offset = next
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   628
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   629
            self.offset = off
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   630
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   631
        return result
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   632
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   633
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   634
class DNSOutgoing(object):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   635
    """Object representation of an outgoing packet"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   636
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1743
diff changeset
   637
    def __init__(self, flags, multicast=1):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   638
        self.finished = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   639
        self.id = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   640
        self.multicast = multicast
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   641
        self.flags = flags
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   642
        self.names = {}
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   643
        self.data = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   644
        self.size = 12
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   645
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   646
        self.questions = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   647
        self.answers = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   648
        self.authorities = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   649
        self.additionals = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   650
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   651
    def addQuestion(self, record):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   652
        """Adds a question"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   653
        self.questions.append(record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   654
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   655
    def addAnswer(self, inp, record):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   656
        """Adds an answer"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   657
        if not record.suppressedBy(inp):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   658
            self.addAnswerAtTime(record, 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   659
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   660
    def addAnswerAtTime(self, record, now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   661
        """Adds an answer if if does not expire by a certain time"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   662
        if record is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   663
            if now == 0 or not record.isExpired(now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   664
                self.answers.append((record, now))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   665
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   666
    def addAuthorativeAnswer(self, record):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   667
        """Adds an authoritative answer"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   668
        self.authorities.append(record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   669
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   670
    def addAdditionalAnswer(self, record):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   671
        """Adds an additional answer"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   672
        self.additionals.append(record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   673
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   674
    def writeByte(self, value):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   675
        """Writes a single byte to the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   676
        format = '!c'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   677
        self.data.append(struct.pack(format, chr(value)))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   678
        self.size += 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   679
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   680
    def insertShort(self, index, value):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   681
        """Inserts an unsigned short in a certain position in the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   682
        format = '!H'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   683
        self.data.insert(index, struct.pack(format, value))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   684
        self.size += 2
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   685
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   686
    def writeShort(self, value):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   687
        """Writes an unsigned short to the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   688
        format = '!H'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   689
        self.data.append(struct.pack(format, value))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   690
        self.size += 2
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   691
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   692
    def writeInt(self, value):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   693
        """Writes an unsigned integer to the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   694
        format = '!I'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   695
        self.data.append(struct.pack(format, int(value)))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   696
        self.size += 4
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   697
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   698
    def writeString(self, value, length):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   699
        """Writes a string to the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   700
        format = '!' + str(length) + 's'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   701
        self.data.append(struct.pack(format, value))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   702
        self.size += length
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   703
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   704
    def writeUTF(self, s):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   705
        """Writes a UTF-8 string of a given length to the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   706
        utfstr = s.encode('utf-8')
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   707
        length = len(utfstr)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   708
        if length > 64:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   709
            raise NamePartTooLongException
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   710
        self.writeByte(length)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   711
        self.writeString(utfstr, length)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   712
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   713
    def writeName(self, name):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   714
        """Writes a domain name to the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   715
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   716
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   717
            # Find existing instance of this name in packet
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   718
            #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   719
            index = self.names[name]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   720
        except KeyError:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   721
            # No record of this name already, so write it
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   722
            # out as normal, recording the location of the name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   723
            # for future pointers to it.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   724
            #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   725
            self.names[name] = self.size
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   726
            parts = name.split('.')
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   727
            if parts[-1] == '':
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   728
                parts = parts[:-1]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   729
            for part in parts:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   730
                self.writeUTF(part)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   731
            self.writeByte(0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   732
            return
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   733
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   734
        # An index was found, so write a pointer to it
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   735
        #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   736
        self.writeByte((index >> 8) | 0xC0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   737
        self.writeByte(index)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   738
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   739
    def writeQuestion(self, question):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   740
        """Writes a question to the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   741
        self.writeName(question.name)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   742
        self.writeShort(question.type)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   743
        self.writeShort(question.clazz)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   744
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   745
    def writeRecord(self, record, now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   746
        """Writes a record (answer, authoritative answer, additional) to
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   747
        the packet"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   748
        self.writeName(record.name)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   749
        self.writeShort(record.type)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   750
        if record.unique and self.multicast:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   751
            self.writeShort(record.clazz | _CLASS_UNIQUE)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   752
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   753
            self.writeShort(record.clazz)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   754
        if now == 0:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   755
            self.writeInt(record.ttl)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   756
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   757
            self.writeInt(record.getRemainingTTL(now))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   758
        index = len(self.data)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   759
        # Adjust size for the short we will write before this record
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   760
        #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   761
        self.size += 2
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   762
        record.write(self)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   763
        self.size -= 2
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   764
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   765
        length = len(''.join(self.data[index:]))
1737
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   766
        self.insertShort(index, length)  # Here is the short we adjusted for
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   767
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   768
    def packet(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   769
        """Returns a string containing the packet's bytes
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   770
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   771
        No further parts should be added to the packet once this
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   772
        is done."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   773
        if not self.finished:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   774
            self.finished = 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   775
            for question in self.questions:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   776
                self.writeQuestion(question)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   777
            for answer, time in self.answers:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   778
                self.writeRecord(answer, time)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   779
            for authority in self.authorities:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   780
                self.writeRecord(authority, 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   781
            for additional in self.additionals:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   782
                self.writeRecord(additional, 0)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   783
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   784
            self.insertShort(0, len(self.additionals))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   785
            self.insertShort(0, len(self.authorities))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   786
            self.insertShort(0, len(self.answers))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   787
            self.insertShort(0, len(self.questions))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   788
            self.insertShort(0, self.flags)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   789
            if self.multicast:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   790
                self.insertShort(0, 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   791
            else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   792
                self.insertShort(0, self.id)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   793
        return ''.join(self.data)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   794
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   795
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   796
class DNSCache(object):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   797
    """A cache of DNS entries"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   798
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   799
    def __init__(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   800
        self.cache = {}
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   801
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   802
    def add(self, entry):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   803
        """Adds an entry"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   804
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   805
            list = self.cache[entry.key]
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   806
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   807
            list = self.cache[entry.key] = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   808
        list.append(entry)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   809
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   810
    def remove(self, entry):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   811
        """Removes an entry"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   812
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   813
            list = self.cache[entry.key]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   814
            list.remove(entry)
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   815
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   816
            pass
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   817
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   818
    def get(self, entry):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   819
        """Gets an entry by key.  Will return None if there is no
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   820
        matching entry."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   821
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   822
            list = self.cache[entry.key]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   823
            return list[list.index(entry)]
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   824
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   825
            return None
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   826
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   827
    def getByDetails(self, name, type, clazz):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   828
        """Gets an entry by details.  Will return None if there is
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   829
        no matching entry."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   830
        entry = DNSEntry(name, type, clazz)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   831
        return self.get(entry)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   832
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   833
    def entriesWithName(self, name):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   834
        """Returns a list of entries whose key matches the name."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   835
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   836
            return self.cache[name]
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   837
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   838
            return []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   839
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   840
    def entries(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   841
        """Returns a list of all entries"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   842
        def add(x, y): return x+y
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   843
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   844
            return reduce(add, self.cache.values())
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   845
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   846
            return []
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   847
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   848
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   849
class Engine(threading.Thread):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   850
    """An engine wraps read access to sockets, allowing objects that
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   851
    need to receive data from sockets to be called back when the
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   852
    sockets are ready.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   853
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   854
    A reader needs a handle_read() method, which is called when the socket
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   855
    it is interested in is ready for reading.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   856
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   857
    Writers are not implemented here, because we only send short
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   858
    packets.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   859
    """
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   860
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   861
    def __init__(self, zeroconf):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   862
        threading.Thread.__init__(self)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   863
        self.zeroconf = zeroconf
1737
a39c2918c015 clean-up: fix PEP8 E261 at least two spaces before inline comment
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1736
diff changeset
   864
        self.readers = {}  # maps socket to reader
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   865
        self.timeout = 5
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   866
        self.condition = threading.Condition()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   867
        self.start()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   868
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   869
    def run(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   870
        while not globals()['_GLOBAL_DONE']:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   871
            rs = self.getReaders()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   872
            if len(rs) == 0:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   873
                # No sockets to manage, but we wait for the timeout
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   874
                # or addition of a socket
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   875
                #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   876
                self.condition.acquire()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   877
                self.condition.wait(self.timeout)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   878
                self.condition.release()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   879
            else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   880
                try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   881
                    rr, wr, er = select.select(rs, [], [], self.timeout)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   882
                    for socket in rr:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   883
                        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   884
                            self.readers[socket].handle_read()
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   885
                        except Exception:
644
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 379
diff changeset
   886
                            # Ignore errors that occur on shutdown
b511cab580eb Better naming of IP address related variables in Beremiz_service.py, Ignore errors that occur on shutdown in Zeroconf.py, fixed discovery dialog crash due to asynchronous call from zeroconf
Edouard Tisserant
parents: 379
diff changeset
   887
                            pass
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
   888
                except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   889
                    pass
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   890
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   891
    def getReaders(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   892
        result = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   893
        self.condition.acquire()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   894
        result = self.readers.keys()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   895
        self.condition.release()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   896
        return result
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   897
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   898
    def addReader(self, reader, socket):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   899
        self.condition.acquire()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   900
        self.readers[socket] = reader
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   901
        self.condition.notify()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   902
        self.condition.release()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   903
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   904
    def delReader(self, socket):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   905
        self.condition.acquire()
1828
396da88d7b5c fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
   906
        del self.readers[socket]
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   907
        self.condition.notify()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   908
        self.condition.release()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   909
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   910
    def notify(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   911
        self.condition.acquire()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   912
        self.condition.notify()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   913
        self.condition.release()
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   914
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1730
diff changeset
   915
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   916
class Listener(object):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   917
    """A Listener is used by this module to listen on the multicast
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   918
    group to which DNS messages are sent, allowing the implementation
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   919
    to cache information as it arrives.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   920
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   921
    It requires registration with an Engine object in order to have
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   922
    the read() method called when a socket is availble for reading."""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   923
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   924
    def __init__(self, zeroconf):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   925
        self.zeroconf = zeroconf
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   926
        self.zeroconf.engine.addReader(self, self.zeroconf.socket)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   927
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   928
    def handle_read(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   929
        data, (addr, port) = self.zeroconf.socket.recvfrom(_MAX_MSG_ABSOLUTE)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   930
        self.data = data
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   931
        msg = DNSIncoming(data)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   932
        if msg.isQuery():
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   933
            # Always multicast responses
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   934
            #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   935
            if port == _MDNS_PORT:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   936
                self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   937
            # If it's not a multicast query, reply via unicast
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   938
            # and multicast
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   939
            #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   940
            elif port == _DNS_PORT:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   941
                self.zeroconf.handleQuery(msg, addr, port)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   942
                self.zeroconf.handleQuery(msg, _MDNS_ADDR, _MDNS_PORT)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   943
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   944
            self.zeroconf.handleResponse(msg)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   945
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   946
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   947
class Reaper(threading.Thread):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   948
    """A Reaper is used by this module to remove cache entries that
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   949
    have expired."""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   950
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   951
    def __init__(self, zeroconf):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   952
        threading.Thread.__init__(self)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   953
        self.zeroconf = zeroconf
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   954
        self.start()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   955
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   956
    def run(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   957
        while 1:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   958
            self.zeroconf.wait(10 * 1000)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   959
            if globals()['_GLOBAL_DONE']:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   960
                return
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   961
            now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   962
            for record in self.zeroconf.cache.entries():
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   963
                if record.isExpired(now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   964
                    self.zeroconf.updateRecord(now, record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   965
                    self.zeroconf.cache.remove(record)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   966
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   967
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   968
class ServiceBrowser(threading.Thread):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   969
    """Used to browse for a service of a specific type.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   970
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   971
    The listener object will have its addService() and
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   972
    removeService() methods called when this browser
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   973
    discovers changes in the services availability."""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   974
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   975
    def __init__(self, zeroconf, type, listener):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   976
        """Creates a browser for a specific type"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   977
        threading.Thread.__init__(self)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   978
        self.zeroconf = zeroconf
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   979
        self.type = type
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   980
        self.listener = listener
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   981
        self.services = {}
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   982
        self.nextTime = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   983
        self.delay = _BROWSER_TIME
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   984
        self.list = []
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
   985
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   986
        self.done = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   987
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   988
        self.zeroconf.addListener(self, DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   989
        self.start()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   990
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   991
    def updateRecord(self, zeroconf, now, record):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   992
        """Callback invoked by Zeroconf when new information arrives.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   993
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   994
        Updates information required by browser in the Zeroconf cache."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   995
        if record.type == _TYPE_PTR and record.name == self.type:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   996
            expired = record.isExpired(now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   997
            try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   998
                oldrecord = self.services[record.alias.lower()]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
   999
                if not expired:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1000
                    oldrecord.resetTTL(record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1001
                else:
1762
fcc406143e5b clean-up: fix PEP8 E731 do not assign a lambda expression, use a def
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1758
diff changeset
  1002
                    def callback(x):
fcc406143e5b clean-up: fix PEP8 E731 do not assign a lambda expression, use a def
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1758
diff changeset
  1003
                        return self.listener.removeService(x, self.type, record.alias)
1828
396da88d7b5c fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
  1004
                    del self.services[record.alias.lower()]
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1005
                    self.list.append(callback)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1006
                    return
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
  1007
            except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1008
                if not expired:
1762
fcc406143e5b clean-up: fix PEP8 E731 do not assign a lambda expression, use a def
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1758
diff changeset
  1009
                    def callback(x):
fcc406143e5b clean-up: fix PEP8 E731 do not assign a lambda expression, use a def
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1758
diff changeset
  1010
                        return self.listener.addService(x, self.type, record.alias)
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1011
                    self.services[record.alias.lower()] = record
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1012
                    self.list.append(callback)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1013
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1014
            expires = record.getExpirationTime(75)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1015
            if expires < self.nextTime:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1016
                self.nextTime = expires
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1017
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1018
    def cancel(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1019
        self.done = 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1020
        self.zeroconf.notifyAll()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1021
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1022
    def run(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1023
        while 1:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1024
            event = None
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1025
            now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1026
            if len(self.list) == 0 and self.nextTime > now:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1027
                self.zeroconf.wait(self.nextTime - now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1028
            if globals()['_GLOBAL_DONE'] or self.done:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1029
                return
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1030
            now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1031
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1032
            if self.nextTime <= now:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1033
                out = DNSOutgoing(_FLAGS_QR_QUERY)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1034
                out.addQuestion(DNSQuestion(self.type, _TYPE_PTR, _CLASS_IN))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1035
                for record in self.services.values():
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1036
                    if not record.isExpired(now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1037
                        out.addAnswerAtTime(record, now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1038
                self.zeroconf.send(out)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1039
                self.nextTime = now + self.delay
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1040
                self.delay = min(20 * 1000, self.delay * 2)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1041
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1042
            if len(self.list) > 0:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1043
                event = self.list.pop(0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1044
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1045
            if event is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1046
                event(self.zeroconf)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1047
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
  1048
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
  1049
class ServiceInfo(object):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1050
    """Service information"""
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1051
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1052
    def __init__(self, type, name, address=None, port=None, weight=0, priority=0, properties=None, server=None):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1053
        """Create a service description.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1054
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1055
        type: fully qualified service type name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1056
        name: fully qualified service name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1057
        address: IP address as unsigned short, network byte order
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1058
        port: port that the service runs on
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1059
        weight: weight of the service
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1060
        priority: priority of the service
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1061
        properties: dictionary of properties (or a string holding the bytes for the text field)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1062
        server: fully qualified name for service host (defaults to name)"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1063
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1064
        if not name.endswith(type):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1065
            raise BadTypeInNameException
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1066
        self.type = type
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1067
        self.name = name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1068
        self.address = address
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1069
        self.port = port
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1070
        self.weight = weight
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1071
        self.priority = priority
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1072
        if server:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1073
            self.server = server
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1074
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1075
            self.server = name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1076
        self.setProperties(properties)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1077
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1078
    def setProperties(self, properties):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1079
        """Sets properties and text of this info from a dictionary"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1080
        if isinstance(properties, dict):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1081
            self.properties = properties
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1082
            list = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1083
            result = ''
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1084
            for key in properties:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1085
                value = properties[key]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1086
                if value is None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1087
                    suffix = ''.encode('utf-8')
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1088
                elif isinstance(value, str):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1089
                    suffix = value.encode('utf-8')
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1090
                elif isinstance(value, int):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1091
                    if value:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1092
                        suffix = 'true'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1093
                    else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1094
                        suffix = 'false'
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1095
                else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1096
                    suffix = ''.encode('utf-8')
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1097
                list.append('='.join((key, suffix)))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1098
            for item in list:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1099
                result = ''.join((result, struct.pack('!c', chr(len(item))), item))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1100
            self.text = result
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1101
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1102
            self.text = properties
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1103
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1104
    def setText(self, text):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1105
        """Sets properties and text given a text field"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1106
        self.text = text
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1107
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1108
            result = {}
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1109
            end = len(text)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1110
            index = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1111
            strs = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1112
            while index < end:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1113
                length = ord(text[index])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1114
                index += 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1115
                strs.append(text[index:index+length])
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1116
                index += length
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1117
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1118
            for s in strs:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1119
                eindex = s.find('=')
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1120
                if eindex == -1:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1121
                    # No equals sign at all
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1122
                    key = s
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1123
                    value = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1124
                else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1125
                    key = s[:eindex]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1126
                    value = s[eindex+1:]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1127
                    if value == 'true':
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1128
                        value = 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1129
                    elif value == 'false' or not value:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1130
                        value = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1131
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1132
                # Only update non-existent properties
1743
c3c3d1318130 clean-up: fix PEP8 E711 comparison to None should be 'if cond is not None:'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
  1133
                if key and result.get(key) is None:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1134
                    result[key] = value
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1135
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1136
            self.properties = result
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
  1137
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1138
            traceback.print_exc()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1139
            self.properties = None
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1140
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1141
    def getType(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1142
        """Type accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1143
        return self.type
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1144
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1145
    def getName(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1146
        """Name accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1147
        if self.type is not None and self.name.endswith("." + self.type):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1148
            return self.name[:len(self.name) - len(self.type) - 1]
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1149
        return self.name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1150
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1151
    def getAddress(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1152
        """Address accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1153
        return self.address
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1154
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1155
    def getPort(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1156
        """Port accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1157
        return self.port
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1158
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1159
    def getPriority(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1160
        """Pirority accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1161
        return self.priority
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1162
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1163
    def getWeight(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1164
        """Weight accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1165
        return self.weight
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1166
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1167
    def getProperties(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1168
        """Properties accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1169
        return self.properties
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1170
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1171
    def getText(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1172
        """Text accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1173
        return self.text
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1174
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1175
    def getServer(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1176
        """Server accessor"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1177
        return self.server
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1178
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1179
    def updateRecord(self, zeroconf, now, record):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1180
        """Updates service information from a DNS record"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1181
        if record is not None and not record.isExpired(now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1182
            if record.type == _TYPE_A:
373
a7f26d9f214f fix pyZeroconf bug where getServiceInfo returns None.
b.taylor@willowglen.ca
parents: 223
diff changeset
  1183
                if record.name == self.server:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1184
                    self.address = record.address
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1185
            elif record.type == _TYPE_SRV:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1186
                if record.name == self.name:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1187
                    self.server = record.server
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1188
                    self.port = record.port
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1189
                    self.weight = record.weight
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1190
                    self.priority = record.priority
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1191
                    self.updateRecord(zeroconf, now, zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1192
            elif record.type == _TYPE_TXT:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1193
                if record.name == self.name:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1194
                    self.setText(record.text)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1195
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1196
    def request(self, zeroconf, timeout):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1197
        """Returns true if the service could be discovered on the
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1198
        network, and updates this object with details discovered.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1199
        """
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1200
        now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1201
        delay = _LISTENER_TIME
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1202
        next = now + delay
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1203
        last = now + timeout
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1204
        result = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1205
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1206
            zeroconf.addListener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1207
            while self.server is None or self.address is None or self.text is None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1208
                if last <= now:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1209
                    return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1210
                if next <= now:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1211
                    out = DNSOutgoing(_FLAGS_QR_QUERY)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1212
                    out.addQuestion(DNSQuestion(self.name, _TYPE_SRV, _CLASS_IN))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1213
                    out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_SRV, _CLASS_IN), now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1214
                    out.addQuestion(DNSQuestion(self.name, _TYPE_TXT, _CLASS_IN))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1215
                    out.addAnswerAtTime(zeroconf.cache.getByDetails(self.name, _TYPE_TXT, _CLASS_IN), now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1216
                    if self.server is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1217
                        out.addQuestion(DNSQuestion(self.server, _TYPE_A, _CLASS_IN))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1218
                        out.addAnswerAtTime(zeroconf.cache.getByDetails(self.server, _TYPE_A, _CLASS_IN), now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1219
                    zeroconf.send(out)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1220
                    next = now + delay
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1221
                    delay = delay * 2
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1222
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1223
                zeroconf.wait(min(next, last) - now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1224
                now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1225
            result = 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1226
        finally:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1227
            zeroconf.removeListener(self)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1228
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1229
        return result
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1230
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1231
    def __eq__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1232
        """Tests equality of service name"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1233
        if isinstance(other, ServiceInfo):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1234
            return other.name == self.name
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1235
        return 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1236
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1237
    def __ne__(self, other):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1238
        """Non-equality test"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1239
        return not self.__eq__(other)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1240
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1241
    def __repr__(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1242
        """String representation"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1243
        result = "service[%s,%s:%s," % (self.name, socket.inet_ntoa(self.getAddress()), self.port)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1244
        if self.text is None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1245
            result += "None"
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1246
        else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1247
            if len(self.text) < 20:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1248
                result += self.text
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1249
            else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1250
                result += self.text[:17] + "..."
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1251
        result += "]"
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1252
        return result
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1253
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
  1254
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
  1255
class Zeroconf(object):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1256
    """Implementation of Zeroconf Multicast DNS Service Discovery
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1257
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1258
    Supports registration, unregistration, queries and browsing.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1259
    """
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1260
    def __init__(self, bindaddress=None):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1261
        """Creates an instance of the Zeroconf class, establishing
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1262
        multicast communications, listening and reaping threads."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1263
        globals()['_GLOBAL_DONE'] = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1264
        self.intf = bindaddress
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1265
        self.group = ('', _MDNS_PORT)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1266
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1267
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1268
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1269
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
  1270
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1271
            # SO_REUSEADDR should be equivalent to SO_REUSEPORT for
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1272
            # multicast UDP sockets (p 731, "TCP/IP Illustrated,
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1273
            # Volume 2"), but some BSD-derived systems require
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1274
            # SO_REUSEPORT to be specified explicity.  Also, not all
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1275
            # versions of Python have SO_REUSEPORT available.  So
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1276
            # if you're on a BSD-based system, and haven't upgraded
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1277
            # to Python 2.3 yet, you may find this library doesn't
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1278
            # work as expected.
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1279
            #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1280
            pass
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1281
        self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, 255)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1282
        self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1283
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1284
            self.socket.bind(self.group)
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
  1285
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1286
            # Some versions of linux raise an exception even though
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1287
            # the SO_REUSE* options have been set, so ignore it
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1288
            #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1289
            pass
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1290
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1291
        if self.intf is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1292
            self.socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(self.intf) + socket.inet_aton('0.0.0.0'))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1293
        self.socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1294
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1295
        self.listeners = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1296
        self.browsers = []
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1297
        self.services = {}
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1298
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1299
        self.cache = DNSCache()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1300
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1301
        self.condition = threading.Condition()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1302
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1303
        self.engine = Engine(self)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1304
        self.listener = Listener(self)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1305
        self.reaper = Reaper(self)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1306
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1307
    def isLoopback(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1308
        if self.intf is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1309
            return self.intf.startswith("127.0.0.1")
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1310
        return False
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1311
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1312
    def isLinklocal(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1313
        if self.intf is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1314
            return self.intf.startswith("169.254.")
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1315
        return False
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1316
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1317
    def wait(self, timeout):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1318
        """Calling thread waits for a given number of milliseconds or
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1319
        until notified."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1320
        self.condition.acquire()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1321
        self.condition.wait(timeout/1000)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1322
        self.condition.release()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1323
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1324
    def notifyAll(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1325
        """Notifies all waiting threads"""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1326
        self.condition.acquire()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1327
        self.condition.notifyAll()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1328
        self.condition.release()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1329
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1330
    def getServiceInfo(self, type, name, timeout=3000):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1331
        """Returns network's service information for a particular
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1332
        name and type, or None if no service matches by the timeout,
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1333
        which defaults to 3 seconds."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1334
        info = ServiceInfo(type, name)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1335
        if info.request(self, timeout):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1336
            return info
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1337
        return None
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1338
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1339
    def addServiceListener(self, type, listener):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1340
        """Adds a listener for a particular service type.  This object
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1341
        will then have its updateRecord method called when information
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1342
        arrives for that type."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1343
        self.removeServiceListener(listener)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1344
        self.browsers.append(ServiceBrowser(self, type, listener))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1345
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1346
    def removeServiceListener(self, listener):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1347
        """Removes a listener from the set that is currently listening."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1348
        for browser in self.browsers:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1349
            if browser.listener == listener:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1350
                browser.cancel()
1828
396da88d7b5c fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
  1351
                del browser
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1352
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1353
    def registerService(self, info, ttl=_DNS_TTL):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1354
        """Registers service information to the network with a default TTL
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1355
        of 60 seconds.  Zeroconf will then respond to requests for
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1356
        information for that service.  The name of the service may be
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1357
        changed if needed to make it unique on the network."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1358
        self.checkService(info)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1359
        self.services[info.name.lower()] = info
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1360
        now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1361
        nextTime = now
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1362
        i = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1363
        while i < 3:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1364
            if now < nextTime:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1365
                self.wait(nextTime - now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1366
                now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1367
                continue
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1368
            out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1369
            out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, ttl, info.name), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1370
            out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, ttl, info.priority, info.weight, info.port, info.server), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1371
            out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1372
            if info.address:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1373
                out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, ttl, info.address), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1374
            self.send(out)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1375
            i += 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1376
            nextTime += _REGISTER_TIME
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1377
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1378
    def unregisterService(self, info):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1379
        """Unregister a service."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1380
        try:
1828
396da88d7b5c fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
  1381
            del self.services[info.name.lower()]
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
  1382
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1383
            pass
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1384
        now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1385
        nextTime = now
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1386
        i = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1387
        while i < 3:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1388
            if now < nextTime:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1389
                self.wait(nextTime - now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1390
                now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1391
                continue
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1392
            out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1393
            out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1394
            out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.name), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1395
            out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1396
            if info.address:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1397
                out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1398
            self.send(out)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1399
            i += 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1400
            nextTime += _UNREGISTER_TIME
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1401
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1402
    def unregisterAllServices(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1403
        """Unregister all registered services."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1404
        if len(self.services) > 0:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1405
            now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1406
            nextTime = now
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1407
            i = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1408
            while i < 3:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1409
                if now < nextTime:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1410
                    self.wait(nextTime - now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1411
                    now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1412
                    continue
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1413
                out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1414
                for info in self.services.values():
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1415
                    out.addAnswerAtTime(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, 0, info.name), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1416
                    out.addAnswerAtTime(DNSService(info.name, _TYPE_SRV, _CLASS_IN, 0, info.priority, info.weight, info.port, info.server), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1417
                    out.addAnswerAtTime(DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1418
                    if info.address:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1419
                        out.addAnswerAtTime(DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0, info.address), 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1420
                self.send(out)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1421
                i += 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1422
                nextTime += _UNREGISTER_TIME
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1423
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1424
    def checkService(self, info):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1425
        """Checks the network for a unique service name, modifying the
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1426
        ServiceInfo passed in if it is not unique."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1427
        now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1428
        nextTime = now
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1429
        i = 0
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1430
        while i < 3:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1431
            for record in self.cache.entriesWithName(info.type):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1432
                if record.type == _TYPE_PTR and not record.isExpired(now) and record.alias == info.name:
1828
396da88d7b5c fix unnecessary parens after keyword
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1826
diff changeset
  1433
                    if info.name.find('.') < 0:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1434
                        info.name = info.name + ".[" + info.address + ":" + info.port + "]." + info.type
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1435
                        self.checkService(info)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1436
                        return
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1437
                    raise NonUniqueNameException
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1438
            if now < nextTime:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1439
                self.wait(nextTime - now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1440
                now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1441
                continue
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1442
            out = DNSOutgoing(_FLAGS_QR_QUERY | _FLAGS_AA)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1443
            self.debug = out
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1444
            out.addQuestion(DNSQuestion(info.type, _TYPE_PTR, _CLASS_IN))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1445
            out.addAuthorativeAnswer(DNSPointer(info.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, info.name))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1446
            self.send(out)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1447
            i += 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1448
            nextTime += _CHECK_TIME
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1449
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1450
    def addListener(self, listener, question):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1451
        """Adds a listener for a given question.  The listener will have
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1452
        its updateRecord method called when information is available to
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1453
        answer the question."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1454
        now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1455
        self.listeners.append(listener)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1456
        if question is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1457
            for record in self.cache.entriesWithName(question.name):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1458
                if question.answeredBy(record) and not record.isExpired(now):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1459
                    listener.updateRecord(self, now, record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1460
        self.notifyAll()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1461
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1462
    def removeListener(self, listener):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1463
        """Removes a listener."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1464
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1465
            self.listeners.remove(listener)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1466
            self.notifyAll()
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
  1467
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1468
            pass
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1469
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1470
    def updateRecord(self, now, rec):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1471
        """Used to notify listeners of new information that has updated
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1472
        a record."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1473
        for listener in self.listeners:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1474
            listener.updateRecord(self, now, rec)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1475
        self.notifyAll()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1476
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1477
    def handleResponse(self, msg):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1478
        """Deal with incoming response packets.  All answers
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1479
        are held in the cache, and listeners are notified."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1480
        now = currentTimeMillis()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1481
        for record in msg.answers:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1482
            expired = record.isExpired(now)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1483
            if record in self.cache.entries():
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1484
                if expired:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1485
                    self.cache.remove(record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1486
                else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1487
                    entry = self.cache.get(record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1488
                    if entry is not None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1489
                        entry.resetTTL(record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1490
                        record = entry
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1491
            else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1492
                self.cache.add(record)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1493
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1494
            self.updateRecord(now, record)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1495
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1496
    def handleQuery(self, msg, addr, port):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1497
        """Deal with incoming query packets.  Provides a response if
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1498
        possible."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1499
        out = None
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1500
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1501
        # Support unicast client responses
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1502
        #
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1503
        if port != _MDNS_PORT:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1504
            out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, 0)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1505
            for question in msg.questions:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1506
                out.addQuestion(question)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1507
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1508
        for question in msg.questions:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1509
            if question.type == _TYPE_PTR:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1510
                for service in self.services.values():
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1511
                    if question.name == service.type:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1512
                        if out is None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1513
                            out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1514
                        out.addAnswer(msg, DNSPointer(service.type, _TYPE_PTR, _CLASS_IN, _DNS_TTL, service.name))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1515
            else:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1516
                try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1517
                    if out is None:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1518
                        out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA)
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1519
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1520
                    # Answer A record queries for any service addresses we know
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1521
                    if question.type == _TYPE_A or question.type == _TYPE_ANY:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1522
                        for service in self.services.values():
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1523
                            if service.server == question.name.lower():
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1524
                                out.addAnswer(msg, DNSAddress(question.name, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address))
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1525
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1526
                    service = self.services.get(question.name.lower(), None)
1756
08e4394ff4fb clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1752
diff changeset
  1527
                    if not service:
08e4394ff4fb clean-up: fix PEP8 E701 multiple statements on one line (colon)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1752
diff changeset
  1528
                        continue
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1529
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1530
                    if question.type == _TYPE_SRV or question.type == _TYPE_ANY:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1531
                        out.addAnswer(msg, DNSService(question.name, _TYPE_SRV, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.priority, service.weight, service.port, service.server))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1532
                    if question.type == _TYPE_TXT or question.type == _TYPE_ANY:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1533
                        out.addAnswer(msg, DNSText(question.name, _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.text))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1534
                    if question.type == _TYPE_SRV:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1535
                        out.addAdditionalAnswer(DNSAddress(service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE, _DNS_TTL, service.address))
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
  1536
                except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1537
                    traceback.print_exc()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1538
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1539
        if out is not None and out.answers:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1540
            out.id = msg.id
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1541
            self.send(out, addr, port)
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1542
1744
69dfdb26f600 clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1743
diff changeset
  1543
    def send(self, out, addr=_MDNS_ADDR, port=_MDNS_PORT):
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1544
        """Sends an outgoing packet."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1545
        # This is a quick test to see if we can parse the packets we generate
1782
5b6ad7a7fd9d clean-up: fix PEP8 E265 block comment should start with '# '
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1780
diff changeset
  1546
        # temp = DNSIncoming(out.packet())
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1547
        try:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1548
            bytes_sent = self.socket.sendto(out.packet(), 0, (addr, port))
1780
c52d1460cea8 clean-up: fix PEP8 E722 do not use bare except'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1762
diff changeset
  1549
        except Exception:
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1550
            # Ignore this, it may be a temporary loss of network connection
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1551
            pass
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1552
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1553
    def close(self):
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1554
        """Ends the background threads, and prevent this instance from
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1555
        servicing further queries."""
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1556
        if globals()['_GLOBAL_DONE'] == 0:
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1557
            globals()['_GLOBAL_DONE'] = 1
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1558
            self.notifyAll()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1559
            self.engine.notify()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1560
            self.unregisterAllServices()
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1561
            self.socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP, socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'))
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1562
            self.socket.close()
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1563
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
  1564
# Test a few module features, including service registration, service
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
  1565
# query (for Zoe), and service unregistration.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
  1566
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1747
diff changeset
  1567
1730
64d8f52bc8c8 clean-up for PEP8: fix W291 trailing whitespace
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 726
diff changeset
  1568
if __name__ == '__main__':
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1569
    print("Multicast DNS Service Discovery for Python, version", __version__)
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1570
    r = Zeroconf()
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1571
    print("1. Testing registration of a service...")
1740
b789b695b5c6 clean-up: fix PEP8 E231 missing whitespace after ':' or ','
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1739
diff changeset
  1572
    desc = {'version': '0.10', 'a': 'test value', 'b': 'another value'}
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1573
    info = ServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.", socket.inet_aton("127.0.0.1"), 1234, 0, 0, desc)
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1574
    print("   Registering service...")
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1575
    r.registerService(info)
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1576
    print("   Registration done.")
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1577
    print("2. Testing query of service information...")
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1578
    print("   Getting ZOE service:", str(r.getServiceInfo("_http._tcp.local.", "ZOE._http._tcp.local.")))
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1579
    print("   Query done.")
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1580
    print("3. Testing query of own service...")
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1581
    print("   Getting self:", str(r.getServiceInfo("_http._tcp.local.", "My Service Name._http._tcp.local.")))
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1582
    print("   Query done.")
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1583
    print("4. Testing unregister of service information...")
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1584
    r.unregisterService(info)
1826
91796f408540 fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1783
diff changeset
  1585
    print("   Unregister done.")
223
1c0d3aaacf3d Fixed bad bahavior when zeroconf bindaddress unspecified.
etisserant
parents: 203
diff changeset
  1586
    r.close()