You are on page 1of 4

G. H. RAISONI COLLEGE OF ENGG.

, NAGPUR
(An Autonomous Institute under UGC Act 1956)
Department of Computer Science & Engg.

Subject: ADN
Session: 2020-21
Student Details:
Roll Number 02
Name Rutuja Dhengle
Semester VI
Section C
Subject ADN

Practical 1

Aim: - Write a program in C language to configure Internet IP Address.

Theory: -

An Internet Protocol address (IP address) is a numerical label assigned to each device
connected to a computer network that uses the Internet Protocol for communication. An
IP address serves two main functions: host or network interface identification and
location addressing.

Internet Protocol version 4 (IPv4) defines an IP address as a 32-bit number. However,


because of the growth of the Internet and the depletion of available IPv4 addresses, a
new version of IP (IPv6), using 128 bits for the IP address, was standardized in 1998.
IPv6 deployment has been ongoing since the mid-2000s.

IP addresses are written and displayed in human-readable notations, such as


172.16.254.1 in IPv4, and 2001:db8:0:1234:0:567:8:1 in IPv6. The size of the routing
prefix of the address is designated in CIDR notation by suffixing the address with the
number of significant bits, e.g., 192.168.1.15/24, which is equivalent to the historically
used subnet mask 255.255.255.0.

The IP address space is managed globally by the Internet Assigned Numbers Authority
(IANA), and by five regional Internet registries (RIRs) responsible in their designated
territories for assignment to local Internet registries, such as Internet service providers
(ISPs), and other end users.

Program:

#include <stdio.h>
#include <unistd.h>
#include <string.h>

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <ifaddrs.h>

int main()
{
unsigned char ip_address[15];
int fd;
struct ifreq ifr;
struct sockaddr_in *addr;
struct ifaddrs *id;
int val;

val = getifaddrs(&id);
printf("Network Interface Name :- %s\n",id->ifa_name);
printf("Network Address of %s :- %d\n",id->ifa_name,id->ifa_addr);
printf("Network Data :- %d \n",id->ifa_data);

/*Read IP Address*/
printf("Enter Ip Address: ");
scanf("%s",ip_address);

/*AF_INET - to define network interface IPv4*/


/*Creating soket for it.*/
fd = socket(AF_INET, SOCK_DGRAM, 0);

/*AF_INET - to define IPv4 Address type.*/


ifr.ifr_addr.sa_family = AF_INET;

/*eth0 - define the ifr_name - port name


where network attached.*/
memcpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);

/*defining the sockaddr_in*/


addr=(struct sockaddr_in *)&ifr.ifr_addr;

/*convert ip address in correct format to write*/


inet_pton(AF_INET,ip_address,&addr->sin_addr);

/*Setting the Ip Address using ioctl*/


ioctl(fd, SIOCSIFADDR, &ifr);
/*closing fd*/
close(fd);

printf("IP Address updated sucessfully.\n");

/*Getting the Ip Address after Updating.*/

/*clear ip_address buffer with 0x20- space*/

memset((unsigned char*)ip_address,0x20,15);

ioctl(fd, SIOCGIFADDR, &ifr);

/*Extracting Ip Address*/

strcpy(ip_address,inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

printf("Updated IP Address is: %s\n",ip_address);

return 0;

Output:

You might also like