Question

In: Computer Science

c-programing repl.it no advance this the instruction below do it as needed and put good comment...

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.

Solutions

Expert Solution

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:


Related Solutions

please using Repl.it basic C-programing part1 Write a program that requests 5 integers from the user...
please using Repl.it basic C-programing part1 Write a program that requests 5 integers from the user and stores them in an array. You may do this with either a for loop OR by getting a string from stdin and using sscanf to store formatted input in each spot in the array. part2 Add a function to the program, called get_mean that determines the mean, which is the average of the values. The function should be passed the array and the...
PLEASE DO IN JAVA AND USING REPL.IT Below are the two files, boysNames.txt and GirlsNames.txt Write...
PLEASE DO IN JAVA AND USING REPL.IT Below are the two files, boysNames.txt and GirlsNames.txt Write a program that reads the contents of the two files into two separate arrays, or ArrayLists. The user should be able to enter a boy’s name, a girl’s name, or both, and the application will display messages indicating whether the names were among the most popular. BoyNames: Jacob Michael Joshua Matthew Daniel Christopher Andrew Ethan Joseph William Anthony David Alexander Nicholas Ryan Tyler James...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: · Your C++ source code file. (The file with the .CPP extension).No other files will be accepted. A screenshot of your program running. Program Instructions: Consider the following incomplete C++ program: #include int main() { … } 1. Write a statement that includes the header files fstream, string, and iomanip in this program. 2. Write statements that declare inFile to be an ifstream variable and...
Please my a logical response to the comment below: “Good hygiene (like frequent handwashing) and the...
Please my a logical response to the comment below: “Good hygiene (like frequent handwashing) and the avoidance of known environmental hazards are important for preventing a host of diseases, and environmental health continues to play a very important role in public health and safety” (Jacobsen, 2014, p. 164). According to the World Health Organization, it was stated that “unsafe water, and poor sanitation and hygiene has killed an estimated 1.7 million people annually” (Environment and health in developing countries, 2018)....
Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
Do you think buying and selling for a profit is morally good or bad? Put yourself...
Do you think buying and selling for a profit is morally good or bad? Put yourself in the place of a business owner before you give your answer. How would 1. Aristotle 2. the Chinese 3. the Indians all answer this question?
List the corrections needed to present in good form the balance sheet below. Errors include misclassifications,...
List the corrections needed to present in good form the balance sheet below. Errors include misclassifications, lack of adequate disclosure, and poor terminology. Do not concern yourself with the arithmetic. If an item can be classified in more than one category, select the category most favored by the authors of your textbook. Tanner Corporation Balance Sheet For the year ended December 31, 2018 Assets Current Assets: Cash $18,000 Equity investments-trading (fair value, $32,000) $27,000 Accounts receivable $75,000 Inventory $60,000 Supplies...
How many days in advance do travelers purchase their airline tickets? Below are data showing the...
How many days in advance do travelers purchase their airline tickets? Below are data showing the advance days for a sample of 13 passengers on United Airlines Flight 812 from Chicago to Los Angeles. 11, 7, 11, 4, 15, 14, 71 29, 8, 7, 16, 29, 249 (a) Calculate the mean, median, and mode. (b) Which is the best measure of central tendency? Why? (c) Base on your discussion in part (b), which is the best measure of variation? Determine...
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT...
In C++ Please comment in all-new lines of code, thank you DO NOT USE ANSWERS THAT ALREADY BEEN POSTED, please use code from the assignment Copy-paste will be reported Write a program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120 integers...
What are some good examples where Research Grade Sodium Heparin would be needed to do research...
What are some good examples where Research Grade Sodium Heparin would be needed to do research... examples of research projects where it would be needed... Please be specific as to what kinds of projects research people would use this for ? And how they would use it ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT