1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 This module contains the low-level implementations of xmpppy connect methods or
19 (in other words) transports for xmpp-stanzas.
20 Currently here is three transports:
21 direct TCP connect - TCPsocket class
22 proxied TCP connect - HTTPPROXYsocket class (CONNECT proxies)
23 TLS connection - TLS class. Can be used for SSL connections also.
24
25 Transports are stackable so you - f.e. TLS use HTPPROXYsocket or TCPsocket as more low-level transport.
26
27 Also exception 'error' is defined to allow capture of this module specific exceptions.
28 """
29
30 import socket,select,base64,dispatcher,sys
31 from simplexml import ustr
32 from client import PlugIn
33 from protocol import *
34
35
36 HAVE_DNSPYTHON = False
37 HAVE_PYDNS = False
38 try:
39 import dns.resolver
40 HAVE_DNSPYTHON = True
41 except ImportError:
42 try:
43 import DNS
44 HAVE_PYDNS = True
45 except ImportError:
46
47 sys.stderr.write("Could not load one of the supported DNS libraries (dnspython or pydns). SRV records will not be queried and you may need to set custom hostname/port for some servers to be accessible.\n")
48
49 DATA_RECEIVED='DATA RECEIVED'
50 DATA_SENT='DATA SENT'
51
53 """An exception to be raised in case of low-level errors in methods of 'transports' module."""
55 """Cache the descriptive string"""
56 self._comment=comment
57
59 """Serialise exception into pre-cached descriptive string."""
60 return self._comment
61
62 BUFLEN=1024
64 """ This class defines direct TCP connection method. """
65 - def __init__(self, server=None, use_srv=True):
66 """ Cache connection point 'server'. 'server' is the tuple of (host, port)
67 absolutely the same as standard tcp socket uses. """
68 PlugIn.__init__(self)
69 self.DBG_LINE='socket'
70 self._exported_methods=[self.send,self.disconnect]
71
72
73 if use_srv and (HAVE_DNSPYTHON or HAVE_PYDNS):
74 host, port = server
75 possible_queries = ['_xmpp-client._tcp.' + host]
76
77 for query in possible_queries:
78 try:
79 if HAVE_DNSPYTHON:
80 answers = [x for x in dns.resolver.query(query, 'SRV')]
81 if answers:
82 host = str(answers[0].target)
83 port = int(answers[0].port)
84 break
85 elif HAVE_PYDNS:
86
87 DNS.ParseResolvConf()
88 response = DNS.Request().req(query, qtype='SRV')
89 answers = response.answers
90 if len(answers) > 0:
91
92 _, _, port, host = answers[0]['data']
93 del _
94 port = int(port)
95 break
96 except:
97
98 print 'An error occurred while looking up %s' % query
99 server = (host, port)
100
101
102 self._server = server
103
105 """ Fire up connection. Return non-empty string on success.
106 Also registers self.disconnected method in the owner's dispatcher.
107 Called internally. """
108 if not self._server: self._server=(self._owner.Server,5222)
109 if not self.connect(self._server): return
110 self._owner.Connection=self
111 self._owner.RegisterDisconnectHandler(self.disconnected)
112 return 'ok'
113
115 """ Return the 'host' value that is connection is [will be] made to."""
116 return self._server[0]
118 """ Return the 'port' value that is connection is [will be] made to."""
119 return self._server[1]
120
122 """ Try to connect. Returns non-empty string on success. """
123 try:
124 if not server: server=self._server
125 self._sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
126 self._sock.connect((server[0], int(server[1])))
127 self._send=self._sock.sendall
128 self._recv=self._sock.recv
129 self.DEBUG("Successfully connected to remote host %s"%`server`,'start')
130 return 'ok'
131 except socket.error, (errno, strerror):
132 self.DEBUG("Failed to connect to remote host %s: %s (%s)"%(`server`, strerror, errno),'error')
133 except: pass
134
136 """ Disconnect from the remote server and unregister self.disconnected method from
137 the owner's dispatcher. """
138 self._sock.close()
139 if self._owner.__dict__.has_key('Connection'):
140 del self._owner.Connection
141 self._owner.UnregisterDisconnectHandler(self.disconnected)
142
144 """ Reads all pending incoming data.
145 In case of disconnection calls owner's disconnected() method and then raises IOError exception."""
146 try: received = self._recv(BUFLEN)
147 except socket.sslerror,e:
148 self._seen_data=0
149 if e[0]==socket.SSL_ERROR_WANT_READ: return ''
150 if e[0]==socket.SSL_ERROR_WANT_WRITE: return ''
151 self.DEBUG('Socket error while receiving data','error')
152 sys.exc_clear()
153 self._owner.disconnected()
154 raise IOError("Disconnected from server")
155 except: received = ''
156
157 while self.pending_data(0):
158 try: add = self._recv(BUFLEN)
159 except: add=''
160 received +=add
161 if not add: break
162
163 if len(received):
164 self._seen_data=1
165 self.DEBUG(received,'got')
166 if hasattr(self._owner, 'Dispatcher'):
167 self._owner.Dispatcher.Event('', DATA_RECEIVED, received)
168 else:
169 self.DEBUG('Socket error while receiving data','error')
170 self._owner.disconnected()
171 raise IOError("Disconnected from server")
172 return received
173
174 - def send(self,raw_data):
175 """ Writes raw outgoing data. Blocks until done.
176 If supplied data is unicode string, encodes it to utf-8 before send."""
177 if type(raw_data)==type(u''): raw_data = raw_data.encode('utf-8')
178 elif type(raw_data)<>type(''): raw_data = ustr(raw_data).encode('utf-8')
179 try:
180 self._send(raw_data)
181
182 if raw_data.strip():
183 self.DEBUG(raw_data,'sent')
184 self._owner.Dispatcher.Event('', DATA_SENT, raw_data)
185 except:
186 self.DEBUG("Socket error while sending data",'error')
187 self._owner.disconnected()
188
190 """ Returns true if there is a data ready to be read. """
191 return select.select([self._sock],[],[],timeout)[0]
192
194 """ Closes the socket. """
195 self.DEBUG("Closing socket",'stop')
196 self._sock.close()
197
199 """ Called when a Network Error or disconnection occurs.
200 Designed to be overidden. """
201 self.DEBUG("Socket operation failed",'error')
202
203 DBG_CONNECT_PROXY='CONNECTproxy'
205 """ HTTP (CONNECT) proxy connection class. Uses TCPsocket as the base class
206 redefines only connect method. Allows to use HTTP proxies like squid with
207 (optionally) simple authentication (using login and password). """
208 - def __init__(self,proxy,server,use_srv=True):
209 """ Caches proxy and target addresses.
210 'proxy' argument is a dictionary with mandatory keys 'host' and 'port' (proxy address)
211 and optional keys 'user' and 'password' to use for authentication.
212 'server' argument is a tuple of host and port - just like TCPsocket uses. """
213 TCPsocket.__init__(self,server,use_srv)
214 self.DBG_LINE=DBG_CONNECT_PROXY
215 self._proxy=proxy
216
218 """ Starts connection. Used interally. Returns non-empty string on success."""
219 owner.debug_flags.append(DBG_CONNECT_PROXY)
220 return TCPsocket.plugin(self,owner)
221
223 """ Starts connection. Connects to proxy, supplies login and password to it
224 (if were specified while creating instance). Instructs proxy to make
225 connection to the target server. Returns non-empty sting on success. """
226 if not TCPsocket.connect(self,(self._proxy['host'],self._proxy['port'])): return
227 self.DEBUG("Proxy server contacted, performing authentification",'start')
228 connector = ['CONNECT %s:%s HTTP/1.0'%self._server,
229 'Proxy-Connection: Keep-Alive',
230 'Pragma: no-cache',
231 'Host: %s:%s'%self._server,
232 'User-Agent: HTTPPROXYsocket/v0.1']
233 if self._proxy.has_key('user') and self._proxy.has_key('password'):
234 credentials = '%s:%s'%(self._proxy['user'],self._proxy['password'])
235 credentials = base64.encodestring(credentials).strip()
236 connector.append('Proxy-Authorization: Basic '+credentials)
237 connector.append('\r\n')
238 self.send('\r\n'.join(connector))
239 try: reply = self.receive().replace('\r','')
240 except IOError:
241 self.DEBUG('Proxy suddenly disconnected','error')
242 self._owner.disconnected()
243 return
244 try: proto,code,desc=reply.split('\n')[0].split(' ',2)
245 except: raise error('Invalid proxy reply')
246 if code<>'200':
247 self.DEBUG('Invalid proxy reply: %s %s %s'%(proto,code,desc),'error')
248 self._owner.disconnected()
249 return
250 while reply.find('\n\n') == -1:
251 try: reply += self.receive().replace('\r','')
252 except IOError:
253 self.DEBUG('Proxy suddenly disconnected','error')
254 self._owner.disconnected()
255 return
256 self.DEBUG("Authentification successfull. Jabber server contacted.",'ok')
257 return 'ok'
258
259 - def DEBUG(self,text,severity):
260 """Overwrites DEBUG tag to allow debug output be presented as "CONNECTproxy"."""
261 return self._owner.DEBUG(DBG_CONNECT_PROXY,text,severity)
262
264 """ TLS connection used to encrypts already estabilished tcp connection."""
265 - def PlugIn(self,owner,now=0):
279
286
298
300 """ Returns true if there possible is a data ready to be read. """
301 return self._tcpsock._seen_data or select.select([self._tcpsock._sock],[],[],timeout)[0]
302
304 """ Immidiatedly switch socket to TLS mode. Used internally."""
305 """ Here we should switch pending_data to hint mode."""
306 tcpsock=self._owner.Connection
307 tcpsock._sslObj = socket.ssl(tcpsock._sock, None, None)
308 tcpsock._sslIssuer = tcpsock._sslObj.issuer()
309 tcpsock._sslServer = tcpsock._sslObj.server()
310 tcpsock._recv = tcpsock._sslObj.read
311 tcpsock._send = tcpsock._sslObj.write
312
313 tcpsock._seen_data=1
314 self._tcpsock=tcpsock
315 tcpsock.pending_data=self.pending_data
316 tcpsock._sock.setblocking(0)
317
318 self.starttls='success'
319
321 """ Handle server reply if TLS is allowed to process. Behaves accordingly.
322 Used internally."""
323 if starttls.getNamespace()<>NS_TLS: return
324 self.starttls=starttls.getName()
325 if self.starttls=='failure':
326 self.DEBUG("Got starttls response: "+self.starttls,'error')
327 return
328 self.DEBUG("Got starttls proceed response. Switching to TLS/SSL...",'ok')
329 self._startSSL()
330 self._owner.Dispatcher.PlugOut()
331 dispatcher.Dispatcher().PlugIn(self._owner)
332