"Network Programming" IOCTL operation

Source: Internet
Author: User
Tags types of functions

Overview

The IOCTL function is similar to the FCNTL function, and can be used to manipulate the descriptor, get or set the properties of the opened descriptor, but the two functions in the network programming related operation for different types, FCNTL functions have socket operations, file operations, and the IOCTL function in addition to Operation Fcntl In addition to the types of functions that can be manipulated, interface operations, routing table operations, ARP cache operations, and streaming system operations can also be performed.

IOCTL function

The function can implement an open descriptor process operation, which is defined as follows:

/* Function function: Action descriptor, set open descriptor property; * Function prototype: */#include <sys/ioctl.h>/* Perform the I/O control operation specified by REQUEST On FD.   One argument may follow; Its presence and type depend on REQUEST.   Return value depends on REQUEST.  Usually-1 indicates error.  */int IOCTL (int fd, unsigned long int request, .../* void *arg */);/* Description: * The request for network-related requests is divided into the following 6 categories: * (1) Socket operation * (2) File operation * (3) interface operation * (4) ARP cache operation * (5) Routing table operation * (6) Stream system *//**fcntl function * Function: Manipulate file descriptor, set properties of opened file */int fcntl (int fd, int cmd, .../* a RG */);/* Description: * The FCNTL function provides the following features related to network programming: * (1) non-blocking IO; flag: O_nonblock * (2) signal-driven IO; flag: O_async * (3) socket attribute; flag: F_getown, F_setow The value of N * * cmd can be as follows: * Copy file Descriptor *   F_DUPFD (LONG) * Set/Get file descriptor flag *   f_getfd (void) *   f_setfd (LONG) * Set/Get file status flag *< C7/>F_GETFL (void) *   F_SETFL (LONG) * Get/Set File lock *   f_getlk *   f_setlk,f_setlkw * *
Where the request parameter and the data type that the ARG address points to are network-related, the following lists the information for the different network types:


The following is an analysis of the 6 major classes that are divided by request requests.


Socket operations

As can be known from the table above, in a socket operation, the request requests parameter of the IOCTL function has 3 optional values, and the data type of the third parameter corresponding to these 3 values is a pointer to an integer.

    1. Siocatmark: If the read pointer of the socket is currently in an out-of- band Marker , the IOCTL function returns a value other than 0 by the integer pointed to by the third argument; otherwise 0 value is returned;
    2. SIOCGPRP: Returns the process ID or process group IDof the socket by the integer pointed to by the third argument, which specifies the receive process for the socket's SIGIO or Sigurg signal, equivalent to the FCNTL function designation F_getown Command
    3. SIOCSPGRP: Sets the process ID or process group ID of the socket to the integer that is pointed to by the third argument, which specifies the receive process for the socket's SIGIO or Sigurg signal, which is equivalent to the FCNTL function designation F_se Town order;


File operations

The requested request parameter differs from the third parameter with the following content:

    1. Fionbio: The non-blocking I/O flag of the socket can be cleared or set by pointing to a value of 0 or not 0 according to the third parameter of the IOCTL function;
    2. Fioasync: According to the third parameter of the IOCTL function to a value of 0 or not 0, you can clear or set the signal-driven asynchronous I/O flag of the socket and decide whether to receive the asynchronous I/O signal for the socket (SIGIO);
    3. Fionread: Returns the number of bytes of the current socket receive buffer according to the integer pointed to by the third parameter of the IOCTL function;
    4. Fiosetown: For sockets and the preceding SIOCSPGRP equivalents:
    5. Fiogetown: For sockets and the preceding SIOCGPGRP equivalents:


Interface operation

To handle a network interface, you must first obtain all the interface information that is configured on the system from the kernel. The value of the request parameter is specified in the table above. Siocgifconf: Gets all the interfaces configured in the system from the kernel. It uses the structure ifconf,ifconf and uses the IFREQ structure. Assign a buffer and a ifconf structure before invoking the IOCTL, and then initialize the latter, and the third parameter of Iotctl points to the ifconf structure.

The/* structure is defined as follows: */struct ifconf {    int ifc_len;/* Size of buffer, Value-result */    Union {        caddr_t ifcu_buf;/* Input From User->kernel */        struct ifreq *ifcu_req;/* return from Kernel->user */    }IFC_IFCU;}; #define IFC_BUF     ifc_ifcu.ifcu_buf#define ifc_req     ifc_ifcu.ifcu_req#define ifnamsiz    16struct ifreq {    Char Ifr_name[ifnamsiz];    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#define ifr_dstaddr     ifr_ifru.ifru_dstaddr#define ifr_broadaddr   Ifr_ifru.broadaddr#define ifr_flags       ifr_ifru.ifru_flags#define ifr_metric      ifr_ifru.ifru_metric#define Ifr_data        Ifr_ifru.ifru_data

ARP Cache operation

The ARP cache can be manipulated using the IOCTL function, and the third parameter of the IOCTL function must point to a ARPREQ structure defined as follows:

/* ARP IOCTL request.  */struct arpreq  {    struct sockaddr arp_pa;/* Protocol address.  */    struct sockaddr arp_ha;/* Hardware address.  */    int arp_flags;/* flags.  *    /struct sockaddr arp_netmask;/* netmask (only for proxy Arps).  */    char arp_dev[16];  };/ * ARP Flag values.  */#define ATF_COM0X02/* completed entry (ha valid).  */#defineATF_PERM0x04/* Permanent entry.  */#defineATF_PUBL0x08/* Publish entry.  */#defineATF_USETRAILERS0x10/* has requested trailers.  */#define ATF_NETMASK     0x20            /* Want to use a NETMASK (only for   proxy entries).  */#define atf_dontpub0x40/* Don ' t answer this addresses.  */#define ATF_MAGIC0X80/* automatically added entry.  *//* support for the user space Arp daemon, ARPD.  */#define Arpd_update0x01#define Arpd_lookup0x02#define arpd_flush0x03
The following are the values of the request parameters and their functions:

    1. Siocsarp: Adds a new table entry to the ARP cache, or modifies a table entry that already exists in it;
    2. Siocdarp: Deletes the specified table entry from the ARP cache;
    3. Siocgarp: Gets a table entry from the ARP cache;


Routing table Operations

The IOCTL function has 2 requests to manipulate the route, and the corresponding third parameter is a pointer to the RTENTRY structure, which is defined as follows

/* This structure gets passed by the SIOCADDRT and SIOCDELRT calls. */struct rtentry  {    unsigned long int rt_pad1;    struct sockaddr rt_dst;/* Target address.  *    /struct sockaddr rt_gateway;/* Gateway addr (rtf_gateway).  *    /struct sockaddr rt_genmask;/* Target network Mask (IP).  */    unsigned short int rt_flags;    short int rt_pad2;    unsigned long int rt_pad3;    unsigned char rt_tos;    unsigned char rt_class; #if __wordsize = = Short int    rt_pad4[3], #else short    int rt_pad4, #endif    Short int rt_metric;/* +1 for binary compatibility!  */    char *rt_dev;/* forcing the device at Add.  */    unsigned long int rt_mtu;/* Per Route Mtu/window.  */    unsigned long int rt_window;/* window clamping.  */    unsigned short int rt_irtt;/* Initial RTT.  */  };
The following are the values of the request parameters and their functions:

    1. SIOCADDRT: Adds a table entry to the routing table;
    2. SIOCDELRT: Removes a table entry from the routing table;
The summary of this chapter is basically around the IOCTL function in the request parameters and the third parameter to do, feel bad memory, because are some parameter signs, related information or read the book on the explanation, the book has given a few examples;
Reference: "Unix network Programming"

"Network Programming" IOCTL operation

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.