OLD | NEW |
(Empty) | |
| 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 |
| 4 # met: |
| 5 # |
| 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. |
| 15 # |
| 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. |
| 27 |
| 28 |
| 29 import socket |
| 30 import SocketServer |
| 31 |
| 32 from . import compression |
| 33 from . import constants |
| 34 from . import discovery |
| 35 |
| 36 |
| 37 def _StatusQuery(peer, query): |
| 38 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 39 code = sock.connect_ex((peer.address, constants.STATUS_PORT)) |
| 40 if code != 0: |
| 41 # TODO(jkummerow): disconnect (after 3 failures?) |
| 42 return |
| 43 compression.Send(query, sock) |
| 44 compression.Send(constants.END_OF_STREAM, sock) |
| 45 rec = compression.Receiver(sock) |
| 46 data = None |
| 47 while not rec.IsDone(): |
| 48 data = rec.Current() |
| 49 assert data[0] == query[0] |
| 50 data = data[1] |
| 51 rec.Advance() |
| 52 sock.close() |
| 53 return data |
| 54 |
| 55 |
| 56 def RequestTrustedPubkeys(peer, server): |
| 57 pubkey_list = _StatusQuery(peer, [constants.LIST_TRUSTED_PUBKEYS]) |
| 58 for pubkey in pubkey_list: |
| 59 if server.IsTrusted(pubkey): continue |
| 60 result = _StatusQuery(peer, [constants.GET_SIGNED_PUBKEY, pubkey]) |
| 61 server.AcceptNewTrusted(result) |
| 62 |
| 63 |
| 64 def NotifyNewTrusted(peer, data): |
| 65 _StatusQuery(peer, [constants.NOTIFY_NEW_TRUSTED] + data) |
| 66 |
| 67 |
| 68 def ITrustYouNow(peer): |
| 69 _StatusQuery(peer, [constants.TRUST_YOU_NOW]) |
| 70 |
| 71 |
| 72 def TryTransitiveTrust(peer, pubkey, server): |
| 73 if _StatusQuery(peer, [constants.DO_YOU_TRUST, pubkey]): |
| 74 result = _StatusQuery(peer, [constants.GET_SIGNED_PUBKEY, pubkey]) |
| 75 server.AcceptNewTrusted(result) |
| 76 |
| 77 |
| 78 class StatusHandler(SocketServer.BaseRequestHandler): |
| 79 def handle(self): |
| 80 rec = compression.Receiver(self.request) |
| 81 while not rec.IsDone(): |
| 82 data = rec.Current() |
| 83 action = data[0] |
| 84 |
| 85 if action == constants.LIST_TRUSTED_PUBKEYS: |
| 86 response = self.server.daemon.ListTrusted() |
| 87 compression.Send([action, response], self.request) |
| 88 |
| 89 elif action == constants.GET_SIGNED_PUBKEY: |
| 90 response = self.server.daemon.SignTrusted(data[1]) |
| 91 compression.Send([action, response], self.request) |
| 92 |
| 93 elif action == constants.NOTIFY_NEW_TRUSTED: |
| 94 self.server.daemon.AcceptNewTrusted(data[1:]) |
| 95 pass # No response. |
| 96 |
| 97 elif action == constants.TRUST_YOU_NOW: |
| 98 self.server.daemon.MarkPeerAsTrusting(self.client_address[0]) |
| 99 pass # No response. |
| 100 |
| 101 elif action == constants.DO_YOU_TRUST: |
| 102 response = self.server.daemon.IsTrusted(data[1]) |
| 103 compression.Send([action, response], self.request) |
| 104 |
| 105 rec.Advance() |
| 106 compression.Send(constants.END_OF_STREAM, self.request) |
| 107 |
| 108 |
| 109 class StatusSocketServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): |
| 110 def __init__(self, daemon): |
| 111 address = (daemon.ip, constants.STATUS_PORT) |
| 112 SocketServer.TCPServer.__init__(self, address, StatusHandler) |
| 113 self.daemon = daemon |
OLD | NEW |