Commit b7a8a92b by “liusq”

修改ping命令-08

parent 6139d6d8
......@@ -334,65 +334,6 @@ void Common::determine_interface(char *interface) {
freeifaddrs(ifaddr);
}
QString Common::GetLocalMac(const char *eth_inf) {
struct ifreq ifr;
int sd;
char mac[MAC_SIZE]; // local buffer for mac address
bzero(&ifr, sizeof(struct ifreq));
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
qDebug() << "get" << eth_inf << "mac address socket create error";
return QString();
}
strncpy(ifr.ifr_name, eth_inf, sizeof(ifr.ifr_name) - 1);
if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0) {
qDebug() << "get" << eth_inf << "mac address error";
close(sd);
return QString();
}
snprintf(mac, MAC_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)ifr.ifr_hwaddr.sa_data[0],
(unsigned char)ifr.ifr_hwaddr.sa_data[1],
(unsigned char)ifr.ifr_hwaddr.sa_data[2],
(unsigned char)ifr.ifr_hwaddr.sa_data[3],
(unsigned char)ifr.ifr_hwaddr.sa_data[4],
(unsigned char)ifr.ifr_hwaddr.sa_data[5]);
close(sd);
return QString(mac); // convert C-string to QString
}
QString Common::GetLocalNetMask(const char *eth_inf) {
int sock_netmask;
struct ifreq ifr_mask;
struct sockaddr_in *net_mask;
sock_netmask = socket(AF_INET, SOCK_STREAM, 0);
if (sock_netmask == -1) {
perror("create socket failure...GetLocalNetMask\n");
return QString();
}
memset(&ifr_mask, 0, sizeof(ifr_mask));
strncpy(ifr_mask.ifr_name, eth_inf, sizeof(ifr_mask.ifr_name)-1);
if ((ioctl(sock_netmask, SIOCGIFNETMASK, &ifr_mask)) < 0) {
qDebug() << "netmask ioctl error";
close(sock_netmask);
return QString();
}
net_mask = (struct sockaddr_in*)&(ifr_mask.ifr_netmask);
QString netmaskAddr = QString(inet_ntoa(net_mask->sin_addr)); // Convert to QString
close(sock_netmask);
return netmaskAddr;
}
QString Common::GetLocalGateWay() {
FILE *fp;
char buf[512];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment