Download ECE 175: Computer Programming for Engineering Applications

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Unix security wikipedia , lookup

Transcript
ECE 175: Computer Programming for Engineering Applications
Homework Assignment 9
Due Date: November 19th, 2013 11:59 PM
Conventions: Name your C programs as hwx py.c where x corresponds to the homework number and y corresponds to the problem number. As an example the C program for hw1 problem 1 should be named as hw1 p1.c.
Submission Instructions: Use the dropbox on D2L to submit only the .c files.
Problem 1: Chapter 9, Sections 9.1, 9.2, 9.3, 9.4.
Problem 2: Internet addresses that follow the Internet Protocol (IP), commonly referred to as IP addresses, are
32-bit binary numbers consisting of four octets. For convenience, IP addresses are expressed in a “dot-decimal”
notation, whereby every octet of binary numbers is converted to a decimal number from 0 to 255. The decimal
numbers are separated by periods, in the form x.y.z.w. As en example,
11000000.10101000.01100101.00000010 → 192.168.101.2
IP addresses consist of two parts: the network part and the host part. The first n most significant bits of the IP
address represent the network part of the address, while the remaining m bits represent the host part (see Figure
1). A network with m bits allocated to the host address can accommodate up to 2m hosts (network devices). The
IP addresses of hosts that belong to the same network have the same network part, but different host part.
Network Part
Host Part
192.168.101
2
n
m
Figure 1: The network and host parts of an IP address with 24 bits devoted to the network part. All IP addresses
with the same network part (first 24 bits) belong to the same network.
The set of IP addresses assigned to a network are written using the first address of that network, followed by
a slash character (/) and the length n of the network part of the address. For instance, network 192.168.1.0/24
is assigned IP addresses from 192.168.1.0 to 192.168.1.255 (a total of 256 addresses). The /24 denotes the length
of the network part of the address, which corresponds to the 24 most significant bits of the binary representation
of an IP address. In this assignment, you are to develop a program that finds all hosts that belong to the same
network from a list of hosts stored in a file hosts.txt. In your program, use the following structure to store the
details of a host:
typedef struct {
int x, y, z, w;
char os[8];
} host;
The x, y, z, w attributes store the four octets of an IP address. The os attribute stores the operating system
running on the host. The operating system can be “windows”, “osx”, or “linux”. Your program should:
1
1. Load the host information contained in a file hosts.txt to an array of type host. The array must be
dynamically allocated to store exactly the number of hosts contained in the file.
2. Ask the user to enter an IP address in the IP format x.y.z.w
3. Ask the user to enter the length of the network part of the IP address.
4. Ask the user to enter an os, or enter “?” for all operating systems.
5. Print all IP addresses that belong to the same network and have the same operating system.
Your code should use the following functions:
1. host convert IP(char s[ ]);
The convert IP function receives as input a string s representing an IP address in format x.y.z.w and returns a
host variable with the four octets of the IP address stored in the x, y, z, and w attributes. You can use function
long int atoi(char *s) from the stlib.h library to convert a string that contains numbers to a long int.
Example: char s[ ] = ‘‘12’’
int x = atoi(s);
2. void print host(host *h);
The print host function receives as input a pointer h to a host variable and prints out the attributes of the variable
in the following form: x.y.z.w operating system.
3. void find hosts(host *net, int size, host t, int len);
The find hosts function receives as input the array net holding all host information, the target host t entered by
the user, the network part length len, and the size of the net array. The function displays all hosts in net that
are in the same network as t and have a matching os attribute.
Test cases.
Input file:
192.168.128.1
192.168.129.3
192.168.195.5
222.12.11.1
192.167.101.1
192.168.140.9
192.168.139.108
216.155.107.2
osx
osx
osx
windows
osx
windows
linux
windows
Test case 1:
Enter the target IP address: 192.168.128.112
Enter the os, or enter “?” for any os: ?
Enter the length of the network part: 20
2
192.168.128.1
192.168.129.3
192.168.140.9
192.168.139.108
osx
osx
windows
linux
Test case 2:
Enter the target IP address: 192.168.128.112
Enter the os, or enter “?” for any os: osx
Enter the length of the network part: 16
192.168.128.1
192.168.129.3
192.168.195.5
osx
osx
osx
Test case 3:
Enter the target IP address: 192.168.128.0
Enter the os, or enter “?” for any os: ?
Enter the length of the network part: 24
192.168.128.1
osx
Test case 4:
Enter the target IP address: 192.170.128.0
Enter the os, or enter “?” for any os: ?
Enter the length of the network part: 9
192.168.128.1
192.168.129.3
192.168.195.5
192.167.101.1
192.168.140.9
192.168.139.108
osx
osx
osx
osx
windows
linux
Submit file hw9 p2.c via D2L dropbox.
3