In: Computer Science
c-programing repl.it no advance this the instruction below do it as needed and put good comment
My Manual DNS (Domain Name Server)
IPv4 internet addresses are of the form:
AAA.BBB.CCC.DDD
Where each letter represents a sectioned "subnet". Computers
also have an associated hostname.
For fun, you can find out your computer's hostname by opening a
terminal and typing hostname.
You are going to write a program that with a series of functions
that perform actions based on this data.
Goal Post 1 (25%):
Create a structure type called address_t that has four integers
(A, B, C, and D respectively)
and stores a hostname (string).
Create a second structure called dns_t that will create an array of 25 address_t types.
Below is a list of functions you will implement. Your main
function should allow a user to pick
which function to perform - you can use a switch statement or if
statements to implement the menu
selection. You program should continue asking the user for an
action to perform unless a program
termination condition is entered (such as q for quit).
Sample menu: Welcome to the IPv4 lookup. s - scan a data file h - find a hostname associated with an IP address i - find an ip address associated with a hostname p - print hostnames associated with a subnet q - end program
Goal Post 2 (25%):
scan_file - Read in from data.txt, which contains a list of no more than 25 IP addresses and nicknames. The ending / terminating line in data.txt wil be an address of 0.0.0.0 and a hostname of none.
Function protypes (recommendations): void scan_file (dns_t *); //scan_file function that only populates a structure of type dns_t OR int scan_file (dns_t *); //scan_file function that populates a structure of type dns_t & returns an int //Recommendation: return an int of how many valid lines you read so you don't search // through array elements that were not populated by valid data
Goal Post 3 (25%):
find_ip - Print out the IP address based on a user given hostname. If the hostname does not exist, it should print a message that states Hostname not in data file. You may assume that no hostname will be repeated twice, so you do not need to continue searching one the first instance is found.
You may prompt the user for the hostname within the function OR within main and then pass the search value to the function.
Function protypes (recommendations) - you can pass a pointer if you want: void find_ip (dns_t); //Only dns_t structure is input parameter - user is prompted for input in the function OR void find_ip (dns_t, char []); //User is prompted for input in main (or elsewhere) OR void find_ip (dns_t, int); //dns_t structure is passed as well as valid data value to constrict search space
Goal Post 4 (25%):
find_hostname - Print out the hostname based on a user given IP address. If the IP does not exist, it should print a message that states IP not in data file. You may assume that no IP will be repeated twice, so you do not need to continue searching one the first instance is found.
Function protypes (recommendations) - you can pass a pointer if you want: void find_hostname (dns_t); //Only dns_t structure is input parameter - user is prompted for input in the function OR void find_hostname (dns_t, int, int, int, int); //User is prompted for input in main (or elsewhere) OR void find_hostname (dns_t, int); //dns_t structure is passed as well as valid data value to constrict search space
Sample execution:
Welcome to the IPv4 lookup. s - scan a data file h - find a hostname associated with an IP address i - find an ip address associated with a hostname p - print hostnames associated with a subnet q - end program Pick something to do: s Data scanned. Pick something to do: i Enter a hostname: bob The hostname bob matches IP 130.108.14.1 Pick something to do: q Goodbye!
Extra Credit (10%):
print_subnet_d_hosts - Prints the hostnames that are on the same
D subnet based on a given user
subnet. For the sample user input 110.115.25.0 this function should
print all hostnames that start with 110.115.25. If none exist,
prints a message that states No hostnames on that subnet.
Working code implemented in C and appropriate comments provided for better understanding.
Source Code for main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ADDRESS 25
#define MAX_HOSTNAME 30
// set up structures needed for code
typedef struct {
int A, B, C, D;
char hostname[MAX_HOSTNAME];
} address_t;
typedef struct {
address_t addresses[MAX_ADDRESS];
} dns_t;
//funtion prototypes
int scan_file (dns_t *);
void find_ip (dns_t, int);
void find_hostname (dns_t, int);
void find_subnet ();
void welcome_screen();
void menu();
// main function
int main () {
//sets up array dns and pointer to pass between functions
dns_t dns;
dns_t *dns_p;
dns_p = &dns;
// counter for how many lines of data are read
int data_lines = 0;
// welcome screen and menu easy print
welcome_screen();
menu();
// 'c' is used to continue loop when no correct input is
chosen
char menu_char = 'c';
// main loop of function runs until user wants to end
program.
while(menu_char == 'c'){
printf("\n\nPlease choose an option:\n");
// While loop to test for the valid input of menu options
while(menu_char != 's' && menu_char != 'h' &&
menu_char != 'i' && menu_char != 'p' && menu_char
!= 'q' && menu_char != 'm'){
menu_char = getchar();
fflush(stdin);
// Invalid input checker
if(menu_char != 's' && menu_char != 'h' &&
menu_char != 'i' && menu_char != 'p' && menu_char
!= 'q' && menu_char != 'm'){
printf("\nInvalid input. Please try again again:\n");
menu_char = 'n';
}
}
// switch statment to easily move between different
functions
switch (menu_char){
// Scan File Case
case 's':
data_lines = scan_file(&dns);
menu_char = 'c';
break;
// Find Host name
case 'h':
find_hostname(dns, data_lines);
menu_char = 'c';
break;
// find an ip
case 'i':
find_ip(dns, data_lines);
menu_char = 'c';
break;
// print host name with subnet
case 'p':
find_subnet(dns, data_lines);
menu_char = 'c';
break;
case 'm':
menu();
menu_char = 'c';
break;
// end program
case 'q':
printf("Program Ended.\nThank you.\n");
break;
}
}
return 0;
}
// function scan for file 'data.txt"
int scan_file(dns_t *a){
printf("scanning file...\n\n");
// sets up FILE pointer to look for file data.txt
FILE *inputp;
inputp = fopen("data.txt","r");
int i = 0;
// scans file one line at a time and puts info into a dns array
called
// once scan is done it ends --- looks for 'none' for terminating
clause
for(i; i < MAX_ADDRESS; i++){
fscanf(inputp, "%d.%d.%d.%d %s", &a->addresses[i].A,
&a->addresses[i].B,
&a->addresses[i].C, &a->addresses[i].D,
a->addresses[i].hostname);
if(strcmp(a->addresses[i].hostname, "none") == 0){
printf("\nEnd of Data Found\n");
printf("Number of lines read = %d\n\n", i);
break;
}
printf("\n Address %2d: %d.%d.%d.%d %s", i, a->addresses[i].A,
a->addresses[i].B,
a->addresses[i].C, a->addresses[i].D,
a->addresses[i].hostname);
}
return i;
}
void find_ip (dns_t a, int lines_read){
printf("Please enter a host name to find an IP
address:\n");
char seek_hostname[MAX_HOSTNAME];
scanf("%s", seek_hostname);
fflush(stdin);
int i = 0;
while(i < lines_read){
if (strcmp(a.addresses[i].hostname, seek_hostname) == 0){
printf("\n\nHOSTNAME FOUND\n");
printf("IP address associated with hostname '%s' is: %d.%d.%d.%d",
seek_hostname,
a.addresses[i].A, a.addresses[i].B, a.addresses[i].C,
a.addresses[i].D);
return;
}
i++;
}
return;
}
void find_hostname (dns_t a, int lines_read){
printf("Please enter an IP Address to find a hostname:\n");
int A, B, C, D;
scanf("%d.%d.%d.%d", &A, &B, &C, &D);
fflush(stdin);
int i = 0;
while(i < lines_read){
//printf("%s\n", a.addresses[i].hostname);
if (a.addresses[i].A == A && a.addresses[i].B == B
&& a.addresses[i].C == C && a.addresses[i].D ==
D){
printf("\n\nIP ADDRESS FOUND\n");
printf("Hostname associated with IP address '%d.%d.%d.%d' is: %s",
A, B, C, D, a.addresses[i].hostname);
return;
}
i++;
}
return;
}
void find_subnet (dns_t a, int lines_read){
printf("Please enter a Subnet to find all hostnames:\n");
int A, B, C;
scanf("%d.%d.%d", &A, &B, &C);
fflush(stdin);
int hostname_counter = 0;
for(int i = 0; i < lines_read; i++){
if(a.addresses[i].A == A && a.addresses[i].B == B
&& a.addresses[i].C == C ){
hostname_counter++;
}
}
if(hostname_counter == 0){
printf("No hostnames found");
} else {
int j = 0;
printf("\n%d Hostnames found with subnet '%d.%d.%d':\n",
hostname_counter, A, B, C);
while(j < lines_read){
//printf("%s\n", a.addresses[i].hostname);
if (a.addresses[j].A == A && a.addresses[j].B == B
&& a.addresses[j].C == C){
printf("%s\n", a.addresses[j].hostname);
}
j++;
}
}
return;
}
void welcome_screen(){
printf(" # # # # # # # # # # # # # # # # # # # # # # # # # #
\n");
printf("# #\n");
printf("# #\n");
printf("# WELCOME #\n");
printf("# to #\n");
printf("# IPv4 Lookup #\n");
printf("# #\n");
printf("# Plese review menu options below #\n");
printf("# #\n");
printf("# #\n");
printf(" # # # # # # # # # # # # # # # # # # # # # # # # # #
\n\n");
return;
}
void menu(){
printf("\tMenu Options:\n");
printf("\ts : scan a data file\n");
printf("\th : find hostname from associated IP address\n");
printf("\ti : find IP address from associated hostname\n");
printf("\tp : print hostnames with associated subnet\n");
printf("\tm : see menu options\n");
printf("\tq : end program\n");
return;
}
Sample Output Screenshots: