0

I am getting the current status of my link whenever it changes (based on this answer) however I need to get the initial status when my daemon starts up. This is what I have.

void read_msg(int fd) {
   // loop through the struct nlmsghdr and pull struct ifinfomsg
   // data to get the flags.
}

int main(int argc, char* argv[]) {
    memset(&sa, 0, sizeof(sa));
    sa.nl_family = AF_NETLINK;
    sa.nl_groups = RTMGRP_LINK;

    fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
    if(fd == -1) 
    {
        perror("Failed to Open Socket");
        return 1;
    }

    struct ifreq ifr;
    memset(&ifr, 0, sizeof(ifr));
    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", cmd_params.iface);
    if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr,
        sizeof(ifr)) < 0) 
    {
            perror("Failed to bind to given interface");
            return 1;
    }
    
    if(bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) 
    {
        perror("Failed to bind to socket");
        return 1;
    }
    
    <--- HERE IS MY ISSUE ------->
    get_link_state("eth0");
    <-- this function uses ioctl(fd, SIOCGIFINDEX, "vif2.0") 
        and ioctl(fd, SIOCGIFFLAGS, struct ifreq*)
    -->

    while(true) {
        read_msg(fd);
    }
}

I would like to avoid the get_link_state() method I am using now so that I don't have to call out my interface since my interface can change during the lifetime of the daemon. As you can see it's a virtual interface.

Finally my question, is there any way I can trigger a call before the while loop so I can read the first message.

Buse
  • 45
  • 6
jiveturkey
  • 2,484
  • 1
  • 23
  • 41

1 Answers1

0

I was able to derive my answer from the answer provided by Tony in this question C code to get the interface name for the IP address in Linux

Community
  • 1
  • 1
jiveturkey
  • 2,484
  • 1
  • 23
  • 41