Ejemplo n.º 1
0
def main():
    # parse all the arguments to the client 
    parser = argparse.ArgumentParser(description='CS 352 Socket Client')
    parser.add_argument('-f','--filename', help='File to Send', required=False)
    parser.add_argument('-d','--destination', help='Destination IP Host', required=True)
    parser.add_argument('-p','--port', help='remote sock352 port', required=False)
    parser.add_argument('-u','--udpportRx', help='UDP port to use for receiving', required=True)
    parser.add_argument('-v','--udpportTx', help='UDP port to use for sending', required=False)
    parser.add_argument('-k','--keyfile', help='keyfile', required=True)

    # get the arguments into local variables 
    args = vars(parser.parse_args())
    filename = args['filename']
    destination = args['destination']
    udpportRx = args['udpportRx']
    keyfilename = args['keyfile']
    
    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''
        
    # the port is not used in part 2 assignment, except as a placeholder
    if (args['port']): 
        port = args['port']
    else:
        port = 5555 

    # open the file to send to the server for reading
    if (filename):
        try: 
            filesize = os.path.getsize(filename)
            fd = open(filename, "rb")
            usefile = True
        except:
            print ( "error opening file: %s" % (filename))
            exit(-1)
    else:
        pass 

    # This is where we set the transmit and receive
    # ports the client uses for the underlying UDP
    # sockets. If we are running the client and
    # server on the same machine, these ports
    # need to be different. If they are running on
    # different machines, we can re-use the same
    # ports. 
    if (udpportTx):
        sock352.init(udpportTx,udpportRx)
    else:
        sock352.init(udpportRx,udpportRx)

    # load a keychain from a file
    keysInHex = sock352.readKeyChain(keyfilename)
    
    # create a socket and connect to the remote server
    s = sock352.socket()
    s.connect((destination,port),sock352.ENCRYPT)

    #mesure the start stamp
    start_stamp = time.clock() 
	#load the whole file into memory
    whole_file = fd.read()
    #mesure its length
    filesize = len(whole_file)
    longPacker = struct.Struct("!L")
    fileLenPacked = longPacker.pack(filesize);
    s.send(fileLenPacked)
	
    sent = s.send(whole_file)
    if (sent != filesize):
        raise RuntimeError("socket broken")

    end_stamp = time.clock() 
    lapsed_seconds = end_stamp - start_stamp
    
    if (lapsed_seconds > 0.0):
        print ("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " % (filesize, lapsed_seconds, (filesize/lapsed_seconds)/(1024*1024)))
    else:
        print ("client1: sent %d bytes in %d seconds, inf MB/s " % (filesize, lapsed_seconds))        

    fd.close()
    s.close()
Ejemplo n.º 2
0
def main():
	
	# parse all the arguments to the client 
	parser = argparse.ArgumentParser(description='CS 352 Socket Server')
	parser.add_argument('-f','--filename', help='Filename to Receiver', required=False)
	parser.add_argument('-p','--port', help='CS 352 Socket Port (optional for part 1)', required=False)
	parser.add_argument('-u','--udpportRx', help='UDP port to use for receiving', required=True)
	parser.add_argument('-v','--udpportTx', help='UDP port to use for sending', required=False)
	parser.add_argument('-k','--keyfile', help='keyfile', required=True)

	args = vars(parser.parse_args())

	# open the file for writing
	filename = args['filename']
	udpportRx = args['udpportRx']
	keyfilename = args['keyfile']
	
	if (args['udpportTx']):
		udpportTx = args['udpportTx']
	else:
		udpportTx = ''
		
	# the port is not used in part 1 assignment, except as a placeholder
	if (args['port']): 
		port = args['port']
	else:
		port = 1111 
	
	if (filename):
		try: 
			fd = open(filename, "wb")
			print("filename is: " + str(filename))
			usefile = True
		except:
			print ( "error opening file: %s" % (filename))
			exit(-1)
	else:
		pass 


	# load a keychain from a file
	(publicKeys,privateKeys) = sock352.readKeyChain(keyfilename)

	# This is where we set the transmit and receive
	# ports the server uses for the underlying UDP
	# sockets. If we are running the client and
	# server on different machines, these ports
	# need to be different, otherwise we can
	# use the same ports
	if (udpportTx):
		sock352.init(udpportTx,udpportRx)
	else:
		sock352.init(udpportRx,udpportRx)

	s = sock352.socket()


	# binding the host to empty allows reception on
	# all network interfaces
	s.bind(('',port))
	s.listen(5)

	# when accept returns, the client is connected 
	(s2,address) = s.accept(sock352.ENCRYPT) 
	start_stamp = time.clock()

	# this receives the size of the file
	# as a 4 byte integer in network byte order (big endian)
	longPacker = struct.Struct("!L")
	long = s2.recv(4)
	fn = longPacker.unpack(long)
	filelen = fn[0]



	whole_file = s2.recv(filelen)
	fd.write(whole_file)

	end_stamp = time.clock() 
	lapsed_seconds = end_stamp - start_stamp

	status = "success"
	if (lapsed_seconds > 0.0):
		print ("server2: %s : received %d bytes in %0.6f seconds, %0.6f MB/s " % (status,filelen, lapsed_seconds, (filelen/lapsed_seconds)/(1024*1024)))
	else:
		print ("server2: %s : received %d bytes in %d seconds, inf MB/s " % (status,filelen, lapsed_seconds))
	fd.close()
	s2.close()
