diff --git a/Trimester - VII/CN/dvr.py b/Trimester - VII/CN/dvr.py new file mode 100644 index 0000000..ee26a47 --- /dev/null +++ b/Trimester - VII/CN/dvr.py @@ -0,0 +1,172 @@ +""" +Name: Atharva G. Jibhakate +TY-CSE (Panel-5) +E1 +PE-03 +""" + +class Node(object): + def __init__(self, name, n): + self.name = name + self.number = n + self.neighbours = [] + self.routingTable = [] + + +# Bellman Ford's Algorithm +class Algorithm(object): + @staticmethod + def shareRT(vertexList): + + print("---------------------\n\nRouting Tables being transmitted 1 by 1:") + + while True: + UPDATE_HAPPENED = False + for receiver in vertexList: + print("-----------------------------------------------") + for sender in receiver.neighbours: + current_cost = 0 + next_hop = "#" + for xyz in receiver.routingTable: + if xyz[0] == sender.name: + current_cost = xyz[1] + next_hop = xyz[2] + print( + sender.name, + " sends its DV to ", + receiver.name, + " , and is at distance ", + current_cost, + " from it.", + ) + + for i in receiver.routingTable: + L1 = list(i) + for j in sender.routingTable: + L2 = list(j) + if (i[0] == j[0]) & ((j[1] + current_cost) < i[1]): + print(L1, "\t", " and ", end="") + print(L2, " ", end="") + L1[1] = L2[1] + current_cost + L1[2] = next_hop + receiver.routingTable.remove((i[0], i[1], i[2])) + i = tuple(L1) + receiver.routingTable.append(i) + print(" updates L1 to ", i) + UPDATE_HAPPENED = True + else: + continue + # i = tuple(L1) + if UPDATE_HAPPENED == True: + print("\n\nUPDATED ", end="") + print(receiver.name, "'s Routing Table:") + receiver.routingTable.sort() + print(receiver.routingTable, sep="\n") + + if UPDATE_HAPPENED == False: + break + + print("STABLE STATE REACHED!") + + +vertexList = [] +print("Enter number of routers:") +n = int(input()) +for i in range(n): + print("Enter label for router %d " % i) + node = Node(input(), i) # + vertexList.append(node) + node.routingTable.append((node.name, 0, node.name)) + +for router in vertexList: + for other in vertexList: + if other.name != router.name: + router.routingTable.append((other.name, 99, other.name)) + + +print("Initiating....") + +print("Enter number of links:") +m = int(input()) +for i in range(m): + print("Source Name, Destination Name, Cost:") + sourceLabel = Node(input(), 0) + destLabel = Node(input(), 0) + cost = int(input()) + + for i in vertexList: + if i.name == sourceLabel.name: + sourceLabel = i + if i.name == destLabel.name: + destLabel = i + + for xyz in sourceLabel.routingTable: + if xyz[0] == destLabel.name: + earlier_cost = xyz[1] + + if destLabel not in sourceLabel.neighbours: + sourceLabel.neighbours.append(destLabel) + if sourceLabel not in destLabel.neighbours: + destLabel.neighbours.append(sourceLabel) + + sourceLabel.routingTable.remove((destLabel.name, earlier_cost, destLabel.name)) + sourceLabel.routingTable.append((destLabel.name, cost, destLabel.name)) + destLabel.routingTable.remove((sourceLabel.name, earlier_cost, sourceLabel.name)) + destLabel.routingTable.append((sourceLabel.name, cost, sourceLabel.name)) + + +print("-----------------INITIALIZATION------------------------") +for i in vertexList: + print(i.name, "'s Routing Table:") + i.routingTable.sort() + for j in i.routingTable: + print(j, "\t", end="") + print() + + +algorithm = Algorithm() +algorithm.shareRT(vertexList) + +while True: + print("Update cost of some link? Y:N") + opt = input() + if opt == "Y": + print("Source Name, Destination Name, Cost:") + sourceLabel = Node(input(), 0) + destLabel = Node(input(), 0) + cost = int(input()) + earlier_cost = 0 + for i in vertexList: + if i.name == sourceLabel.name: + sourceLabel = i + if i.name == destLabel.name: + destLabel = i + + for xyz in sourceLabel.routingTable: + if xyz[0] == destLabel.name: + earlier_cost = xyz[1] + + print( + "Earlier cost from ", + sourceLabel.name, + " to ", + destLabel.name, + " was ", + earlier_cost, + ) + sourceLabel.routingTable.remove((destLabel.name, earlier_cost, destLabel.name)) + sourceLabel.routingTable.append((destLabel.name, cost, destLabel.name)) + destLabel.routingTable.remove( + (sourceLabel.name, earlier_cost, sourceLabel.name) + ) + destLabel.routingTable.append((sourceLabel.name, cost, sourceLabel.name)) + algorithm.shareRT(vertexList) + else: + break +for i in vertexList: + print(i.name, "'s Routing Table:") + i.routingTable.sort() + for j in i.routingTable: + print(j, "\t", end="") + print() + diff --git a/Trimester - VII/CN/subnet_ss.py b/Trimester - VII/CN/subnet_ss.py new file mode 100644 index 0000000..fcff8b3 --- /dev/null +++ b/Trimester - VII/CN/subnet_ss.py @@ -0,0 +1,306 @@ + +ip1, ip2, ip3, ip4 = input("Enter the ip address in Decimal Form\n").split(".") +print("The IP Address id: {}.{}.{}.{}".format(ip1, ip2, ip3,ip4)) +print("Decimal Form: {}.{}.{}.{}".format(bin(int(ip1)).replace("0b", ""), bin(int(ip2)).replace("0b", ""), + bin(int(ip3)).replace("0b", ""),bin(int(ip4)).replace("0b", "") )) +ip1 = int(ip1) +ip2 = int(ip2) +ip3 = int(ip3) +ip4 = int(ip4) + +# print("\n\nThe given IP Address belongs to :") +# if(ip1 >= 0 and ip1 <= 127): +# print("Class A") +# print("Default Mask: 255.0.0.0") +# elif(ip1 > 127 and ip1 <= 191): +# print("Class B") +# print("Default Mask: 255.255.0.0") +# elif(ip1 > 191 and ip1 <= 223): +# print("Class C") +# print("Default Mask: 255.255.255.0") +# elif(ip1 > 223 and ip1 <= 239): +# print('Class D') +# print("No Default Mask") +# elif(ip1 > 239 and ip1 <= 255): +# print('Class E') +# print("No Default Mask") + +n = int(input("\nHow many Subnet do you want?")) + +nb = bin(n).replace("0b", "") + +list1 = [1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768, + 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304] + +nblen = len(nb) +if n not in list1: + nblen = nblen + 1 + +print("nblen = {}".format(nblen)) + +if nblen >= 0: + nblen1 = nblen-1 +else: + pass + +if (nblen > 0 and nblen <9 ): + pow = 7 + sub_mask_d = 0 + for i in range(0, (nblen -1) ): + sub_mask_d = sub_mask_d + (2**pow) + pow = pow - 1 +elif (nblen > 8 and nblen < 17): + pow = 7 + sub_mask_d = 0 + for i in range(0, (nblen -1) - 8 ): + sub_mask_d = sub_mask_d + (2**pow) + pow = pow - 1 +elif(nblen > 16 and nblen < 25): + pow = 7 + sub_mask_d = 0 + for i in range(0, (nblen -1) - 16 ): + sub_mask_d = sub_mask_d + (2**pow) + pow = pow - 1 + + +# print("\n\nThe Default Subnet Mask is :") +# if(ip1 >= 0 and ip1 <= 127): +# # print("Class A") +# if nblen > 0 and nblen < 9: +# print("Subnet Mask:") +# print("255.", end= '') +# print(sub_mask_d, end = '') +# print(".0.0") + +# print("Binary Form:") +# print("11111111.", end='') +# print(bin(sub_mask_d).replace("0b", ""), end='') +# print(".0.0") +# elif nblen > 8 and nblen < 17: +# print("Subnet Mask:") +# print("255.255.", end= '') +# print(sub_mask_d, end = '') +# print(".0") + +# print("Binary Form:") +# print("11111111.11111111.", end='') +# print(bin(sub_mask_d).replace("0b", ""), end='') +# print(".0") +# elif(nblen > 16 and nblen < 25): +# print("Subnet Mask:") +# print("255.255.255.", end= '') +# print(sub_mask_d, end = '') + +# print("Binary Form:") +# print("11111111.11111111.11111111.", end='') +# print(bin(sub_mask_d).replace("0b", ""), end='') +# # print("0.0") +# # print(".0") + +# elif(ip1 > 127 and ip1 <= 191): +# # print("Subnet Mask:") +# if nblen > 0 and nblen < 9: +# print("Subnet Mask:") +# print("255.255.", end= '') +# print(sub_mask_d, end = '') +# print(".0") +# elif nblen > 8 and nblen < 17: +# print("Subnet Mask:") +# print("255.255.255.", end= '') +# print(sub_mask_d, end = '') +# # print(".0") + +# elif(ip1 > 191 and ip1 <= 223): +# if nblen > 0 and nblen < 9: +# print("Subnet Mask:") +# print("255.255.255.", end= '') +# print(sub_mask_d, end = '') +# # print(".0") +# else: +# print("Not Possible") +# elif(ip1 > 223 and ip1 <= 239): +# print('Class D') +# print("No Subnet Mask") +# elif(ip1 > 239 and ip1 <= 255): +# print('Class E') +# print("No Subnet Mask") + +# Subnetting +na = 1 + +for j in list1: + if j >= n: + na = j + break + +print("na = {}".format(na)) +if(ip1 >= 0 and ip1 <= 127): + if nblen > 0 and nblen < 9: + sub = 0 + + print("Range of each Subnets") + for i in range(0, n): + print(ip1, end='') + print(".",end = '') + print(int(sub), end = '') + print(".0.0", end='') + + print(" - ", end='') + + print(ip1, end='') + print(".", end='') + print(int(sub+ (256/na)-1), end='') + print(".255.255") + sub = sub + (256/na) + print("\nClass : A") + print("Default Mask: 255.0.0.0") + print("Network ID: {}.0.0.0".format(ip1)) + print("Broadcaster ID: {}.0.0.0".format(ip1)) + print("Total Bits Borrowed/Fixed: {}".format(nblen1)) + print("Range of Each Subnet: {}".format((2**(16+(8-nblen1))) )) + print("Total Avalable Id: {}".format( (2**(16+(8-nblen1))) -2) ) + print("Subnet Mask: ", end='') + print("255.", end= '') + print(sub_mask_d, end = '') + print(".0.0") + elif nblen > 8 and nblen < 17: + print("\nClass : A") + print("Default Mask: 255.0.0.0") + print("Network ID: {}.0.0.0".format(ip1)) + print("Broadcaster ID: {}.0.0.0".format(ip1)) + print("Total Bits Borrowed/Fixed: {}".format(nblen1)) + print("Range of Each Subnet: {}".format((2**(16+(8-nblen1))) )) + print("Total Avalable Id: {}".format( (2**(16+(8-nblen1))) -2) ) + print("Subnet Mask: ", end='') + print("255.255.", end= '') + print(sub_mask_d, end = '') + print(".0") + + + elif(nblen > 16 and nblen < 25): + print("\nClass : A") + print("Default Mask: 255.0.0.0") + print("Network ID: {}.0.0.0".format(ip1)) + print("Broadcaster ID: {}.0.0.0".format(ip1)) + print("Total Bits Borrowed/Fixed: {}".format(nblen1)) + print("Range of Each Subnet: {}".format((2**(16+(8-nblen1))) )) + print("Total Avalable Id: {}".format( (2**(16+(8-nblen1))) -2) ) + print("Subnet Mask: ", end='') + print("255.255.255.", end= '') + print(sub_mask_d) + # print(".0") + # print(".0") +elif(ip1 > 127 and ip1 <= 191): + if nblen > 0 and nblen < 9: + sub = 0 + print("Range of each Subnets") + for i in range(0, n): + print(ip1, end='') + print(".",end = '') + print(ip2, end='') + print(".",end = '') + print(int(sub), end = '') + print(".0", end='') + + print(" - ", end='') + + print(ip1, end='') + print(".", end='') + print(ip2, end='') + print(".", end='') + print(int(sub+ (256/na)-1), end='') + print(".255") + sub = sub + (256/na) + print("\nClass : B") + print("Default Mask: 255.255.0.0") + print("Network ID: {}.{}.0.0".format(ip1, ip2)) + print("Broadcaster ID: {}.{}.255.255".format(ip1, ip2)) + print("Total Bits Borrowed/Fixed: {}".format(nblen1)) + print("Range of Each Subnet: {}".format((2**(8+(8-nblen1))) )) + print("Total Avalable Id: {}".format( (2**(8+(8-nblen1))) -2) ) + print("Subnet Mask:") + print("255.255.", end= '') + print(sub_mask_d, end = '') + print(".0", end= '') + # print(".0") + elif nblen > 8 and nblen < 17: + print("\nClass : B") + print("Default Mask: 255.0.0.0") + print("Network ID: {}.0.0.0".format(ip1)) + print("Broadcaster ID: {}.0.0.0".format(ip1)) + print("Total Bits Borrowed/Fixed: {}".format(nblen1)) + print("Range of Each Subnet: {}".format((2**(8+(8-nblen1))) )) + print("Total Avalable Id: {}".format( (2**(8+(8-nblen1))) -2) ) + print("Subnet Mask: ", end='') + print("255.255.", end= '') + print(sub_mask_d, end = '') + print(".0") +elif(ip1 > 191 and ip1 <= 223): + if n == 256: + print("Not Possible") + else: + if nblen > 0 and nblen < 9: + sub = 0 + + print("Range of each Subnets") + for i in range(0, n): + print(ip1, end='') + print(".",end = '') + print(ip2, end='') + print(".",end = '') + print(ip3, end='') + print(".",end = '') + print(int(sub), end = '') + # print(".0", end='') + + print(" - ", end='') + + print(ip1, end='') + print(".", end='') + print(ip2, end='') + print(".", end='') + print(ip3, end='') + print(".", end='') + print(int(sub+ (256/na)-1), end='') + print("") + sub = sub + (256/na) + + + # print("\n\n Subnet ID: ") + # print(ip1, end='') + # print(".",end = '') + # print(ip2, end='') + # print(".",end = '') + # print(ip3, end='') + # print(".",end = '') + # print(int(sub)) + # # print(".0", end='') + + # print("Broadcaster ID: ") + # print(ip1, end='') + # print(".", end='') + # print(ip2, end='') + # print(".", end='') + # print(ip3, end='') + # print(".", end='') + # print(int(sub+ (256/na)-1), end='') + # print("/n/n") + + + print("\nClass : C") + print("Default Mask: 255.255.255.0") + print("Network ID: {}.{}.{}.0".format(ip1, ip2, ip3)) + print("Network ID: {}.{}.{}.255".format(ip1, ip2, ip3)) + print("Total Bits Borrowed/Fixed: {}".format(nblen1)) + print("Range of Each Subnet: {}".format(int(256/na) )) + print("Total Avalable Id: {}".format((int(256/na)-2)) ) + print("Subnet Mask: ", end='') + print("255.255.255.", end= '') + print(sub_mask_d, end = '') + + +elif(ip1 > 223 and ip1 <= 239): + print("Error") +elif(ip1 > 239 and ip1 <= 255): + print("Error") + \ No newline at end of file diff --git a/Trimester - VII/CN/tcp_client.c b/Trimester - VII/CN/tcp_client.c new file mode 100644 index 0000000..e43d8a3 --- /dev/null +++ b/Trimester - VII/CN/tcp_client.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#define MAX 80 +#define PORT 8080 +#define SA struct sockaddr +void func(int sockfd) +{ + char buff[MAX]; + int n; + for (;;) { + bzero(buff, sizeof(buff)); + printf("Enter the string : "); + n = 0; + while ((buff[n++] = getchar()) != '\n') + ; + write(sockfd, buff, sizeof(buff)); + bzero(buff, sizeof(buff)); + read(sockfd, buff, sizeof(buff)); + printf("From Server : %s", buff); + if ((strncmp(buff, "exit", 4)) == 0) { + printf("Client Exit...\n"); + break; + } + } +} + +int main() +{ + int sockfd, connfd; + struct sockaddr_in servaddr, cli; + + // socket create and varification + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) { + printf("socket creation failed...\n"); + exit(0); + } + else + printf("Socket successfully created..\n"); + bzero(&servaddr, sizeof(servaddr)); + + // assign IP, PORT + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); + servaddr.sin_port = htons(PORT); + + // connect the client socket to server socket + if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) { + printf("connection with the server failed...\n"); + exit(0); + } + else + printf("connected to the server..\n"); + + // function for chat + func(sockfd); + + // close the socket + close(sockfd); +} + diff --git a/Trimester - VII/CN/tcp_server.c b/Trimester - VII/CN/tcp_server.c new file mode 100644 index 0000000..d202589 --- /dev/null +++ b/Trimester - VII/CN/tcp_server.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#define MAX 80 +#define PORT 8080 +#define SA struct sockaddr + +// Function designed for chat between client and server. +void func(int sockfd) +{ + char buff[MAX]; + int n; + // infinite loop for chat + for (;;) { + bzero(buff, MAX); + + // read the message from client and copy it in buffer + read(sockfd, buff, sizeof(buff)); + // print buffer which contains the client contents + printf("From client: %s\t To client : ", buff); + bzero(buff, MAX); + n = 0; + // copy server message in the buffer + while ((buff[n++] = getchar()) != '\n') + ; + + // and send that buffer to client + write(sockfd, buff, sizeof(buff)); + + // if msg contains "Exit" then server exit and chat ended. + if (strncmp("exit", buff, 4) == 0) { + printf("Server Exit...\n"); + break; + } + } +} + +// Driver function +int main() +{ + int sockfd, connfd, len; + struct sockaddr_in servaddr, cli; + + // socket create and verification + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1) { + printf("socket creation failed...\n"); + exit(0); + } + else + printf("Socket successfully created..\n"); + bzero(&servaddr, sizeof(servaddr)); + + // assign IP, PORT + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(PORT); + + // Binding newly created socket to given IP and verification + if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) { + printf("socket bind failed...\n"); + exit(0); + } + else + printf("Socket successfully binded..\n"); + + // Now server is ready to listen and verification + if ((listen(sockfd, 5)) != 0) { + printf("Listen failed...\n"); + exit(0); + } + else + printf("Server listening..\n"); + len = sizeof(cli); + + // Accept the data packet from client and verification + connfd = accept(sockfd, (SA*)&cli, &len); + if (connfd < 0) { + printf("server acccept failed...\n"); + exit(0); + } + else + printf("server acccept the client...\n"); + + // Function for chatting between client and server + func(connfd); + + // After chatting close the socket + close(sockfd); +} + diff --git a/Trimester - VII/CN/udp_client.c b/Trimester - VII/CN/udp_client.c new file mode 100644 index 0000000..a019c4a --- /dev/null +++ b/Trimester - VII/CN/udp_client.c @@ -0,0 +1,50 @@ +// Client side implementation of UDP client-server model +#include +#include +#include +#include +#include +#include +#include +#include + +#define PORT 8080 +#define MAXLINE 1024 + +// Driver code +int main() { + int sockfd; + char buffer[MAXLINE]; + char *hello = "Hello from client"; + struct sockaddr_in servaddr; + + // Creating socket file descriptor + if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { + perror("socket creation failed"); + exit(EXIT_FAILURE); + } + + memset(&servaddr, 0, sizeof(servaddr)); + + // Filling server information + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(PORT); + servaddr.sin_addr.s_addr = INADDR_ANY; + + int n, len; + + sendto(sockfd, (const char *)hello, strlen(hello), + MSG_CONFIRM, (const struct sockaddr *) &servaddr, + sizeof(servaddr)); + printf("Hello message sent.\n"); + + n = recvfrom(sockfd, (char *)buffer, MAXLINE, + MSG_WAITALL, (struct sockaddr *) &servaddr, + &len); + buffer[n] = '\0'; + printf("Server : %s\n", buffer); + + close(sockfd); + return 0; +} + diff --git a/Trimester - VII/CN/udp_server.c b/Trimester - VII/CN/udp_server.c new file mode 100644 index 0000000..fe15316 --- /dev/null +++ b/Trimester - VII/CN/udp_server.c @@ -0,0 +1,59 @@ +// Server side implementation of UDP client-server model +#include +#include +#include +#include +#include +#include +#include +#include + +#define PORT 8080 +#define MAXLINE 1024 + +// Driver code +int main() { + int sockfd; + char buffer[MAXLINE]; + char *hello = "Hello from server"; + struct sockaddr_in servaddr, cliaddr; + + // Creating socket file descriptor + if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) { + perror("socket creation failed"); + exit(EXIT_FAILURE); + } + + memset(&servaddr, 0, sizeof(servaddr)); + memset(&cliaddr, 0, sizeof(cliaddr)); + + // Filling server information + servaddr.sin_family = AF_INET; // IPv4 + servaddr.sin_addr.s_addr = INADDR_ANY; + servaddr.sin_port = htons(PORT); + + // Bind the socket with the server address + if ( bind(sockfd, (const struct sockaddr *)&servaddr, + sizeof(servaddr)) < 0 ) + { + perror("bind failed"); + exit(EXIT_FAILURE); + } + + int len, n; + + len = sizeof(cliaddr); //len is value/resuslt + + n = recvfrom(sockfd, (char *)buffer, MAXLINE, + MSG_WAITALL, ( struct sockaddr *) &cliaddr, + &len); + buffer[n] = '\0'; + printf("Client : %s\n", buffer); + sendto(sockfd, (const char *)hello, strlen(hello), + MSG_CONFIRM, (const struct sockaddr *) &cliaddr, + len); + printf("Hello message sent.\n"); + + return 0; +} +