Networking- Classful to Classless Addressing

This is a code that I am making as an improvement in a GFG Article I read yesterday, basically the code that they had provided was not even doing what they were explaining and was just giving random number for the place of the mask that specifies the number of bits used for the Network Portion of an IP. 

I'll be extending the code tomorrow as it is around 12:40 midnight and also because I am not able to figure out the problem that how am I supposed to convert a Classful Addr. to a Classless one as the Classful Addr. can only tell me the Class and on basis of that I can get to know about the NID and HID Parts and also the No. of Hosts and No. of Networks but what about the Subnets? I cannot determine how many Subnets will there be on just the basis of the Classful IP Address as they can be anything like for the case of a Class A Address we can borrow any no. of bits from the HID(24 bits) part and so I cannot know that how many bits represent the SID Part and so I cannot determine the Mask. Well, tomorrow I'll think about this tomorrow and will see if I can continue this...

This is the Code till now, I am going to update the C++ code as well provide a Python code for the same article, also there are few mistakes in the conceptual portion too-

def classless(ipaddress):
    '''
        Will specify the Class of the IP Address and will return the Classless IP
        Address
    '''
    # Alongwith the info of the Default Subnet Mask, info like no. of bits used for NID and no. of bits used for HID can also be specified as they depend on the Class only
    # The no. of Hosts can also be specified on the basis of the Input IP Address
   
    firstOctet=int(ipaddress[:ipaddress.index('.')])
    if(1<=firstOctet<=126):
        print("Class A!")
        print("Default Subnet Mask: 255.0.0.0")
        return ipaddress+'/8'
    elif(128<=firstOctet<=191):
        print("Class B!")
        print("Defaul Subnet Mask: 255.255.0.0")
        return ipaddress+'/16'
    elif(192<=firstOctet<=223):
        print("Class C!")
        print("Default Subnet Mask: 255.255.255.0")
        return ipaddress+'/24'
    else:
        print("This is a reserved IP Address, INVALID!!")
       
       
print(classless("192.168.0.0"))
print()
print(classless("172.16.0.0"))
print()
print(classless("224.0.0.0"))

Comments

Popular Posts