2017-03-22 3 views
0

Je reçois le statut actuel de mon lien quand il change mais j'ai besoin d'obtenir le statut initial quand mon démon démarre. C'est ce que j'ai.Comment déclencher le premier message recv sur un socket pour obtenir le statut NETLINK

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); 
    } 
} 

Je voudrais éviter la méthode get_link_state() je me sers maintenant pour que je ne dois pas appeler mon interface depuis mon interface peut changer au cours de la durée de vie du démon. Comme vous pouvez le voir, c'est une interface virtuelle. Enfin, ma question, est-il possible de déclencher un appel avant la boucle while afin que je puisse lire le premier message.

Répondre