Detailed description of IOCTL functions in Linux

Source: Internet
Author: User

Address: http://www.iteye.com/topic/309442

 

This function affects an open file referenced by the FD parameter.

# Include <unistd. h>

Int IOCTL (int fd, int request,.../* void * Arg */);

Return 0: Success-1: Error

The third parameter is always a pointer, but the pointer type depends on the request parameter.

We can divide network-related requests into 6 categories:

Set interface operations

File Operations

Interface operation

ARP high-speed cache operations

Route table operations

Stream System

The following table lists the Request Parameters for network-related IOCTL requests and the data types that the ARG address must point:

Category

Request

Description

Data Type

Set

Connect

Port

Siocatmark

Siocspgrp

Siocgpgrp

Whether it is in the out-of-band mark

Set the process ID or process group ID of the Set Interface

Obtain the process ID or group ID of the Set interface.

Int

Int

Int

Text

Parts

Fionbin

Fioasync

Fionread

Fiosetown

Fiogetown

Set/clear non-blocking I/O flag

Set/clear signal-driven asynchronous I/O flag

Obtain the number of bytes in the receiving cache.

Set the process ID or group ID of the file.

Obtain the process ID or group ID of the file.

Int

Int

Int

Int

Int

Connect

Port

Siocgifconf

Siocsifaddr

Siocgifaddr

Siocsifflags

Siocgifflags

Siocsifdstaddr

Siocgifdstaddr

Siocgifbrdaddr

Siocsifbrdaddr

Siocgifnetmask

Siocsifnetmask

Siocgifmetric

Siocsifmetric

Siocgifmtu

Siocxxx

Retrieve the list of all interfaces

Set the interface address

Obtain the interface address

Set interface flag

Get interface flag

Set point-to-point address

Get point-to-point address

Get broadcast address

Set broadcast address

Obtain Subnet Mask

Set Subnet Mask

Obtain the measure of an interface

Set the measure of the interface

Obtain the MTU Interface

(A lot more depends on the implementation of the system)

Struct ifconf

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

Struct ifreq

ARP

Siocsarp

Siocgarp

Siocdarp

Create/modify ARP table items

Obtain ARP table items

Delete ARP table items

Struct arpreq

Struct arpreq

Struct arpreq

Road

By

Siocaddrt

Siocdelrt

Add path

Delete path

Struct rtentry

Struct rtentry

Stream

I _xxx

Set operation:

There are three IOCTL requests explicitly used to set interface operations. Both require that the third parameter of IOCTL be a pointer to an integer.

Siocatmark: If the degree pointer of this set of interfaces is currently in the out-of-band mark, a non-0 value is returned through the integer pointed to by the third parameter; otherwise, a 0 value is returned. POSIX replaces this request with the sockatmark function.

Siocgpgrp: return the process ID or group ID of this interface through the integer indicated by the third parameter. This ID specifies the receiving process of the sigio or sigurg signal of this interface. This request is equivalent to the f_getown command of fcntl. POSIX standardizes the fcntl function.

Siocspgrp: sets the process ID or group ID of this interface to the integer pointed to by the third parameter. This ID specifies the receiving process of the sigio or sigurg signal of this interface, this request is equivalent to the f_setown command of fcntl. POSIX standardizes fcntl operations.

File Operations:

The following five requests require the third parameter of IOCTL to point to an integer.

Fionbio: Based on the third parameter of ioctl, point to a 0 or non-0 value to clear or set the non-blocking flag of this interface. This request is equivalent to the o_nonblock File status flag, which is cleared or set through the f_setfl command of fcntl.

Fioasync: Based on the third parameter of iocl, point to a 0 value or a non-0 value to clear or set the signal-driven asynchronous I/O flag for this interface, respectively, it determines whether to receive asynchronous I/O signals (sigio) for this interface ). This request is equivalent to the o_async File status flag, which can be cleared or set through the f_setfl command of fcntl.

Fionread: returns the number of bytes in the buffer zone currently received by this interface through the integer pointed to by the third parameter of IOCTL. This feature is also applicable to files, pipelines, and terminals.

Fiosetown: equivalent to siocspgrp for the set interface.

Fiogetown: equivalent to siocgpgrp for the set interface.

Interface Configuration:

All the interfaces in the system are completed by the siocgifconf request. The request uses the ifconf structure, and the ifconf uses the ifreq

Structure:

Struct ifconf {

Int ifc_len; // buffer size

Union {

Caddr_t ifcu_buf; // input from user-> kernel

Struct ifreq * ifcu_req; // return of structures returned

} Ifc_ifcu;

};

# Define ifc_buf ifc_ifcu.ifcu_buf // buffer address

# Define ifc_req ifc_ifcu.ifcu_req // array of structures returned

# Define ifnamsiz 16

Struct ifreq {

Char ifr_name [ifnamsiz]; // Interface Name, e.g., "le0"

Union {

Struct sockaddr ifru_addr;

Struct sockaddr ifru_dstaddr;

Struct sockaddr ifru_broadaddr;

Short ifru_flags;

Int ifru_metric;

Caddr_t ifru_data;

} Ifr_ifru;

};

# Define ifr_addr ifr_ifru.ifru_addr // address

# Define ifr_dstaddr ifr_ifru.ifru_dstaddr // otner end of p-to-P Link

# Define ifr_broadaddr ifr_ifru.ifru_broadaddr // broadcast address

# Define ifr_flags ifr_ifru.ifru_flags // flags

# Define ifr_metric ifr_ifru.ifru_metric // Metric

# Define ifr_data ifr_ifru.ifru_data // for use by Interface

Before calling ioctl, we must first split a buffer zone and an ifconf structure before initializing the latter. For example

Shows the initialization structure of an ifconf structure, where the buffer size is 1024, and the third parameter of IOCTL points

Such an ifconf structure.

Ifc_len

Ifc_buf

1024

---------------------> Cache

Assume that the kernel returns two ifreq structures. When IOCTL returns, the two ifreq structures are entered in the buffer zone of the same ifconf structure. The ifc_len members of the ifconf structure are also updated, to reflect the amount of information stored in the buffer

Generally, the call of IOCTL in a user program is: IOCTL (int fd, int command, (char *) argstruct) IOCTL call is related to network programming (this article only discusses this ), the file descriptor FD is actually returned by the socket () System Call. The value of the command parameter is specified by/usr/include/Linux/sockios. h. Different functions of these commands can be divided into the following sub-classes:
• Change the route table (for example, siocaddrt and siocdelrt ),
• Read/update arp/RARP caches (for example, siocdarp and siocsrarp ),
• Generally related to network interfaces (such as siocgifname and siocsifaddr)
There are various examples in the gooodies directory that show how to use IOCTL. When you look at these programs, note that the argstruct parameter is related to the command parameter. For example, IOCTL related to the route table uses the rtentry structure. rtentry is defined in/usr/include/Linux/route. H (see the example adddefault. C ). ARP-related IOCTL calls use the arpreq structure. arpreq is defined in/usr/include/Linux/if_arp.h (see the example of arpread. c) The command parameter used for IOCTL calls related to network interfaces usually looks in the form of siocxifyyyy. Here, X is either S (set, write) or G (get, read ). In the getifinfo. C program, command parameters in this form are used to read IP addresses, hardware addresses, broadcast addresses, and flag related to network interfaces ). In these IOCTL calls, the third parameter is the ifreq structure, which is defined in/usr/include/Linux/If. h. In some cases, ioctrl calls may use new definitions other than sockios. H. For example, the wavelan wireless network card

 

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.