Ejemplo n.º 3
0
#fuser 8888/udp     <-- finds udp port 8888 being used
#fuser -k 8888/udp  <-- kills process running on port 8888

import sock352
import struct

sock352.init(9993, 8883)

(publicKeys, privateKeys) = sock352.readKeyChain('keychain.txt')

sock = sock352.socket()

sock.bind(('', 1111))
sock.listen(1)

(s, address) = sock.accept(sock352.ENCRYPT)

longPacker = struct.Struct("!L")
long = s.recv(4)

fn = longPacker.unpack(long)
size = fn[0]

message = s.recv(size)

print("Received: " + message)

s.close()
Ejemplo n.º 4
0
def main():
    # parse all the arguments to the client
    parser = argparse.ArgumentParser(description='CS 352 Socket Client')
    parser.add_argument('-f',
                        '--filename',
                        help='File to Send',
                        required=False)
    parser.add_argument('-d',
                        '--destination',
                        help='Destination IP Host',
                        required=True)
    parser.add_argument('-p',
                        '--port',
                        help='remote sock352 port',
                        required=False)
    parser.add_argument('-u',
                        '--udpportRx',
                        help='UDP port to use for receiving',
                        required=True)
    parser.add_argument('-v',
                        '--udpportTx',
                        help='UDP port to use for sending',
                        required=False)
    parser.add_argument('-k', '--keyfile', help='keyfile', required=True)

    # get the arguments into local variables
    args = vars(parser.parse_args())
    filename = args['filename']
    destination = args['destination']
    udpportRx = args['udpportRx']
    keyfilename = args['keyfile']

    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''

    # the port is not used in part 1 assignment, except as a placeholder
    if (args['port']):
        port = args['port']
    else:
        port = 5555

    # open the file to send to the server for reading
    if (filename):
        try:
            filesize = os.path.getsize(filename)
            fd = open(filename, "rb")
            usefile = True
        except:
            print("error opening file: %s" % (filename))
            exit(-1)
    else:
        pass

    # This is where we set the transmit and receive
    # ports the client uses for the underlying UDP
    # sockets. If we are running the client and
    # server on the same machine, these ports
    # need to be different. If they are running on
    # different machines, we can re-use the same
    # ports.
    if (udpportTx):
        sock352.init(udpportTx, udpportRx)
    else:
        sock352.init(udpportRx, udpportRx)

    # load a keychain from a file
    keysInHex = sock352.readKeyChain(keyfilename)

    # create a socket and connect to the remote server
    s = sock352.socket()
    s.connect((destination, port), sock352.ENCRYPT)

    # send the size of the file as a 4 byte integer
    # to the server, so it knows how much to read
    FRAGMENTSIZE = 8192
    longPacker = struct.Struct("!L")
    fileLenPacked = longPacker.pack(filesize)
    s.send(fileLenPacked)

    # use the MD5 hash algorithm to validate all the data is correct
    mdhash = md5.new()

    # loop for the size of the file, sending the fragments
    bytes_to_send = filesize

    start_stamp = time.clock()
    while (bytes_to_send > 0):
        fragment = fd.read(FRAGMENTSIZE)
        mdhash.update(fragment)
        totalsent = 0
        # make sure we sent the whole fragment
        while (totalsent < len(fragment)):
            sent = s.send(fragment[totalsent:])
            if (sent == 0):
                raise RuntimeError("socket broken")
            totalsent = totalsent + sent
        bytes_to_send = bytes_to_send - len(fragment)

    end_stamp = time.clock()
    lapsed_seconds = end_stamp - start_stamp

    # this part send the lenght of the digest, then the
    # digest. It will be check on the server

    digest = mdhash.digest()
    # send the length of the digest
    long = len(digest)
    digestLenPacked = longPacker.pack(long)
    sent = s.send(digestLenPacked)
    if (sent != 4):
        raise RuntimeError("socket broken")

    # send the digest
    sent = s.send(digest)
    if (sent != len(digest)):
        raise RuntimeError("socket broken")

    if (lapsed_seconds > 0.0):
        print("client1: sent %d bytes in %0.6f seconds, %0.6f MB/s " %
              (filesize, lapsed_seconds,
               (filesize / lapsed_seconds) / (1024 * 1024)))
    else:
        print("client1: sent %d bytes in %d seconds, inf MB/s " %
              (filesize, lapsed_seconds))

    fd.close()
    s.close()
