📄 netscanner.cpp
字号:
/* netscanner.cpp * * Copyright (c) 2000, Alexander Neundorf, * neundorf@kde.org * * You may distribute under the terms of the GNU General Public * License as specified in the COPYING file. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * */#include "config.h"#include "netscanner.h"#include "ipaddress.h"#include "lisadefines.h"#include "getdebug.h"#include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#ifdef __osf__#undef BYTE_ORDER#define _OSF_SOURCE#undef _MACHINE_ENDIAN_H_#undef __STDC__#define __STDC__ 0#include <netinet/ip.h>#undef __STDC__#define __STDC__ 1#endif#include <netinet/in_systm.h>#include <netinet/ip.h>#include <netinet/ip_icmp.h>#define getDebug() getDebug()<<procIdstruct ICMPEchoRequest{ unsigned char type; unsigned char code; unsigned short int checkSum; unsigned short id; unsigned short seqNumber;};unsigned short in_cksum(unsigned short *addr, int len){ int nleft = len; int sum(0); unsigned short *w = addr; unsigned short answer = 0; /* * Our algorithm is simple, using a 32 bit accumulator (sum), we add * sequential 16 bit words to it, and at the end, fold back all the * carry bits from the top 16 bits into the lower 16 bits. */ while (nleft > 1) { sum += *w++; nleft -= 2; } /* 4mop up an odd byte, if necessary */ if (nleft == 1) { *(unsigned char *)(&answer) = *(unsigned char *)w ; sum += answer; } /* 4add back carry outs from top 16 bits to low 16 bits */ sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ sum += (sum >> 16); /* add carry */ answer = ~sum; /* truncate to 16 bits */ return(answer);}NetScanner::NetScanner(int& rawSocketFD, int strictMode):procId(""),m_firstWait(5),m_secondWait(15),m_strictMode(strictMode),m_rawSocketFD(rawSocketFD),validator(),ipRangeStr(";"),m_maxPings(256),m_deliverUnnamedHosts(0),m_useNmblookup(0),hostList(0),tmpIPRange(""){};NetScanner::~NetScanner(){ delete hostList; ::close(m_rawSocketFD);};void addMissingSemicolon(MyString& text){ if (text.isEmpty()) return; if (text[text.length()-1]!=';') text+=';';};void NetScanner::configure(Config& config){ //ranges are not allowed in strict mode if (!m_strictMode) { ipRangeStr=stripWhiteSpace(config.getEntry("PingAddresses","")); addMissingSemicolon(ipRangeStr); }; MyString pingNames=stripWhiteSpace(config.getEntry("PingNames","")); addMissingSemicolon(pingNames); MyString nextName; int semicolonPos=pingNames.find(';'); int hostsAdded(0); while (semicolonPos!=-1) { nextName=pingNames.left(semicolonPos); getDebug()<<"NetScanner::configure(): looking up -"<<nextName<<"-"<<endl; //now the name lookup in_addr server_addr; hostent *hp=gethostbyname(nextName.data()); if (hp!=0) { if ((m_strictMode) && (hostsAdded>=STRICTMODEMAXHOSTS)) break; memcpy(&server_addr, hp->h_addr, hp->h_length); char *ip=inet_ntoa(server_addr); getDebug()<<"NetScanner::configure(): looking up "<<nextName<<" gives -"<<ip<<"-"<<endl; ipRangeStr=ipRangeStr+ip+';'; hostsAdded++; } pingNames=pingNames.mid(semicolonPos+1); semicolonPos=pingNames.find(';'); }; if ((!ipRangeStr.isEmpty()) && (ipRangeStr[0]==';')) ipRangeStr=ipRangeStr.mid(1); m_deliverUnnamedHosts=config.getEntry("DeliverUnnamedHosts",0); m_useNmblookup=config.getEntry("SearchUsingNmblookup",0); m_maxPings=config.getEntry("MaxPingsAtOnce",256); m_firstWait=config.getEntry("FirstWait",5); m_secondWait=config.getEntry("SecondWait",15); if (m_firstWait<1) m_firstWait=1; if (m_maxPings<8) m_maxPings=8; if (m_maxPings>1024) m_maxPings=1024; //on some systems (Solaris ?) select() doesn't work correctly // if the microseconds are more than 1.000.000 if (m_firstWait>99) m_firstWait=99; if (m_secondWait>99) m_secondWait=99; getDebug()<<"NetScanner::configure(): "<<ipRangeStr<<endl;};struct in_addr NetScanner::getIPfromArray(unsigned int index){ //getDebug()<<endl<<"*** start ***"<<endl; unsigned int tmpIndex(0),indexLeft(index); resetIPRange(); MyString tmp(getNextIPRange());// getDebug()<<"NetScanner::getIPFromArray: -"<<tmp<<"-"<<endl; while (tmp!="") { if (tmp.contains('/')) { //getDebug()<<"net/mask combination detected"<<endl; MyString netStr(tmp.left(tmp.find("/"))); MyString maskStr(tmp.mid(tmp.find("/")+1)); unsigned int net(IPAddress(netStr).asInt()); unsigned int mask(IPAddress(maskStr).asInt()); if ((~mask)<indexLeft) { indexLeft=indexLeft-(~mask+1); tmpIndex+=(~mask)+1; //getDebug()<<"i: "<<tmpIndex<<" left: "<<indexLeft<<endl; } else { net+=indexLeft; return IPAddress(net).asStruct(); //return string2Struct(ipInt2String(net)); }; } else if (tmp.contains('-')==1) { //getDebug()<<"single range detected"<<endl; MyString fromIPStr(tmp.left(tmp.find("-"))); MyString toIPStr(tmp.mid(tmp.find("-")+1)); //getDebug()<<"fromIPStr: "<<fromIPStr<<endl; //getDebug()<<"toIPStr: "<<toIPStr<<endl; unsigned int fromIP(IPAddress(fromIPStr).asInt()); unsigned int toIP(IPAddress(toIPStr).asInt()); //unsigned int fromIP(ipString2Int(fromIPStr)), toIP(ipString2Int(toIPStr)); //index hinter diesem bereich if ((fromIP+indexLeft)>toIP) { tmpIndex+=1+toIP-fromIP; indexLeft=indexLeft-(1+toIP-fromIP); //getDebug()<<"i: "<<tmpIndex<<" left: "<<indexLeft<<endl; } //index in diesem bereich else { fromIP+=indexLeft; return IPAddress(fromIP).asStruct(); //return string2Struct(ipInt2String(fromIP)); }; } else if (tmp.contains('-')==4) { //getDebug()<<"multiple range detected"<<endl; int cp(tmp.find('-')); int from1(atoi(tmp.left(cp).data())); tmp=tmp.mid(cp+1); cp=tmp.find('.'); int to1(atoi(tmp.left(cp).data())); tmp=tmp.mid(cp+1); cp=tmp.find('-'); int from2(atoi(tmp.left(cp).data())); tmp=tmp.mid(cp+1); cp=tmp.find('.'); int to2(atoi(tmp.left(cp).data())); tmp=tmp.mid(cp+1); cp=tmp.find('-'); int from3(atoi(tmp.left(cp).data())); tmp=tmp.mid(cp+1); cp=tmp.find('.'); int to3(atoi(tmp.left(cp).data())); tmp=tmp.mid(cp+1); cp=tmp.find('-'); int from4(atoi(tmp.left(cp).data())); tmp=tmp.mid(cp+1); int to4(atoi(tmp.data())); unsigned int count((1+to4-from4)*(1+to3-from3)*(1+to2-from2)*(1+to1-from1)); if (count<indexLeft) { tmpIndex+=count; indexLeft-=count; //getDebug()<<"i: "<<tmpIndex<<" left: "<<indexLeft<<endl; } else { for (int b1=from1; b1<=to1; b1++) for (int b2=from2; b2<=to2; b2++) for (int b3=from3; b3<=to3; b3++) for (int b4=from4; b4<=to4; b4++) { if (tmpIndex==index) { return IPAddress(b1,b2,b3,b4).asStruct(); }; tmpIndex++; indexLeft--; //getDebug()<<"i: "<<tmpIndex<<" left: "<<indexLeft<<endl; }; }; } //single IP address else if (tmp.contains('.')==3) { //getDebug()<<"single IP address detected"<<endl; //if (tmpIndex==index) return string2Struct(tmp); if (tmpIndex==index) return IPAddress(tmp).asStruct(); else { tmpIndex++; indexLeft--; //getDebug()<<"i: "<<tmpIndex<<" left: "<<indexLeft<<endl; }; }; //getDebug()<<"nextIPRange: *"<<tmp<<"*"<<endl; tmp=getNextIPRange(); }; return IPAddress("0.0.0.0").asStruct();};void NetScanner::resetIPRange(){ tmpIPRange=ipRangeStr;};MyString NetScanner::getNextIPRange(){ if (tmpIPRange.contains(';')<1) return ""; int cp(tmpIPRange.find(';')); MyString tmp(tmpIPRange.left(cp)); tmpIPRange=tmpIPRange.mid(cp+1); return tmp;};char* NetScanner::ip2Name(struct in_addr ip){ struct hostent *hostname=0; // Set the hostname of node if ( ( hostname = gethostbyaddr( (char *) &ip.s_addr, 4, AF_INET ) ) == 0 ) { getDebug() << "TCPNode::TCPNode->gethostbyname* error" << endl; return inet_ntoa( ip ); } getDebug()<<"NetScanner::ip2name -"<<hostname->h_name<<endl; return hostname->h_name;};void NetScanner::nmblookupScan(SimpleList<Node>* newList){ getDebug()<<"NetScanner::nmblookupScan()"<<endl; //newList->clear(); FILE * nmblookupFile=popen("nmblookup \"*\"","r"); //no success if (nmblookupFile==0) { getDebug()<<"NetScanner::nmblookupScan(): could not start nmblookup"<<endl; return; }; MyString dummy(""); char *receiveBuffer=0; int bufferSize(0); int nmblookupFd=fileno(nmblookupFile); struct timeval tv; fd_set fds; int done(0); int timeOuts(0); char *tmpBuf=new char[16*1024]; do
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -