Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3.23 IP Address Conversion An Internet Protocol (IP) address is a unique number that allows computers to network together. Computers communicate similar to how you

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

3.23 IP Address Conversion An Internet Protocol (IP) address is a unique number that allows computers to network together. Computers communicate similar to how you get a letter in the mail. In the virtual world, the letter is called a packet. In the real world, a letter has a message (the payload in the virtual world), wrapped in an envelope (the header in the virtual world), and contains the source and destination addresses (typically IP addresses in the virtual world). The mail courier knows where each address lies on the map so that they can deliver the letter. In the virtual world, a computer knows where each IP address is located in a network in the same way that a courier knows where each address lies on the map. Each address may have its own name, ZIP code, city, state, country, etc. The IP address is also divided into several subnetworks, also called SubNets. An IPv4 address has 4 bytes in the format bytel.byte2.byte3.byte4, e.g., 192.168.1.1, where each byte ranges from 0 to 255. A computer may store an IP address as a uint32_t which specifies a four-byte (or 32 bit) unsigned integer, which preserves it's size (four bytes) across machines. This is just a fancy way of saying "four-byte, unsigned variable that has a range from 0 to 4294967296." In this lab, you will convert a uint32_t to a readable IP address using bit shift >>) and bitwise AND (&) operators, along with a mask OxFF; generate a SubNet mask to extract the first three bytes of the IP address; and use the SubNet mask to extract the network address, Below is an example of how to use a mask. Note: In C, you can represent hexadecimal numbers by starting the number with Ox and you can represent a binary number by starting the number with Ob. For instance 0xFF in hexadecimal is Ob11111111 in binary and 255 in decimal. uint32 t IPAddr = Ox12345678, lastByteMask = 0x000000FF; // declares and initializes two uint32 t variables using hexadecimal numbers printf("Fourth byte is: fu ", IPAddr & lastByteMask); // fu is a format specifier for an unsigned integer and is the bitwise AND operator ni+bocadill o al biobiotbelt bustofubeinaddre Executing this code will print "Fourth byte is: 120" (120 = 0x78), which is the last byte of the IP address. IPAddr's bits may also be shifted and masked to obtain a different byte's value, as shown below. uint32_t IPAddr = 0x12345678, lastByteMask = 0x000000FF; printf("Third byte is: $u ", (IPAddr>>8) & lastByteMask); // (IPAddr >> 8) shifts IPAddr by 8 bits to the right, i.e., (IPAddr >> 8) = 0x00123456 This code will now print 'Third byte is: 86' (86 = 0x56), the third byte of the IP address, Your job is to do the following: 1. Using the template, assign the correct values to bytel, byte2, byte3, byte4 using the bit shift and bitwise AND operators, combined with the lastByteMask. 2. Then, generate a SubNet mask by using the lastByteMask and the bitwise NOT (-) operator, and store it in the variable subnetMask. This will result in a SubNet mask of OxFFFFFF00 for this problem. Please note that bitwise NOT is a unary operator that takes a single argument 3. Using the AND (&) operator and the SubNet mask, extract the subnet addresses (i.e., the first three bytes) for IPAddr and localAddress and store the results in variables networkAddress1 and networkAddress2. 4. Using the network addresses, bitwise XOR ) operator, and if /else, determine if the packet came from the local network. If the XOR of the two network addresses is o, print "The packet is from the local network. In". Otherwise, print "The packet is not from the local network. '. Example output: Please input the IP Address: 2130706433 Subnet mask: Oxffffff00 The IP address is: 127.0.0.1 The packet is not from the local network. LAB ACTIVITY 3.23.1: IP Address Conversion 319 main.c Load default template... 1 #include 2 #include // Used to create variables of uint32_t data type 4 int main(void) // uint32_t specifies a 4 byte (32 bit) unsigned // integer, whose size of 4 bytes remains fixed // across all machines. uint32_t IPAddr, bytel, byte2, byte3, byte4; uint32_t lastByteMask = 0x000000FF; uint32_t local Address = 3232235777; uint32_t subnetMask, networkAddressi, networkAddress2; printf("Please input the IP Address: "); scanf("%u", &IPAddr); /* Generate Subnet Mask from lastByteMask using the bitwise NOT here */ subnetMask = -lastByteMask; main.c Load default template... // Extract individual bytes from the IP address (IPAddr) and // assign to byte1, byte2, byte3 and byte4 here byte1 = (IPAddr>>7) & lastByteMask; byte2 = (IPAddr>>5) & lastByteMask; byte3 = (IPAddr>>4) & lastByteMask; byte4 = (IPAddr>>4) & lastByteMask; printf("The IP address is: %u%u.%u.%u ", bytel, byte2, byte3, byte4); /* Get the two network addresses using subnet mask from above */ // Check if packet belongs to the local network using the bitwise XOR and // If Else statements here printf("The packet is not from the local network. "); return 0: 3.23 IP Address Conversion An Internet Protocol (IP) address is a unique number that allows computers to network together. Computers communicate similar to how you get a letter in the mail. In the virtual world, the letter is called a packet. In the real world, a letter has a message (the payload in the virtual world), wrapped in an envelope (the header in the virtual world), and contains the source and destination addresses (typically IP addresses in the virtual world). The mail courier knows where each address lies on the map so that they can deliver the letter. In the virtual world, a computer knows where each IP address is located in a network in the same way that a courier knows where each address lies on the map. Each address may have its own name, ZIP code, city, state, country, etc. The IP address is also divided into several subnetworks, also called SubNets. An IPv4 address has 4 bytes in the format bytel.byte2.byte3.byte4, e.g., 192.168.1.1, where each byte ranges from 0 to 255. A computer may store an IP address as a uint32_t which specifies a four-byte (or 32 bit) unsigned integer, which preserves it's size (four bytes) across machines. This is just a fancy way of saying "four-byte, unsigned variable that has a range from 0 to 4294967296." In this lab, you will convert a uint32_t to a readable IP address using bit shift >>) and bitwise AND (&) operators, along with a mask OxFF; generate a SubNet mask to extract the first three bytes of the IP address; and use the SubNet mask to extract the network address, Below is an example of how to use a mask. Note: In C, you can represent hexadecimal numbers by starting the number with Ox and you can represent a binary number by starting the number with Ob. For instance 0xFF in hexadecimal is Ob11111111 in binary and 255 in decimal. uint32 t IPAddr = Ox12345678, lastByteMask = 0x000000FF; // declares and initializes two uint32 t variables using hexadecimal numbers printf("Fourth byte is: fu ", IPAddr & lastByteMask); // fu is a format specifier for an unsigned integer and is the bitwise AND operator ni+bocadill o al biobiotbelt bustofubeinaddre Executing this code will print "Fourth byte is: 120" (120 = 0x78), which is the last byte of the IP address. IPAddr's bits may also be shifted and masked to obtain a different byte's value, as shown below. uint32_t IPAddr = 0x12345678, lastByteMask = 0x000000FF; printf("Third byte is: $u ", (IPAddr>>8) & lastByteMask); // (IPAddr >> 8) shifts IPAddr by 8 bits to the right, i.e., (IPAddr >> 8) = 0x00123456 This code will now print 'Third byte is: 86' (86 = 0x56), the third byte of the IP address, Your job is to do the following: 1. Using the template, assign the correct values to bytel, byte2, byte3, byte4 using the bit shift and bitwise AND operators, combined with the lastByteMask. 2. Then, generate a SubNet mask by using the lastByteMask and the bitwise NOT (-) operator, and store it in the variable subnetMask. This will result in a SubNet mask of OxFFFFFF00 for this problem. Please note that bitwise NOT is a unary operator that takes a single argument 3. Using the AND (&) operator and the SubNet mask, extract the subnet addresses (i.e., the first three bytes) for IPAddr and localAddress and store the results in variables networkAddress1 and networkAddress2. 4. Using the network addresses, bitwise XOR ) operator, and if /else, determine if the packet came from the local network. If the XOR of the two network addresses is o, print "The packet is from the local network. In". Otherwise, print "The packet is not from the local network. '. Example output: Please input the IP Address: 2130706433 Subnet mask: Oxffffff00 The IP address is: 127.0.0.1 The packet is not from the local network. LAB ACTIVITY 3.23.1: IP Address Conversion 319 main.c Load default template... 1 #include 2 #include // Used to create variables of uint32_t data type 4 int main(void) // uint32_t specifies a 4 byte (32 bit) unsigned // integer, whose size of 4 bytes remains fixed // across all machines. uint32_t IPAddr, bytel, byte2, byte3, byte4; uint32_t lastByteMask = 0x000000FF; uint32_t local Address = 3232235777; uint32_t subnetMask, networkAddressi, networkAddress2; printf("Please input the IP Address: "); scanf("%u", &IPAddr); /* Generate Subnet Mask from lastByteMask using the bitwise NOT here */ subnetMask = -lastByteMask; main.c Load default template... // Extract individual bytes from the IP address (IPAddr) and // assign to byte1, byte2, byte3 and byte4 here byte1 = (IPAddr>>7) & lastByteMask; byte2 = (IPAddr>>5) & lastByteMask; byte3 = (IPAddr>>4) & lastByteMask; byte4 = (IPAddr>>4) & lastByteMask; printf("The IP address is: %u%u.%u.%u ", bytel, byte2, byte3, byte4); /* Get the two network addresses using subnet mask from above */ // Check if packet belongs to the local network using the bitwise XOR and // If Else statements here printf("The packet is not from the local network. "); return 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Neo4j

Authors: Gregory Jordan

1st Edition

1484200225, 9781484200223

More Books

Students also viewed these Databases questions