In linux, ioctl detects the physical connection of the NIC

Source: Internet
Author: User
In linux, ioctl detects the physical connection of NICs-general Linux technology-Linux programming and kernel information. For details, see the following. /* Check network adapter is up or down */
// Note: one or two adapter drivers may be not support this method
// Exemple: based virtual machine

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include

Const char * VERSION = "0.6.0.48 -- maintain by isnowran ";

Struct mii_data
{
_ 2010phy_id;
_ 2010reg_num;
_ 2010val_in;
_ 2010val_out;
};

Int main (int argc, char * argv [])
{
If (argc! = 2)
{
Printf ("Version: % s \ n", VERSION );
Printf ("Useage: argv [0] ethNO. \ n ");
Return 0;
}

Int skfd = 0;
If (skfd = socket (AF_INET, SOCK_DGRAM, 0) <0)
{
Perror ("socket ");
Return-1;
}

Struct ifreq ifr;
Bzero (& ifr, sizeof (ifr ));
Strncpy (ifr. ifr_name, argv [1], IFNAMSIZ-1 );
Ifr. ifr_name [IFNAMSIZ-1] = 0;
If (ioctl (skfd, SIOCGMIIPHY, & ifr) <0)
{
Perror ("ioctl ");
Return-1;
}

Struct mii_data * mii = NULL;
Mii = (struct mii_data *) & ifr. ifr_data;
Mii-> reg_num = 0x01;
If (ioctl (skfd, SIOCGMIIREG, & ifr) <0)
{
Perror ("ioctl2 ");
Return-1;
}

If (mii-> val_out & 0x0004)
Printf ("Linkup \ n ");
Else
Printf ("Linkdown \ n ");

Close (skfd );
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.