[C]關於 ioctl 跟 ifreq
ioctl() 是提供對device作input/output control的system call, 根據deivce的不同所實作的參數也不同, 但前兩個是固定的.
<code>int ioctl(int d, int request, ...)</code>
主要有3個參數:
1. d: descriptor, 是透過open()某個device而得到的handle(linux稱descriptor而windows稱handle), 主要是描述該deivice的abstract indicator.
2. request: 根據不同的device提供的request也不同
ex.
SIOCGIFADDRS - Gets the list of addresses associated with an interface.
SIOCGIFADDR - gets an interface address
另外就是 ifreq 是指用來config network device的結構
參考
<code>int ioctl(int d, int request, ...)</code>
主要有3個參數:
1. d: descriptor, 是透過open()某個device而得到的handle(linux稱descriptor而windows稱handle), 主要是描述該deivice的abstract indicator.
2. request: 根據不同的device提供的request也不同
ex.
SIOCGIFADDRS - Gets the list of addresses associated with an interface.
SIOCGIFADDR - gets an interface address
取得本機位址
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
ioctl(sock_fd, SIOCGIFADDRS, &ifr);
另外就是 ifreq 是指用來config network device的結構
struct ifreq {
char ifr_name[IFNAMSIZ];/* Interface name */
union {
struct sockaddrifr_addr;
struct sockaddrifr_dstaddr;
struct sockaddrifr_broadaddr;
struct sockaddrifr_netmask;
struct sockaddrifr_hwaddr;
short ifr_flags;
int ifr_ifindex;
int ifr_metric;
int ifr_mtu;
struct ifmapifr_map;
char ifr_slave[IFNAMSIZ];
char ifr_newname[IFNAMSIZ];
char * ifr_data;
};
};
struct ifconf {
int ifc_len; /* size of buffer */
union {
char * ifc_buf; /* buffer address */
struct ifreq *ifc_req; /* array of structures */
};
};
參考
Comments
Post a Comment