IOCTL-io channel management function

Source: Internet
Author: User
Tags socket error

In_addr is the structure for storing IP addresses.
Struct in_addr {
U_int32_t s_addr;
};

Ifreq is a structure related to network interfaces.
/*
* Interface request structure used for socket
* IOCTLs. All interface IOCTLs must have Parameter
* Definitions which begin with ifr_name.
* Remainder may be interface specific.
*/

Struct ifreq
{
# Define ifhwaddrlen 6
# Define ifnamsiz 16
Union
{
Char ifrn_name [ifnamsiz];/* If name, e.g. "en0 "*/
} Ifr_ifrn;

Union {
Struct sockaddr ifru_addr;
Struct sockaddr ifru_dstaddr;
Struct sockaddr ifru_broadaddr;
Struct sockaddr ifru_netmask;
Struct sockaddr ifru_hwaddr;
Short ifru_flags;
Int ifru_ivalue;
Int ifru_mtu;
Struct ifmap ifru_map;
Char ifru_slave [ifnamsiz];/* just fits the size */
Char ifru_newname [ifnamsiz];
Char * ifru_data;
Struct if_settings ifru_settings;
} Ifr_ifru;
};

 

 

struct ifreq {#define IFNAMSIZ 16char ifr_name[IFNAMSIZ]; /* if name, e.g., "en0" */union {  struct sockaddr ifru_addr;  struct sockaddr ifru_dstaddr;  char ifru_oname[IFNAMSIZ]; /* other if name */  struct sockaddr ifru_broadaddr;  short ifru_flags;  int ifru_metric;  char ifru_data[1]; /* interface dependent data */  char ifru_enaddr[6];} ifr_ifru;#define ifr_addr ifr_ifru.ifru_addr#define ifr_dstaddr ifr_ifru.ifru_dstaddr#define ifr_oname ifr_ifru.ifru_oname#define ifr_broadaddr ifr_ifru.ifru_broadaddr#define ifr_flags ifr_ifru.ifru_flags#define ifr_metric ifr_ifru.ifru_metric#define ifr_data ifr_ifru.ifru_data#define ifr_enaddr ifr_ifru.ifru_enaddr};

* ************ Program 1 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;
Struct ifreq IFR;
Char * Name, * address;
Int sockfd;
If (argc! = 2)
Usage ();
Else
Name = argv [1];
Sockfd = socket (af_inet, sock_dgram, 0 );
Strncpy (IFR. ifr_name, name, IFNAMSIZ-1 );
If (IOCTL (sockfd, siocgifaddr, & IFR) =-1)
Perror ("IOCTL error"), exit (1 );
ADDR = (struct sockaddr_in *) & (IFR. ifr_addr );
Address = inet_ntoa (ADDR-> sin_addr );
Printf ("Inet ADDR: % s", address );
If (IOCTL (sockfd, siocgifbrdaddr, & IFR) =-1)
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)
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 );
}

 

* ************************** Program 2 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)
{
Struct ifreq IFR;
2010* data, mii_val;
Unsigned phy_id;
/* Get the vitals from the interface .*/
Strncpy (IFR. ifr_name, ifname, ifnamsiz );
If (IOCTL (skfd, siocgmiiphy, & IFR) <0)
{
Fprintf (stderr, "siocgmiiphy on % s failed: % s/n", ifname,
Strerror (errno ));
(Void) Close (skfd );
Return 2;
}
Data = (2008*) (& IFR. ifr_data );
Phy_id = data [0];
Data [1] = 1;
If (IOCTL (skfd, siocgmiireg, & 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;
If (IOCTL (skfd, siocethtool, & IFR) =-1)
{
Printf ("ethtool_glink failed: % s/n", strerror (errno ));
Return 2;
}
Return (edata. Data? 0: 1 );
}
Int main (INT argc, char ** argv)
{
Int skfd =-1;
Char * ifname;
Int retval;
If (argv [1])
Ifname = argv [1];
Else
Ifname = "eth0 ";
/* Open a socket .*/
If (skfd = socket (af_inet, sock_dgram, 0) <0)
{
Printf ("socket error/N ");
Exit (-1 );
}
Retval = detect_ethtool (skfd, ifname );
If (retval = 2)
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;
}

--------------------------- Program 3: test the physical connection more easily ---------------------------

* ******************************* Program 3 ******* **************************************** ******
# 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. ifr_name, devname );
/* 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]);
}
* 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;
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 ");
If (argc <3)
Exit (1 );
If (mixer_fd = open ("/dev/mixer", o_rdwr ))){
Printf ("mixer opened successfully, working.../N ");
Value = base_value * atoi (argv [2]);
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.