Assignment 2
1. To find out MAC and IP address of host.
Ans.
#include <string.h> //strncpy
#include <sys/ioctl.h>
#include <net/if.h> //ifreq
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// Returns hostname for the local computer
void checkHostName(int hostname)
{
if (hostname == -1)
{
perror("gethostname");
exit(1);
}
}
// Returns host information corresponding to host name
void checkHostEntry(struct hostent * hostentry)
{
if (hostentry == NULL)
{
perror("gethostbyname");
exit(1);
}
}
// Converts space-delimited IPv4 addresses
// to dotted-decimal format
void checkIPbuffer(char *IPbuffer)
{
if (NULL == IPbuffer)
{
perror("inet_ntoa");
exit(1);
}
}
int main()
{
int fd;
struct ifreq ifr;
char *iface = "eth";
unsigned char *mac;
fd = socket(AF_INET, SOCK_DGRAM, 0);
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name , iface , IFNAMSIZ-1);
ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);
mac = (unsigned char *)ifr.ifr_hwaddr.sa_data;
//display mac address
printf("Mac : %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n" , mac[0], mac[1], mac[2], mac[3], mac[4],
mac[5]);
char hostbuffer[256];
char *IPbuffer;
struct hostent *host_entry;
int hostname;
// To retrieve hostname
hostname = gethostname(hostbuffer, sizeof(hostbuffer));
checkHostName(hostname);
// To retrieve host information
host_entry = gethostbyname(hostbuffer);
checkHostEntry(host_entry);
// To convert an Internet network
// address into ASCII string
IPbuffer = inet_ntoa(*((struct in_addr*)
host_entry->h_addr_list[0]));
printf("Hostname: %s\n", hostbuffer);
printf("Host IP: %s", IPbuffer);
return 0;
}
Output:
2. To Find out the IP address from a given domain and vice-versa.
Ans.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
void resolveDomainToIP(const char *domain) {
struct hostent *host_entry;
char *IPbuffer;
// Get host by name
host_entry = gethostbyname(domain);
if (host_entry == NULL) {
herror("gethostbyname");
exit(1);
}
// Convert the IP address from network byte order to string
IPbuffer = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));
printf("Domain: %s\n", domain);
printf("IP Address: %s\n", IPbuffer);
}
void resolveIPToDomain(const char *ip) {
struct in_addr addr;
struct hostent *host_entry;
// Convert the IP address string to in_addr structure
inet_pton(AF_INET, ip, &addr);
// Get host by IP address
host_entry = gethostbyaddr((const void *) &addr, sizeof(addr), AF_INET);
if (host_entry == NULL) {
herror("gethostbyaddr");
exit(1);
}
printf("IP Address: %s\n", ip);
printf("Domain Name: %s\n", host_entry->h_name);
}
int main() {
char domain[] = "www.google.com"; // Change to any domain you want
char ip[] = "184.25.109.204"; // Change to any IP you want
// Resolve domain to IP
resolveDomainToIP(domain);
// Resolve IP to domain
resolveIPToDomain(ip);
return 0;
}
Output:
3. Encryption and Decryption of a messages in C.
#include <stdio.h>
#include <string.h>
void encrypt(char *message, int shift) {
for (int i = 0; message[i] != '\0'; i++) {
if (message[i] >= 'A' && message[i] <= 'Z') {
message[i] = ((message[i] - 'A' + shift) % 26) + 'A';
}
else if (message[i] >= 'a' && message[i] <= 'z') {
message[i] = ((message[i] - 'a' + shift) % 26) + 'a';
}
}
}
void decrypt(char *message, int shift) {
for (int i = 0; message[i] != '\0'; i++) {
if (message[i] >= 'A' && message[i] <= 'Z') {
message[i] = ((message[i] - 'A' - shift + 26) % 26) + 'A';
}
else if (message[i] >= 'a' && message[i] <= 'z') {
message[i] = ((message[i] - 'a' - shift + 26) % 26) + 'a';
}
}
}
int main() {
char message[100];
int shift;
printf("Enter the message: ");
fgets(message, sizeof(message), stdin);
message[strcspn(message, "\n")] = '\0';
printf("Enter shift value (key): ");
scanf("%d", &shift);
encrypt(message, shift);
printf("Encrypted message: %s\n", message);
decrypt(message, shift);
printf("Decrypted message: %s\n", message);
return 0;
}
Output: