-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdispersy.py
1780 lines (1437 loc) · 83.4 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 logging
import os
from collections import defaultdict, Iterable, OrderedDict
from hashlib import sha1
from itertools import groupby, count
from pprint import pformat
from socket import inet_aton
from struct import unpack_from
from time import time
import netifaces
from twisted.internet import reactor
from twisted.internet.defer import maybeDeferred, gatherResults, inlineCallbacks, returnValue
from twisted.internet.task import LoopingCall
from twisted.python.failure import Failure
from twisted.python.threadable import isInIOThread
from .authentication import MemberAuthentication, DoubleMemberAuthentication
from .candidate import LoopbackCandidate, WalkCandidate, Candidate
from .community import Community
from .crypto import DispersyCrypto, ECCrypto
from .destination import CommunityDestination, CandidateDestination, NHopCommunityDestination
from .discovery.community import DiscoveryCommunity
from .dispersydatabase import DispersyDatabase
from .distribution import SyncDistribution, FullSyncDistribution, LastSyncDistribution
from .endpoint import Endpoint
from .exception import CommunityNotFoundException, ConversionNotFoundException, MetaNotFoundException
from .member import DummyMember, Member
from .message import Message, DropPacket, DelayPacket
from .statistics import DispersyStatistics, _runtime_statistics
from .taskmanager import TaskManager
from .util import (attach_runtime_statistics, init_instrumentation, blocking_call_on_reactor_thread, is_valid_address,
get_lan_address_without_netifaces, address_is_lan_without_netifaces)
# Set up the instrumentation utilities
init_instrumentation()
FLUSH_DATABASE_INTERVAL = 60.0
STATS_DETAILED_CANDIDATES_INTERVAL = 5.0
class Dispersy(TaskManager):
"""
The Dispersy class provides the interface to all Dispersy related commands, managing the in- and
outgoing data for, possibly, multiple communities.
"""
def __init__(self, endpoint, working_directory, database_filename=u"dispersy.db", crypto=ECCrypto()):
"""
Initialise a Dispersy instance.
@param endpoint: Instance for communication.
@type callback: Endpoint
@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(endpoint, Endpoint), type(endpoint)
assert isinstance(working_directory, unicode), type(working_directory)
assert isinstance(database_filename, unicode), type(database_filename)
assert isinstance(crypto, DispersyCrypto), type(crypto)
super(Dispersy, self).__init__()
self._logger = logging.getLogger(self.__class__.__name__)
self.running = False
# communication endpoint
self._endpoint = endpoint
# where we store all data
self._working_directory = os.path.abspath(working_directory)
self._discovery_community = None
self._member_cache_by_hash = OrderedDict()
# 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(database_filename)
self._crypto = crypto
# 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._netifaces_failed = False
self._lan_address = self._get_lan_address(True)
self._wan_address = ("0.0.0.0", 0)
self._wan_address_votes = defaultdict(set)
self._logger.debug("my LAN address is %s:%d", self._lan_address[0], self._lan_address[1])
self._logger.debug("my WAN address is %s:%d", self._wan_address[0], self._wan_address[1])
self._logger.debug("my connection type is %s", self._connection_type)
# communities that can be auto loaded. classification:(cls, args, kargs) pairs.
self._auto_load_communities = OrderedDict()
# loaded communities. cid:Community pairs.
self._communities = {}
# progress handlers (used to notify the user when something will take a long time)
self._progress_handlers = []
# statistics...
self._statistics = DispersyStatistics(self)
@staticmethod
def _get_interface_addresses():
"""
Yields Interface instances for each available AF_INET interface found.
An Interface instance has the following properties:
- name (i.e. "eth0")
- address (i.e. "10.148.3.254")
- netmask (i.e. "255.255.255.0")
- broadcast (i.e. "10.148.3.255")
"""
class Interface(object):
def __init__(self, name, address, netmask, broadcast):
self.name = name
self.address = address
self.netmask = netmask
self.broadcast = broadcast
self._l_address, = unpack_from(">L", inet_aton(address))
self._l_netmask, = unpack_from(">L", inet_aton(netmask))
def __contains__(self, address):
assert isinstance(address, str), type(address)
l_address, = unpack_from(">L", inet_aton(address))
return (l_address & self._l_netmask) == (self._l_address & self._l_netmask)
def __str__(self):
return "<{self.__class__.__name__} \"{self.name}\" addr:{self.address} mask:{self.netmask}>".format(self=self)
def __repr__(self):
return "<{self.__class__.__name__} \"{self.name}\" addr:{self.address} mask:{self.netmask}>".format(self=self)
try:
for interface in netifaces.interfaces():
try:
addresses = netifaces.ifaddresses(interface)
except ValueError:
# some interfaces are given that are invalid, we encountered one called ppp0
pass
else:
for option in addresses.get(netifaces.AF_INET, []):
try:
# On Windows netifaces currently returns IP addresses as unicode,
# and on *nix it returns str. So, we convert any unicode objects to str.
unicode_to_str = lambda s: s.encode('utf-8') if isinstance(s, unicode) else s
yield Interface(interface,
unicode_to_str(option.get("addr")),
unicode_to_str(option.get("netmask")),
unicode_to_str(option.get("broadcast")))
except TypeError:
# some interfaces have no netmask configured, causing a TypeError when
# trying to unpack _l_netmask
pass
except OSError, e:
logger = logging.getLogger("dispersy")
logger.warning("failed to check network interfaces, error was: %r", e)
def _address_is_lan(self, address):
if self._netifaces_failed:
return address_is_lan_without_netifaces(address)
else:
return any(address in interface for interface in self._local_interfaces)
def _get_lan_address(self, bootstrap=False):
"""
Attempt to get the newest lan ip of this machine, preferably with netifaces, but use the fallback if it fails
:return: lan address
"""
if self._netifaces_failed:
return (get_lan_address_without_netifaces(), self._lan_address[1])
else:
self._local_interfaces = list(self._get_interface_addresses())
interface = self._guess_lan_address(self._local_interfaces)
return (interface.address if interface else get_lan_address_without_netifaces()), \
(0 if bootstrap else self._lan_address[1])
def _guess_lan_address(self, interfaces, default=None):
"""
Chooses the most likely Interface instance out of INTERFACES to use as our LAN address.
INTERFACES can be obtained from _get_interface_addresses()
DEFAULT is used when no appropriate Interface can be found
"""
assert isinstance(interfaces, list), type(interfaces)
blacklist = ["127.0.0.1", "0.0.0.0", "255.255.255.255"]
# prefer interfaces where we have a broadcast address
for interface in interfaces:
if interface.broadcast and interface.address and not interface.address in blacklist:
self._logger.debug("%s", interface)
return interface
# Exception for virtual machines/containers
for interface in interfaces:
if interface.address and not interface.address in blacklist:
self._logger.debug("%s", interface)
return interface
self._logger.warning("Unable to find our public interface!")
self._netifaces_failed = True
return default
@property
def working_directory(self):
"""
The full directory path where all dispersy related files are stored.
@rtype: unicode
"""
return self._working_directory
@property
def endpoint(self):
"""
The endpoint object used to send packets.
@rtype: Object with a send(address, data) method
"""
return self._endpoint
def _endpoint_ready(self):
"""
Guess our LAN and WAN address from information provided by endpoint.
This method is called immediately after endpoint.start finishes.
"""
host, port = self._endpoint.get_address()
self._logger.info("update LAN address %s:%d -> %s:%d",
self._lan_address[0], self._lan_address[1], self._lan_address[0], port)
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)
self._logger.info("update WAN address %s:%d -> %s:%d",
self._wan_address[0], self._wan_address[1], self._lan_address[0], self._lan_address[1])
self._wan_address = self._lan_address
if not is_valid_address(self._lan_address):
self._logger.info("update LAN address %s:%d -> %s:%d",
self._lan_address[0], self._lan_address[1], host, self._lan_address[1])
self._lan_address = (host, self._lan_address[1])
if not is_valid_address(self._lan_address):
self._logger.info("update LAN address %s:%d -> %s:%d",
self._lan_address[0], self._lan_address[1],
self._wan_address[0], self._lan_address[1])
self._lan_address = (self._wan_address[0], self._lan_address[1])
# our address may not be a candidate
for community in self._communities.itervalues():
community.candidates.pop(self._lan_address, None)
@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 database(self):
"""
The Dispersy database singleton.
@rtype: DispersyDatabase
"""
return self._database
@property
def crypto(self):
"""
The Dispersy crypto singleton.
@rtype: DispersyCrypto
"""
return self._crypto
@property
def statistics(self):
"""
The Statistics instance.
"""
return self._statistics
def define_auto_load(self, community_cls, my_member, args=(), kargs=None, load=False):
"""
Tell Dispersy how to load COMMUNITY if need be.
COMMUNITY_CLS is the community class that is defined.
MY_MEMBER is the member to be used within the community.
ARGS an KARGS are optional arguments and keyword arguments passed to the
community constructor.
When LOAD is True all available communities of this type will be immediately loaded.
Returns a list with loaded communities.
"""
assert isInIOThread(), "Must be called from the callback thread"
assert issubclass(community_cls, Community), type(community_cls)
assert isinstance(args, tuple), type(args)
assert kargs is None or isinstance(kargs, dict), type(kargs)
assert not community_cls.get_classification() in self._auto_load_communities
assert isinstance(load, bool), type(load)
if kargs is None:
kargs = {}
self._auto_load_communities[community_cls.get_classification()] = (community_cls, my_member, args, kargs)
communities = []
if load:
for master in community_cls.get_master_members(self):
if not master.mid in self._communities:
self._logger.debug("Loading %s at start", community_cls.get_classification())
community = community_cls.init_community(self, master, my_member, *args, **kargs)
communities.append(community)
assert community.master_member.mid == master.mid
assert community.master_member.mid in self._communities
return communities
def undefine_auto_load(self, community):
"""
Tell Dispersy to no longer load COMMUNITY.
COMMUNITY is the community class that is defined.
"""
assert issubclass(community, Community)
assert community.get_classification() in self._auto_load_communities
del self._auto_load_communities[community.get_classification()]
def attach_community(self, community):
# add community to communities dict
self._communities[community.cid] = community
self._statistics.dict_inc(u"attachment", community.cid)
# let discovery community know
if self._discovery_community:
self._discovery_community.new_community(community)
def detach_community(self, community):
del self._communities[community.cid]
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, mid="", public_key="", private_key=""):
"""Returns a Member instance associated with public_key.
Since we have the public_key, we can create this user if it doesn't yet. Hence, this method always succeeds.
@param public_key: The public key of the member we want to obtain.
@param private_key: The public/private key pair of the member we want to obtain.
@type public_key: string
@type private_key: string
@return: The Member instance associated with public_key.
@rtype: Member
"""
assert sum(map(bool, (mid, public_key, private_key))) == 1, \
"Only one of the three optional arguments may be passed: %s" % str((mid, public_key, private_key))
assert isinstance(mid, str)
assert isinstance(public_key, str)
assert isinstance(private_key, str)
assert not mid or len(mid) == 20, (mid.encode("HEX"), len(mid))
assert not public_key or self.crypto.is_valid_public_bin(public_key)
assert not private_key or self.crypto.is_valid_private_bin(private_key)
if not mid:
if public_key:
mid = sha1(public_key).digest()
elif private_key:
_key = self.crypto.key_from_private_bin(private_key)
mid = self.crypto.key_to_hash(_key.pub())
member = self._member_cache_by_hash.get(mid)
if member:
return member
if private_key:
key = self.crypto.key_from_private_bin(private_key)
public_key = self.crypto.key_to_bin(key.pub())
elif public_key:
key = self.crypto.key_from_public_bin(public_key)
# both public and private keys are valid at this point
# The member is not cached, let's try to get it from the database
row = self.database.execute(u"SELECT id, public_key, private_key FROM member WHERE mid = ? LIMIT 1", (buffer(mid),)).fetchone()
if row:
database_id, public_key_from_db, private_key_from_db = row
public_key_from_db = "" if public_key_from_db is None else str(public_key_from_db)
private_key_from_db = "" if private_key_from_db is None else str(private_key_from_db)
# the private key that was passed as an argument overrules everything, update db if neccesary
if private_key:
assert public_key
if private_key_from_db != private_key:
self.database.execute(u"UPDATE member SET public_key = ?, private_key = ? WHERE id = ?",
(buffer(public_key), buffer(private_key), database_id))
else:
# the private key from the database overrules the public key argument
if private_key_from_db:
key = self.crypto.key_from_private_bin(private_key_from_db)
# the public key argument overrules anything in the database
elif public_key:
if public_key_from_db != public_key:
self.database.execute(u"UPDATE member SET public_key = ? WHERE id = ?",
(buffer(public_key), database_id))
# no priv/pubkey arguments passed, maybe use the public key from the database
elif public_key_from_db:
key = self.crypto.key_from_public_bin(public_key_from_db)
else:
return DummyMember(self, database_id, mid)
# the member is not in the database, insert it
elif public_key or private_key:
if private_key:
assert public_key
# The MID or public/private keys are not in the database, store them.
database_id = self.database.execute(
u"INSERT INTO member (mid, public_key, private_key) VALUES (?, ?, ?)",
(buffer(mid), buffer(public_key), buffer(private_key)), get_lastrowid=True)
else:
# We could't find the key on the DB, nothing else to do
database_id = self.database.execute(u"INSERT INTO member (mid) VALUES (?)",
(buffer(mid),), get_lastrowid=True)
return DummyMember(self, database_id, mid)
member = Member(self, key, database_id, mid)
# store in cache
self._member_cache_by_hash[member.mid] = member
# limit cache length
if len(self._member_cache_by_hash) > 1024:
self._member_cache_by_hash.popitem(False)
return member
def get_new_member(self, securitylevel=u"medium"):
"""
Returns a Member instance created from a newly generated public key.
"""
assert isinstance(securitylevel, unicode), type(securitylevel)
key = self.crypto.generate_key(securitylevel)
return self.get_member(private_key=self.crypto.key_to_bin(key))
def get_member_from_database_id(self, database_id):
"""
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)
try:
public_key, = next(self._database.execute(u"SELECT public_key FROM member WHERE id = ?", (database_id,)))
return self.get_member(public_key=str(public_key))
except StopIteration:
pass
@inlineCallbacks
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
"""
assert isinstance(source, (Community, Member))
assert issubclass(destination, Community)
assert type(source) is not type(destination), (type(source), type(destination))
destination_classification = destination.get_classification()
if isinstance(source, Member):
self._logger.debug("reclassify <unknown> -> %s", destination_classification)
master = source
else:
self._logger.debug("reclassify %s -> %s", source.get_classification(), destination_classification)
assert source.cid in self._communities
assert self._communities[source.cid] == source
master = source.master_member
yield source.unload_community()
self._database.execute(u"UPDATE community SET classification = ? WHERE master = ?",
(destination_classification, master.database_id))
if destination_classification in self._auto_load_communities:
cls, my_member, args, kargs = self._auto_load_communities[destination_classification]
assert cls == destination, [cls, destination]
else:
my_member_did, = self._database.execute(u"SELECT member FROM community WHERE master = ?",
(master.database_id,)).next()
my_member = self.get_member_from_database_id(my_member_did)
args = ()
kargs = {}
res = destination.init_community(self, master, my_member, *args, **kargs)
returnValue(res)
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:
if load or auto_load:
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 = self.get_member(public_key=str(master_public_key)) if master_public_key else self.get_member(mid=cid)
cls, my_member, args, kargs = self._auto_load_communities[classification]
community = cls.init_community(self, master, my_member, *args, **kargs)
assert master.mid in self._communities
return community
else:
self._logger.warning("unable to auto load %s is an undefined classification [%s]",
cid.encode("HEX"), classification)
else:
self._logger.debug("not allowed to load [%s]", classification)
raise CommunityNotFoundException(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.
"""
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):
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:
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)
def set_lan_address(address):
" Set LAN address when ADDRESS is different from self._LAN_ADDRESS. "
if self._lan_address == address:
return False
else:
self._logger.info("update LAN address %s:%d -> %s:%d",
self._lan_address[0], self._lan_address[1], address[0], address[1])
self._lan_address = address
return True
def set_wan_address(address):
" Set WAN address when ADDRESS is different from self._WAN_ADDRESS. "
if self._wan_address == address:
return False
else:
self._logger.info("update WAN address %s:%d -> %s:%d",
self._wan_address[0], self._wan_address[1], address[0], address[1])
self._wan_address = address
return True
def set_connection_type(connection_type):
" Set connection type when CONNECTION_TYPE is different from self._CONNECTION_TYPE. "
if self._connection_type == connection_type:
return False
else:
self._logger.info("update connection type %s -> %s", self._connection_type, connection_type)
self._connection_type = connection_type
return True
# undo previous vote
self.wan_address_unvote(voter)
# ensure ADDRESS is valid
if not is_valid_address(address):
self._logger.debug("ignore vote for %s from %s (address is invalid)", address, voter.sock_addr)
return
# ignore votes from voters that we know are within any of our LAN interfaces. these voters
# can not know our WAN address
if self._address_is_lan(voter.sock_addr[0]):
self._logger.debug("ignore vote for %s from %s (voter is within our LAN)", address, voter.sock_addr)
return
# do vote
self._logger.debug("add vote for %s from %s", address, voter.sock_addr)
self._wan_address_votes[address].add(voter.sock_addr)
#
# check self._lan_address and self._wan_address
#
# change when new vote count is higher than old address vote count (don't use equal to avoid
# alternating between two equally voted addresses)
if len(self._wan_address_votes[address]) > len(self._wan_address_votes.get(self._wan_address, ())):
if set_wan_address(address):
# refresh our LAN address(es), perhaps we are running on a roaming device
lan_address = self._get_lan_address()
if not is_valid_address(lan_address):
lan_address = (self._wan_address[0], self._lan_address[1])
set_lan_address(lan_address)
# remove our lan/wan addresses from all communities candidate lists
for community in self._communities.itervalues():
community.remove_candidate(self._wan_address)
community.remove_candidate(self._lan_address)
#
# check self._connection_type
#
if len(self._wan_address_votes) == 1 and self._lan_address == self._wan_address:
# external peers are reporting the same WAN address that happens to be our LAN address
# as well
set_connection_type(u"public")
elif len(self._wan_address_votes) > 1:
for voters in self._wan_address_votes.itervalues():
if len(set([address[0] for address in voters])) > 1:
# A single NAT mapping has more than one destination IP hence
# it cannot be a symmetric NAT
set_connection_type(u"unknown")
break
else:
# Our nat created a new mapping for each destination IP
set_connection_type(u"symmetric-NAT")
else:
set_connection_type(u"unknown")
def load_message(self, community, member, global_time, verify=False):
"""
Returns the message identified by community, member, and global_time.
Each message is uniquely identified by the community that it is created in, the member it is
created by and the global time when it is created. Using these three parameters we return
the associated the Message.Implementation instance. None is returned when we do not have
this message or it can not be decoded.
"""
assert isinstance(community, Community), type(community)
assert isinstance(member, DummyMember), type(member)
assert isinstance(global_time, (int, long)), type(global_time)
try:
packet_id, packet, undone = self._database.execute(u"SELECT id, packet, undone FROM sync WHERE community = ? AND member = ? AND global_time = ? LIMIT 1",
(community.database_id, member.database_id, global_time)).next()
except StopIteration:
return None
message = self.convert_packet_to_message(str(packet), community, verify=verify)
if message:
message.packet_id = packet_id
message.undone = undone
return message
def load_message_by_packetid(self, community, packet_id, verify=False):
"""
Returns the message identified by community, member, and global_time.
Each message is uniquely identified by the community that it is created in, the member it is
created by and the global time when it is created. Using these three parameters we return
the associated the Message.Implementation instance. None is returned when we do not have
this message or it can not be decoded.
"""
assert isinstance(community, Community), type(community)
assert isinstance(packet_id, (int, long)), type(packet_id)
try:
packet, undone = self._database.execute(u"SELECT packet, undone FROM sync WHERE id = ?",
(packet_id,)).next()
except StopIteration:
return None
message = self.convert_packet_to_message(str(packet), community, verify=verify)
if message:
message.packet_id = packet_id
message.undone = undone
return message
def convert_packet_to_message(self, packet, community=None, load=True, auto_load=True, candidate=None, verify=True):
"""
Returns the Message.Implementation representing the packet or None when no conversion is
possible.
"""
assert isinstance(packet, str), type(packet)
assert community is None or isinstance(community, Community), type(community)
assert isinstance(load, bool), type(load)
assert isinstance(auto_load, bool), type(auto_load)
assert candidate is None or isinstance(candidate, Candidate), type(candidate)
# find associated community
try:
if not community:
community = self.get_community(packet[2:22], load, auto_load)
# find associated conversion
conversion = community.get_conversion_for_packet(packet)
return conversion.decode_message(LoopbackCandidate() if candidate is None else candidate, packet, verify)
except CommunityNotFoundException:
self._logger.warning("unable to convert a %d byte packet (unknown community)", len(packet))
except ConversionNotFoundException:
self._logger.warning("unable to convert a %d byte packet (unknown conversion)", len(packet))
except (DropPacket, DelayPacket) as exception:
self._logger.warning("unable to convert a %d byte packet (%s)", len(packet), exception)
return None
def convert_packets_to_messages(self, packets, community=None, load=True, auto_load=True, candidate=None, verify=True):
"""
Returns a list with messages representing each packet or None when no conversion is
possible.
"""
assert isinstance(packets, Iterable), type(packets)
assert all(isinstance(packet, str) for packet in packets), [type(packet) for packet in packets]
return [self.convert_packet_to_message(packet, community, load, auto_load, candidate, verify) for packet in packets]
def on_incoming_packets(self, packets, cache=True, timestamp=0.0, source=u"unknown"):
"""
Process incoming UDP packets.
This method is called to process one or more UDP packets. This occurs when new packets are
received, to attempt to process previously delayed packets, or when a member explicitly
creates a packet to process. The last option should only occur for debugging purposes.
The following steps are followed:
1. Group the packets by community.
2. Try to obtain the community.
3. In case 2 suceeded: Pass the packets to the community for further processing.
"""
assert isinstance(packets, (tuple, list)), packets
assert len(packets) > 0, packets
assert all(isinstance(packet, tuple) for packet in packets), packets
assert all(len(packet) == 2 for packet in packets), packets # tuple(Candidate, datagram)
assert all(isinstance(packet[0], Candidate) for packet in packets), packets
assert all((is_valid_address(packet[0].sock_addr) for packet in packets)), packets
assert all(isinstance(packet[1], str) for packet in packets), packets
assert all(len(packet[1]) > 22 for packet in packets), [
(str(packet[0]), repr(packet[1])) for packet in packets]
assert isinstance(cache, bool), cache
assert isinstance(timestamp, float), timestamp
assert isinstance(source, unicode), source
if self.running:
self._statistics.total_received += len(packets)
# Ugly hack to sort the identity messages before any other to avoid sending missing identity requests
# for identities we have already received but not processed yet. (248 == identity message ID)
# /-------------------------------\
sort_key = lambda tup: (tup[1][2:22], tup[1][1], 0 if tup[1][22] == chr(248) else tup[1][22]) # community ID, community version, message meta type
groupby_key = lambda tup: tup[1][2:22] # community ID
for community_id, iterator in groupby(sorted(packets, key=sort_key), key=groupby_key):
# find associated community
try:
community = self.get_community(community_id)
community.on_incoming_packets(list(iterator), cache, timestamp, source)
except CommunityNotFoundException:
packets = list(iterator)
candidates = set([candidate for candidate, _ in packets])
self._logger.debug("drop %d packets (received packet(s) for unknown community): %s",
len(packets), map(str, candidates))
self._statistics.msg_statistics.increase_count(
u"drop", u"_convert_packets_into_batch:unknown community")
else:
self._logger.info("dropping %d packets as dispersy is not running", len(packets))
@attach_runtime_statistics(u"Dispersy.{function_name} {1[0].name}")
def _store(self, messages):
"""
Store a message in the database.
Messages with the Last- or Full-SyncDistribution policies need to be stored in the database
to allow them to propagate to other members.
Messages with the LastSyncDistribution policy may also cause an older message to be removed
from the database.