Networking Forums

Networking Forums > Computer Networking > Linux Networking > Programatically change IP Address

Reply
Thread Tools Display Modes

Programatically change IP Address

 
 
ibondre@gmail.com
Guest
Posts: n/a

 
      01-18-2006, 04:26 PM
How can i programatically change the IP Address, DNS, gateway,
Enable/disable DHCP, in Linux Using C or C++.

Is there also an API to get this properties?

Thanks
Irfan.

 
Reply With Quote
 
 
 
 
Tauno Voipio
Guest
Posts: n/a

 
      01-18-2006, 05:31 PM
(E-Mail Removed) wrote:
> How can i programatically change the IP Address, DNS, gateway,
> Enable/disable DHCP, in Linux Using C or C++.
>
> Is there also an API to get this properties?


The DNS and DHCP settings are in configuration files
you can modify if you have sufficient access rights.

To effect the changes you may need to send a signal
to the corresponding daemon or to restart it.

The IP addresses and routing information are in
kernel tables which are accessible with suitable
ioctl() calls. For details, get a book on network
programming, e.g.

W. Richard Stevens, UNIX Network Programming

Needless to say that the process modifying any network
settings needs sufficient privileges.

--

Tauno Voipio
tauno voipio (at) iki fi
 
Reply With Quote
 
FLY135
Guest
Posts: n/a

 
      01-18-2006, 07:21 PM

(E-Mail Removed) wrote:
> How can i programatically change the IP Address, DNS, gateway,
> Enable/disable DHCP, in Linux Using C or C++.
>
> Is there also an API to get this properties?


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iptools.h>

static const char *l_pszConfigFile = "/etc/sysconfig/network/ifcfg-";

//================================================== ============================
// function: SetNetworkConfig
// description: updates the network configuration file
//================================================== ============================
int SetNetworkConfig(char *ifname,in_addr_t ip,in_addr_t mask,in_addr_t
gw)
{
char szTemp[256];
in_addr_t bc,nw;

mask = ValidateMask(mask);
nw = IP2Network(ip,mask);
bc = IP2Broadcast(ip,mask);

sprintf(szTemp,"%s%s",l_pszConfigFile,ifname);
printf("open -> %s\n",szTemp);
FILE *file = fopen(szTemp,"wr");
if ( file == NULL )
{
perror("SetNetworkConfig");
return -1;
}

fprintf(file,"IPADDR=\"%s\"\n",inet_ntoa(inet_make addr(ip,0)));
fprintf(file,"NETMASK=\"%s\"\n",inet_ntoa(inet_mak eaddr(mask,0)));
fprintf(file,"NETWORK=\"%s\"\n",inet_ntoa(inet_mak eaddr(nw,0)));
fprintf(file,"BROADCAST=\"%s\"\n",inet_ntoa(inet_m akeaddr(bc,0)));
if ( gw != 0 )
fprintf(file,"GATEWAY=\"%s\"\n",inet_ntoa(inet_mak eaddr(gw,0)));
fclose(file);
system("/etc/init.d/network restart");
}

//================================================== ============================
// function: IP2Broadcast
// description: Takes a subnet mask and IP returns a subnet broadcast
address
//================================================== ============================
in_addr_t IP2Broadcast(in_addr_t IP, in_addr_t Mask)
{
in_addr_t bcIP;
in_addr_t bit;
in_addr_t notbit;
int i;

bit = 0x80000000;
notbit = 0xFFFFFFFF;
bcIP = 0;
for ( i=0; i<31; i++)
{
if ((bit & Mask) == 0)
break;
bcIP |= (IP & bit);
notbit &= ~bit;
bit = bit >> 1;
}
bcIP != notbit;
//printf("bcIP = %s\n",inet_ntoa(inet_makeaddr(bcIP,0)));
return bcIP;
}

