UNIX network programming-detailed description of ioctl function usage

Source: Internet
Author: User
Tags socket error
1. Introduction

The interaction between Linux network programs and the kernel is implemented through IOCTL. IOCTL interacts with the network protocol stack to obtain information about network interfaces, the ing properties of NIC devices, and the network interfaces. you can also view, modify, and delete ARP high-speed cache information. Therefore, it is necessary to understand the specific implementation of the ioctl function.

2. Function Description

Synopsis
# Include <sys/IOCTL. h>

Int IOCTL (int d, int request ,...);

Description
The IOCTL () function manipulates the underlying device parameters
Special files. In particle, specify operating characteristics of char-
Acter special files (e.g., terminals) may be controlled with IOCTL ()
Requests. The argument D must be an open file descriptor.

The second argument is a device-dependent Request Code. The third
Argument is an untyped pointer to memory. It's traditionally char
* Argp (from the days before void * was valid C), and will be so named
For this discussion.

An IOCTL () request has encoded in it whether the argument is an in
Parameter or out parameter, and the size of the argument argp in bytes.
Macros and defines used in specifying an IOCTL () request are located in
The file <sys/IOCTL. h>.

Return Value
Usually, on Success zero is returned. A few IOCTL () requests use
Return value as an output parameter and return a non-negative value on
Success. on error,-1 is returned, and errno is set appropriately.

3. parameter description

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

   

4. Related Data Structure

