Linux IOCTL functions use instance comments

Source: Internet
Author: User

Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://idear.blog.51cto.com/4097017/853582
Source code source: http://www.linuxidc.com/Linux/2007-12/9623p2.htm
As there are almost no source code annotations, it may be difficult for beginners to understand them. Therefore, I added comments and changed some minor errors.
Program 1: inet_addr, netmask, and broad_addr of the detection Interface
Procedure 2: Check whether the physical connection of the interface is normal
Procedure 3: Test physical connections more easily
Procedure 4: adjust the volume

Program 1: inet_addr, netmask, and broad_addr of the detection Interface

Information that may be used:
1. IOCTL and struct ifreq

Http://wenku.baidu.com/view/59f4508d680203d8ce2f2412.html

The source code in the link above, the header file needs to be modified by yourself

// Program 1: inet_addr, netmask, broad_addr # include <stdio. h> # include <string. h> # include <stdlib. h> # include <errno. h> # include <unistd. h> # include <sys/types. h> # include <sys/socket. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <sys/IOCTL. h> # include <net/if. h> static void usage () {printf ("Usage: ipconfig interface \ n"); exit (0) ;}int main (INT argc, char ** argv) {struct sockaddr_in * ADDR; // socket address structure struct ifreq IFR; // used for IOCTL char * Name, * address; // network device name, address int sockfd; // sets the sub-Descriptor if (argc! = 2) // The parameter usage (); else name = argv [1]; sockfd = socket (af_inet, sock_dgram, 0 ); // enable a data stream to intercept strncpy (IFR. ifr_name, name, IFNAMSIZ-1); // restrict the length of the device name and truncate it to prevent overflow if (IOCTL (sockfd, siocgifaddr, & IFR) =-1) // use siocgifaddr to obtain the interface address perror ("IOCTL error"), exit (1); // description error code ADDR = (struct sockaddr_in *) & (IFR. ifr_addr); Address = inet_ntoa (ADDR-> sin_addr); // address translation printf ("Inet ADDR: % s", address); If (IOCTL (sockfd, siocgifbrdaddr, & IFR) =-1) // use siocgifbrdaddr to obtain the broadcast address perror ("IOCTL error"), exit (1); ADDR = (struct sockaddr_in *) & IFR. ifr_broadaddr; address = inet_ntoa (ADDR-> sin_addr); printf ("broad ADDR: % s", address); If (IOCTL (sockfd, siocgifnetmask, & IFR) =-1) // use siocgifnetmask to obtain the mask address perror ("IOCTL error"), exit (1); ADDR = (struct sockaddr_in *) & IFR. ifr_addr; address = inet_ntoa (ADDR-> sin_addr); printf ("Inet mask: % s", address); printf ("\ n"); exit (0 );}

Procedure 2: Check whether the physical connection of the interface is normal

// Program 2: Check whether the physical connection of the interface is normal # include <stdio. h> # include <string. h> # include <errno. h> # include <fcntl. h> # include <getopt. h> # include <sys/socket. h> # include <sys/IOCTL. h> # include <net/if. h> # include <stdlib. h> # include <unistd. h> typedef unsigned short 2010; typedef unsigned int u32; typedef unsigned char u8; # include <Linux/ethtool. h> # include <Linux/sockios. h> int detect_mii (INT skfd, char * ifname) {St Ruct ifreq IFR; 2010* data, mii_val; unsigned phy_id; // get the vitals from the interface. strncpy (IFR. ifr_name, ifname, ifnamsiz); // MII = (media-independent interface) // PHY = physical link if (IOCTL (skfd, siocgmiiphy, & IFR) <0) {fprintf (stderr, "siocgmiiphy on % s failed: % s \ n", ifname, strerror (errno); (void) Close (skfd); return 2 ;} data = (2010*) (& IFR. ifr_data); phy_id = data [0]; data [1] = 1; // reg Regedit if (IOCTL (skfd, S Iocgmiireg, & IFR) <0) {fprintf (stderr, "siocgmiireg on % s failed: % s \ n", IFR. ifr_name, strerror (errno); return 2;} mii_val = data [3]; Return (mii_val & 0x0016) = 0x0004 )? 0: 1);} int detect_ethtool (INT skfd, char * ifname) {struct ifreq IFR; struct ethtool_value edata; memset (& IFR, 0, sizeof (IFR); edata. cmd = ethtool_glink; strncpy (IFR. ifr_name, ifname, sizeof (IFR. ifr_name)-1); IFR. ifr_data = (char *) & edata; // ETH: Ethernet n. ethernet o/\ tool if (IOCTL (skfd, siocethtool, & IFR) =-1) {printf ("ethtool_glink failed: % s \ n ", strerror (errno); return 2;} return (E Data. Data? 0: 1);} int main (INT argc, char ** argv) {int skfd =-1; // sets the sub-Descriptor char * ifname; // interface device name int retval; // return value if (argv [1]) ifname = argv [1]; else ifname = "eth0 "; // default value // open a set of sub-If (skfd = socket (af_inet, sock_dgram, 0) <0) {printf ("socket error \ n "); exit (-1);} // detects the Ethernet device retval = detect_ethtool (skfd, ifname); If (retval = 2) // when the above failure occurs, // The physical link retval = detect_mii (skfd, ifname); close (skfd); If (retval = 2) printf ("cocould not determine status \ n"); If (retval = 1) printf ("link down \ n"); If (retval = 0) printf ("link up \ n"); Return retval ;}

