2006-07-27 22:36:21 +02:00
|
|
|
## common/zeroconf/zeroconf.py
|
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Stefan Bethge <stefan@lanpartei.de>
|
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 2 only.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
|
2006-05-26 16:43:51 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2006-05-30 01:03:20 +02:00
|
|
|
import socket
|
2006-07-11 17:22:59 +02:00
|
|
|
from common import gajim
|
2006-09-13 20:49:34 +02:00
|
|
|
from common import xmpp
|
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
try:
|
|
|
|
import avahi, gobject, dbus
|
|
|
|
except ImportError:
|
2006-09-17 17:07:35 +02:00
|
|
|
gajim.log.debug("Error: python-avahi and python-dbus need to be installed. No zeroconf support.")
|
2006-05-26 12:56:21 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
import dbus.glib
|
|
|
|
except ImportError, e:
|
|
|
|
pass
|
|
|
|
|
2006-09-17 17:07:35 +02:00
|
|
|
|
|
|
|
C_NAME, C_DOMAIN, C_INTERFACE, C_PROTOCOL, C_HOST, \
|
|
|
|
C_ADDRESS, C_PORT, C_BARE_NAME, C_TXT = range(9)
|
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
class Zeroconf:
|
2006-07-11 17:22:59 +02:00
|
|
|
def __init__(self, new_serviceCB, remove_serviceCB, name, host, port):
|
2006-05-26 12:56:21 +02:00
|
|
|
self.domain = None # specific domain to browse
|
2006-05-26 21:18:13 +02:00
|
|
|
self.stype = '_presence._tcp'
|
2006-07-11 17:22:59 +02:00
|
|
|
self.port = port # listening port that gets announced
|
|
|
|
self.username = name
|
|
|
|
self.host = host
|
2006-06-13 23:19:39 +02:00
|
|
|
self.name = self.username+'@'+ self.host # service name
|
2006-05-26 21:18:13 +02:00
|
|
|
self.txt = {} # service data
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-31 01:13:36 +02:00
|
|
|
self.new_serviceCB = new_serviceCB
|
|
|
|
self.remove_serviceCB = remove_serviceCB
|
|
|
|
|
2006-05-26 16:43:51 +02:00
|
|
|
self.service_browsers = {}
|
2006-05-26 12:56:21 +02:00
|
|
|
self.contacts = {} # all current local contacts with data
|
2006-05-31 01:13:36 +02:00
|
|
|
self.entrygroup = None
|
2006-08-01 02:44:51 +02:00
|
|
|
self.connected = False
|
2006-08-02 02:04:47 +02:00
|
|
|
self.announced = False
|
2006-09-17 22:54:32 +02:00
|
|
|
self.invalid_self_contact = {}
|
|
|
|
|
2006-08-01 02:44:51 +02:00
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
## handlers for dbus callbacks
|
|
|
|
|
|
|
|
# error handler - maybe replace with gui version/gajim facilities
|
|
|
|
def print_error_callback(self, err):
|
|
|
|
print "Error:", str(err)
|
|
|
|
|
|
|
|
def new_service_callback(self, interface, protocol, name, stype, domain, flags):
|
2006-09-17 22:54:32 +02:00
|
|
|
# print "Found service '%s' in domain '%s' on %i.%i." % (name, domain, interface, protocol)
|
2006-06-13 23:19:39 +02:00
|
|
|
|
2006-09-17 22:54:32 +02:00
|
|
|
#synchronous resolving
|
|
|
|
self.server.ResolveService( int(interface), int(protocol), name, stype, \
|
|
|
|
domain, avahi.PROTO_UNSPEC, dbus.UInt32(0), \
|
|
|
|
reply_handler=self.service_resolved_callback, error_handler=self.print_error_callback)
|
2006-05-26 12:56:21 +02:00
|
|
|
|
|
|
|
def remove_service_callback(self, interface, protocol, name, stype, domain, flags):
|
2006-06-14 00:42:37 +02:00
|
|
|
# print "Service '%s' in domain '%s' on %i.%i disappeared." % (name, domain, interface, protocol)
|
2006-09-15 05:30:52 +02:00
|
|
|
if name != self.name:
|
2006-09-17 17:07:35 +02:00
|
|
|
for key in self.contacts.keys():
|
|
|
|
if self.contacts[key][C_BARE_NAME] == name:
|
|
|
|
del self.contacts[key]
|
|
|
|
self.remove_serviceCB(key)
|
|
|
|
return
|
2006-05-26 12:56:21 +02:00
|
|
|
|
|
|
|
def new_service_type(self, interface, protocol, stype, domain, flags):
|
|
|
|
# Are we already browsing this domain for this type?
|
2006-05-26 16:43:51 +02:00
|
|
|
if self.service_browsers.has_key((interface, protocol, stype, domain)):
|
2006-05-26 12:56:21 +02:00
|
|
|
return
|
|
|
|
|
2006-06-14 00:42:37 +02:00
|
|
|
# print "Browsing for services of type '%s' in domain '%s' on %i.%i ..." % (stype, domain, interface, protocol)
|
2006-05-26 12:56:21 +02:00
|
|
|
|
|
|
|
b = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, \
|
|
|
|
self.server.ServiceBrowserNew(interface, protocol, \
|
|
|
|
stype, domain, dbus.UInt32(0))),avahi.DBUS_INTERFACE_SERVICE_BROWSER)
|
|
|
|
b.connect_to_signal('ItemNew', self.new_service_callback)
|
|
|
|
b.connect_to_signal('ItemRemove', self.remove_service_callback)
|
|
|
|
|
2006-05-26 16:43:51 +02:00
|
|
|
self.service_browsers[(interface, protocol, stype, domain)] = b
|
2006-05-26 12:56:21 +02:00
|
|
|
|
|
|
|
def new_domain_callback(self,interface, protocol, domain, flags):
|
|
|
|
if domain != "local":
|
|
|
|
self.browse_domain(interface, protocol, domain)
|
|
|
|
|
2006-07-11 20:03:50 +02:00
|
|
|
def txt_array_to_dict(self,txt_array):
|
|
|
|
items = {}
|
2006-05-30 00:17:13 +02:00
|
|
|
|
2006-07-11 20:03:50 +02:00
|
|
|
for byte_array in txt_array:
|
|
|
|
# 'str' is used for string type in python
|
|
|
|
value = avahi.byte_array_to_string(byte_array)
|
|
|
|
poseq = value.find('=')
|
|
|
|
items[value[:poseq]] = value[poseq+1:]
|
|
|
|
return items
|
2006-05-29 21:57:39 +02:00
|
|
|
|
2006-06-02 20:46:52 +02:00
|
|
|
def service_resolved_callback(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
|
2006-09-20 01:07:54 +02:00
|
|
|
#print "Service data for service '%s' in domain '%s' on %i.%i:" % (name, domain, interface, protocol)
|
|
|
|
#print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, avahi.txt_array_to_string_array(txt))
|
2006-09-17 17:07:35 +02:00
|
|
|
bare_name = name
|
|
|
|
if name.find('@') == -1:
|
|
|
|
name = name + '@' + name
|
|
|
|
|
2006-09-17 22:54:32 +02:00
|
|
|
# we don't want to see ourselves in the list
|
|
|
|
if name != self.name:
|
|
|
|
self.contacts[name] = (name, domain, interface, protocol, host, address, port,
|
2006-09-17 17:07:35 +02:00
|
|
|
bare_name, txt)
|
2006-09-17 22:54:32 +02:00
|
|
|
self.new_serviceCB(name)
|
|
|
|
else:
|
|
|
|
# remember data
|
|
|
|
# In case this is not our own record but of another
|
|
|
|
# gajim instance on the same machine,
|
|
|
|
# it will be used when we get a new name.
|
|
|
|
self.invalid_self_contact[name] = (name, domain, interface, protocol, host, address, port, bare_name, txt)
|
|
|
|
|
2006-06-02 20:46:52 +02:00
|
|
|
|
|
|
|
# different handler when resolving all contacts
|
|
|
|
def service_resolved_all_callback(self, interface, protocol, name, stype, domain, host, aprotocol, address, port, txt, flags):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print "Service data for service '%s' in domain '%s' on %i.%i:" % (name, domain, interface, protocol)
|
|
|
|
#print "\tHost %s (%s), port %i, TXT data: %s" % (host, address, port, str(avahi.txt_array_to_string_array(txt)))
|
2006-09-17 17:07:35 +02:00
|
|
|
bare_name = name
|
|
|
|
if name.find('@') == -1:
|
|
|
|
name = name + '@' + name
|
|
|
|
self.contacts[name] = (name, domain, interface, protocol, host, address, port, bare_name, txt)
|
2006-05-26 12:56:21 +02:00
|
|
|
|
|
|
|
def service_added_callback(self):
|
2006-06-14 00:42:37 +02:00
|
|
|
# print 'Service successfully added'
|
|
|
|
pass
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
def service_committed_callback(self):
|
2006-06-14 00:42:37 +02:00
|
|
|
# print 'Service successfully committed'
|
|
|
|
pass
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
def service_updated_callback(self):
|
2006-06-14 00:42:37 +02:00
|
|
|
# print 'Service successfully updated'
|
|
|
|
pass
|
2006-05-26 12:56:21 +02:00
|
|
|
|
|
|
|
def service_add_fail_callback(self, err):
|
2006-05-26 21:18:13 +02:00
|
|
|
print 'Error while adding service:', str(err)
|
2006-09-17 22:54:32 +02:00
|
|
|
old_name = self.name
|
2006-05-26 12:56:21 +02:00
|
|
|
self.name = self.server.GetAlternativeServiceName(self.name)
|
|
|
|
self.create_service()
|
2006-09-17 22:54:32 +02:00
|
|
|
if self.invalid_self_contact.has_key(old_name):
|
|
|
|
self.contacts[old_name] = self.invalid_self_contact[old_name]
|
|
|
|
self.new_serviceCB(old_name)
|
|
|
|
del self.invalid_self_contact[old_name]
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
def server_state_changed_callback(self, state, error):
|
2006-05-26 21:18:13 +02:00
|
|
|
print 'server.state %s' % state
|
2006-05-26 12:56:21 +02:00
|
|
|
if state == avahi.SERVER_RUNNING:
|
|
|
|
self.create_service()
|
|
|
|
elif state == avahi.SERVER_COLLISION:
|
|
|
|
self.entrygroup.Reset()
|
|
|
|
# elif state == avahi.CLIENT_FAILURE: # TODO: add error handling (avahi daemon dies...?)
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
def entrygroup_state_changed_callback(self, state, error):
|
2006-05-26 21:18:13 +02:00
|
|
|
# the name is already present, so recreate
|
2006-05-26 12:56:21 +02:00
|
|
|
if state == avahi.ENTRY_GROUP_COLLISION:
|
2006-05-26 21:18:13 +02:00
|
|
|
self.service_add_fail_callback('Local name collision, recreating.')
|
2006-08-01 02:44:51 +02:00
|
|
|
elif state == avahi.ENTRY_GROUP_FAILURE:
|
|
|
|
print 'zeroconf.py: ENTRY_GROUP_FAILURE reached(that should not happen)'
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-29 17:36:18 +02:00
|
|
|
# make zeroconf-valid names
|
|
|
|
def replace_show(self, show):
|
2006-08-02 02:04:47 +02:00
|
|
|
if show == 'chat' or show == 'online' or show == '':
|
|
|
|
show = 'avail'
|
2006-05-29 17:36:18 +02:00
|
|
|
elif show == 'xa':
|
|
|
|
show = 'away'
|
|
|
|
return show
|
2006-05-26 12:56:21 +02:00
|
|
|
|
|
|
|
def create_service(self):
|
2006-08-01 02:44:51 +02:00
|
|
|
try:
|
|
|
|
if not self.entrygroup:
|
|
|
|
# create an EntryGroup for publishing
|
|
|
|
self.entrygroup = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, self.server.EntryGroupNew()), avahi.DBUS_INTERFACE_ENTRY_GROUP)
|
|
|
|
self.entrygroup.connect_to_signal('StateChanged', self.entrygroup_state_changed_callback)
|
|
|
|
|
|
|
|
self.txt['port.p2pj'] = self.port
|
|
|
|
self.txt['version'] = 1
|
|
|
|
self.txt['txtvers'] = 1
|
|
|
|
|
|
|
|
# replace gajim's show messages with compatible ones
|
|
|
|
if self.txt.has_key('status'):
|
|
|
|
self.txt['status'] = self.replace_show(self.txt['status'])
|
2006-05-26 12:56:21 +02:00
|
|
|
|
2006-08-01 02:44:51 +02:00
|
|
|
# print "Publishing service '%s' of type %s" % (self.name, self.stype)
|
|
|
|
self.entrygroup.AddService(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype, '', '', self.port, avahi.dict_to_txt_array(self.txt), reply_handler=self.service_added_callback, error_handler=self.service_add_fail_callback)
|
|
|
|
self.entrygroup.Commit(reply_handler=self.service_committed_callback, error_handler=self.print_error_callback)
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-08-01 02:44:51 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
except dbus.dbus_bindings.DBusException, e:
|
|
|
|
gajim.log.debug(str(e))
|
|
|
|
return False
|
|
|
|
|
2006-05-26 21:18:13 +02:00
|
|
|
def announce(self):
|
2006-08-01 02:44:51 +02:00
|
|
|
if self.connected:
|
|
|
|
state = self.server.GetState()
|
2006-05-26 12:56:21 +02:00
|
|
|
|
2006-08-01 02:44:51 +02:00
|
|
|
if state == avahi.SERVER_RUNNING:
|
|
|
|
self.create_service()
|
2006-08-02 02:04:47 +02:00
|
|
|
self.announced = True
|
2006-08-01 02:44:51 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
def remove_announce(self):
|
2006-08-02 02:04:47 +02:00
|
|
|
if self.announced == False:
|
|
|
|
return False
|
2006-08-01 02:44:51 +02:00
|
|
|
try:
|
|
|
|
if self.entrygroup.GetState() != avahi.ENTRY_GROUP_FAILURE:
|
|
|
|
self.entrygroup.Reset()
|
|
|
|
self.entrygroup.Free()
|
|
|
|
self.entrygroup = None
|
2006-08-02 02:04:47 +02:00
|
|
|
self.announced = False
|
2006-08-01 02:44:51 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
except dbus.dbus_bindings.DBusException, e:
|
|
|
|
print "zeroconf.py: Can't remove service, avahi daemon not running?"
|
2006-05-26 12:56:21 +02:00
|
|
|
|
|
|
|
def browse_domain(self, interface, protocol, domain):
|
|
|
|
self.new_service_type(interface, protocol, self.stype, domain, '')
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
# connect to dbus
|
|
|
|
def connect(self):
|
2006-07-11 17:22:59 +02:00
|
|
|
try:
|
2006-09-14 19:16:01 +02:00
|
|
|
self.bus = dbus.SystemBus()
|
2006-08-01 02:44:51 +02:00
|
|
|
# is there any way to check, if a dbus name exists?
|
|
|
|
# that might make the Introspect Error go away...
|
|
|
|
self.server = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, \
|
|
|
|
avahi.DBUS_PATH_SERVER), avahi.DBUS_INTERFACE_SERVER)
|
|
|
|
|
2006-07-11 17:22:59 +02:00
|
|
|
self.server.connect_to_signal('StateChanged', self.server_state_changed_callback)
|
|
|
|
except dbus.dbus_bindings.DBusException, e:
|
|
|
|
# Avahi service is not present
|
|
|
|
gajim.log.debug(str(e))
|
2006-08-01 02:44:51 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
self.connected = True
|
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
# start browsing
|
|
|
|
if self.domain is None:
|
|
|
|
# Explicitly browse .local
|
|
|
|
self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, "local")
|
|
|
|
|
|
|
|
# Browse for other browsable domains
|
|
|
|
db = dbus.Interface(self.bus.get_object(avahi.DBUS_NAME, \
|
|
|
|
self.server.DomainBrowserNew(avahi.IF_UNSPEC, \
|
|
|
|
avahi.PROTO_UNSPEC, '', avahi.DOMAIN_BROWSER_BROWSE,\
|
|
|
|
dbus.UInt32(0))), avahi.DBUS_INTERFACE_DOMAIN_BROWSER)
|
|
|
|
db.connect_to_signal('ItemNew', self.new_domain_callback)
|
|
|
|
else:
|
|
|
|
self.browse_domain(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, domain)
|
2006-09-17 22:54:32 +02:00
|
|
|
|
2006-08-01 02:44:51 +02:00
|
|
|
return True
|
|
|
|
|
2006-05-26 21:18:13 +02:00
|
|
|
def disconnect(self):
|
2006-08-02 02:04:47 +02:00
|
|
|
if self.connected:
|
|
|
|
self.connected = False
|
|
|
|
self.remove_announce()
|
2006-05-26 12:56:21 +02:00
|
|
|
|
2006-08-01 02:44:51 +02:00
|
|
|
# refresh txt data of all contacts manually (no callback available)
|
2006-05-26 12:56:21 +02:00
|
|
|
def resolve_all(self):
|
2006-05-30 00:17:13 +02:00
|
|
|
for val in self.contacts.values():
|
2006-09-17 17:07:35 +02:00
|
|
|
self.server.ResolveService(int(val[C_INTERFACE]), int(val[C_PROTOCOL]), val[C_BARE_NAME], \
|
|
|
|
self.stype, val[C_DOMAIN], avahi.PROTO_UNSPEC, dbus.UInt32(0),\
|
2006-06-02 20:46:52 +02:00
|
|
|
reply_handler=self.service_resolved_all_callback, error_handler=self.print_error_callback)
|
2006-08-01 02:44:51 +02:00
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
def get_contacts(self):
|
|
|
|
return self.contacts
|
|
|
|
|
2006-05-31 01:13:36 +02:00
|
|
|
def get_contact(self, jid):
|
2006-09-17 17:07:35 +02:00
|
|
|
return self.contacts[jid]
|
2006-05-31 01:13:36 +02:00
|
|
|
|
2006-05-26 21:18:13 +02:00
|
|
|
def update_txt(self, txt):
|
|
|
|
# update only given keys
|
|
|
|
for key in txt.keys():
|
|
|
|
self.txt[key]=txt[key]
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-29 17:36:18 +02:00
|
|
|
if txt.has_key('status'):
|
|
|
|
self.txt['status'] = self.replace_show(txt['status'])
|
2006-05-30 01:03:20 +02:00
|
|
|
|
2006-05-26 12:56:21 +02:00
|
|
|
txt = avahi.dict_to_txt_array(self.txt)
|
2006-09-20 01:07:54 +02:00
|
|
|
if self.connected and self.entrygroup:
|
2006-08-01 02:44:51 +02:00
|
|
|
self.entrygroup.UpdateServiceTxt(avahi.IF_UNSPEC, avahi.PROTO_UNSPEC, dbus.UInt32(0), self.name, self.stype,'', txt, reply_handler=self.service_updated_callback, error_handler=self.print_error_callback)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2006-05-26 12:56:21 +02:00
|
|
|
|
2006-06-28 00:28:49 +02:00
|
|
|
|
2006-05-26 16:43:51 +02:00
|
|
|
# END Zeroconf
|
|
|
|
|
|
|
|
'''
|
2006-06-28 00:28:49 +02:00
|
|
|
# how to use
|
2006-05-29 21:57:39 +02:00
|
|
|
|
2006-05-26 21:18:13 +02:00
|
|
|
zeroconf = Zeroconf()
|
|
|
|
zeroconf.connect()
|
2006-05-30 01:03:20 +02:00
|
|
|
zeroconf.txt['1st'] = 'foo'
|
|
|
|
zeroconf.txt['last'] = 'bar'
|
2006-09-13 20:49:34 +02:00
|
|
|
zeroconf.txt['email'] = foo@bar.org
|
2006-05-26 21:18:13 +02:00
|
|
|
zeroconf.announce()
|
|
|
|
|
|
|
|
# updating after announcing
|
|
|
|
txt = {}
|
2006-05-29 21:57:39 +02:00
|
|
|
txt['status'] = 'avail'
|
2006-05-26 21:18:13 +02:00
|
|
|
txt['msg'] = 'Here I am'
|
|
|
|
zeroconf.update_txt(txt)
|
2006-05-26 16:43:51 +02:00
|
|
|
'''
|