Skip to main content
Loading...

More Python Posts

#Sets
U = {0,1,2,3,4,5,6,7,8,9}
P = {1,2,3,4}
Q = {4,5,6}
R = {3,4,6,8,9}

def set2bits(xs,us) :
    bs=[]
    for x in us :
        if x in xs :
            bs.append(1)
        else:
            bs.append(0)
    assert len(us) == len(bs)
    return bs

def union(set1,set2) :
    finalSet = set()
    bitList1 = set2bits(set1, U)
    bitList2 = set2bits(set2, U)

    for i in range(len(U)) :
        if(bitList1[i] or bitList2[i]) :
            finalSet.add(i)

    return finalSet

def intersection(set1,set2) :
    finalSet = set()
    bitList1 = set2bits(set1, U)
    bitList2 = set2bits(set2, U)

    for i in range(len(U)) :
        if(bitList1[i] and bitList2[i]) :
            finalSet.add(i)

    return finalSet

def compliment(set1) :
    finalSet = set()
    bitList = set2bits(set1, U)

    for i in range(len(U)) :
        if(not bitList[i]) :
            finalSet.add(i)

    return finalSet

def implication(a,b):
    return union(compliment(a), b)

###########################################################################################
######################         Problems 1-6         #######################################
###########################################################################################

#p \/ (q /\ r) = (p \/ q) /\ (p \/ r)
def prob1():
    return union(P, intersection(Q,R)) == intersection(union(P,Q), union(P,R))

#p /\ (q \/ r) = (p /\ q) \/ (p /\ r)
def prob2():
    return intersection(P, union(Q,R)) == union(intersection(P,Q), intersection(P,R))

#~(p /\ q) = ~p \/ ~q
def prob3():
    return compliment(intersection(P,R)) == union(compliment(P), compliment(R))

#~(p \/ q) = ~p /\ ~q
def prob4():
    return compliment(union(P,Q)) == intersection(compliment(P), compliment(Q))

#(p=>q) = (~q => ~p)
def prob5():
    return implication(P,Q) == implication(compliment(Q), compliment(P))

#(p => q) /\ (q => r)  =>  (p => r)
def prob6():
    return implication(intersection(implication(P,Q), implication(Q,R)), implication(P,R))


print("Problem 1: ", prob1())
print("Problem 2: ", prob2())
print("Problem 3: ", prob3())
print("Problem 4: ", prob4())
print("Problem 5: ", prob5())
print("Problem 6: ", prob6())

'''
Problem 1:  True
Problem 2:  True
Problem 3:  True
Problem 4:  True
Problem 5:  True
Problem 6:  {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
'''
import re

def _map_ikev2_vendor_capabilities(message_type, input_string):
    # List of acceptable message types
    valid_message_types = ['IKE_SA_INIT', 'IKE_AUTH']
    
    # Validate message type
    if message_type not in valid_message_types:
        raise ValueError(f"Invalid message type: {message_type}. Must be one of {valid_message_types}")
    
    # Mapping dictionary for IKE values with RFC references
    value_map = {
        # IKE_SA_INIT capabilities
        'FRAG_SUP': 'IKE Fragmentation',  # RFC 7383, Section 3
        'REDIR_SUP': 'IKE Redirection',  # RFC 5685, Section 3
        'HASH_ALG': 'Hash Algorithms',  # RFC 7296, Section 3.3.2
        'NATD_S_IP': 'NAT Detection (Source IP)',  # RFC 7296, Section 2.23
        'NATD_D_IP': 'NAT Detection (Destination IP)',  # RFC 7296, Section 2.23
        'SIGN_HASH_ALGS': 'Signature Hash Algorithms',  # RFC 7296, Section 2.15
        'NON_FIRST_FRAGMENTS': 'Non-First IKE Fragments',  # RFC 7383, Section 3
        'CHILDLESS_IKEV2_SUP': 'Childless IKEv2',  # RFC 6023, Section 3
        'INTERMEDIATE': 'Intermediate Exchange',  # RFC 9242, Section 3
        'COOKIE': 'Cookie-Based DoS Protection',  # RFC 7296, Section 2.6
        # IKE_AUTH capabilities
        'ESP_TFC_PAD_N': 'ESPv3 TFC Padding Disabled',  # RFC 7296, Section 3.3.1
        'MOBIKE_SUP': 'MOBIKE',  # RFC 4555, Section 3
        'MULT_AUTH': 'Multiple Auth',  # RFC 4739, Section 3
        'EAP_ONLY': 'EAP-Only Auth',  # RFC 5998, Section 3
        'MSG_ID_SYN_SUP': 'Message ID Synchronization',  # RFC 6311, Section 3
        'IPCOMP_SUPPORTED': 'IP Payload Compression Support',  # RFC 7296, Section 3.3.2
        'ADD_4_ADDR': 'Additional IPv4 Addresses',  # RFC 4555, Section 3.2
        'ADD_6_ADDR': 'Additional IPv6 Addresses',  # RFC 4555, Section 3.2
        'INIT_CONTACT': 'Initial Contact',  # RFC 7296, Section 3.16
        'HTTP_CERT_LOOKUP_SUP': 'HTTP Certificate Lookup',  # RFC 7296, Section 3.7
    }
        
    # Regex to capture N(...) patterns
    pattern = r'N\([^)]+\)'
    matches = re.findall(pattern, input_string)
    
    # Parse matches and create result list
    result = []
    for match in matches:
        key = match[2:-1]  # Extract content inside N(...)
        # Only include keys valid for the message type
        if key in value_map:
            result.append(value_map[key])
    
    return ', '.join(result)

# Example usage
ike_sa_init_string = '2025-07-18 20:16:22.839 15[ENC] <4> parsed IKE_SA_INIT request 0 [ SA KE No N(NATD_S_IP) N(NATD_D_IP) N(FRAG_SUP) N(HASH_ALG) N(MULT_AUTH) ]'
ike_auth_string = '2025-07-18 20:16:22.898 07[ENC] <4> parsed IKE_AUTH request 1 [ IDi N(INIT_CONTACT) IDr AUTH N(ESP_TFC_PAD_N) SA TSi TSr N(MOBIKE_SUP) N(ADD_4_ADDR)N(MULT_AUTH) N(EAP_ONLY) N(MSG_ID_SYN_SUP) ]'

# Test with IKE_SA_INIT
print("IKE_SA_INIT Results:")
print(_map_ikev2_vendor_capabilities("IKE_SA_INIT", ike_sa_init_string))

# Test with IKE_AUTH
print("\nIKE_AUTH Results:")
print(_map_ikev2_vendor_capabilities("IKE_AUTH", ike_auth_string))