On Linux: how can I programmatically determine if a NIC interface is enabled and plugged in?

前端 未结 5 1049
旧巷少年郎
旧巷少年郎 2021-02-05 11:51

I want to determine if a network card is enabled, up, and plugged in. Basically, I want to know if the network card will work. I need this information from with a C++ program, a

5条回答
  •  北海茫月
    2021-02-05 12:18

    1. open AF_NETLINK socket
    2. bind it to sockaddr_nl with nl_groups = RTMGRP_LINK
    3. send message RTM_GETLINK to kernel
    4. make poll/epoll on socket to read RTM_NEWLINK and RTM_DELLINK messages
    5. you will receive initial interfaces list and its changes in future

提交回复
热议问题