S
so. dies ist jetzt mein definitiv letztes posting zu dem thema.
ich habe nach dem prinzip wie oben beschrieben (private ips) folgenden code entwickelt, der für meine zwecke absolut perfekt ist.
/** Ermittelt Hostnamen und alle IPs des Rechners und gibt diese als String zurück */
std::string GibAlleIPs() {
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
int n;
PHOSTENT hostinfo;
wVersionRequested=MAKEWORD(2,2);
char *ip;
std::string s="";
if (WSAStartup(wVersionRequested,&wsaData)==0) {
if (gethostname(name,sizeof(name))==0) {
std::string tmp=name;
s="\r\nHostname: "+tmp+"\r\nIPs:";
if ((hostinfo=gethostbyname(name))!=NULL) {
for (n=0; hostinfo->h_addr_list[n];n++) {
ip=inet_ntoa(*(struct in_addr *)hostinfo->h_addr_list[n]);
s=s+" "+ip;
}
}
}
}
else WSACleanup();
return s;
}
/* Ermittelt ob eine Internetverbindung besteht. */
bool HatInternetVerbindung() {
DWORD status;
bool ergebnis=InternetGetConnectedState(&status,0);
if (ergebnis==true) if (status==INTERNET_CONNECTION_LAN) return true;
std::string ipliste=GibAlleIPs();
int pos;
pos=ipliste.rfind(":");
ipliste=ipliste.substr(pos+2,ipliste.length());
int anzahlips=0;
int anzahllanips=0;
char tmp[4];
bool gefunden=false;
std::string ip="";
std::string testip="";
while ((pos=ipliste.find(" "))>0) {
gefunden=false;
ip=ipliste.substr(0,pos);
ipliste=ipliste.substr(pos+1,ipliste.length());
anzahlips++;
// 127.0.0.0 - 127.0.0.255
pos=ip.rfind(".");
ip=ip.substr(0,pos);
testip="127.0.0";
if (ip==testip) return false;
// 192.168.0.0 - 192.168.255.255
pos=ip.rfind(".");
ip=ip.substr(0,pos);
testip="192.168";
if (ip==testip) {
anzahllanips++;
gefunden=true;
}
// 172.16.0.0 - 172.31.255.255
if (!gefunden)
for (int i=16; i<=31; i++) {
testip="172.";
itoa(i,tmp,10);
testip.append(tmp);
if (ip==testip) {
anzahllanips++;
i=32;
gefunden=true;
}
}
// 10.0.0.0 - 10.255.255.255
if (!gefunden) {
pos=ip.rfind(".");
ip=ip.substr(0,pos);
testip="10";
if (ip==testip) {
anzahllanips++;
}
}
}
// Letzte IP testen
gefunden=false;
ip=ipliste;
anzahlips++;
// 127.0.0.0 - 127.0.0.255
pos=ip.rfind(".");
ip=ip.substr(0,pos);
testip="127.0.0";
if (ip==testip) return false;
// 192.168.0.0 - 192.168.255.255
pos=ip.rfind(".");
ip=ip.substr(0,pos);
testip="192.168";
if (ip==testip) {
anzahllanips++;
gefunden=true;
}
// 172.16.0.0 - 172.31.255.255
if (!gefunden)
for (int i=16; i<=31; i++) {
testip="172.";
itoa(i,tmp,10);
testip.append(tmp);
if (ip==testip) {
anzahllanips++;
i=32;
gefunden=true;
}
}
// 10.0.0.0 - 10.255.255.255
if (!gefunden) {
pos=ip.rfind(".");
ip=ip.substr(0,pos);
testip="10";
if (ip==testip) anzahllanips++;
}
if (anzahllanips==anzahlips) return false;
else return true;
}
juhu!