(1) Network Interface request structure ifreqstruct ifreq {# define ifhwaddrlen 6 // 6 bytes hardware address, that is, macunion {char ifrn_name [ifnamesiz]; // network interface name} ifr_ifrn; union {struct sockaddr ifru_addr; // The local IP address struct sockaddr Dr; // The target IP address struct sockaddr ifru_broadaddr; // The broadcast IP address struct sockaddr ifru_netmask; // The local subnet mask address struct sockaddr; // local MAC address short ifru_flags; // Network Interface flag int ifru_ivalue; // different request meanings have different struct ifmap ifru_map; // NIC address ing int ifru_mtu; // maximum transmission unit char ifru_slave [ifnamsiz]; // placeholder char ifru_newname [ifnamsize]; // new name void _ User * ifru_data; // user data struct if_settings ifru_settings; // device Protocol Settings} ifr_ifru;} # define ifr_name ifr_ifrn.ifrn_name; // interface name # define ifr_hwaddr restart; // Mac # define ifr_addr restart; // local IP # define ifr_dstaddr restart; // target IP # define ifr_broadaddr ifr_ifru.broadaddr; // broadcast IP # define ifr_netmask mask; // subnet mask # define ifr_flags mask; // flag # define ifr_metric ifr_ifru.ifru_ivalue; // interface degree # define ifr_mtu degree; // maximum transmission unit # define ifr_map ifr_ifru.ifru_map; // device address ing # define ifr_slave degree; // sub-device # define ifr_data ifr_ifru.ifru_data; // interface use # define ifr_ifrindex serial; // Network Interface serial number # define ifr_bandwidth serial; // connection bandwidth # define ifr_qlen serial; // transmission unit length # define ifr_newname ifr_ifru.ifru_newname; // new name # define ifr_seeting ifr_ifru.ifru_settings; // if you want to obtain information about network interfaces, input the ifreq struct. (2) Nic device attribute ifmapstruct ifmap {// Nic device ing attribute unsigned long mem_start; // start address unsigned long mem_end; // end address unsigned short base_addr; // base address unsigned char IRQ; // interrupt number unsigned char DMA; // dmaunsigned char port; // port} (3) network Configuration Interface ifconfstruct ifconf {// the network configuration structure is a buffer zone int ifc_len; // the buffer zone ifr_buf size Union {char _ User * ifcu_buf; // display the dashboard pointer struct ifreq _ User * ifcu_req; // point to ifreq pointer} ifc_ifcu;}; # define ifc_buf ifc_ifcu.ifcu_buf; // The buffer address # define ifc_req accept; // ifc_req address (4) ARP high-speed cache operation arpreq/** ARP high-speed cache operation, including IP address and hardware address ing table operation ARP high-speed cache command word siocdarp, siocgarp, siocsarp respectively deletes a record of the ARP high-speed cache, obtains a record of the ARP high-speed cache, and modifies a record of the ARP high-speed cache, struct arpreq {struct sockaddr arp_pa; // Protocol address struct sockaddr arp_ha; // hardware address int arp_flags; // flag struct sockaddr arp_netmask; // subnet mask of Protocol address char arp_dev [16]; // query the name of the network interface}

5. Related code example

# Include <sys/types. h> # include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <sys/IOCTL. h> # include <sys/socket. h> # include <netdb. h> # include <string. h> # include <fcntl. h> # include <net/if. h> # include <string. h> int main (INT argc, char * argv []) {int S, sv6; int err; S = socket (af_inet, sock_dgram, 0); If (S <0) {perror ("socket error"); Return-1 ;}// enter the network interface serial number to obtain the network interface name struct ifreq IFR; int IA = Sizeof (struct ifreq); IA = sizeof (struct sockaddr); IA = 1; err = 0; bzero (& IFR, sizeof (struct ifreq); While (Err! =-1) {IFR. ifr_ifindex = Ia ++; // obtain the name of the 2nd network interfaces err = IOCTL (S, siocgifname, & IFR); If (-1 = ERR) {perror ("index error");} else {// 10111111 11111111 11101101 11101000 char ipbuf [255]; inet_ntop (af_inet, & (struct sockaddr_in *) & (IFR. ifr_ifru.ifru_addr)-> sin_addr, ipbuf, sizeof (ipbuf); printf ("the % DST interface is: % s \ t ADDR: % s \ n", IFR. ifr_ifindex, IFR. ifr_name, ipbuf) ;}// enter the network interface name to obtain the bzero (& IFR, sizeof (Struct ifreq); memcpy (IFR. ifr_name, "eth0", 5); err = IOCTL (S, siocgifflags, & IFR); If (! Err) {printf ("siocgifflags: % d \ n", IFR. ifr_flags);} // obtain MTU and Mac bzero (& IFR, sizeof (struct ifreq); memcpy (IFR. ifr_name, "eth0", 5); err = IOCTL (S, siocgifmtu, & IFR); If (Err! =-1) {printf ("siocgifmtu: % d \ n", IFR. ifr_mtu);} else perror ("IOCTL:"); // obtain the MAC address bzero (& IFR, sizeof (struct ifreq); memcpy (IFR. ifr_name, "eth0", 5); // IFR. ifr_ifindex = 1; err = IOCTL (S, siocgifhwaddr, & IFR); If (-1! = ERR) {unsigned char * hW = IFR. ifr_ifru.ifru_hwaddr.sa_data; printf ("siocgifhwaddr: % 02x: % 02x: % 02x: % 02x: % 02x: % 02x \ n", HW [0], HW [1], HW [2], HW [3], HW [4], HW [5]);} else perror ("IOCTL :"); // obtain the NIC ing parameter command word siocgifmap err = IOCTL (S, siocgifmap, & IFR); If (! Err) {printf ("siocgifmap, mem_start: % d, mem_end: % d, base_addr: % d, ifr_map: % d, DMA: % d, Port: % d \ n ", IFR. ifr_map.mem_start, IFR. ifr_map.mem_end, IFR. ifr_map.base_addr, IFR. ifr_map.irq, IFR. ifr_map.dma, IFR. ifr_map.port);} // obtain the NIC serial number err = IOCTL (S, siocgifindex, & IFR); If (! Err) {printf ("siocgifindex: % d \ n", IFR. ifr_ifindex);} // get the length of the sending queue err = IOCTL (S, siocgiftxqlen, & IFR); If (! Err) {printf ("siocgiftxqlen: % d \ n", IFR. ifr_qlen);} // obtain the network interface IP address bzero (& IFR, sizeof (struct ifreq); memcpy (IFR. ifr_name, "eth0", 5); // IFR. ifr_ifru.ifru_ivalue = 2; struct sockaddr_in * sin = (struct sockaddr_in *) & IFR. ifr_addr; // stores the binary IP char IP Address [16]; // character array, which stores the string memset (IP, 0, 16); err = IOCTL (S, siocgifaddr, & IFR); If (! Err) {inet_ntop (af_inet, & sin-> sin_addr.s_addr, IP, 16); // Save the converted string to the IP array. The second parameter is the binary IP pointer to be converted, the third parameter is the buffer where the IP address is converted and the last parameter is the buffer length printf ("siocgifaddr: % s \ n", ip );} // query the target IP address err = IOCTL (S, siocgifdstaddr, & IFR); If (! Err) {inet_ntop (af_inet, & sin-> sin_addr.s_addr, IP, 16); printf ("siocgifdstaddr: % s \ n", ip );} // query the subnet mask bzero (& IFR, sizeof (struct ifreq); memcpy (IFR. ifr_name, "eth0", 5); err = IOCTL (S, siocgifnetmask, & IFR); If (! Err) {inet_ntop (af_inet, & sin-> sin_addr.s_addr, IP, 16); printf ("siocgifnetmask: % s \ n", ip);}/* // set the IP address, set Network Interface inet_ton (af_inet, "222.27.253.108", & sin-> sin_addr.s_addr); // convert string IP address to binary err = IOCTL (S, siocsifaddr, & IFR ); // send the command if (! Err) {printf ("Check IP -----"); memset (& IFR, 0, sizeof (IFR); memcpy (IFR. ifr_name, "eth0", 5); IOCTL (S, siocgifaddr, & IFR); inet_ntop (af_inet, & sin-> sin_addr.s_addr, IP, 16 ); printf ("% s \ n", ip);} * // obtain the broadcast address memset (& IFR, 0, sizeof (IFR) of the interface; memcpy (IFR. ifr_name, "eth0", 5); IOCTL (S, siocgifbrdaddr, & IFR); struct sockaddr_in * broadcast = (struct sockaddr_in *) & IFR. ifr_broadaddr; // convert to the string inet_ntop (af_inet, & Broadcast-> sin_addr.s_addr, IP, 16 ); // inet_ntop converts the binary IP address to the string printf ("broadcast IP: % s \ n", ip); close (s );}

6. Main kernel function call framework

The kernel implements the ioctl () function sys_ioctl (). The main call framework in the kernel is shown below. It clearly shows the ioctl Control Transfer Framework, the following content will be explained in detail based on the figure:

7. Summary

This article introduces the main usage of ioctrl. Of course, there are still many usage options, which will be appended later. For example, the function usage mainly refers to the usage of the corresponding request list, this is the second parameter usage of the function.

Reference: UNIX Network Programming

Http://blog.163.com/jlz_325/blog/static/19174000920126893245653/

Http://www.linuxidc.com/Linux/2007-12/9680.htm

Http://linux.chinaunix.net/techdoc/develop/2007/09/05/967137.shtml

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.