// RAHN2.cpp
#include "stdafx.h"
#include <iostream>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <conio.h>
using namespace std;
#pragma comment(lib, "IPHLPAPI.lib")
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "ws2_32.lib")
#define BUFFER_SIZE 1024
char *myIp(){
WSADATA wsaData;
WORD wVer = MAKEWORD(2, 0);
WSAStartup( wVer, &wsaData );
char hostname[255];
gethostname(hostname, sizeof(hostname));
return inet_ntoa(*reinterpret_cast<in_addr*>(*gethostbyname(hostname)->h_addr_list));
}
char *macAddr(char *Ip)
{
DWORD dwRetVal;
IPAddr DestIp = 0;
IPAddr SrcIp = 0;
ULONG MacAddr[2];
ULONG PhysAddrLen = 6;
BYTE *bPhysAddr;
DestIp = inet_addr(Ip);
SrcIp = inet_addr(NULL);
//memset(&MacAddr, 0xff, sizeof (MacAddr));
dwRetVal = SendARP(DestIp, SrcIp, &MacAddr, &PhysAddrLen);
char add[3];
static char Ret[18];
sprintf(Ret, "");
if (dwRetVal == NO_ERROR) {
bPhysAddr = (BYTE *) & MacAddr;
if (PhysAddrLen) {
for (int i = 0; i < (int) PhysAddrLen; i++) {
sprintf(add, "%.2X", (int) bPhysAddr[i]);
strcat(Ret, add);
if(i<((int)PhysAddrLen-1)){
strcat(Ret, "-");
}
}
} else {
return "ERROR";
}
} else {
return "ERROR";
}
return Ret;
}
bool testConnection(char *ip)
{
DWORD dwRetVal;
ULONG MacAddr[2]; /* for 6-byte hardware addresses */
ULONG PhysAddrLen = 6; /* default to length of six bytes */
IPAddr DestIp = inet_addr(ip);
memset(&MacAddr, 0xff, sizeof (MacAddr));
dwRetVal = SendARP(DestIp, 0, &MacAddr, &PhysAddrLen);
if (dwRetVal == NO_ERROR) {
return true;
} else {
return false;
}
return false;
}
char *getHostName(char *ip){
WSADATA wsaData;
int iResult;
DWORD dwRetval;
struct sockaddr_in saGNI;
char hostname[NI_MAXHOST];
char servInfo[NI_MAXSERV];
u_short port = 80;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
return ip;
}
saGNI.sin_family = AF_INET;
saGNI.sin_addr.s_addr = inet_addr(ip);
saGNI.sin_port = htons(port);
dwRetval = getnameinfo((struct sockaddr *) &saGNI,
sizeof (struct sockaddr),
hostname,
NI_MAXHOST, servInfo,
NI_MAXSERV, NI_NUMERICSERV);
if (dwRetval == 0) {
return hostname;
}
return ip;
}
int main(int argc, char **argv)
{
int t = clock()/(CLOCKS_PER_SEC/1000);
char* half_ip = "192.168.178";
char ip[16];
char partD[4];
char* ret;
char out[1000000];
system("title Resolve All HostNames 2.0");
strcpy(out, " ");
printf("\n");
for(int i = 0; i<256; i++){
if(i<255){
printf(" %i%%\r", (int)((i*100)/256+1));
}else{
printf(" 100%%");
}
strcpy(ip, half_ip);
strcat(ip, ".");
sprintf(partD, "%i", i);
strcat(ip, partD);
ret = getHostName(ip);
bool active;
const char* activity;
if(ret!=NULL&&strcmp(ret, ip)!=0){
strcat(out, " \n\n");
strcat(out, " IP:\t\t");
strcat(out, ip);
strcat(out, "\n Hostname:\t");
strcat(out, ret);
strcat(out, "\n Aktivitaet:\t");
if(strcmp(partD, "1")==0){
active = true;
strcat(out, "aktiv "); //Router muss aktiv sein
}else{
active = testConnection(ip);
activity = (active?"aktiv ":"inaktiv");
strcat(out, activity);
}
strcat(out, "\n MAC-Adresse:\t");
if(active){
ret = macAddr(ip);
//strcat(out, ip);
//cout<<endl<<ip<<endl;
strcat(out, ret);
}else{
//strcat(out, "(unbekannt)");
strcat(out, "(unbekannt) ");
}
strcat(out, "\n");
}
}
strcat(out, "\n\n Verbrauchte Zeit: ");
sprintf(out, "%s%i Millisekunden", out, (clock()/(CLOCKS_PER_SEC/1000))-t);
strcat(out, "\n\n\n Bitte druecken Sie eine Taste, um das Programm zu beenden...");
cout<<out;
getch();
printf("\n");
WSACleanup();
exit(0);
return 0;
}