Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write the main driver function analysePackets() having the following interface: analysePackets: packet_lst -> void Each packet in the stream of packets will be in the following format: (SRC, DST, LEN, PRT, SP, DP, SQN, PLD) A tuple with the packet info. The analysePackets function accepts a list of packets in the above format. The packet_lst has the following format. [(SRC, DST, LEN, PRT, SP, DP, SQN, PLD), (SRC, DST, LEN, PRT, SP, DP, SQN, PLD)...] This function accepts a stream of packet info as a list of packets (not in ADT format) and creates a packet (as described in our packet ADT). It analyses each packet using the functions we created in Part 3 and registers a suspicion score. The function then adds each packet to a score ADT. With that, the function separates the packets into the packet queue and packet stack based on the requirements stated in Parts 5 and 6 using the function created in part 7. Finally, the function returns the queue of all the packets ready to be forwarded in the order of highest to lowest overall


What I have tried:

def makePacket(srcIP, dstIP, length, prt, sp, dp, sqn, pld):
    return ("PK", srcIP, dstIP, [length, prt, [sp,dp], sqn, pld])
def getPacketSrc(pkt):
    return pkt [1:][0]   
def getPacketDst(pkt):
    return pkt [2:][0]    
def getPacketDetails(pkt):
    return pkt [3:][0]  
def isPacket(pkt):
    if type(())==type(pkt) and pkt[0] =='PK' and type([3])==type([]) and len(pkt)==4:
        return True
    else:
        return False
def isEmptyPkt(pkt):
    if pkt == ['Packet']:
        return True
    return False

def getLength(pkt):
    return pkt [3][0]  
def getProtocol(pkt):
    return pkt [3][1]
def getSrcPort(pkt):
    return pkt [3][2][0]

def getDstPort(pkt):
    return pkt [3][2][1]

def getSqn(pkt):
    return pkt[3][3]

def getPayloadSize(pkt):
    return pkt[3][4]

def flowAverage(pkt_list):
    total=0
    plist=[]
    for pkt in pkt_list:
        total+= getPayloadSize(pkt)
    average = total/len(pkt_list)
    plist = [pkt for pkt in pkt_list if getPayloadSize(pkt)>average]
    return plist
    

def suspPort(pkt):
    if getSrcPort(pkt) > 500 or getDstPort(pkt)>500:
        return True
    else:
        return False

        
def suspProto(pkt):
    protoLst=["HTTP","SMTP", "UDP", "TCP", "DHCP"]
    if getProtocol(pkt) not in protoLst:
        return True
    else:
        return False

def ipBlacklist(pkt):
    ipBlackList=[["213.217.236.184","444.221.232.94","149.88.83.47","223.70.250.146","169.51.6.136","229.22369.24"]]
    if getPacketSrc(pkt) in IpBlackList:
        return True
    else:
        return False
    
def calScore(pkt):
    a = flowAverage(pkt_list)
    b = suspProto(pkt)
    c = suspPort(pkt)
    d = ipBlacklist(pkt)
    score = 0
    if pkt in flowAverage(pkt_list): 
        score += 3.56
    if b == True:
        score += 2.74
    if c == True:
        score += 1.45        
    if d == True:
        score += 10
    return round(score,2)

def makeScore(pkt_list):
    New_list = []
    for pkt in pkt_list:
        New_list+= [(pkt, calScore(pkt))]
    return ["SCORE", New_list]
    
def addPacket(ScoreList, pkt):
    s=calScore(pkt)
    return ScoreList[1].append((pkt,s))

def getSuspPkts(ScoreList):
    susplist = []
    for s in ScoreList[1]:
        if s[1]>5: susplist.append(s[0])
    return susplist
    
def getRegulPkts(ScoreList):
    reglist = []
    for z in ScoreList[1]:
         if z[1]<=5: reglist.append(z[0])
    return reglist
   
           
def isScore(ScoreList):
    return ScoreList[0] == "SCORE" and type(ScoreList) == type([]) and type(ScoreList[1]) == type([]) and len(ScoreList)==2


def isEmptyScore(ScoreList):
    return ScoreList == []



def makePacketQueue():
    return ("PQ", [])
def contentsQ(q):
    return q[1]
def frontPacketQ(q):
    i = contentsQ(q)[0]
    return i[0]
def addToPacketQ(pkt,q):
    i = get_pos(pkt,contentsQ(q))
    contentsQ(q).insert(i,pkt)
    return q
def get_pos(pkt,lst):
    if (lst == []):
        return 0
    elif getSqn(pkt) < getSqn(lst[0]):
        return 0 + get_pos(pkt,[])
    else:
        return 1 + get_pos(pkt,lst[1:])
            
def removeFromPacketQ(q):
    return contentsQ(q).pop(0)

def isPacketQ(q):
    return type(q) == type(()) and ("PQ",[],1) and type(q[1]) == type([]) and len(q)==2
        

def isEmptPacketQ(q):
    return contentsQ(q) == []

def makePacketStack():
    return ('PS',[])

def contentsStack(stk):
    return stk[1]

def topProjectStack (stk):
    contentsStack(stk)[-1]

def pushProjectStack(pkt,stk):
    contentsStack(stk).append(pkt)

def popPickupStack(stk):
    contentsStack(stk).pop()

def isPKstack(stk):
    return type(())==type(stk) and stk[0]=='PS' and type(stk[1])==type([]) and len(stk)==2

def isEmptyPKStack(stk):
    return contentsStack(stk)==[]

def sortPackets(scoreList,stack,queue):
    for i in scoreList[1]:
        if i[1]<=5:
            addToPacketQ(i[0],queue)
        else:
            pushProjectStack(i[0],stack)

def analysePackets(packet_List):
  # Write your code here

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    first_multiple_input = input().rstrip().split()
    
    srcIP = str(first_multiple_input[0])
    dstIP = str(first_multiple_input[1])
    length = int(first_multiple_input[2])
    prt = str(first_multiple_input[3])
    sp = int(first_multiple_input[4])
    dp = int(first_multiple_input[5])
    sqn = int(first_multiple_input[6])
    pld = int(first_multiple_input[7])
    
    ProtocolList = ["HTTPS","SMTP","UDP","TCP","DHCP","IRC"]
    IpBlackList = ["213.217.236.184","444.221.232.94","149.88.83.47","223.70.250.146","169.51.6.136","229.223.169.245"]
    
    packet_List = [(srcIP, dstIP, length, prt, sp, dp, sqn, pld),\
              ("111.202.230.44","62.82.29.190",31,"HTTP",80,20,1562436,38),\
              ("222.57.155.164","50.168.160.19",22,"UDP",90,5431,1662435,82),\
              ("333.230.18.207","213.217.236.184",56,"IRC",501,5643,1762434,318),\
              ("444.221.232.94","50.168.160.19",1003,"TCP",4657,4875,1962433,428),\
              ("555.221.232.94","50.168.160.19",236,"TCP",7753,5724,2062432,48)]
    
    fptr.write('Forward Packets => ' + str(analysePackets(packet_List)) + '\n')
    
    fptr.close()
Posted
Updated 1-Dec-22 22:41pm
Comments
Dave Kreskowiak 1-Dec-22 17:54pm    
...and you had a question or a problem with this?

1 solution

You need to get together with your classmate who posted How do I get the desired output?[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900