unsigned long do_ioctl_get_ipaddress(char *dev)
{
struct ifreq ifr;
int fd;
int err;
unsigned long ip;
unsigned long myip;
struct in_addr tmp_addr;
int i=0;
strcpy(ifr.ifr_name, dev);
fd = socket(AF_INET, SOCK_DGRAM, 0);
err = ioctl(fd, SIOCGIFADDR, &ifr);
if (err) {
perror("ioctl");
return 0;
}
close(fd);
memcpy(&ip,ifr.ifr_addr.sa_data + 2,4);
myip = ip;
tmp_addr.s_addr=myip;
return myip;
}
#include <sys/socket.h> |
int socket (int family, int type, int protocol); |
Figure 4.2. Protocol family constants for socket function.
Not all combinations of socket family and type are valid. Figure 4.5 shows the valid combinations, along with the actual protocols that are valid for each pair. The boxes marked "Yes" are valid but do not have handy acronyms. The blank boxes are not supported.
#include <unistd.h> |
int ioctl(int fd, int request, ... /* void *arg */ ); |
#define IFNAMSIZ 16
struct ifreq {
char ifr_name[IFNAMSIZ]; /* interface name, e.g., "le0" */
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;
};套接口地址结构
struct ifreq {
char ifr_name[IFNAMSIZ]; /* interface name, e.g., "le0" */
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;
};套接口地址结构