-
Notifications
You must be signed in to change notification settings - Fork 1
/
dispersy.py
4530 lines (3649 loc) · 232 KB
/
dispersy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
The Distributed Permission System, or Dispersy, is a platform to simplify the design of distributed
communities. At the heart of Dispersy lies a simple identity and message handling system where each
community and each user is uniquely and securely identified using elliptic curve cryptography.
Since we can not guarantee each member to be online all the time, messages that they created at one
point in time should be able to retain their meaning even when the member is off-line. This can be
achieved by signing such messages and having them propagated though other nodes in the network.
Unfortunately, this increases the strain on these other nodes, which we try to alleviate using
specific message policies, which will be described below.
Following from this, we can easily package each message into one UDP packet to simplify
connect-ability problems since UDP packets are much easier to pass though NAT's and firewalls.
Earlier we hinted that messages can have different policies. A message has the following four
different policies, and each policy defines how a specific part of the message should be handled.
- Authentication defines if the message is signed, and if so, by how many members.
- Resolution defines how the permission system should resolve conflicts between messages.
- Distribution defines if the message is send once or if it should be gossiped around. In the
latter case, it can also define how many messages should be kept in the network.
- Destination defines to whom the message should be send or gossiped.
To ensure that every node handles a messages in the same way, i.e. has the same policies associated
to each message, a message exists in two stages. The meta-message and the implemented-message
stage. Each message has one meta-message associated to it and tells us how the message is supposed
to be handled. When a message is send or received an implementation is made from the meta-message
that contains information specifically for that message. For example: a meta-message could have the
member-authentication-policy that tells us that the message must be signed by a member but only the
an implemented-message will have data and this signature.
A community can tweak the policies and how they behave by changing the parameters that the policies
supply. Aside from the four policies, each meta-message also defines the community that it is part
of, the name it uses as an internal identifier, and the class that will contain the payload.
"""
import os
import sys
import netifaces
from hashlib import sha1
from itertools import groupby, islice, count, cycle
from random import random, shuffle
from socket import inet_aton, error as socket_error
from time import time
from .authentication import NoAuthentication, MemberAuthentication, DoubleMemberAuthentication
from .bloomfilter import BloomFilter
from .bootstrap import get_bootstrap_candidates
from .callback import Callback
from .candidate import BootstrapCandidate, LoopbackCandidate, WalkCandidate, Candidate
from .destination import CommunityDestination, CandidateDestination, MemberDestination
from .dispersydatabase import DispersyDatabase
from .distribution import SyncDistribution, FullSyncDistribution, LastSyncDistribution, DirectDistribution
from .dprint import dprint
from .endpoint import DummyEndpoint
from .member import DummyMember, Member, MemberFromId, MemberFromDatabaseId, MemberWithoutCheck
from .message import BatchConfiguration, Packet, Message
from .message import DropMessage, DelayMessage, DelayMessageByProof, DelayMessageBySequence, DelayMessageByMissingMessage
from .message import DropPacket, DelayPacket
from .payload import AuthorizePayload, RevokePayload, UndoPayload
from .payload import DestroyCommunityPayload
from .payload import DynamicSettingsPayload
from .payload import IdentityPayload, MissingIdentityPayload
from .payload import IntroductionRequestPayload, IntroductionResponsePayload, PunctureRequestPayload, PuncturePayload
from .payload import MissingMessagePayload, MissingLastMessagePayload
from .payload import MissingSequencePayload, MissingProofPayload
from .payload import SignatureRequestPayload, SignatureResponsePayload
from .requestcache import Cache, RequestCache
from .resolution import PublicResolution, LinearResolution
from .revision import update_revision_information
from .statistics import DispersyStatistics
from .singleton import Singleton
# update version information directly from SVN
update_revision_information("$HeadURL$", "$Revision$")
# the callback identifier for the task that periodically takes a step
CANDIDATE_WALKER_CALLBACK_ID = "dispersy-candidate-walker"
class SignatureRequestCache(Cache):
cleanup_delay = 0.0
def __init__(self, members, response_func, response_args, timeout):
self.request = None
# MEMBERS is a list containing all the members that should add their signature. currently
# we only support double signed messages, hence MEMBERS contains only a single Member
# instance.
self.members = members
self.response_func = response_func
self.response_args = response_args
self.timeout_delay = timeout
def on_timeout(self):
if __debug__: dprint("signature timeout")
self.response_func(self, None, True, *self.response_args)
class IntroductionRequestCache(Cache):
# we will accept the response at most 10.5 seconds after our request
timeout_delay = 10.5
# the cache remains available at most 4.5 after receiving the response. this gives some time to
# receive the puncture message
cleanup_delay = 4.5
def __init__(self, community, helper_candidate):
self.community = community
self.helper_candidate = helper_candidate
self.response_candidate = None
self.puncture_candidate = None
def on_timeout(self):
# helper_candidate did not respond to a request message in this community. after some time
# inactive candidates become obsolete and will be removed by
# _periodically_cleanup_candidates
if __debug__:
dprint("walker timeout for ", self.helper_candidate)
self.community.dispersy.statistics.dict_inc(self.community.dispersy.statistics.walk_fail, self.helper_candidate.sock_addr)
# we choose to set the entire helper to inactive instead of just the community where the
# timeout occurred. this will allow us to quickly respond to nodes going offline, while the
# downside is that one dropped packet will cause us to invalidly inactivate all communities
# of the candidate.
now = time()
self.helper_candidate.obsolete(self.community, now)
self.helper_candidate.all_inactive(now)
class MissingSomethingCache(Cache):
cleanup_delay = 0.0
def __init__(self, timeout):
if __debug__: dprint(self.__class__.__name__, ": waiting for ", timeout, " seconds")
self.timeout_delay = timeout
self.callbacks = []
def on_timeout(self):
if __debug__: dprint(self.__class__.__name__, ": timeout on ", len(self.callbacks), " callbacks")
for func, args in self.callbacks:
func(None, *args)
@staticmethod
def properties_to_identifier(*args):
raise NotImplementedError()
@staticmethod
def message_to_identifier(message):
raise NotImplementedError()
class MissingMemberCache(MissingSomethingCache):
@staticmethod
def properties_to_identifier(community, member):
return "-missing-member-%s-%s-" % (community.cid, member.mid)
@staticmethod
def message_to_identifier(message):
return "-missing-member-%s-%s-" % (message.community.cid, message.authentication.member.mid)
class MissingMessageCache(MissingSomethingCache):
@staticmethod
def properties_to_identifier(community, member, global_time):
return "-missing-message-%s-%s-%d-" % (community.cid, member.mid, global_time)
@staticmethod
def message_to_identifier(message):
return "-missing-message-%s-%s-%d-" % (message.community.cid, message.authentication.member.mid, message.distribution.global_time)
class MissingLastMessageCache(MissingSomethingCache):
@staticmethod
def properties_to_identifier(community, member, message):
return "-missing-last-message-%s-%s-%s-" % (community.cid, member.mid, message.name.encode("UTF-8"))
@staticmethod
def message_to_identifier(message):
return "-missing-last-message-%s-%s-%s-" % (message.community.cid, message.authentication.member.mid, message.name.encode("UTF-8"))
class MissingProofCache(MissingSomethingCache):
def __init__(self, timeout):
super(MissingProofCache, self).__init__(timeout)
# duplicates contains the (meta messages, member) for which we have already requesting
# proof, this allows us send fewer duplicate requests
self.duplicates = []
@staticmethod
def properties_to_identifier(community):
return "-missing-proof-%s-" % (community.cid,)
@staticmethod
def message_to_identifier(message):
return "-missing-proof-%s-" % (message.community.cid,)
class MissingSequenceOverviewCache(Cache):
cleanup_delay = 0.0
def __init__(self, timeout):
self.timeout_delay = timeout
self.missing_high = 0
def on_timeout(self):
pass
@staticmethod
def properties_to_identifier(community, member, message):
return "-missing-sequence-overview-%s-%s-%s-" % (community.cid, member.mid, message.name.encode("UTF-8"))
class MissingSequenceCache(MissingSomethingCache):
@staticmethod
def properties_to_identifier(community, member, message, missing_high):
return "-missing-sequence-%s-%s-%s-%d-" % (community.cid, member.mid, message.name.encode("UTF-8"), missing_high)
@staticmethod
def message_to_identifier(message):
return "-missing-sequence-%s-%s-%s-%d-" % (message.community.cid, message.authentication.member.mid, message.name.encode("UTF-8"), message.distribution.sequence_number)
class GlobalCandidateCache():
def __init__(self, dispersy):
self._dispersy = dispersy
def __contains__(self, item):
for community in self._dispersy._communities.itervalues():
if item in community._candidates:
return True
def __delitem__(self, item):
for community in self._dispersy._communities.itervalues():
if item in community._candidates:
del community._candidates[item]
def iteritems(self):
for community in self._dispersy._communities.itervalues():
for key, value in community._candidates.iteritems():
yield key, value
def itervalues(self):
for community in self._dispersy._communities.itervalues():
for value in community._candidates.itervalues():
yield value
def get(self, item, default=None):
for community in self._dispersy._communities.itervalues():
if item in community._candidates:
return community._candidates[item]
return default
def __len__(self):
candidates = set()
for community in self._dispersy._communities.itervalues():
candidates.update(community._candidates.itervalues())
return len(candidates)
class Dispersy(Singleton):
"""
The Dispersy class provides the interface to all Dispersy related commands, managing the in- and
outgoing data for, possibly, multiple communities.
"""
def __init__(self, callback, working_directory, database_filename=u"dispersy.db"):
"""
Initialize the Dispersy singleton instance.
Currently we use the rawserver to schedule events. This may change in the future to offload
all data processing to a different thread. The only mechanism used from the rawserver is
the add_task method.
@param callback: Object for callback scheduling.
@type rawserver: Callback
@param working_directory: The directory where all files should be stored.
@type working_directory: unicode
@param database_filename: The database filename or u":memory:"
@type database_filename: unicode
"""
assert isinstance(callback, Callback)
assert isinstance(working_directory, unicode)
assert isinstance(database_filename, unicode)
super(Dispersy, self).__init__()
# the raw server
self._callback = callback
# batch caching incoming packets
self._batch_cache = {}
# where we store all data
self._working_directory = os.path.abspath(working_directory)
# our data storage
if not database_filename == u":memory:":
database_directory = os.path.join(self._working_directory, u"sqlite")
if not os.path.isdir(database_directory):
os.makedirs(database_directory)
database_filename = os.path.join(database_directory, database_filename)
self._database = DispersyDatabase.get_instance(database_filename)
# peer selection candidates. address:Candidate pairs (where
# address is obtained from socket.recv_from)
self._candidates = GlobalCandidateCache(self)
self._callback.register(self._periodically_cleanup_candidates)
# assigns temporary cache objects to unique identifiers
self._request_cache = RequestCache(self._callback)
# indicates what our connection type is. currently it can be u"unknown", u"public", or
# u"symmetric-NAT"
self._connection_type = u"unknown"
# our LAN and WAN addresses
self._lan_address = (self._guess_lan_address() or "0.0.0.0", 0)
self._wan_address = ("0.0.0.0", 0)
self._wan_address_votes = {}
if __debug__:
dprint("my LAN address is ", self._lan_address[0], ":", self._lan_address[1])
dprint("my WAN address is ", self._wan_address[0], ":", self._wan_address[1])
# bootstrap peers
bootstrap_candidates = get_bootstrap_candidates(self)
if not all(bootstrap_candidates):
self._callback.register(self._retry_bootstrap_candidates)
self._bootstrap_candidates = dict((candidate.sock_addr, candidate) for candidate in bootstrap_candidates if candidate)
# communities that can be auto loaded. classification:(cls, args, kargs) pairs.
self._auto_load_communities = {}
# loaded communities. cid:Community pairs.
self._communities = {}
self._walker_commmunities = []
# communication endpoint
self._endpoint = DummyEndpoint()
self._check_distribution_batch_map = {DirectDistribution:self._check_direct_distribution_batch,
FullSyncDistribution:self._check_full_sync_distribution_batch,
LastSyncDistribution:self._check_last_sync_distribution_batch}
# progress handlers (used to notify the user when something will take a long time)
self._progress_handlers = []
# commit changes to the database periodically
self._callback.register(self._watchdog)
# statistics...
self._statistics = DispersyStatistics(self)
# memory profiler
if "--memory-dump" in sys.argv:
def memory_dump():
from meliae import scanner
start = time()
try:
while True:
yield float(60 * 60)
scanner.dump_all_objects("memory-%d.out" % (time() - start))
except GeneratorExit:
scanner.dump_all_objects("memory-%d-shutdown.out" % (time() - start))
self._callback.register(memory_dump)
if __debug__:
self._callback.register(self._stats_candidates)
self._callback.register(self._stats_detailed_candidates)
@staticmethod
def _guess_lan_address():
"""
Returns the address of the first AF_INET interface it can find.
"""
blacklist = ["127.0.0.1", "0.0.0.0", "255.255.255.255"]
for interface in netifaces.interfaces():
addresses = netifaces.ifaddresses(interface)
for option in addresses.get(netifaces.AF_INET, []):
if "broadcast" in option and "addr" in option and not option["addr"] in blacklist:
if __debug__: dprint("interface ", interface, " address ", option["addr"])
return option["addr"]
#Exception for virtual machines/containers
for interface in netifaces.interfaces():
addresses = netifaces.ifaddresses(interface)
for option in addresses.get(netifaces.AF_INET, []):
if "addr" in option and not option["addr"] in blacklist:
if __debug__: dprint("interface ", interface, " address ", option["addr"])
return option["addr"]
dprint("Unable to find our public interface!", level="error")
return None
def _retry_bootstrap_candidates(self):
"""
One or more bootstrap addresses could not be retrieved.
The first 30 seconds we will attempt to resolve the addresses once every second. If we did
not succeed after 30 seconds will will retry once every 30 seconds until we succeed.
"""
if __debug__: dprint("unable to resolve all bootstrap addresses", level="warning")
for counter in count(1):
yield 1.0 if counter < 30 else 30.0
if __debug__: dprint("attempt #", counter, level="warning")
candidates = get_bootstrap_candidates(self)
for candidate in candidates:
if candidate is None:
break
else:
if __debug__: dprint("resolved all bootstrap addresses")
self._bootstrap_candidates = dict((candidate.sock_addr, candidate) for candidate in candidates if candidate)
break
@property
def working_directory(self):
"""
The full directory path where all dispersy related files are stored.
@rtype: unicode
"""
return self._working_directory
# @property
def __get_endpoint(self):
"""
The endpoint object used to send packets.
@rtype: Object with a send(address, data) method
"""
return self._endpoint
# @endpoint.setter
def __set_endpoint(self, endpoint):
"""
Set a endpoint object.
@param endpoint: The endpoint object.
@type endpoint: Object with a send(address, data) method
"""
self._endpoint = endpoint
host, port = endpoint.get_address()
if __debug__: dprint("update LAN address ", self._lan_address[0], ":", self._lan_address[1], " -> ", self._lan_address[0], ":", port, force=True)
self._lan_address = (self._lan_address[0], port)
# at this point we do not yet have a WAN address, set it to the LAN address to ensure we
# have something
assert self._wan_address == ("0.0.0.0", 0)
if __debug__: dprint("update WAN address ", self._wan_address[0], ":", self._wan_address[1], " -> ", self._lan_address[0], ":", self._lan_address[1], force=True, level='error')
self._wan_address = self._lan_address
if not self.is_valid_address(self._lan_address):
if __debug__: dprint("update LAN address ", self._lan_address[0], ":", self._lan_address[1], " -> ", host, ":", self._lan_address[1], force=True)
self._lan_address = (host, self._lan_address[1])
if not self.is_valid_address(self._lan_address):
if __debug__: dprint("update LAN address ", self._lan_address[0], ":", self._lan_address[1], " -> ", self._wan_address[0], ":", self._lan_address[1], force=True)
self._lan_address = (self._wan_address[0], self._lan_address[1])
# our address may not be a bootstrap address
if self._lan_address in self._bootstrap_candidates:
del self._bootstrap_candidates[self._lan_address]
# our address may not be a candidate
if self._lan_address in self._candidates:
del self._candidates[self._lan_address]
# .setter was introduced in Python 2.6
endpoint = property(__get_endpoint, __set_endpoint)
@property
def lan_address(self):
"""
The LAN address where we believe people who are inside our LAN can find us.
Our LAN address is determined by the default gateway of our
system and our port.
@rtype: (str, int)
"""
return self._lan_address
@property
def wan_address(self):
"""
The wan address where we believe that we can be found from outside our LAN.
Our wan address is determined by majority voting. Each time when we receive a message
that contains an opinion about our wan address, we take this into account. The
address with the most votes wins.
Votes can be added by calling the wan_address_vote(...) method.
Usually these votes are received through dispersy-introduction-request and
dispersy-introduction-response messages.
@rtype: (str, int)
"""
return self._wan_address
@property
def connection_type(self):
"""
The connection type that we believe we have.
Currently the following types are recognized:
- u'unknown': the default value until the actual type can be recognized.
- u'public': when the LAN and WAN addresses are determined to be the same.
- u'symmetric-NAT': when each remote peer reports different external port numbers.
@rtype: unicode
"""
return self._connection_type
@property
def callback(self):
return self._callback
@property
def database(self):
"""
The Dispersy database singleton.
@rtype: DispersyDatabase
"""
return self._database
@property
def request_cache(self):
"""
The request cache instance responsible for maintaining identifiers and timeouts for
outstanding requests.
@rtype: RequestCache
"""
return self._request_cache
@property
def statistics(self):
"""
The Statistics instance.
"""
return self._statistics
def initiate_meta_messages(self, community):
"""
Create the meta messages that Dispersy uses.
This method is called once for each community when it is created. The resulting meta
messages can be obtained by either community.get_meta_message(name) or
community.get_meta_messages().
Since these meta messages will be used along side the meta messages that each community
provides, all message names are prefixed with 'dispersy-' to ensure that the names are
unique.
@param community: The community that will get the messages.
@type community: Community
@return: The new meta messages.
@rtype: [Message]
"""
if __debug__:
from .community import Community
assert isinstance(community, Community)
messages = [Message(community, u"dispersy-identity", MemberAuthentication(encoding="bin"), PublicResolution(), LastSyncDistribution(synchronization_direction=u"ASC", priority=16, history_size=1), CommunityDestination(node_count=0), IdentityPayload(), self._generic_timeline_check, self.on_identity),
Message(community, u"dispersy-signature-request", NoAuthentication(), PublicResolution(), DirectDistribution(), MemberDestination(), SignatureRequestPayload(), self.check_signature_request, self.on_signature_request),
Message(community, u"dispersy-signature-response", NoAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), SignatureResponsePayload(), self.check_signature_response, self.on_signature_response),
Message(community, u"dispersy-authorize", MemberAuthentication(), PublicResolution(), FullSyncDistribution(enable_sequence_number=True, synchronization_direction=u"ASC", priority=128), CommunityDestination(node_count=10), AuthorizePayload(), self._generic_timeline_check, self.on_authorize),
Message(community, u"dispersy-revoke", MemberAuthentication(), PublicResolution(), FullSyncDistribution(enable_sequence_number=True, synchronization_direction=u"ASC", priority=128), CommunityDestination(node_count=10), RevokePayload(), self._generic_timeline_check, self.on_revoke),
Message(community, u"dispersy-undo-own", MemberAuthentication(), PublicResolution(), FullSyncDistribution(enable_sequence_number=True, synchronization_direction=u"ASC", priority=128), CommunityDestination(node_count=10), UndoPayload(), self.check_undo, self.on_undo),
Message(community, u"dispersy-undo-other", MemberAuthentication(), LinearResolution(), FullSyncDistribution(enable_sequence_number=True, synchronization_direction=u"ASC", priority=128), CommunityDestination(node_count=10), UndoPayload(), self.check_undo, self.on_undo),
Message(community, u"dispersy-destroy-community", MemberAuthentication(), LinearResolution(), FullSyncDistribution(enable_sequence_number=False, synchronization_direction=u"ASC", priority=192), CommunityDestination(node_count=50), DestroyCommunityPayload(), self._generic_timeline_check, self.on_destroy_community),
Message(community, u"dispersy-dynamic-settings", MemberAuthentication(), LinearResolution(), FullSyncDistribution(enable_sequence_number=True, synchronization_direction=u"DESC", priority=191), CommunityDestination(node_count=10), DynamicSettingsPayload(), self._generic_timeline_check, community.dispersy_on_dynamic_settings),
#
# when something is missing, a dispersy-missing-... message can be used to request
# it from another peer
#
# when we have a member id (20 byte sha1 of the public key) but not the public key
Message(community, u"dispersy-missing-identity", NoAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), MissingIdentityPayload(), self._generic_timeline_check, self.on_missing_identity),
# when we are missing one or more SyncDistribution messages in a certain sequence
Message(community, u"dispersy-missing-sequence", NoAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), MissingSequencePayload(), self._generic_timeline_check, self.on_missing_sequence, batch=BatchConfiguration(max_window=0.1)),
# when we have a reference to a message that we do not have. a reference consists
# of the community identifier, the member identifier, and the global time
Message(community, u"dispersy-missing-message", NoAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), MissingMessagePayload(), self._generic_timeline_check, self.on_missing_message),
# when we might be missing a dispersy-authorize message
Message(community, u"dispersy-missing-proof", NoAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), MissingProofPayload(), self._generic_timeline_check, self.on_missing_proof),
# when we have a reference to a LastSyncDistribution that we do not have. a
# reference consists of the community identifier and the member identifier
Message(community, u"dispersy-missing-last-message", NoAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), MissingLastMessagePayload(), self._generic_timeline_check, self.on_missing_last_message),
]
if community.dispersy_enable_candidate_walker_responses:
messages.extend([Message(community, u"dispersy-introduction-request", MemberAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), IntroductionRequestPayload(), self.check_introduction_request, self.on_introduction_request),
Message(community, u"dispersy-introduction-response", MemberAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), IntroductionResponsePayload(), self.check_introduction_response, self.on_introduction_response),
Message(community, u"dispersy-puncture-request", NoAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), PunctureRequestPayload(), self.check_puncture_request, self.on_puncture_request),
Message(community, u"dispersy-puncture", MemberAuthentication(), PublicResolution(), DirectDistribution(), CandidateDestination(), PuncturePayload(), self.check_puncture, self.on_puncture)])
return messages
def define_auto_load(self, community, args=(), kargs=None):
"""
Tell Dispersy how to load COMMUNITY is needed.
COMMUNITY is the community class that is defined.
ARGS an KARGS are optional arguments and keyword arguments used when a community is loaded
using COMMUNITY.load_community(master, *ARGS, **KARGS).
"""
if __debug__:
from .community import Community
assert issubclass(community, Community)
assert isinstance(args, tuple)
assert kargs is None or isinstance(kargs, dict)
assert not community.get_classification() in self._auto_load_communities
self._auto_load_communities[community.get_classification()] = (community, args, kargs if kargs else {})
def undefine_auto_load(self, community):
"""
Tell Dispersy to no longer load COMMUNITY.
COMMUNITY is the community class that is defined.
"""
if __debug__:
from .community import Community
assert issubclass(community, Community)
assert community.get_classification() in self._auto_load_communities
del self._auto_load_communities[community.get_classification()]
def attach_progress_handler(self, func):
assert callable(func), "handler must be callable"
self._progress_handlers.append(func)
def detach_progress_handler(self, func):
assert callable(func), "handler must be callable"
assert func in self._progress_handlers, "handler is not attached"
self._progress_handlers.remove(func)
def get_progress_handlers(self):
return self._progress_handlers
def get_member(self, public_key, private_key=""):
"""
Returns a Member instance associated with public_key.
Since we have the public_key, we can create this user when it didn't already exist. Hence,
this method always succeeds.
@param public_key: The public key of the member we want to obtain.
@type public_key: string
@return: The Member instance associated with public_key.
@rtype: Member
@note: This returns -any- Member, it may not be a member that is part of this community.
@todo: Since this method returns Members that are not specifically bound to any community,
this method should be moved to Dispersy
"""
assert isinstance(public_key, str)
assert isinstance(private_key, str)
return Member(public_key, private_key)
def get_members_from_id(self, mid, cache=True):
"""
Returns zero or more Member instances associated with mid, where mid is the sha1 digest of a
member public key.
As we are using only 20 bytes to represent the actual member public key, this method may
return multiple possible Member instances. In this case, other ways must be used to figure
out the correct Member instance. For instance: if a signature or encryption is available,
all Member instances could be used, but only one can succeed in verifying or decrypting.
Since we may not have the public key associated to MID, this method may return an empty
list. In such a case it is sometimes possible to DelayPacketByMissingMember to obtain the
public key.
@param mid: The 20 byte sha1 digest indicating a member.
@type mid: string
@return: A list containing zero or more Member instances.
@rtype: [Member]
@note: This returns -any- Member, it may not be a member that is part of this community.
"""
assert isinstance(mid, str), type(mid)
assert len(mid) == 20, len(mid)
assert isinstance(cache, bool), type(cache)
if cache:
try:
return [MemberFromId(mid)]
except LookupError:
pass
# note that this allows a security attack where someone might obtain a crypographic key that
# has the same sha1 as the master member, however unlikely. the only way to prevent this,
# as far as we know, is to increase the size of the community identifier, for instance by
# using sha256 instead of sha1.
return [MemberWithoutCheck(str(public_key))
for public_key,
in list(self._database.execute(u"SELECT public_key FROM member WHERE mid = ?", (buffer(mid),)))
if public_key]
def get_member_from_database_id(self, database_id, cache=True):
"""
Returns a Member instance associated with DATABASE_ID or None when this row identifier is
not available.
"""
assert isinstance(database_id, (int, long)), type(database_id)
assert isinstance(cache, bool), type(cache)
if cache:
try:
return MemberFromDatabaseId(database_id)
except LookupError:
pass
try:
public_key, = next(self._database.execute(u"SELECT public_key FROM member WHERE id = ?", (database_id,)))
except StopIteration:
return None
else:
return MemberWithoutCheck(str(public_key))
def attach_community(self, community):
"""
Add a community to the Dispersy instance.
Each community must be known to Dispersy, otherwise an incoming message will not be able to
be passed along to it's associated community.
In general this method is called from the Community.__init__(...) method.
@param community: The community that will be added.
@type community: Community
"""
if __debug__:
from .community import Community
assert isinstance(community, Community)
if __debug__: dprint(community.cid.encode("HEX"), " ", community.get_classification())
assert not community.cid in self._communities
assert not community in self._walker_commmunities
self._communities[community.cid] = community
community.dispersy_check_database()
if community.dispersy_enable_candidate_walker:
self._walker_commmunities.insert(0, community)
# restart walker scheduler
self._callback.replace_register(CANDIDATE_WALKER_CALLBACK_ID, self._candidate_walker)
# count the number of times that a community was attached
self._statistics.dict_inc(self._statistics.attachment, community.cid)
if __debug__:
# schedule the sanity check... it also checks that the dispersy-identity is available and
# when this is a create or join this message is created only after the attach_community
if "--sanity-check" in sys.argv:
try:
self.sanity_check(community)
except ValueError:
dprint(exception=True, level="error")
assert False, "One or more exceptions occurred during sanity check"
def detach_community(self, community):
"""
Remove an attached community from the Dispersy instance.
Once a community is detached it will no longer receive incoming messages. When the
community is marked as auto_load it will be loaded, using community.load_community(...),
when a message for this community is received.
@param community: The community that will be added.
@type community: Community
"""
if __debug__:
from .community import Community
assert isinstance(community, Community)
if __debug__: dprint(community.cid.encode("HEX"), " ", community.get_classification())
assert community.cid in self._communities
assert self._communities[community.cid] == community
assert not community.dispersy_enable_candidate_walker or community in self._walker_commmunities, [community.dispersy_enable_candidate_walker, community in self._walker_commmunities]
del self._communities[community.cid]
# stop walker
if community.dispersy_enable_candidate_walker:
self._walker_commmunities.remove(community)
if self._walker_commmunities:
# restart walker scheduler
self._callback.replace_register(CANDIDATE_WALKER_CALLBACK_ID, self._candidate_walker)
else:
# stop walker scheduler
self._callback.unregister(CANDIDATE_WALKER_CALLBACK_ID)
# remove any items that are left in the cache
for meta in community.get_meta_messages():
if meta.batch.enabled and meta in self._batch_cache:
task_identifier, _, _ = self._batch_cache[meta]
self._callback.unregister(task_identifier)
def reclassify_community(self, source, destination):
"""
Change a community classification.
Each community has a classification that dictates what source code is handling this
community. By default the classification of a community is the unicode name of the class in
the source code.
In some cases it may be usefull to change the classification, for instance: if community A
has a subclass community B, where B has similar but reduced capabilities, we could
reclassify B to A at some point and keep all messages collected so far while using the
increased capabilities of community A.
@param source: The community that will be reclassified. This must be either a Community
instance (when the community is loaded) or a Member instance giving the master member (when
the community is not loaded).
@type source: Community or Member
@param destination: The new community classification. This must be a Community class.
@type destination: Community class
"""
if __debug__:
from .community import Community
assert isinstance(source, (Community, Member))
assert issubclass(destination, Community)
destination_classification = destination.get_classification()
if isinstance(source, Member):
if __debug__: dprint("reclassify ??? -> ", destination_classification)
master = source
else:
if __debug__: dprint("reclassify ", source.get_classification(), " -> ", destination_classification)
assert source.cid in self._communities
assert self._communities[source.cid] == source
master = source.master_member
source.unload_community()
self._database.execute(u"UPDATE community SET classification = ? WHERE master = ?",
(destination_classification, master.database_id))
assert self._database.changes == 1
if destination_classification in self._auto_load_communities:
cls, args, kargs = self._auto_load_communities[destination_classification]
assert cls == destination, [cls, destination]
else:
args = ()
kargs = {}
return destination.load_community(master, *args, **kargs)
def has_community(self, cid):
"""
Returns True when there is a community CID.
"""
return cid in self._communities
def get_community(self, cid, load=False, auto_load=True):
"""
Returns a community by its community id.
The community id, or cid, is the binary representation of the public key of the master
member for the community.
When the community is available but not currently loaded it will be automatically loaded
when (a) the load parameter is True or (b) the auto_load parameter is True and the auto_load
flag for this community is True (this flag is set in the database).
@param cid: The community identifier.
@type cid: string, of any size
@param load: When True, will load the community when available and not yet loaded.
@type load: bool
@param auto_load: When True, will load the community when available, the auto_load flag is
True, and, not yet loaded.
@type load: bool
@warning: It is possible, however unlikely, that multiple communities will have the same
cid. This is currently not handled.
"""
assert isinstance(cid, str)
assert isinstance(load, bool), type(load)
assert isinstance(auto_load, bool)
try:
return self._communities[cid]
except KeyError:
try:
# have we joined this community
classification, auto_load_flag, master_public_key = self._database.execute(u"SELECT community.classification, community.auto_load, member.public_key FROM community JOIN member ON member.id = community.master WHERE mid = ?",
(buffer(cid),)).next()
except StopIteration:
pass
else:
if load or (auto_load and auto_load_flag):
if classification in self._auto_load_communities:
master = Member(str(master_public_key)) if master_public_key else DummyMember(cid)
cls, args, kargs = self._auto_load_communities[classification]
community = cls.load_community(master, *args, **kargs)
assert master.mid in self._communities
return community
else:
import sys
print >> sys.stderr, "unable to auto load, '", classification, "' is an undefined classification [", cid.encode("HEX"), "]"
if __debug__: dprint("unable to auto load, '", classification, "' is an undefined classification [", cid.encode("HEX"), "]", level="warning")
else:
if __debug__: dprint("not allowed to load '", classification, "'")
raise KeyError(cid)
def get_communities(self):
"""
Returns a list with all known Community instances.
"""
return self._communities.values()
def get_message(self, community, member, global_time):
"""
Returns a Member.Implementation instance uniquely identified by its community, member, and
global_time.
Returns None if this message is not in the local database.
"""
if __debug__:
from .community import Community
assert isinstance(community, Community)
assert isinstance(member, Member)
assert isinstance(global_time, (int, long))
try:
packet, = self._database.execute(u"SELECT packet FROM sync WHERE community = ? AND member = ? AND global_time = ?",
(community.database_id, member.database_id, global_time)).next()
except StopIteration:
return None
else:
return self.convert_packet_to_message(str(packet), community)
def get_last_message(self, community, member, meta):
if __debug__:
from .community import Community
assert isinstance(community, Community)
assert isinstance(member, Member)
assert isinstance(meta, Message)
try:
packet, = self._database.execute(u"SELECT packet FROM sync WHERE member = ? AND meta_message = ? ORDER BY global_time DESC LIMIT 1",
(member.database_id, meta.database_id)).next()
except StopIteration:
return None
else:
return self.convert_packet_to_message(str(packet), community)
def wan_address_unvote(self, voter):
"""
Removes and returns one vote made by VOTER.
"""
assert isinstance(voter, Candidate)
for vote, voters in self._wan_address_votes.iteritems():
if voter.sock_addr in voters:
if __debug__: dprint("removing vote for ", vote, " made by ", voter)
voters.remove(voter.sock_addr)
if len(voters) == 0:
del self._wan_address_votes[vote]
return vote
def wan_address_vote(self, address, voter):
"""
Add one vote and possibly re-determine our wan address.
Our wan address is determined by majority voting. Each time when we receive a message
that contains anothers opinion about our wan address, we take this into account. The
address with the most votes wins.
Usually these votes are received through dispersy-candidate-request and
dispersy-candidate-response messages.
@param address: The wan address that the voter believes us to have.
@type address: (str, int)
@param voter: The voter candidate.
@type voter: Candidate
"""
assert isinstance(address, tuple)
assert len(address) == 2
assert isinstance(address[0], str)
assert isinstance(address[1], int)
assert isinstance(voter, Candidate), type(voter)
if self._wan_address[0] in (voter.wan_address[0], voter.sock_addr[0]):
if __debug__: dprint("ignoring vote from candidate on the same LAN")
return
if not self.is_valid_address(address):
if __debug__: dprint("got invalid external vote from ", voter, " received ", address[0], ":", address[1])
return
if __debug__:
debug_previous_connection_type = self._connection_type
# undo previous vote
self.wan_address_unvote(voter)
# do vote