能否通过socket获取本机IP地址,子网掩码,或者是网卡名称?
已知一个已获得连接的fdsocklen_tlen=sizeof(structsockaddr_in);structsockaddr_in*my_addr=calloc(1...
已知一个已获得连接的 fd
socklen_t len = sizeof(struct sockaddr_in);
struct sockaddr_in *my_addr = calloc(1,sizeof(struct sockaddr_in));
getsockname(protocol->fd, (struct sockaddr *)my_addr, &len);
printf ("[my addr] : %s\n",inet_ntoa(my_addr->sin_addr));
通过这段代码,我可已获得本机ip地址
我想通过这些已知信息,获得子网掩码 或者是网卡名称
是(eth0:有线网卡) 还是( wlan0:无线网卡)?
请问这可以实现吗?
写错了,getsockename那句改成下面这样:
getsockname( fd, (struct sockaddr *)my_addr, &len); 展开
socklen_t len = sizeof(struct sockaddr_in);
struct sockaddr_in *my_addr = calloc(1,sizeof(struct sockaddr_in));
getsockname(protocol->fd, (struct sockaddr *)my_addr, &len);
printf ("[my addr] : %s\n",inet_ntoa(my_addr->sin_addr));
通过这段代码,我可已获得本机ip地址
我想通过这些已知信息,获得子网掩码 或者是网卡名称
是(eth0:有线网卡) 还是( wlan0:无线网卡)?
请问这可以实现吗?
写错了,getsockename那句改成下面这样:
getsockname( fd, (struct sockaddr *)my_addr, &len); 展开
1个回答
展开全部
可以实现的,使用ioctl函数,加上想获得的选项即可。
正好手头有个类似的例子:
=======================================================
#include <stdio.h>#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXINTERFACES 16
int main(int argc, char **argv){
register int fd, interface, retn = 0;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;
char mac[32] = "";
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) { ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc)) {
interface = ifc.ifc_len / sizeof(struct ifreq);
printf("interface num is interface=%d\n\n", interface);
while (interface-- > 0) {
printf("net device %s\n", buf[interface].ifr_name);
/*Jugde whether the net card status is promisc */
if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[interface]))) { if (buf[interface].ifr_flags & IFF_PROMISC) {
printf("the interface is PROMISC");
return 0;
}
} else {
char str[256] = "";
sprintf(str, "cpm: ioctl device %s",buf[interface].ifr_name);
perror(str);
}
/*Jugde whether the net card status is up */ if (buf[interface].ifr_flags & IFF_UP) {
printf("the interface status is UP\n");
} else {
printf("the interface status is DOWN\n");
}
/*Get IP of the net card */ if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[interface]))) {
printf("IP address is:");
printf("%s\n", inet_ntoa(((struct sockaddr_in*) (&buf[interface].ifr_addr))->sin_addr));
} else {
char str[256] = "";
sprintf(str, "cpm: ioctl device %s",buf[interface].ifr_name);
perror(str);
}
/*Get HW ADDRESS of the net card */ if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[interface]))) {
printf("HW address is:");
sprintf(mac, "%02x%02x%02x%02x%02x%02x",
(unsigned char) buf[interface].ifr_hwaddr.sa_data[0],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[1],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[2],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[3],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[4],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[5]); // 利用sprintf转换成char *
printf("%s\n", mac);
printf("\n");
}
else {
char str[256];
sprintf(str, "cpm: ioctl device %s",buf[interface].ifr_name);
perror(str);
}
} //end of while
} else
perror("cpm: ioctl");
} else
perror("cpm: socket");
close(fd);
return retn;
}
==============================================
输出:
[root@temp]$./deleteme interface num is interface=2
net device eth0the interface status is UP
IP address is:10.6.15.102
HW address is:005056a44485
net device lothe interface status is UP
IP address is:127.0.0.1
HW address is:000000000000
正好手头有个类似的例子:
=======================================================
#include <stdio.h>#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXINTERFACES 16
int main(int argc, char **argv){
register int fd, interface, retn = 0;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;
char mac[32] = "";
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) { ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl(fd, SIOCGIFCONF, (char *) &ifc)) {
interface = ifc.ifc_len / sizeof(struct ifreq);
printf("interface num is interface=%d\n\n", interface);
while (interface-- > 0) {
printf("net device %s\n", buf[interface].ifr_name);
/*Jugde whether the net card status is promisc */
if (!(ioctl(fd, SIOCGIFFLAGS, (char *) &buf[interface]))) { if (buf[interface].ifr_flags & IFF_PROMISC) {
printf("the interface is PROMISC");
return 0;
}
} else {
char str[256] = "";
sprintf(str, "cpm: ioctl device %s",buf[interface].ifr_name);
perror(str);
}
/*Jugde whether the net card status is up */ if (buf[interface].ifr_flags & IFF_UP) {
printf("the interface status is UP\n");
} else {
printf("the interface status is DOWN\n");
}
/*Get IP of the net card */ if (!(ioctl(fd, SIOCGIFADDR, (char *) &buf[interface]))) {
printf("IP address is:");
printf("%s\n", inet_ntoa(((struct sockaddr_in*) (&buf[interface].ifr_addr))->sin_addr));
} else {
char str[256] = "";
sprintf(str, "cpm: ioctl device %s",buf[interface].ifr_name);
perror(str);
}
/*Get HW ADDRESS of the net card */ if (!(ioctl(fd, SIOCGIFHWADDR, (char *) &buf[interface]))) {
printf("HW address is:");
sprintf(mac, "%02x%02x%02x%02x%02x%02x",
(unsigned char) buf[interface].ifr_hwaddr.sa_data[0],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[1],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[2],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[3],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[4],
(unsigned char) buf[interface].ifr_hwaddr.sa_data[5]); // 利用sprintf转换成char *
printf("%s\n", mac);
printf("\n");
}
else {
char str[256];
sprintf(str, "cpm: ioctl device %s",buf[interface].ifr_name);
perror(str);
}
} //end of while
} else
perror("cpm: ioctl");
} else
perror("cpm: socket");
close(fd);
return retn;
}
==============================================
输出:
[root@temp]$./deleteme interface num is interface=2
net device eth0the interface status is UP
IP address is:10.6.15.102
HW address is:005056a44485
net device lothe interface status is UP
IP address is:127.0.0.1
HW address is:000000000000
追问
这段程序我在CSDN上看到过了,
代码应该在加两个头文件:
#include //close()函数需要
#include //inet_ntoa()函数需要
依然非常感谢!
追答
客气啦,O(∩_∩)O哈哈~,应该是一个例子
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家提供动态代理服务器服务的企业,旨在帮助用户更好地管理网络访问和安全。以下是一些关于StormProxies的IP动态代理服务的特点:1. 高匿名性:StormProxies的动态代理服务器具有高匿名性,可以有效...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询