Programatically change IP Address

Discussion in 'Linux Networking' started by ibondre, Jan 18, 2006.

  1. ibondre

    ibondre Guest

    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.
     
    ibondre, Jan 18, 2006
    #1
    1. Advertisements

  2. ibondre

    Tauno Voipio Guest

    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, Jan 18, 2006
    #2
    1. Advertisements

  3. ibondre

    FLY135 Guest

    #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_makeaddr(ip,0)));
    fprintf(file,"NETMASK=\"%s\"\n",inet_ntoa(inet_makeaddr(mask,0)));
    fprintf(file,"NETWORK=\"%s\"\n",inet_ntoa(inet_makeaddr(nw,0)));
    fprintf(file,"BROADCAST=\"%s\"\n",inet_ntoa(inet_makeaddr(bc,0)));
    if ( gw != 0 )
    fprintf(file,"GATEWAY=\"%s\"\n",inet_ntoa(inet_makeaddr(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;
    }
     
    FLY135, Jan 18, 2006
    #3
  4. In comp.os.linux.networking :
    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, Jan 18, 2006
    #4
  5. ibondre

    Unruh Guest

    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)

     
    Unruh, Jan 18, 2006
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.