Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6235301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:38:46+00:00 2026-05-24T10:38:46+00:00

I am trying to get and set the IP address using the IOCTL interface

  • 0

I am trying to get and set the IP address using the IOCTL interface on Linux.
I am successfully able to get and set it. When I set the ip address,
ifconfig eth0 shows a proper IP address, but then the system gets disconnected.
i.e. System is not pingable.
Here’s my code for setting the IP address. Please let me know if I am missing something.

struct ifreq ifr;
in_addr_t in_addr;
struct sockaddr_in sin;

memset(&ifr, 0, sizeof(struct ifreq));
memset(&sin, 0, sizeof(struct sockaddr_in));
sockfd = socket(AF_INET, SOCK_STREAM, 0);
sprintf(ifr.ifr_name, "eth0");
in_addr = inet_addr("192.168.101.17");
sin.sin_addr.s_addr = in_addr;
memcpy(&ifr.ifr_addr, &sin, sizeof(struct sockaddr));
io = ioctl(sockfd, SIOCSIFADDR, (char *)&ifr);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:38:47+00:00Added an answer on May 24, 2026 at 10:38 am

    This will work for interfaces or aliases. Use “strace” to verify correct operation:

    strace ./ifconfig
    
    socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 5
    ioctl(5, SIOCSIFADDR, {ifr_name="eth0:8", ifr_addr={AF_INET, inet_addr("192.168.1.202")}}) = 0
    ioctl(5, SIOCGIFFLAGS, {ifr_name="eth0:8", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
    ioctl(5, SIOCSIFFLAGS, {ifr_name="eth0:8", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
    close(5)                                = 0
    

    Complete source code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stddef.h>             /* offsetof */
    #include <net/if.h>
    #include <net/if.h>
    #include <linux/sockios.h>
    #include <netinet/in.h>
    #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
    #include <netpacket/packet.h>
    #include <net/ethernet.h>
    #else
    #include <asm/types.h>
    #include <linux/if_ether.h>
    #endif
    
    #define IFNAME "eth0:2"
    #define HOST "192.168.1.204"
    #define ifreq_offsetof(x)  offsetof(struct ifreq, x)
    
    int main(int argc, char **argv) {
    
            struct ifreq ifr;
            struct sockaddr_in sai;
            int sockfd;                     /* socket fd we use to manipulate stuff with */
            int selector;
            unsigned char mask;
    
            char *p;
    
    
            /* Create a channel to the NET kernel. */
            sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    
            /* get interface name */
            strncpy(ifr.ifr_name, IFNAME, IFNAMSIZ);
    
            memset(&sai, 0, sizeof(struct sockaddr));
            sai.sin_family = AF_INET;
            sai.sin_port = 0;
    
            sai.sin_addr.s_addr = inet_addr(HOST);
    
            p = (char *) &sai;
            memcpy( (((char *)&ifr + ifreq_offsetof(ifr_addr) )),
                            p, sizeof(struct sockaddr));
    
            ioctl(sockfd, SIOCSIFADDR, &ifr);
            ioctl(sockfd, SIOCGIFFLAGS, &ifr);
    
            ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
            // ifr.ifr_flags &= ~selector;  // unset something
    
            ioctl(sockfd, SIOCSIFFLAGS, &ifr);
            close(sockfd);
            return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a new Enterprise Application Project set up in Eclipse using
I've got an asp.net ajax style AccordionPane control that I am trying to get/set
I'm trying to get a set of reports running in an (VB) ASP.NET project
I am new to EC2 and am trying to get it set up so
I'm following several tutorials and references trying to get my kernel set up. I've
I am trying to get an AIM Bot set up in C# and I
I am trying to get a build process set up in TeamCity 5, and
I'm trying to get the jQuery slider to have set values to slide to
I am trying to get my data which is hierarchically set up with a
I'm trying to get the following SQL statement to work: UPDATE myschema.tableA update_tableA SET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.