Cleanup and echo example
This commit is contained in:
parent
e66622bd69
commit
cfb1ed84d2
|
@ -179,7 +179,6 @@ class Identity:
|
||||||
if self.pub != None:
|
if self.pub != None:
|
||||||
chunksize = (Identity.KEYSIZE-Identity.PADDINGSIZE)/8
|
chunksize = (Identity.KEYSIZE-Identity.PADDINGSIZE)/8
|
||||||
chunks = int(math.ceil(len(plaintext)/(float(chunksize))))
|
chunks = int(math.ceil(len(plaintext)/(float(chunksize))))
|
||||||
# TODO: Remove debug output print("Plaintext size is "+str(len(plaintext))+", with "+str(chunks)+" chunks")
|
|
||||||
|
|
||||||
ciphertext = "";
|
ciphertext = "";
|
||||||
for chunk in range(chunks):
|
for chunk in range(chunks):
|
||||||
|
@ -187,8 +186,6 @@ class Identity:
|
||||||
end = (chunk+1)*chunksize
|
end = (chunk+1)*chunksize
|
||||||
if (chunk+1)*chunksize > len(plaintext):
|
if (chunk+1)*chunksize > len(plaintext):
|
||||||
end = len(plaintext)
|
end = len(plaintext)
|
||||||
|
|
||||||
# TODO: Remove debug output print("Processing chunk "+str(chunk+1)+" of "+str(chunks)+". Starting at "+str(start)+" and stopping at "+str(end)+". The length is "+str(len(plaintext[start:end])))
|
|
||||||
|
|
||||||
ciphertext += self.pub.encrypt(
|
ciphertext += self.pub.encrypt(
|
||||||
plaintext[start:end],
|
plaintext[start:end],
|
||||||
|
@ -198,7 +195,6 @@ class Identity:
|
||||||
label=None
|
label=None
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
# TODO: Remove debug output print("Plaintext encrypted, ciphertext length is "+str(len(ciphertext))+" bytes.")
|
|
||||||
return ciphertext
|
return ciphertext
|
||||||
else:
|
else:
|
||||||
raise KeyError("Encryption failed because identity does not hold a public key")
|
raise KeyError("Encryption failed because identity does not hold a public key")
|
||||||
|
|
|
@ -9,7 +9,6 @@ import FPE
|
||||||
|
|
||||||
class SerialInterface(Interface):
|
class SerialInterface(Interface):
|
||||||
MAX_CHUNK = 32768
|
MAX_CHUNK = 32768
|
||||||
TIMEOUT_SECONDS = 1.0
|
|
||||||
|
|
||||||
owner = None
|
owner = None
|
||||||
port = None
|
port = None
|
||||||
|
|
|
@ -37,13 +37,9 @@ class UdpInterface(Interface):
|
||||||
|
|
||||||
|
|
||||||
def processIncoming(self, data):
|
def processIncoming(self, data):
|
||||||
# TODO: remove
|
|
||||||
#FPE.log("IN: "+FPE.prettyhexrep(data))
|
|
||||||
self.owner.inbound(data)
|
self.owner.inbound(data)
|
||||||
|
|
||||||
def processOutgoing(self,data):
|
def processOutgoing(self,data):
|
||||||
# TODO: remove
|
|
||||||
#FPE.log("OUT: "+FPE.prettyhexrep(" "+data))
|
|
||||||
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
udp_socket.sendto(data, (self.forward_ip, self.forward_port))
|
udp_socket.sendto(data, (self.forward_ip, self.forward_port))
|
||||||
|
@ -57,7 +53,5 @@ class UdpInterfaceHandler(SocketServer.BaseRequestHandler):
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
if (UdpInterfaceHandler.interface != None):
|
if (UdpInterfaceHandler.interface != None):
|
||||||
# TODO: remove
|
|
||||||
#FPE.log("Datagram contents: "+FPE.prettyhexrep(self.request[0]))
|
|
||||||
data = self.request[0]
|
data = self.request[0]
|
||||||
UdpInterfaceHandler.interface.processIncoming(data)
|
UdpInterfaceHandler.interface.processIncoming(data)
|
|
@ -179,31 +179,55 @@ def client(destination_hexhash, configpath):
|
||||||
# user to wait for an announce to arrive.
|
# user to wait for an announce to arrive.
|
||||||
FPE.log("Destination is not yet known. Wait for an announce to arrive.")
|
FPE.log("Destination is not yet known. Wait for an announce to arrive.")
|
||||||
|
|
||||||
|
# This method is called when our reply destination
|
||||||
|
# receives a proof packet.
|
||||||
def clientProofCallback(proof_packet):
|
def clientProofCallback(proof_packet):
|
||||||
|
# We save the current time so we can calculate
|
||||||
|
# round-trip time for the packet
|
||||||
now = time.time()
|
now = time.time()
|
||||||
|
|
||||||
|
# Let's look through our list of sent requests,
|
||||||
|
# and see if we can find one that matches the
|
||||||
|
# proof we just received.
|
||||||
for unproven_packet in sent_requests:
|
for unproven_packet in sent_requests:
|
||||||
if unproven_packet.packet_hash == proof_packet.data[:32]:
|
try:
|
||||||
if unproven_packet.validateProofPacket(proof_packet):
|
# Check that the proof hash matches the
|
||||||
rtt = now - unproven_packet.sent_at
|
# hash of the packet we sent earlier
|
||||||
if (rtt >= 1):
|
if unproven_packet.packet_hash == proof_packet.data[:32]:
|
||||||
rtt = round(rtt, 3)
|
# We need to actually calidate the proof.
|
||||||
rttstring = str(rtt)+" seconds"
|
# This is simply done by calling the
|
||||||
|
# validateProofPacket method on the packet
|
||||||
|
# we sent earlier.
|
||||||
|
if unproven_packet.validateProofPacket(proof_packet):
|
||||||
|
# If the proof is valid, we will calculate
|
||||||
|
# the round-trip time, and inform the user.
|
||||||
|
rtt = now - unproven_packet.sent_at
|
||||||
|
if (rtt >= 1):
|
||||||
|
rtt = round(rtt, 3)
|
||||||
|
rttstring = str(rtt)+" seconds"
|
||||||
|
else:
|
||||||
|
rtt = round(rtt*1000, 3)
|
||||||
|
rttstring = str(rtt)+" milliseconds"
|
||||||
|
|
||||||
|
FPE.log(
|
||||||
|
"Valid echo reply, proved by "+FPE.prettyhexrep(unproven_packet.destination.hash)+
|
||||||
|
", round-trip time was "+rttstring
|
||||||
|
)
|
||||||
|
# Perform some cleanup
|
||||||
|
sent_requests.remove(unproven_packet)
|
||||||
|
del unproven_packet
|
||||||
else:
|
else:
|
||||||
rtt = round(rtt*1000, 3)
|
# If the proof was invalid, we inform
|
||||||
rttstring = str(rtt)+" milliseconds"
|
# the user of this.
|
||||||
|
FPE.log("Echo reply received, but proof was invalid")
|
||||||
FPE.log(
|
except:
|
||||||
"Valid echo reply, proved by "+FPE.prettyhexrep(unproven_packet.destination.hash)+
|
FPE.log("Proof packet received, but packet contained invalid or unparsable data")
|
||||||
", round-trip time was "+rttstring
|
|
||||||
)
|
|
||||||
sent_requests.remove(unproven_packet)
|
|
||||||
del unproven_packet
|
|
||||||
else:
|
|
||||||
FPE.log("Proof invalid")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
# Set up command line arguments and start
|
||||||
|
# the selected program mode.
|
||||||
try:
|
try:
|
||||||
parser = argparse.ArgumentParser(description="Simple echo server and client utility")
|
parser = argparse.ArgumentParser(description="Simple echo server and client utility")
|
||||||
parser.add_argument("-s", "--server", action="store_true", help="wait for incoming packets from clients")
|
parser.add_argument("-s", "--server", action="store_true", help="wait for incoming packets from clients")
|
||||||
|
|
Loading…
Reference in New Issue