2009-03-01 08:47:22 +01:00
|
|
|
import socket, struct, syslog
|
2009-02-07 12:08:50 +01:00
|
|
|
import SocketServer
|
|
|
|
|
|
|
|
FRODO_NETWORK_MAGIC = 0x1976
|
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
CONNECT_TO_BROKER = 99 # Hello, broker
|
|
|
|
LIST_PEERS = 98 # List of peers
|
|
|
|
CONNECT_TO_PEER = 97 # A peer wants to connect
|
|
|
|
SELECT_PEER = 93 # The client selects who to connect to
|
|
|
|
DISCONNECT = 96 # Disconnect from a peer
|
2009-03-08 10:00:13 +01:00
|
|
|
PING = 95 # Are you alive?
|
|
|
|
ACK = 94 # Yep
|
2009-02-28 19:45:26 +01:00
|
|
|
STOP = 55 # No more packets
|
2009-02-07 12:08:50 +01:00
|
|
|
|
2009-03-01 08:47:22 +01:00
|
|
|
def log(pri, msg, echo):
|
|
|
|
syslog.syslog(pri, msg)
|
2009-03-08 10:00:13 +01:00
|
|
|
# if echo:
|
|
|
|
print msg
|
2009-03-01 08:47:22 +01:00
|
|
|
|
|
|
|
def log_error(msg, echo = False):
|
|
|
|
log(syslog.LOG_ERR, msg, echo)
|
|
|
|
|
|
|
|
def log_warn(msg, echo = False):
|
|
|
|
log(syslog.LOG_WARNING, msg, echo)
|
|
|
|
|
|
|
|
def log_info(msg, echo = False):
|
|
|
|
log(syslog.LOG_INFO, msg, echo)
|
|
|
|
|
2009-02-07 12:08:50 +01:00
|
|
|
class Packet:
|
2009-02-28 19:45:26 +01:00
|
|
|
def __init__(self):
|
|
|
|
"""Create a new packet"""
|
|
|
|
self.magic = FRODO_NETWORK_MAGIC
|
|
|
|
self.type = 0
|
|
|
|
self.size = 8
|
|
|
|
|
|
|
|
def demarshal_from_data(self, data):
|
2009-02-07 12:08:50 +01:00
|
|
|
"""Create a new packet from raw data. Data should always be in network
|
|
|
|
byte order"""
|
2009-03-08 10:00:13 +01:00
|
|
|
self.magic = struct.unpack(">H", data[0:2])[0]
|
|
|
|
self.type = struct.unpack(">H", data[2:4])[0]
|
|
|
|
self.size = struct.unpack(">L", data[4:8])[0]
|
2009-02-07 12:08:50 +01:00
|
|
|
|
|
|
|
def get_magic(self):
|
|
|
|
return self.magic
|
|
|
|
|
|
|
|
def get_type(self):
|
|
|
|
return self.type
|
|
|
|
|
|
|
|
def get_size(self):
|
|
|
|
return self.size
|
|
|
|
|
|
|
|
def marshal(self):
|
|
|
|
return struct.pack(">HHL", self.magic, self.type, self.size)
|
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
class StopPacket(Packet):
|
|
|
|
def __init__(self):
|
|
|
|
Packet.__init__(self)
|
|
|
|
self.type = STOP
|
2009-02-07 12:08:50 +01:00
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
class SelectPeerPacket(Packet):
|
|
|
|
def __init__(self):
|
|
|
|
Packet.__init__(self)
|
|
|
|
self.type = SELECT_PEER
|
|
|
|
self.server_id = 0
|
2009-02-07 12:08:50 +01:00
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
def demarshal_from_data(self, data):
|
|
|
|
"""Create a new packet from raw data."""
|
|
|
|
Packet.demarshal_from_data(self, data)
|
2009-03-28 12:32:59 +01:00
|
|
|
self.server_id = struct.unpack(">L", data[8:12])[0]
|
2009-02-07 12:08:50 +01:00
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
def get_id(self):
|
|
|
|
return self.server_id
|
2009-02-07 12:08:50 +01:00
|
|
|
|
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
class ConnectToBrokerPacket(Packet):
|
2009-02-07 12:08:50 +01:00
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.key = 0
|
|
|
|
self._is_master = 0
|
2009-02-07 19:24:50 +01:00
|
|
|
self.private_port = 0
|
|
|
|
self.public_port = 0
|
2009-02-28 19:45:26 +01:00
|
|
|
self.private_ip = ""
|
|
|
|
self.public_ip = ""
|
|
|
|
self.type = CONNECT_TO_BROKER
|
2009-02-07 19:24:50 +01:00
|
|
|
self.name = ""
|
2009-02-28 19:45:26 +01:00
|
|
|
self.serverid = 0
|
2009-02-07 19:24:50 +01:00
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
def demarshal_from_data(self, data):
|
|
|
|
Packet.demarshal_from_data(self, data)
|
2009-02-07 19:24:50 +01:00
|
|
|
|
2009-03-08 10:00:13 +01:00
|
|
|
self.key = struct.unpack(">H", data[44:46])[0]
|
|
|
|
self._is_master = struct.unpack(">H", data[46:48])[0]
|
|
|
|
self.name = struct.unpack(">32s", data[48:48+32])[0]
|
2009-02-07 12:08:50 +01:00
|
|
|
|
2009-02-07 19:24:50 +01:00
|
|
|
def get_key(self):
|
|
|
|
return self.key
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
return self.name
|
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
def is_master(self):
|
|
|
|
return self._is_master
|
|
|
|
|
|
|
|
class ListPeersPacket(Packet):
|
|
|
|
def __init__(self):
|
|
|
|
Packet.__init__(self)
|
|
|
|
self.n_peers = 0
|
|
|
|
self.peers = []
|
|
|
|
self.type = LIST_PEERS
|
|
|
|
self.size = self.size + 24
|
|
|
|
|
|
|
|
def add_peer(self, peer):
|
|
|
|
self.peers.append(peer)
|
|
|
|
self.n_peers = self.n_peers + 1
|
|
|
|
self.size = self.size + 76
|
|
|
|
|
|
|
|
def marshal(self):
|
|
|
|
out = struct.pack(">L16sHxx", self.n_peers, "", 0)
|
|
|
|
|
|
|
|
for peer in self.peers:
|
|
|
|
out = out + struct.pack(">HH16s16sHH32sL",
|
|
|
|
0, peer.public_port, "",
|
|
|
|
peer.public_ip, peer.key,
|
|
|
|
peer.is_master, peer.name,
|
|
|
|
peer.id)
|
|
|
|
|
|
|
|
return Packet.marshal(self) + out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Peer:
|
|
|
|
def __init__(self, addr, srv, id):
|
|
|
|
self.srv = srv
|
|
|
|
|
|
|
|
self.addr = addr
|
|
|
|
self.public_ip, self.public_port = self.addr_to_ip_and_port(addr)
|
|
|
|
# These will be set by the CONNECT_TO_BROKER packet below
|
|
|
|
self.key = 0
|
|
|
|
self.name = ""
|
|
|
|
self.is_master = 0
|
|
|
|
self.id = id
|
|
|
|
|
|
|
|
def addr_to_ip_and_port(self, addr):
|
|
|
|
ip = struct.unpack("@L", socket.inet_pton(socket.AF_INET, addr[0]))[0]
|
|
|
|
port = addr[1]
|
|
|
|
return "%08x" % (ip), port
|
|
|
|
|
|
|
|
def handle_packet(self, pkt):
|
|
|
|
if pkt.type == CONNECT_TO_BROKER:
|
|
|
|
self.key = pkt.get_key()
|
|
|
|
self.name = pkt.get_name()
|
|
|
|
self.is_master = pkt.is_master()
|
|
|
|
|
|
|
|
# Send list of peers if this is not a master
|
|
|
|
if not self.is_master:
|
|
|
|
lp = ListPeersPacket()
|
|
|
|
|
|
|
|
for peer in self.srv.peers.itervalues():
|
|
|
|
if peer != self and peer.is_master:
|
|
|
|
lp.add_peer(peer)
|
|
|
|
# And send the packet to this peer
|
2009-03-01 15:26:40 +01:00
|
|
|
log_info("Sending list of peers (%d) to %s:%d" % (lp.n_peers,
|
|
|
|
self.addr[0], self.addr[1]) )
|
2009-02-28 19:45:26 +01:00
|
|
|
self.send_packet(lp.marshal())
|
|
|
|
|
|
|
|
if pkt.type == SELECT_PEER:
|
|
|
|
peer = self.srv.get_peer_by_id( pkt.get_id() )
|
|
|
|
|
|
|
|
# Tell the peer that we have connected
|
|
|
|
lp = ListPeersPacket()
|
|
|
|
lp.add_peer(self)
|
2009-03-01 15:26:40 +01:00
|
|
|
log_info("Sending list of peers for peer selected to %s:%d" % (
|
|
|
|
self.addr[0], self.addr[1]))
|
2009-02-28 19:45:26 +01:00
|
|
|
peer.send_packet( lp.marshal() )
|
|
|
|
|
|
|
|
# These two are no longer needed
|
|
|
|
self.srv.remove_peer(peer)
|
|
|
|
self.srv.remove_peer(self)
|
|
|
|
|
|
|
|
def send_packet(self, data):
|
|
|
|
self.srv.socket.sendto(data + StopPacket().marshal(),
|
|
|
|
0, self.addr)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '%s:%d "%s" %d %d' % (self.public_ip, self.public_port,
|
|
|
|
self.name, self.key, self.is_master)
|
2009-02-07 12:08:50 +01:00
|
|
|
|
|
|
|
class BrokerPacketHandler(SocketServer.DatagramRequestHandler):
|
2009-02-28 19:45:26 +01:00
|
|
|
def get_packet_from_data(self, data):
|
2009-03-08 10:00:13 +01:00
|
|
|
magic = struct.unpack(">H", data[0:2])[0]
|
|
|
|
type = struct.unpack(">H", data[2:4])[0]
|
2009-02-28 19:45:26 +01:00
|
|
|
|
|
|
|
if magic != FRODO_NETWORK_MAGIC:
|
|
|
|
raise Exception("Packet magic does not match: %4x vs %4x\n" % (magic,
|
|
|
|
FRODO_NETWORK_MAGIC) )
|
|
|
|
try:
|
|
|
|
out = packet_class_by_type[type]()
|
|
|
|
out.demarshal_from_data(data)
|
|
|
|
return out
|
|
|
|
except KeyError, e:
|
|
|
|
raise Exception("Unknown packet type %d" % (type))
|
|
|
|
|
2009-02-07 12:08:50 +01:00
|
|
|
def handle(self):
|
|
|
|
srv = self.server
|
2009-02-28 19:45:26 +01:00
|
|
|
data = self.rfile.read()
|
|
|
|
|
|
|
|
try:
|
|
|
|
pkt = self.get_packet_from_data(data)
|
|
|
|
except Exception, e:
|
2009-03-01 08:47:22 +01:00
|
|
|
log_error("Broken packet: %s" % e)
|
2009-02-28 19:45:26 +01:00
|
|
|
return
|
|
|
|
|
2009-03-01 08:47:22 +01:00
|
|
|
log_info("Received packet %d from %s:%d" % (pkt.get_type(), self.client_address[0],
|
|
|
|
self.client_address[1]))
|
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
peer = srv.get_peer(self.client_address)
|
2009-02-07 12:08:50 +01:00
|
|
|
|
|
|
|
try:
|
2009-02-28 19:45:26 +01:00
|
|
|
peer.handle_packet(pkt)
|
|
|
|
except Exception, e:
|
2009-02-07 12:08:50 +01:00
|
|
|
# Sends crap, let's remove it
|
2009-03-01 08:47:22 +01:00
|
|
|
log_error("Handling packet failed, removing peer: %s" % e)
|
2009-02-07 12:08:50 +01:00
|
|
|
srv.remove_peer(peer)
|
|
|
|
|
|
|
|
class Broker(SocketServer.UDPServer):
|
|
|
|
|
|
|
|
def __init__(self, host, req_handler):
|
|
|
|
SocketServer.UDPServer.__init__(self, host, req_handler)
|
|
|
|
# Instead of setsockopt( ... REUSEADDR ... )
|
|
|
|
self.allow_reuse_address = True
|
|
|
|
self.peers = {}
|
2009-02-28 19:45:26 +01:00
|
|
|
self.peers_by_id = {}
|
|
|
|
self.id = 0
|
2009-02-07 12:08:50 +01:00
|
|
|
|
2009-02-07 19:24:50 +01:00
|
|
|
def send_data(self, dst, data):
|
|
|
|
self.socket.sendto(data, dst)
|
|
|
|
|
2009-02-07 12:08:50 +01:00
|
|
|
def get_peer(self, key):
|
|
|
|
"Return the peer for a certain key, or a new one if it doesn't exist"
|
|
|
|
try:
|
|
|
|
peer = self.peers[key]
|
|
|
|
except KeyError, e:
|
2009-02-28 19:45:26 +01:00
|
|
|
peer = Peer(key, self, self.id)
|
|
|
|
self.peers[key] = peer
|
|
|
|
self.peers_by_id[self.id] = peer
|
|
|
|
self.id = self.id + 1
|
2009-02-07 12:08:50 +01:00
|
|
|
return peer
|
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
def get_peer_by_id(self, id):
|
|
|
|
return self.peers_by_id[id]
|
|
|
|
|
2009-02-07 19:24:50 +01:00
|
|
|
def get_peer_by_name_key(self, name, key):
|
|
|
|
for k,v in self.peers.iteritems():
|
|
|
|
if name == v.get_name() and key == v.get_key():
|
|
|
|
return v
|
|
|
|
return None
|
|
|
|
|
2009-02-07 12:08:50 +01:00
|
|
|
def remove_peer(self, peer):
|
2009-03-01 15:26:40 +01:00
|
|
|
try:
|
|
|
|
del self.peers[ peer.addr ]
|
|
|
|
del self.peers_by_id[ peer.id ]
|
|
|
|
except:
|
|
|
|
log_error("Could not remove %s, something is wrong" % (str(peer.addr)))
|
2009-02-28 19:45:26 +01:00
|
|
|
|
|
|
|
# Some of the Frodo network packets. There are more packets, but these
|
|
|
|
# are not interesting to the broker (and shouldn't be sent there either!)
|
|
|
|
packet_class_by_type = {
|
|
|
|
CONNECT_TO_BROKER : ConnectToBrokerPacket,
|
|
|
|
SELECT_PEER : SelectPeerPacket,
|
|
|
|
}
|
2009-02-07 12:08:50 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2009-03-01 08:47:22 +01:00
|
|
|
syslog.openlog("frodo")
|
|
|
|
log_info("Starting Frodo network broker", True)
|
2009-03-08 10:00:13 +01:00
|
|
|
s = Broker( ("", 46214), BrokerPacketHandler)
|
2009-02-07 12:08:50 +01:00
|
|
|
s.serve_forever()
|