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