Merge lp:~cyli/divmod.org/remove-vertex into lp:divmod.org
- remove-vertex
- Merge into trunk
Proposed by
cyli
Status: | Merged |
---|---|
Approved by: | Allen Short |
Approved revision: | 2702 |
Merged at revision: | 2702 |
Proposed branch: | lp:~cyli/divmod.org/remove-vertex |
Merge into: | lp:divmod.org |
Diff against target: |
9122 lines (+1/-8893) 41 files modified
Divmod.pth (+1/-2) Vertex/DEPS.txt (+0/-5) Vertex/LICENSE (+0/-20) Vertex/MANIFEST.in (+0/-4) Vertex/NAME.txt (+0/-10) Vertex/NEWS.txt (+0/-20) Vertex/README.txt (+0/-12) Vertex/bin/gvertex (+0/-7) Vertex/bin/vertex (+0/-7) Vertex/doc/notes (+0/-61) Vertex/doc/q2q-standalone.tac (+0/-5) Vertex/prime/plugins/vertex_client.py (+0/-4) Vertex/setup.py (+0/-31) Vertex/vertex/__init__.py (+0/-3) Vertex/vertex/_version.py (+0/-3) Vertex/vertex/bits.py (+0/-142) Vertex/vertex/conncache.py (+0/-160) Vertex/vertex/depserv.py (+0/-197) Vertex/vertex/endpoint.py (+0/-56) Vertex/vertex/gtk2hack.glade (+0/-635) Vertex/vertex/gtk2hack.py (+0/-270) Vertex/vertex/ivertex.py (+0/-108) Vertex/vertex/ptcp.py (+0/-1050) Vertex/vertex/q2q.py (+0/-2763) Vertex/vertex/q2qadmin.py (+0/-21) Vertex/vertex/q2qclient.py (+0/-453) Vertex/vertex/q2qstandalone.py (+0/-108) Vertex/vertex/sigma.py (+0/-760) Vertex/vertex/statemachine.py (+0/-56) Vertex/vertex/subproducer.py (+0/-125) Vertex/vertex/tcpdfa.py (+0/-96) Vertex/vertex/test/__init__.py (+0/-3) Vertex/vertex/test/mock_data.py (+0/-2) Vertex/vertex/test/test_bits.py (+0/-68) Vertex/vertex/test/test_client.py (+0/-30) Vertex/vertex/test/test_dependencyservice.py (+0/-69) Vertex/vertex/test/test_ptcp.py (+0/-365) Vertex/vertex/test/test_q2q.py (+0/-712) Vertex/vertex/test/test_sigma.py (+0/-210) Vertex/vertex/test/test_statemachine.py (+0/-67) Vertex/vertex/test/test_subproducer.py (+0/-173) |
To merge this branch: | bzr merge lp:~cyli/divmod.org/remove-vertex |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Allen Short | Approve | ||
Review via email: mp+171717@code.launchpad.net |
Commit message
Description of the change
Proposing to remove Vertex from the divmod repos. I found only one branch with a merge proposal for it:
https:/
I've ported trunk as well as alfred-54's branch to https:/
To post a comment you must log in.
Revision history for this message
Allen Short (washort) : | # |
review:
Approve
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'Divmod.pth' | |||
2 | --- Divmod.pth 2013-01-02 10:08:46 +0000 | |||
3 | +++ Divmod.pth 2013-06-27 06:07:28 +0000 | |||
4 | @@ -1,4 +1,4 @@ | |||
6 | 1 | # -*- test-case-name: axiom,combinator,epsilon,xmantissa,nevow,formless,xquotient,reverend,sine,vertex,hyperbola,imaginary,examplegame -*- | 1 | # -*- test-case-name: axiom,combinator,epsilon,xmantissa,nevow,formless,xquotient,reverend,sine,hyperbola,imaginary,examplegame -*- |
7 | 2 | Axiom | 2 | Axiom |
8 | 3 | Combinator | 3 | Combinator |
9 | 4 | Epsilon | 4 | Epsilon |
10 | @@ -7,7 +7,6 @@ | |||
11 | 7 | Quotient | 7 | Quotient |
12 | 8 | Reverend | 8 | Reverend |
13 | 9 | Sine | 9 | Sine |
14 | 10 | Vertex | ||
15 | 11 | Hyperbola | 10 | Hyperbola |
16 | 12 | Imaginary | 11 | Imaginary |
17 | 13 | Imaginary/ExampleGame | 12 | Imaginary/ExampleGame |
18 | 14 | 13 | ||
19 | === removed directory 'Vertex' | |||
20 | === removed file 'Vertex/DEPS.txt' | |||
21 | --- Vertex/DEPS.txt 2006-06-14 11:54:41 +0000 | |||
22 | +++ Vertex/DEPS.txt 1970-01-01 00:00:00 +0000 | |||
23 | @@ -1,5 +0,0 @@ | |||
24 | 1 | Python 2.4 | ||
25 | 2 | Twisted 2.4.0 | ||
26 | 3 | PyOpenSSL 0.6 | ||
27 | 4 | OpenSSL 0.9.7 | ||
28 | 5 | Epsilon 0.5.0 | ||
29 | 6 | 0 | ||
30 | === removed file 'Vertex/LICENSE' | |||
31 | --- Vertex/LICENSE 2005-12-10 22:31:51 +0000 | |||
32 | +++ Vertex/LICENSE 1970-01-01 00:00:00 +0000 | |||
33 | @@ -1,20 +0,0 @@ | |||
34 | 1 | Copyright (c) 2005 Divmod Inc. | ||
35 | 2 | |||
36 | 3 | Permission is hereby granted, free of charge, to any person obtaining | ||
37 | 4 | a copy of this software and associated documentation files (the | ||
38 | 5 | "Software"), to deal in the Software without restriction, including | ||
39 | 6 | without limitation the rights to use, copy, modify, merge, publish, | ||
40 | 7 | distribute, sublicense, and/or sell copies of the Software, and to | ||
41 | 8 | permit persons to whom the Software is furnished to do so, subject to | ||
42 | 9 | the following conditions: | ||
43 | 10 | |||
44 | 11 | The above copyright notice and this permission notice shall be | ||
45 | 12 | included in all copies or substantial portions of the Software. | ||
46 | 13 | |||
47 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
48 | 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
49 | 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
50 | 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
51 | 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
52 | 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
53 | 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
54 | 21 | \ No newline at end of file | 0 | \ No newline at end of file |
55 | 22 | 1 | ||
56 | === removed file 'Vertex/MANIFEST.in' | |||
57 | --- Vertex/MANIFEST.in 2006-06-14 11:54:41 +0000 | |||
58 | +++ Vertex/MANIFEST.in 1970-01-01 00:00:00 +0000 | |||
59 | @@ -1,4 +0,0 @@ | |||
60 | 1 | include LICENSE | ||
61 | 2 | include NAME.txt | ||
62 | 3 | include DEPS.txt | ||
63 | 4 | include doc/q2q-standalone.tac | ||
64 | 5 | 0 | ||
65 | === removed file 'Vertex/NAME.txt' | |||
66 | --- Vertex/NAME.txt 2005-08-27 23:09:07 +0000 | |||
67 | +++ Vertex/NAME.txt 1970-01-01 00:00:00 +0000 | |||
68 | @@ -1,10 +0,0 @@ | |||
69 | 1 | |||
70 | 2 | See: http://mathworld.wolfram.com/Vertex.html | ||
71 | 3 | |||
72 | 4 | A vertex in mathematics is a location where two or more lines or edges meet. | ||
73 | 5 | |||
74 | 6 | Divmod Vertex is an implementation of and interface to the Q2Q protocol. It is | ||
75 | 7 | named for a vertext because it provides a way for peers on the internet to | ||
76 | 8 | establish connections with each other, e.g. to make their connection lines | ||
77 | 9 | meet, regardless of intermediary interference such as network address | ||
78 | 10 | translators and lack of naming information. | ||
79 | 11 | 0 | ||
80 | === removed file 'Vertex/NEWS.txt' | |||
81 | --- Vertex/NEWS.txt 2009-11-30 01:08:55 +0000 | |||
82 | +++ Vertex/NEWS.txt 1970-01-01 00:00:00 +0000 | |||
83 | @@ -1,20 +0,0 @@ | |||
84 | 1 | 0.3.0 (2009-11-25): | ||
85 | 2 | - Remove use of deprecated Twisted APIs from the test suite and improve | ||
86 | 3 | some error handling as a necessary consequence. | ||
87 | 4 | - Use twisted.internet.ssl instead of epsilon.sslverify. | ||
88 | 5 | - Remove an implementation of deferLater. | ||
89 | 6 | |||
90 | 7 | 0.2.0 (2006-06-12): | ||
91 | 8 | - Moved JUICE implementation into Epsilon. | ||
92 | 9 | - Removed dependency on Nevow's formless. | ||
93 | 10 | - Clarify licensing terms. | ||
94 | 11 | - Fix bugs on 64-bit platforms. | ||
95 | 12 | - removed buggy legacy non-TLS options which would break negotiation with | ||
96 | 13 | OpenSSL 0.9.8a. | ||
97 | 14 | - Deprecated twisted test APIs removed. | ||
98 | 15 | - First phase of integration with twisted.cred; vertex endpoints can now be | ||
99 | 16 | authenticated against a Twisted UsernamePassword cred authenticator. | ||
100 | 17 | |||
101 | 18 | 0.1.0 (2005-10-10): | ||
102 | 19 | |||
103 | 20 | - Initial release. | ||
104 | 21 | 0 | ||
105 | === removed file 'Vertex/README.txt' | |||
106 | --- Vertex/README.txt 2006-06-14 11:54:41 +0000 | |||
107 | +++ Vertex/README.txt 1970-01-01 00:00:00 +0000 | |||
108 | @@ -1,12 +0,0 @@ | |||
109 | 1 | |||
110 | 2 | Divmod Vertex | ||
111 | 3 | ============= | ||
112 | 4 | |||
113 | 5 | Divmod Vertex is the first implementation of the Q2Q protocol, which is a | ||
114 | 6 | peer-to-peer communication protocol for establishing stream-based communication | ||
115 | 7 | between named endpoints. | ||
116 | 8 | |||
117 | 9 | It is also a P2P application client and server platform in the early stages of | ||
118 | 10 | development. It is currently quite usable for knocking holes in firewalls, but | ||
119 | 11 | requires some polish to really be usable. | ||
120 | 12 | |||
121 | 13 | 0 | ||
122 | === removed directory 'Vertex/bin' | |||
123 | === removed file 'Vertex/bin/gvertex' | |||
124 | --- Vertex/bin/gvertex 2006-03-08 04:10:37 +0000 | |||
125 | +++ Vertex/bin/gvertex 1970-01-01 00:00:00 +0000 | |||
126 | @@ -1,7 +0,0 @@ | |||
127 | 1 | #!/usr/bin/python | ||
128 | 2 | # Copyright 2005 Divmod, Inc. See LICENSE file for details | ||
129 | 3 | |||
130 | 4 | from vertex.gtk2hack import main | ||
131 | 5 | |||
132 | 6 | if __name__ == '__main__': | ||
133 | 7 | main() | ||
134 | 8 | 0 | ||
135 | === removed file 'Vertex/bin/vertex' | |||
136 | --- Vertex/bin/vertex 2005-08-10 22:20:03 +0000 | |||
137 | +++ Vertex/bin/vertex 1970-01-01 00:00:00 +0000 | |||
138 | @@ -1,7 +0,0 @@ | |||
139 | 1 | #!/usr/bin/python | ||
140 | 2 | # Copyright 2005 Divmod, Inc. See LICENSE file for details | ||
141 | 3 | |||
142 | 4 | from vertex.q2qclient import Q2QClientProgram | ||
143 | 5 | |||
144 | 6 | if __name__ == '__main__': | ||
145 | 7 | Q2QClientProgram().parseOptions() | ||
146 | 8 | 0 | ||
147 | === removed directory 'Vertex/doc' | |||
148 | === removed file 'Vertex/doc/notes' | |||
149 | --- Vertex/doc/notes 2005-08-05 03:13:02 +0000 | |||
150 | +++ Vertex/doc/notes 1970-01-01 00:00:00 +0000 | |||
151 | @@ -1,61 +0,0 @@ | |||
152 | 1 | |||
153 | 2 | Gin | ||
154 | 3 | === | ||
155 | 4 | |||
156 | 5 | TCP-alike over UDP | ||
157 | 6 | |||
158 | 7 | Packet Format | ||
159 | 8 | ============= | ||
160 | 9 | |||
161 | 10 | Connection ID - 32 bits | ||
162 | 11 | Sequence Number - 32 bits | ||
163 | 12 | Ack number - 32 bits | ||
164 | 13 | Window - 32 bits | ||
165 | 14 | Flags (SYN, ACK, FIN, IGN) - 8 bits | ||
166 | 15 | Checksum - 32 bits | ||
167 | 16 | Data length in bytes - 16 bits | ||
168 | 17 | Data - See previous | ||
169 | 18 | |||
170 | 19 | SEMANTICS | ||
171 | 20 | ========= | ||
172 | 21 | Different types of packets: | ||
173 | 22 | |||
174 | 23 | just SYN: | ||
175 | 24 | SYN+ACK: | ||
176 | 25 | just ACK: normal tcp packet meanings | ||
177 | 26 | |||
178 | 27 | SYN+NAT: Request for information about the sender's public address | ||
179 | 28 | |||
180 | 29 | ACK+NAT: Response with information about the recipient's public address. The | ||
181 | 30 | data is the IP address and port number to which the packet is being sent, | ||
182 | 31 | formatted as a dotted-quad formatted IP address followed by a colon followed by | ||
183 | 32 | the base-10 string representation of the port number.. | ||
184 | 33 | |||
185 | 34 | STB: Size Too Big - a packet with a dlen greater than the length of its data | ||
186 | 35 | was received. | ||
187 | 36 | |||
188 | 37 | Other Stuff: | ||
189 | 38 | |||
190 | 39 | Connection IDs uniquely identify a stream for a protocol. All bytes | ||
191 | 40 | associated with a particular connectionID will be delivered to the | ||
192 | 41 | same protocol instance. | ||
193 | 42 | |||
194 | 43 | Sequence Numbers indicate the senders notion of how far into its | ||
195 | 44 | outgoing stream this packet is. Sequence numbers start from a | ||
196 | 45 | pseudo-random value within the allowed range and are incremented by | ||
197 | 46 | the number of bytes in each packet transmitted (re-transmits | ||
198 | 47 | discounted). This indicates to the peer where the bytes in each | ||
199 | 48 | packet lie in the stream, allowing ordered delivery. | ||
200 | 49 | |||
201 | 50 | Ack numbers indicate the senders notion of how far into its incoming | ||
202 | 51 | stream all data has been received. This value is always what the | ||
203 | 52 | sender expects the receiver to use as its next sequence number. | ||
204 | 53 | |||
205 | 54 | Window indicates the number of bytes in advance of the senders ack | ||
206 | 55 | number the receiver may proceed in sending. This receiver's sequence | ||
207 | 56 | numbers must never be greater than the sender's last ack number plus | ||
208 | 57 | their window number. | ||
209 | 58 | |||
210 | 59 | Checksum is a CRC 32 of the data (and only the data - wait maybe this | ||
211 | 60 | should be the header less the checksum, too, in case things get | ||
212 | 61 | corrupted there) | ||
213 | 62 | 0 | ||
214 | === removed file 'Vertex/doc/q2q-standalone.tac' | |||
215 | --- Vertex/doc/q2q-standalone.tac 2005-08-10 22:20:03 +0000 | |||
216 | +++ Vertex/doc/q2q-standalone.tac 1970-01-01 00:00:00 +0000 | |||
217 | @@ -1,5 +0,0 @@ | |||
218 | 1 | # Copyright 2005 Divmod, Inc. See LICENSE file for details | ||
219 | 2 | from vertex.q2qstandalone import defaultConfig | ||
220 | 3 | |||
221 | 4 | application = defaultConfig() | ||
222 | 5 | |||
223 | 6 | 0 | ||
224 | === removed directory 'Vertex/prime' | |||
225 | === removed directory 'Vertex/prime/plugins' | |||
226 | === removed file 'Vertex/prime/plugins/vertex_client.py' | |||
227 | --- Vertex/prime/plugins/vertex_client.py 2006-06-01 04:57:02 +0000 | |||
228 | +++ Vertex/prime/plugins/vertex_client.py 1970-01-01 00:00:00 +0000 | |||
229 | @@ -1,4 +0,0 @@ | |||
230 | 1 | |||
231 | 2 | from vertex.gtk2hack import PlugEntry | ||
232 | 3 | |||
233 | 4 | pe = PlugEntry() | ||
234 | 5 | 0 | ||
235 | === removed file 'Vertex/setup.py' | |||
236 | --- Vertex/setup.py 2009-11-30 01:08:55 +0000 | |||
237 | +++ Vertex/setup.py 1970-01-01 00:00:00 +0000 | |||
238 | @@ -1,31 +0,0 @@ | |||
239 | 1 | from epsilon import setuphelper | ||
240 | 2 | |||
241 | 3 | from vertex import version | ||
242 | 4 | |||
243 | 5 | setuphelper.autosetup( | ||
244 | 6 | name="Vertex", | ||
245 | 7 | version=version.short(), | ||
246 | 8 | maintainer="Divmod, Inc.", | ||
247 | 9 | maintainer_email="support@divmod.org", | ||
248 | 10 | url="http://divmod.org/trac/wiki/DivmodVertex", | ||
249 | 11 | license="MIT", | ||
250 | 12 | platforms=["any"], | ||
251 | 13 | description= | ||
252 | 14 | """ | ||
253 | 15 | Divmod Vertex is the first implementation of the Q2Q protocol, which | ||
254 | 16 | is a peer-to-peer communication protocol for establishing | ||
255 | 17 | stream-based communication between named endpoints. | ||
256 | 18 | """, | ||
257 | 19 | classifiers=[ | ||
258 | 20 | "Development Status :: 2 - Pre-Alpha", | ||
259 | 21 | "Framework :: Twisted", | ||
260 | 22 | "Intended Audience :: Developers", | ||
261 | 23 | "License :: OSI Approved :: MIT License", | ||
262 | 24 | "Programming Language :: Python", | ||
263 | 25 | "Topic :: Communications", | ||
264 | 26 | "Topic :: Internet", | ||
265 | 27 | "Topic :: Internet :: File Transfer Protocol (FTP)", | ||
266 | 28 | "Topic :: Internet :: Name Service (DNS)", | ||
267 | 29 | "Topic :: Software Development :: Libraries :: Python Modules", | ||
268 | 30 | ], | ||
269 | 31 | ) | ||
270 | 32 | 0 | ||
271 | === removed directory 'Vertex/vertex' | |||
272 | === removed file 'Vertex/vertex/__init__.py' | |||
273 | --- Vertex/vertex/__init__.py 2008-08-11 11:19:59 +0000 | |||
274 | +++ Vertex/vertex/__init__.py 1970-01-01 00:00:00 +0000 | |||
275 | @@ -1,3 +0,0 @@ | |||
276 | 1 | # -*- test-case-name: vertex.test -*- | ||
277 | 2 | |||
278 | 3 | from vertex._version import version | ||
279 | 4 | 0 | ||
280 | === removed file 'Vertex/vertex/_version.py' | |||
281 | --- Vertex/vertex/_version.py 2009-11-30 01:08:55 +0000 | |||
282 | +++ Vertex/vertex/_version.py 1970-01-01 00:00:00 +0000 | |||
283 | @@ -1,3 +0,0 @@ | |||
284 | 1 | # This is an auto-generated file. Use admin/change-versions to update. | ||
285 | 2 | from twisted.python import versions | ||
286 | 3 | version = versions.Version(__name__[:__name__.rfind('.')], 0, 3, 0) | ||
287 | 4 | 0 | ||
288 | === removed file 'Vertex/vertex/bits.py' | |||
289 | --- Vertex/vertex/bits.py 2005-08-05 06:02:56 +0000 | |||
290 | +++ Vertex/vertex/bits.py 1970-01-01 00:00:00 +0000 | |||
291 | @@ -1,142 +0,0 @@ | |||
292 | 1 | # Copyright 2005 Divmod, Inc. See LICENSE file for details | ||
293 | 2 | # -*- test-case-name: vertex.test.test_bits -*- | ||
294 | 3 | """ The purpose of this module is to provide the class BitArray, a compact | ||
295 | 4 | overlay onto an array of bytes which is instead bit-addressable. It also | ||
296 | 5 | includes several bitwise operators. | ||
297 | 6 | |||
298 | 7 | It does not include all array operations yet, most notably those related to | ||
299 | 8 | slicing, since it is written primarily for use by the swarming implementation | ||
300 | 9 | and swarming only requires fixed-size bit masks. | ||
301 | 10 | |||
302 | 11 | """ | ||
303 | 12 | |||
304 | 13 | __metaclass__ = type | ||
305 | 14 | |||
306 | 15 | import array | ||
307 | 16 | import operator | ||
308 | 17 | import math | ||
309 | 18 | |||
310 | 19 | BITS_PER_BYTE = 8 | ||
311 | 20 | |||
312 | 21 | def operate(operation): | ||
313 | 22 | # XXX TODO: optimize this and countbits later | ||
314 | 23 | def __x__(self, other): | ||
315 | 24 | if len(self) < len(other): | ||
316 | 25 | return operation(other, self) | ||
317 | 26 | new = BitArray(size=len(self)) | ||
318 | 27 | for offt, (mybit, hisbit) in enumerate(zip(self, other)): | ||
319 | 28 | result = new[offt] = operation(mybit, hisbit) | ||
320 | 29 | |||
321 | 30 | for j in range(offt+1, len(self)): | ||
322 | 31 | new[j] = operation(self[j], 0) | ||
323 | 32 | return new | ||
324 | 33 | return __x__ | ||
325 | 34 | |||
326 | 35 | |||
327 | 36 | class BitArray: | ||
328 | 37 | """ | ||
329 | 38 | A large mutable array of bits. | ||
330 | 39 | """ | ||
331 | 40 | |||
332 | 41 | def __init__(self, bytes=None, size=None, default=0): | ||
333 | 42 | if bytes is None and size is None: | ||
334 | 43 | size = 0 | ||
335 | 44 | if bytes is None: | ||
336 | 45 | bytes = array.array("B") | ||
337 | 46 | bytesize = int(math.ceil(float(size) / BITS_PER_BYTE)) | ||
338 | 47 | if default: | ||
339 | 48 | padbyte = 255 | ||
340 | 49 | else: | ||
341 | 50 | padbyte = 0 | ||
342 | 51 | bytes.fromlist([padbyte] * bytesize) | ||
343 | 52 | self.bytes = bytes | ||
344 | 53 | if size is None: | ||
345 | 54 | size = len(self.bytes) * self.bytes.itemsize * BITS_PER_BYTE | ||
346 | 55 | self.size = size | ||
347 | 56 | |||
348 | 57 | # initialize 'on' and 'off' lists to optimize various things | ||
349 | 58 | self.on = [] | ||
350 | 59 | self.off = [] | ||
351 | 60 | blists = self.blists = self.off, self.on | ||
352 | 61 | |||
353 | 62 | for index, bit in enumerate(self): | ||
354 | 63 | blists[bit].append(index) | ||
355 | 64 | |||
356 | 65 | def append(self, bit): | ||
357 | 66 | offt = self.size | ||
358 | 67 | self.size += 1 | ||
359 | 68 | if (len(self.bytes) * self.bytes.itemsize * BITS_PER_BYTE) < self.size: | ||
360 | 69 | self.bytes.append(0) | ||
361 | 70 | self[offt] = bit | ||
362 | 71 | |||
363 | 72 | def any(self, req=1): | ||
364 | 73 | return bool(self.blists[req]) | ||
365 | 74 | |||
366 | 75 | def percent(self): | ||
367 | 76 | """ | ||
368 | 77 | debugging method; returns a string indicating percentage completion | ||
369 | 78 | """ | ||
370 | 79 | if not len(self): | ||
371 | 80 | return 'Inf%' | ||
372 | 81 | return '%0.2f%%'% ((float(self.countbits()) / len(self)) * 100,) | ||
373 | 82 | |||
374 | 83 | def __getitem__(self, bitcount): | ||
375 | 84 | if bitcount < 0: | ||
376 | 85 | bitcount += self.size | ||
377 | 86 | if bitcount >= self.size: | ||
378 | 87 | raise IndexError("%r >= %r" % (bitcount, self.size)) | ||
379 | 88 | div, mod = divmod(bitcount, self.bytes.itemsize * BITS_PER_BYTE) | ||
380 | 89 | byte = self.bytes[div] | ||
381 | 90 | return (byte >> mod) & 1 | ||
382 | 91 | |||
383 | 92 | def __setitem__(self, bitcount, bit): | ||
384 | 93 | if bitcount < 0: | ||
385 | 94 | bitcount += self.size | ||
386 | 95 | if bitcount >= self.size: | ||
387 | 96 | raise IndexError("bitcount too big") | ||
388 | 97 | div, mod = divmod(bitcount, self.bytes.itemsize * BITS_PER_BYTE) | ||
389 | 98 | if bit: | ||
390 | 99 | self.bytes[div] |= 1 << mod | ||
391 | 100 | else: | ||
392 | 101 | self.bytes[div] &= ~(1 << mod) | ||
393 | 102 | |||
394 | 103 | # change updating | ||
395 | 104 | notbitlist = self.blists[not bit] | ||
396 | 105 | try: | ||
397 | 106 | notbitlist.remove(bitcount) | ||
398 | 107 | except ValueError: | ||
399 | 108 | pass | ||
400 | 109 | bitlist = self.blists[bit] | ||
401 | 110 | if bitcount not in bitlist: | ||
402 | 111 | bitlist.append(bitcount) | ||
403 | 112 | |||
404 | 113 | def __len__(self): | ||
405 | 114 | return self.size | ||
406 | 115 | |||
407 | 116 | def __repr__(self): | ||
408 | 117 | l = [] | ||
409 | 118 | l.append('[') | ||
410 | 119 | for b in self: | ||
411 | 120 | if b: | ||
412 | 121 | c = 'X' | ||
413 | 122 | else: | ||
414 | 123 | c = ' ' | ||
415 | 124 | l.append(c) | ||
416 | 125 | l.append(']') | ||
417 | 126 | return ''.join(l) | ||
418 | 127 | |||
419 | 128 | def countbits(self, on=True): | ||
420 | 129 | return len(self.blists[on]) | ||
421 | 130 | |||
422 | 131 | def positions(self, bit): | ||
423 | 132 | """ | ||
424 | 133 | An iterator of all positions that a bit holds in this BitArray. | ||
425 | 134 | |||
426 | 135 | @param bit: 1 or 0 | ||
427 | 136 | """ | ||
428 | 137 | return self.blists[bit][:] | ||
429 | 138 | |||
430 | 139 | __xor__ = operate(operator.xor) | ||
431 | 140 | __and__ = operate(operator.and_) | ||
432 | 141 | __or__ = operate(operator.or_) | ||
433 | 142 | |||
434 | 143 | 0 | ||
435 | === removed file 'Vertex/vertex/conncache.py' | |||
436 | --- Vertex/vertex/conncache.py 2009-07-06 11:40:18 +0000 | |||
437 | +++ Vertex/vertex/conncache.py 1970-01-01 00:00:00 +0000 | |||
438 | @@ -1,160 +0,0 @@ | |||
439 | 1 | # Copyright 2005 Divmod, Inc. See LICENSE file for details | ||
440 | 2 | # -*- test-case-name: vertex.test.test_q2q.TCPConnection.testSendingFiles -*- | ||
441 | 3 | |||
442 | 4 | """ | ||
443 | 5 | Connect between two endpoints using a message-based protocol to exchange | ||
444 | 6 | messages lazily in response to UI events, caching the protocol as necessary. | ||
445 | 7 | Using connection-oriented protocols, you will most likely not want to use this | ||
446 | 8 | class - you might end up retrieving a cached connection in the middle of a | ||
447 | 9 | chunk of data being sent. For the purposes of this distinction, a | ||
448 | 10 | 'message-oriented' protocol is one which has an API which either:: | ||
449 | 11 | |||
450 | 12 | a) writes only whole messages to its transport so there is never an | ||
451 | 13 | opportunity to insert data into the middle of a message, or | ||
452 | 14 | |||
453 | 15 | b) provides an API on the Protocol instance for queuing whole messages such | ||
454 | 16 | that if partial messages are sent, calling the API multiple times will | ||
455 | 17 | queue them internally so that clients do not need to care whether the | ||
456 | 18 | connection is made or not. | ||
457 | 19 | |||
458 | 20 | It is worth noting that all Juice-derived protocols meet constraint (b). | ||
459 | 21 | """ | ||
460 | 22 | |||
461 | 23 | from zope.interface import implements | ||
462 | 24 | |||
463 | 25 | from twisted.internet.defer import maybeDeferred, DeferredList, Deferred | ||
464 | 26 | from twisted.internet.main import CONNECTION_LOST | ||
465 | 27 | from twisted.internet import interfaces | ||
466 | 28 | from twisted.internet.protocol import ClientFactory | ||
467 | 29 | |||
468 | 30 | |||
469 | 31 | class ConnectionCache: | ||
470 | 32 | def __init__(self): | ||
471 | 33 | """ | ||
472 | 34 | """ | ||
473 | 35 | # map (fromAddress, toAddress, protoName): protocol instance | ||
474 | 36 | self.cachedConnections = {} | ||
475 | 37 | # map (fromAddress, toAddress, protoName): list of Deferreds | ||
476 | 38 | self.inProgress = {} | ||
477 | 39 | |||
478 | 40 | def connectCached(self, endpoint, protocolFactory, | ||
479 | 41 | extraWork=lambda x: x, | ||
480 | 42 | extraHash=None): | ||
481 | 43 | """See module docstring | ||
482 | 44 | """ | ||
483 | 45 | key = endpoint, extraHash | ||
484 | 46 | D = Deferred() | ||
485 | 47 | if key in self.cachedConnections: | ||
486 | 48 | D.callback(self.cachedConnections[key]) | ||
487 | 49 | elif key in self.inProgress: | ||
488 | 50 | self.inProgress[key].append(D) | ||
489 | 51 | else: | ||
490 | 52 | self.inProgress[key] = [D] | ||
491 | 53 | endpoint.connect( | ||
492 | 54 | _CachingClientFactory( | ||
493 | 55 | self, key, protocolFactory, | ||
494 | 56 | extraWork)) | ||
495 | 57 | return D | ||
496 | 58 | |||
497 | 59 | def cacheUnrequested(self, endpoint, extraHash, protocol): | ||
498 | 60 | self.connectionMadeForKey((endpoint, extraHash), protocol) | ||
499 | 61 | |||
500 | 62 | def connectionMadeForKey(self, key, protocol): | ||
501 | 63 | deferreds = self.inProgress.pop(key, []) | ||
502 | 64 | self.cachedConnections[key] = protocol | ||
503 | 65 | for d in deferreds: | ||
504 | 66 | d.callback(protocol) | ||
505 | 67 | |||
506 | 68 | def connectionLostForKey(self, key): | ||
507 | 69 | if key in self.cachedConnections: | ||
508 | 70 | del self.cachedConnections[key] | ||
509 | 71 | |||
510 | 72 | def connectionFailedForKey(self, key, reason): | ||
511 | 73 | deferreds = self.inProgress.pop(key) | ||
512 | 74 | for d in deferreds: | ||
513 | 75 | d.errback(reason) | ||
514 | 76 | |||
515 | 77 | def shutdown(self): | ||
516 | 78 | return DeferredList( | ||
517 | 79 | [maybeDeferred(p.transport.loseConnection) | ||
518 | 80 | for p in self.cachedConnections.values()]) | ||
519 | 81 | |||
520 | 82 | |||
521 | 83 | class _CachingClientFactory(ClientFactory): | ||
522 | 84 | debug = False | ||
523 | 85 | |||
524 | 86 | def __init__(self, cache, key, subFactory, extraWork): | ||
525 | 87 | """ | ||
526 | 88 | @param cache: a Q2QService | ||
527 | 89 | |||
528 | 90 | @param key: a 2-tuple of (endpoint, extra) that represents what | ||
529 | 91 | connections coming from this factory are for. | ||
530 | 92 | |||
531 | 93 | @param subFactory: a ClientFactory which I forward methods to. | ||
532 | 94 | |||
533 | 95 | @param extraWork: extraWork(proto) -> Deferred which fires when the | ||
534 | 96 | connection has been prepared sufficiently to be used by subsequent | ||
535 | 97 | connections and can be counted as a success. | ||
536 | 98 | """ | ||
537 | 99 | |||
538 | 100 | self.cache = cache | ||
539 | 101 | self.key = key | ||
540 | 102 | self.subFactory = subFactory | ||
541 | 103 | self.finishedExtraWork = False | ||
542 | 104 | self.extraWork = extraWork | ||
543 | 105 | |||
544 | 106 | lostAsFailReason = CONNECTION_LOST | ||
545 | 107 | |||
546 | 108 | def clientConnectionMade(self, protocol): | ||
547 | 109 | def success(reason): | ||
548 | 110 | self.cache.connectionMadeForKey(self.key, protocol) | ||
549 | 111 | self.finishedExtraWork = True | ||
550 | 112 | return protocol | ||
551 | 113 | |||
552 | 114 | def failed(reason): | ||
553 | 115 | self.lostAsFailReason = reason | ||
554 | 116 | protocol.transport.loseConnection() | ||
555 | 117 | return reason | ||
556 | 118 | maybeDeferred(self.extraWork, protocol).addCallbacks( | ||
557 | 119 | success, failed) | ||
558 | 120 | |||
559 | 121 | def clientConnectionLost(self, connector, reason): | ||
560 | 122 | if self.finishedExtraWork: | ||
561 | 123 | self.cache.connectionLostForKey(self.key) | ||
562 | 124 | else: | ||
563 | 125 | self.cache.connectionFailedForKey(self.key, | ||
564 | 126 | self.lostAsFailReason) | ||
565 | 127 | self.subFactory.clientConnectionLost(connector, reason) | ||
566 | 128 | |||
567 | 129 | def clientConnectionFailed(self, connector, reason): | ||
568 | 130 | self.cache.connectionFailedForKey(self.key, reason) | ||
569 | 131 | self.subFactory.clientConnectionFailed(connector, reason) | ||
570 | 132 | |||
571 | 133 | def buildProtocol(self, addr): | ||
572 | 134 | return _CachingTransportShim(self, self.subFactory.buildProtocol(addr)) | ||
573 | 135 | |||
574 | 136 | |||
575 | 137 | class _CachingTransportShim: | ||
576 | 138 | disconnecting = property(lambda self: self.transport.disconnecting) | ||
577 | 139 | |||
578 | 140 | implements(interfaces.IProtocol) | ||
579 | 141 | |||
580 | 142 | def __init__(self, factory, protocol): | ||
581 | 143 | self.factory = factory | ||
582 | 144 | self.protocol = protocol | ||
583 | 145 | |||
584 | 146 | # IProtocol | ||
585 | 147 | self.dataReceived = protocol.dataReceived | ||
586 | 148 | self.connectionLost = protocol.connectionLost | ||
587 | 149 | |||
588 | 150 | |||
589 | 151 | def makeConnection(self, transport): | ||
590 | 152 | self.transport = transport | ||
591 | 153 | self.protocol.makeConnection(transport) | ||
592 | 154 | self.factory.clientConnectionMade(self.protocol) | ||
593 | 155 | |||
594 | 156 | |||
595 | 157 | def __repr__(self): | ||
596 | 158 | return 'Q2Q-Cached<%r, %r>' % (self.transport, | ||
597 | 159 | self.protocol) | ||
598 | 160 | |||
599 | 161 | 0 | ||
600 | === removed file 'Vertex/vertex/depserv.py' | |||
601 | --- Vertex/vertex/depserv.py 2009-07-06 11:40:18 +0000 | |||
602 | +++ Vertex/vertex/depserv.py 1970-01-01 00:00:00 +0000 | |||
603 | @@ -1,197 +0,0 @@ | |||
604 | 1 | # Copyright 2005 Divmod, Inc. See LICENSE file for details | ||
605 | 2 | |||
606 | 3 | """ | ||
607 | 4 | This module is no longer supported for use outside Vertex. | ||
608 | 5 | """ | ||
609 | 6 | |||
610 | 7 | from twisted.python import log | ||
611 | 8 | from sets import Set | ||
612 | 9 | from twisted.persisted import sob | ||
613 | 10 | from twisted.application import service, internet | ||
614 | 11 | |||
615 | 12 | from zope.interface import implements | ||
616 | 13 | |||
617 | 14 | class Conf(dict): | ||
618 | 15 | """A class to help in construction the configuration for delpoy(). | ||
619 | 16 | |||
620 | 17 | Typical usage:: | ||
621 | 18 | |||
622 | 19 | from vertex.depserv import Conf | ||
623 | 20 | conf = Conf() | ||
624 | 21 | s = conf.section | ||
625 | 22 | s('pop', | ||
626 | 23 | port = 110, | ||
627 | 24 | sslPort = 995) | ||
628 | 25 | ... | ||
629 | 26 | """ | ||
630 | 27 | def section(self, name, **kw): | ||
631 | 28 | self.setdefault(name, {}).update(kw) | ||
632 | 29 | |||
633 | 30 | |||
634 | 31 | class NotPersistable: | ||
635 | 32 | implements(sob.IPersistable) | ||
636 | 33 | def __init__(self, original): | ||
637 | 34 | self.original = original | ||
638 | 35 | |||
639 | 36 | def setStyle(self, style): | ||
640 | 37 | self.style = style | ||
641 | 38 | |||
642 | 39 | def save(self, tag=None, filename=None, passphrase=None): | ||
643 | 40 | pass | ||
644 | 41 | |||
645 | 42 | |||
646 | 43 | class StartupError(Exception): | ||
647 | 44 | pass | ||
648 | 45 | |||
649 | 46 | |||
650 | 47 | class DependencyService(service.MultiService): | ||
651 | 48 | """A MultiService that can start multiple services with interdependencies. | ||
652 | 49 | |||
653 | 50 | Each keyword parameter is a dict which serves as the options for that | ||
654 | 51 | service. | ||
655 | 52 | |||
656 | 53 | Each service defines a method setup_SERVICE, which is called with the | ||
657 | 54 | matching parameters (the service name must be all caps). If there is no key | ||
658 | 55 | for SERVICE in the class parameters, the setup method is not called. The | ||
659 | 56 | return value is ignored, and DependencyService makes no assumptions about | ||
660 | 57 | any side effects. | ||
661 | 58 | |||
662 | 59 | Each service may also optionally define depends_SERVICE which is called | ||
663 | 60 | before the setup method with the same parameters as the setup method. This | ||
664 | 61 | method returns a list of names of services on which SERVICE depends. | ||
665 | 62 | DependencyService will then initialize the service is the correct order. If | ||
666 | 63 | circular dependencies result, or a service depends on another service which | ||
667 | 64 | does not exist or is not configured to run, StartupError is raised. | ||
668 | 65 | |||
669 | 66 | The class can define required services by setting 'requiredServices' to a | ||
670 | 67 | list of service names. These services will be initialized first in the | ||
671 | 68 | order they appear in the list, ignoring all dependency information. If | ||
672 | 69 | there are no parameters for a required service (consequently, the setup | ||
673 | 70 | method would not normally be called), StartupError is raised. | ||
674 | 71 | """ | ||
675 | 72 | |||
676 | 73 | |||
677 | 74 | requiredServices = [] | ||
678 | 75 | |||
679 | 76 | |||
680 | 77 | def __init__(self, **kw): | ||
681 | 78 | service.MultiService.__init__(self) | ||
682 | 79 | |||
683 | 80 | # this makes it possible for one service to change the configuration of | ||
684 | 81 | # another. Avoid if possible, there if you need it. Be sure to properly | ||
685 | 82 | # set the dependencies. | ||
686 | 83 | self.config = kw | ||
687 | 84 | self.servers = [] | ||
688 | 85 | |||
689 | 86 | services = kw.keys() | ||
690 | 87 | initedServices = Set() | ||
691 | 88 | uninitedServices = Set(services) | ||
692 | 89 | |||
693 | 90 | # build dependencies | ||
694 | 91 | dependencies = {} | ||
695 | 92 | for serv in services: | ||
696 | 93 | try: | ||
697 | 94 | dependMethod = self._getDependsMethod(serv) | ||
698 | 95 | except AttributeError: | ||
699 | 96 | continue | ||
700 | 97 | dependencies[serv] = dependMethod(**kw[serv]) | ||
701 | 98 | |||
702 | 99 | def initializeService(svc): | ||
703 | 100 | self._getServiceMethod(svc)(**kw[svc]) | ||
704 | 101 | initedServices.add(svc) | ||
705 | 102 | uninitedServices.remove(svc) | ||
706 | 103 | |||
707 | 104 | for svc in self.requiredServices: | ||
708 | 105 | if dependencies.get(svc): | ||
709 | 106 | raise StartupError( | ||
710 | 107 | '%r is a required service but has unsatisfied ' | ||
711 | 108 | 'dependency on %r' % (svc, dependencies[svc])) | ||
712 | 109 | initializeService(svc) | ||
713 | 110 | |||
714 | 111 | while uninitedServices: | ||
715 | 112 | # iterate over the uninitialized services, adding those with no | ||
716 | 113 | # outstanding dependencies to initThisRound. | ||
717 | 114 | initThisRound = [] | ||
718 | 115 | for serv in uninitedServices: | ||
719 | 116 | for dep in dependencies.get(serv, []): | ||
720 | 117 | if dep not in initedServices: | ||
721 | 118 | if dep not in uninitedServices: | ||
722 | 119 | raise StartupError( | ||
723 | 120 | 'service %r depends on service %r, which is not ' | ||
724 | 121 | 'configured or does not exist.' % (serv, dep)) | ||
725 | 122 | break | ||
726 | 123 | else: | ||
727 | 124 | initThisRound.append(serv) | ||
728 | 125 | if not initThisRound: | ||
729 | 126 | raise StartupError( | ||
730 | 127 | 'Can not initialize all services. Circular dependencies ' | ||
731 | 128 | 'between setup methods?') | ||
732 | 129 | for svc in initThisRound: | ||
733 | 130 | initializeService(svc) | ||
734 | 131 | |||
735 | 132 | |||
736 | 133 | def _getServiceMethod(self, service): | ||
737 | 134 | return getattr(self, 'setup_%s' % (service.upper(),)) | ||
738 | 135 | |||
739 | 136 | |||
740 | 137 | def _getDependsMethod(self, service): | ||
741 | 138 | return getattr(self, 'depends_%s' % (service.upper(),)) | ||
742 | 139 | |||
743 | 140 | |||
744 | 141 | def deploy(Class, name=None, uid=None, gid=None, **kw): | ||
745 | 142 | """Create an application with the give name, uid, and gid. | ||
746 | 143 | |||
747 | 144 | The application has one child service, an instance of Class | ||
748 | 145 | configured based on the additional keyword arguments passed. | ||
749 | 146 | |||
750 | 147 | The application is not persistable. | ||
751 | 148 | """ | ||
752 | 149 | svc = Class(**kw) | ||
753 | 150 | |||
754 | 151 | if name is None: | ||
755 | 152 | name = Class.__name__ | ||
756 | 153 | # Make it easier (possible) to find this service by name later on | ||
757 | 154 | svc.setName(name) | ||
758 | 155 | |||
759 | 156 | app = service.Application(name, uid=uid, gid=gid) | ||
760 | 157 | app.addComponent(NotPersistable(app), ignoreClass=True) | ||
761 | 158 | svc.setServiceParent(app) | ||
762 | 159 | |||
763 | 160 | return app | ||
764 | 161 | deploy = classmethod(deploy) | ||
765 | 162 | |||
766 | 163 | def attach(self, subservice): | ||
767 | 164 | subservice.setServiceParent(self) | ||
768 | 165 | return subservice | ||
769 | 166 | |||
770 | 167 | def detach(self, subservice): | ||
771 | 168 | subservice.disownServiceParent() | ||
772 | 169 | |||
773 | 170 | def addServer(self, normalPort, sslPort, f, name): | ||
774 | 171 | """Add a TCP and an SSL server. Name them `name` and `name`+'s'.""" | ||
775 | 172 | tcp = internet.TCPServer(normalPort,f) | ||
776 | 173 | tcp.setName(name) | ||
777 | 174 | self.servers.append(tcp) | ||
778 | 175 | if sslPort is not None: | ||
779 | 176 | ssl = internet.SSLServer(sslPort, f, contextFactory=self.sslfac) | ||
780 | 177 | ssl.setName(name+'s') | ||
781 | 178 | self.servers.append(ssl) | ||
782 | 179 | |||
783 | 180 | def discernPrivilegedServers(self): | ||
784 | 181 | return [srv for srv in self.servers if srv.args[0] <= 1024] | ||
785 | 182 | |||
786 | 183 | def discernUnprivilegedServers(self): | ||
787 | 184 | return [srv for srv in self.servers if srv.args[0] > 1024] | ||
788 | 185 | |||
789 | 186 | def privilegedStartService(self): | ||
790 | 187 | for server in self.discernPrivilegedServers(): | ||
791 | 188 | log.msg("privileged attach %r" % server) | ||
792 | 189 | self.attach(server) | ||
793 | 190 | return service.MultiService.privilegedStartService(self) | ||
794 | 191 | |||
795 | 192 | def startService(self): | ||
796 | 193 | for server in self.discernUnprivilegedServers(): | ||
797 | 194 | log.msg("attaching %r" % server) | ||
798 | 195 | self.attach(server) | ||
799 | 196 | |||
800 | 197 | return service.MultiService.startService(self) | ||
801 | 198 | 0 | ||
802 | === removed file 'Vertex/vertex/endpoint.py' | |||
803 | --- Vertex/vertex/endpoint.py 2005-08-05 06:02:56 +0000 | |||
804 | +++ Vertex/vertex/endpoint.py 1970-01-01 00:00:00 +0000 | |||
805 | @@ -1,56 +0,0 @@ | |||
806 | 1 | # Copyright 2005 Divmod, Inc. See LICENSE file for details | ||
807 | 2 | |||
808 | 3 | def stablesort(self, other): | ||
809 | 4 | return cmp(self.__class__, getattr(other, '__class__', type(other))) | ||
810 | 5 | |||
811 | 6 | class TCPEndpoint: | ||
812 | 7 | def __init__(self, host, port): | ||
813 | 8 | self.host = host | ||
814 | 9 | self.port = port | ||
815 | 10 | |||
816 | 11 | def __hash__(self): | ||
817 | 12 | return hash((self.host, self.port)) + 5 | ||
818 | 13 | |||
819 | 14 | def connect(self, protocolFactory): | ||
820 | 15 | from twisted.internet import reactor | ||
821 | 16 | return reactor.connectTCP(self.host, self.port, protocolFactory) | ||
822 | 17 | |||
823 | 18 | def __repr__(self): | ||
824 | 19 | return '<TCP@%s,%d>' % (self.host, self.port) | ||
825 | 20 | |||
826 | 21 | def __cmp__(self, other): | ||
827 | 22 | if isinstance(other, TCPEndpoint): | ||
828 | 23 | return cmp((self.host, self.port), | ||
829 | 24 | (other.host, other.port)) | ||
830 | 25 | return stablesort(self, other) | ||
831 | 26 | |||
832 | 27 | |||
833 | 28 | class Q2QEndpoint: | ||
834 | 29 | def __init__(self, service, fromAddress, toAddress, protocolName): | ||
835 | 30 | self.service = service | ||
836 | 31 | self.fromAddress = fromAddress | ||
837 | 32 | self.toAddress = toAddress | ||
838 | 33 | self.protocolName = protocolName | ||
839 | 34 | |||
840 | 35 | def __repr__(self): | ||
841 | 36 | return '<Q2Q from <%s> to <%s> on %r>' % ( | ||
842 | 37 | self.fromAddress, self.toAddress, self.protocolName) | ||
843 | 38 | |||
844 | 39 | def __cmp__(self, other): | ||
845 | 40 | if isinstance(other, Q2QEndpoint): | ||
846 | 41 | return cmp((self.fromAddress, self.toAddress, self.protocolName), | ||
847 | 42 | (other.fromAddress, other.toAddress, other.protocolName)) | ||
848 | 43 | return stablesort(self, other) | ||
849 | 44 | |||
850 | 45 | def __hash__(self): | ||
851 | 46 | return hash((self.fromAddress, | ||
852 | 47 | self.toAddress, | ||
853 | 48 | self.protocolName)) + 7 | ||
854 | 49 | |||
855 | 50 | def connect(self, protocolFactory): | ||
856 | 51 | # from twisted.python.context import get | ||
857 | 52 | # get("q2q-service") | ||
858 | 53 | return self.service.connectQ2Q( | ||
859 | 54 | self.fromAddress, self.toAddress, self.protocolName, | ||
860 | 55 | protocolFactory) | ||
861 | 56 | |||
862 | 57 | 0 | ||
863 | === removed file 'Vertex/vertex/gtk2hack.glade' | |||
864 | --- Vertex/vertex/gtk2hack.glade 2006-06-01 04:57:02 +0000 | |||
865 | +++ Vertex/vertex/gtk2hack.glade 1970-01-01 00:00:00 +0000 | |||
866 | @@ -1,635 +0,0 @@ | |||
867 | 1 | <?xml version="1.0" standalone="no"?> <!--*- mode: xml -*--> | ||
868 | 2 | <!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd"> | ||
869 | 3 | |||
870 | 4 | <glade-interface> | ||
871 | 5 | <requires lib="gnome"/> | ||
872 | 6 | |||
873 | 7 | <widget class="GtkMenu" id="notification_popup"> | ||
874 | 8 | |||
875 | 9 | <child> | ||
876 | 10 | <widget class="GtkImageMenuItem" id="add_contact1"> | ||
877 | 11 | <property name="visible">True</property> | ||
878 | 12 | <property name="label" translatable="yes">Add Contact</property> | ||
879 | 13 | <property name="use_underline">True</property> | ||
880 | 14 | <signal name="activate" handler="addContact" last_modification_time="Sun, 22 May 2005 01:24:07 GMT"/> | ||
881 | 15 | |||
882 | 16 | <child internal-child="image"> | ||
883 | 17 | <widget class="GtkImage" id="image9"> | ||
884 | 18 | <property name="visible">True</property> | ||
885 | 19 | <property name="stock">gtk-add</property> | ||
886 | 20 | <property name="icon_size">1</property> | ||
887 | 21 | <property name="xalign">0.5</property> | ||
888 | 22 | <property name="yalign">0.5</property> | ||
889 | 23 | <property name="xpad">0</property> | ||
890 | 24 | <property name="ypad">0</property> | ||
891 | 25 | </widget> | ||
892 | 26 | </child> | ||
893 | 27 | </widget> | ||
894 | 28 | </child> | ||
895 | 29 | |||
896 | 30 | <child> | ||
897 | 31 | <widget class="GtkMenuItem" id="identifymenuitem"> | ||
898 | 32 | <property name="visible">True</property> | ||
899 | 33 | <property name="label" translatable="yes">Identify</property> | ||
900 | 34 | <property name="use_underline">True</property> | ||
901 | 35 | <signal name="activate" handler="identifyDialog" last_modification_time="Sun, 22 May 2005 03:20:18 GMT"/> | ||
902 | 36 | </widget> | ||
903 | 37 | </child> | ||
904 | 38 | |||
905 | 39 | <child> | ||
906 | 40 | <widget class="GtkSeparatorMenuItem" id="contacts_begin"> | ||
907 | 41 | <property name="visible">True</property> | ||
908 | 42 | </widget> | ||
909 | 43 | </child> | ||
910 | 44 | |||
911 | 45 | <child> | ||
912 | 46 | <widget class="GtkSeparatorMenuItem" id="separator3"> | ||
913 | 47 | <property name="visible">True</property> | ||
914 | 48 | </widget> | ||
915 | 49 | </child> | ||
916 | 50 | |||
917 | 51 | <child> | ||
918 | 52 | <widget class="GtkMenuItem" id="animate"> | ||
919 | 53 | <property name="visible">True</property> | ||
920 | 54 | <property name="label" translatable="yes">Animate</property> | ||
921 | 55 | <property name="use_underline">True</property> | ||
922 | 56 | <signal name="activate" handler="toggleAnimate" last_modification_time="Sun, 22 May 2005 01:44:10 GMT"/> | ||
923 | 57 | </widget> | ||
924 | 58 | </child> | ||
925 | 59 | |||
926 | 60 | <child> | ||
927 | 61 | <widget class="GtkSeparatorMenuItem" id="separator1"> | ||
928 | 62 | <property name="visible">True</property> | ||
929 | 63 | </widget> | ||
930 | 64 | </child> | ||
931 | 65 | |||
932 | 66 | <child> | ||
933 | 67 | <widget class="GtkImageMenuItem" id="quit1"> | ||
934 | 68 | <property name="visible">True</property> | ||
935 | 69 | <property name="stock_item">GNOMEUIINFO_MENU_EXIT_ITEM</property> | ||
936 | 70 | <signal name="activate" handler="quit" last_modification_time="Sun, 22 May 2005 01:24:07 GMT"/> | ||
937 | 71 | </widget> | ||
938 | 72 | </child> | ||
939 | 73 | </widget> | ||
940 | 74 | |||
941 | 75 | <widget class="GtkDialog" id="ident_dialog"> | ||
942 | 76 | <property name="visible">True</property> | ||
943 | 77 | <property name="title" translatable="yes">Identify Yourself</property> | ||
944 | 78 | <property name="type">GTK_WINDOW_TOPLEVEL</property> | ||
945 | 79 | <property name="window_position">GTK_WIN_POS_CENTER</property> | ||
946 | 80 | <property name="modal">False</property> | ||
947 | 81 | <property name="resizable">False</property> | ||
948 | 82 | <property name="destroy_with_parent">False</property> | ||
949 | 83 | <property name="decorated">True</property> | ||
950 | 84 | <property name="skip_taskbar_hint">False</property> | ||
951 | 85 | <property name="skip_pager_hint">False</property> | ||
952 | 86 | <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property> | ||
953 | 87 | <property name="gravity">GDK_GRAVITY_NORTH_WEST</property> | ||
954 | 88 | <property name="focus_on_map">True</property> | ||
955 | 89 | <property name="urgency_hint">False</property> | ||
956 | 90 | <property name="has_separator">True</property> | ||
957 | 91 | |||
958 | 92 | <child internal-child="vbox"> | ||
959 | 93 | <widget class="GtkVBox" id="dialog-vbox1"> | ||
960 | 94 | <property name="visible">True</property> | ||
961 | 95 | <property name="homogeneous">False</property> | ||
962 | 96 | <property name="spacing">0</property> | ||
963 | 97 | |||
964 | 98 | <child internal-child="action_area"> | ||
965 | 99 | <widget class="GtkHButtonBox" id="dialog-action_area1"> | ||
966 | 100 | <property name="visible">True</property> | ||
967 | 101 | <property name="layout_style">GTK_BUTTONBOX_END</property> | ||
968 | 102 | |||
969 | 103 | <child> | ||
970 | 104 | <widget class="GtkButton" id="cancelbutton1"> | ||
971 | 105 | <property name="visible">True</property> | ||
972 | 106 | <property name="can_default">True</property> | ||
973 | 107 | <property name="can_focus">True</property> | ||
974 | 108 | <property name="label">gtk-cancel</property> | ||
975 | 109 | <property name="use_stock">True</property> | ||
976 | 110 | <property name="relief">GTK_RELIEF_NORMAL</property> | ||
977 | 111 | <property name="focus_on_click">True</property> | ||
978 | 112 | <property name="response_id">-6</property> | ||
979 | 113 | <signal name="clicked" handler="identifyCancel" last_modification_time="Sun, 22 May 2005 03:14:51 GMT"/> | ||
980 | 114 | </widget> | ||
981 | 115 | </child> | ||
982 | 116 | |||
983 | 117 | <child> | ||
984 | 118 | <widget class="GtkButton" id="okbutton1"> | ||
985 | 119 | <property name="visible">True</property> | ||
986 | 120 | <property name="can_default">True</property> | ||
987 | 121 | <property name="can_focus">True</property> | ||
988 | 122 | <property name="label">gtk-ok</property> | ||
989 | 123 | <property name="use_stock">True</property> | ||
990 | 124 | <property name="relief">GTK_RELIEF_NORMAL</property> | ||
991 | 125 | <property name="focus_on_click">True</property> | ||
992 | 126 | <property name="response_id">-5</property> | ||
993 | 127 | <signal name="clicked" handler="identifyOK" last_modification_time="Sun, 22 May 2005 03:15:00 GMT"/> | ||
994 | 128 | </widget> | ||
995 | 129 | </child> | ||
996 | 130 | </widget> | ||
997 | 131 | <packing> | ||
998 | 132 | <property name="padding">0</property> | ||
999 | 133 | <property name="expand">False</property> | ||
1000 | 134 | <property name="fill">True</property> | ||
1001 | 135 | <property name="pack_type">GTK_PACK_END</property> | ||
1002 | 136 | </packing> | ||
1003 | 137 | </child> | ||
1004 | 138 | |||
1005 | 139 | <child> | ||
1006 | 140 | <widget class="GtkVBox" id="vbox1"> | ||
1007 | 141 | <property name="visible">True</property> | ||
1008 | 142 | <property name="homogeneous">False</property> | ||
1009 | 143 | <property name="spacing">0</property> | ||
1010 | 144 | |||
1011 | 145 | <child> | ||
1012 | 146 | <widget class="GtkTable" id="table1"> | ||
1013 | 147 | <property name="border_width">5</property> | ||
1014 | 148 | <property name="visible">True</property> | ||
1015 | 149 | <property name="n_rows">2</property> | ||
1016 | 150 | <property name="n_columns">2</property> | ||
1017 | 151 | <property name="homogeneous">False</property> | ||
1018 | 152 | <property name="row_spacing">2</property> | ||
1019 | 153 | <property name="column_spacing">5</property> | ||
1020 | 154 | |||
1021 | 155 | <child> | ||
1022 | 156 | <widget class="GtkLabel" id="label4"> | ||
1023 | 157 | <property name="visible">True</property> | ||
1024 | 158 | <property name="label" translatable="yes">Address</property> | ||
1025 | 159 | <property name="use_underline">False</property> | ||
1026 | 160 | <property name="use_markup">False</property> | ||
1027 | 161 | <property name="justify">GTK_JUSTIFY_LEFT</property> | ||
1028 | 162 | <property name="wrap">False</property> | ||
1029 | 163 | <property name="selectable">False</property> | ||
1030 | 164 | <property name="xalign">0</property> | ||
1031 | 165 | <property name="yalign">0.5</property> | ||
1032 | 166 | <property name="xpad">0</property> | ||
1033 | 167 | <property name="ypad">0</property> | ||
1034 | 168 | <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property> | ||
1035 | 169 | <property name="width_chars">-1</property> | ||
1036 | 170 | <property name="single_line_mode">False</property> | ||
1037 | 171 | <property name="angle">0</property> | ||
1038 | 172 | </widget> | ||
1039 | 173 | <packing> | ||
1040 | 174 | <property name="left_attach">0</property> | ||
1041 | 175 | <property name="right_attach">1</property> | ||
1042 | 176 | <property name="top_attach">0</property> | ||
1043 | 177 | <property name="bottom_attach">1</property> | ||
1044 | 178 | <property name="x_options">fill</property> | ||
1045 | 179 | <property name="y_options"></property> | ||
1046 | 180 | </packing> | ||
1047 | 181 | </child> | ||
1048 | 182 | |||
1049 | 183 | <child> | ||
1050 | 184 | <widget class="GtkLabel" id="label5"> | ||
1051 | 185 | <property name="visible">True</property> | ||
1052 | 186 | <property name="label" translatable="yes">Password</property> | ||
1053 | 187 | <property name="use_underline">False</property> | ||
1054 | 188 | <property name="use_markup">False</property> | ||
1055 | 189 | <property name="justify">GTK_JUSTIFY_LEFT</property> | ||
1056 | 190 | <property name="wrap">False</property> | ||
1057 | 191 | <property name="selectable">False</property> | ||
1058 | 192 | <property name="xalign">0</property> | ||
1059 | 193 | <property name="yalign">0.5</property> | ||
1060 | 194 | <property name="xpad">0</property> | ||
1061 | 195 | <property name="ypad">0</property> | ||
1062 | 196 | <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property> | ||
1063 | 197 | <property name="width_chars">-1</property> | ||
1064 | 198 | <property name="single_line_mode">False</property> | ||
1065 | 199 | <property name="angle">0</property> | ||
1066 | 200 | </widget> | ||
1067 | 201 | <packing> | ||
1068 | 202 | <property name="left_attach">0</property> | ||
1069 | 203 | <property name="right_attach">1</property> | ||
1070 | 204 | <property name="top_attach">1</property> | ||
1071 | 205 | <property name="bottom_attach">2</property> | ||
1072 | 206 | <property name="x_options">fill</property> | ||
1073 | 207 | <property name="y_options"></property> | ||
1074 | 208 | </packing> | ||
1075 | 209 | </child> | ||
1076 | 210 | |||
1077 | 211 | <child> | ||
1078 | 212 | <widget class="GtkEntry" id="addressEntry"> | ||
1079 | 213 | <property name="visible">True</property> | ||
1080 | 214 | <property name="can_focus">True</property> | ||
1081 | 215 | <property name="has_focus">True</property> | ||
1082 | 216 | <property name="editable">True</property> | ||
1083 | 217 | <property name="visibility">True</property> | ||
1084 | 218 | <property name="max_length">0</property> | ||
1085 | 219 | <property name="text" translatable="yes"></property> | ||
1086 | 220 | <property name="has_frame">True</property> | ||
1087 | 221 | <property name="invisible_char">*</property> | ||
1088 | 222 | <property name="activates_default">False</property> | ||
1089 | 223 | </widget> | ||
1090 | 224 | <packing> | ||
1091 | 225 | <property name="left_attach">1</property> | ||
1092 | 226 | <property name="right_attach">2</property> | ||
1093 | 227 | <property name="top_attach">0</property> | ||
1094 | 228 | <property name="bottom_attach">1</property> | ||
1095 | 229 | <property name="y_options"></property> | ||
1096 | 230 | </packing> | ||
1097 | 231 | </child> | ||
1098 | 232 | |||
1099 | 233 | <child> | ||
1100 | 234 | <widget class="GtkEntry" id="passwordEntry"> | ||
1101 | 235 | <property name="visible">True</property> | ||
1102 | 236 | <property name="can_focus">True</property> | ||
1103 | 237 | <property name="editable">True</property> | ||
1104 | 238 | <property name="visibility">False</property> | ||
1105 | 239 | <property name="max_length">0</property> | ||
1106 | 240 | <property name="text" translatable="yes"></property> | ||
1107 | 241 | <property name="has_frame">True</property> | ||
1108 | 242 | <property name="invisible_char">*</property> | ||
1109 | 243 | <property name="activates_default">True</property> | ||
1110 | 244 | </widget> | ||
1111 | 245 | <packing> | ||
1112 | 246 | <property name="left_attach">1</property> | ||
1113 | 247 | <property name="right_attach">2</property> | ||
1114 | 248 | <property name="top_attach">1</property> | ||
1115 | 249 | <property name="bottom_attach">2</property> | ||
1116 | 250 | <property name="y_options"></property> | ||
1117 | 251 | </packing> | ||
1118 | 252 | </child> | ||
1119 | 253 | </widget> | ||
1120 | 254 | <packing> | ||
1121 | 255 | <property name="padding">0</property> | ||
1122 | 256 | <property name="expand">True</property> | ||
1123 | 257 | <property name="fill">True</property> | ||
1124 | 258 | </packing> | ||
1125 | 259 | </child> | ||
1126 | 260 | |||
1127 | 261 | <child> | ||
1128 | 262 | <widget class="GtkProgressBar" id="identifyProgressBar"> | ||
1129 | 263 | <property name="visible">True</property> | ||
1130 | 264 | <property name="orientation">GTK_PROGRESS_LEFT_TO_RIGHT</property> | ||
1131 | 265 | <property name="fraction">0</property> | ||
1132 | 266 | <property name="pulse_step">0.10000000149</property> | ||
1133 | 267 | <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property> | ||
1134 | 268 | </widget> | ||
1135 | 269 | <packing> | ||
1136 | 270 | <property name="padding">0</property> | ||
1137 | 271 | <property name="expand">False</property> | ||
1138 | 272 | <property name="fill">False</property> | ||
1139 | 273 | </packing> | ||
1140 | 274 | </child> | ||
1141 | 275 | |||
1142 | 276 | <child> | ||
1143 | 277 | <widget class="GtkLabel" id="identifyProgressLabel"> | ||
1144 | 278 | <property name="visible">True</property> | ||
1145 | 279 | <property name="label" translatable="yes"></property> | ||
1146 | 280 | <property name="use_underline">False</property> | ||
1147 | 281 | <property name="use_markup">False</property> | ||
1148 | 282 | <property name="justify">GTK_JUSTIFY_LEFT</property> | ||
1149 | 283 | <property name="wrap">False</property> | ||
1150 | 284 | <property name="selectable">False</property> | ||
1151 | 285 | <property name="xalign">0.5</property> | ||
1152 | 286 | <property name="yalign">0.5</property> | ||
1153 | 287 | <property name="xpad">0</property> | ||
1154 | 288 | <property name="ypad">0</property> | ||
1155 | 289 | <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property> | ||
1156 | 290 | <property name="width_chars">-1</property> | ||
1157 | 291 | <property name="single_line_mode">False</property> | ||
1158 | 292 | <property name="angle">0</property> | ||
1159 | 293 | </widget> | ||
1160 | 294 | <packing> | ||
1161 | 295 | <property name="padding">3</property> | ||
1162 | 296 | <property name="expand">False</property> | ||
1163 | 297 | <property name="fill">False</property> | ||
1164 | 298 | </packing> | ||
1165 | 299 | </child> | ||
1166 | 300 | </widget> | ||
1167 | 301 | <packing> | ||
1168 | 302 | <property name="padding">0</property> | ||
1169 | 303 | <property name="expand">False</property> | ||
1170 | 304 | <property name="fill">True</property> | ||
1171 | 305 | </packing> | ||
1172 | 306 | </child> | ||
1173 | 307 | </widget> | ||
1174 | 308 | </child> | ||
1175 | 309 | </widget> | ||
1176 | 310 | |||
1177 | 311 | <widget class="GtkDialog" id="add_contact_dialog"> | ||
1178 | 312 | <property name="visible">True</property> | ||
1179 | 313 | <property name="title" translatable="yes">Add Contact</property> | ||
1180 | 314 | <property name="type">GTK_WINDOW_TOPLEVEL</property> | ||
1181 | 315 | <property name="window_position">GTK_WIN_POS_NONE</property> | ||
1182 | 316 | <property name="modal">False</property> | ||
1183 | 317 | <property name="resizable">True</property> | ||
1184 | 318 | <property name="destroy_with_parent">False</property> | ||
1185 | 319 | <property name="decorated">True</property> | ||
1186 | 320 | <property name="skip_taskbar_hint">False</property> | ||
1187 | 321 | <property name="skip_pager_hint">False</property> | ||
1188 | 322 | <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property> | ||
1189 | 323 | <property name="gravity">GDK_GRAVITY_NORTH_WEST</property> | ||
1190 | 324 | <property name="focus_on_map">True</property> | ||
1191 | 325 | <property name="urgency_hint">False</property> | ||
1192 | 326 | <property name="has_separator">True</property> | ||
1193 | 327 | |||
1194 | 328 | <child internal-child="vbox"> | ||
1195 | 329 | <widget class="GtkVBox" id="dialog-vbox2"> | ||
1196 | 330 | <property name="visible">True</property> | ||
1197 | 331 | <property name="homogeneous">False</property> | ||
1198 | 332 | <property name="spacing">0</property> | ||
1199 | 333 | |||
1200 | 334 | <child internal-child="action_area"> | ||
1201 | 335 | <widget class="GtkHButtonBox" id="dialog-action_area2"> | ||
1202 | 336 | <property name="visible">True</property> | ||
1203 | 337 | <property name="layout_style">GTK_BUTTONBOX_END</property> | ||
1204 | 338 | |||
1205 | 339 | <child> | ||
1206 | 340 | <widget class="GtkButton" id="cancelbutton2"> | ||
1207 | 341 | <property name="visible">True</property> | ||
1208 | 342 | <property name="can_default">True</property> | ||
1209 | 343 | <property name="can_focus">True</property> | ||
1210 | 344 | <property name="label">gtk-cancel</property> | ||
1211 | 345 | <property name="use_stock">True</property> | ||
1212 | 346 | <property name="relief">GTK_RELIEF_NORMAL</property> | ||
1213 | 347 | <property name="focus_on_click">True</property> | ||
1214 | 348 | <property name="response_id">-6</property> | ||
1215 | 349 | <signal name="activate" handler="popdownDialog" last_modification_time="Tue, 28 Feb 2006 09:34:42 GMT"/> | ||
1216 | 350 | </widget> | ||
1217 | 351 | </child> | ||
1218 | 352 | |||
1219 | 353 | <child> | ||
1220 | 354 | <widget class="GtkButton" id="okbutton2"> | ||
1221 | 355 | <property name="visible">True</property> | ||
1222 | 356 | <property name="can_default">True</property> | ||
1223 | 357 | <property name="can_focus">True</property> | ||
1224 | 358 | <property name="label">gtk-ok</property> | ||
1225 | 359 | <property name="use_stock">True</property> | ||
1226 | 360 | <property name="relief">GTK_RELIEF_NORMAL</property> | ||
1227 | 361 | <property name="focus_on_click">True</property> | ||
1228 | 362 | <property name="response_id">-5</property> | ||
1229 | 363 | <signal name="activate" handler="doAddContact" last_modification_time="Tue, 28 Feb 2006 09:34:23 GMT"/> | ||
1230 | 364 | <signal name="clicked" handler="doAddContact" last_modification_time="Tue, 28 Feb 2006 10:36:25 GMT"/> | ||
1231 | 365 | </widget> | ||
1232 | 366 | </child> | ||
1233 | 367 | </widget> | ||
1234 | 368 | <packing> | ||
1235 | 369 | <property name="padding">0</property> | ||
1236 | 370 | <property name="expand">False</property> | ||
1237 | 371 | <property name="fill">True</property> | ||
1238 | 372 | <property name="pack_type">GTK_PACK_END</property> | ||
1239 | 373 | </packing> | ||
1240 | 374 | </child> | ||
1241 | 375 | |||
1242 | 376 | <child> | ||
1243 | 377 | <widget class="GtkFrame" id="frame1"> | ||
1244 | 378 | <property name="visible">True</property> | ||
1245 | 379 | <property name="label_xalign">0</property> | ||
1246 | 380 | <property name="label_yalign">0.5</property> | ||
1247 | 381 | <property name="shadow_type">GTK_SHADOW_ETCHED_IN</property> | ||
1248 | 382 | |||
1249 | 383 | <child> | ||
1250 | 384 | <widget class="GtkAlignment" id="alignment1"> | ||
1251 | 385 | <property name="visible">True</property> | ||
1252 | 386 | <property name="xalign">0.5</property> | ||
1253 | 387 | <property name="yalign">0.5</property> | ||
1254 | 388 | <property name="xscale">1</property> | ||
1255 | 389 | <property name="yscale">1</property> | ||
1256 | 390 | <property name="top_padding">0</property> | ||
1257 | 391 | <property name="bottom_padding">0</property> | ||
1258 | 392 | <property name="left_padding">12</property> | ||
1259 | 393 | <property name="right_padding">0</property> | ||
1260 | 394 | |||
1261 | 395 | <child> | ||
1262 | 396 | <widget class="GtkTable" id="table2"> | ||
1263 | 397 | <property name="border_width">16</property> | ||
1264 | 398 | <property name="visible">True</property> | ||
1265 | 399 | <property name="n_rows">2</property> | ||
1266 | 400 | <property name="n_columns">2</property> | ||
1267 | 401 | <property name="homogeneous">False</property> | ||
1268 | 402 | <property name="row_spacing">16</property> | ||
1269 | 403 | <property name="column_spacing">16</property> | ||
1270 | 404 | |||
1271 | 405 | <child> | ||
1272 | 406 | <widget class="GtkEntry" id="nameentry"> | ||
1273 | 407 | <property name="visible">True</property> | ||
1274 | 408 | <property name="can_focus">True</property> | ||
1275 | 409 | <property name="editable">True</property> | ||
1276 | 410 | <property name="visibility">True</property> | ||
1277 | 411 | <property name="max_length">0</property> | ||
1278 | 412 | <property name="text" translatable="yes"></property> | ||
1279 | 413 | <property name="has_frame">True</property> | ||
1280 | 414 | <property name="invisible_char">*</property> | ||
1281 | 415 | <property name="activates_default">False</property> | ||
1282 | 416 | </widget> | ||
1283 | 417 | <packing> | ||
1284 | 418 | <property name="left_attach">1</property> | ||
1285 | 419 | <property name="right_attach">2</property> | ||
1286 | 420 | <property name="top_attach">0</property> | ||
1287 | 421 | <property name="bottom_attach">1</property> | ||
1288 | 422 | <property name="y_options"></property> | ||
1289 | 423 | </packing> | ||
1290 | 424 | </child> | ||
1291 | 425 | |||
1292 | 426 | <child> | ||
1293 | 427 | <widget class="GtkEntry" id="q2qidentry"> | ||
1294 | 428 | <property name="visible">True</property> | ||
1295 | 429 | <property name="can_focus">True</property> | ||
1296 | 430 | <property name="editable">True</property> | ||
1297 | 431 | <property name="visibility">True</property> | ||
1298 | 432 | <property name="max_length">0</property> | ||
1299 | 433 | <property name="text" translatable="yes"></property> | ||
1300 | 434 | <property name="has_frame">True</property> | ||
1301 | 435 | <property name="invisible_char">*</property> | ||
1302 | 436 | <property name="activates_default">False</property> | ||
1303 | 437 | </widget> | ||
1304 | 438 | <packing> | ||
1305 | 439 | <property name="left_attach">1</property> | ||
1306 | 440 | <property name="right_attach">2</property> | ||
1307 | 441 | <property name="top_attach">1</property> | ||
1308 | 442 | <property name="bottom_attach">2</property> | ||
1309 | 443 | <property name="y_options"></property> | ||
1310 | 444 | </packing> | ||
1311 | 445 | </child> | ||
1312 | 446 | |||
1313 | 447 | <child> | ||
1314 | 448 | <widget class="GtkLabel" id="label7"> | ||
1315 | 449 | <property name="visible">True</property> | ||
1316 | 450 | <property name="label" translatable="yes">Name</property> | ||
1317 | 451 | <property name="use_underline">False</property> | ||
1318 | 452 | <property name="use_markup">False</property> | ||
1319 | 453 | <property name="justify">GTK_JUSTIFY_LEFT</property> | ||
1320 | 454 | <property name="wrap">False</property> | ||
1321 | 455 | <property name="selectable">False</property> | ||
1322 | 456 | <property name="xalign">0</property> | ||
1323 | 457 | <property name="yalign">0.5</property> | ||
1324 | 458 | <property name="xpad">0</property> | ||
1325 | 459 | <property name="ypad">0</property> | ||
1326 | 460 | <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property> | ||
1327 | 461 | <property name="width_chars">-1</property> | ||
1328 | 462 | <property name="single_line_mode">False</property> | ||
1329 | 463 | <property name="angle">0</property> | ||
1330 | 464 | </widget> | ||
1331 | 465 | <packing> | ||
1332 | 466 | <property name="left_attach">0</property> | ||
1333 | 467 | <property name="right_attach">1</property> | ||
1334 | 468 | <property name="top_attach">0</property> | ||
1335 | 469 | <property name="bottom_attach">1</property> | ||
1336 | 470 | <property name="x_options">fill</property> | ||
1337 | 471 | <property name="y_options"></property> | ||
1338 | 472 | </packing> | ||
1339 | 473 | </child> | ||
1340 | 474 | |||
1341 | 475 | <child> | ||
1342 | 476 | <widget class="GtkLabel" id="label8"> | ||
1343 | 477 | <property name="visible">True</property> | ||
1344 | 478 | <property name="label" translatable="yes">Q2QID</property> | ||
1345 | 479 | <property name="use_underline">False</property> | ||
1346 | 480 | <property name="use_markup">False</property> | ||
1347 | 481 | <property name="justify">GTK_JUSTIFY_LEFT</property> | ||
1348 | 482 | <property name="wrap">False</property> | ||
1349 | 483 | <property name="selectable">False</property> | ||
1350 | 484 | <property name="xalign">0</property> | ||
1351 | 485 | <property name="yalign">0.5</property> | ||
1352 | 486 | <property name="xpad">0</property> | ||
1353 | 487 | <property name="ypad">0</property> | ||
1354 | 488 | <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property> | ||
1355 | 489 | <property name="width_chars">-1</property> | ||
1356 | 490 | <property name="single_line_mode">False</property> | ||
1357 | 491 | <property name="angle">0</property> | ||
1358 | 492 | </widget> | ||
1359 | 493 | <packing> | ||
1360 | 494 | <property name="left_attach">0</property> | ||
1361 | 495 | <property name="right_attach">1</property> | ||
1362 | 496 | <property name="top_attach">1</property> | ||
1363 | 497 | <property name="bottom_attach">2</property> | ||
1364 | 498 | <property name="x_options">fill</property> | ||
1365 | 499 | <property name="y_options"></property> | ||
1366 | 500 | </packing> | ||
1367 | 501 | </child> | ||
1368 | 502 | </widget> | ||
1369 | 503 | </child> | ||
1370 | 504 | </widget> | ||
1371 | 505 | </child> | ||
1372 | 506 | |||
1373 | 507 | <child> | ||
1374 | 508 | <widget class="GtkLabel" id="label6"> | ||
1375 | 509 | <property name="visible">True</property> | ||
1376 | 510 | <property name="label" translatable="yes">Contact Information</property> | ||
1377 | 511 | <property name="use_underline">False</property> | ||
1378 | 512 | <property name="use_markup">True</property> | ||
1379 | 513 | <property name="justify">GTK_JUSTIFY_LEFT</property> | ||
1380 | 514 | <property name="wrap">False</property> | ||
1381 | 515 | <property name="selectable">False</property> | ||
1382 | 516 | <property name="xalign">0.5</property> | ||
1383 | 517 | <property name="yalign">0.5</property> | ||
1384 | 518 | <property name="xpad">0</property> | ||
1385 | 519 | <property name="ypad">0</property> | ||
1386 | 520 | <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property> | ||
1387 | 521 | <property name="width_chars">-1</property> | ||
1388 | 522 | <property name="single_line_mode">False</property> | ||
1389 | 523 | <property name="angle">0</property> | ||
1390 | 524 | </widget> | ||
1391 | 525 | <packing> | ||
1392 | 526 | <property name="type">label_item</property> | ||
1393 | 527 | </packing> | ||
1394 | 528 | </child> | ||
1395 | 529 | </widget> | ||
1396 | 530 | <packing> | ||
1397 | 531 | <property name="padding">0</property> | ||
1398 | 532 | <property name="expand">True</property> | ||
1399 | 533 | <property name="fill">True</property> | ||
1400 | 534 | </packing> | ||
1401 | 535 | </child> | ||
1402 | 536 | </widget> | ||
1403 | 537 | </child> | ||
1404 | 538 | </widget> | ||
1405 | 539 | |||
1406 | 540 | <widget class="GtkDialog" id="accept_connection_dialog"> | ||
1407 | 541 | <property name="visible">True</property> | ||
1408 | 542 | <property name="title" translatable="yes">Accept Connection?</property> | ||
1409 | 543 | <property name="type">GTK_WINDOW_TOPLEVEL</property> | ||
1410 | 544 | <property name="window_position">GTK_WIN_POS_NONE</property> | ||
1411 | 545 | <property name="modal">False</property> | ||
1412 | 546 | <property name="resizable">True</property> | ||
1413 | 547 | <property name="destroy_with_parent">False</property> | ||
1414 | 548 | <property name="decorated">True</property> | ||
1415 | 549 | <property name="skip_taskbar_hint">False</property> | ||
1416 | 550 | <property name="skip_pager_hint">False</property> | ||
1417 | 551 | <property name="type_hint">GDK_WINDOW_TYPE_HINT_DIALOG</property> | ||
1418 | 552 | <property name="gravity">GDK_GRAVITY_NORTH_WEST</property> | ||
1419 | 553 | <property name="focus_on_map">True</property> | ||
1420 | 554 | <property name="urgency_hint">False</property> | ||
1421 | 555 | <property name="has_separator">True</property> | ||
1422 | 556 | <signal name="destroy" handler="rejectConnectionEvt" last_modification_time="Tue, 28 Feb 2006 10:00:37 GMT"/> | ||
1423 | 557 | |||
1424 | 558 | <child internal-child="vbox"> | ||
1425 | 559 | <widget class="GtkVBox" id="dialog-vbox3"> | ||
1426 | 560 | <property name="visible">True</property> | ||
1427 | 561 | <property name="homogeneous">False</property> | ||
1428 | 562 | <property name="spacing">0</property> | ||
1429 | 563 | |||
1430 | 564 | <child internal-child="action_area"> | ||
1431 | 565 | <widget class="GtkHButtonBox" id="dialog-action_area3"> | ||
1432 | 566 | <property name="visible">True</property> | ||
1433 | 567 | <property name="layout_style">GTK_BUTTONBOX_END</property> | ||
1434 | 568 | |||
1435 | 569 | <child> | ||
1436 | 570 | <widget class="GtkButton" id="cancelbutton3"> | ||
1437 | 571 | <property name="visible">True</property> | ||
1438 | 572 | <property name="can_default">True</property> | ||
1439 | 573 | <property name="can_focus">True</property> | ||
1440 | 574 | <property name="label">gtk-cancel</property> | ||
1441 | 575 | <property name="use_stock">True</property> | ||
1442 | 576 | <property name="relief">GTK_RELIEF_NORMAL</property> | ||
1443 | 577 | <property name="focus_on_click">True</property> | ||
1444 | 578 | <property name="response_id">-6</property> | ||
1445 | 579 | <signal name="activate" handler="destroyit" last_modification_time="Tue, 28 Feb 2006 10:00:55 GMT"/> | ||
1446 | 580 | <signal name="clicked" handler="destroyit" last_modification_time="Tue, 28 Feb 2006 10:36:09 GMT"/> | ||
1447 | 581 | </widget> | ||
1448 | 582 | </child> | ||
1449 | 583 | |||
1450 | 584 | <child> | ||
1451 | 585 | <widget class="GtkButton" id="okbutton3"> | ||
1452 | 586 | <property name="visible">True</property> | ||
1453 | 587 | <property name="can_default">True</property> | ||
1454 | 588 | <property name="can_focus">True</property> | ||
1455 | 589 | <property name="label">gtk-ok</property> | ||
1456 | 590 | <property name="use_stock">True</property> | ||
1457 | 591 | <property name="relief">GTK_RELIEF_NORMAL</property> | ||
1458 | 592 | <property name="focus_on_click">True</property> | ||
1459 | 593 | <property name="response_id">-5</property> | ||
1460 | 594 | <signal name="activate" handler="acceptConnectionEvt" last_modification_time="Tue, 28 Feb 2006 09:59:48 GMT"/> | ||
1461 | 595 | <signal name="clicked" handler="acceptConnectionEvt" last_modification_time="Tue, 28 Feb 2006 10:36:51 GMT"/> | ||
1462 | 596 | </widget> | ||
1463 | 597 | </child> | ||
1464 | 598 | </widget> | ||
1465 | 599 | <packing> | ||
1466 | 600 | <property name="padding">0</property> | ||
1467 | 601 | <property name="expand">False</property> | ||
1468 | 602 | <property name="fill">True</property> | ||
1469 | 603 | <property name="pack_type">GTK_PACK_END</property> | ||
1470 | 604 | </packing> | ||
1471 | 605 | </child> | ||
1472 | 606 | |||
1473 | 607 | <child> | ||
1474 | 608 | <widget class="GtkLabel" id="accept_connection_label"> | ||
1475 | 609 | <property name="visible">True</property> | ||
1476 | 610 | <property name="label" translatable="yes">Accept connection?</property> | ||
1477 | 611 | <property name="use_underline">False</property> | ||
1478 | 612 | <property name="use_markup">False</property> | ||
1479 | 613 | <property name="justify">GTK_JUSTIFY_LEFT</property> | ||
1480 | 614 | <property name="wrap">False</property> | ||
1481 | 615 | <property name="selectable">False</property> | ||
1482 | 616 | <property name="xalign">0.5</property> | ||
1483 | 617 | <property name="yalign">0.5</property> | ||
1484 | 618 | <property name="xpad">0</property> | ||
1485 | 619 | <property name="ypad">0</property> | ||
1486 | 620 | <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property> | ||
1487 | 621 | <property name="width_chars">-1</property> | ||
1488 | 622 | <property name="single_line_mode">False</property> | ||
1489 | 623 | <property name="angle">0</property> | ||
1490 | 624 | </widget> | ||
1491 | 625 | <packing> | ||
1492 | 626 | <property name="padding">0</property> | ||
1493 | 627 | <property name="expand">False</property> | ||
1494 | 628 | <property name="fill">False</property> | ||
1495 | 629 | </packing> | ||
1496 | 630 | </child> | ||
1497 | 631 | </widget> | ||
1498 | 632 | </child> | ||
1499 | 633 | </widget> | ||
1500 | 634 | |||
1501 | 635 | </glade-interface> | ||
1502 | 636 | 0 | ||
1503 | === removed file 'Vertex/vertex/gtk2hack.py' | |||
1504 | --- Vertex/vertex/gtk2hack.py 2006-06-01 04:57:02 +0000 | |||
1505 | +++ Vertex/vertex/gtk2hack.py 1970-01-01 00:00:00 +0000 | |||
1506 | @@ -1,270 +0,0 @@ | |||
1507 | 1 | |||
1508 | 2 | import os | ||
1509 | 3 | import rfc822 | ||
1510 | 4 | |||
1511 | 5 | from twisted.python.filepath import FilePath | ||
1512 | 6 | |||
1513 | 7 | # import gtk ### pyflakes complains about this, due to the next line | ||
1514 | 8 | import gtk.glade | ||
1515 | 9 | |||
1516 | 10 | from vertex.q2qclient import ClientQ2QService | ||
1517 | 11 | from vertex.q2q import Q2QAddress | ||
1518 | 12 | |||
1519 | 13 | class _NullCb: | ||
1520 | 14 | def __init__(self, name): | ||
1521 | 15 | self.name = name | ||
1522 | 16 | |||
1523 | 17 | def __call__(self, *a, **kw): | ||
1524 | 18 | print 'No callback provided for', self.name, a, kw | ||
1525 | 19 | |||
1526 | 20 | class _SignalAttacher: | ||
1527 | 21 | def __init__(self, original): | ||
1528 | 22 | self.original = original | ||
1529 | 23 | |||
1530 | 24 | def __getitem__(self, callbackName): | ||
1531 | 25 | return getattr(self.original, callbackName, None) or _NullCb(callbackName) | ||
1532 | 26 | |||
1533 | 27 | GLADE_FILE = os.path.splitext(__file__)[0] + '.glade' | ||
1534 | 28 | |||
1535 | 29 | class IdentificationDialog: | ||
1536 | 30 | def __init__(self, clientService, plug): | ||
1537 | 31 | self.xml = gtk.glade.XML(GLADE_FILE, "ident_dialog") | ||
1538 | 32 | self.clientService = clientService | ||
1539 | 33 | self.xml.signal_autoconnect(_SignalAttacher(self)) | ||
1540 | 34 | self.addressEntry = self.xml.get_widget('addressEntry') | ||
1541 | 35 | self.passwordEntry = self.xml.get_widget('passwordEntry') | ||
1542 | 36 | self.progressBar = self.xml.get_widget('identifyProgressBar') | ||
1543 | 37 | self.progressLabel = self.xml.get_widget('identifyProgressLabel') | ||
1544 | 38 | self.identifyWindow = self.xml.get_widget("ident_dialog") | ||
1545 | 39 | self.cancelButton = self.xml.get_widget('cancelbutton1') | ||
1546 | 40 | self.okButton = self.xml.get_widget('okbutton1') | ||
1547 | 41 | self.plug = plug | ||
1548 | 42 | |||
1549 | 43 | def identifyCancel(self, event): | ||
1550 | 44 | self.identifyWindow.destroy() | ||
1551 | 45 | |||
1552 | 46 | def identifyOK(self, event): | ||
1553 | 47 | idstr = self.addressEntry.get_text() | ||
1554 | 48 | D = self.clientService.authorize( | ||
1555 | 49 | Q2QAddress.fromString(idstr), | ||
1556 | 50 | self.passwordEntry.get_text()) | ||
1557 | 51 | |||
1558 | 52 | sensitiveWidgets = [self.addressEntry, | ||
1559 | 53 | self.passwordEntry, | ||
1560 | 54 | self.okButton, | ||
1561 | 55 | self.cancelButton] | ||
1562 | 56 | for widget in sensitiveWidgets: | ||
1563 | 57 | widget.set_sensitive(False) | ||
1564 | 58 | self.progressLabel.set_text("Authenticating...") | ||
1565 | 59 | def itWorked(workedNone): | ||
1566 | 60 | self.identifyWindow.destroy() | ||
1567 | 61 | self.plug.setCurrentID(idstr) | ||
1568 | 62 | def itDidntWork(error): | ||
1569 | 63 | self.progressLabel.set_text(error.getErrorMessage()) | ||
1570 | 64 | for widget in sensitiveWidgets: | ||
1571 | 65 | widget.set_sensitive(True) | ||
1572 | 66 | D.addCallbacks(itWorked, itDidntWork) | ||
1573 | 67 | |||
1574 | 68 | class AddContactDialog: | ||
1575 | 69 | def __init__(self, plug): | ||
1576 | 70 | self.xml = gtk.glade.XML(GLADE_FILE, "add_contact_dialog") | ||
1577 | 71 | self.xml.signal_autoconnect(_SignalAttacher(self)) | ||
1578 | 72 | self.window = self.xml.get_widget("add_contact_dialog") | ||
1579 | 73 | self.window.show_all() | ||
1580 | 74 | self.plug = plug | ||
1581 | 75 | |||
1582 | 76 | def doAddContact(self, evt): | ||
1583 | 77 | name = self.xml.get_widget("nameentry").get_text() | ||
1584 | 78 | addr = self.xml.get_widget("q2qidentry").get_text() | ||
1585 | 79 | self.plug.addBuddy(name, addr) | ||
1586 | 80 | self.popdownDialog() | ||
1587 | 81 | |||
1588 | 82 | def popdownDialog(self, evt=None): | ||
1589 | 83 | self.window.destroy() | ||
1590 | 84 | |||
1591 | 85 | class AcceptConnectionDialog: | ||
1592 | 86 | def __init__(self, d, From, to, protocol): | ||
1593 | 87 | self.d = d | ||
1594 | 88 | self.xml = gtk.glade.XML(GLADE_FILE, "accept_connection_dialog") | ||
1595 | 89 | self.xml.signal_autoconnect(_SignalAttacher(self)) | ||
1596 | 90 | self.label = self.xml.get_widget("accept_connection_label") | ||
1597 | 91 | self.label.set_text( | ||
1598 | 92 | "Accept connection from %s for %s?" % (From, protocol)) | ||
1599 | 93 | self.window = self.xml.get_widget("accept_connection_dialog") | ||
1600 | 94 | self.window.show_all() | ||
1601 | 95 | |||
1602 | 96 | done = False | ||
1603 | 97 | |||
1604 | 98 | def destroyit(self, evt): | ||
1605 | 99 | self.window.destroy() | ||
1606 | 100 | |||
1607 | 101 | def acceptConnectionEvt(self, evt): | ||
1608 | 102 | self.done = True | ||
1609 | 103 | print "YES" | ||
1610 | 104 | self.d.callback(1) | ||
1611 | 105 | print "WHAT" | ||
1612 | 106 | self.window.destroy() | ||
1613 | 107 | |||
1614 | 108 | def rejectConnectionEvt(self, evt): | ||
1615 | 109 | print "DSTRY" | ||
1616 | 110 | if not self.done: | ||
1617 | 111 | print "DIE!" | ||
1618 | 112 | from twisted.python import failure | ||
1619 | 113 | self.d.errback(failure.Failure(KeyError("Connection rejected by user"))) | ||
1620 | 114 | else: | ||
1621 | 115 | print "OK" | ||
1622 | 116 | |||
1623 | 117 | from twisted.internet.protocol import ServerFactory | ||
1624 | 118 | from twisted.internet.protocol import Protocol | ||
1625 | 119 | |||
1626 | 120 | class VertexDemoProtocol(Protocol): | ||
1627 | 121 | |||
1628 | 122 | def connectionMade(self): | ||
1629 | 123 | print 'CONN MADE' | ||
1630 | 124 | |||
1631 | 125 | def dataReceived(self, data): | ||
1632 | 126 | print 'HOLY SHNIKIES', data | ||
1633 | 127 | |||
1634 | 128 | class VertexFactory(ServerFactory): | ||
1635 | 129 | protocol = VertexDemoProtocol | ||
1636 | 130 | |||
1637 | 131 | def __init__(self, plug): | ||
1638 | 132 | self.plug = plug | ||
1639 | 133 | |||
1640 | 134 | def startFactory(self): | ||
1641 | 135 | #self.plug.animator.stop(1) | ||
1642 | 136 | pass | ||
1643 | 137 | |||
1644 | 138 | def stopFactory(self): | ||
1645 | 139 | #self.plug.animator.stop(0) | ||
1646 | 140 | pass | ||
1647 | 141 | |||
1648 | 142 | |||
1649 | 143 | class BuddyItem: | ||
1650 | 144 | def __init__(self, plug, alias, q2qaddress): | ||
1651 | 145 | mi = self.menuItem = gtk.MenuItem(alias + " <"+q2qaddress+">") | ||
1652 | 146 | mi.connect("activate", self.initiateFileTransfer) | ||
1653 | 147 | mi.show_all() | ||
1654 | 148 | self.plug = plug | ||
1655 | 149 | self.alias = alias | ||
1656 | 150 | self.q2qaddress = q2qaddress | ||
1657 | 151 | self.plug.loadedBuddies[q2qaddress] = self | ||
1658 | 152 | |||
1659 | 153 | def initiateFileTransfer(self, evt): | ||
1660 | 154 | print 'Initiate transfer with ' + self.alias + self.q2qaddress | ||
1661 | 155 | |||
1662 | 156 | def addToMenu(self): | ||
1663 | 157 | self.plug.section.append(self.menuItem) | ||
1664 | 158 | |||
1665 | 159 | def removeFromMenu(self): | ||
1666 | 160 | self.plug.section.remove(self.menuItem) | ||
1667 | 161 | |||
1668 | 162 | from twisted.plugin import IPlugin | ||
1669 | 163 | from prime.iprime import IMenuApplication | ||
1670 | 164 | from zope.interface import implements | ||
1671 | 165 | |||
1672 | 166 | class PlugEntry: | ||
1673 | 167 | implements(IMenuApplication, IPlugin) | ||
1674 | 168 | |||
1675 | 169 | def __init__(self): | ||
1676 | 170 | self.xml = gtk.glade.XML(GLADE_FILE, "notification_popup") | ||
1677 | 171 | |||
1678 | 172 | def register(self, section): | ||
1679 | 173 | print 'REGISTER' | ||
1680 | 174 | self.section = section | ||
1681 | 175 | |||
1682 | 176 | workingdir = FilePath(os.path.expanduser("~/.vertex")) | ||
1683 | 177 | self.clientService = ClientQ2QService( | ||
1684 | 178 | workingdir.child("q2q-certificates").path, | ||
1685 | 179 | verifyHook=self.displayVerifyDialog, | ||
1686 | 180 | inboundTCPPortnum=8172, | ||
1687 | 181 | # q2qPortnum=8173, | ||
1688 | 182 | udpEnabled=False) | ||
1689 | 183 | self.setCurrentID(self.clientService.getDefaultFrom()) | ||
1690 | 184 | self.buddiesfile = workingdir.child("q2q-buddies.txt") | ||
1691 | 185 | self.loadedBuddies = {} | ||
1692 | 186 | self.parseBuddies() | ||
1693 | 187 | |||
1694 | 188 | def parseBuddies(self): | ||
1695 | 189 | try: | ||
1696 | 190 | self.buddyList = rfc822.AddressList(self.buddiesfile.open().read()) | ||
1697 | 191 | except IOError: | ||
1698 | 192 | return | ||
1699 | 193 | self.clearContactMenu() | ||
1700 | 194 | for dispn, addr in self.buddyList: | ||
1701 | 195 | if addr not in self.loadedBuddies: | ||
1702 | 196 | BuddyItem(self, dispn, addr) | ||
1703 | 197 | self.buildContactMenu() | ||
1704 | 198 | |||
1705 | 199 | def clearContactMenu(self): | ||
1706 | 200 | for bud in self.loadedBuddies.values(): | ||
1707 | 201 | bud.removeFromMenu() | ||
1708 | 202 | |||
1709 | 203 | def buildContactMenu(self): | ||
1710 | 204 | l = self.loadedBuddies.values() | ||
1711 | 205 | l.sort(key=lambda x: x.alias) | ||
1712 | 206 | l.reverse() | ||
1713 | 207 | for bud in l: | ||
1714 | 208 | bud.addToMenu() | ||
1715 | 209 | |||
1716 | 210 | def addBuddy(self, alias, q2qaddr): | ||
1717 | 211 | temp = self.buddiesfile.temporarySibling() | ||
1718 | 212 | try: | ||
1719 | 213 | origdata = self.buddiesfile.open().read() | ||
1720 | 214 | except IOError: | ||
1721 | 215 | origdata = '' | ||
1722 | 216 | moredata = '\n%s <%s>' % (alias, q2qaddr) | ||
1723 | 217 | ftemp = temp.open('w') | ||
1724 | 218 | ftemp.write(origdata) | ||
1725 | 219 | ftemp.write(moredata) | ||
1726 | 220 | ftemp.close() | ||
1727 | 221 | temp.moveTo(self.buddiesfile) | ||
1728 | 222 | self.parseBuddies() | ||
1729 | 223 | |||
1730 | 224 | def displayVerifyDialog(self, From, to, protocol): | ||
1731 | 225 | from twisted.internet import defer | ||
1732 | 226 | d = defer.Deferred() | ||
1733 | 227 | AcceptConnectionDialog(d, From, to, protocol) | ||
1734 | 228 | return d | ||
1735 | 229 | |||
1736 | 230 | def setCurrentID(self, idName): | ||
1737 | 231 | |||
1738 | 232 | if idName is not None: | ||
1739 | 233 | currentID = Q2QAddress.fromString(idName) | ||
1740 | 234 | # log in? | ||
1741 | 235 | # self.animator.start() | ||
1742 | 236 | SL = self.xml.get_widget("identifymenuitem").get_children()[0].set_label | ||
1743 | 237 | def loggedIn(result): | ||
1744 | 238 | SL(str(currentID)) | ||
1745 | 239 | self.currentID = currentID | ||
1746 | 240 | def notLoggedIn(error): | ||
1747 | 241 | SL("Identify") | ||
1748 | 242 | # self.animator.stop(0) | ||
1749 | 243 | # This following order is INSANE - you should definitely not have | ||
1750 | 244 | # to wait until the LISTEN succeeds to start the service; quite the | ||
1751 | 245 | # opposite, you should wait until the service has started, then | ||
1752 | 246 | # issue the LISTEN!! For some reason, the connection drops | ||
1753 | 247 | # immediately if you do that, and I have no idea why. As soon as I | ||
1754 | 248 | # can fix that issue the startService should be moved up previous | ||
1755 | 249 | # to listenQ2Q. | ||
1756 | 250 | self.clientService.listenQ2Q(currentID, | ||
1757 | 251 | {'vertex': VertexFactory(self)}, | ||
1758 | 252 | "desktop vertex UI").addCallbacks( | ||
1759 | 253 | loggedIn, notLoggedIn).addCallback( | ||
1760 | 254 | lambda ign: self.clientService.startService()) | ||
1761 | 255 | |||
1762 | 256 | # XXX event handlers | ||
1763 | 257 | |||
1764 | 258 | def toggleAnimate(self, event): | ||
1765 | 259 | if self.animator.animating: | ||
1766 | 260 | # SL("Animate") | ||
1767 | 261 | self.animator.stop() | ||
1768 | 262 | else: | ||
1769 | 263 | # SL("Stop Animating") | ||
1770 | 264 | self.animator.start() | ||
1771 | 265 | |||
1772 | 266 | def identifyDialog(self, event): | ||
1773 | 267 | IdentificationDialog(self.clientService, self) | ||
1774 | 268 | |||
1775 | 269 | def addContact(self, event): | ||
1776 | 270 | AddContactDialog(self) | ||
1777 | 271 | 0 | ||
1778 | === removed file 'Vertex/vertex/icon-active.png' | |||
1779 | 272 | Binary files Vertex/vertex/icon-active.png 2006-06-01 04:57:02 +0000 and Vertex/vertex/icon-active.png 1970-01-01 00:00:00 +0000 differ | 1 | Binary files Vertex/vertex/icon-active.png 2006-06-01 04:57:02 +0000 and Vertex/vertex/icon-active.png 1970-01-01 00:00:00 +0000 differ |
1780 | === removed file 'Vertex/vertex/icon-inactive.png' | |||
1781 | 273 | Binary files Vertex/vertex/icon-inactive.png 2006-06-01 04:57:02 +0000 and Vertex/vertex/icon-inactive.png 1970-01-01 00:00:00 +0000 differ | 2 | Binary files Vertex/vertex/icon-inactive.png 2006-06-01 04:57:02 +0000 and Vertex/vertex/icon-inactive.png 1970-01-01 00:00:00 +0000 differ |
1782 | === removed file 'Vertex/vertex/ivertex.py' | |||
1783 | --- Vertex/vertex/ivertex.py 2012-03-14 16:23:22 +0000 | |||
1784 | +++ Vertex/vertex/ivertex.py 1970-01-01 00:00:00 +0000 | |||
1785 | @@ -1,108 +0,0 @@ | |||
1786 | 1 | # Copyright 2005 Divmod, Inc. See LICENSE file for details | ||
1787 | 2 | |||
1788 | 3 | from zope.interface import Interface | ||
1789 | 4 | |||
1790 | 5 | class IQ2QTransport(Interface): | ||
1791 | 6 | """ | ||
1792 | 7 | I am a byte-stream-oriented transport which has Q2Q identifiers associated | ||
1793 | 8 | with the endpoints, and possibly some cryptographic verification of the | ||
1794 | 9 | authenticity of those endpoints. | ||
1795 | 10 | """ | ||
1796 | 11 | |||
1797 | 12 | def getQ2QHost(): | ||
1798 | 13 | """ Returns a Q2QAddress object representing the user on this end of the | ||
1799 | 14 | connection. | ||
1800 | 15 | """ | ||
1801 | 16 | |||
1802 | 17 | def getQ2QPeer(): | ||
1803 | 18 | """ Returns a Q2QAddress object representing the user on the other end of the | ||
1804 | 19 | connection. | ||
1805 | 20 | """ | ||
1806 | 21 | |||
1807 | 22 | class IQ2QUser(Interface): | ||
1808 | 23 | """ | ||
1809 | 24 | A cred interface for Q2Q users. | ||
1810 | 25 | """ | ||
1811 | 26 | def signCertificateRequest(certificateRequest, domainCert, suggestedSerial): | ||
1812 | 27 | """ | ||
1813 | 28 | Return a signed certificate object if the subject fields in the | ||
1814 | 29 | certificateRequest are valid. | ||
1815 | 30 | """ | ||
1816 | 31 | |||
1817 | 32 | class IFileTransfer(Interface): | ||
1818 | 33 | |||
1819 | 34 | def getUploadSink(self, path): | ||
1820 | 35 | """ | ||
1821 | 36 | @param path: a PathFragment that the client wishes to upload to. | ||
1822 | 37 | |||
1823 | 38 | @return: a DataSink where we'll save the data to. | ||
1824 | 39 | """ | ||
1825 | 40 | |||
1826 | 41 | def getDownloadSource(self, path): | ||
1827 | 42 | """ | ||
1828 | 43 | @param path: a PathFragment that the client wishes to download. | ||
1829 | 44 | |||
1830 | 45 | @return: a DataSource to download data from. | ||
1831 | 46 | """ | ||
1832 | 47 | |||
1833 | 48 | def listChildren(self, path): | ||
1834 | 49 | """ | ||
1835 | 50 | @param path: a PathFragment that the client wishes to get a list of. | ||
1836 | 51 | |||
1837 | 52 | @return: a list of dictionaries mapping:: | ||
1838 | 53 | {'name': str, | ||
1839 | 54 | 'size': int, | ||
1840 | 55 | 'type': vertex.filexfer.MIMEType, | ||
1841 | 56 | 'modified': datetime.datetime} | ||
1842 | 57 | """ | ||
1843 | 58 | |||
1844 | 59 | class ISessionTokenStorage(Interface): | ||
1845 | 60 | def idFromCookie(self, cookie, domain): | ||
1846 | 61 | """Look up a user ID from the given cookie in the given domain. | ||
1847 | 62 | """ | ||
1848 | 63 | |||
1849 | 64 | class ICertificateStorage(Interface): | ||
1850 | 65 | def getSelfSignedCertificate(self, domainName): | ||
1851 | 66 | """ | ||
1852 | 67 | @return: a Deferred which will fire with the certificate for the given | ||
1853 | 68 | domain name. | ||
1854 | 69 | """ | ||
1855 | 70 | |||
1856 | 71 | def storeSelfSignedCertificate(self, domainName, mainCert): | ||
1857 | 72 | """ | ||
1858 | 73 | @type mainCert: C{str} | ||
1859 | 74 | @param mainCert: Serialized, self-signed certificate to associate | ||
1860 | 75 | with the given domain. | ||
1861 | 76 | |||
1862 | 77 | @return: a Deferred which will fire when the certificate has been | ||
1863 | 78 | stored successfully. | ||
1864 | 79 | """ | ||
1865 | 80 | |||
1866 | 81 | def getPrivateCertificate(self, domainName): | ||
1867 | 82 | """ | ||
1868 | 83 | @return: a PrivateCertificate instance, e.g. a certificate including a | ||
1869 | 84 | private key, for 'domainName'. | ||
1870 | 85 | """ | ||
1871 | 86 | |||
1872 | 87 | def addPrivateCertificate(self, domainName, existingCertificate=None): | ||
1873 | 88 | """ | ||
1874 | 89 | """ | ||
1875 | 90 | |||
1876 | 91 | class IOfferUp(Interface): | ||
1877 | 92 | """ | ||
1878 | 93 | Sharing control database storage. | ||
1879 | 94 | """ | ||
1880 | 95 | |||
1881 | 96 | class IPlugin(Interface): | ||
1882 | 97 | """ | ||
1883 | 98 | """ | ||
1884 | 99 | |||
1885 | 100 | class ITestPlugin(Interface): | ||
1886 | 101 | """ | ||
1887 | 102 | Dummy plug-in interface for unit testing. | ||
1888 | 103 | """ | ||
1889 | 104 | |||
1890 | 105 | class ITestPlugin2(Interface): | ||
1891 | 106 | """ | ||
1892 | 107 | Dummy plug-in interface for unit testing. | ||
1893 | 108 | """ | ||
1894 | 109 | 0 | ||
1895 | === removed file 'Vertex/vertex/ptcp.py' | |||
1896 | --- Vertex/vertex/ptcp.py 2012-03-14 16:23:22 +0000 | |||
1897 | +++ Vertex/vertex/ptcp.py 1970-01-01 00:00:00 +0000 | |||
1898 | @@ -1,1050 +0,0 @@ | |||
1899 | 1 | # -*- test-case-name: vertex.test.test_ptcp -*- | ||
1900 | 2 | |||
1901 | 3 | import struct | ||
1902 | 4 | |||
1903 | 5 | from binascii import crc32 # used to use zlib.crc32 - but that gives different | ||
1904 | 6 | # results on 64-bit platforms!! | ||
1905 | 7 | |||
1906 | 8 | import itertools | ||
1907 | 9 | |||
1908 | 10 | from twisted.python.failure import Failure | ||
1909 | 11 | from twisted.internet.defer import Deferred | ||
1910 | 12 | from twisted.internet import protocol, error, reactor, defer | ||
1911 | 13 | from twisted.internet.main import CONNECTION_DONE | ||
1912 | 14 | from twisted.python import log, util | ||
1913 | 15 | |||
1914 | 16 | from vertex import tcpdfa | ||
1915 | 17 | from vertex.statemachine import StateError | ||
1916 | 18 | |||
1917 | 19 | |||
1918 | 20 | genConnID = itertools.count(8).next | ||
1919 | 21 | |||
1920 | 22 | MAX_PSEUDO_PORT = (2 ** 16) | ||
1921 | 23 | |||
1922 | 24 | _packetFormat = ('!' # WTF did you think | ||
1923 | 25 | 'H' # sourcePseudoPort | ||
1924 | 26 | 'H' # destPseudoPort | ||
1925 | 27 | 'L' # sequenceNumber | ||
1926 | 28 | 'L' # acknowledgementNumber | ||
1927 | 29 | 'L' # window | ||
1928 | 30 | 'B' # flags | ||
1929 | 31 | 'l' # checksum | ||
1930 | 32 | # (signed because of binascii.crc32) | ||
1931 | 33 | 'H' # dlen | ||
1932 | 34 | ) | ||
1933 | 35 | _fixedSize = struct.calcsize(_packetFormat) | ||
1934 | 36 | |||
1935 | 37 | _SYN, _ACK, _FIN, _RST, _STB = [1 << n for n in range(5)] | ||
1936 | 38 | |||
1937 | 39 | def _flagprop(flag): | ||
1938 | 40 | def setter(self, value): | ||
1939 | 41 | if value: | ||
1940 | 42 | self.flags |= flag | ||
1941 | 43 | else: | ||
1942 | 44 | self.flags &= ~flag | ||
1943 | 45 | return property(lambda self: bool(self.flags & flag), setter) | ||
1944 | 46 | |||
1945 | 47 | def relativeSequence(wireSequence, initialSequence, lapNumber): | ||
1946 | 48 | """ Compute a relative sequence number from a wire sequence number so that we | ||
1947 | 49 | can use natural Python comparisons on it, such as <, >, ==. | ||
1948 | 50 | |||
1949 | 51 | @param wireSequence: the sequence number received on the wire. | ||
1950 | 52 | |||
1951 | 53 | @param initialSequence: the ISN for this sequence, negotiated at SYN time. | ||
1952 | 54 | |||
1953 | 55 | @param lapNumber: the number of times that this value has wrapped around | ||
1954 | 56 | 2**32. | ||
1955 | 57 | """ | ||
1956 | 58 | return (wireSequence + (lapNumber * (2**32))) - initialSequence | ||
1957 | 59 | |||
1958 | 60 | class PTCPPacket(util.FancyStrMixin, object): | ||
1959 | 61 | showAttributes = ( | ||
1960 | 62 | ('sourcePseudoPort', 'sourcePseudoPort', '%d'), | ||
1961 | 63 | ('destPseudoPort', 'destPseudoPort', '%d'), | ||
1962 | 64 | ('shortdata', 'data', '%r'), | ||
1963 | 65 | ('niceflags', 'flags', '%s'), | ||
1964 | 66 | ('dlen', 'dlen', '%d'), | ||
1965 | 67 | ('seqNum', 'seq', '%d'), | ||
1966 | 68 | ('ackNum', 'ack', '%d'), | ||
1967 | 69 | ('checksum', 'checksum', '%x'), | ||
1968 | 70 | ('peerAddressTuple', 'peerAddress', '%r'), | ||
1969 | 71 | ('retransmitCount', 'retransmitCount', '%d'), | ||
1970 | 72 | ) | ||
1971 | 73 | |||
1972 | 74 | syn = _flagprop(_SYN) | ||
1973 | 75 | ack = _flagprop(_ACK) | ||
1974 | 76 | fin = _flagprop(_FIN) | ||
1975 | 77 | rst = _flagprop(_RST) | ||
1976 | 78 | stb = _flagprop(_STB) | ||
1977 | 79 | |||
1978 | 80 | # Number of retransmit attempts left for this segment. When it reaches | ||
1979 | 81 | # zero, this segment is dead. | ||
1980 | 82 | retransmitCount = 50 | ||
1981 | 83 | |||
1982 | 84 | def shortdata(): | ||
1983 | 85 | def get(self): | ||
1984 | 86 | if len(self.data) > 13: | ||
1985 | 87 | return self.data[:5] + '...' + self.data[-5:] | ||
1986 | 88 | else: | ||
1987 | 89 | return self.data | ||
1988 | 90 | return get, | ||
1989 | 91 | shortdata = property(*shortdata()) | ||
1990 | 92 | |||
1991 | 93 | def niceflags(): | ||
1992 | 94 | def get(self): | ||
1993 | 95 | res = [] | ||
1994 | 96 | for (f, v) in [ | ||
1995 | 97 | (self.syn, 'S'), (self.ack, 'A'), (self.fin, 'F'), | ||
1996 | 98 | (self.rst, 'R'), (self.stb, 'T')]: | ||
1997 | 99 | res.append(f and v or '.') | ||
1998 | 100 | return ''.join(res) | ||
1999 | 101 | return get, | ||
2000 | 102 | niceflags = property(*niceflags()) | ||
2001 | 103 | |||
2002 | 104 | def create(cls, | ||
2003 | 105 | sourcePseudoPort, destPseudoPort, | ||
2004 | 106 | seqNum, ackNum, data, | ||
2005 | 107 | window=(1 << 15), | ||
2006 | 108 | syn=False, ack=False, fin=False, | ||
2007 | 109 | rst=False, stb=False, | ||
2008 | 110 | destination=None): | ||
2009 | 111 | i = cls(sourcePseudoPort, destPseudoPort, | ||
2010 | 112 | seqNum, ackNum, window, | ||
2011 | 113 | 0, 0, len(data), data) | ||
2012 | 114 | i.syn = syn | ||
2013 | 115 | i.ack = ack | ||
2014 | 116 | i.fin = fin | ||
2015 | 117 | i.rst = rst | ||
2016 | 118 | i.stb = stb | ||
2017 | 119 | i.checksum = i.computeChecksum() | ||
2018 | 120 | i.destination = destination | ||
2019 | 121 | return i | ||
2020 | 122 | create = classmethod(create) | ||
2021 | 123 | |||
2022 | 124 | |||
2023 | 125 | def __init__(self, | ||
2024 | 126 | sourcePseudoPort, | ||
2025 | 127 | destPseudoPort, | ||
2026 | 128 | seqNum, ackNum, window, flags, | ||
2027 | 129 | checksum, dlen, data, peerAddressTuple=None, | ||
2028 | 130 | seqOffset=0, ackOffset=0, seqLaps=0, ackLaps=0): | ||
2029 | 131 | self.sourcePseudoPort = sourcePseudoPort | ||
2030 | 132 | self.destPseudoPort = destPseudoPort | ||
2031 | 133 | self.seqNum = seqNum | ||
2032 | 134 | self.ackNum = ackNum | ||
2033 | 135 | self.window = window | ||
2034 | 136 | self.flags = flags | ||
2035 | 137 | self.checksum = checksum | ||
2036 | 138 | self.dlen = dlen | ||
2037 | 139 | self.data = data | ||
2038 | 140 | self.peerAddressTuple = peerAddressTuple # None if local | ||
2039 | 141 | |||
2040 | 142 | self.seqOffset = seqOffset | ||
2041 | 143 | self.ackOffset = ackOffset | ||
2042 | 144 | self.seqLaps = seqLaps | ||
2043 | 145 | self.ackLaps = ackLaps | ||
2044 | 146 | |||
2045 | 147 | def segmentLength(self): | ||
2046 | 148 | """RFC page 26: 'The segment length (SEG.LEN) includes both data and sequence | ||
2047 | 149 | space occupying controls' | ||
2048 | 150 | """ | ||
2049 | 151 | return self.dlen + self.syn + self.fin | ||
2050 | 152 | |||
2051 | 153 | def relativeSeq(self): | ||
2052 | 154 | return relativeSequence(self.seqNum, self.seqOffset, self.seqLaps) | ||
2053 | 155 | |||
2054 | 156 | def relativeAck(self): | ||
2055 | 157 | return relativeSequence(self.ackNum, self.ackOffset, self.ackLaps) | ||
2056 | 158 | |||
2057 | 159 | |||
2058 | 160 | def verifyChecksum(self): | ||
2059 | 161 | if len(self.data) != self.dlen: | ||
2060 | 162 | if len(self.data) > self.dlen: | ||
2061 | 163 | raise GarbageDataError(self) | ||
2062 | 164 | else: | ||
2063 | 165 | raise TruncatedDataError(self) | ||
2064 | 166 | expected = self.computeChecksum() | ||
2065 | 167 | received = self.checksum | ||
2066 | 168 | if expected != received: | ||
2067 | 169 | raise ChecksumMismatchError(expected, received) | ||
2068 | 170 | |||
2069 | 171 | def computeChecksum(self): | ||
2070 | 172 | return crc32(self.data) | ||
2071 | 173 | |||
2072 | 174 | def decode(cls, bytes, hostPortPair): | ||
2073 | 175 | fields = struct.unpack(_packetFormat, bytes[:_fixedSize]) | ||
2074 | 176 | sourcePseudoPort, destPseudoPort, seq, ack, window, flags, checksum, dlen = fields | ||
2075 | 177 | data = bytes[_fixedSize:] | ||
2076 | 178 | pkt = cls(sourcePseudoPort, destPseudoPort, seq, ack, window, flags, | ||
2077 | 179 | checksum, dlen, data, hostPortPair) | ||
2078 | 180 | return pkt | ||
2079 | 181 | decode = classmethod(decode) | ||
2080 | 182 | |||
2081 | 183 | def mustRetransmit(self): | ||
2082 | 184 | """Check to see if this packet must be retransmitted until it was received. | ||
2083 | 185 | """ | ||
2084 | 186 | if self.syn or self.fin or self.dlen: | ||
2085 | 187 | return True | ||
2086 | 188 | return False | ||
2087 | 189 | |||
2088 | 190 | def encode(self): | ||
2089 | 191 | dlen = len(self.data) | ||
2090 | 192 | checksum = self.computeChecksum() | ||
2091 | 193 | return struct.pack( | ||
2092 | 194 | _packetFormat, | ||
2093 | 195 | self.sourcePseudoPort, self.destPseudoPort, | ||
2094 | 196 | self.seqNum, self.ackNum, self.window, | ||
2095 | 197 | self.flags, checksum, dlen) + self.data | ||
2096 | 198 | |||
2097 | 199 | def fragment(self, mtu): | ||
2098 | 200 | if self.dlen < mtu: | ||
2099 | 201 | return [self] | ||
2100 | 202 | assert not self.syn, "should not be originating syn packets w/ data" | ||
2101 | 203 | seqOfft = 0 | ||
2102 | 204 | L = [] | ||
2103 | 205 | # XXX TODO: need to take seqLaps into account, etc. | ||
2104 | 206 | for chunk in iterchunks(self.data, mtu): | ||
2105 | 207 | last = self.create(self.sourcePseudoPort, | ||
2106 | 208 | self.destPseudoPort, | ||
2107 | 209 | self.seqNum + seqOfft, | ||
2108 | 210 | self.ackNum, | ||
2109 | 211 | chunk, | ||
2110 | 212 | self.window, | ||
2111 | 213 | destination=self.destination, | ||
2112 | 214 | ack=self.ack) | ||
2113 | 215 | L.append(last) | ||
2114 | 216 | seqOfft += len(chunk) | ||
2115 | 217 | if self.fin: | ||
2116 | 218 | last.fin = self.fin | ||
2117 | 219 | last.checksum = last.computeChecksum() | ||
2118 | 220 | return L | ||
2119 | 221 | |||
2120 | 222 | |||
2121 | 223 | def iterchunks(data, chunksize): | ||
2122 | 224 | """iterate chunks of data | ||
2123 | 225 | """ | ||
2124 | 226 | offt = 0 | ||
2125 | 227 | while offt < len(data): | ||
2126 | 228 | yield data[offt:offt+chunksize] | ||
2127 | 229 | offt += chunksize | ||
2128 | 230 | |||
2129 | 231 | |||
2130 | 232 | def ISN(): | ||
2131 | 233 | """ | ||
2132 | 234 | Initial Sequence Number generator. | ||
2133 | 235 | """ | ||
2134 | 236 | # return int((time.time() * 1000000) / 4) % 2**32 | ||
2135 | 237 | return 0 | ||
2136 | 238 | |||
2137 | 239 | |||
2138 | 240 | def segmentAcceptable(RCV_NXT, RCV_WND, SEG_SEQ, SEG_LEN): | ||
2139 | 241 | # RFC page 26. | ||
2140 | 242 | if SEG_LEN == 0 and RCV_WND == 0: | ||
2141 | 243 | return SEG_SEQ == RCV_NXT | ||
2142 | 244 | if SEG_LEN == 0 and RCV_WND > 0: | ||
2143 | 245 | return ((RCV_NXT <= SEG_SEQ) and (SEG_SEQ < RCV_NXT + RCV_WND)) | ||
2144 | 246 | if SEG_LEN > 0 and RCV_WND == 0: | ||
2145 | 247 | return False | ||
2146 | 248 | if SEG_LEN > 0 and RCV_WND > 0: | ||
2147 | 249 | return (( (RCV_NXT <= SEG_SEQ) and (SEG_SEQ < RCV_NXT + RCV_WND)) | ||
2148 | 250 | or ((RCV_NXT <= SEG_SEQ+SEG_LEN-1) and | ||
2149 | 251 | (SEG_SEQ+SEG_LEN-1 < RCV_NXT + RCV_WND))) | ||
2150 | 252 | assert 0, 'Should be impossible to get here.' | ||
2151 | 253 | return False | ||
2152 | 254 | |||
2153 | 255 | class BadPacketError(Exception): | ||
2154 | 256 | """ | ||
2155 | 257 | A packet was bad for some reason. | ||
2156 | 258 | """ | ||
2157 | 259 | |||
2158 | 260 | class ChecksumMismatchError(Exception): | ||
2159 | 261 | """ | ||
2160 | 262 | The checksum and data received did not match. | ||
2161 | 263 | """ | ||
2162 | 264 | |||
2163 | 265 | class TruncatedDataError(Exception): | ||
2164 | 266 | """ | ||
2165 | 267 | The packet was truncated in transit, and all of the data did not arrive. | ||
2166 | 268 | """ | ||
2167 | 269 | |||
2168 | 270 | class GarbageDataError(Exception): | ||
2169 | 271 | """ | ||
2170 | 272 | Too much data was received (???) | ||
2171 | 273 | """ | ||
2172 | 274 | |||
2173 | 275 | class PTCPConnection(tcpdfa.TCP): | ||
2174 | 276 | """ | ||
2175 | 277 | Implementation of RFC 793 state machine. | ||
2176 | 278 | |||
2177 | 279 | @ivar oldestUnackedSendSeqNum: (TCP RFC: SND.UNA) The oldest (relative) | ||
2178 | 280 | sequence number referring to an octet which we have sent or may send which | ||
2179 | 281 | is unacknowledged. This begins at 0, which is special because it is not | ||
2180 | 282 | for an octet, but rather for the initial SYN packet. Unless it is 0, this | ||
2181 | 283 | represents the sequence number of self._outgoingBytes[0]. | ||
2182 | 284 | |||
2183 | 285 | @ivar nextSendSeqNum: (TCP RFC: SND.NXT) The next (relative) sequence | ||
2184 | 286 | number that we will send to our peer after the current buffered segments | ||
2185 | 287 | have all been acknowledged. This is the sequence number of the | ||
2186 | 288 | not-yet-extant octet in the stream at | ||
2187 | 289 | self._outgoingBytes[len(self._outgoingBytes)]. | ||
2188 | 290 | |||
2189 | 291 | @ivar nextRecvSeqNum: (TCP RFC: RCV.NXT) The next (relative) sequence | ||
2190 | 292 | number that the peer should send to us if they want to send more data; | ||
2191 | 293 | their first unacknowledged sequence number as far as we are concerned; the | ||
2192 | 294 | left or lower edge of the receive window; the sequence number of the first | ||
2193 | 295 | octet that has not been delivered to the application. changed whenever we | ||
2194 | 296 | receive an appropriate ACK. | ||
2195 | 297 | |||
2196 | 298 | @ivar peerSendISN: the initial sequence number that the peer sent us during | ||
2197 | 299 | the negotiation phase. All peer-relative sequence numbers are computed | ||
2198 | 300 | using this. (see C{relativeSequence}). | ||
2199 | 301 | |||
2200 | 302 | @ivar hostSendISN: the initial sequence number that the we sent during the | ||
2201 | 303 | negotiation phase. All host-relative sequence numbers are computed using | ||
2202 | 304 | this. (see C{relativeSequence}) | ||
2203 | 305 | |||
2204 | 306 | @ivar retransmissionQueue: a list of packets to be re-sent until their | ||
2205 | 307 | acknowledgements come through. | ||
2206 | 308 | |||
2207 | 309 | @ivar recvWindow: (TCP RFC: RCV.WND) - the size [in octets] of the current | ||
2208 | 310 | window allowed by this host, to be in transit from the other host. | ||
2209 | 311 | |||
2210 | 312 | @ivar sendWindow: (TCP RFC: SND.WND) - the size [in octets] of the current | ||
2211 | 313 | window allowed by our peer, to be in transit from us. | ||
2212 | 314 | |||
2213 | 315 | """ | ||
2214 | 316 | |||
2215 | 317 | mtu = 512 - _fixedSize | ||
2216 | 318 | |||
2217 | 319 | recvWindow = mtu | ||
2218 | 320 | sendWindow = mtu | ||
2219 | 321 | sendWindowRemaining = mtu * 2 | ||
2220 | 322 | |||
2221 | 323 | protocol = None | ||
2222 | 324 | |||
2223 | 325 | def __init__(self, | ||
2224 | 326 | hostPseudoPort, peerPseudoPort, | ||
2225 | 327 | ptcp, factory, peerAddressTuple): | ||
2226 | 328 | tcpdfa.TCP.__init__(self) | ||
2227 | 329 | self.hostPseudoPort = hostPseudoPort | ||
2228 | 330 | self.peerPseudoPort = peerPseudoPort | ||
2229 | 331 | self.ptcp = ptcp | ||
2230 | 332 | self.factory = factory | ||
2231 | 333 | self._receiveBuffer = [] | ||
2232 | 334 | self.retransmissionQueue = [] | ||
2233 | 335 | self.peerAddressTuple = peerAddressTuple | ||
2234 | 336 | |||
2235 | 337 | self.oldestUnackedSendSeqNum = 0 | ||
2236 | 338 | self.nextSendSeqNum = 0 | ||
2237 | 339 | self.hostSendISN = 0 | ||
2238 | 340 | self.nextRecvSeqNum = 0 | ||
2239 | 341 | self.peerSendISN = 0 | ||
2240 | 342 | self.setPeerISN = False | ||
2241 | 343 | |||
2242 | 344 | peerSendISN = None | ||
2243 | 345 | |||
2244 | 346 | def packetReceived(self, packet): | ||
2245 | 347 | # XXX TODO: probably have to do something to the packet here to | ||
2246 | 348 | # identify its relative sequence number. | ||
2247 | 349 | |||
2248 | 350 | # print 'received', self, packet | ||
2249 | 351 | |||
2250 | 352 | if packet.stb: | ||
2251 | 353 | # Shrink the MTU | ||
2252 | 354 | [self.mtu] = struct.unpack('!H', packet.data) | ||
2253 | 355 | rq = [] | ||
2254 | 356 | for pkt in self.retransmissionQueue: | ||
2255 | 357 | rq.extend(pkt.fragment(self.mtu)) | ||
2256 | 358 | self.retransmissionQueue = rq | ||
2257 | 359 | return | ||
2258 | 360 | |||
2259 | 361 | if self._paused: | ||
2260 | 362 | return | ||
2261 | 363 | |||
2262 | 364 | generatedStateMachineInput = False | ||
2263 | 365 | if packet.syn: | ||
2264 | 366 | if packet.dlen: | ||
2265 | 367 | # Whoops, what? SYNs probably can contain data, I think, but I | ||
2266 | 368 | # certainly don't see anything in the spec about how to deal | ||
2267 | 369 | # with this or in ethereal for how linux deals with it -glyph | ||
2268 | 370 | raise BadPacketError( | ||
2269 | 371 | "currently no data allowed in SYN packets: %r" | ||
2270 | 372 | % (packet,)) | ||
2271 | 373 | else: | ||
2272 | 374 | assert packet.segmentLength() == 1 | ||
2273 | 375 | if self.peerAddressTuple is None: | ||
2274 | 376 | # we're a server | ||
2275 | 377 | assert self.wasEverListen, "Clients must specify a connect address." | ||
2276 | 378 | self.peerAddressTuple = packet.peerAddressTuple | ||
2277 | 379 | else: | ||
2278 | 380 | # we're a client | ||
2279 | 381 | assert self.peerAddressTuple == packet.peerAddressTuple | ||
2280 | 382 | if self.setPeerISN: | ||
2281 | 383 | if self.peerSendISN != packet.seqNum: | ||
2282 | 384 | raise BadPacketError( | ||
2283 | 385 | "Peer ISN was already set to %s but incoming packet " | ||
2284 | 386 | "tried to set it to %s" % ( | ||
2285 | 387 | self.peerSendISN, packet.seqNum)) | ||
2286 | 388 | if not self.retransmissionQueue: | ||
2287 | 389 | # If our retransmissionQueue is hot, we are going to send | ||
2288 | 390 | # them an ACK to this with the next packet we send them | ||
2289 | 391 | # anyway; as a bonus, this will properly determine whether | ||
2290 | 392 | # we're sending a SYN+ACK or merely an ACK; the only time | ||
2291 | 393 | # we send an ACK is when we have nothing to say to them and | ||
2292 | 394 | # they're blocked on getting a response to their SYN+ACK | ||
2293 | 395 | # from us. -glyph | ||
2294 | 396 | self.originate(ack=True) | ||
2295 | 397 | return | ||
2296 | 398 | self.setPeerISN = True | ||
2297 | 399 | self.peerSendISN = packet.seqNum | ||
2298 | 400 | # syn, fin, and data are mutually exclusive, so this relative | ||
2299 | 401 | # sequence-number increment is done both here, and below in the | ||
2300 | 402 | # data/fin processing block. | ||
2301 | 403 | self.nextRecvSeqNum += packet.segmentLength() | ||
2302 | 404 | if not packet.ack: | ||
2303 | 405 | generatedStateMachineInput = True | ||
2304 | 406 | self.input(tcpdfa.SYN) | ||
2305 | 407 | |||
2306 | 408 | SEG_ACK = packet.relativeAck() # aliasing this for easier reading w/ | ||
2307 | 409 | # the RFC | ||
2308 | 410 | if packet.ack: | ||
2309 | 411 | if (self.oldestUnackedSendSeqNum < SEG_ACK and | ||
2310 | 412 | SEG_ACK <= self.nextSendSeqNum): | ||
2311 | 413 | # According to the spec, an 'acceptable ack | ||
2312 | 414 | rq = self.retransmissionQueue | ||
2313 | 415 | while rq: | ||
2314 | 416 | segmentOnQueue = rq[0] | ||
2315 | 417 | qSegSeq = segmentOnQueue.relativeSeq() | ||
2316 | 418 | if qSegSeq + segmentOnQueue.segmentLength() <= SEG_ACK: | ||
2317 | 419 | # fully acknowledged, as per RFC! | ||
2318 | 420 | rq.pop(0) | ||
2319 | 421 | sminput = None | ||
2320 | 422 | self.sendWindowRemaining += segmentOnQueue.segmentLength() | ||
2321 | 423 | # print 'inc send window', self, self.sendWindowRemaining | ||
2322 | 424 | if segmentOnQueue.syn: | ||
2323 | 425 | if packet.syn: | ||
2324 | 426 | sminput = tcpdfa.SYN_ACK | ||
2325 | 427 | else: | ||
2326 | 428 | sminput = tcpdfa.ACK | ||
2327 | 429 | elif segmentOnQueue.fin: | ||
2328 | 430 | sminput = tcpdfa.ACK | ||
2329 | 431 | if sminput is not None: | ||
2330 | 432 | # print 'ack input:', segmentOnQueue, packet, sminput | ||
2331 | 433 | generatedStateMachineInput = True | ||
2332 | 434 | self.input(sminput) | ||
2333 | 435 | else: | ||
2334 | 436 | break | ||
2335 | 437 | else: | ||
2336 | 438 | # write buffer is empty; alert the application layer. | ||
2337 | 439 | self._writeBufferEmpty() | ||
2338 | 440 | self.oldestUnackedSendSeqNum = SEG_ACK | ||
2339 | 441 | |||
2340 | 442 | if packet.syn: | ||
2341 | 443 | assert generatedStateMachineInput | ||
2342 | 444 | return | ||
2343 | 445 | |||
2344 | 446 | # XXX TODO: examine 'window' field and adjust sendWindowRemaining | ||
2345 | 447 | # is it 'occupying a portion of valid receive sequence space'? I think | ||
2346 | 448 | # this means 'packet which might acceptably contain useful data' | ||
2347 | 449 | if not packet.segmentLength(): | ||
2348 | 450 | assert packet.ack, "What the _HELL_ is wrong with this packet:" +str(packet) | ||
2349 | 451 | return | ||
2350 | 452 | |||
2351 | 453 | if not segmentAcceptable(self.nextRecvSeqNum, | ||
2352 | 454 | self.recvWindow, | ||
2353 | 455 | packet.relativeSeq(), | ||
2354 | 456 | packet.segmentLength()): | ||
2355 | 457 | # We have to transmit an ack here since it's old data. We probably | ||
2356 | 458 | # need to ack in more states than just ESTABLISHED... but which | ||
2357 | 459 | # ones? | ||
2358 | 460 | if not self.retransmissionQueue: | ||
2359 | 461 | self.originate(ack=True) | ||
2360 | 462 | return | ||
2361 | 463 | |||
2362 | 464 | # OK! It's acceptable! Let's process the various bits of data. | ||
2363 | 465 | # Where is the useful data in the packet? | ||
2364 | 466 | if packet.relativeSeq() > self.nextRecvSeqNum: | ||
2365 | 467 | # XXX: Here's what's going on. Data can be 'in the window', but | ||
2366 | 468 | # still in the future. For example, if I have a window of length 3 | ||
2367 | 469 | # and I send segments DATA1(len 1) DATA2(len 1) FIN and you receive | ||
2368 | 470 | # them in the order FIN DATA1 DATA2, you don't actually want to | ||
2369 | 471 | # process the FIN until you've processed the data. | ||
2370 | 472 | |||
2371 | 473 | # For the moment we are just dropping anything that isn't exactly | ||
2372 | 474 | # the next thing we want to process. This is perfectly valid; | ||
2373 | 475 | # these packets might have been dropped, so the other end will have | ||
2374 | 476 | # to retransmit them anyway. | ||
2375 | 477 | return | ||
2376 | 478 | |||
2377 | 479 | if packet.dlen: | ||
2378 | 480 | assert not packet.syn, 'no seriously I _do not_ know how to handle this' | ||
2379 | 481 | usefulData = packet.data[self.nextRecvSeqNum - packet.relativeSeq():] | ||
2380 | 482 | # DONT check/slice the window size here, the acceptability code | ||
2381 | 483 | # checked it, we can over-ack if the other side is buggy (???) | ||
2382 | 484 | if self.protocol is not None: | ||
2383 | 485 | try: | ||
2384 | 486 | self.protocol.dataReceived(usefulData) | ||
2385 | 487 | except: | ||
2386 | 488 | log.err() | ||
2387 | 489 | self.loseConnection() | ||
2388 | 490 | |||
2389 | 491 | self.nextRecvSeqNum += packet.segmentLength() | ||
2390 | 492 | if self.state == tcpdfa.ESTABLISHED: | ||
2391 | 493 | # In all other states, the state machine takes care of sending ACKs | ||
2392 | 494 | # in its output process. | ||
2393 | 495 | self.originate(ack=True) | ||
2394 | 496 | |||
2395 | 497 | if packet.fin: | ||
2396 | 498 | self.input(tcpdfa.FIN) | ||
2397 | 499 | |||
2398 | 500 | |||
2399 | 501 | def getHost(self): | ||
2400 | 502 | tupl = self.ptcp.transport.getHost() | ||
2401 | 503 | return PTCPAddress((tupl.host, tupl.port), | ||
2402 | 504 | self.pseudoPortPair) | ||
2403 | 505 | |||
2404 | 506 | def getPeer(self): | ||
2405 | 507 | return PTCPAddress(self.peerAddressTuple, | ||
2406 | 508 | self.pseudoPortPair) | ||
2407 | 509 | |||
2408 | 510 | _outgoingBytes = '' | ||
2409 | 511 | _nagle = None | ||
2410 | 512 | |||
2411 | 513 | def write(self, bytes): | ||
2412 | 514 | assert not self.disconnected, 'Writing to a transport that was already disconnected.' | ||
2413 | 515 | self._outgoingBytes += bytes | ||
2414 | 516 | self._writeLater() | ||
2415 | 517 | |||
2416 | 518 | |||
2417 | 519 | def writeSequence(self, seq): | ||
2418 | 520 | self.write(''.join(seq)) | ||
2419 | 521 | |||
2420 | 522 | |||
2421 | 523 | def _writeLater(self): | ||
2422 | 524 | if self._nagle is None: | ||
2423 | 525 | self._nagle = reactor.callLater(0.001, self._reallyWrite) | ||
2424 | 526 | |||
2425 | 527 | def _originateOneData(self): | ||
2426 | 528 | amount = min(self.sendWindowRemaining, self.mtu) | ||
2427 | 529 | sendOut = self._outgoingBytes[:amount] | ||
2428 | 530 | # print 'originating data packet', len(sendOut) | ||
2429 | 531 | self._outgoingBytes = self._outgoingBytes[amount:] | ||
2430 | 532 | self.sendWindowRemaining -= len(sendOut) | ||
2431 | 533 | self.originate(ack=True, data=sendOut) | ||
2432 | 534 | |||
2433 | 535 | def _reallyWrite(self): | ||
2434 | 536 | # print self, 'really writing', self._paused | ||
2435 | 537 | self._nagle = None | ||
2436 | 538 | if self._outgoingBytes: | ||
2437 | 539 | # print 'window and bytes', self.sendWindowRemaining, len(self._outgoingBytes) | ||
2438 | 540 | while self.sendWindowRemaining and self._outgoingBytes: | ||
2439 | 541 | self._originateOneData() | ||
2440 | 542 | |||
2441 | 543 | _retransmitter = None | ||
2442 | 544 | _retransmitTimeout = 0.5 | ||
2443 | 545 | |||
2444 | 546 | def _retransmitLater(self): | ||
2445 | 547 | assert self.state != tcpdfa.CLOSED | ||
2446 | 548 | if self._retransmitter is None: | ||
2447 | 549 | self._retransmitter = reactor.callLater(self._retransmitTimeout, self._reallyRetransmit) | ||
2448 | 550 | |||
2449 | 551 | def _stopRetransmitting(self): | ||
2450 | 552 | # used both as a quick-and-dirty test shutdown hack and a way to shut | ||
2451 | 553 | # down when we die... | ||
2452 | 554 | if self._retransmitter is not None: | ||
2453 | 555 | self._retransmitter.cancel() | ||
2454 | 556 | self._retransmitter = None | ||
2455 | 557 | if self._nagle is not None: | ||
2456 | 558 | self._nagle.cancel() | ||
2457 | 559 | self._nagle = None | ||
2458 | 560 | if self._closeWaitLoseConnection is not None: | ||
2459 | 561 | self._closeWaitLoseConnection.cancel() | ||
2460 | 562 | self._closeWaitLoseConnection = None | ||
2461 | 563 | |||
2462 | 564 | def _reallyRetransmit(self): | ||
2463 | 565 | # XXX TODO: packet fragmentation & coalescing. | ||
2464 | 566 | # print 'Wee a retransmit! What I got?', self.retransmissionQueue | ||
2465 | 567 | self._retransmitter = None | ||
2466 | 568 | if self.retransmissionQueue: | ||
2467 | 569 | for packet in self.retransmissionQueue: | ||
2468 | 570 | packet.retransmitCount -= 1 | ||
2469 | 571 | if packet.retransmitCount: | ||
2470 | 572 | packet.ackNum = self.currentAckNum() | ||
2471 | 573 | self.ptcp.sendPacket(packet) | ||
2472 | 574 | else: | ||
2473 | 575 | self.input(tcpdfa.TIMEOUT) | ||
2474 | 576 | return | ||
2475 | 577 | self._retransmitLater() | ||
2476 | 578 | |||
2477 | 579 | disconnecting = False # This is *TWISTED* level state-machine stuff, | ||
2478 | 580 | # not TCP-level. | ||
2479 | 581 | |||
2480 | 582 | def loseConnection(self): | ||
2481 | 583 | if not self.disconnecting: | ||
2482 | 584 | self.disconnecting = True | ||
2483 | 585 | if not self._outgoingBytes: | ||
2484 | 586 | self._writeBufferEmpty() | ||
2485 | 587 | |||
2486 | 588 | |||
2487 | 589 | def _writeBufferEmpty(self): | ||
2488 | 590 | if self._outgoingBytes: | ||
2489 | 591 | self._reallyWrite() | ||
2490 | 592 | elif self.producer is not None: | ||
2491 | 593 | if (not self.streamingProducer) or self.producerPaused: | ||
2492 | 594 | self.producerPaused = False | ||
2493 | 595 | self.producer.resumeProducing() | ||
2494 | 596 | elif self.disconnecting and (not self.disconnected | ||
2495 | 597 | or self.state == tcpdfa.CLOSE_WAIT): | ||
2496 | 598 | self.input(tcpdfa.APP_CLOSE) | ||
2497 | 599 | |||
2498 | 600 | |||
2499 | 601 | def _writeBufferFull(self): | ||
2500 | 602 | # print 'my write buffer is full' | ||
2501 | 603 | if (self.producer is not None | ||
2502 | 604 | and not self.producerPaused): | ||
2503 | 605 | self.producerPaused = True | ||
2504 | 606 | # print 'producer pausing' | ||
2505 | 607 | self.producer.pauseProducing() | ||
2506 | 608 | # print 'producer paused' | ||
2507 | 609 | else: | ||
2508 | 610 | # print 'but I am not telling my producer to pause!' | ||
2509 | 611 | # print ' ', self.producer, self.streamingProducer, self.producerPaused | ||
2510 | 612 | pass | ||
2511 | 613 | |||
2512 | 614 | |||
2513 | 615 | disconnected = False | ||
2514 | 616 | producer = None | ||
2515 | 617 | producerPaused = False | ||
2516 | 618 | streamingProducer = False | ||
2517 | 619 | |||
2518 | 620 | def registerProducer(self, producer, streaming): | ||
2519 | 621 | if self.producer is not None: | ||
2520 | 622 | raise RuntimeError( | ||
2521 | 623 | "Cannot register producer %s, " | ||
2522 | 624 | "because producer %s was never unregistered." | ||
2523 | 625 | % (producer, self.producer)) | ||
2524 | 626 | if self.disconnected: | ||
2525 | 627 | producer.stopProducing() | ||
2526 | 628 | else: | ||
2527 | 629 | self.producer = producer | ||
2528 | 630 | self.streamingProducer = streaming | ||
2529 | 631 | if not streaming and not self._outgoingBytes: | ||
2530 | 632 | producer.resumeProducing() | ||
2531 | 633 | |||
2532 | 634 | def unregisterProducer(self): | ||
2533 | 635 | self.producer = None | ||
2534 | 636 | if not self._outgoingBytes: | ||
2535 | 637 | self._writeBufferEmpty() | ||
2536 | 638 | |||
2537 | 639 | _paused = False | ||
2538 | 640 | def pauseProducing(self): | ||
2539 | 641 | self._paused = True | ||
2540 | 642 | |||
2541 | 643 | def resumeProducing(self): | ||
2542 | 644 | self._paused = False | ||
2543 | 645 | |||
2544 | 646 | def currentAckNum(self): | ||
2545 | 647 | return (self.nextRecvSeqNum + self.peerSendISN) % (2**32) | ||
2546 | 648 | |||
2547 | 649 | def originate(self, data='', syn=False, ack=False, fin=False): | ||
2548 | 650 | if syn: | ||
2549 | 651 | # We really should be randomizing the ISN but until we finish the | ||
2550 | 652 | # implementations of the various bits of wraparound logic that were | ||
2551 | 653 | # started with relativeSequence | ||
2552 | 654 | assert self.nextSendSeqNum == 0 | ||
2553 | 655 | assert self.hostSendISN == 0 | ||
2554 | 656 | p = PTCPPacket.create(self.hostPseudoPort, | ||
2555 | 657 | self.peerPseudoPort, | ||
2556 | 658 | seqNum=(self.nextSendSeqNum + self.hostSendISN) % (2**32), | ||
2557 | 659 | ackNum=self.currentAckNum(), | ||
2558 | 660 | data=data, | ||
2559 | 661 | window=self.recvWindow, | ||
2560 | 662 | syn=syn, ack=ack, fin=fin, | ||
2561 | 663 | destination=self.peerAddressTuple) | ||
2562 | 664 | # do we want to enqueue this packet for retransmission? | ||
2563 | 665 | sl = p.segmentLength() | ||
2564 | 666 | self.nextSendSeqNum += sl | ||
2565 | 667 | |||
2566 | 668 | if p.mustRetransmit(): | ||
2567 | 669 | # print self, 'originating retransmittable packet', len(self.retransmissionQueue) | ||
2568 | 670 | if self.retransmissionQueue: | ||
2569 | 671 | if self.retransmissionQueue[-1].fin: | ||
2570 | 672 | raise AssertionError("Sending %r after FIN??!" % (p,)) | ||
2571 | 673 | # print 'putting it on the queue' | ||
2572 | 674 | self.retransmissionQueue.append(p) | ||
2573 | 675 | # print 'and sending it later' | ||
2574 | 676 | self._retransmitLater() | ||
2575 | 677 | if not self.sendWindowRemaining: # len(self.retransmissionQueue) > 5: | ||
2576 | 678 | # print 'oh no my queue is too big' | ||
2577 | 679 | # This is a random number (5) because I ought to be summing the | ||
2578 | 680 | # packet lengths or something. | ||
2579 | 681 | self._writeBufferFull() | ||
2580 | 682 | else: | ||
2581 | 683 | # print 'my queue is still small enough', len(self.retransmissionQueue), self, self.sendWindowRemaining | ||
2582 | 684 | pass | ||
2583 | 685 | self.ptcp.sendPacket(p) | ||
2584 | 686 | |||
2585 | 687 | # State machine transition definitions, hooray. | ||
2586 | 688 | def transition_SYN_SENT_to_CLOSED(self): | ||
2587 | 689 | """ | ||
2588 | 690 | The connection never got anywhere. Goodbye. | ||
2589 | 691 | """ | ||
2590 | 692 | # XXX CONNECTOR API OMFG | ||
2591 | 693 | self.factory.clientConnectionFailed(None, error.TimeoutError()) | ||
2592 | 694 | |||
2593 | 695 | |||
2594 | 696 | wasEverListen = False | ||
2595 | 697 | |||
2596 | 698 | def enter_LISTEN(self): | ||
2597 | 699 | # Spec says this is necessary for RST handling; we need it for making | ||
2598 | 700 | # sure it's OK to bind port numbers. | ||
2599 | 701 | self.wasEverListen = True | ||
2600 | 702 | |||
2601 | 703 | def enter_CLOSED(self): | ||
2602 | 704 | self.ptcp.connectionClosed(self) | ||
2603 | 705 | self._stopRetransmitting() | ||
2604 | 706 | if self._timeWaitCall is not None: | ||
2605 | 707 | self._timeWaitCall.cancel() | ||
2606 | 708 | self._timeWaitCall = None | ||
2607 | 709 | |||
2608 | 710 | _timeWaitCall = None | ||
2609 | 711 | _timeWaitTimeout = 0.01 # REALLY fast timeout, right now this is for | ||
2610 | 712 | # the tests... | ||
2611 | 713 | |||
2612 | 714 | def enter_TIME_WAIT(self): | ||
2613 | 715 | self._stopRetransmitting() | ||
2614 | 716 | self._timeWaitCall = reactor.callLater(self._timeWaitTimeout, self._do2mslTimeout) | ||
2615 | 717 | |||
2616 | 718 | def _do2mslTimeout(self): | ||
2617 | 719 | self._timeWaitCall = None | ||
2618 | 720 | self.input(tcpdfa.TIMEOUT) | ||
2619 | 721 | |||
2620 | 722 | peerAddressTuple = None | ||
2621 | 723 | |||
2622 | 724 | def transition_LISTEN_to_SYN_SENT(self): | ||
2623 | 725 | """ | ||
2624 | 726 | Uh, what? We were listening and we tried to send some bytes. | ||
2625 | 727 | This is an error for PTCP. | ||
2626 | 728 | """ | ||
2627 | 729 | raise StateError("You can't write anything until someone connects to you.") | ||
2628 | 730 | |||
2629 | 731 | # def invalidInput(self, datum): | ||
2630 | 732 | # print self, self.protocol, 'invalid input', datum | ||
2631 | 733 | |||
2632 | 734 | def pseudoPortPair(): | ||
2633 | 735 | def get(self): | ||
2634 | 736 | return (self.hostPseudoPort, | ||
2635 | 737 | self.peerPseudoPort) | ||
2636 | 738 | return get, | ||
2637 | 739 | pseudoPortPair = property(*pseudoPortPair()) | ||
2638 | 740 | |||
2639 | 741 | def enter_ESTABLISHED(self): | ||
2640 | 742 | """ | ||
2641 | 743 | We sent out SYN, they acknowledged it. Congratulations, you | ||
2642 | 744 | have a new baby connection. | ||
2643 | 745 | """ | ||
2644 | 746 | assert not self.disconnecting | ||
2645 | 747 | assert not self.disconnected | ||
2646 | 748 | try: | ||
2647 | 749 | p = self.factory.buildProtocol(PTCPAddress( | ||
2648 | 750 | self.peerAddressTuple, self.pseudoPortPair)) | ||
2649 | 751 | p.makeConnection(self) | ||
2650 | 752 | except: | ||
2651 | 753 | log.msg("Exception during PTCP connection setup.") | ||
2652 | 754 | log.err() | ||
2653 | 755 | self.loseConnection() | ||
2654 | 756 | else: | ||
2655 | 757 | self.protocol = p | ||
2656 | 758 | |||
2657 | 759 | def exit_ESTABLISHED(self): | ||
2658 | 760 | assert not self.disconnected | ||
2659 | 761 | self.disconnected = True | ||
2660 | 762 | try: | ||
2661 | 763 | self.protocol.connectionLost(Failure(CONNECTION_DONE)) | ||
2662 | 764 | except: | ||
2663 | 765 | log.err() | ||
2664 | 766 | self.protocol = None | ||
2665 | 767 | |||
2666 | 768 | if self.producer is not None: | ||
2667 | 769 | try: | ||
2668 | 770 | self.producer.stopProducing() | ||
2669 | 771 | except: | ||
2670 | 772 | log.err() | ||
2671 | 773 | self.producer = None | ||
2672 | 774 | |||
2673 | 775 | |||
2674 | 776 | _closeWaitLoseConnection = None | ||
2675 | 777 | |||
2676 | 778 | def enter_CLOSE_WAIT(self): | ||
2677 | 779 | # Twisted automatically reacts to network half-close by issuing a full | ||
2678 | 780 | # close. | ||
2679 | 781 | self._closeWaitLoseConnection = reactor.callLater(0.01, self._loseConnectionBecauseOfCloseWait) | ||
2680 | 782 | |||
2681 | 783 | def _loseConnectionBecauseOfCloseWait(self): | ||
2682 | 784 | self._closeWaitLoseConnection = None | ||
2683 | 785 | self.loseConnection() | ||
2684 | 786 | |||
2685 | 787 | def immediateShutdown(self): | ||
2686 | 788 | """_IMMEDIATELY_ shut down this connection, sending one (non-retransmitted) | ||
2687 | 789 | app-close packet, emptying our buffers, clearing our producer and | ||
2688 | 790 | getting ready to die right after this call. | ||
2689 | 791 | """ | ||
2690 | 792 | self._outgoingBytes = '' | ||
2691 | 793 | if self.state == tcpdfa.ESTABLISHED: | ||
2692 | 794 | self.input(tcpdfa.APP_CLOSE) | ||
2693 | 795 | self._stopRetransmitting() | ||
2694 | 796 | self._reallyRetransmit() | ||
2695 | 797 | |||
2696 | 798 | # All states that we can reasonably be in handle a timeout; force our | ||
2697 | 799 | # connection to think that it's become desynchronized with the other | ||
2698 | 800 | # end so that it will totally shut itself down. | ||
2699 | 801 | |||
2700 | 802 | self.input(tcpdfa.TIMEOUT) | ||
2701 | 803 | assert self._retransmitter is None | ||
2702 | 804 | assert self._nagle is None | ||
2703 | 805 | |||
2704 | 806 | def output_ACK(self): | ||
2705 | 807 | self.originate(ack=True) | ||
2706 | 808 | |||
2707 | 809 | def output_FIN(self): | ||
2708 | 810 | self.originate(fin=True) | ||
2709 | 811 | |||
2710 | 812 | def output_SYN_ACK(self): | ||
2711 | 813 | self.originate(syn=True, ack=True) | ||
2712 | 814 | |||
2713 | 815 | def output_SYN(self): | ||
2714 | 816 | self.originate(syn=True) | ||
2715 | 817 | |||
2716 | 818 | class PTCPAddress(object): | ||
2717 | 819 | # garbage | ||
2718 | 820 | |||
2719 | 821 | def __init__(self, (host, port), (pseudoHostPort, pseudoPeerPort)): | ||
2720 | 822 | self.host = host | ||
2721 | 823 | self.port = port | ||
2722 | 824 | self.pseudoHostPort = pseudoHostPort | ||
2723 | 825 | self.pseudoPeerPort = pseudoPeerPort | ||
2724 | 826 | |||
2725 | 827 | def __repr__(self): | ||
2726 | 828 | return 'PTCPAddress((%r, %r), (%r, %r))' % ( | ||
2727 | 829 | self.host, self.port, | ||
2728 | 830 | self.pseudoHostPort, | ||
2729 | 831 | self.pseudoPeerPort) | ||
2730 | 832 | |||
2731 | 833 | |||
2732 | 834 | |||
2733 | 835 | class _PendingEvent(object): | ||
2734 | 836 | def __init__(self): | ||
2735 | 837 | self.listeners = [] | ||
2736 | 838 | |||
2737 | 839 | |||
2738 | 840 | def deferred(self): | ||
2739 | 841 | d = Deferred() | ||
2740 | 842 | self.listeners.append(d) | ||
2741 | 843 | return d | ||
2742 | 844 | |||
2743 | 845 | |||
2744 | 846 | def callback(self, result): | ||
2745 | 847 | l = self.listeners | ||
2746 | 848 | self.listeners = [] | ||
2747 | 849 | for d in l: | ||
2748 | 850 | d.callback(result) | ||
2749 | 851 | |||
2750 | 852 | |||
2751 | 853 | def errback(self, result=None): | ||
2752 | 854 | if result is None: | ||
2753 | 855 | result = Failure() | ||
2754 | 856 | l = self.listeners | ||
2755 | 857 | self.listeners = [] | ||
2756 | 858 | for d in l: | ||
2757 | 859 | d.errback(result) | ||
2758 | 860 | |||
2759 | 861 | |||
2760 | 862 | |||
2761 | 863 | class PTCP(protocol.DatagramProtocol): | ||
2762 | 864 | """ | ||
2763 | 865 | L{PTCP} implements a strongly TCP-like protocol on top of UDP. It | ||
2764 | 866 | provides a transport which is connection-oriented, streaming, | ||
2765 | 867 | ordered, and reliable. | ||
2766 | 868 | |||
2767 | 869 | @ivar factory: A L{ServerFactory} which is used to create | ||
2768 | 870 | L{IProtocol} providers whenever a new PTCP connection is made | ||
2769 | 871 | to this port. | ||
2770 | 872 | |||
2771 | 873 | @ivar _connections: A mapping of endpoint addresses to connection | ||
2772 | 874 | objects. These are the active connections being multiplexed | ||
2773 | 875 | over this UDP port. Many PTCP connections may run over the | ||
2774 | 876 | same L{PTCP} instance, communicating with many different | ||
2775 | 877 | remote hosts as well as multiplexing different PTCP | ||
2776 | 878 | connections to the same remote host. The mapping keys, | ||
2777 | 879 | endpoint addresses, are three-tuples of: | ||
2778 | 880 | |||
2779 | 881 | - The destination pseudo-port which is always C{1} | ||
2780 | 882 | - The source pseudo-port | ||
2781 | 883 | - A (host, port) tuple giving the UDP address of a PTCP | ||
2782 | 884 | peer holding the other side of the connection | ||
2783 | 885 | |||
2784 | 886 | The mapping values, connection objects, are L{PTCPConnection} | ||
2785 | 887 | instances. | ||
2786 | 888 | @type _connections: C{dict} | ||
2787 | 889 | |||
2788 | 890 | """ | ||
2789 | 891 | # External API | ||
2790 | 892 | |||
2791 | 893 | def __init__(self, factory): | ||
2792 | 894 | self.factory = factory | ||
2793 | 895 | self._allConnectionsClosed = _PendingEvent() | ||
2794 | 896 | |||
2795 | 897 | |||
2796 | 898 | def connect(self, factory, host, port, pseudoPort=1): | ||
2797 | 899 | """ | ||
2798 | 900 | Attempt to establish a new connection via PTCP to the given | ||
2799 | 901 | remote address. | ||
2800 | 902 | |||
2801 | 903 | @param factory: A L{ClientFactory} which will be used to | ||
2802 | 904 | create an L{IProtocol} provider if the connection is | ||
2803 | 905 | successfully set up, or which will have failure callbacks | ||
2804 | 906 | invoked on it otherwise. | ||
2805 | 907 | |||
2806 | 908 | @param host: The IP address of another listening PTCP port to | ||
2807 | 909 | connect to. | ||
2808 | 910 | @type host: C{str} | ||
2809 | 911 | |||
2810 | 912 | @param port: The port number of that other listening PTCP port | ||
2811 | 913 | to connect to. | ||
2812 | 914 | @type port: C{int} | ||
2813 | 915 | |||
2814 | 916 | @param pseudoPort: Not really implemented. Do not pass a | ||
2815 | 917 | value for this parameter or things will break. | ||
2816 | 918 | |||
2817 | 919 | @return: A L{PTCPConnection} instance representing the new | ||
2818 | 920 | connection, but you really shouldn't use this for | ||
2819 | 921 | anything. Write a protocol! | ||
2820 | 922 | """ | ||
2821 | 923 | sourcePseudoPort = genConnID() % MAX_PSEUDO_PORT | ||
2822 | 924 | conn = self._connections[(pseudoPort, sourcePseudoPort, (host, port)) | ||
2823 | 925 | ] = PTCPConnection( | ||
2824 | 926 | sourcePseudoPort, pseudoPort, self, factory, (host, port)) | ||
2825 | 927 | conn.input(tcpdfa.APP_ACTIVE_OPEN) | ||
2826 | 928 | return conn | ||
2827 | 929 | |||
2828 | 930 | def sendPacket(self, packet): | ||
2829 | 931 | if self.transportGoneAway: | ||
2830 | 932 | return | ||
2831 | 933 | self.transport.write(packet.encode(), packet.destination) | ||
2832 | 934 | |||
2833 | 935 | |||
2834 | 936 | # Internal stuff | ||
2835 | 937 | def startProtocol(self): | ||
2836 | 938 | self.transportGoneAway = False | ||
2837 | 939 | self._lastConnID = 10 # random.randrange(2 ** 32) | ||
2838 | 940 | self._connections = {} | ||
2839 | 941 | |||
2840 | 942 | def _finalCleanup(self): | ||
2841 | 943 | """ | ||
2842 | 944 | Clean up all of our connections by issuing application-level close and | ||
2843 | 945 | stop notifications, sending hail-mary final FIN packets (which may not | ||
2844 | 946 | reach the other end, but nevertheless can be useful) when possible. | ||
2845 | 947 | """ | ||
2846 | 948 | for conn in self._connections.values(): | ||
2847 | 949 | conn.immediateShutdown() | ||
2848 | 950 | assert not self._connections | ||
2849 | 951 | |||
2850 | 952 | def stopProtocol(self): | ||
2851 | 953 | """ | ||
2852 | 954 | Notification from twisted that our underlying port has gone away; | ||
2853 | 955 | make sure we're not going to try to send any packets through our | ||
2854 | 956 | transport and blow up, then shut down all of our protocols, issuing | ||
2855 | 957 | appr | ||
2856 | 958 | opriate application-level messages. | ||
2857 | 959 | """ | ||
2858 | 960 | self.transportGoneAway = True | ||
2859 | 961 | self._finalCleanup() | ||
2860 | 962 | |||
2861 | 963 | def cleanupAndClose(self): | ||
2862 | 964 | """ | ||
2863 | 965 | Clean up all remaining connections, then close our transport. | ||
2864 | 966 | |||
2865 | 967 | Although in a pinch we will do cleanup after our socket has gone away | ||
2866 | 968 | (if it does so unexpectedly, above in stopProtocol), we would really | ||
2867 | 969 | prefer to do cleanup while we still have access to a transport, since | ||
2868 | 970 | that way we can force out a few final packets and save the remote | ||
2869 | 971 | application an awkward timeout (if it happens to get through, which | ||
2870 | 972 | is generally likely). | ||
2871 | 973 | """ | ||
2872 | 974 | self._finalCleanup() | ||
2873 | 975 | return self._stop() | ||
2874 | 976 | |||
2875 | 977 | def datagramReceived(self, bytes, addr): | ||
2876 | 978 | if len(bytes) < _fixedSize: | ||
2877 | 979 | # It can't be any good. | ||
2878 | 980 | return | ||
2879 | 981 | |||
2880 | 982 | pkt = PTCPPacket.decode(bytes, addr) | ||
2881 | 983 | try: | ||
2882 | 984 | pkt.verifyChecksum() | ||
2883 | 985 | except TruncatedDataError: | ||
2884 | 986 | # print '(ptcp packet truncated: %r)' % (pkt,) | ||
2885 | 987 | self.sendPacket( | ||
2886 | 988 | PTCPPacket.create( | ||
2887 | 989 | pkt.destPseudoPort, | ||
2888 | 990 | pkt.sourcePseudoPort, | ||
2889 | 991 | 0, | ||
2890 | 992 | 0, | ||
2891 | 993 | struct.pack('!H', len(pkt.data)), | ||
2892 | 994 | stb=True, | ||
2893 | 995 | destination=addr)) | ||
2894 | 996 | except GarbageDataError: | ||
2895 | 997 | print "garbage data!", pkt | ||
2896 | 998 | except ChecksumMismatchError, cme: | ||
2897 | 999 | print "bad checksum", pkt, cme | ||
2898 | 1000 | print repr(pkt.data) | ||
2899 | 1001 | print hex(pkt.checksum), hex(pkt.computeChecksum()) | ||
2900 | 1002 | else: | ||
2901 | 1003 | self.packetReceived(pkt) | ||
2902 | 1004 | |||
2903 | 1005 | stopped = False | ||
2904 | 1006 | def _stop(self, result=None): | ||
2905 | 1007 | if not self.stopped: | ||
2906 | 1008 | self.stopped = True | ||
2907 | 1009 | return self.transport.stopListening() | ||
2908 | 1010 | else: | ||
2909 | 1011 | return defer.succeed(None) | ||
2910 | 1012 | |||
2911 | 1013 | def waitForAllConnectionsToClose(self): | ||
2912 | 1014 | """ | ||
2913 | 1015 | Wait for all currently-open connections to enter the 'CLOSED' state. | ||
2914 | 1016 | Currently this is only usable from test fixtures. | ||
2915 | 1017 | """ | ||
2916 | 1018 | if not self._connections: | ||
2917 | 1019 | return self._stop() | ||
2918 | 1020 | return self._allConnectionsClosed.deferred().addBoth(self._stop) | ||
2919 | 1021 | |||
2920 | 1022 | def connectionClosed(self, ptcpConn): | ||
2921 | 1023 | packey = (ptcpConn.peerPseudoPort, ptcpConn.hostPseudoPort, | ||
2922 | 1024 | ptcpConn.peerAddressTuple) | ||
2923 | 1025 | del self._connections[packey] | ||
2924 | 1026 | if ((not self.transportGoneAway) and | ||
2925 | 1027 | (not self._connections) and | ||
2926 | 1028 | self.factory is None): | ||
2927 | 1029 | self._stop() | ||
2928 | 1030 | if not self._connections: | ||
2929 | 1031 | self._allConnectionsClosed.callback(None) | ||
2930 | 1032 | |||
2931 | 1033 | def packetReceived(self, packet): | ||
2932 | 1034 | packey = (packet.sourcePseudoPort, packet.destPseudoPort, packet.peerAddressTuple) | ||
2933 | 1035 | if packey not in self._connections: | ||
2934 | 1036 | if packet.flags == _SYN and packet.destPseudoPort == 1: # SYN and _ONLY_ SYN set. | ||
2935 | 1037 | conn = PTCPConnection(packet.destPseudoPort, | ||
2936 | 1038 | packet.sourcePseudoPort, self, | ||
2937 | 1039 | self.factory, packet.peerAddressTuple) | ||
2938 | 1040 | conn.input(tcpdfa.APP_PASSIVE_OPEN) | ||
2939 | 1041 | self._connections[packey] = conn | ||
2940 | 1042 | else: | ||
2941 | 1043 | log.msg("corrupted packet? %r %r %r" % (packet,packey, self._connections)) | ||
2942 | 1044 | return | ||
2943 | 1045 | try: | ||
2944 | 1046 | self._connections[packey].packetReceived(packet) | ||
2945 | 1047 | except: | ||
2946 | 1048 | log.msg("PTCPConnection error on %r:" % (packet,)) | ||
2947 | 1049 | log.err() | ||
2948 | 1050 | del self._connections[packey] | ||
2949 | 1051 | 0 | ||
2950 | === removed file 'Vertex/vertex/q2q.py' | |||
2951 | --- Vertex/vertex/q2q.py 2012-03-14 23:42:53 +0000 | |||
2952 | +++ Vertex/vertex/q2q.py 1970-01-01 00:00:00 +0000 | |||
2953 | @@ -1,2763 +0,0 @@ | |||
2954 | 1 | # -*- test-case-name: vertex.test.test_q2q -*- | ||
2955 | 2 | # Copyright 2005-2008 Divmod, Inc. See LICENSE file for details | ||
2956 | 3 | |||
2957 | 4 | """ | ||
2958 | 5 | I{Quotient to Quotient} protocol implementation. | ||
2959 | 6 | """ | ||
2960 | 7 | |||
2961 | 8 | # stdlib | ||
2962 | 9 | import itertools | ||
2963 | 10 | from hashlib import md5 | ||
2964 | 11 | import struct | ||
2965 | 12 | import datetime | ||
2966 | 13 | import time | ||
2967 | 14 | from collections import namedtuple | ||
2968 | 15 | |||
2969 | 16 | from pprint import pformat | ||
2970 | 17 | |||
2971 | 18 | from zope.interface import implements | ||
2972 | 19 | |||
2973 | 20 | # twisted | ||
2974 | 21 | from twisted.internet import reactor, defer, interfaces, protocol, error | ||
2975 | 22 | from twisted.internet.main import CONNECTION_DONE | ||
2976 | 23 | from twisted.internet.ssl import ( | ||
2977 | 24 | CertificateRequest, Certificate, PrivateCertificate, KeyPair, | ||
2978 | 25 | DistinguishedName) | ||
2979 | 26 | from twisted.python import log | ||
2980 | 27 | from twisted.python.failure import Failure | ||
2981 | 28 | from twisted.application import service | ||
2982 | 29 | |||
2983 | 30 | # twisted.cred | ||
2984 | 31 | from twisted.cred.checkers import ICredentialsChecker | ||
2985 | 32 | from twisted.cred.portal import IRealm, Portal | ||
2986 | 33 | from twisted.cred.credentials import IUsernamePassword, UsernamePassword | ||
2987 | 34 | from twisted.cred.error import UnauthorizedLogin | ||
2988 | 35 | |||
2989 | 36 | from twisted.protocols.amp import Argument, Boolean, Integer, String, Unicode, ListOf, AmpList | ||
2990 | 37 | from twisted.protocols.amp import AmpBox, Command, StartTLS, ProtocolSwitchCommand, AMP | ||
2991 | 38 | from twisted.protocols.amp import _objectsToStrings | ||
2992 | 39 | |||
2993 | 40 | # vertex | ||
2994 | 41 | from vertex import subproducer, ptcp | ||
2995 | 42 | from vertex import endpoint, ivertex | ||
2996 | 43 | from vertex.conncache import ConnectionCache | ||
2997 | 44 | |||
2998 | 45 | MESSAGE_PROTOCOL = 'q2q-message' | ||
2999 | 46 | port = 8788 | ||
3000 | 47 | |||
3001 | 48 | class ConnectionError(Exception): | ||
3002 | 49 | pass | ||
3003 | 50 | |||
3004 | 51 | class AttemptsFailed(ConnectionError): | ||
3005 | 52 | pass | ||
3006 | 53 | |||
3007 | 54 | class NoAttemptsMade(ConnectionError): | ||
3008 | 55 | pass | ||
3009 | 56 | |||
3010 | 57 | class VerifyError(Exception): | ||
3011 | 58 | pass | ||
3012 | 59 | |||
3013 | 60 | class BadCertificateRequest(VerifyError): | ||
3014 | 61 | pass | ||
3015 | 62 | |||
3016 | 63 | class IgnoreConnectionFailed(protocol.ClientFactory): | ||
3017 | 64 | def __init__(self, realFactory): | ||
3018 | 65 | self.realFactory = realFactory | ||
3019 | 66 | |||
3020 | 67 | def clientConnectionLost(self, connector, reason): | ||
3021 | 68 | self.realFactory.clientConnectionLost(connector, reason) | ||
3022 | 69 | |||
3023 | 70 | def clientConnectionFailed(self, connector, reason): | ||
3024 | 71 | pass | ||
3025 | 72 | |||
3026 | 73 | def buildProtocol(self, addr): | ||
3027 | 74 | return self.realFactory.buildProtocol(addr) | ||
3028 | 75 | |||
3029 | 76 | class Q2QAddress(object): | ||
3030 | 77 | def __init__(self, domain, resource=None): | ||
3031 | 78 | self.resource = resource | ||
3032 | 79 | self.domain = domain | ||
3033 | 80 | |||
3034 | 81 | def domainAddress(self): | ||
3035 | 82 | """ Return an Address object which is the same as this one with ONLY the | ||
3036 | 83 | 'domain' attribute set, not 'resource'. | ||
3037 | 84 | |||
3038 | 85 | May return 'self' if 'resource' is already None. | ||
3039 | 86 | """ | ||
3040 | 87 | if self.resource is None: | ||
3041 | 88 | return self | ||
3042 | 89 | else: | ||
3043 | 90 | return Q2QAddress(self.domain) | ||
3044 | 91 | |||
3045 | 92 | def claimedAsIssuerOf(self, cert): | ||
3046 | 93 | """ | ||
3047 | 94 | Check if the information in a provided certificate *CLAIMS* to be issued by | ||
3048 | 95 | this address. | ||
3049 | 96 | |||
3050 | 97 | PLEASE NOTE THAT THIS METHOD IS IN NO WAY AUTHORITATIVE. It does not | ||
3051 | 98 | perform any cryptographic checks. | ||
3052 | 99 | |||
3053 | 100 | Currently this check is if L{Q2QAddress.__str__}C{(self)} is equivalent | ||
3054 | 101 | to the commonName on the certificate's issuer. | ||
3055 | 102 | """ | ||
3056 | 103 | return cert.getIssuer().commonName == str(self) | ||
3057 | 104 | |||
3058 | 105 | def claimedAsSubjectOf(self, cert): | ||
3059 | 106 | """ | ||
3060 | 107 | Check if the information in a provided certificate *CLAIMS* to be | ||
3061 | 108 | provided for use by this address. | ||
3062 | 109 | |||
3063 | 110 | PLEASE NOTE THAT THIS METHOD IS IN NO WAY AUTHORITATIVE. It does not | ||
3064 | 111 | perform any cryptographic checks. | ||
3065 | 112 | |||
3066 | 113 | Currently this check is if L{Q2QAddress.__str__}C{(self)} is equivalent | ||
3067 | 114 | to the commonName on the certificate's subject. | ||
3068 | 115 | """ | ||
3069 | 116 | return cert.getSubject().commonName == str(self) | ||
3070 | 117 | |||
3071 | 118 | def __cmp__(self, other): | ||
3072 | 119 | if not isinstance(other, Q2QAddress): | ||
3073 | 120 | return cmp(self.__class__, other.__class__) | ||
3074 | 121 | return cmp((self.domain, self.resource), (other.domain, other.resource)) | ||
3075 | 122 | |||
3076 | 123 | def __iter__(self): | ||
3077 | 124 | return iter((self.resource, self.domain)) | ||
3078 | 125 | |||
3079 | 126 | def __str__(self): | ||
3080 | 127 | """ | ||
3081 | 128 | Return a string of the normalized form of this address. e.g.:: | ||
3082 | 129 | |||
3083 | 130 | glyph@divmod.com # for a user | ||
3084 | 131 | divmod.com # for a domain | ||
3085 | 132 | """ | ||
3086 | 133 | if self.resource: | ||
3087 | 134 | resource = self.resource + '@' | ||
3088 | 135 | else: | ||
3089 | 136 | resource = '' | ||
3090 | 137 | return (resource + self.domain).encode('utf-8') | ||
3091 | 138 | |||
3092 | 139 | def __repr__(self): | ||
3093 | 140 | return '<Q2Q at %s>' % self.__str__() | ||
3094 | 141 | |||
3095 | 142 | def __hash__(self): | ||
3096 | 143 | return hash(str(self)) | ||
3097 | 144 | |||
3098 | 145 | def fromString(cls, string): | ||
3099 | 146 | args = string.split("@",1) | ||
3100 | 147 | args.reverse() | ||
3101 | 148 | return cls(*args) | ||
3102 | 149 | fromString = classmethod(fromString) | ||
3103 | 150 | |||
3104 | 151 | |||
3105 | 152 | class VirtualTransportAddress: | ||
3106 | 153 | def __init__(self, underlying): | ||
3107 | 154 | self.underlying = underlying | ||
3108 | 155 | |||
3109 | 156 | def __repr__(self): | ||
3110 | 157 | return 'VirtualTransportAddress(%r)' % (self.underlying,) | ||
3111 | 158 | |||
3112 | 159 | class Q2QTransportAddress: | ||
3113 | 160 | """ | ||
3114 | 161 | The return value of getPeer() and getHost() for Q2Q-enabled transports. | ||
3115 | 162 | Passed to buildProtocol of factories passed to listenQ2Q. | ||
3116 | 163 | |||
3117 | 164 | @ivar underlying: The return value of the underlying transport's getPeer() | ||
3118 | 165 | or getHost(); an address which indicates the path which the bytes carrying | ||
3119 | 166 | Q2Q traffic are travelling over. It is tempting to think of this as a | ||
3120 | 167 | 'physical' layer but that it not necessarily accurate; there are | ||
3121 | 168 | potentially multiple layers of wrapping on any Q2Q transport, as an SSL | ||
3122 | 169 | transport may be tunnelled over a UDP NAT-traversal layer. Implements | ||
3123 | 170 | C{IAddress} from Twisted, for all the good that will do you. | ||
3124 | 171 | |||
3125 | 172 | @ivar logical: a L{Q2QAddress}, The logical peer; the user ostensibly | ||
3126 | 173 | listening to data on the other end of this transport. | ||
3127 | 174 | |||
3128 | 175 | @ivar protocol: a L{str}, the name of the protocol that is connected. | ||
3129 | 176 | """ | ||
3130 | 177 | |||
3131 | 178 | def __init__(self, underlying, logical, protocol): | ||
3132 | 179 | self.underlying = underlying | ||
3133 | 180 | self.logical = logical | ||
3134 | 181 | self.protocol = protocol | ||
3135 | 182 | |||
3136 | 183 | def __repr__(self): | ||
3137 | 184 | return 'Q2QTransportAddress(%r, %r, %r)' % ( | ||
3138 | 185 | self.underlying, | ||
3139 | 186 | self.logical, | ||
3140 | 187 | self.protocol) | ||
3141 | 188 | |||
3142 | 189 | |||
3143 | 190 | class AmpTime(Argument): | ||
3144 | 191 | def toString(self, inObject): | ||
3145 | 192 | return inObject.strftime("%Y-%m-%dT%H:%M:%S") | ||
3146 | 193 | |||
3147 | 194 | |||
3148 | 195 | def fromString(self, inString): | ||
3149 | 196 | return datetime.datetime.strptime(inString, "%Y-%m-%dT%H:%M:%S") | ||
3150 | 197 | |||
3151 | 198 | |||
3152 | 199 | |||
3153 | 200 | class Q2QAddressArgument(Argument): | ||
3154 | 201 | fromString = Q2QAddress.fromString | ||
3155 | 202 | toString = Q2QAddress.__str__ | ||
3156 | 203 | |||
3157 | 204 | class HostPort(Argument): | ||
3158 | 205 | def toString(self, inObj): | ||
3159 | 206 | return "%s:%d" % tuple(inObj) | ||
3160 | 207 | |||
3161 | 208 | def fromString(self, inStr): | ||
3162 | 209 | host, sPort = inStr.split(":") | ||
3163 | 210 | return (host, int(sPort)) | ||
3164 | 211 | |||
3165 | 212 | |||
3166 | 213 | |||
3167 | 214 | class _BinaryLoadable(String): | ||
3168 | 215 | def toString(self, arg): | ||
3169 | 216 | assert isinstance(arg, self.loader), "%r not %r" % (arg, self.loader) | ||
3170 | 217 | return String.toString(self, arg.dump()) | ||
3171 | 218 | |||
3172 | 219 | def fromString(self, arg): | ||
3173 | 220 | return self.loader.load(String.fromString(self, arg)) | ||
3174 | 221 | |||
3175 | 222 | class CertReq(_BinaryLoadable): | ||
3176 | 223 | loader = CertificateRequest | ||
3177 | 224 | |||
3178 | 225 | class Cert(_BinaryLoadable): | ||
3179 | 226 | loader = Certificate | ||
3180 | 227 | |||
3181 | 228 | from twisted.internet import protocol | ||
3182 | 229 | |||
3183 | 230 | class Q2QClientProtocolFactoryWrapper: | ||
3184 | 231 | |||
3185 | 232 | def __init__(self, service, cpf, fromAddress, toAddress, protocolName, | ||
3186 | 233 | connectionEstablishedDeferred): | ||
3187 | 234 | self.service = service | ||
3188 | 235 | self.cpf = cpf | ||
3189 | 236 | self.fromAddress = fromAddress | ||
3190 | 237 | self.toAddress = toAddress | ||
3191 | 238 | self.protocolName = protocolName | ||
3192 | 239 | self.connectionEstablishedDeferred = connectionEstablishedDeferred | ||
3193 | 240 | connectionEstablishedDeferred.addCallback(self.setMyClient) | ||
3194 | 241 | |||
3195 | 242 | myClient = None | ||
3196 | 243 | def setMyClient(self, myClient): | ||
3197 | 244 | # print '***CLIENT SET***', self, self.fromAddress, self.toAddress, self.cpf | ||
3198 | 245 | self.myClient = myClient | ||
3199 | 246 | return myClient | ||
3200 | 247 | |||
3201 | 248 | def buildProtocol(self, addr): | ||
3202 | 249 | # xxx modify addr to include q2q information. | ||
3203 | 250 | subProto = self.cpf.buildProtocol(self.toAddress) | ||
3204 | 251 | myProto = SeparateConnectionTransport(self.service, subProto, self.fromAddress, | ||
3205 | 252 | self.toAddress, self.protocolName, | ||
3206 | 253 | self.connectionEstablishedDeferred) | ||
3207 | 254 | return myProto | ||
3208 | 255 | |||
3209 | 256 | def clientConnectionFailed(self, connector, reason): | ||
3210 | 257 | # DON'T forward this to our client protocol factory; only one attempt | ||
3211 | 258 | # has failed; let that happen later, when _ALL_ attempts have failed. | ||
3212 | 259 | assert self.myClient is None | ||
3213 | 260 | self.connectionEstablishedDeferred.errback(reason) | ||
3214 | 261 | |||
3215 | 262 | def clientConnectionLost(self, connector, reason): | ||
3216 | 263 | # as in clientConnectionFailed, don't bother to forward; this | ||
3217 | 264 | # clientConnectionLost is actually a clientConnectionFailed for the | ||
3218 | 265 | # underlying transport. | ||
3219 | 266 | if self.myClient is not None: | ||
3220 | 267 | # forward in this case because it's likely that we need to pass it | ||
3221 | 268 | # along... | ||
3222 | 269 | self.cpf.clientConnectionLost(connector, reason) | ||
3223 | 270 | |||
3224 | 271 | def doStart(self): | ||
3225 | 272 | self.cpf.doStart() | ||
3226 | 273 | |||
3227 | 274 | def doStop(self): | ||
3228 | 275 | self.cpf.doStop() | ||
3229 | 276 | |||
3230 | 277 | class ImmediatelyLoseConnection(protocol.Protocol): | ||
3231 | 278 | def connectionMade(self): | ||
3232 | 279 | self.transport.loseConnection() | ||
3233 | 280 | |||
3234 | 281 | class AbstractConnectionAttempt(protocol.ClientFactory): | ||
3235 | 282 | |||
3236 | 283 | |||
3237 | 284 | def __init__(self, method, q2qproto, connectionID, fromAddress, toAddress, | ||
3238 | 285 | protocolName, clientProtocolFactory, issueGreeting=False): | ||
3239 | 286 | self.method = method | ||
3240 | 287 | self.q2qproto = q2qproto | ||
3241 | 288 | assert isinstance(connectionID, str) | ||
3242 | 289 | self.connectionID = connectionID | ||
3243 | 290 | self.q2qproto = q2qproto | ||
3244 | 291 | self.fromAddress = fromAddress | ||
3245 | 292 | self.toAddress = toAddress | ||
3246 | 293 | self.protocolName = protocolName | ||
3247 | 294 | self.deferred = defer.Deferred() | ||
3248 | 295 | self.clientProtocolFactory = Q2QClientProtocolFactoryWrapper( | ||
3249 | 296 | q2qproto.service, | ||
3250 | 297 | clientProtocolFactory, fromAddress, toAddress, protocolName, | ||
3251 | 298 | self.deferred) | ||
3252 | 299 | self.issueGreeting = issueGreeting | ||
3253 | 300 | |||
3254 | 301 | |||
3255 | 302 | def startAttempt(self): | ||
3256 | 303 | """ | ||
3257 | 304 | +-+ | ||
3258 | 305 | |?| | ||
3259 | 306 | +-+ | ||
3260 | 307 | """ | ||
3261 | 308 | raise NotImplementedError() | ||
3262 | 309 | |||
3263 | 310 | |||
3264 | 311 | q2qb = None | ||
3265 | 312 | |||
3266 | 313 | cancelled = False | ||
3267 | 314 | |||
3268 | 315 | def buildProtocol(self, addr): | ||
3269 | 316 | if self.cancelled: | ||
3270 | 317 | return ImmediatelyLoseConnection() | ||
3271 | 318 | assert self.q2qb is None | ||
3272 | 319 | self.q2qb = Q2QBootstrap( | ||
3273 | 320 | self.connectionID, self.clientProtocolFactory) | ||
3274 | 321 | return self.q2qb | ||
3275 | 322 | |||
3276 | 323 | def clientConnectionFailed(self, connector, reason): | ||
3277 | 324 | """ | ||
3278 | 325 | """ | ||
3279 | 326 | # Don't bother forwarding. In fact this should probably never be | ||
3280 | 327 | # called because we're not bothering to forward them along from | ||
3281 | 328 | # Q2QClientProtocolFactoryWrapper | ||
3282 | 329 | |||
3283 | 330 | def clientConnectionLost(self, connector, reason): | ||
3284 | 331 | """ | ||
3285 | 332 | """ | ||
3286 | 333 | # we don't care - this will be handled by Q2QBootstrap. | ||
3287 | 334 | |||
3288 | 335 | def cancel(self): | ||
3289 | 336 | """ | ||
3290 | 337 | - Stop attempting to connect. | ||
3291 | 338 | |||
3292 | 339 | - If a connection is somehow made after this has been cancelled, reject | ||
3293 | 340 | it. | ||
3294 | 341 | |||
3295 | 342 | - Clean up any resources, such as listening UDP or TCP ports, | ||
3296 | 343 | associated with this connection attempt [obviously, that are unshared | ||
3297 | 344 | by other connection attempt] | ||
3298 | 345 | |||
3299 | 346 | """ | ||
3300 | 347 | self.cancelled = True | ||
3301 | 348 | |||
3302 | 349 | |||
3303 | 350 | class TCPConnectionAttempt(AbstractConnectionAttempt): | ||
3304 | 351 | attempted = False | ||
3305 | 352 | def startAttempt(self): | ||
3306 | 353 | assert not self.attempted | ||
3307 | 354 | self.attempted = True | ||
3308 | 355 | reactor.connectTCP(self.method.host, self.method.port, self) | ||
3309 | 356 | return self.deferred | ||
3310 | 357 | |||
3311 | 358 | |||
3312 | 359 | class TCPMethod: | ||
3313 | 360 | def __init__(self, hostport): | ||
3314 | 361 | self.host, port = hostport.split(':') | ||
3315 | 362 | self.port = int(port) | ||
3316 | 363 | |||
3317 | 364 | attemptFactory = TCPConnectionAttempt | ||
3318 | 365 | relayable = True | ||
3319 | 366 | ptype = 'tcp' | ||
3320 | 367 | |||
3321 | 368 | def toString(self): | ||
3322 | 369 | return '%s@%s:%d' % (self.ptype, self.host, self.port) | ||
3323 | 370 | |||
3324 | 371 | def __repr__(self): | ||
3325 | 372 | return '<%s>'%self.toString() | ||
3326 | 373 | |||
3327 | 374 | def attempt(self, *a): | ||
3328 | 375 | return [self.attemptFactory(self, *a)] | ||
3329 | 376 | |||
3330 | 377 | connectionCounter = itertools.count().next | ||
3331 | 378 | connectionCounter() | ||
3332 | 379 | |||
3333 | 380 | class VirtualConnectionAttempt(AbstractConnectionAttempt): | ||
3334 | 381 | attempted = False | ||
3335 | 382 | def startAttempt(self): | ||
3336 | 383 | assert not self.attempted | ||
3337 | 384 | self.attempted = True | ||
3338 | 385 | cid = connectionCounter() | ||
3339 | 386 | if self.q2qproto.isServer: | ||
3340 | 387 | cid = -cid | ||
3341 | 388 | innerTransport = VirtualTransport(self.q2qproto, cid, self, True) | ||
3342 | 389 | def startit(result): | ||
3343 | 390 | proto = innerTransport.startProtocol() | ||
3344 | 391 | return self.deferred | ||
3345 | 392 | |||
3346 | 393 | d = self.q2qproto.callRemote(Virtual, id=cid) | ||
3347 | 394 | d.addCallback(startit) | ||
3348 | 395 | return d | ||
3349 | 396 | |||
3350 | 397 | |||
3351 | 398 | class VirtualMethod: | ||
3352 | 399 | def __init__(self, virt=None): | ||
3353 | 400 | pass | ||
3354 | 401 | |||
3355 | 402 | relayable = False | ||
3356 | 403 | |||
3357 | 404 | def toString(self): | ||
3358 | 405 | return 'virtual' | ||
3359 | 406 | |||
3360 | 407 | def __repr__(self): | ||
3361 | 408 | return '<%s>' % (self.toString(),) | ||
3362 | 409 | |||
3363 | 410 | def attempt(self, *a): | ||
3364 | 411 | return [VirtualConnectionAttempt(self, *a)] | ||
3365 | 412 | |||
3366 | 413 | |||
3367 | 414 | class _PTCPConnectionAttempt1NoPress(AbstractConnectionAttempt): | ||
3368 | 415 | attempted = False | ||
3369 | 416 | def startAttempt(self): | ||
3370 | 417 | assert not self.attempted | ||
3371 | 418 | self.attempted = True | ||
3372 | 419 | svc = self.q2qproto.service | ||
3373 | 420 | dsp = svc.dispatcher | ||
3374 | 421 | dsp.connectPTCP( | ||
3375 | 422 | self.method.host, self.method.port, self, | ||
3376 | 423 | svc.sharedUDPPortnum) | ||
3377 | 424 | return self.deferred | ||
3378 | 425 | |||
3379 | 426 | class _PTCPConnectionAttemptPress(AbstractConnectionAttempt): | ||
3380 | 427 | attempted = False | ||
3381 | 428 | def startAttempt(self): | ||
3382 | 429 | assert not self.attempted | ||
3383 | 430 | self.attempted = True | ||
3384 | 431 | |||
3385 | 432 | svc = self.q2qproto.service | ||
3386 | 433 | dsp = svc.dispatcher | ||
3387 | 434 | newPort = self.newPort = dsp.bindNewPort() | ||
3388 | 435 | dsp.connectPTCP( | ||
3389 | 436 | self.method.host, self.method.port, self, | ||
3390 | 437 | newPort) | ||
3391 | 438 | |||
3392 | 439 | return self.deferred | ||
3393 | 440 | |||
3394 | 441 | def cancel(self): | ||
3395 | 442 | if not self.cancelled: | ||
3396 | 443 | self.q2qproto.service.dispatcher.unbindPort(self.newPort) | ||
3397 | 444 | else: | ||
3398 | 445 | print 'totally wacky, [press] cancelled twice!' | ||
3399 | 446 | AbstractConnectionAttempt.cancel(self) | ||
3400 | 447 | |||
3401 | 448 | class PTCPMethod(TCPMethod): | ||
3402 | 449 | """Pseudo-TCP method. | ||
3403 | 450 | """ | ||
3404 | 451 | ptype = 'ptcp' | ||
3405 | 452 | |||
3406 | 453 | def attempt(self, *a): | ||
3407 | 454 | return [_PTCPConnectionAttempt1NoPress(self, *a), | ||
3408 | 455 | _PTCPConnectionAttemptPress(self, *a)] | ||
3409 | 456 | |||
3410 | 457 | class RPTCPConnectionAttempt(AbstractConnectionAttempt): | ||
3411 | 458 | attempted = False | ||
3412 | 459 | def startAttempt(self): | ||
3413 | 460 | assert not self.attempted | ||
3414 | 461 | self.attempted = True | ||
3415 | 462 | |||
3416 | 463 | realLocalUDP = self.newPort = self.q2qproto.service.dispatcher.seedNAT((self.method.host, self.method.port)) | ||
3417 | 464 | # self.host and self.port are remote host and port | ||
3418 | 465 | # realLocalUDP is a local port | ||
3419 | 466 | |||
3420 | 467 | # The arguments here are given from the perspective of the recipient of | ||
3421 | 468 | # the command. we are asking the recipient of the connection to map a | ||
3422 | 469 | # NAT entry of a pre-existing listening UDP socket on their end of the | ||
3423 | 470 | # connection by sending us some traffic. therefore the src is their | ||
3424 | 471 | # endpoint, the dst is our endpoint, the user we are asking them to | ||
3425 | 472 | # send TO is us, the user we are asking them to accept this FROM is us. | ||
3426 | 473 | |||
3427 | 474 | # we include protocol as an arg because this is helpful for relaying. | ||
3428 | 475 | |||
3429 | 476 | def enbinden(boundereded): | ||
3430 | 477 | if not self.cancelled: | ||
3431 | 478 | self.q2qproto.service.dispatcher.connectPTCP( | ||
3432 | 479 | self.method.host, self.method.port, self, realLocalUDP | ||
3433 | 480 | ) | ||
3434 | 481 | return self.deferred | ||
3435 | 482 | |||
3436 | 483 | def swallowKnown(error): | ||
3437 | 484 | error.trap(ConnectionError) | ||
3438 | 485 | self.deferred.errback(CONNECTION_DONE) | ||
3439 | 486 | return self.deferred | ||
3440 | 487 | |||
3441 | 488 | d = self.q2qproto.callRemote( | ||
3442 | 489 | BindUDP, | ||
3443 | 490 | q2qsrc=self.toAddress, | ||
3444 | 491 | q2qdst=self.fromAddress, | ||
3445 | 492 | protocol=self.protocolName, | ||
3446 | 493 | udpsrc=(self.method.host, self.method.port), | ||
3447 | 494 | udpdst=(self.q2qproto._determinePublicIP(), realLocalUDP)) | ||
3448 | 495 | d.addCallbacks(enbinden, swallowKnown) | ||
3449 | 496 | return d | ||
3450 | 497 | |||
3451 | 498 | def cancel(self): | ||
3452 | 499 | if not self.cancelled: | ||
3453 | 500 | self.q2qproto.service.dispatcher.unbindPort(self.newPort) | ||
3454 | 501 | else: | ||
3455 | 502 | print 'totally wacky, [rptcp] cancelled twice!' | ||
3456 | 503 | AbstractConnectionAttempt.cancel(self) | ||
3457 | 504 | |||
3458 | 505 | |||
3459 | 506 | |||
3460 | 507 | |||
3461 | 508 | class RPTCPMethod(TCPMethod): | ||
3462 | 509 | """ Certain NATs respond very poorly to seed traffic: e.g. if they receive | ||
3463 | 510 | unsolicited traffic to a particular port, they will make that outbound port | ||
3464 | 511 | unavailable for outbound traffic originated internally. The | ||
3465 | 512 | Reverse-Pseudo-TCP method is a way to have the *sender* send the first UDP | ||
3466 | 513 | packet, so they will bind it. | ||
3467 | 514 | |||
3468 | 515 | This is a worst-case scenario: if both ends of the connection have NATs | ||
3469 | 516 | which behave this way, there is no way to establish a connection. | ||
3470 | 517 | """ | ||
3471 | 518 | |||
3472 | 519 | ptype = 'rptcp' | ||
3473 | 520 | attemptFactory = RPTCPConnectionAttempt | ||
3474 | 521 | |||
3475 | 522 | |||
3476 | 523 | class UnknownMethod: | ||
3477 | 524 | |||
3478 | 525 | relayable = True | ||
3479 | 526 | |||
3480 | 527 | def __init__(self, S): | ||
3481 | 528 | self.string = S | ||
3482 | 529 | |||
3483 | 530 | def attemptConnect(self, q2qproto, connectionID, From, to, | ||
3484 | 531 | protocolName, protocolFactory): | ||
3485 | 532 | return defer.fail(Failure(ConnectionError( | ||
3486 | 533 | "unknown connection method: %s" % (self.string,)))) | ||
3487 | 534 | |||
3488 | 535 | |||
3489 | 536 | _methodFactories = {'virtual': VirtualMethod, | ||
3490 | 537 | 'tcp': TCPMethod, | ||
3491 | 538 | 'ptcp': PTCPMethod, | ||
3492 | 539 | 'rptcp': RPTCPMethod} | ||
3493 | 540 | |||
3494 | 541 | class Method(Argument): | ||
3495 | 542 | def toString(self, inObj): | ||
3496 | 543 | return inObj.toString() | ||
3497 | 544 | |||
3498 | 545 | |||
3499 | 546 | def fromString(self, inString): | ||
3500 | 547 | f = inString.split("@", 1) | ||
3501 | 548 | factoryName = f[0] | ||
3502 | 549 | if len(f) > 1: | ||
3503 | 550 | factoryData = f[1] | ||
3504 | 551 | else: | ||
3505 | 552 | factoryData = '' | ||
3506 | 553 | methodFactory = _methodFactories.get(factoryName, None) | ||
3507 | 554 | if methodFactory is None: | ||
3508 | 555 | factory = UnknownMethod(inString) | ||
3509 | 556 | else: | ||
3510 | 557 | factory = methodFactory(factoryData) | ||
3511 | 558 | return factory | ||
3512 | 559 | |||
3513 | 560 | |||
3514 | 561 | class Secure(StartTLS): | ||
3515 | 562 | |||
3516 | 563 | commandName = "secure" | ||
3517 | 564 | arguments = StartTLS.arguments + [ | ||
3518 | 565 | ('From', Q2QAddressArgument(optional=True)), | ||
3519 | 566 | ('to', Q2QAddressArgument()), | ||
3520 | 567 | ('authorize', Boolean()) | ||
3521 | 568 | ] | ||
3522 | 569 | |||
3523 | 570 | |||
3524 | 571 | |||
3525 | 572 | class Listen(Command): | ||
3526 | 573 | """ | ||
3527 | 574 | A simple command for registering interest with an active Q2Q connection | ||
3528 | 575 | to hear from a server when others come calling. An occurrence of this | ||
3529 | 576 | command might have this appearance on the wire:: | ||
3530 | 577 | |||
3531 | 578 | C: -Command: Listen | ||
3532 | 579 | C: -Ask: 1 | ||
3533 | 580 | C: From: glyph@divmod.com | ||
3534 | 581 | C: Protocols: q2q-example, q2q-example2 | ||
3535 | 582 | C: Description: some simple protocols | ||
3536 | 583 | C: | ||
3537 | 584 | S: -Answer: 1 | ||
3538 | 585 | S: | ||
3539 | 586 | |||
3540 | 587 | This puts some state on the server side that will affect any Connect | ||
3541 | 588 | commands with q2q-example or q2q-example2 in the Protocol: header. | ||
3542 | 589 | """ | ||
3543 | 590 | |||
3544 | 591 | commandName = 'listen' | ||
3545 | 592 | arguments = [ | ||
3546 | 593 | ('From', Q2QAddressArgument()), | ||
3547 | 594 | ('protocols', ListOf(String())), | ||
3548 | 595 | ('description', Unicode())] | ||
3549 | 596 | |||
3550 | 597 | result = [] | ||
3551 | 598 | |||
3552 | 599 | class ConnectionStartBox(AmpBox): | ||
3553 | 600 | def __init__(self, __transport): | ||
3554 | 601 | super(ConnectionStartBox, self).__init__() | ||
3555 | 602 | self.virtualTransport = __transport | ||
3556 | 603 | |||
3557 | 604 | # XXX Overriding a private interface | ||
3558 | 605 | def _sendTo(self, proto): | ||
3559 | 606 | super(ConnectionStartBox, self)._sendTo(proto) | ||
3560 | 607 | self.virtualTransport.startProtocol() | ||
3561 | 608 | |||
3562 | 609 | class Virtual(Command): | ||
3563 | 610 | commandName = 'virtual' | ||
3564 | 611 | result = [] | ||
3565 | 612 | |||
3566 | 613 | arguments = [('id', Integer())] | ||
3567 | 614 | |||
3568 | 615 | def makeResponse(cls, objects, proto): | ||
3569 | 616 | tpt = objects.pop('__transport__') | ||
3570 | 617 | # XXX Using a private API | ||
3571 | 618 | return _objectsToStrings( | ||
3572 | 619 | objects, cls.response, | ||
3573 | 620 | ConnectionStartBox(tpt), | ||
3574 | 621 | proto) | ||
3575 | 622 | |||
3576 | 623 | makeResponse = classmethod(makeResponse) | ||
3577 | 624 | |||
3578 | 625 | class Identify(Command): | ||
3579 | 626 | """ | ||
3580 | 627 | Respond to an IDENTIFY command with a self-signed certificate for the | ||
3581 | 628 | domain requested, assuming we are an authority for said domain. An | ||
3582 | 629 | occurrence of this command might have this appearance on the wire:: | ||
3583 | 630 | |||
3584 | 631 | C: -Command: Identify | ||
3585 | 632 | C: -Ask: 1 | ||
3586 | 633 | C: Domain: divmod.com | ||
3587 | 634 | C: | ||
3588 | 635 | S: -Answer: 1 | ||
3589 | 636 | S: Certificate: <<<base64-encoded self-signed certificate of divmod.com>>> | ||
3590 | 637 | S: | ||
3591 | 638 | |||
3592 | 639 | """ | ||
3593 | 640 | |||
3594 | 641 | commandName = 'identify' | ||
3595 | 642 | |||
3596 | 643 | arguments = [('subject', Q2QAddressArgument())] | ||
3597 | 644 | |||
3598 | 645 | response = [('certificate', Cert())] | ||
3599 | 646 | |||
3600 | 647 | class BindUDP(Command): | ||
3601 | 648 | """ | ||
3602 | 649 | See UDPXMethod | ||
3603 | 650 | """ | ||
3604 | 651 | |||
3605 | 652 | commandName = 'bind-udp' | ||
3606 | 653 | |||
3607 | 654 | arguments = [ | ||
3608 | 655 | ('protocol', String()), | ||
3609 | 656 | ('q2qsrc', Q2QAddressArgument()), | ||
3610 | 657 | ('q2qdst', Q2QAddressArgument()), | ||
3611 | 658 | ('udpsrc', HostPort()), | ||
3612 | 659 | ('udpdst', HostPort()), | ||
3613 | 660 | ] | ||
3614 | 661 | |||
3615 | 662 | errors = {ConnectionError: 'ConnectionError'} | ||
3616 | 663 | |||
3617 | 664 | response = [] | ||
3618 | 665 | |||
3619 | 666 | class SourceIP(Command): | ||
3620 | 667 | """ | ||
3621 | 668 | Ask a server on the public internet what my public IP probably is. An | ||
3622 | 669 | occurrence of this command might have this appearance on the wire:: | ||
3623 | 670 | |||
3624 | 671 | C: -Command: Source-IP | ||
3625 | 672 | C: -Ask: 1 | ||
3626 | 673 | C: | ||
3627 | 674 | S: -Answer: 1 | ||
3628 | 675 | S: IP: 4.3.2.1 | ||
3629 | 676 | S: | ||
3630 | 677 | |||
3631 | 678 | """ | ||
3632 | 679 | |||
3633 | 680 | commandName = 'source-ip' | ||
3634 | 681 | |||
3635 | 682 | arguments = [] | ||
3636 | 683 | |||
3637 | 684 | response = [('ip', String())] | ||
3638 | 685 | |||
3639 | 686 | class Inbound(Command): | ||
3640 | 687 | """ | ||
3641 | 688 | Request information about where to connect to a particular resource. | ||
3642 | 689 | |||
3643 | 690 | Generally speaking this is an "I want to connect to you" request. | ||
3644 | 691 | |||
3645 | 692 | The format of this request is transport neutral except for the optional | ||
3646 | 693 | 'Udp_Source' header, which specifies an IP/Port pair for all receiving peers to | ||
3647 | 694 | send an almost-empty (suggested value of '\\r\\n') UDP packet to to help | ||
3648 | 695 | with NAT traversal issues. | ||
3649 | 696 | |||
3650 | 697 | See L{Q2QService.connectQ2Q} for details. | ||
3651 | 698 | |||
3652 | 699 | An occurrence of this command might have this appearance on the wire:: | ||
3653 | 700 | |||
3654 | 701 | C: -Command: Inbound | ||
3655 | 702 | C: -Ask: 1 | ||
3656 | 703 | C: From: glyph@divmod.com | ||
3657 | 704 | C: Id: 681949ffa3be@twistedmatrix.com | ||
3658 | 705 | C: To: radix@twistedmatrix.com | ||
3659 | 706 | C: Protocol: q2q-example | ||
3660 | 707 | C: Udp_Source: 1.2.3.4:4321 | ||
3661 | 708 | C: | ||
3662 | 709 | S: -Answer: 1 | ||
3663 | 710 | S: Listeners: | ||
3664 | 711 | S: Description: at lab | ||
3665 | 712 | S: Methods: tcp@18.38.12.4:3827, virtual | ||
3666 | 713 | S: | ||
3667 | 714 | S: Description: my home machine | ||
3668 | 715 | S: Methods: tcp@187.48.38.3:49812, udp@187.48.38.3:49814, virtual | ||
3669 | 716 | |||
3670 | 717 | Now the connection-id has been registered and either client or server can | ||
3671 | 718 | issue WRITE or CLOSE commands. | ||
3672 | 719 | |||
3673 | 720 | Failure modes:: | ||
3674 | 721 | |||
3675 | 722 | - "NotFound": the toResource or toDomain is invalid, or the resource does | ||
3676 | 723 | not speak that protocol. | ||
3677 | 724 | |||
3678 | 725 | - "VerifyError": Authenticity or security for the requested connection | ||
3679 | 726 | could not be authorized. This is a fatal error: the connection will be | ||
3680 | 727 | dropped. | ||
3681 | 728 | |||
3682 | 729 | The "Udp_Source" header indicates the address from which this Inbound chain | ||
3683 | 730 | originated. It is to be used to establish connections where possible | ||
3684 | 731 | between NATs which require traffic between two host/port pairs to be | ||
3685 | 732 | bidirectional before a "hole" is established, such as port restricted cone | ||
3686 | 733 | and symmetric NATs. (Note, this only has about a 30% probability of | ||
3687 | 734 | working on a symmetric NAT, but it's worth trying sometimes anyway). Any | ||
3688 | 735 | UDP-based connection methods (currently only Gin, but in principle others | ||
3689 | 736 | such as RTP, RTCP, SIP and Quake traffic) that wish to use this connection | ||
3690 | 737 | must first send some garbage traffic to the host/port specified by the | ||
3691 | 738 | "Udp_Source" header. | ||
3692 | 739 | |||
3693 | 740 | The response is a list of "listeners" - a small (unicode) textual | ||
3694 | 741 | description of a host, plus a list of methods describing how to connect to | ||
3695 | 742 | it. | ||
3696 | 743 | """ | ||
3697 | 744 | |||
3698 | 745 | commandName = 'inbound' | ||
3699 | 746 | arguments = [('From', Q2QAddressArgument()), | ||
3700 | 747 | ('to', Q2QAddressArgument()), | ||
3701 | 748 | ('protocol', String()), | ||
3702 | 749 | ('udp_source', HostPort(optional=True))] | ||
3703 | 750 | |||
3704 | 751 | response = [('listeners', AmpList( | ||
3705 | 752 | [('id', String()), | ||
3706 | 753 | ('certificate', Cert(optional=True)), | ||
3707 | 754 | ('methods', ListOf(Method())), | ||
3708 | 755 | ('expires', AmpTime()), | ||
3709 | 756 | ('description', Unicode())]))] | ||
3710 | 757 | |||
3711 | 758 | errors = {KeyError: "NotFound"} | ||
3712 | 759 | fatalErrors = {VerifyError: "VerifyError"} | ||
3713 | 760 | |||
3714 | 761 | class Outbound(Command): | ||
3715 | 762 | """Similar to Inbound, but _requires that the recipient already has the | ||
3716 | 763 | id parameter as an outgoing connection attempt_. | ||
3717 | 764 | """ | ||
3718 | 765 | commandName = 'outbound' | ||
3719 | 766 | |||
3720 | 767 | arguments = [('From', Q2QAddressArgument()), | ||
3721 | 768 | ('to', Q2QAddressArgument()), | ||
3722 | 769 | ('protocol', String()), | ||
3723 | 770 | ('id', String()), | ||
3724 | 771 | ('methods', ListOf(Method()))] | ||
3725 | 772 | |||
3726 | 773 | response = [] | ||
3727 | 774 | |||
3728 | 775 | errors = {AttemptsFailed: 'AttemptsFailed'} | ||
3729 | 776 | |||
3730 | 777 | class Sign(Command): | ||
3731 | 778 | commandName = 'sign' | ||
3732 | 779 | arguments = [('certificate_request', CertReq()), | ||
3733 | 780 | ('password', String())] | ||
3734 | 781 | |||
3735 | 782 | response = [('certificate', Cert())] | ||
3736 | 783 | |||
3737 | 784 | errors = {KeyError: "NoSuchUser", | ||
3738 | 785 | BadCertificateRequest: "BadCertificateRequest"} | ||
3739 | 786 | |||
3740 | 787 | class Choke(Command): | ||
3741 | 788 | """Ask our peer to be quiet for a while. | ||
3742 | 789 | """ | ||
3743 | 790 | commandName = 'Choke' | ||
3744 | 791 | arguments = [('id', Integer())] | ||
3745 | 792 | requiresAnswer = False | ||
3746 | 793 | |||
3747 | 794 | |||
3748 | 795 | class Unchoke(Command): | ||
3749 | 796 | """Reverse the effects of a choke. | ||
3750 | 797 | """ | ||
3751 | 798 | commandName = 'Unchoke' | ||
3752 | 799 | arguments = [('id', Integer())] | ||
3753 | 800 | requiresAnswer = False | ||
3754 | 801 | |||
3755 | 802 | |||
3756 | 803 | def safely(f, *a, **k): | ||
3757 | 804 | """try/except around something, w/ twisted error handling. | ||
3758 | 805 | """ | ||
3759 | 806 | try: | ||
3760 | 807 | f(*a,**k) | ||
3761 | 808 | except: | ||
3762 | 809 | log.err() | ||
3763 | 810 | |||
3764 | 811 | class Q2Q(AMP, subproducer.SuperProducer): | ||
3765 | 812 | """ Quotient to Quotient protocol. | ||
3766 | 813 | |||
3767 | 814 | At a low level, this uses a protocol called 'Juice' (JUice Is Concurrent | ||
3768 | 815 | Events), which is a simple rfc2822-inspired (although not -compliant) | ||
3769 | 816 | protocol for request/response pair hookup. | ||
3770 | 817 | |||
3771 | 818 | At a higher level, it provides a mechanism for SSL certificate exchange, | ||
3772 | 819 | looking up physical locations of users' data, and switching into other | ||
3773 | 820 | protocols after an initial handshake. | ||
3774 | 821 | |||
3775 | 822 | @ivar publicIP: The IP that the other end of the connection claims to know | ||
3776 | 823 | us by. This will be used when responding to L{Inbound} commands if the Q2Q | ||
3777 | 824 | service I am attached to does not specify a public IP to use. | ||
3778 | 825 | |||
3779 | 826 | @ivar authorized: A boolean indicating whether SSL verification has taken | ||
3780 | 827 | place to ensure that this connection's peer has claimed an accurate identity. | ||
3781 | 828 | """ | ||
3782 | 829 | |||
3783 | 830 | protocolName = 'q2q' | ||
3784 | 831 | service = None | ||
3785 | 832 | publicIP = None | ||
3786 | 833 | authorized = False | ||
3787 | 834 | |||
3788 | 835 | def __init__(self, *a, **kw): | ||
3789 | 836 | """ Q2Q instances should only be created by Q2QService. See | ||
3790 | 837 | L{Q2QService.connectQ2Q} and L{Q2QService.listenQ2Q}. | ||
3791 | 838 | """ | ||
3792 | 839 | subproducer.SuperProducer.__init__(self) | ||
3793 | 840 | AMP.__init__(self, *a, **kw) | ||
3794 | 841 | |||
3795 | 842 | def connectionMade(self): | ||
3796 | 843 | self.producingTransports = {} | ||
3797 | 844 | self.connections = {} | ||
3798 | 845 | self.listeningClient = [] | ||
3799 | 846 | self.connectionObservers = [] | ||
3800 | 847 | if self.service.publicIP is None: | ||
3801 | 848 | log.msg("Service has no public IP: determining") | ||
3802 | 849 | self.service.publicIP = self.transport.getHost().host | ||
3803 | 850 | self.service._publicIPIsReallyPrivate = True | ||
3804 | 851 | def rememberPublicIP(pubip): | ||
3805 | 852 | ip = pubip['ip'] | ||
3806 | 853 | log.msg('remembering public ip as %r' % ip) | ||
3807 | 854 | self.publicIP = ip | ||
3808 | 855 | self.service.publicIP = ip | ||
3809 | 856 | self.service._publicIPIsReallyPrivate = False | ||
3810 | 857 | self.callRemote(SourceIP).addCallback(rememberPublicIP) | ||
3811 | 858 | else: | ||
3812 | 859 | log.msg("Using existing public IP: %r" % (self.service.publicIP,)) | ||
3813 | 860 | |||
3814 | 861 | def connectionLost(self, reason): | ||
3815 | 862 | "" | ||
3816 | 863 | AMP.connectionLost(self, reason) | ||
3817 | 864 | self._uncacheMe() | ||
3818 | 865 | self.producingTransports = {} | ||
3819 | 866 | for key, value in self.listeningClient: | ||
3820 | 867 | log.msg("removing remote listener for %r" % (key,)) | ||
3821 | 868 | self.service.listeningClients[key].remove(value) | ||
3822 | 869 | self.listeningClient = [] | ||
3823 | 870 | for xport in self.connections.values(): | ||
3824 | 871 | safely(xport.connectionLost, reason) | ||
3825 | 872 | for observer in self.connectionObservers: | ||
3826 | 873 | safely(observer) | ||
3827 | 874 | |||
3828 | 875 | def notifyOnConnectionLost(self, observer): | ||
3829 | 876 | "" | ||
3830 | 877 | self.connectionObservers.append(observer) | ||
3831 | 878 | |||
3832 | 879 | def _bindUDP(self, q2qsrc, q2qdst, udpsrc, udpdst, protocol): | ||
3833 | 880 | |||
3834 | 881 | # we are representing the src, because they are the ones being told to | ||
3835 | 882 | # originate a UDP packet. | ||
3836 | 883 | |||
3837 | 884 | self.verifyCertificateAllowed(q2qsrc, q2qdst) | ||
3838 | 885 | |||
3839 | 886 | # if I've got a local factory for this 3-tuple, do the bind if I own | ||
3840 | 887 | # this IP... | ||
3841 | 888 | srchost, srcport = udpsrc | ||
3842 | 889 | |||
3843 | 890 | lcget = self.service.listeningClients.get((q2qsrc, protocol), ()) | ||
3844 | 891 | |||
3845 | 892 | bindery = [] | ||
3846 | 893 | |||
3847 | 894 | for (listener, listenCert, desc | ||
3848 | 895 | ) in lcget: | ||
3849 | 896 | # print 'looking at listener', listener | ||
3850 | 897 | # print listener.transport.getPeer().host, srchost | ||
3851 | 898 | if listener.transport.getPeer().host == srchost: | ||
3852 | 899 | # print 'bound in clients loop' | ||
3853 | 900 | |||
3854 | 901 | d = listener.callRemote( | ||
3855 | 902 | BindUDP, | ||
3856 | 903 | q2qsrc=q2qsrc, | ||
3857 | 904 | q2qdst=q2qdst, | ||
3858 | 905 | udpsrc=udpsrc, | ||
3859 | 906 | udpdst=udpdst, | ||
3860 | 907 | protocol=protocol) | ||
3861 | 908 | def swallowKnown(err): | ||
3862 | 909 | err.trap(error.ConnectionDone, error.ConnectionLost) | ||
3863 | 910 | d.addErrback(swallowKnown) | ||
3864 | 911 | bindery.append(d) | ||
3865 | 912 | if bindery: | ||
3866 | 913 | # print 'bindery return', len(bindery) | ||
3867 | 914 | def _justADict(ign): | ||
3868 | 915 | return dict() | ||
3869 | 916 | return defer.DeferredList(bindery).addCallback(_justADict) | ||
3870 | 917 | |||
3871 | 918 | # print 'what?', lcget | ||
3872 | 919 | if (self.service.getLocalFactories(q2qdst, q2qsrc, protocol) | ||
3873 | 920 | and srchost == self._determinePublicIP()): | ||
3874 | 921 | self.service.dispatcher.seedNAT(udpdst, srcport, conditional=True) | ||
3875 | 922 | # print 'bound locally' | ||
3876 | 923 | return dict() | ||
3877 | 924 | # print 'conn-error' | ||
3878 | 925 | raise ConnectionError("unable to find appropriate UDP binder") | ||
3879 | 926 | |||
3880 | 927 | BindUDP.responder(_bindUDP) | ||
3881 | 928 | |||
3882 | 929 | def _identify(self, subject): | ||
3883 | 930 | """ | ||
3884 | 931 | Implementation of L{Identify}. | ||
3885 | 932 | """ | ||
3886 | 933 | ourCA = self.service.certificateStorage.getPrivateCertificate(str(subject)) | ||
3887 | 934 | return dict(certificate=ourCA) | ||
3888 | 935 | Identify.responder(_identify) | ||
3889 | 936 | |||
3890 | 937 | |||
3891 | 938 | def verifyCertificateAllowed(self, | ||
3892 | 939 | ourAddress, | ||
3893 | 940 | theirAddress): | ||
3894 | 941 | """ | ||
3895 | 942 | Check that the certificate currently in use by this transport is valid to | ||
3896 | 943 | claim that the connection offers authorization for this host speaking | ||
3897 | 944 | for C{ourAddress}, to a host speaking for C{theirAddress}. The remote | ||
3898 | 945 | host (the one claiming to use theirAddress) may have a certificate | ||
3899 | 946 | which is issued for the domain for theirAddress or the full address | ||
3900 | 947 | given in theirAddress. | ||
3901 | 948 | |||
3902 | 949 | This method runs B{after} cryptographic verification of the validity of | ||
3903 | 950 | certificates, although it does not perform any cryptographic checks | ||
3904 | 951 | itself. It depends on SSL connection handshaking - *and* the | ||
3905 | 952 | particular certificate lookup logic which prevents spoofed Issuer | ||
3906 | 953 | fields, to work properly. However, all it checks is the X509 names | ||
3907 | 954 | present in the certificates matching with the application-level | ||
3908 | 955 | security claims being made by our peer. | ||
3909 | 956 | |||
3910 | 957 | An example of successful verification, because both parties have | ||
3911 | 958 | properly signed certificates for their usage from the domain they | ||
3912 | 959 | have been issued:: | ||
3913 | 960 | |||
3914 | 961 | our current certficate: | ||
3915 | 962 | issuer: divmod.com | ||
3916 | 963 | subject: glyph@divmod.com | ||
3917 | 964 | their current certificate: | ||
3918 | 965 | issuer: twistedmatrix.com | ||
3919 | 966 | subject: exarkun@twistedmatrix.com | ||
3920 | 967 | Arguments to verifyCertificateAllowed: | ||
3921 | 968 | ourAddress: glyph@divmod.com | ||
3922 | 969 | theirAddress: exarkun@twistedmatrix.com | ||
3923 | 970 | Result of verifyCertificateAllowed: None | ||
3924 | 971 | |||
3925 | 972 | An example of rejected verification, because domain certificates are | ||
3926 | 973 | always B{self}-signed in Q2Q; verisign is not a trusted certificate | ||
3927 | 974 | authority for the entire internet as with some other TLS | ||
3928 | 975 | implementations:: | ||
3929 | 976 | |||
3930 | 977 | our current certificate: | ||
3931 | 978 | issuer: divmod.com | ||
3932 | 979 | subject: divmod.com | ||
3933 | 980 | their current certificate: | ||
3934 | 981 | issuer: verisign.com | ||
3935 | 982 | subject: twistedmatrix.com | ||
3936 | 983 | Arguments to verifyCertificateAllowed: | ||
3937 | 984 | ourAddress: divmod.com | ||
3938 | 985 | theirAddress: twistedmatrix.com | ||
3939 | 986 | Result of verifyCertificateAllowed: exception VerifyError raised | ||
3940 | 987 | |||
3941 | 988 | Another example of successful verification, because we assume our | ||
3942 | 989 | current certificate is under the control of this side of the | ||
3943 | 990 | connection, so *any* claimed subject is considered acceptable:: | ||
3944 | 991 | |||
3945 | 992 | our current certificate: | ||
3946 | 993 | issuer: divmod.com | ||
3947 | 994 | subject: divmod.com | ||
3948 | 995 | their current certificate: | ||
3949 | 996 | issuer: divmod.com | ||
3950 | 997 | subject: glyph@twistedmatrix.com | ||
3951 | 998 | Arguments to verifyCertificateAllowed: | ||
3952 | 999 | ourAddress: divmod.com | ||
3953 | 1000 | theirAddress: glyph@twistedmatrix.com | ||
3954 | 1001 | Result of verifyCertificateAllowed: None | ||
3955 | 1002 | |||
3956 | 1003 | Another example of successful verification, because the user is | ||
3957 | 1004 | claiming to be anonymous; there is also a somewhat looser | ||
3958 | 1005 | cryptographic check applied to signatures for anonymous | ||
3959 | 1006 | connections:: | ||
3960 | 1007 | |||
3961 | 1008 | our current certificate: | ||
3962 | 1009 | issuer: divmod.com | ||
3963 | 1010 | subject: divmod.com | ||
3964 | 1011 | their current certificate: | ||
3965 | 1012 | issuer: @ | ||
3966 | 1013 | subject: @ | ||
3967 | 1014 | arguments to verifyCertificateAllowed: | ||
3968 | 1015 | ourAddress: divmod.com | ||
3969 | 1016 | theirAddress: @ | ||
3970 | 1017 | Result of verifyCertificateAllowed: None | ||
3971 | 1018 | |||
3972 | 1019 | Accept anonymous connections with caution. | ||
3973 | 1020 | |||
3974 | 1021 | @param ourAddress: a L{Q2QAddress} representing the address that we are | ||
3975 | 1022 | supposed to have authority for, requested by our peer. | ||
3976 | 1023 | |||
3977 | 1024 | @param theirAddress: a L{Q2QAddress} representing the address that our | ||
3978 | 1025 | network peer claims to be communicating on behalf of. For example, if | ||
3979 | 1026 | our peer is foobar.com they may claim to be operating on behalf of any | ||
3980 | 1027 | user @foobar.com. | ||
3981 | 1028 | |||
3982 | 1029 | @raise: L{VerifyError} if the certificates do not match the | ||
3983 | 1030 | claimed addresses. | ||
3984 | 1031 | """ | ||
3985 | 1032 | |||
3986 | 1033 | # XXX TODO: Somehow, it's got to be possible for a single cluster to | ||
3987 | 1034 | # internally claim to be agents of any other host when issuing a | ||
3988 | 1035 | # CONNECT; in other words, we always implicitly trust ourselves. Also, | ||
3989 | 1036 | # we might want to issue anonymous CONNECTs over unencrypted | ||
3990 | 1037 | # connections. | ||
3991 | 1038 | |||
3992 | 1039 | # IOW: *we* can sign a certificate to be whoever, but the *peer* can | ||
3993 | 1040 | # only sign the certificate to be the peer. | ||
3994 | 1041 | |||
3995 | 1042 | # The easiest way to make this work is to issue ourselves a wildcard | ||
3996 | 1043 | # certificate. | ||
3997 | 1044 | |||
3998 | 1045 | if not self.authorized: | ||
3999 | 1046 | if theirAddress.domain == '': | ||
4000 | 1047 | # XXX TODO: document this rule, anonymous connections are | ||
4001 | 1048 | # allowed to not be authorized because they are not making any | ||
4002 | 1049 | # claims about who they are | ||
4003 | 1050 | |||
4004 | 1051 | # XXX also TODO: make it so that anonymous connections are | ||
4005 | 1052 | # disabled by default for most protocols | ||
4006 | 1053 | return True | ||
4007 | 1054 | raise VerifyError("No official negotiation has taken place.") | ||
4008 | 1055 | |||
4009 | 1056 | peerCert = Certificate.peerFromTransport(self.transport) | ||
4010 | 1057 | ourCert = self.hostCertificate | ||
4011 | 1058 | |||
4012 | 1059 | ourClaimedDomain = ourAddress.domainAddress() | ||
4013 | 1060 | theirClaimedDomain = theirAddress.domainAddress() | ||
4014 | 1061 | |||
4015 | 1062 | # Sanity check #1: did we pick the right certificate on our end? | ||
4016 | 1063 | if not ourClaimedDomain.claimedAsIssuerOf(ourCert): | ||
4017 | 1064 | raise VerifyError( | ||
4018 | 1065 | "Something has gone horribly wrong: local domain mismatch " | ||
4019 | 1066 | "claim: %s actual: %s" % (ourClaimedDomain, | ||
4020 | 1067 | ourCert.getIssuer())) | ||
4021 | 1068 | if theirClaimedDomain.claimedAsIssuerOf(peerCert): | ||
4022 | 1069 | # Their domain issued their certificate. | ||
4023 | 1070 | if theirAddress.claimedAsSubjectOf(peerCert) or theirClaimedDomain.claimedAsSubjectOf(peerCert): | ||
4024 | 1071 | return | ||
4025 | 1072 | elif ourClaimedDomain.claimedAsIssuerOf(peerCert): | ||
4026 | 1073 | # *our* domain can spoof *anything* | ||
4027 | 1074 | return | ||
4028 | 1075 | elif ourAddress.claimedAsIssuerOf(peerCert): | ||
4029 | 1076 | # Neither our domain nor their domain signed this. Did *we*? | ||
4030 | 1077 | # (Useful in peer-to-peer persistent transactions where we don't | ||
4031 | 1078 | # want the server involved: exarkun@twistedmatrix.com can sign | ||
4032 | 1079 | # glyph@divmod.com's certificate). | ||
4033 | 1080 | return | ||
4034 | 1081 | |||
4035 | 1082 | raise VerifyError( | ||
4036 | 1083 | "Us: %s Them: %s " | ||
4037 | 1084 | "TheyClaimWeAre: %s TheyClaimTheyAre: %s" % | ||
4038 | 1085 | (ourCert, peerCert, | ||
4039 | 1086 | ourAddress, theirAddress)) | ||
4040 | 1087 | |||
4041 | 1088 | def _listen(self, protocols, From, description): | ||
4042 | 1089 | """ | ||
4043 | 1090 | Implementation of L{Listen}. | ||
4044 | 1091 | """ | ||
4045 | 1092 | # The peer is coming from a client-side representation of the user | ||
4046 | 1093 | # described by 'From', and talking *to* a server-side representation of | ||
4047 | 1094 | # the user described by 'From'. | ||
4048 | 1095 | self.verifyCertificateAllowed(From, From) | ||
4049 | 1096 | theirCert = Certificate.peerFromTransport(self.transport) | ||
4050 | 1097 | for protocolName in protocols: | ||
4051 | 1098 | if protocolName.startswith('.'): | ||
4052 | 1099 | raise VerifyError( | ||
4053 | 1100 | "Internal protocols are for server-server use _only_: %r" % | ||
4054 | 1101 | protocolName) | ||
4055 | 1102 | |||
4056 | 1103 | key = (From, protocolName) | ||
4057 | 1104 | value = (self, theirCert, description) | ||
4058 | 1105 | log.msg("%r listening for %r" % key) | ||
4059 | 1106 | self.listeningClient.append((key, value)) | ||
4060 | 1107 | self.service.listeningClients.setdefault(key, []).append(value) | ||
4061 | 1108 | return {} | ||
4062 | 1109 | Listen.responder(_listen) | ||
4063 | 1110 | |||
4064 | 1111 | |||
4065 | 1112 | def _inbound(self, From, to, protocol, udp_source=None): | ||
4066 | 1113 | """ | ||
4067 | 1114 | Implementation of L{Inbound}. | ||
4068 | 1115 | """ | ||
4069 | 1116 | # Verify stuff! | ||
4070 | 1117 | |||
4071 | 1118 | self.verifyCertificateAllowed(to, From) | ||
4072 | 1119 | return self.service.verifyHook(From, to, protocol | ||
4073 | 1120 | ).addCallback(self._inboundimpl, | ||
4074 | 1121 | From, | ||
4075 | 1122 | to, | ||
4076 | 1123 | protocol, | ||
4077 | 1124 | udp_source).addErrback( | ||
4078 | 1125 | lambda f: f.trap(KeyError) and dict(listeners=[])) | ||
4079 | 1126 | Inbound.responder(_inbound) | ||
4080 | 1127 | |||
4081 | 1128 | def _inboundimpl(self, ign, From, to, protocol, udp_source): | ||
4082 | 1129 | |||
4083 | 1130 | # 2-tuples of factory, description | ||
4084 | 1131 | srvfacts = self.service.getLocalFactories(From, to, protocol) | ||
4085 | 1132 | |||
4086 | 1133 | result = [] # list of listener dicts | ||
4087 | 1134 | |||
4088 | 1135 | if srvfacts: | ||
4089 | 1136 | log.msg("local factories found for inbound request: %r" % (srvfacts,)) | ||
4090 | 1137 | localMethods = [] | ||
4091 | 1138 | publicIP = self._determinePublicIP() | ||
4092 | 1139 | privateIP = self._determinePrivateIP() | ||
4093 | 1140 | if self.service.inboundTCPPort is not None: | ||
4094 | 1141 | tcpPort = self.service.inboundTCPPort.getHost().port | ||
4095 | 1142 | localMethods.append(TCPMethod( | ||
4096 | 1143 | '%s:%d' % | ||
4097 | 1144 | (publicIP, tcpPort))) | ||
4098 | 1145 | if publicIP != privateIP: | ||
4099 | 1146 | localMethods.append(TCPMethod( | ||
4100 | 1147 | '%s:%d' % | ||
4101 | 1148 | (privateIP, tcpPort))) | ||
4102 | 1149 | |||
4103 | 1150 | if not self.service.udpEnabled: | ||
4104 | 1151 | log.msg("udp not enabled -- but I so want to send udp traffic!") | ||
4105 | 1152 | elif udp_source is None: | ||
4106 | 1153 | log.msg("udp_source was none on inbound") | ||
4107 | 1154 | else: | ||
4108 | 1155 | if self.service.dispatcher is None: | ||
4109 | 1156 | log.msg("udp_source %s:%d, but dispatcher not running" % | ||
4110 | 1157 | udp_source) | ||
4111 | 1158 | else: | ||
4112 | 1159 | remoteUDPHost, remoteUDPPort = udp_source | ||
4113 | 1160 | log.msg( | ||
4114 | 1161 | "remote PTCP: %s:%d, " | ||
4115 | 1162 | "local public IP: %s, local private IP: %s" | ||
4116 | 1163 | % (remoteUDPHost, remoteUDPPort, publicIP, privateIP) ) | ||
4117 | 1164 | |||
4118 | 1165 | # Seed my NAT from my shared UDP port | ||
4119 | 1166 | udpPort = self.service.dispatcher.seedNAT(udp_source, self.service.sharedUDPPortnum) | ||
4120 | 1167 | |||
4121 | 1168 | if remoteUDPHost == publicIP and publicIP != privateIP: | ||
4122 | 1169 | log.msg( | ||
4123 | 1170 | "Remote IP matches local, public IP %r;" | ||
4124 | 1171 | " preferring internal IP %r" % (publicIP, privateIP)) | ||
4125 | 1172 | localMethods.append( | ||
4126 | 1173 | PTCPMethod("%s:%d" % (privateIP, udpPort))) | ||
4127 | 1174 | localMethods.append( | ||
4128 | 1175 | PTCPMethod("%s:%d" % (publicIP, udpPort))) | ||
4129 | 1176 | |||
4130 | 1177 | # XXX CLEANUP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
4131 | 1178 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
4132 | 1179 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
4133 | 1180 | privateUDPPort = self.service.dispatcher.seedNAT(udp_source) | ||
4134 | 1181 | localMethods.append( | ||
4135 | 1182 | PTCPMethod('%s:%d' % (publicIP, privateUDPPort))) | ||
4136 | 1183 | |||
4137 | 1184 | udpxPort = self.service.dispatcher.seedNAT(udp_source) | ||
4138 | 1185 | localMethods.append( | ||
4139 | 1186 | RPTCPMethod("%s:%d" % (publicIP, udpxPort))) | ||
4140 | 1187 | |||
4141 | 1188 | if self.service.virtualEnabled: | ||
4142 | 1189 | localMethods.append(VirtualMethod()) | ||
4143 | 1190 | log.msg('responding to inbound with local methods: %r' % (localMethods,)) | ||
4144 | 1191 | |||
4145 | 1192 | for serverFactory, description in srvfacts: | ||
4146 | 1193 | expiryTime, listenID = self.service.mapListener( | ||
4147 | 1194 | to, From, protocol, serverFactory) | ||
4148 | 1195 | result.append(dict(id=listenID, | ||
4149 | 1196 | expires=expiryTime, | ||
4150 | 1197 | methods=localMethods, | ||
4151 | 1198 | description=description)) | ||
4152 | 1199 | |||
4153 | 1200 | # We've looked for our local factory. Let's see if we have any | ||
4154 | 1201 | # listening protocols elsewhere. | ||
4155 | 1202 | |||
4156 | 1203 | |||
4157 | 1204 | key = (to, protocol) | ||
4158 | 1205 | if key in self.service.listeningClients: | ||
4159 | 1206 | args = dict(From=From, | ||
4160 | 1207 | to=to, | ||
4161 | 1208 | protocol=protocol, | ||
4162 | 1209 | udp_source=udp_source) | ||
4163 | 1210 | DL = [] | ||
4164 | 1211 | lclients = self.service.listeningClients[key] | ||
4165 | 1212 | log.msg("listeners found for %s:%r" % (to, protocol)) | ||
4166 | 1213 | for listener, listenCert, desc in lclients: | ||
4167 | 1214 | log.msg("relaying inbound to %r via %r" % (to, listener)) | ||
4168 | 1215 | DL.append(listener.callRemote(Inbound, **args).addCallback( | ||
4169 | 1216 | self._massageClientInboundResponse, listener, result)) | ||
4170 | 1217 | |||
4171 | 1218 | def allListenerResponses(x): | ||
4172 | 1219 | log.msg("all inbound responses received: %s" % (pformat(result),)) | ||
4173 | 1220 | return dict(listeners=result) | ||
4174 | 1221 | return defer.DeferredList(DL).addCallback(allListenerResponses) | ||
4175 | 1222 | else: | ||
4176 | 1223 | log.msg("no listenening clients for %s:%r. local methods: %r" % (to,protocol, result)) | ||
4177 | 1224 | return dict(listeners=result) | ||
4178 | 1225 | |||
4179 | 1226 | |||
4180 | 1227 | def _massageClientInboundResponse(self, inboundResponse, listener, result): | ||
4181 | 1228 | irl = inboundResponse['listeners'] | ||
4182 | 1229 | log.msg("received relayed inbound response: %r via %r" % | ||
4183 | 1230 | (inboundResponse, listener)) | ||
4184 | 1231 | |||
4185 | 1232 | for listenerInfo in irl: | ||
4186 | 1233 | # inboundResponse['description'] = ??? trust client version for | ||
4187 | 1234 | # now... maybe the server doesn't even need to know about | ||
4188 | 1235 | # descriptions...? | ||
4189 | 1236 | listenerInfo['methods'] = [ | ||
4190 | 1237 | meth for meth in listenerInfo['methods'] if meth.relayable] | ||
4191 | 1238 | # make sure that the certificate that we're relaying matches the | ||
4192 | 1239 | # certificate that they gave us! | ||
4193 | 1240 | if listenerInfo['methods']: | ||
4194 | 1241 | allowedCertificate = Certificate.peerFromTransport( | ||
4195 | 1242 | listener.transport) | ||
4196 | 1243 | listenerInfo['certificate'] = allowedCertificate | ||
4197 | 1244 | result.append(listenerInfo) | ||
4198 | 1245 | |||
4199 | 1246 | def _determinePublicIP(self): | ||
4200 | 1247 | reservePublicIP = None | ||
4201 | 1248 | if self.service.publicIP is not None: | ||
4202 | 1249 | if self.service._publicIPIsReallyPrivate: | ||
4203 | 1250 | reservePublicIP = self.service.publicIP | ||
4204 | 1251 | else: | ||
4205 | 1252 | return self.service.publicIP | ||
4206 | 1253 | if self.publicIP is not None: | ||
4207 | 1254 | return self.publicIP | ||
4208 | 1255 | if reservePublicIP is not None: | ||
4209 | 1256 | return reservePublicIP | ||
4210 | 1257 | return self._determinePrivateIP() | ||
4211 | 1258 | |||
4212 | 1259 | def _determinePrivateIP(self): | ||
4213 | 1260 | return self.transport.getHost().host | ||
4214 | 1261 | |||
4215 | 1262 | def _sourceIP(self): | ||
4216 | 1263 | result = {'ip': self.transport.getPeer().host} | ||
4217 | 1264 | return result | ||
4218 | 1265 | SourceIP.responder(_sourceIP) | ||
4219 | 1266 | |||
4220 | 1267 | def _resume(self, connection, data, writeDeferred): | ||
4221 | 1268 | try: | ||
4222 | 1269 | connection.dataReceived(data) | ||
4223 | 1270 | except: | ||
4224 | 1271 | writeDeferred.errback() | ||
4225 | 1272 | else: | ||
4226 | 1273 | writeDeferred.callback({}) | ||
4227 | 1274 | |||
4228 | 1275 | |||
4229 | 1276 | def _choke(self, id): | ||
4230 | 1277 | connection = self.connections[id] | ||
4231 | 1278 | connection.choke() | ||
4232 | 1279 | return {} | ||
4233 | 1280 | Choke.responder(_choke) | ||
4234 | 1281 | |||
4235 | 1282 | |||
4236 | 1283 | def _unchoke(self, id): | ||
4237 | 1284 | connection = self.connections[id] | ||
4238 | 1285 | connection.unchoke() | ||
4239 | 1286 | return {} | ||
4240 | 1287 | Unchoke.responder(_unchoke) | ||
4241 | 1288 | |||
4242 | 1289 | |||
4243 | 1290 | def amp_WRITE(self, box): | ||
4244 | 1291 | """ | ||
4245 | 1292 | Respond to a WRITE command, sending some data over a virtual channel | ||
4246 | 1293 | created by VIRTUAL. The answer is simply an acknowledgement, as it is | ||
4247 | 1294 | simply meant to note that the write went through without errors. | ||
4248 | 1295 | |||
4249 | 1296 | An occurrence of I{Write} on the wire, together with the response | ||
4250 | 1297 | generated by this method, might have this apperance:: | ||
4251 | 1298 | |||
4252 | 1299 | C: -Command: Write | ||
4253 | 1300 | C: -Ask: 1 | ||
4254 | 1301 | C: -Length: 13 | ||
4255 | 1302 | C: Id: glyph@divmod.com->radix@twistedmatrix.com:q2q-example:0 | ||
4256 | 1303 | C: | ||
4257 | 1304 | C: HELLO WORLD | ||
4258 | 1305 | C: | ||
4259 | 1306 | S: -Answer: 1 | ||
4260 | 1307 | S: | ||
4261 | 1308 | |||
4262 | 1309 | """ | ||
4263 | 1310 | id = int(box['id']) | ||
4264 | 1311 | if id not in self.connections: | ||
4265 | 1312 | raise error.ConnectionDone() | ||
4266 | 1313 | connection = self.connections[id] | ||
4267 | 1314 | data = box['body'] | ||
4268 | 1315 | connection.dataReceived(data) | ||
4269 | 1316 | return AmpBox() | ||
4270 | 1317 | |||
4271 | 1318 | def amp_CLOSE(self, box): | ||
4272 | 1319 | """ | ||
4273 | 1320 | Respond to a CLOSE command, dumping some data onto the stream. As with | ||
4274 | 1321 | WRITE, this returns an empty acknowledgement. | ||
4275 | 1322 | |||
4276 | 1323 | An occurrence of I{Close} on the wire, together with the response | ||
4277 | 1324 | generated by this method, might have this apperance:: | ||
4278 | 1325 | |||
4279 | 1326 | C: -Command: Close | ||
4280 | 1327 | C: -Ask: 1 | ||
4281 | 1328 | C: Id: glyph@divmod.com->radix@twistedmatrix.com:q2q-example:0 | ||
4282 | 1329 | C: | ||
4283 | 1330 | S: -Answer: 1 | ||
4284 | 1331 | S: | ||
4285 | 1332 | |||
4286 | 1333 | """ | ||
4287 | 1334 | # The connection is removed from the mapping by connectionLost. | ||
4288 | 1335 | connection = self.connections[int(box['id'])] | ||
4289 | 1336 | connection.connectionLost(Failure(CONNECTION_DONE)) | ||
4290 | 1337 | return AmpBox() | ||
4291 | 1338 | |||
4292 | 1339 | |||
4293 | 1340 | def _sign(self, certificate_request, password): | ||
4294 | 1341 | """ | ||
4295 | 1342 | Respond to a request to sign a CSR for a user or agent located within | ||
4296 | 1343 | our domain. | ||
4297 | 1344 | """ | ||
4298 | 1345 | if self.service.portal is None: | ||
4299 | 1346 | raise BadCertificateRequest("This agent cannot sign certificates.") | ||
4300 | 1347 | |||
4301 | 1348 | subj = certificate_request.getSubject() | ||
4302 | 1349 | |||
4303 | 1350 | sk = subj.keys() | ||
4304 | 1351 | if 'commonName' not in sk: | ||
4305 | 1352 | raise BadCertificateRequest( | ||
4306 | 1353 | "Certificate requested with bad subject: %s" % (sk,)) | ||
4307 | 1354 | |||
4308 | 1355 | uandd = subj.commonName.split("@") | ||
4309 | 1356 | if len(uandd) != 2: | ||
4310 | 1357 | raise BadCertificateRequest("Won't sign certificates for other domains") | ||
4311 | 1358 | domain = uandd[1] | ||
4312 | 1359 | |||
4313 | 1360 | CS = self.service.certificateStorage | ||
4314 | 1361 | ourCert = CS.getPrivateCertificate(domain) | ||
4315 | 1362 | |||
4316 | 1363 | D = self.service.portal.login( | ||
4317 | 1364 | UsernamePassword(subj.commonName, | ||
4318 | 1365 | password), | ||
4319 | 1366 | self, | ||
4320 | 1367 | ivertex.IQ2QUser) | ||
4321 | 1368 | |||
4322 | 1369 | def _(ial): | ||
4323 | 1370 | (iface, aspect, logout) = ial | ||
4324 | 1371 | ser = CS.genSerial(domain) | ||
4325 | 1372 | return dict(certificate=aspect.signCertificateRequest( | ||
4326 | 1373 | certificate_request, ourCert, ser)) | ||
4327 | 1374 | |||
4328 | 1375 | return D.addCallback(_) | ||
4329 | 1376 | Sign.responder(_sign) | ||
4330 | 1377 | |||
4331 | 1378 | |||
4332 | 1379 | def _secure(self, to, From, authorize): | ||
4333 | 1380 | """ | ||
4334 | 1381 | Response to a SECURE command, starting TLS when necessary, and using a | ||
4335 | 1382 | certificate identified by the I{To} header. | ||
4336 | 1383 | |||
4337 | 1384 | An occurrence of I{Secure} on the wire, together with the response | ||
4338 | 1385 | generated by this method, might have the following appearance:: | ||
4339 | 1386 | |||
4340 | 1387 | C: -Command: Secure | ||
4341 | 1388 | C: -Ask: 1 | ||
4342 | 1389 | C: To: divmod.com | ||
4343 | 1390 | C: From: twistedmatrix.com | ||
4344 | 1391 | C: Authorize: True | ||
4345 | 1392 | C: | ||
4346 | 1393 | Client Starts TLS here with twistedmatrix.com certificate | ||
4347 | 1394 | S: -Answer: 1 | ||
4348 | 1395 | S: | ||
4349 | 1396 | Server Starts TLS here with divmod.com certificate | ||
4350 | 1397 | |||
4351 | 1398 | """ | ||
4352 | 1399 | if self.hostCertificate is not None: | ||
4353 | 1400 | raise RuntimeError("Re-encrypting already encrypted connection") | ||
4354 | 1401 | CS = self.service.certificateStorage | ||
4355 | 1402 | ourCert = CS.getPrivateCertificate(str(to.domainAddress())) | ||
4356 | 1403 | if authorize: | ||
4357 | 1404 | D = CS.getSelfSignedCertificate(str(From.domainAddress())) | ||
4358 | 1405 | else: | ||
4359 | 1406 | self.authorized = False | ||
4360 | 1407 | return {'tls_localCertificate': ourCert} | ||
4361 | 1408 | |||
4362 | 1409 | def hadCert(peerSigned): | ||
4363 | 1410 | self.authorized = True | ||
4364 | 1411 | self._cacheMeNow(From, to, authorize) | ||
4365 | 1412 | return {'tls_localCertificate': ourCert, | ||
4366 | 1413 | 'tls_verifyAuthorities': [peerSigned]} | ||
4367 | 1414 | |||
4368 | 1415 | def didNotHaveCert(err): | ||
4369 | 1416 | err.trap(KeyError) | ||
4370 | 1417 | return self._retrieveRemoteCertificate(From, port) | ||
4371 | 1418 | |||
4372 | 1419 | D.addErrback(didNotHaveCert) | ||
4373 | 1420 | D.addCallback(hadCert) | ||
4374 | 1421 | |||
4375 | 1422 | return D | ||
4376 | 1423 | Secure.responder(_secure) | ||
4377 | 1424 | |||
4378 | 1425 | _cachedUnrequested = False | ||
4379 | 1426 | |||
4380 | 1427 | def _cacheMeNow(self, From, to, authorize): | ||
4381 | 1428 | tcpeer = self.transport.getPeer() | ||
4382 | 1429 | # XXX 'port' is insane here, but we lack a better number to hash | ||
4383 | 1430 | # against. perhaps the SECURE request should give a reciprocal | ||
4384 | 1431 | # connection identifier...? | ||
4385 | 1432 | self.service.secureConnectionCache.cacheUnrequested( | ||
4386 | 1433 | endpoint.TCPEndpoint(tcpeer.host, port), | ||
4387 | 1434 | (From, to.domain, authorize), self) | ||
4388 | 1435 | assert not self._cachedUnrequested | ||
4389 | 1436 | self._cachedUnrequested = (From, to, authorize, tcpeer) | ||
4390 | 1437 | |||
4391 | 1438 | def _uncacheMe(self): | ||
4392 | 1439 | if self._cachedUnrequested: | ||
4393 | 1440 | # If this is a client connection, this will never be called, since | ||
4394 | 1441 | # _cacheMeNow is called from the _server_ half of this business. | ||
4395 | 1442 | # The uncaching API here is a bit of a ragged edge of conncache.py; | ||
4396 | 1443 | # the interface should probably be cleaned up, but I don't think | ||
4397 | 1444 | # there are any functional problems with it. | ||
4398 | 1445 | From, to, authorize, tcpeer = self._cachedUnrequested | ||
4399 | 1446 | self.service.secureConnectionCache.connectionLostForKey( | ||
4400 | 1447 | (endpoint.TCPEndpoint(tcpeer.host, port), | ||
4401 | 1448 | (From, to.domain, authorize))) | ||
4402 | 1449 | |||
4403 | 1450 | def _retrieveRemoteCertificate(self, From, port=port): | ||
4404 | 1451 | """ | ||
4405 | 1452 | The entire conversation, starting with TCP handshake and ending at | ||
4406 | 1453 | disconnect, to retrieve a foreign domain's certificate for the first | ||
4407 | 1454 | time. | ||
4408 | 1455 | """ | ||
4409 | 1456 | CS = self.service.certificateStorage | ||
4410 | 1457 | host = str(From.domainAddress()) | ||
4411 | 1458 | p = AMP() | ||
4412 | 1459 | p.wrapper = self.wrapper | ||
4413 | 1460 | f = protocol.ClientCreator(reactor, lambda: p) | ||
4414 | 1461 | connD = f.connectTCP(host, port) | ||
4415 | 1462 | |||
4416 | 1463 | def connected(proto): | ||
4417 | 1464 | dhost = From.domainAddress() | ||
4418 | 1465 | iddom = proto.callRemote(Identify, subject=dhost) | ||
4419 | 1466 | def gotCert(identifyBox): | ||
4420 | 1467 | theirCert = identifyBox['certificate'] | ||
4421 | 1468 | theirIssuer = theirCert.getIssuer().commonName | ||
4422 | 1469 | theirName = theirCert.getSubject().commonName | ||
4423 | 1470 | if (theirName != str(dhost)): | ||
4424 | 1471 | raise VerifyError( | ||
4425 | 1472 | "%r claimed it was %r in IDENTIFY response" | ||
4426 | 1473 | % (theirName, dhost)) | ||
4427 | 1474 | if (theirIssuer != str(dhost)): | ||
4428 | 1475 | raise VerifyError( | ||
4429 | 1476 | "self-signed %r claimed it was issued by " | ||
4430 | 1477 | "%r in IDENTIFY response" % (dhost, theirIssuer)) | ||
4431 | 1478 | def storedCert(ignored): | ||
4432 | 1479 | return theirCert | ||
4433 | 1480 | return CS.storeSelfSignedCertificate( | ||
4434 | 1481 | str(dhost), theirCert).addCallback(storedCert) | ||
4435 | 1482 | def nothingify(x): | ||
4436 | 1483 | proto.transport.loseConnection() | ||
4437 | 1484 | return x | ||
4438 | 1485 | return iddom.addCallback(gotCert).addBoth(nothingify) | ||
4439 | 1486 | connD.addCallback(connected) | ||
4440 | 1487 | return connD | ||
4441 | 1488 | |||
4442 | 1489 | |||
4443 | 1490 | def secure(self, fromAddress, toAddress, | ||
4444 | 1491 | fromCertificate, foreignCertificateAuthority=None, | ||
4445 | 1492 | authorize=True): | ||
4446 | 1493 | """Return a Deferred which fires True when this connection has been secured as | ||
4447 | 1494 | a channel between fromAddress (locally) and toAddress (remotely). | ||
4448 | 1495 | Raises an error if this is not possible. | ||
4449 | 1496 | """ | ||
4450 | 1497 | if self.hostCertificate is not None: | ||
4451 | 1498 | raise RuntimeError("Re-securing already secured connection.") | ||
4452 | 1499 | |||
4453 | 1500 | def _cbSecure(response): | ||
4454 | 1501 | if foreignCertificateAuthority is not None: | ||
4455 | 1502 | self.authorized = True | ||
4456 | 1503 | return True | ||
4457 | 1504 | extra = {'tls_localCertificate': fromCertificate} | ||
4458 | 1505 | if foreignCertificateAuthority is not None: | ||
4459 | 1506 | extra['tls_verifyAuthorities'] = [foreignCertificateAuthority] | ||
4460 | 1507 | |||
4461 | 1508 | return self.callRemote( | ||
4462 | 1509 | Secure, | ||
4463 | 1510 | From=fromAddress, | ||
4464 | 1511 | to=toAddress, | ||
4465 | 1512 | authorize=authorize, **extra).addCallback(_cbSecure) | ||
4466 | 1513 | |||
4467 | 1514 | def _virtual(self, id): | ||
4468 | 1515 | if self.isServer: | ||
4469 | 1516 | assert id > 0 | ||
4470 | 1517 | else: | ||
4471 | 1518 | assert id < 0 | ||
4472 | 1519 | # We are double-deferring here so that we only start writing data to | ||
4473 | 1520 | # our client _after_ they have processed our ACK. | ||
4474 | 1521 | tpt = VirtualTransport(self, id, self.service._bootstrapFactory, False) | ||
4475 | 1522 | |||
4476 | 1523 | |||
4477 | 1524 | return dict(__transport__=tpt) | ||
4478 | 1525 | |||
4479 | 1526 | Virtual.responder(_virtual) | ||
4480 | 1527 | |||
4481 | 1528 | |||
4482 | 1529 | # Client/Support methods. | ||
4483 | 1530 | |||
4484 | 1531 | def attemptConnectionMethods(self, methods, connectionID, From, to, | ||
4485 | 1532 | protocolName, protocolFactory): | ||
4486 | 1533 | attemptObjects = [] | ||
4487 | 1534 | for meth in methods: | ||
4488 | 1535 | atts = meth.attempt(self, connectionID, From, to, | ||
4489 | 1536 | protocolName, protocolFactory) | ||
4490 | 1537 | attemptObjects.extend(atts) | ||
4491 | 1538 | |||
4492 | 1539 | attemptDeferreds = [att.startAttempt() for att in attemptObjects] | ||
4493 | 1540 | |||
4494 | 1541 | d = defer.DeferredList(attemptDeferreds, | ||
4495 | 1542 | fireOnOneCallback=True, | ||
4496 | 1543 | fireOnOneErrback=False) | ||
4497 | 1544 | def dontLogThat(e): | ||
4498 | 1545 | e.trap(error.ConnectionLost, error.ConnectionDone) | ||
4499 | 1546 | |||
4500 | 1547 | for attDef in attemptDeferreds: | ||
4501 | 1548 | attDef.addErrback(dontLogThat) | ||
4502 | 1549 | |||
4503 | 1550 | def _unfortunate_defer_hack(results): | ||
4504 | 1551 | #Do you see what you've made me become? | ||
4505 | 1552 | if isinstance(results, tuple): | ||
4506 | 1553 | stuff = [(False, None)] * len(attemptObjects) | ||
4507 | 1554 | stuff[results[1]] = (True, results[0]) | ||
4508 | 1555 | return stuff | ||
4509 | 1556 | return results | ||
4510 | 1557 | |||
4511 | 1558 | |||
4512 | 1559 | def gotResults(results): | ||
4513 | 1560 | theResult = None | ||
4514 | 1561 | anyResult = False | ||
4515 | 1562 | for index, (success, result) in enumerate(results): | ||
4516 | 1563 | if success: | ||
4517 | 1564 | # woohoo! home free. | ||
4518 | 1565 | # XXX Cancel outstanding attempts, maybe. They'll fail anyway, | ||
4519 | 1566 | # because the factory will return None from buildProtocol(). | ||
4520 | 1567 | theResult = result | ||
4521 | 1568 | anyResult = True | ||
4522 | 1569 | else: | ||
4523 | 1570 | attemptObjects[index].cancel() | ||
4524 | 1571 | if anyResult: | ||
4525 | 1572 | # theResult will be a SeparateConnectionTransport | ||
4526 | 1573 | return theResult.subProtocol | ||
4527 | 1574 | else: | ||
4528 | 1575 | reason = Failure(AttemptsFailed([fobj for (f, fobj) in results])) | ||
4529 | 1576 | return reason | ||
4530 | 1577 | |||
4531 | 1578 | d.addCallback(_unfortunate_defer_hack) | ||
4532 | 1579 | d.addCallback(gotResults) | ||
4533 | 1580 | return d | ||
4534 | 1581 | |||
4535 | 1582 | |||
4536 | 1583 | def listen(self, fromAddress, protocols, serverDescription): | ||
4537 | 1584 | return self.callRemote( | ||
4538 | 1585 | Listen, From=fromAddress, | ||
4539 | 1586 | protocols=protocols, description=serverDescription) | ||
4540 | 1587 | |||
4541 | 1588 | |||
4542 | 1589 | def connect(self, From, to, | ||
4543 | 1590 | protocolName, clientFactory, | ||
4544 | 1591 | chooser): | ||
4545 | 1592 | """ | ||
4546 | 1593 | Issue an INBOUND command, creating a virtual connection to the peer, | ||
4547 | 1594 | given identifying information about the endpoint to connect to, and a | ||
4548 | 1595 | protocol factory. | ||
4549 | 1596 | |||
4550 | 1597 | @param clientFactory: a *Client* ProtocolFactory instance which will | ||
4551 | 1598 | generate a protocol upon connect. | ||
4552 | 1599 | |||
4553 | 1600 | @return: a Deferred which fires with the protocol instance that was | ||
4554 | 1601 | connected, or fails with AttemptsFailed if the connection was not | ||
4555 | 1602 | possible. | ||
4556 | 1603 | """ | ||
4557 | 1604 | |||
4558 | 1605 | publicIP = self._determinePublicIP() | ||
4559 | 1606 | |||
4560 | 1607 | A = dict(From=From, | ||
4561 | 1608 | to=to, | ||
4562 | 1609 | protocol=protocolName) | ||
4563 | 1610 | |||
4564 | 1611 | if self.service.dispatcher is not None: | ||
4565 | 1612 | # tell them exactly where they can shove it | ||
4566 | 1613 | A['udp_source'] = (publicIP, | ||
4567 | 1614 | self.service.sharedUDPPortnum) | ||
4568 | 1615 | else: | ||
4569 | 1616 | # don't tell them because we don't know | ||
4570 | 1617 | log.msg("dispatcher unavailable when connecting") | ||
4571 | 1618 | |||
4572 | 1619 | D = self.callRemote(Inbound, **A) | ||
4573 | 1620 | |||
4574 | 1621 | def _connected(answer): | ||
4575 | 1622 | listenersD = defer.maybeDeferred(chooser, answer['listeners']) | ||
4576 | 1623 | def gotListeners(listeners): | ||
4577 | 1624 | allConnectionAttempts = [] | ||
4578 | 1625 | for listener in listeners: | ||
4579 | 1626 | d = self.attemptConnectionMethods( | ||
4580 | 1627 | listener['methods'], | ||
4581 | 1628 | listener['id'], | ||
4582 | 1629 | From, to, | ||
4583 | 1630 | protocolName, clientFactory, | ||
4584 | 1631 | ) | ||
4585 | 1632 | allConnectionAttempts.append(d) | ||
4586 | 1633 | return defer.DeferredList(allConnectionAttempts) | ||
4587 | 1634 | listenersD.addCallback(gotListeners) | ||
4588 | 1635 | def finishedAllAttempts(results): | ||
4589 | 1636 | succeededAny = False | ||
4590 | 1637 | failures = [] | ||
4591 | 1638 | if not results: | ||
4592 | 1639 | return Failure(NoAttemptsMade( | ||
4593 | 1640 | "there was no available path for connections " | ||
4594 | 1641 | "(%r->%r/%s)" % (From, to, protocolName))) | ||
4595 | 1642 | for succeeded, result in results: | ||
4596 | 1643 | if succeeded: | ||
4597 | 1644 | succeededAny = True | ||
4598 | 1645 | randomConnection = result | ||
4599 | 1646 | break | ||
4600 | 1647 | else: | ||
4601 | 1648 | failures.append(result) | ||
4602 | 1649 | if not succeededAny: | ||
4603 | 1650 | return Failure(AttemptsFailed( | ||
4604 | 1651 | [failure.getBriefTraceback() for failure in failures])) | ||
4605 | 1652 | |||
4606 | 1653 | # XXX TODO: this connection is really random; connectQ2Q should | ||
4607 | 1654 | # not return one of the connections it's made, put it into your | ||
4608 | 1655 | # protocol's connectionMade handler | ||
4609 | 1656 | |||
4610 | 1657 | return randomConnection | ||
4611 | 1658 | |||
4612 | 1659 | return listenersD.addCallback(finishedAllAttempts) | ||
4613 | 1660 | return D.addCallback(_connected) | ||
4614 | 1661 | |||
4615 | 1662 | |||
4616 | 1663 | class SeparateConnectionTransport(object): | ||
4617 | 1664 | def __init__(self, | ||
4618 | 1665 | service, | ||
4619 | 1666 | subProtocol, | ||
4620 | 1667 | q2qhost, | ||
4621 | 1668 | q2qpeer, | ||
4622 | 1669 | protocolName, | ||
4623 | 1670 | connectionEstablishedDeferred=None): | ||
4624 | 1671 | self.service = service | ||
4625 | 1672 | self.subProtocol = subProtocol | ||
4626 | 1673 | self.q2qhost = q2qhost | ||
4627 | 1674 | self.q2qpeer = q2qpeer | ||
4628 | 1675 | self.protocolName = protocolName | ||
4629 | 1676 | self.connectionEstablishedDeferred = connectionEstablishedDeferred | ||
4630 | 1677 | |||
4631 | 1678 | subProtocol = None | ||
4632 | 1679 | q2qhost = None | ||
4633 | 1680 | q2qpeer = None | ||
4634 | 1681 | protocolName = 'unknown' | ||
4635 | 1682 | |||
4636 | 1683 | # ITransport | ||
4637 | 1684 | disconnecting = property(lambda self: self.transport.disconnecting) | ||
4638 | 1685 | |||
4639 | 1686 | # IQ2QTransport | ||
4640 | 1687 | |||
4641 | 1688 | def getQ2QHost(self): | ||
4642 | 1689 | return self.q2qhost | ||
4643 | 1690 | |||
4644 | 1691 | def getQ2QPeer(self): | ||
4645 | 1692 | return self.q2qpeer | ||
4646 | 1693 | |||
4647 | 1694 | def makeConnection(self, tpt): | ||
4648 | 1695 | self.transport = tpt | ||
4649 | 1696 | self.service.subConnections.append(self) | ||
4650 | 1697 | self.subProtocol.makeConnection(self) | ||
4651 | 1698 | if self.connectionEstablishedDeferred is not None: | ||
4652 | 1699 | self.connectionEstablishedDeferred.callback(self) | ||
4653 | 1700 | |||
4654 | 1701 | def getPeer(self): | ||
4655 | 1702 | return Q2QTransportAddress(self.getQ2QPeer(), | ||
4656 | 1703 | self.transport.getPeer(), | ||
4657 | 1704 | self.protocolName) | ||
4658 | 1705 | |||
4659 | 1706 | def getHost(self): | ||
4660 | 1707 | return Q2QTransportAddress(self.getQ2QHost(), | ||
4661 | 1708 | self.transport.getHost(), | ||
4662 | 1709 | self.protocolName) | ||
4663 | 1710 | |||
4664 | 1711 | def dataReceived(self, data): | ||
4665 | 1712 | self.subProtocol.dataReceived(data) | ||
4666 | 1713 | |||
4667 | 1714 | def write(self, data): | ||
4668 | 1715 | self.transport.write(data) | ||
4669 | 1716 | |||
4670 | 1717 | def writeSequence(self, data): | ||
4671 | 1718 | self.transport.writeSequence(data) | ||
4672 | 1719 | |||
4673 | 1720 | def registerProducer(self, producer, streaming): | ||
4674 | 1721 | self.transport.registerProducer(producer, streaming) | ||
4675 | 1722 | |||
4676 | 1723 | def unregisterProducer(self): | ||
4677 | 1724 | self.transport.unregisterProducer() | ||
4678 | 1725 | |||
4679 | 1726 | def loseConnection(self): | ||
4680 | 1727 | self.transport.loseConnection() | ||
4681 | 1728 | |||
4682 | 1729 | def connectionLost(self, reason): | ||
4683 | 1730 | self.service.subConnections.remove(self) | ||
4684 | 1731 | if self.subProtocol is not None: | ||
4685 | 1732 | self.subProtocol.connectionLost(reason) | ||
4686 | 1733 | self.subProtocol = None | ||
4687 | 1734 | |||
4688 | 1735 | class WhoAmI(Command): | ||
4689 | 1736 | commandName = 'Who-Am-I' | ||
4690 | 1737 | |||
4691 | 1738 | response = [ | ||
4692 | 1739 | ('address', HostPort()), | ||
4693 | 1740 | ] | ||
4694 | 1741 | |||
4695 | 1742 | class RetrieveConnection(ProtocolSwitchCommand): | ||
4696 | 1743 | commandName = 'Retrieve-Connection' | ||
4697 | 1744 | |||
4698 | 1745 | arguments = [ | ||
4699 | 1746 | ('identifier', String()), | ||
4700 | 1747 | ] | ||
4701 | 1748 | |||
4702 | 1749 | fatalErrors = {KeyError: "NoSuchConnection"} | ||
4703 | 1750 | |||
4704 | 1751 | class Q2QBootstrap(AMP): | ||
4705 | 1752 | def __init__(self, connIdentifier=None, protoFactory=None): | ||
4706 | 1753 | AMP.__init__(self) | ||
4707 | 1754 | assert connIdentifier is None or isinstance(connIdentifier, (str)) | ||
4708 | 1755 | self.connIdentifier = connIdentifier | ||
4709 | 1756 | self.protoFactory = protoFactory | ||
4710 | 1757 | |||
4711 | 1758 | def connectionMade(self): | ||
4712 | 1759 | if self.connIdentifier is not None: | ||
4713 | 1760 | def swallowKnown(err): | ||
4714 | 1761 | err.trap(error.ConnectionDone, KeyError) | ||
4715 | 1762 | self.retrieveConnection(self.connIdentifier, self.protoFactory).addErrback(swallowKnown) | ||
4716 | 1763 | |||
4717 | 1764 | def whoami(self): | ||
4718 | 1765 | """Return a Deferred which fires with a 2-tuple of (dotted quad ip, port | ||
4719 | 1766 | number). | ||
4720 | 1767 | """ | ||
4721 | 1768 | def cbWhoAmI(result): | ||
4722 | 1769 | return result['address'] | ||
4723 | 1770 | return self.callRemote(WhoAmI).addCallback(cbWhoAmI) | ||
4724 | 1771 | |||
4725 | 1772 | |||
4726 | 1773 | def _whoami(self): | ||
4727 | 1774 | peer = self.transport.getPeer() | ||
4728 | 1775 | return { | ||
4729 | 1776 | 'address': (peer.host, peer.port), | ||
4730 | 1777 | } | ||
4731 | 1778 | WhoAmI.responder(_whoami) | ||
4732 | 1779 | |||
4733 | 1780 | |||
4734 | 1781 | def retrieveConnection(self, identifier, factory): | ||
4735 | 1782 | return self.callRemote(RetrieveConnection, factory, identifier=identifier) | ||
4736 | 1783 | |||
4737 | 1784 | |||
4738 | 1785 | def _retrieveConnection(self, identifier): | ||
4739 | 1786 | listenerInfo = self.service.lookupListener(identifier) | ||
4740 | 1787 | if listenerInfo is None: | ||
4741 | 1788 | raise KeyError(identifier) | ||
4742 | 1789 | else: | ||
4743 | 1790 | proto = listenerInfo.protocolFactory.buildProtocol(listenerInfo.From) | ||
4744 | 1791 | return SeparateConnectionTransport( | ||
4745 | 1792 | self.service, | ||
4746 | 1793 | proto, | ||
4747 | 1794 | listenerInfo.to, | ||
4748 | 1795 | listenerInfo.From, | ||
4749 | 1796 | listenerInfo.protocolName) | ||
4750 | 1797 | |||
4751 | 1798 | RetrieveConnection.responder(_retrieveConnection) | ||
4752 | 1799 | |||
4753 | 1800 | |||
4754 | 1801 | |||
4755 | 1802 | class Q2QBootstrapFactory(protocol.Factory): | ||
4756 | 1803 | protocol = Q2QBootstrap | ||
4757 | 1804 | |||
4758 | 1805 | def __init__(self, service): | ||
4759 | 1806 | self.service = service | ||
4760 | 1807 | |||
4761 | 1808 | def buildProtocol(self, addr): | ||
4762 | 1809 | q2etc = protocol.Factory.buildProtocol(self, addr) | ||
4763 | 1810 | q2etc.service = self.service | ||
4764 | 1811 | return q2etc | ||
4765 | 1812 | |||
4766 | 1813 | class VirtualTransport(subproducer.SubProducer): | ||
4767 | 1814 | implements(interfaces.IProducer, interfaces.ITransport, interfaces.IConsumer) | ||
4768 | 1815 | disconnecting = False | ||
4769 | 1816 | |||
4770 | 1817 | def __init__(self, q2q, connectionID, protocolFactory, isClient): | ||
4771 | 1818 | """ | ||
4772 | 1819 | @param q2q: a Q2Q Protocol instance. | ||
4773 | 1820 | |||
4774 | 1821 | @param connectionID: an integer identifier, unique to the q2q instance | ||
4775 | 1822 | that I am wrapping (my underlying physical connection). | ||
4776 | 1823 | |||
4777 | 1824 | @param protocolFactory: an IProtocolFactory implementor which returns a | ||
4778 | 1825 | protocol instance for me to use. I'll use it to build the protocol, | ||
4779 | 1826 | and if the 'client' flag is True, also use it to notify | ||
4780 | 1827 | connectionLost/connectionFailed. | ||
4781 | 1828 | |||
4782 | 1829 | @param isClient: a boolean describing whether my protocol is the | ||
4783 | 1830 | initiating half of this connection or not. | ||
4784 | 1831 | """ | ||
4785 | 1832 | subproducer.SubProducer.__init__(self, q2q) | ||
4786 | 1833 | self.q2q = q2q | ||
4787 | 1834 | |||
4788 | 1835 | self.id = connectionID | ||
4789 | 1836 | self.isClient = isClient | ||
4790 | 1837 | self.q2q.connections[self.id] = self | ||
4791 | 1838 | self.protocolFactory = protocolFactory | ||
4792 | 1839 | |||
4793 | 1840 | protocol = None | ||
4794 | 1841 | |||
4795 | 1842 | def startProtocol(self): | ||
4796 | 1843 | self.protocol = self.protocolFactory.buildProtocol(self.getPeer()) | ||
4797 | 1844 | self.protocol.makeConnection(self) | ||
4798 | 1845 | return self.protocol | ||
4799 | 1846 | |||
4800 | 1847 | def pauseProducing(self): | ||
4801 | 1848 | self.q2q.callRemote(Choke, id=self.id) | ||
4802 | 1849 | |||
4803 | 1850 | def resumeProducing(self): | ||
4804 | 1851 | self.q2q.callRemote(Unchoke, id=self.id) | ||
4805 | 1852 | |||
4806 | 1853 | def writeSequence(self, iovec): | ||
4807 | 1854 | self.write(''.join(iovec)) | ||
4808 | 1855 | |||
4809 | 1856 | def loseConnection(self): | ||
4810 | 1857 | if self.disconnecting: | ||
4811 | 1858 | # print 'omg wtf loseConnection!???!' | ||
4812 | 1859 | return | ||
4813 | 1860 | self.disconnecting = True | ||
4814 | 1861 | d = self.q2q.callRemoteString('close', id=str(self.id)) | ||
4815 | 1862 | def cbClosed(ignored): | ||
4816 | 1863 | self.connectionLost(Failure(CONNECTION_DONE)) | ||
4817 | 1864 | def ebClosed(reason): | ||
4818 | 1865 | if self.id in self.q2q.connections: | ||
4819 | 1866 | self.connectionLost(reason) | ||
4820 | 1867 | elif not reason.check(error.ConnectionDone): | ||
4821 | 1868 | # Anything but a ConnectionDone (or similar things, perhaps) | ||
4822 | 1869 | # is fishy. Like an IndexError, that'd be wacko. But a | ||
4823 | 1870 | # ConnectionDone when self.id is already out of the Q2Q's | ||
4824 | 1871 | # connections mapping means the connection was closed after | ||
4825 | 1872 | # we thought it was supposed to be closed. No harm there. | ||
4826 | 1873 | log.err(reason, "Close virtual #%d failed" % (self.id,)) | ||
4827 | 1874 | d.addCallbacks(cbClosed, ebClosed) | ||
4828 | 1875 | |||
4829 | 1876 | |||
4830 | 1877 | def connectionLost(self, reason): | ||
4831 | 1878 | del self.q2q.connections[self.id] | ||
4832 | 1879 | if self.protocol is not None: | ||
4833 | 1880 | self.protocol.connectionLost(reason) | ||
4834 | 1881 | if self.isClient: | ||
4835 | 1882 | self.protocolFactory.clientConnectionLost(None, reason) | ||
4836 | 1883 | |||
4837 | 1884 | |||
4838 | 1885 | def dataReceived(self, data): | ||
4839 | 1886 | try: | ||
4840 | 1887 | self.protocol.dataReceived(data) | ||
4841 | 1888 | except: | ||
4842 | 1889 | # XXX: unconditionally logging errors from user code makes it hard | ||
4843 | 1890 | # to write tests, and is not always the right thing to do. we | ||
4844 | 1891 | # should revamp Twisted to have some kind of control over this | ||
4845 | 1892 | # behavior, and add that control back in to this code path as well | ||
4846 | 1893 | # (although logging exceptions from dataReceived is _by default_ | ||
4847 | 1894 | # certainly the right thing to do) --glyph+exarkun | ||
4848 | 1895 | reason = Failure() | ||
4849 | 1896 | log.err(reason) | ||
4850 | 1897 | self.connectionLost(reason) | ||
4851 | 1898 | |||
4852 | 1899 | def write(self, data): | ||
4853 | 1900 | self.q2q.callRemoteString( | ||
4854 | 1901 | 'write', False, body=data, id=str(self.id)) | ||
4855 | 1902 | |||
4856 | 1903 | def getHost(self): | ||
4857 | 1904 | return VirtualTransportAddress(self.q2q.transport.getHost()) | ||
4858 | 1905 | |||
4859 | 1906 | def getPeer(self): | ||
4860 | 1907 | return VirtualTransportAddress(self.q2q.transport.getPeer()) | ||
4861 | 1908 | |||
4862 | 1909 | |||
4863 | 1910 | _counter = 0 | ||
4864 | 1911 | def _nextJuiceLog(): | ||
4865 | 1912 | global _counter | ||
4866 | 1913 | try: | ||
4867 | 1914 | return str(_counter) | ||
4868 | 1915 | finally: | ||
4869 | 1916 | _counter = _counter + 1 | ||
4870 | 1917 | |||
4871 | 1918 | class DefaultQ2QAvatar: | ||
4872 | 1919 | implements(ivertex.IQ2QUser) | ||
4873 | 1920 | |||
4874 | 1921 | def __init__(self, username, domain): | ||
4875 | 1922 | self.username = username | ||
4876 | 1923 | self.domain = domain | ||
4877 | 1924 | |||
4878 | 1925 | def signCertificateRequest(self, certificateRequest, | ||
4879 | 1926 | domainCert, suggestedSerial): | ||
4880 | 1927 | keyz = certificateRequest.getSubject().keys() | ||
4881 | 1928 | if keyz != ['commonName']: | ||
4882 | 1929 | raise BadCertificateRequest( | ||
4883 | 1930 | "Don't know how to verify fields other than CN: " + | ||
4884 | 1931 | repr(keyz)) | ||
4885 | 1932 | newCert = domainCert.signRequestObject( | ||
4886 | 1933 | certificateRequest, | ||
4887 | 1934 | suggestedSerial) | ||
4888 | 1935 | log.msg('signing certificate for user %s@%s: %s' % ( | ||
4889 | 1936 | self.username, self.domain, newCert.digest())) | ||
4890 | 1937 | return newCert | ||
4891 | 1938 | |||
4892 | 1939 | |||
4893 | 1940 | |||
4894 | 1941 | class DefaultCertificateStore: | ||
4895 | 1942 | |||
4896 | 1943 | implements(ICredentialsChecker, IRealm) | ||
4897 | 1944 | |||
4898 | 1945 | credentialInterfaces = [IUsernamePassword] | ||
4899 | 1946 | |||
4900 | 1947 | def requestAvatar(self, avatarId, mind, interface): | ||
4901 | 1948 | assert interface is ivertex.IQ2QUser, ( | ||
4902 | 1949 | "default certificate store only supports one interface") | ||
4903 | 1950 | return interface, DefaultQ2QAvatar(*avatarId.split("@")), lambda : None | ||
4904 | 1951 | |||
4905 | 1952 | def requestAvatarId(self, credentials): | ||
4906 | 1953 | username, domain = credentials.username.split("@") | ||
4907 | 1954 | pw = self.users.get((domain, username)) | ||
4908 | 1955 | if pw is None: | ||
4909 | 1956 | return defer.fail(UnauthorizedLogin()) | ||
4910 | 1957 | def _(passwordIsCorrect): | ||
4911 | 1958 | if passwordIsCorrect: | ||
4912 | 1959 | return username + '@' + domain | ||
4913 | 1960 | else: | ||
4914 | 1961 | raise UnauthorizedLogin() | ||
4915 | 1962 | return defer.maybeDeferred( | ||
4916 | 1963 | credentials.checkPassword, pw).addCallback(_) | ||
4917 | 1964 | |||
4918 | 1965 | def __init__(self): | ||
4919 | 1966 | self.remoteStore = {} | ||
4920 | 1967 | self.localStore = {} | ||
4921 | 1968 | self.users = {} | ||
4922 | 1969 | |||
4923 | 1970 | def getSelfSignedCertificate(self, domainName): | ||
4924 | 1971 | return defer.maybeDeferred(self.remoteStore.__getitem__, domainName) | ||
4925 | 1972 | |||
4926 | 1973 | def addUser(self, domain, username, privateSecret): | ||
4927 | 1974 | self.users[domain, username] = privateSecret | ||
4928 | 1975 | |||
4929 | 1976 | def checkUser(self, domain, username, privateSecret): | ||
4930 | 1977 | if self.users.get((domain, username)) != privateSecret: | ||
4931 | 1978 | return defer.fail(KeyError()) | ||
4932 | 1979 | return defer.succeed(True) | ||
4933 | 1980 | |||
4934 | 1981 | def storeSelfSignedCertificate(self, domainName, mainCert): | ||
4935 | 1982 | """ | ||
4936 | 1983 | |||
4937 | 1984 | @return: a Deferred which will fire when the certificate has been | ||
4938 | 1985 | stored successfully. | ||
4939 | 1986 | """ | ||
4940 | 1987 | assert not isinstance(mainCert, str) | ||
4941 | 1988 | return defer.maybeDeferred(self.remoteStore.__setitem__, domainName, mainCert) | ||
4942 | 1989 | |||
4943 | 1990 | def getPrivateCertificate(self, domainName): | ||
4944 | 1991 | """ | ||
4945 | 1992 | |||
4946 | 1993 | @return: a PrivateCertificate instance, e.g. a certificate including a | ||
4947 | 1994 | private key, for 'domainName'. | ||
4948 | 1995 | """ | ||
4949 | 1996 | return self.localStore[domainName] | ||
4950 | 1997 | |||
4951 | 1998 | |||
4952 | 1999 | def genSerial(self, name): | ||
4953 | 2000 | return abs(struct.unpack('!i', md5(name).digest()[:4])[0]) | ||
4954 | 2001 | |||
4955 | 2002 | def addPrivateCertificate(self, subjectName, existingCertificate=None): | ||
4956 | 2003 | """ | ||
4957 | 2004 | Add a PrivateCertificate object to this store for this subjectName. | ||
4958 | 2005 | |||
4959 | 2006 | If existingCertificate is None, add a new self-signed certificate. | ||
4960 | 2007 | """ | ||
4961 | 2008 | if existingCertificate is None: | ||
4962 | 2009 | assert '@' not in subjectName, "Don't self-sign user certs!" | ||
4963 | 2010 | mainDN = DistinguishedName(commonName=subjectName) | ||
4964 | 2011 | mainKey = KeyPair.generate() | ||
4965 | 2012 | mainCertReq = mainKey.certificateRequest(mainDN) | ||
4966 | 2013 | mainCertData = mainKey.signCertificateRequest(mainDN, mainCertReq, | ||
4967 | 2014 | lambda dn: True, | ||
4968 | 2015 | self.genSerial(subjectName)) | ||
4969 | 2016 | mainCert = mainKey.newCertificate(mainCertData) | ||
4970 | 2017 | else: | ||
4971 | 2018 | mainCert = existingCertificate | ||
4972 | 2019 | self.localStore[subjectName] = mainCert | ||
4973 | 2020 | |||
4974 | 2021 | import os | ||
4975 | 2022 | |||
4976 | 2023 | class _pemmap(object): | ||
4977 | 2024 | def __init__(self, pathname, certclass): | ||
4978 | 2025 | self.pathname = pathname | ||
4979 | 2026 | try: | ||
4980 | 2027 | os.makedirs(pathname) | ||
4981 | 2028 | except (OSError, IOError): | ||
4982 | 2029 | pass | ||
4983 | 2030 | self.certclass = certclass | ||
4984 | 2031 | |||
4985 | 2032 | def file(self, name, mode): | ||
4986 | 2033 | try: | ||
4987 | 2034 | return file(os.path.join(self.pathname, name)+'.pem', mode) | ||
4988 | 2035 | except IOError, ioe: | ||
4989 | 2036 | raise KeyError(name, ioe) | ||
4990 | 2037 | |||
4991 | 2038 | def __setitem__(self, key, cert): | ||
4992 | 2039 | kn = cert.getSubject().commonName | ||
4993 | 2040 | assert kn == key | ||
4994 | 2041 | self.file(kn, 'wb').write(cert.dumpPEM()) | ||
4995 | 2042 | |||
4996 | 2043 | def __getitem__(self, cn): | ||
4997 | 2044 | return self.certclass.loadPEM(self.file(cn, 'rb').read()) | ||
4998 | 2045 | |||
4999 | 2046 | def iteritems(self): | ||
5000 | 2047 | files = os.listdir(self.pathname) |
The diff has been truncated for viewing.