Ejemplo n.º 5
0
def main():

    # parse all the arguments to the client
    parser = argparse.ArgumentParser(description='CS 352 Socket Server')
    parser.add_argument('-f',
                        '--filename',
                        help='Filename to Receiver',
                        required=False)
    parser.add_argument('-p',
                        '--port',
                        help='CS 352 Socket Port (optional for part 1)',
                        required=False)
    parser.add_argument('-u',
                        '--udpportRx',
                        help='UDP port to use for receiving',
                        required=True)
    parser.add_argument('-v',
                        '--udpportTx',
                        help='UDP port to use for sending',
                        required=False)
    parser.add_argument('-k', '--keyfile', help='keyfile', required=True)

    args = vars(parser.parse_args())

    # open the file for writing
    filename = args['filename']
    udpportRx = args['udpportRx']
    keyfilename = args['keyfile']

    if (args['udpportTx']):
        udpportTx = args['udpportTx']
    else:
        udpportTx = ''

    # the port is not used in part 1 assignment, except as a placeholder
    if (args['port']):
        port = args['port']
    else:
        port = 1111

    if (filename):
        try:
            fd = open(filename, "wb")
            usefile = True
        except:
            print("error opening file: %s" % (filename))
            exit(-1)
    else:
        pass

    # load a keychain from a file
    (publicKeys, privateKeys) = sock352.readKeyChain(keyfilename)

    # This is where we set the transmit and receive
    # ports the server uses for the underlying UDP
    # sockets. If we are running the client and
    # server on different machines, these ports
    # need to be different, otherwise we can
    # use the same ports
    if (udpportTx):
        sock352.init(udpportTx, udpportRx)
    else:
        sock352.init(udpportRx, udpportRx)

    s = sock352.socket()

    # set the fragment size we will read on
    FRAGMENTSIZE = 4096

    # binding the host to empty allows reception on
    # all network interfaces
    s.bind(('', port))
    s.listen(5)

    # when accept returns, the client is connected
    (s2, address) = s.accept(sock352.ENCRYPT)

    # this receives the size of the file
    # as a 4 byte integer in network byte order (big endian)
    longPacker = struct.Struct("!L")
    long = s2.recv(4)
    fn = longPacker.unpack(long)
    filelen = fn[0]

    # the MD5 computes a unique hash for all the data
    mdhash = md5.new()

    bytes_to_receive = filelen
    start_stamp = time.clock()

    # main loop to receive the data from the client
    while (bytes_to_receive > 0):
        if (bytes_to_receive >= FRAGMENTSIZE):
            fragment = s2.recv(FRAGMENTSIZE)
        else:
            fragment = s2.recv(bytes_to_receive)

        mdhash.update(fragment)
        bytes_to_receive = bytes_to_receive - len(fragment)
        fd.write(fragment)

    end_stamp = time.clock()
    lapsed_seconds = end_stamp - start_stamp

    # finish computing the MD5 hash
    local_digest = mdhash.digest()

    # receive the size of the remote hash
    dl = longPacker.unpack(s2.recv(4))
    digestlen = dl[0]
    remote_digest = s2.recv(digestlen)

    # check is the size matches
    if (len(remote_digest) != digestlen):
        raise RuntimeError("socket error")

    status = "success"
    # compare the two digests, byte for byte
    for i, server_byte in enumerate(local_digest):
        client_byte = remote_digest[i]
        if (client_byte != server_byte):
            print("digest failed at byte %d %c %c " %
                  (i, client_byte, server_byte))
            status = "failed"

    if (lapsed_seconds > 0.0):
        print("server2: %s : received %d bytes in %0.6f seconds, %0.6f MB/s " %
              (status, filelen, lapsed_seconds,
               (filelen / lapsed_seconds) / (1024 * 1024)))
    else:
        print("server2: %s : received %d bytes in %d seconds, inf MB/s " %
              (status, filelen, lapsed_seconds))
    fd.close()
    s2.close()
Ejemplo n.º 6
0
import sock352
import struct
import socket as syssock

sock352.init(8883, 9993)

keysInHex = sock352.readKeyChain('keychain.txt')

s = sock352.socket()

s.connect(('127.0.0.1', 1111), sock352.ENCRYPT)

message = b"I never offered Pardons to Homeland Security Officials, never ordered anyone to close our Southern Border (although I have the absolute right to do so, and may if Mexico does not apprehend the illegals coming to our Border), and am not \"frustrated.\" It is all Fake & Corrupt News!"

size = len(message)
longPacker = struct.Struct("!L")
package = longPacker.pack(size)
s.send(package)

b = s.send(message)

print("Sent " + str(b) + " bytes")

s.close()