├── study ├── crc │ └── readme.md ├── udp-cpp │ └── readme.md ├── hamming │ └── readme.md ├── packet_tracer │ └── readme.md └── network_simulator │ └── readme.md ├── ass5-udp-cpp ├── a ├── server │ ├── a │ ├── a.out │ ├── server │ └── server.cpp ├── a.out └── client.cpp ├── ass6-tcp-cpp ├── a ├── server │ ├── a │ ├── a.out │ └── server.cpp ├── a.out └── client.cpp ├── ass2-hamming-crc ├── a.out ├── crc.cpp └── hamming.cpp ├── ass12-peer-to-peer ├── Client.class ├── Server.class ├── GetMessage.class ├── GetMesssage.class ├── Client.java └── Server.java ├── ass3-subnetting ├── Subnetting.class └── Subnetting.java ├── CNL-lab LIST OF ASSIGNMENTS-Moc-19-20.pdf ├── ass10-ns2-star ├── packet_delivery_ratio.awk ├── throughput.awk └── star.tcl ├── ass9-ns2-basic ├── simple.tcl └── out.tr ├── ass10-ns2-complex └── complex.tcl └── readme.md /study/crc/readme.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ass5-udp-cpp/a: -------------------------------------------------------------------------------- 1 | this is a 2 | file -------------------------------------------------------------------------------- /ass6-tcp-cpp/a: -------------------------------------------------------------------------------- 1 | Th 2 | is 3 | is a file -------------------------------------------------------------------------------- /ass5-udp-cpp/server/a: -------------------------------------------------------------------------------- 1 | this is a 2 | file -------------------------------------------------------------------------------- /ass6-tcp-cpp/server/a: -------------------------------------------------------------------------------- 1 | Th 2 | is 3 | is a file -------------------------------------------------------------------------------- /ass5-udp-cpp/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass5-udp-cpp/a.out -------------------------------------------------------------------------------- /ass6-tcp-cpp/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass6-tcp-cpp/a.out -------------------------------------------------------------------------------- /ass2-hamming-crc/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass2-hamming-crc/a.out -------------------------------------------------------------------------------- /ass5-udp-cpp/server/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass5-udp-cpp/server/a.out -------------------------------------------------------------------------------- /ass5-udp-cpp/server/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass5-udp-cpp/server/server -------------------------------------------------------------------------------- /ass6-tcp-cpp/server/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass6-tcp-cpp/server/a.out -------------------------------------------------------------------------------- /ass12-peer-to-peer/Client.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass12-peer-to-peer/Client.class -------------------------------------------------------------------------------- /ass12-peer-to-peer/Server.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass12-peer-to-peer/Server.class -------------------------------------------------------------------------------- /ass3-subnetting/Subnetting.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass3-subnetting/Subnetting.class -------------------------------------------------------------------------------- /ass12-peer-to-peer/GetMessage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass12-peer-to-peer/GetMessage.class -------------------------------------------------------------------------------- /ass12-peer-to-peer/GetMesssage.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/ass12-peer-to-peer/GetMesssage.class -------------------------------------------------------------------------------- /CNL-lab LIST OF ASSIGNMENTS-Moc-19-20.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ashawe/CNL/HEAD/CNL-lab LIST OF ASSIGNMENTS-Moc-19-20.pdf -------------------------------------------------------------------------------- /study/udp-cpp/readme.md: -------------------------------------------------------------------------------- 1 | # Links 2 | - [GFG UDP CLIENT SERVER](https://www.geeksforgeeks.org/udp-server-client-implementation-c/) -------------------------------------------------------------------------------- /study/hamming/readme.md: -------------------------------------------------------------------------------- 1 | # ReferenceLinks 2 | - [Geeks for Geeks - Hamming Code](https://www.geeksforgeeks.org/hamming-code-in-computer-network) 3 | - [Decimal to binary conversino](https://stackoverflow.com/q/22746429) The answer is in the quesion itself 4 | - [Binary to decimal conversino](https://www.geeksforgeeks.org/program-binary-decimal-conversion/) 2nd version -------------------------------------------------------------------------------- /ass10-ns2-star/packet_delivery_ratio.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | 3 | recieved = 0; 4 | dropped = 0; 5 | } 6 | 7 | 8 | 9 | /^r.*/ { 10 | 11 | recieved ++ ; 12 | 13 | } 14 | 15 | 16 | 17 | /^d.*/ { 18 | 19 | dropped ++ ; 20 | 21 | } 22 | 23 | 24 | 25 | END { 26 | 27 | if( dropped!= 0) 28 | printf "total recieved: %d, total dropped: %d, ratio:%.4f",recieved,dropped,(recieved/dropped); 29 | else 30 | printf "hi" 31 | } 32 | 33 | -------------------------------------------------------------------------------- /ass10-ns2-star/throughput.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | 3 | fromNode=1; toNode=3; 4 | 5 | lineCount = 0;totalBits = 0; 6 | droppedBits = 0; 7 | 8 | } 9 | 10 | /^r/ { 11 | 12 | totalBits += 8*$6; 13 | 14 | if ( lineCount==0 ) { 15 | 16 | timeBegin = $2; lineCount++; 17 | 18 | } else { 19 | 20 | timeEnd = $2; 21 | 22 | }; 23 | 24 | }; 25 | 26 | /^d/{ 27 | droppedBits += 8*$6; 28 | } 29 | 30 | END{ 31 | 32 | duration = timeEnd-timeBegin; 33 | 34 | printf("Number of records is %d\n", NR); 35 | 36 | print "Output: "; 37 | 38 | #print "Transmission: N" fromNode "->N" toNode; 39 | 40 | print " - Total transmitted bits = " totalBits " bits"; 41 | 42 | print " - Total droppedBits bits = " droppedBits " bits"; 43 | 44 | print " - duration = " duration " s"; 45 | 46 | print " - Thoughput = " totalBits/duration/1e3 " kbps."; 47 | 48 | }; -------------------------------------------------------------------------------- /ass6-tcp-cpp/client.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define PORT 6511 11 | #define MAXLINES 1024 12 | using namespace std; 13 | int main() 14 | { 15 | 16 | int sockfd; 17 | sockaddr_in server; 18 | char buffer[MAXLINES]; 19 | string fileName; 20 | 21 | if( (sockfd = socket(AF_INET,SOCK_STREAM,0) ) < 0 ) 22 | { 23 | cout << "err"; 24 | } 25 | server.sin_family = AF_INET; 26 | server.sin_port = htons(PORT); 27 | server.sin_addr.s_addr = inet_addr("127.0.0.1"); 28 | 29 | if(connect(sockfd,(const sockaddr *) &server,sizeof(server)) < 0 ) 30 | cout << "error"; 31 | 32 | cout << "Enter fileName" << endl; 33 | cin >> fileName; 34 | send(sockfd,fileName.c_str(),fileName.length(),0); 35 | 36 | int n = read(sockfd,buffer,MAXLINES); 37 | 38 | ofstream ofs(fileName,ios::out); 39 | ofs.write(buffer,n); 40 | ofs.close(); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /ass9-ns2-basic/simple.tcl: -------------------------------------------------------------------------------- 1 | set ns [new Simulator] 2 | 3 | $ns color 1 Blue 4 | 5 | set nf [open out.nam w] 6 | $ns namtrace-all $nf 7 | 8 | set nt [open out.tr w] 9 | $ns trace-all $nt 10 | 11 | proc finish {} { 12 | 13 | global ns nf nt 14 | $ns flush-trace 15 | 16 | close $nf 17 | close $nt 18 | 19 | exec nam out.nam & 20 | exit 0 21 | } 22 | 23 | set n0 [$ns node] 24 | set n1 [$ns node] 25 | set n2 [$ns node] 26 | 27 | $ns duplex-link $n0 $n1 2Mb 10ms DropTail 28 | $ns duplex-link $n1 $n2 1Mb 10ms DropTail 29 | 30 | $ns queue-limit $n1 $n2 10 31 | 32 | $ns duplex-link-op $n0 $n1 orient right 33 | $ns duplex-link-op $n1 $n2 orient right 34 | 35 | $ns duplex-link-op $n1 $n2 queuePos 0.5 36 | 37 | set tcp [new Agent/TCP] 38 | $tcp set class_ 2 39 | $ns attach-agent $n0 $tcp 40 | set sink [new Agent/TCPSink] 41 | $ns attach-agent $n2 $sink 42 | $ns connect $tcp $sink 43 | $tcp set fid_ 1 44 | 45 | set ftp [new Application/FTP] 46 | $ftp attach-agent $tcp 47 | $ftp set type_ FTP 48 | 49 | $ns at 0.1 "$ftp start" 50 | $ns at 2 "$ftp stop" 51 | $ns at 2.1 "finish" 52 | 53 | $ns run -------------------------------------------------------------------------------- /ass6-tcp-cpp/server/server.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define PORT 6511 11 | #define MAXLINES 1024 12 | using namespace std; 13 | int main() 14 | { 15 | 16 | int sockfd,connfd; 17 | sockaddr_in server,client; 18 | char buffer[MAXLINES]; 19 | char fileBuffer[MAXLINES]; 20 | 21 | sockfd = socket(AF_INET,SOCK_STREAM,0); 22 | 23 | server.sin_family = AF_INET; 24 | server.sin_port = htons(PORT); 25 | server.sin_addr.s_addr = INADDR_ANY; 26 | 27 | 28 | bind(sockfd,(const sockaddr*) &server,sizeof(server)); 29 | 30 | listen(sockfd,5); 31 | socklen_t len; 32 | connfd = accept(sockfd, (sockaddr *)&client, &len); 33 | 34 | if (connfd < 0) 35 | { 36 | cout << "failse"; 37 | } 38 | else 39 | { 40 | cout << "success"; 41 | } 42 | 43 | int n = read(connfd,buffer,sizeof(buffer)); 44 | buffer[n] = '\0'; 45 | cout << "Client said: " << buffer << endl; 46 | 47 | ifstream ifs(buffer,ios::in|ios::ate); 48 | int size = ifs.tellg(); 49 | ifs.seekg(ios::beg); 50 | ifs.read(fileBuffer,size); 51 | send(connfd,fileBuffer,size,0); 52 | 53 | return 0; 54 | } -------------------------------------------------------------------------------- /ass5-udp-cpp/client.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define PORT 7512 12 | #define MAXLINES 1024 13 | 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | int sockfd; 19 | string fileName; 20 | char buffer[MAXLINES]; 21 | struct sockaddr_in server; 22 | 23 | sockfd = socket(AF_INET,SOCK_DGRAM,0); 24 | memset(&server,0,sizeof(server)); 25 | 26 | server.sin_family = AF_INET; 27 | server.sin_addr.s_addr = INADDR_ANY; 28 | server.sin_port = htons(PORT); 29 | 30 | cout << "Enter file name" << endl; 31 | cin >> fileName; 32 | 33 | cout << fileName.c_str() << endl; 34 | sendto(sockfd, (const char *)fileName.c_str(), fileName.length(),0,(const struct sockaddr *)&server, sizeof(server)); 35 | cout << "Sent filename to server" << endl; 36 | 37 | cout << "Waiting for response" << endl; 38 | socklen_t len; 39 | int n = recvfrom(sockfd,(char *)buffer,sizeof(buffer),0,(struct sockaddr *)&server,&len); 40 | buffer[n] = '\0'; 41 | cout << "Response recieved: " << buffer << endl; 42 | 43 | ofstream ofs; 44 | cout << "Creating file" << endl; 45 | ofs.open(fileName,ios::out); 46 | cout << "Writing to file" << endl; 47 | ofs.write(buffer,n); 48 | cout << "Closing file" << endl; 49 | ofs.close(); 50 | cout << "Closing socket"< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define PORT 7512 12 | #define MAXLINES 1024 13 | 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | int sockfd; 19 | char* fileName = new char[1]; 20 | char buffer[MAXLINES]; 21 | struct sockaddr_in server,client; 22 | 23 | sockfd = socket(AF_INET,SOCK_DGRAM,0); 24 | memset(&server,0,sizeof(server)); 25 | memset(&client,0,sizeof(client)); 26 | 27 | server.sin_family = AF_INET; 28 | server.sin_addr.s_addr = INADDR_ANY; 29 | server.sin_port = htons(PORT); 30 | 31 | bind(sockfd,(const struct sockaddr*) &server,sizeof(server)); 32 | 33 | 34 | socklen_t len; 35 | int n = recvfrom(sockfd,fileName,MAXLINES,0,( struct sockaddr *)&client,&len); 36 | fileName[n]='\0'; 37 | cout << "Client wants file: " << fileName << endl; 38 | 39 | cout << "Opening file" << endl; 40 | std::ifstream ifs(fileName,ios::ate); 41 | if(!ifs) 42 | { 43 | cout << "file not present"; 44 | } 45 | else 46 | { 47 | int size = ifs.tellg(); 48 | ifs.seekg(ios::beg); 49 | 50 | cout << "Reading file. size:"< This repo is for students who want to study / complete their CN assignments / submit code folders. It will also help students better understand which path to take while studying for the assignments by themselves. I am writing this when my practical exams are going on. Therefore, you can be sure that each program will surely run and perform the task it is supposed to do. Although, you must be aware that there might be some corner cases which might get left out. 3 | 4 | > If you just want the codes you can go to each assignment's folder and get it. 5 | > If you are doing this while your lab is going on / you're studying for practicals, you might want to go through the [study plan](#study-plan) and look through the [study folder](/study) for a better understanding of the practical. 6 | 7 | ## FEEL FREE TO CONTRIBUTE YOUR OWN STUDY MATERIAL TO HELP OTHER STUDENTS. CREATING A PR IS FREE! 8 | ### I suggest at improving/adding at least one thing in this repo. 9 | 10 | ## INDEX 11 | - [List of Assignments](#list-of-assignments) 12 | - [Study Plan](#study-plan) 13 | 14 | 15 | # List of assignments: 16 | 17 | ### A1 ) Wired & Wireless lan 18 | #### Wired 19 | - cable making 20 | - connecting PCs 21 | - PINGING 22 | #### Wireless 23 | - connecting PCs 24 | - PINGING 25 | - wireshark 26 | 27 | ### A2) HAMMING & CRC ( c / c++ ) 28 | - 7-8 bit ASCII codes 29 | - wireshark 30 | 31 | ### A3) Subnetting 32 | - WAP for subnetting 33 | - INPUT 34 | - ip address 35 | - subnet mask 36 | - OUTPUT 37 | - first & last address 38 | - range 39 | 40 | ### A4) Go back N and Selective Repeat 41 | - WAP for sliding window protocol 42 | - INPUT 43 | - window size 44 | - Number of packets to transfer 45 | - drop or recieve 46 | - OUTPUT 47 | - sent / recieved 48 | - wireshark 49 | 50 | ### A5) UDP Socket ( C / C++ ) 51 | - transfer file 52 | 53 | ### A6) TCP Socket ( C / C++ ) 54 | - Hello 55 | - File Transfer 56 | - Calculator ( Arithmatic / Trig ) 57 | - expression based. ( Must handle -10*54 ) 58 | 59 | ### A7) Analyzing wireshark packets ( c / c++ ) 60 | 61 | ### A8) DHCP 62 | - connect 2 machines 63 | - SSH into another machine 64 | - WAP ( c++ / JAVA / Python ) to install / remove software to / from remote machine 65 | 66 | ### B1 ) NS2 Study 67 | - 3 Nodes 68 | - TCP 69 | 70 | ### B2 ) NS2 71 | - Monitoring traffic for the given topology. 72 | - Analysis of CSMA and Ethernet protocols. 73 | - Network Routing: Shortest path routing, AODV. 74 | - Analysis of congestion control (TCP and UDP). 75 | 76 | ### B3 ) Configure RIP/OSPF/BGP using packet Tracer 77 | 78 | ### B4 ) TCP / UDP ( Java / Python ) 79 | - Peer to Peer 80 | - Multi User 81 | 82 | # Study Plan 83 | Each topics will have additional study materials and links inside their respective folders. 84 | 85 | - [x] Error Detection. Hamming Codes. CRC Codes. 86 | - [x] Hamming & CRC ( C++ ) 87 | - [x] Subnetting 88 | - [x] Subnetting Program ( Java ) 89 | - [ ] Sliding Window 90 | - [ ] Go Back N ( C++ / Java ) 91 | - [ ] Selective Repeat ( C++ / Java ) 92 | - [x] UDP Programming in C++ 93 | - [x] UDP File Transfer 94 | - [x] TCP Programming in C++ 95 | - [x] TCP File Transfer 96 | - [ ] TCP Arithmatic 97 | - [ ] UDP & TCP Programming in JAVA 98 | - [ ] Peer to Peer Chat 99 | - [ ] Multi User Chat 100 | - [x] What is TCL & Syntax of TCL 101 | - [x] NS2 102 | - [x] NS2 Basics 103 | - [x] Simple with 3 nodes: TCP/FTP 104 | - [x] Complex ( TCP as well as UDP ) 105 | - [x] Star Topology 106 | - [x] Packet Tracer 107 | - [x] Simple topology 108 | - [x] Complex topology 109 | - [x] RIP, OSPF, BGP 110 | - [x] DHCP 111 | - [x] Installation 112 | - [x] SSH using terminal 113 | - [x] SSH using PYTHON (SSHPASS) 114 | - [ ] Analyzing wireshark packets using C++ 115 | 116 | ## Don't forget to star and share this REPO! 117 | -------------------------------------------------------------------------------- /ass2-hamming-crc/crc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | deque getDeque(string input) 8 | { 9 | deque result; 10 | int x = strlen(input.c_str()); 11 | for (int i = 0; i < x; i++) 12 | { 13 | result.push_back( input[i] == '1'? 1:0 ); 14 | } 15 | return result; 16 | } 17 | 18 | deque findCRC(string bin, string key, string CRC, bool isCheck) 19 | { 20 | int binLen = strlen(bin.c_str()); 21 | int keyLen = strlen(key.c_str()); 22 | 23 | if(!isCheck) 24 | // Attaching ( key-1 ) number of 0's behind data word 25 | for( int i = 0; i < keyLen-1; i++ ) 26 | { 27 | bin += '0'; 28 | } 29 | else 30 | { 31 | // if checking CRC, concat crc instead of key 32 | for( int i = 0; i < keyLen; i++ ) 33 | { 34 | bin += CRC[i]; 35 | } 36 | } 37 | 38 | deque divisor = getDeque(key); 39 | deque divident = getDeque(bin); 40 | deque remainder; 41 | deque currentDivisor; // length of key; part of Divident which we want to xor 42 | string quotient; 43 | 44 | // copying ( key length ) part from divident for xor-ing with the key 45 | for (int i = 0; i < divisor.size()-1; i++) 46 | { 47 | currentDivisor.push_back(divident.front()); 48 | divident.pop_front(); 49 | } 50 | 51 | currentDivisor.push_back(divident.front()); 52 | 53 | // Modulo 2 54 | while(! divident.empty() ) 55 | { 56 | divident.pop_front(); 57 | remainder.clear(); 58 | if(currentDivisor.front() == 0) 59 | { 60 | quotient += '0'; 61 | currentDivisor.push_back(divident.front()); 62 | currentDivisor.pop_front(); 63 | continue; 64 | } 65 | 66 | quotient += '1'; 67 | for (int i = 0; i < divisor.size(); i++) 68 | { 69 | remainder.push_back( currentDivisor.front() ^ divisor[i] ); 70 | currentDivisor.pop_front(); 71 | } 72 | remainder.pop_front(); 73 | currentDivisor = remainder; 74 | currentDivisor.push_back(divident.front()); 75 | } 76 | 77 | // if(!isCheck || quotient[quotient.length()-1] == '0') 78 | // { 79 | // // for last digit 80 | // quotient += '1'; 81 | // for (int i = 0; i < divisor.size(); i++) 82 | // { 83 | // remainder.push_back( currentDivisor.front() ^ divisor[i] ); 84 | // currentDivisor.pop_front(); 85 | // } 86 | // remainder.pop_front(); 87 | // } 88 | // cout << "Quotient: " << quotient << " CRC: "; 89 | cout << " CRC: "; 90 | for (int i = 0; i < remainder.size(); i++) 91 | { 92 | cout << remainder[i]; 93 | } 94 | cout << endl; 95 | 96 | return remainder; 97 | 98 | } 99 | 100 | 101 | bool isValidCRC( deque CRC ) 102 | { 103 | for( int i : CRC ) 104 | if( i != 0 ) 105 | return false; 106 | return true; 107 | } 108 | 109 | // Data word to be sent - 100100 110 | // Key - 1101 111 | 112 | int main() 113 | { 114 | string bin,key; 115 | int choice; 116 | 117 | cout << "Enter binary number\n"; 118 | cin >> bin; 119 | int binLen = strlen(bin.c_str()); 120 | cout << "Enter binary key\n"; 121 | cin >> key; 122 | 123 | deque CRC = findCRC(bin,key,"",false); 124 | 125 | cout << "Enter bit to change ( 0 if no change ) "; 126 | cin >> choice; 127 | 128 | 129 | int totalLen = binLen+CRC.size(); 130 | if( choice != 0 ) 131 | { 132 | if( choice < binLen ) 133 | { 134 | bin[choice-1] = bin[choice-1] == '1' ? '0' : '1'; 135 | } 136 | else if ( choice <= (totalLen) ) 137 | { 138 | CRC[choice-binLen-1] = !CRC[choice-binLen-1]; 139 | } 140 | } 141 | string generatedCRC; 142 | 143 | for( int i : CRC ) 144 | { 145 | generatedCRC+= i==1? '1':'0'; 146 | } 147 | generatedCRC+='\0'; 148 | 149 | if(!isValidCRC(findCRC(bin,key,generatedCRC,true)) ) 150 | cout << "Error exists\n"; 151 | else 152 | cout << "No Error\n"; 153 | 154 | return 0; 155 | } -------------------------------------------------------------------------------- /ass3-subnetting/Subnetting.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.lang.Math; 3 | 4 | public class Subnetting 5 | { 6 | // 8+8+8+x 7 | // powerNumber = 2^x 8 | int powerNumber; 9 | 10 | private int getPowerNumber() 11 | { 12 | return powerNumber; 13 | } 14 | 15 | private void setPowerNumberFromNoOfSubnets( int nSubnets ) 16 | { 17 | while( 256%nSubnets != 0 ) 18 | { 19 | nSubnets++; 20 | } 21 | // powerNumber = (int)Math.pow(2,nSubnets); 22 | powerNumber = 256/nSubnets; 23 | } 24 | 25 | private void setPowerNumberFromCIDR( int cidr ) 26 | { 27 | // finding 8 + 8 + ? + ? 28 | int mod = cidr%8; 29 | powerNumber = (int)Math.pow(2,8-mod); 30 | } 31 | 32 | private int getNumberOfSubnets() 33 | { 34 | return (256/powerNumber); 35 | } 36 | 37 | 38 | public static void main(String[] args) 39 | { 40 | Scanner sc = new Scanner(System.in); 41 | String ip,subnetMask = "255.255."; 42 | int choice,cidr,nSubnets; 43 | boolean isSupernetting = false; 44 | Subnetting subnetting = new Subnetting(); // created object because main() is static. Either do this or create another class especially for main() 45 | 46 | System.out.println("Enter Ip Address"); 47 | ip = sc.next(); 48 | String[] test = ip.split("\\.",5); 49 | for ( String str : test ) 50 | { 51 | int x = Integer.valueOf(str); 52 | if( x < 0 || x > 255 ) 53 | { 54 | System.out.println("Invalid IP"); 55 | System.exit(1); 56 | } 57 | } 58 | 59 | System.out.println("1. Enter CIDR ( ex. 26 )"); 60 | System.out.println("2. Enter number of subnets ( ex. 4 )"); 61 | choice = sc.nextInt(); 62 | 63 | if( choice!=1 && choice !=2) 64 | { 65 | System.out.println("Invalid Input"); 66 | sc.close(); 67 | System.exit(1); 68 | } 69 | 70 | 71 | if( choice == 1 ) 72 | { 73 | cidr = sc.nextInt(); 74 | if( cidr < 16 || cidr > 31) 75 | { 76 | System.out.println("CIDR Does not fit into subnetting or supernetting"); 77 | System.exit(1); 78 | } 79 | // finding if supernetting or subnetting 80 | if( Integer.valueOf(cidr / 8) < 3 ) 81 | isSupernetting = true; 82 | 83 | subnetting.setPowerNumberFromCIDR(cidr); 84 | } 85 | else if ( choice == 2 ) 86 | { 87 | nSubnets = sc.nextInt(); 88 | subnetting.setPowerNumberFromNoOfSubnets(nSubnets); 89 | } 90 | 91 | 92 | int host = 256 - subnetting.getPowerNumber(); 93 | 94 | if( isSupernetting ) 95 | subnetMask += host + ".0"; 96 | else 97 | subnetMask += "255." + host; 98 | System.out.println(subnetMask); 99 | 100 | if(!isSupernetting) 101 | System.out.println("Number of subnets formed: " + subnetting.getNumberOfSubnets()); 102 | else 103 | System.out.println("Number of supernets formed: " + subnetting.getNumberOfSubnets()); 104 | 105 | // removing last element from 106 | ArrayList test2 = new ArrayList<>(Arrays.asList(test)); 107 | int lastIpBits; 108 | if( isSupernetting ) 109 | { 110 | test2.remove(2); 111 | test2.remove(2); 112 | lastIpBits = Integer.valueOf(test[2]); 113 | } 114 | else 115 | { 116 | test2.remove(3); 117 | lastIpBits = Integer.valueOf(test[3]); 118 | } 119 | 120 | // converting array back to string 121 | // half ip will be first 3 ip bits e.g. 192.168.13. ( for printing range ) 122 | String halfIp = ""; 123 | for( String str : test2 ) 124 | { 125 | halfIp = halfIp + str + "."; 126 | } 127 | 128 | // finding range 129 | int pow = subnetting.getPowerNumber(); 130 | int maxLimit = pow; 131 | int minLimit = 0; 132 | while( 256 >= maxLimit ) 133 | { 134 | if( !isSupernetting ) 135 | System.out.print( halfIp + minLimit + " to " + halfIp + (maxLimit-1) ); 136 | else 137 | System.out.print( halfIp + minLimit + ".0" + " to " + halfIp + (maxLimit-1) + ".0"); 138 | 139 | if( minLimit < lastIpBits && maxLimit > lastIpBits ) 140 | System.out.print(" <- ip belongs to this range\n"); 141 | else 142 | System.out.println(); 143 | minLimit = maxLimit; 144 | maxLimit += pow; 145 | } 146 | 147 | sc.close(); 148 | } 149 | } -------------------------------------------------------------------------------- /ass2-hamming-crc/hamming.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int binaryToDecimal(string n) 9 | { 10 | string num = n; 11 | int dec_value = 0; 12 | 13 | // Initializing base value to 1, i.e 2^0 14 | int base = 1; 15 | 16 | int len = num.length(); 17 | for (int i = len - 1; i >= 0; i--) { 18 | if (num[i] == '1') 19 | dec_value += base; 20 | base = base * 2; 21 | } 22 | 23 | return dec_value; 24 | } 25 | 26 | 27 | bool isPowerOfTwo(int n) 28 | { 29 | if(n==0) 30 | return false; 31 | return (ceil(log2(n)) == floor(log2(n))); 32 | } 33 | 34 | string toBinary( int x ){ 35 | string b; 36 | if(x==0) return "0"; 37 | while(x!=0) 38 | { 39 | b = (x%2==0?"0":"1") + b; 40 | x = x / 2; 41 | } 42 | return b; 43 | } 44 | 45 | int getR(int len) 46 | { 47 | int r = 0; 48 | while( pow(2,r) < (len+r+1) ) 49 | r++; 50 | return r; 51 | } 52 | 53 | 54 | // returns decimal of single digit char 55 | int testnum(char c) 56 | { 57 | switch(c) 58 | { 59 | case '0': 60 | return 0; 61 | case '1': 62 | return 1; 63 | case '2' : 64 | return 2; 65 | case '3' : 66 | return 3; 67 | case '4' : 68 | return 4; 69 | case '5' : 70 | return 5; 71 | case '6' : 72 | return 6; 73 | case '7' : 74 | return 7; 75 | case '8' : 76 | return 8; 77 | case '9' : 78 | return 9; 79 | } 80 | } 81 | 82 | 83 | // returns vector of redundant bits 84 | vector hammingBits (string input, int r,bool rev) 85 | { 86 | vector bits; 87 | string ip = input; 88 | int ipLen = input.length(); 89 | int totalLen = ipLen; 90 | if(!rev) 91 | totalLen = ipLen + r; 92 | 93 | // at which place from right we want 1 94 | // for ex. for R1 we want '1' at the LSB 95 | for( int i = 0; i < r; i++ ) 96 | { 97 | // cout << "R" << i; 98 | int count = 0,bit; 99 | // iterate through ip and find AND of only bits which have '1' at appropriate place 100 | for( int j = 1; j <= totalLen; j++) 101 | { 102 | string bin = toBinary(j); 103 | int len = bin.length(); 104 | 105 | if( bin[len-i-1] == '1' ) 106 | { 107 | if( ip[ ( (j-1) ) ] == '1' ) 108 | count++; 109 | 110 | } 111 | } 112 | 113 | bit = (count%2); 114 | bits.push_back(bit); 115 | 116 | } 117 | return bits; 118 | 119 | } 120 | 121 | // returns m+r len hamming code 122 | string usingBinary(string ip){ 123 | int ipLen = ip.length(); 124 | int r = getR(ipLen); 125 | 126 | int totalLen = ipLen+r; 127 | int code[totalLen]; 128 | int counter = 0; 129 | 130 | // demo hamming code with 0 in place of redundant bits; 131 | for(int i = 0; i < totalLen; i++ ) 132 | { 133 | if( isPowerOfTwo(i+1) ) 134 | { 135 | code[i] = 0; 136 | } 137 | else 138 | { 139 | code[i] = testnum(ip[ipLen-counter-1]); 140 | counter++; 141 | } 142 | } 143 | 144 | string codeStr; 145 | for( int i : code ) 146 | { 147 | codeStr += i==1? '1' : '0'; 148 | } 149 | 150 | vector bits; 151 | bits = hammingBits(codeStr,r,true); 152 | 153 | int cnt = 1; 154 | for(int i = 0; i < r; i++) 155 | { 156 | code[cnt-1] = bits[i]; 157 | bits.pop_back(); 158 | cnt*=2; 159 | } 160 | 161 | // converting full m + r len code to string 162 | string res; 163 | for(int i = 0; i < totalLen; i++ ) 164 | { 165 | res += code[i] == 1 ? '1' : '0'; 166 | } 167 | return res; 168 | } 169 | 170 | // generates error bits on reciever side 171 | int errorBits(string input) 172 | { 173 | int r = getR(input.length()); 174 | vector bits; 175 | bits = hammingBits(input,r,true); 176 | 177 | // creating string from error bits 178 | string x; 179 | for(int i : bits) 180 | x += i == 1 ? '1' : '0'; 181 | 182 | // reversing error ( nessesary ) 183 | string rev = x; 184 | int ctr = 0; 185 | for( int i = x.length()-1; i>=0; i--) 186 | { 187 | rev[ctr] = x[i]; 188 | ctr++; 189 | } 190 | 191 | return binaryToDecimal(rev); 192 | } 193 | 194 | int main() 195 | { 196 | string input; 197 | int change; 198 | cout << "Enter 8 bit binary number\n"; 199 | cin >> input; 200 | string hamming = usingBinary(input); 201 | int hamLen = hamming.length(); 202 | cout << "Created Hamming Code: " << hamming << endl; 203 | cout << "Which bit to change? ( 0 if not to change)"; 204 | cin >> change; 205 | if( change!= 0 && change < hamming.length() ) 206 | { 207 | hamming[change-1] = hamming[change-1] == '1' ? '0' : '1'; 208 | } 209 | cout << "new number:" << hamming << endl; 210 | int err = errorBits(hamming); 211 | // cout << "ERR:" <