# csc523sept2023.parts6and8.py Outline of an approach to STUDENT part 6 of # Assignment 1, CSC523 Fall 2023. # D. Parson def matcher(DICT, timestamp, srcip, srcport, dstip, dstport, bytes): ''' matcher outlines the code of part 6 as a stand-alone function for demo purposes, but you will just integrate it into the STUDENT 6 code as an if...elif...else construct. I am supplying some fake, demo values for these parameters. ''' srcaddr = srcip + ':' + srcport dstaddr = dstip + ':' + dstport srcToDst = srcaddr + '-' + dstaddr dstToSrc = dstaddr + '-' + srcaddr if not (srcToDst in DICT.keys() or dstToSrc in DICT.keys()): # No client-to-server or server-to-client datagrams, this is first one. # Initial timestamp for min & max, datagram from client, 0 from server. DICT[srcToDst]=[timestamp,timestamp,srcaddr,1,bytes,dstaddr,0,0] elif srcToDst in DICT.keys(): # Subsequent client-to-server datagram, update client fields. inst = DICT[srcToDst] inst[0] = min(inst[0], timestamp) inst[1] = max(inst[1], timestamp) inst[3] += 1 # One more client-to-server datagram inst[4] += bytes # Update bytes for client-to-server else: # Subsequent server-to-client reply, update its fields. inst = DICT[dstToSrc] inst[0] = min(inst[0], timestamp) inst[1] = max(inst[1], timestamp) inst[6] += 1 # One more server-to-client datagram inst[7] += bytes # Update bytes for server-to-client DICT = {} IPSRC='1.2.3.4' IPDST = '11.22.33.44' TCPSRC='12345' TCPDST='80' # Demo 3 datagrams coming through. # First datagram use keywords just to illustrate they are already bound to # values from Parson's previous Ethernet frame variables and STUDENT # IP and TCP variables. Start out client to server. matcher(DICT, timestamp=.01, srcip=IPSRC, srcport=TCPSRC, dstip=IPDST, dstport=TCPDST, bytes=20) print('''matcher(DICT, timestamp=.01, srcip=IPSRC, srcport=TCPSRC, dstip=IPDST, dstport=TCPDST, bytes=20)''', '\n\tDICT=', DICT) # Reply from server. matcher(DICT, 1.0, IPDST, TCPDST, IPSRC, TCPSRC, 40) print('matcher(DICT, 1.0, IPDST, TCPDST, IPSRC, TCPSRC, 40)', '\n\tDICT=', DICT) # Next datagram from client. Timestamps are out of order to test min & max. matcher(DICT, 0.001, IPSRC, TCPSRC, IPDST, TCPDST, 70) print('matcher(DICT, 0.001, IPSRC, TCPSRC, IPDST, TCPDST, 70)', '\n\tDICT=', DICT) # Here is the output from running this: # $ python sept21.parts6and8.py # matcher(DICT, timestamp=.01, srcip=IPSRC, srcport=TCPSRC, dstip=IPDST, # dstport=TCPDST, bytes=20) # DICT= {'1.2.3.4:12345-11.22.33.44:80': [0.01, 0.01, '1.2.3.4:12345', 1, 20, '11.22.33.44:80', 0, 0]} # matcher(DICT, 1.0, IPDST, TCPDST, IPSRC, TCPSRC, 40) # DICT= {'1.2.3.4:12345-11.22.33.44:80': [0.01, 1.0, '1.2.3.4:12345', 1, 20, '11.22.33.44:80', 1, 40]} # matcher(DICT, 0.001, IPSRC, TCPSRC, IPDST, TCPDST, 70) # DICT= {'1.2.3.4:12345-11.22.33.44:80': [0.001, 1.0, '1.2.3.4:12345', 2, 90, '11.22.33.44:80', 1, 40]}