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

📄 license.cpp

📁 跨操作系统的微型中间件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "License.h"
#include <stdio.h>


#ifdef WIN32#include "OSHeaders.h"
#include <stdlib.h>#include <memory.h>#include <string.h>#define LICENSE_FILE "../../config/license.xml"#define IPADDR  "10.70.138.93"
#define MACADDR "00-10-5C-BA-53-C4"
#include "MD5_Check.h"           /*MD5算法的头文件*/#include "DES3_Encrypt.h"        /*DES3算法的?*/#include "netcardinfo.h"         //yangcb add#ifdef LINUX#include<time.h>#include<linux/types.h>#include<unistd.h>#include<sys/time.h>#include <stdarg.h>#endif#ifndef __Win32__ //yangcb add#include <sys/ioctl.h> #include <sys/types.h>#include <sys/socket.h> #include <net/if.h> #include <netinet/in.h> #include <net/if_arp.h> #endif#if __solaris__ #include <sys/sockio.h> #endif #ifndef __Win32__ //yangcb add#include <stropts.h>#include <arpa/inet.h>#include <string.h>#include <unistd.h>#endifCHAR LicenseMng::m_LicenseSerial[256] = {0}; /*License描述串*/INT32 LicenseMng::m_ESN = 0;               /*设备ID号*/CHAR LicenseMng::m_MAC[18] = {0};          /*设备的主MAC地址*/CHAR LicenseMng::m_LicenseType[8] = {0};   /*设备类型:CS(R+M)/CS-M/CS-R/ES-M/ES-R*/ULONG LicenseMng::m_MaxBandwidth = 0;      /*最大流媒体带宽,单位:Mbps*/ULONG LicenseMng::m_MaxConnects = 0;       /*最大并发用户数*/CHAR LicenseMng::m_LicenseKey[33] = {0};   /*LicenseKey*/CHAR LicenseMng::m_StartTime[20]={0};CHAR LicenseMng::m_EndTime[20]={0};BOOL LicenseMng::init_flag = TRUE;			/*对象初始化标识*/ULONG LicenseMng::rfss_online_usernum = 0;ULONG LicenseMng::rfss_user_bandwidth = 0;
XMLDoc* LicenseMng::m_LicenseDoc = NULL;ULONG LicenseMng::m_LiveIn = 0;
NETCARDINFO NetCardInfo[10];int NetCardCount = 0;#define MAXINTERFACES   16 
void TERMDEBUGMSG(int level, const char* msg, ...);
void get_macaddr(){#ifndef __Win32__	int fd, intrface, retn = 0;    struct ifreq buf[MAXINTERFACES];    struct arpreq arp;    struct ifconf ifc;    if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)    {       ifc.ifc_len = sizeof buf;       ifc.ifc_buf = (caddr_t) buf;       if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))       {          intrface = ifc.ifc_len / sizeof (struct ifreq);          //printf("interface num is intrface=%d\n",intrface);          while (intrface-- > 0)          { 			/*Get IP of the net card */             if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))             {                  char ip[ 20 ] = {0 };                 strcpy( ip, inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));                  //printf( "%s\n", ip );                 if ( strcmp( "127.0.0.1", ip ) == 0 )                 	continue;            }             else             {                printf( "ioctl1 device %s", buf[intrface].ifr_name);            	}            			/*Jugde whether the net card status is promisc  */             if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])))             {                if (buf[intrface].ifr_flags & IFF_PROMISC)                {                   //puts ("the interface is PROMISC");                   retn++;                }             }            else             {                printf( "ioctl2 device %s", buf[intrface].ifr_name);             } /* this section can't get Hardware Address,I don't know whether the reason is module driver*/ //          ((struct sockaddr_in*)&arp.arp_pa)->sin_addr=((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr; #if __solaris__             arp.arp_pa.sa_family = AF_INET;             arp.arp_ha.sa_family = AF_INET;             ((struct sockaddr_in*)&arp.arp_pa)->sin_addr.s_addr=            	((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr.s_addr;             if (!(ioctl (fd, SIOCGARP, (char *) &arp)))             {             	sprintf( NetCardInfo[NetCardCount].MacAddr, "%02x:%02x:%02x:%02x:%02x:%02x", 				(unsigned char)arp.arp_ha.sa_data[0],                             (unsigned char)arp.arp_ha.sa_data[1],                             (unsigned char)arp.arp_ha.sa_data[2],                             (unsigned char)arp.arp_ha.sa_data[3],                             (unsigned char)arp.arp_ha.sa_data[4],                             (unsigned char)arp.arp_ha.sa_data[5]); 		NetCardInfo[NetCardCount].MacAddr[ 17 ] = 0;		NetCardCount++;            } #else /*Get HW ADDRESS of the net card */             if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))             {             	sprintf(NetCardInfo[NetCardCount].MacAddr, "%02x:%02x:%02x:%02x:%02x:%02x\n",                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],                                 (unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]); 		NetCardInfo[NetCardCount].MacAddr[ 17 ] = 0;		NetCardCount++;                } #endif             else             {                printf ("ioctl3 device %s\n", buf[intrface].ifr_name);             	}         }       }       else          printf ("ioctl4\n");    }    else       printf ("ioctl5\n");     close (fd); #endif}/*************************************************  Function:     LicenseMng  Description:  LicenseMng的构造函数            *************************************************/LicenseMng::LicenseMng(){	if (m_LicenseDoc == NULL)	{		m_LicenseDoc = new XMLDoc(LICENSE_FILE);		if (m_LicenseDoc != NULL)		{			if (m_LicenseDoc->LoadXMLDoc() == FALSE)				return;		}	}	init_flag = FALSE;}/*************************************************  Function:     ~LicenseMng  Description:  LicenseMng的析构函数            *************************************************/LicenseMng::~LicenseMng(){    if (m_LicenseDoc != NULL)    {    	delete m_LicenseDoc;		m_LicenseDoc = NULL;    }}/*************************************************  Function:     ~is_inited  Description:  LicenseMng是否初始化成功              Calls:        LicenseMng::initialize_license()				LicenseMng::is_legal()  Called By:    ECManager::LoadModules()     Return:       成功返回TRUE;失败返回FALSE*************************************************/BOOL LicenseMng::is_inited(){	if( FALSE == init_flag)	{		return FALSE;	}			/*根据License文件初始化各配置参数*/	if( TRUE != initialize_license() )	{		return FALSE;	}	/*检查license文件合法性*/	if( TRUE != is_legal() )	{		//TERMDEBUGMSG(1,"License非法,请恢复为购买时配置!");		return FALSE;	}		return TRUE;}/*************************************************  Function:     initialize_license  Description:  LicenseMng的初始化函数,读取配置项             Calls:        LicenseConfigFile::GetItemValue()  Called By:    void ECManager::LoadModules()     Return:       成功:TRUE/失败:FALSE   *************************************************/ULONG LicenseMng::initialize_license(){	XMLNode* pRootNode = m_LicenseDoc->GetRootNode("License");	if (pRootNode == NULL)	{		return FALSE;	}	XMLNode* pConfigNode = pRootNode->GetChildren().GetNodeByName("config");	XMLNode* pKeyNode = pRootNode->GetChildren().GetNodeByName("key");	XMLNode* pReadNode = NULL;	if (pConfigNode != NULL)	{		//LicenseSerial		pReadNode = pConfigNode->GetChildren().GetNodeByName("LicenseSerial");		if (pReadNode != NULL)			strcpy(m_LicenseSerial,pReadNode->GetContent().c_str());
		//maxBandwidth		pReadNode = pConfigNode->GetChildren().GetNodeByName("maxBandwidth");		if (pReadNode != NULL)			m_MaxBandwidth = atoi(pReadNode->GetContent().c_str());
		//maxConnects		pReadNode = pConfigNode->GetChildren().GetNodeByName("maxConnects");		if (pReadNode != NULL)		{			m_MaxConnects = atoi(pReadNode->GetContent().c_str());			#ifndef __Win32__				m_MaxConnects=m_MaxConnects<__FD_SETSIZE?m_MaxConnects:__FD_SETSIZE;			#endif		}
		//startTime		pReadNode = pConfigNode->GetChildren().GetNodeByName("startTime");		if (pReadNode != NULL)			strcpy(m_StartTime,pReadNode->GetContent().c_str());
		//endTime		pReadNode = pConfigNode->GetChildren().GetNodeByName("endTime");		if (pReadNode != NULL)			strcpy(m_EndTime,pReadNode->GetContent().c_str());	}	else	{		return FALSE;	}
	if (pKeyNode != NULL)	{		strcpy(m_LicenseKey,pKeyNode->GetContent().c_str());	}	else	{		return FALSE;	}
#ifndef __Win32__	/*Get all network adapter's MAC address*/	//ipaddr_list_link(0, 0);	get_macaddr();#else	// 读取主网卡MAC地址	CHAR master_ip[20];	memcpy(master_ip,IPADDR,20);		if( (FALSE == get_MAC(master_ip)) || (NULL == m_MAC ) )	{		return FALSE;	}#endif	return TRUE;}/*************************************************  Function:     trim  Description:  去掉字符串尾部空格符             Calls:        strlen()  Called By:    LicenseMng::initialize_license()    Input:        CHAR *pstr 字符串  Return:       pstr 字符串   *************************************************/CHAR* LicenseMng::trim(CHAR *pstr){    /*参数有效性检查*/    if (pstr == NULL)    {        return NULL;    }    ULONG nLen = strlen(pstr);    CHAR *pTail = pstr + nLen;    while (*(pTail - 1) == '\x20')	{        pTail--;	}    *pTail = '\0';    return pstr;}BOOL LicenseMng::isTimeEffect(){#ifndef __Win32__	char CurTime[20]={0};	char tmp[20]={0};	struct timeval tv;	struct timezone tz;	int zoneoffsettime=0;	struct tm *tms;	gettimeofday(&tv,&tz);	zoneoffsettime=tz.tz_minuteswest*60;	tv.tv_sec=tv.tv_sec-zoneoffsettime;	tms=gmtime(&tv.tv_sec);	sprintf(CurTime,"%04d-%02d-%02d",1900+tms->tm_year,tms->tm_mon+1,tms->tm_mday);	if(strcmp(m_StartTime,m_EndTime)>0)	{	strcpy(tmp,m_StartTime);	strcpy(m_StartTime,m_EndTime);	strcpy(m_EndTime,tmp);	}	if(strcmp(m_StartTime,CurTime)>0){return FALSE;}	if(strcmp(CurTime,m_EndTime)>0){return FALSE;}#endif	return TRUE;}/*************************************************  Function:     is_legal  Description:  License文件是否合法            Calls:        strcmp()  Called By:    ECManager::LoadModules()  Return:       成功:TRUE/失败:FALSE  *************************************************/BOOL LicenseMng::is_legal(){	/*首先检查参数的合法性*/	if( NULL == m_LicenseSerial )	{		TERMDEBUGMSG(1,"LicenseSerial is NULL!");		return FALSE;	}#ifndef __Win32__	if( NULL == m_StartTime)	  {	    TERMDEBUGMSG(1,"startTime is NULL!");	    return FALSE;	  }	if( NULL == m_EndTime)	  {	    TERMDEBUGMSG(1,"endTime is NULL!");	    return FALSE;	  }#endif	if( 0 == m_ESN )	{		TERMDEBUGMSG(1,"ESN is NULL!");		return FALSE;	}	if( NULL == m_LicenseType )	{		TERMDEBUGMSG(1,"LicenseType is NULL!");		return FALSE;	}	/*if( 0 == m_MaxBandwidth		  && 0 == m_MaxConnects )		{			TERMDEBUGMSG(1,"缺少带宽和并发流限制参数!");			return FALSE;		}*/			/*将ESN、MaxBandwidth、MaxConnects转换成字符串*/	CHAR strESN[4] = {0};	CHAR strMaxBandwidth[8] = {0};	CHAR strMaxConnects[8] = {0};	if( m_ESN > 0 )	{		sprintf(strESN, "%d", m_ESN);	}	else	{		TERMDEBUGMSG(1,"Equipment ID is less than 0 !");		return FALSE;	}	if( m_MaxBandwidth > 0 )	{		sprintf(strMaxBandwidth, "%d", m_MaxBandwidth);	}	if( m_MaxConnects > 0 )	{		sprintf(strMaxConnects, "%d", m_MaxConnects);	}	CHAR strLiveIn[8] = {0};	if (m_LiveIn >= 0)	{		sprintf(strLiveIn,"%d",m_LiveIn);	}

⌨️ 快捷键说明

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