//================================================== ============================
// function: IP2Network
// description: Takes a subnet mask and IP returns a subnet address
//================================================== ============================
in_addr_t IP2Network(in_addr_t IP, in_addr_t Mask)
{
in_addr_t netIP;
in_addr_t bit;
int i;

bit = 0x80000000;
netIP = 0;
for ( i=0; i<31; i++)
{
if ((bit & Mask) == 0)
break;
netIP |= (IP & bit);
bit = bit >> 1;
}
// printf("Network = %s\n",inet_ntoa(inet_makeaddr(netIP,0)));
return netIP;
}

//================================================== ============================
// function: ValidateMask
// description: Takes a subnet mask and returns a valid version
//================================================== ============================
in_addr_t ValidateMask(in_addr_t Mask)
{
in_addr_t newMask;
in_addr_t bit;
int i;

bit = 0x80000000;
newMask = 0;
for ( i=0; i<31; i++)
{
if ((bit & Mask) == 0)
break;
newMask |= bit;
bit = bit >> 1;
}
// printf("Mask = %s\n",inet_ntoa(inet_makeaddr(Mask,0)));
return Mask;
}

//================================================== ============================
// function: GetLocalIP
// description: retrieve current network ip and mask
//================================================== ============================
int GetIP(char *ifname, in_addr_t *ip, in_addr_t *mask)
{
struct ifreq ifr;
int sock = socket(AF_INET,SOCK_DGRAM,0);

// Get the interface IP address
strcpy( ifr.ifr_name, ifname );
ifr.ifr_addr.sa_family = AF_INET;

if (ioctl( sock, SIOCGIFADDR, &ifr ) < 0)
perror( "SIOCGIFADDR" );
*ip = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
shutdown(sock,SHUT_RDWR);
}

//================================================== ============================
// function: set_default_gw
// description: sets routing table's default gateway to the sock peer
addr
//================================================== ============================
static int set_default_gw( int sockfd, in_addr_t gip )
{
struct sockaddr_in *dst, *gw, *mask;
struct rtentry route;

memset(&route,0,sizeof(struct rtentry));

dst = (struct sockaddr_in *)(&(route.rt_dst));
gw = (struct sockaddr_in *)(&(route.rt_gateway));
mask = (struct sockaddr_in *)(&(route.rt_genmask));

/* Make sure we're talking about IP here */
dst->sin_family = AF_INET;
gw->sin_family = AF_INET;
mask->sin_family = AF_INET;

/* Set up the data for removing the default route */
dst->sin_addr.s_addr = 0;
gw->sin_addr.s_addr = 0;
mask->sin_addr.s_addr = 0;
route.rt_flags = RTF_UP | RTF_GATEWAY;

/* Remove the default route */
ioctl(sockfd,SIOCDELRT,&route);

/* Set up the data for adding the default route */
dst->sin_addr.s_addr = 0;
gw->sin_addr.s_addr = gip;
mask->sin_addr.s_addr = 0;
route.rt_metric = 1;
route.rt_flags = RTF_UP | RTF_GATEWAY;

/* Remove this route if it already exists */
ioctl(sockfd,SIOCDELRT,&route);

/* Add the default route */
if( ioctl(sockfd,SIOCADDRT,&route) == -1 )
{
fprintf( stderr,"Adding default route: %d", errno);
return -1;
}

//fprintf( stdout,"Added default route successfully." );
return 0;
}

//================================================== ============================
// function: UpdateIP
// description: updates the networks parameters
//================================================== ============================
void UpdateIP(const char *ifname, in_addr_t ip, in_addr_t mask,
in_addr_t gip)
{
struct ifreq ifr;
char *myip;
int sock = socket(AF_INET,SOCK_DGRAM,0);
in_addr_t IP;
struct rtentry rt;
struct sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
memset(&rt, 0, sizeof(rt));

// Get the interface IP address
strcpy( ifr.ifr_name, ifname );
ifr.ifr_addr.sa_family = AF_INET;

if (ioctl( sock, SIOCGIFADDR, &ifr ) < 0)
perror( "SIOCGIFADDR" );
myip = inet_ntoa( ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr );

IP = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr = ip;
if (ioctl( sock, SIOCSIFADDR, &ifr ) < 0)
perror( "SIOCSIFADDR" );

set_default_gw(sock, gip);
shutdown(sock,SHUT_RDWR);
}

//================================================== ============================
// function: GetMacAddress
// description: retrieve current network address
//================================================== ============================
int GetMacAddress(char *ifname, char *addr)
{
struct ifreq ifr;
int sock = socket(AF_INET,SOCK_DGRAM,0);

// Get the interface IP address
strcpy( ifr.ifr_name, ifname );
ifr.ifr_addr.sa_family = AF_INET;

if (ioctl( sock, SIOCGIFHWADDR, &ifr ) < 0)
{
perror( "SIOCGHWADDR" );
return -1;
}
memcpy(addr,ifr.ifr_hwaddr.sa_data,6);
//uint8_t *hwaddr = (uint8_t*)addr;
//printf("The hardware address (SIOCGIFHWADDR) of %s is type %d "
// "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n", ifname,
// ifr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
// hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
shutdown(sock,SHUT_RDWR);
return 0;
}

//================================================== ============================
// function: SetMacAddress
// description: Set network address
//================================================== ============================
int SetMacAddress(char *ifname, char *addr)
{
struct ifreq ifr;
int sock = socket(AF_INET,SOCK_DGRAM,0);

// Set the interface IP address
strcpy( ifr.ifr_name, ifname );
ifr.ifr_addr.sa_family = AF_UNIX;
memcpy(ifr.ifr_hwaddr.sa_data,addr,6);

if (ioctl( sock, SIOCSIFHWADDR, &ifr ) < 0)
{
perror( "SIOCSIFHWADDR" );
return -1;
}
shutdown(sock,SHUT_RDWR);
return 0;
}

 
Reply With Quote
 
Michael Heiming
Guest
Posts: n/a

 
      01-18-2006, 07:42 PM
In comp.os.linux.networking (E-Mail Removed):
> How can i programatically change the IP Address, DNS, gateway,
> Enable/disable DHCP, in Linux Using C or C++.


> Is there also an API to get this properties?


Why bother with C/C++ if you can do it from a simple shell script
or even perl, if you like?

BTW
Please read this before posting anything else:

http://cfaj.freeshell.org/google
--
Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
mail: echo (E-Mail Removed) | perl -pe 'y/a-z/n-za-m/'
#bofh excuse 348: We're on Token Ring, and it looks like the
token got loose.
 
Reply With Quote
 
Unruh
Guest
Posts: n/a

 
      01-18-2006, 08:05 PM
Tauno Voipio <(E-Mail Removed)> writes:

>(E-Mail Removed) wrote:
>> How can i programatically change the IP Address, DNS, gateway,
>> Enable/disable DHCP, in Linux Using C or C++.
>>
>> Is there also an API to get this properties?


Why?
The dns server is in /etc/resolv.conf. The gateway is set by the route
command or the ip command in more modern situations.
The address of the card is set either by dhcp or by ifconfig.

You can run programs from within your C program (execle)


>The DNS and DHCP settings are in configuration files
>you can modify if you have sufficient access rights.


>To effect the changes you may need to send a signal
>to the corresponding daemon or to restart it.


>The IP addresses and routing information are in
>kernel tables which are accessible with suitable
>ioctl() calls. For details, get a book on network
>programming, e.g.


> W. Richard Stevens, UNIX Network Programming


>Needless to say that the process modifying any network
>settings needs sufficient privileges.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
TCP/IP Address Change Mark Windows Networking 1 06-24-2006 04:18 PM
Configure tle wireless client of xp programatically Javier Martínez Wireless Networks 0 02-13-2006 10:40 AM
change MAC address can change IP address of a machine? apngss@yahoo.com Linux Networking 7 01-05-2006 11:09 PM
change MAC address can change IP address of a machine? apngss@yahoo.com Wireless Internet 7 01-05-2006 11:09 PM
choose IP for SNAT programatically Grzegorz Sowa Linux Networking 0 06-09-2004 07:40 PM



1 2 3 4 5 6 7 8 9 10 11