/************************************************* Function: set_ipaddr Description: set ip addr Input: net_dev ipaddr Output: Return: 0 sucess -1 failed Others: *************************************************/int set_ipaddr(const char *net_dev, const char* ipaddr){ struct ifreq ifr; int fd = 0; struct sockaddr_in *pAddr; if((NULL == net_dev) || (NULL == ipaddr)) { dbg_log_print(LOG_ERR, "illegal call function SetGeneralIP!"); return -1; } if ((fd = socket(AF_INET,SOCK_DGRAM,0)) < 0) { dbg_log_print(LOG_ERR,"socket....setip..false!!!"); return -1; } strcpy(ifr.ifr_name, net_dev); pAddr = (struct sockaddr_in *)&(ifr.ifr_addr); bzero(pAddr, sizeof(struct sockaddr_in)); pAddr->sin_addr.s_addr = inet_addr(ipaddr); pAddr->sin_family = AF_INET; if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) { close(fd); dbg_log_print(LOG_ERR,"ioctl..set_ipaddr..false!!!"); return -1; } close(fd); return 0;}
About the ifreq structure:
Structure prototype:
/*
* Interface request structure used for socket
* IOCTL's. All interface IOCTL's must have Parameter
* Definitions which begin with ifr_name.
* Remainder may be interface specific.
*/
Struct ifreq
{
# Define ifhwaddrlen 6
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];
Void _ User * ifru_data;
Struct if_settings ifru_settings;
} Ifr_ifru;
};
# Define ifr_name ifr_ifrn.ifrn_name/* interface name */
# Define ifr_hwaddr ifr_ifru.ifru_hwaddr/* MAC address */
# Define ifr_addr ifr_ifru.ifru_addr/* address */
# Define ifr_dstaddr ifr_ifru.ifru_dstaddr/* Other end of p-p lnk */
# Define ifr_broadaddr ifr_ifru.ifru_broadaddr/* broadcast address */
# Define ifr_netmask ifr_ifru.ifru_netmask/* interface net mask */
# Define ifr_flags ifr_ifru.ifru_flags/* flags */
# Define ifr_metric ifr_ifru.ifru_ivalue/* Metric */
# Define ifr_mtu ifr_ifru.ifru_mtu/* MTU */
# Define ifr_map ifr_ifru.ifru_map/* Device Map */
# Define ifr_slave ifr_ifru.ifru_slave/* slave device */
# Define ifr_data ifr_ifru.ifru_data/* for use by interface */
# Define ifr_ifindex ifr_ifru.ifru_ivalue/* interface Index */
# Define ifr_bandwidth ifr_ifru.ifru_ivalue/* link bandwidth */
# Define ifr_qlen ifr_ifru.ifru_ivalue/* queue length */
# Define ifr_newname ifr_ifru.ifru_newname/* New name */
# Define ifr_settings ifr_ifru.ifru_settings/* device/proto settings */
Basic Introduction:
The ifreq structure is defined in/usr/include/NET/If. H. It is used to configure IP addresses, activate interfaces, configure MTU and other interface information. It contains the name and specific content of an interface (it is a shared body, which may be an IP address, broadcast address, subnet mask, MAC Number, MTU or other content ). Ifreq is included in the ifconf structure. The ifconf structure is usually used to save information of all interfaces.
Example:
In Linux, The ifconfig command communicates with the kernel through the ioctl interface. For example, when the system administrator enters the following command to change the MTU size of the eth0 interface:
Ifconfig eth0 MTU 1250
The ifconfig command first opens a socket, then initializes a data structure through the parameters entered by the system administrator, and transmits the data to the kernel through The IOCTL call. Siocsifmtu is the command identifier.
Struct ifreq data;
FD = socket (pf_inet, sock_dgram, 0 );
<... Initialize "data"...>
Err = IOCTL (FD, siocsifmtu, & data );
From: http://blog.csdn.net/zhu114wei/article/details/6927513