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

📄 opendhcpd.cpp

📁 Linux下的DHCP服务器程序源代码, 实现DHCP Server端协议
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	                    sizeof(req->target));	//printf("Here3\n");	//errno = WSAGetLastError();	if (errno || req->bytes <= 0)	{		if (cfig.logLevel)		{			if (cfig.replication == 1)				sprintf(logBuff, "Error %u Sending DHCP Update to Secondary Server %s", errno, IP2String(tempbuff, req->target.sin_addr.s_addr));			else				sprintf(logBuff, "Error %u Sending DHCP Update to Primary Server %s", errno, IP2String(tempbuff, req->target.sin_addr.s_addr));			logMess(logBuff, 2);		}		return 0;	}	else if (cfig.logLevel == 2)	{		if (cfig.replication == 1)			sprintf(logBuff, "DHCP Update for host %s sent to Secondary Server %s", req->hostname, IP2String(tempbuff, req->target.sin_addr.s_addr));		else			sprintf(logBuff, "DHCP Update for host %s sent to Primary Server %s", req->hostname, IP2String(tempbuff, req->target.sin_addr.s_addr));		logMess(logBuff, 2);	}	return req->dhcpp.header.bp_yiaddr;}void recvRepl(data1 *req){	//printf("Here 3\n");	DWORD ip = req->dhcpp.header.bp_yiaddr ? req->dhcpp.header.bp_yiaddr : req->dhcpp.header.bp_ciaddr;	if (!ip || !req->lease)		return;	else if (cfig.replication == 1 && req->addr.sin_addr.s_addr != cfig.zoneServers[1])		return;	else if (cfig.replication == 2 && req->addr.sin_addr.s_addr != cfig.zoneServers[0])		return;	//printf("Here 3\n");	char rInd = getRangeInd(ip);	req->dhcpEntry = dhcpCache[toBase64(req->dhcpp.header.bp_chaddr, req->dhcpp.header.bp_hlen)];	if (!req->dhcpEntry && rInd >= 0)	{		req->dhcpEntry = (data7*)calloc(1, sizeof(data7));		if (!req->dhcpEntry)		{			sprintf(logBuff, "Memory Allocation Error");			logMess(logBuff, 1);			return;		}		req->dhcpEntry->mapname = cloneString(toBase64(req->dhcpp.header.bp_chaddr, req->dhcpp.header.bp_hlen));		if (!req->dhcpEntry->mapname)		{			sprintf(logBuff, "Memory Allocation Error");			free(req->dhcpEntry);			logMess(logBuff, 1);			return;		}		//req->dhcpEntry->dataType = DHCP;		dhcpCache[req->dhcpEntry->mapname] = req->dhcpEntry;	}	if (req->dhcpEntry)	{		req->dhcpEntry->ip = ip;		req->dhcpEntry->rangeInd = rInd;		if (req->dhcpEntry->hostname && req->hostname[0] && strcasecmp(req->dhcpEntry->hostname, req->hostname))		{			free(req->dhcpEntry->hostname);			req->dhcpEntry->hostname = 0;		}		if (req->hostname[0] && !req->dhcpEntry->hostname)			req->dhcpEntry->hostname = cloneString(req->hostname);		if (updateDHCP(req))		{			if (cfig.logLevel == 2)			{				if (cfig.replication == 1)					sprintf(logBuff, "DHCP Update received for %s from Secondary Server %s", req->hostname, IP2String(tempbuff, req->addr.sin_addr.s_addr));				else					sprintf(logBuff, "DHCP Update received for %s from Primary Server %s", req->hostname, IP2String(tempbuff, req->addr.sin_addr.s_addr));				logMess(logBuff, 2);			}		}	}}char getRangeInd(DWORD ip){	if (ip)	{		DWORD iip = htonl(ip);		for (char k = 0; k < 32 && cfig.dhcpRanges[k].rangeStart; k++)			if (iip >= cfig.dhcpRanges[k].rangeStart && iip <= cfig.dhcpRanges[k].rangeEnd)				return k;	}	return -1;}int getIndex(char rangeInd, DWORD ip){	if (ip && rangeInd >= 0 && rangeInd < 32)	{		DWORD iip = htonl(ip);		if (iip >= cfig.dhcpRanges[rangeInd].rangeStart && iip <= cfig.dhcpRanges[rangeInd].rangeEnd)			return (iip - cfig.dhcpRanges[rangeInd].rangeStart);	}	return -1;}BYTE *loadOptions(char *iniStr, char *sectionName, DWORD *ip, DWORD *mask, char *MacRange){	*ip = 0;	*mask = 0;	*MacRange = 0;	BYTE *lastOptionIndex = NULL;	WORD buffsize = sizeof(dhcp_packet) - sizeof(dhcp_header);	BYTE *options = (BYTE*)calloc(1, buffsize);	if (!options)	{		sprintf(logBuff, "DHCP Option Load, Memory Allocation Error");		logMess(logBuff, 1);		return NULL;	}	BYTE *dp = options;	*dp = 0;	dp++;	char *iniStrPtr = myGetToken(iniStr, 0);	for (; iniStrPtr[0]; iniStrPtr = myGetToken(iniStrPtr, 1))	{		char name[512];		char value[512];		mySplit(name, value, iniStrPtr, '=');		if (name[0])		{			if (!value[0])				strcpy(value, "none-none*");			if (!strcasecmp(name, "DHCP_Range"))			{				if (!strcasecmp(sectionName, "DHCP_RANGE"))				{					addDHCPRange(value);				}				else				{					sprintf(logBuff, "Warning: section [%s] option %s not allowed in this section, option ignored", sectionName, iniStrPtr);					logMess(logBuff, 1);				}				continue;			}			else if (!strcasecmp(name, "Subnet_Mask"))			{				if (!isIP(value) || !checkMask(inet_addr(value)))				{					sprintf(logBuff, "Warning: section [%s] Invalid %s %s, option ignored", sectionName, name, value);					logMess(logBuff, 1);				}				else					(*mask) = inet_addr(value);			}			else if (!strcasecmp(name, "MAC_Range"))			{				if (!strcasecmp(sectionName, "DHCP_RANGE"))					strcpy(MacRange, value);				else				{					sprintf(logBuff, "Warning: section [%s] option %s not allowed in this section, option ignored", sectionName, iniStrPtr);					logMess(logBuff, 1);				}				continue;			}			else if (!strcasecmp(name, "Hostname") && (!strcasecmp(sectionName, "GLOBAL_OPTIONS") || !strcasecmp(sectionName, "DHCP_RANGE")))			{				sprintf(logBuff, "Warning: section [%s] option %s not allowed in this section, option ignored", sectionName, iniStrPtr);				logMess(logBuff, 1);				continue;			}			else if (!strcasecmp(name, "Hostname") && strchr(name, '.'))			{				sprintf(logBuff, "Warning: section [%s] option %s should be bare name", sectionName, name);				logMess(logBuff, 1);				continue;			}			else if (!strcasecmp(name, "IP_Addr"))			{				if (!strcasecmp(sectionName, "GLOBAL_OPTIONS") || !strcasecmp(sectionName, "DHCP_RANGE"))				{					sprintf(logBuff, "Warning: section [%s] option %s not allowed in this section, option ignored", sectionName, iniStrPtr);					logMess(logBuff, 1);				}				else if (!isIP(value))				{					sprintf(logBuff, "Warning: section [%s] option %s Invalid IP Addr %s option ignored", sectionName, value);					logMess(logBuff, 1);				}				else					*ip = my_inet_addr(value);				continue;			}			else			{				WORD opTag = atoi(name);				if (opTag && opTag < UCHAR_MAX)				{					BYTE hoption[256];					BYTE hopsize = sizeof(hoption);					char *errorPos = getHexValue(hoption, value, &hopsize);					if (errorPos)					{						sprintf(logBuff, "Warning: section [%s] option %s position %u, Invalid char %c, option ignored", sectionName, name, ((DWORD)errorPos - (DWORD)value) + 1, *(errorPos));						logMess(logBuff, 1);						continue;					}					else if (buffsize > (hopsize + 2))					{						*dp = opTag;						dp++;						*dp = hopsize;						dp++;						memcpy(dp, hoption, hopsize);						dp += hopsize;						buffsize -= (hopsize + 2);					}					else					{						sprintf(logBuff, "Warning: section [%s] option %s no more space for options", sectionName, name);						logMess(logBuff, 1);					}					continue;				}			}		}		else		{			sprintf(logBuff, "Warning: section [%s] option %s, Missing option name, entry %s ignored", sectionName, iniStrPtr);			logMess(logBuff, 1);			continue;		}		int op_index;		BYTE n = sizeof(opdata) / sizeof(data4);		for (op_index = 0; op_index < n; op_index++)			if (!strcasecmp(name, opdata[op_index].op_name))				break;		if (op_index >= n)		{			sprintf(logBuff, "Warning: section [%s] Invalid option %s, ignored", sectionName, name);			logMess(logBuff, 1);			continue;		}		if (!strcasecmp(value, "none-none*"))		{			if (buffsize > 2)			{				lastOptionIndex = 0;				*dp = opdata[op_index].op_code;				dp++;				*dp = 0;				dp++;				buffsize -= 2;			}			else			{				sprintf(logBuff, "Warning: section [%s] option %s no more space for options", sectionName, name);				logMess(logBuff, 1);			}			continue;		}		if (opdata[op_index].op_size < UCHAR_MAX && opdata[op_index].op_size != 0 && (*value < '0' || *value > '9'))		{			sprintf(logBuff, "Warning: section [%s] option %s Numeric Value Required, option ignored", sectionName, name);			logMess(logBuff, 1);			continue;		}		if (opdata[op_index].op_size == 0)		{			if (isIP(value) && my_inet_addr(value) != INADDR_NONE)			{				DWORD inetAddr = my_inet_addr(value);				if (buffsize > 6)				{					if (lastOptionIndex && *lastOptionIndex == opdata[op_index].op_code)					{						*(lastOptionIndex + 1) += 4;					}					else					{						lastOptionIndex = dp;						*dp = opdata[op_index].op_code;						dp++;						*dp = 4;						dp++;						buffsize -= 2;					}					dp += pIP(dp, inetAddr);					buffsize -= 4;				}				else				{					sprintf(logBuff, "Warning: section [%s] option %s no more space for options", sectionName, name);					logMess(logBuff, 1);				}			}			else			{				sprintf(logBuff, "Warning: section [%s] option %s Value should be 4 octates, option ignored", sectionName, name);				logMess(logBuff, 1);			}		}		else if (opdata[op_index].op_size == 4)		{			if (buffsize > 6)			{				lastOptionIndex = 0;				DWORD j = atol(value);				if (opdata[op_index].op_code == DHCP_OPTION_IPADDRLEASE)				{					if (j == 0)						j = cfig.lease;					else if (j > cfig.lease)					{						sprintf(logBuff, "Warning: section [%s] option %s value should be less then %u, ignored", sectionName, name, cfig.lease);						logMess(logBuff, 1);						j = cfig.lease;					}				}				*dp = opdata[op_index].op_code;				dp++;				*dp = 4;				dp++;				dp += pULong(dp, j);				buffsize -= 6;				//printf("%s=%u=%u\n",opdata[op_index].op_name,opdata[op_index].op_size,htonl(j));			}			else			{				sprintf(logBuff, "Warning: section [%s] option %s no more space for options", sectionName, name);				logMess(logBuff, 1);			}		}		else if (opdata[op_index].op_size == 2)		{			long j = atol(value);			if (j > USHRT_MAX)			{				sprintf(logBuff, "Warning: section [%s] option %s Value should be between 0 & %u, option ignored", sectionName, name, USHRT_MAX);				logMess(logBuff, 1);			}			else if (buffsize > 4)			{				if (lastOptionIndex && *lastOptionIndex == opdata[op_index].op_code)					*(lastOptionIndex + 1) += 2;				else				{					lastOptionIndex = dp;					*dp = opdata[op_index].op_code;					dp++;					*dp = 2;					dp++;					buffsize -= 2;				}				dp += pUShort(dp, j);				buffsize -= 2;			}			else			{				sprintf(logBuff, "Warning: section [%s] option %s no more space for options", sectionName, name);				logMess(logBuff, 1);			}		}		else if (opdata[op_index].op_size == 1)		{			long j = atol(value);			if (j > UCHAR_MAX)			{				sprintf(logBuff, "Warning: section [%s] option %s Value should be between 0 & %u, option ignored", sectionName, name, UCHAR_MAX);				logMess(logBuff, 1);			}			else if (buffsize > 3)			{				if (lastOptionIndex && *lastOptionIndex == opdata[op_index].op_code)				{					*(lastOptionIndex + 1) += 1;				}				else				{					lastOptionIndex = dp;					*dp = opdata[op_index].op_code;					dp++;					*dp = 1;					dp++;					buffsize -= 2;				}				*dp = j;				dp++;				buffsize -= 1;			}			else			{				sprintf(logBuff, "Warning: section [%s] option %s no more space for options", sectionName, name);				logMess(logBuff, 1);			}		}		else		{			lastOptionIndex = NULL;			if (strlen(value) >= UCHAR_MAX)			{				sprintf(logBuff, "Warning: section [%s] option %s value too big, option ignored", sectionName, name);				logMess(logBuff, 1);			}			else if (buffsize > strlen(value) + 2)			{				*dp = opdata[op_index].op_code;				dp++;				*dp = strlen(value);				dp++;				memcpy(dp, value, strlen(value));				dp += (strlen(value));				buffsize -= (strlen(value) + 2);			}			else			{				sprintf(logBuff, "Warning: section [%s] option %s no more space for options", sectionName, name);				logMess(logBuff, 1);			}		}	}	*dp = DHCP_OPTION_END;	dp++;	WORD optionSize = ((DWORD)dp - (DWORD)options);	if (optionSize > 2)	{		dp = (BYTE*)calloc(1, optionSize);		if (!dp)		{			sprintf(logBuff, "DHCP Option Load, Memory Allocation Error");			logMess(logBuff, 1);			free(options);			return NULL;		}		memcpy(dp, options, optionSize);		free(options);		return dp;	}	free(options);	return NULL;}char* myGetToken(char* buff, BYTE index){	while (*buff)	{		if (index)			index--;		else			break;		buff += strlen(buff) + 1;	}	return buff;}WORD myTokenize(char *target, char *source, char *sep, bool whiteSep){	bool found = true;	char *dp = target;	WORD kount = 0;	while (*source)	{		if (sep && sep[0] && strchr(sep, (*source)))		{			found = true;			source++;			continue;		}		else if (whiteSep && *source <= 32)		{			found = true;			source++;			continue;		}		if (found)

⌨️ 快捷键说明

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