Procedure 3: Test physical connections more easily

// Program 3: test the physical connection more easily # include <stdio. h> # include <stdlib. h> # include <string. h> # include <errno. h> # include <net/if. h> # include <Linux/sockios. h> # include <sys/IOCTL. h> # define linktest_glink 0x0000000a struct linktest_value {unsigned int cmd; unsigned int data ;}; static void usage (const char * pname) {fprintf (stderr, "Usage: % S <device> \ n ", pname); fprintf (stderr," returns: \ n "); fprintf (stderr ," \ T 0: link detected \ n "); fprintf (stderr," \ t % d: % s \ n ", enodev, strerror (enodev); fprintf (stderr, "\ t % d: % s \ n", enonet, strerror (enonet); fprintf (stderr, "\ t % d: % s \ n", eopnotsupp, strerror (eopnotsupp); exit (exit_failure);} static int linktest (const char * devname) {struct ifreq IFR; struct linktest_value edata; int FD; // setup our control structures. memset (& IFR, 0, sizeof (IFR); strcpy (IFR. I Fr_name, devname); // Why strncpy is not used // open control socket. FD = socket (af_inet, sock_dgram, 0); If (FD <0) {return-ecomm;} errno = 0; edata. cmd = linktest_glink; IFR. ifr_data = (caddr_t) & edata; If (! IOCTL (FD, siocethtool, & IFR) {If (edata. data) {fprintf (stdout, "link detected on % s \ n", devname); Return 0 ;}else {errno = enonet ;}} perror ("linktest "); return errno;} int main (INT argc, char * argv []) {If (argc! = 2) {usage (argv [0]);} return linktest (argv [1]);}

Procedure 4: adjust the volume
Information that may be used:
Linux Mixing Device/dev/Mixer
Http://blog.163.com/ji_wei8888/blog/static/48680446201110925825588/

// Procedure 4: adjust the volume # include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> # include <sys/IOCTL. h> # include <sys/Soundcard. h> # include <stdio. h> # include <unistd. h> # include <math. h> # include <string. h> # include <stdlib. h> # define base_value 257 int main (INT argc, char * argv []) {int mixer_fd = 0; char * Names [sound_mixer_nrdevices] = sound_device_labels; int value, I; // check the parameter if (argc <3) {printf ("\ nusage: % s dev_no. [0 .. 24] value [0 .. 100] \ n ", argv [0]); printf (" eg. % s 0 100 \ n ", argv [0]); printf (" will change the volume to max volume. \ n "); printf (" The dev_no. are as below: \ n "); for (I = 0; I <sound_mixer_nrdevices; I ++) {if (I % 3 = 0) printf ("\ n"); printf ("% s: % d \ t", Names [I], I );} printf ("\ n"); exit (1) ;}// open the file if (mixer_fd = open ("/dev/mixer", o_rdwr ))) {printf ("mixer opened successfully, working... \ n "); value = base_value * atoi (argv [2]); // modify the file if (IOCTL (mixer_fd, mixer_write (atoi (argv [1]), & Value) = 0) printf ("successfully ..... "); else printf (" unsuccessfully ..... "); printf (" done. \ n ");} else printf (" can't open/dev/mixer error .... \ n "); exit (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.