⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 macdhcp.cpp

📁 linux下更改网卡mac地址
💻 CPP
字号:
#include "macdhcp.h"macdhcp::macdhcp(){     ifc.ifc_len = sizeof buf;     ifc.ifc_buf = (caddr_t) buf;     if( !(fd = socket (AF_INET, SOCK_DGRAM, 0)) ) {       qWarning( "DEBUG: can't open socket" );     }}macdhcp::~macdhcp(){  //close(fd);	}int macdhcp::getIfNum(){     if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc)) {       interfaceNum = ifc.ifc_len / sizeof (struct ifreq);       if( interfaceNum > MAXINTERFACES ){		 interfaceNum = MAXINTERFACES;		 printf( "interface is too much, we'll list max %d!\n", MAXINTERFACES );       }       return interfaceNum;     }     return 0;}QString macdhcp::getMacAddress(int ifnumber){     QString tempString;     if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[ifnumber]))){       tempString.sprintf( "%02x-%02x-%02x-%02x-%02x-%02x",			   (unsigned char)buf[ifnumber].ifr_hwaddr.sa_data[0],			   (unsigned char)buf[ifnumber].ifr_hwaddr.sa_data[1],			   (unsigned char)buf[ifnumber].ifr_hwaddr.sa_data[2],			   (unsigned char)buf[ifnumber].ifr_hwaddr.sa_data[3],			   (unsigned char)buf[ifnumber].ifr_hwaddr.sa_data[4],			   (unsigned char)buf[ifnumber].ifr_hwaddr.sa_data[5]);     }	        else{       tempString.sprintf( "192.168.0.120" );	   setIPAddress(ifnumber, tempString);     }     return tempString;}QString macdhcp::getNetwork(){    QString tempString,SN;	unsigned int network=intIp&intNetmask;    SN = tempString.sprintf( "%d.",network&0xff );    SN += tempString.sprintf( "%d.",network>>8&0xff );    SN += tempString.sprintf( "%d.",network>>16&0xff );    SN += tempString.sprintf( "%d",network>>24&0xff );	return SN;}QString macdhcp::getBroadcast(){    QString tempString,SB;	unsigned int broadcast=intIp&intNetmask;    SB = tempString.sprintf( "%d.",broadcast&0xff );    SB += tempString.sprintf( "%d.",broadcast>>8&0xff );    SB += tempString.sprintf( "%d.",broadcast>>16&0xff );    SB += tempString.sprintf( "%d",255 );	return SB;}QString macdhcp::getNetMask(int ifnumber){    QString tempString, netmask;     unsigned int templong;     if (!(ioctl (fd, SIOCGIFNETMASK, (char *) &buf[ifnumber]))){       intNetmask=templong = (unsigned int)((struct sockaddr_in*)(&buf[ifnumber].ifr_netmask))->sin_addr.s_addr;       netmask = tempString.sprintf( "%d.",templong&0xff );       netmask += tempString.sprintf( "%d.",templong>>8&0xff );       netmask += tempString.sprintf( "%d.",templong>>16&0xff );       netmask += tempString.sprintf( "%d",templong>>24&0xff );     }     else{	   intNetmask=0xffffff00;		   netmask.sprintf( "255.255.255.0" );	   system("ifconfig eth0 netmask 255.255.255.0 up");	   setNetMask(ifnumber,netmask);	     }     return netmask;	}QString macdhcp::setNetMask(int ifnumber, QString netmask){     QString tempString;     int tempPosition;     tempString.sprintf( "ifconfig " + getIfName(ifnumber) + " netmask " + netmask );     tempPosition = tempString.find( '\n', 0, TRUE );     if( tempPosition > 0 ){       tempString.remove( tempPosition, 1 );     }     system( tempString );     tempString = getNetMask( ifnumber );     return tempString;}QString macdhcp::getIPAddress(int ifnumber){     QString tempString, ipAddress;     unsigned int templong;     if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[ifnumber]))){       intIp=templong = (unsigned int)((struct sockaddr_in*)(&buf[ifnumber].ifr_addr))->sin_addr.s_addr;       ipAddress = tempString.sprintf( "%d.",templong&0xff );       ipAddress += tempString.sprintf( "%d.",templong>>8&0xff );       ipAddress += tempString.sprintf( "%d.",templong>>16&0xff );       ipAddress += tempString.sprintf( "%d",templong>>24&0xff );     }     else{	   intIp=0xc0a80078;       ipAddress.sprintf( "192.168.0.120" );	   system("ifconfig eth0 192.168.0.120 up");	   setIPAddress(ifnumber,ipAddress);	     }     return ipAddress;}QString macdhcp::getDNS(){     QString tempString;     char temp[100], temp1[20],temp2[20];     FILE *fp;     tempString.sprintf( "NA" );     if( (fp = fopen( "/etc/resolv.conf", "r" ) ) >= 0 ){ 	    fgets( temp, 100, fp ); 	    do{		 sscanf( temp, "%s %s", temp1, temp2 );	 	 if( strcmp( temp1, "nameserver" ) == 0 ){	   		tempString.sprintf( "%s", temp2 );		   break;		 }    	}while( fgets( temp, 100, fp ) != '\0' );     }     fclose( fp );	 if(tempString.find("NA")==-1)     	tempString=checkIP(tempString);	return tempString;}QString macdhcp::getGateway(){	QString tempString,gatewayString;	QHostAddress GWAddress;	bool ok;	char temp[100], temp1[10],temp2[10],temp3[10],temp4[10];	long gateway;	FILE *fp;	tempString.sprintf( "NA" );	if( (fp = fopen( "/proc/net/route", "r" ) ) >= 0 ){	  fgets(temp, 100, fp );	  while( fgets( temp, 100, fp ) != '\0' ){	    memset( temp1, 0, 10 );	    memset( temp2, 0, 10 );	    memset( temp3, 0, 10 );	    memset( temp4, 0, 10 );	    sscanf( temp, "%s %s %s %s", temp1, temp2, temp3, temp4 );	    if( strcmp( temp3, "00000000" ) != 0 && strcmp( temp2, "" ) != 0 ){	      gatewayString.sprintf( "%s", temp3 );	      gateway = gatewayString.toUInt( &ok, 16 );	      gateway = ntohl( gateway );	      GWAddress.setAddress( gateway );	      tempString = GWAddress.toString();			      break;	    }	  }	}	fclose( fp );	if(tempString.find("NA")==-1)		tempString=checkIP(tempString);	return tempString;}QString macdhcp::getIfName(int ifnumber){     QString tempString;     tempString.sprintf("%s\n", buf[ifnumber].ifr_name);     return tempString;}QString macdhcp::setMacAddress(int ifnumber, QString macaddress){     QString tempString;	 macaddress.replace( QRegExp("-"), ":" );     tempString.sprintf( "ifconfig eth0 down \n ifconfig eth0 hw ether "+ macaddress + "\n ifconfig eth0 up\n" );     system( tempString );     tempString = getMacAddress( ifnumber );	 tempString.replace( QRegExp(":"), "-" );     return tempString;}QString macdhcp::setIPAddress(int ifnumber, QString ipaddress){     QString tempString;     int tempPosition;     tempString.sprintf( "ifconfig " + getIfName(ifnumber) + " " + ipaddress );     tempPosition = tempString.find( '\n', 0, TRUE );     if( tempPosition > 0 ){       tempString.remove( tempPosition, 1 );     }     system( tempString );     tempString = getIPAddress( ifnumber );     return tempString;}QString macdhcp::checkIP(QString IP){	int i,prePos=0,curPos;	int IPVal;	bool valideIP=TRUE,OK;	QString oIP;	for(i=0;i<3;i++)	{		curPos=IP.find(".",prePos);		if(curPos!=-1)		{			oIP=IP.mid(prePos,curPos-prePos);			IPVal=oIP.toInt(&OK);			if(IPVal<0||IPVal>255||!OK)			{				valideIP=FALSE;				break;			}			else				prePos=curPos+1;		}		else		{			valideIP=FALSE;			break;		}	}		if(valideIP)		{			curPos=IP.length();			if(curPos>prePos)			{				oIP=IP.mid(prePos,curPos-prePos);				IPVal=oIP.toInt(&OK);				if(IPVal<0||IPVal>255||!OK)					valideIP=FALSE;			}			else				valideIP=FALSE;		}	if(valideIP)		return IP;	else		return QString("192.168.0.120");}QString macdhcp::setDNS(QString dns){     QString tempString;     char temp[100], temp1[20],temp2[20];     FILE *fp;	 if(dns.length()==0)		dns="NA";	 if(dns.find("NA")==-1&&dns.length()!=0)		 dns=checkIP(dns);		 qWarning(dns);     if( (fp = fopen( "/etc/resolv.conf", "w" ) ) >= 0 )	 {			if(dns.find("NA")==-1)		{	     dns = "nameserver "+dns;	     fputs( dns.data(), fp );		}	     fclose(fp);	     tempString.sprintf( "/etc/rc.d/init.d/named restart\n" );	     system( tempString );	}    tempString = getDNS();    if(tempString.find("NA")==-1&&tempString.length()!=0)	   	tempString=checkIP(tempString);	return tempString;}QString macdhcp::setGateway(QString gateway){     QString tempString;     tempString.sprintf( "route del default\n" );     system( tempString );	 if(gateway.find("NA")==-1&&gateway.length()!=0)	 {	 	checkIP(gateway);     	tempString.sprintf( "route add default gw " + gateway + "\n" );     	system( tempString );     	tempString = getGateway();		tempString=checkIP(tempString);	 }	 else	 tempString="NA";     return tempString;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -