/************************************************* 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;}
關於ifreq結構:
結構原型:
/*
* Interface request structure used for socket
* ioctl's. All interface ioctl's must have parameter
* definitions which begin with ifr_name. The
* 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*/
基本介紹:
ifreq結構定義在/usr/include/net/if.h,用來配置ip地址,啟用介面,配置MTU等介面資訊的。其中包含了一個介面的名字和具體內容——(是個共用體,有可能是IP地址,廣播位址,子網路遮罩,MAC號,MTU或其他內容)。ifreq包含在ifconf結構中。而ifconf結構通常是用來儲存所有介面的資訊的。
舉例說明:
在Linux系統中,ifconfig命令是通過ioctl介面與核心通訊,例如,當系統管理員輸入如下命令來改變介面eth0的MTU大小:
ifconfig eth0 mtu 1250
ifconfig命令首先開啟一個socket,然後通過系統管理員輸入的參數初始化一個資料結構,並通過ioctl調用將資料傳送到核心。SIOCSIFMTU是命令標識符。
struct ifreq data;
fd = socket(PF_INET, SOCK_DGRAM, 0);
< ... initialize "data" ...>
err = ioctl(fd, SIOCSIFMTU, &data);
轉自:http://blog.csdn.net/zhu114wei/article/details/6927513