+1 (315) 557-6473 

IP Address and Alias Management Program in C

In this C program, we've developed an IP address and alias management tool. It employs a linked list structure to create, store, and manipulate IP addresses and their corresponding aliases. The program offers functionalities such as adding new address entries, looking up addresses by their aliases, displaying the complete list of addresses, and showing all aliases associated with a specific location. Additionally, it allows users to load addresses from and save them to a file. The program promotes data validation and input error handling, ensuring the accuracy and integrity of the stored information. It provides a user-friendly interface for effective IP address and alias management.

Comprehensive C Program for Address Management

This C program is a comprehensive address management system that can help with your C assignment. It allows users to store and manipulate IP addresses and their associated aliases in a linked list. The program features functions for adding new addresses, looking up addresses by alias, displaying the entire address list, and finding aliases for a specific location. It also supports the saving of address data to a file and loading addresses from a file when the program starts. The code employs structured data handling, robust input validation, and user-friendly menu-driven interaction, making it an excellent example for students seeking to learn about file I/O, data structures, and input validation in C programming.

Block 1: Header Files and Constants

#include < stdio.h > #include < stdlib.h > #include < string.h > #include < ctype.h > #define TRUE 1 #define FALSE 0
  • This block includes necessary header files for input/output, memory allocation, string manipulation, and character classification.
  • It defines constants TRUE and FALSE, which are used for boolean values in the program.

Block 2: Data Structure Definition

struct address_t { int octet[4]; char alias[11]; struct address_t* next; };
  • Defines a structure address_t that represents an IP address with four octets and an alias.
  • It also has a pointer to the next address, which allows creating a linked list of addresses.

Block 3: Global Variables

struct address_t* head = NULL;
  • Declares a global pointer variable head, which represents the head of the linked list of addresses. It's initialized to NULL.

Block 4: addAddressToList Function

void addAddressToList(struct address_t* address) { // ... }
  • This function adds a new address to the end of the linked list.
  • It checks if the list is empty and adds the address as the first element if it is.
  • If the list is not empty, it traverses the list to find the last element and appends the new address to it.

Block 5: parseAddress Function

struct address_t* parseAddress(char line[255]) { // ... }
  • This function parses a line of input containing an address/alias pair and returns an address_t structure.
  • It uses sscanf to parse the input, checking if it contains the correct number of elements (5) and that the alias is no longer than 10 characters.
  • If parsing is successful, it creates an address_t object and returns it. If parsing fails, it returns NULL.

Block 6: areEqualIgnoreCase Function

int areEqualIgnoreCase(char* str1, char* str2) { // ... }
  • This function checks if two strings are equal while ignoring case.
  • It compares the lengths of the strings and then compares each character, converting them to lowercase before comparing.
  • It returns TRUE if the strings are equal and FALSE if they are not.

Block 7: toUpperCase Function

void toUpperCase(char* str) { // ... }
  • This function converts all letters in a string to uppercase.
  • It iterates through the string and modifies each character to its uppercase equivalent if it is a lowercase letter.

Block 8: loadAddresses Function

void loadAddresses() { // ... }
  • This function loads addresses from a file ("CS222_Inet.txt") into the linked list.
  • It opens the file, reads each line, parses the address/alias pairs, and adds them to the list.
  • It stops when a sentinel address ("0.0.0.0 NONE") is encountered.

Block 9: saveAddresses Function

void saveAddresses() { // ... }
  • This function writes all addresses from the linked list to a file ("CS222_Inet.txt") in the same format as when they were loaded.

Block 10: freeAddresses Function

void freeAddresses() { // ... }
  • This function deallocates memory for all address objects in the linked list.

Block 11: displayAddresses Function

void freeAddresses() { // ... }
  • This function displays all addresses in the linked list, along with their aliases.
  • It also counts the number of addresses and displays the count at the end.

Block 12: trim Function

void trim(char* str) { // ... }
  • This function removes trailing spaces from a string by modifying the string in place.

Block 13: addAddress Function

void addAddress() { // ... }
  • This function allows the user to enter a new address and alias and adds it to the list.
  • It performs various validations, including checking the format, range of octets, and uniqueness of address and alias.

Block 14: lookUpAddress Function

void lookUpAddress() { // ... }
  • This function prompts the user to enter an alias and displays the corresponding address if it exists in the list.

Block 15: displayAliasesForLocation Function

void displayAliasesForLocation() { // ... }
  • This function prompts the user to enter an address location and displays all aliases that match the specified location.

Block 16: main Function

int main(int argc, char *argv[]) { // ... }
  • The main function is the entry point of the program.
  • It starts by loading addresses from a file and then presents a menu to the user for managing the list.
  • It uses a loop to repeatedly prompt the user for menu choices and performs the corresponding actions.
  • The program can add addresses, look up addresses by alias, display the entire list, display aliases for a specific location, save addresses to a file, or exit the program.

Conclusion

The C program provided here serves as a robust tool for managing IP addresses and their corresponding aliases. It offers a comprehensive set of functionalities, allowing users to add, search, and display addresses, making it a valuable resource for both system administrators and network enthusiasts. The program showcases efficient data parsing, validation, and memory management techniques, ensuring data integrity and reliability. With the ability to save addresses to a file and load them on program initialization, it offers a seamless user experience. Whether you are looking to maintain a list of IP addresses for networking purposes or need a practical example of C programming, this code demonstrates effective address management. Its versatility and user-friendly interface make it an essential tool for anyone working with IP address data.