1
# Copyright 2012 the V8 project authors. All rights reserved.
2
# Redistribution and use in source and binary forms, with or without
3
# modification, are permitted provided that the following conditions are
6
# * Redistributions of source code must retain the above copyright
7
# notice, this list of conditions and the following disclaimer.
8
# * Redistributions in binary form must reproduce the above
9
# copyright notice, this list of conditions and the following
10
# disclaimer in the documentation and/or other materials provided
11
# with the distribution.
12
# * Neither the name of Google Inc. nor the names of its
13
# contributors may be used to endorse or promote products derived
14
# from this software without specific prior written permission.
16
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
from . import compression
33
from . import constants
36
def _StatusQuery(peer, query):
37
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
38
code = sock.connect_ex((peer.address, constants.STATUS_PORT))
40
# TODO(jkummerow): disconnect (after 3 failures?)
42
compression.Send(query, sock)
43
compression.Send(constants.END_OF_STREAM, sock)
44
rec = compression.Receiver(sock)
46
while not rec.IsDone():
48
assert data[0] == query[0]
55
def RequestTrustedPubkeys(peer, server):
56
pubkey_list = _StatusQuery(peer, [constants.LIST_TRUSTED_PUBKEYS])
57
for pubkey in pubkey_list:
58
if server.IsTrusted(pubkey): continue
59
result = _StatusQuery(peer, [constants.GET_SIGNED_PUBKEY, pubkey])
60
server.AcceptNewTrusted(result)
63
def NotifyNewTrusted(peer, data):
64
_StatusQuery(peer, [constants.NOTIFY_NEW_TRUSTED] + data)
67
def ITrustYouNow(peer):
68
_StatusQuery(peer, [constants.TRUST_YOU_NOW])
71
def TryTransitiveTrust(peer, pubkey, server):
72
if _StatusQuery(peer, [constants.DO_YOU_TRUST, pubkey]):
73
result = _StatusQuery(peer, [constants.GET_SIGNED_PUBKEY, pubkey])
74
server.AcceptNewTrusted(result)
77
class StatusHandler(SocketServer.BaseRequestHandler):
79
rec = compression.Receiver(self.request)
80
while not rec.IsDone():
84
if action == constants.LIST_TRUSTED_PUBKEYS:
85
response = self.server.daemon.ListTrusted()
86
compression.Send([action, response], self.request)
88
elif action == constants.GET_SIGNED_PUBKEY:
89
response = self.server.daemon.SignTrusted(data[1])
90
compression.Send([action, response], self.request)
92
elif action == constants.NOTIFY_NEW_TRUSTED:
93
self.server.daemon.AcceptNewTrusted(data[1:])
96
elif action == constants.TRUST_YOU_NOW:
97
self.server.daemon.MarkPeerAsTrusting(self.client_address[0])
100
elif action == constants.DO_YOU_TRUST:
101
response = self.server.daemon.IsTrusted(data[1])
102
compression.Send([action, response], self.request)
105
compression.Send(constants.END_OF_STREAM, self.request)
108
class StatusSocketServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
109
def __init__(self, daemon):
110
address = (daemon.ip, constants.STATUS_PORT)
111
SocketServer.TCPServer.__init__(self, address, StatusHandler)