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

📄 test.c

📁 Linux下DHCP Client的实现。
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef WIN32
#include <winsock2.h>
#include "stdint.h"
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <netinet/in.h>
#endif
#include "DhcpcMsg.h"

#define SERVPORT 9000
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

#define MAX_INTERFACE_NAMELEN 16

typedef unsigned long ipaddr;
typedef uint32_t bool_t;
typedef struct DHCPConfigStruct
{
#define MAX_DNS_VALUE 2
#define MAX_GATEWAY_VALUE 2
	ipaddr ifAddress;
	ipaddr ifNetmask;
	ipaddr ifDns[MAX_DNS_VALUE];
	ipaddr ifGateway[MAX_GATEWAY_VALUE];
}DHCPConfigStruct_t;

typedef struct DHCPGetParma
{
	bool_t	ulEnable;		//0: disable  1: enable
	uint8_t	ulNowStates;	//RENEWING... etc
	DHCPConfigStruct_t	conf;
}DHCPGetParma_t;

typedef struct sndDHCPConfigMsg
{
#define DHCP_DEFAULT_CONFIG 1
#define DHCP_ENABLE			2
#define DHCP_GET_STATUS		3
	unsigned char type;
	unsigned char interfacename[MAX_INTERFACE_NAMELEN];
	union
	{
		DHCPConfigStruct_t	conf;
		DHCPGetParma_t		Status;
	};
}sndDHCPConfigMsg_t;

#ifdef WIN32
char* winsock_startup (void)
{
	WORD VersionRequested;
	WSADATA stWSAData;
	int i;
	static char errmsg[100];
	
	VersionRequested = MAKEWORD(1,1);
	i = WSAStartup(VersionRequested, &stWSAData); 
	if (i != 0)
	{
		if (i == WSAVERNOTSUPPORTED)
			sprintf(errmsg,"Unable to init. socket lib, does not support 1.1");
		else
		{
			sprintf(errmsg,"Socket Startup error %d", i);
		}
		return(errmsg);
	}
	return(NULL);
}
#endif

int main(int argc, char *argv[])
{
	int communfd;
	int i = 0;
	struct sockaddr_in servaddr;
	char buf[1024];
	char sDeleteIfName[16] = {0};
	//char *pBuf = buf;
	//unsigned long ipTmp;
	static DhcpcMsgConfigStatus_t conf;
	DhcpcMsgConfigStatus_t		*pMsg;
	DhcpcMsgInterfaceList_t 	*ListMsg = NULL;
	//char szStatus[20] = {0};

	const char *cszStatus[] = 
	{
		"DHCP STOP",
		"DHCP LINKING",
		"DHCP BOUND",
		"DHCP INTERFACE NOT EXIST"
	};

	memset(&conf, 0, sizeof(conf));	
	conf.ulMsgType = DHCPC_MSG_INTERFACE_CONFIG;
	strcpy(conf.szIfName, argv[1]);

	conf.IPSetting.ulIP= inet_addr("192.168.12.233");
	conf.IPSetting.ulDNS[0] = inet_addr("10.27.1.30");
	conf.IPSetting.ulDNS[1] = inet_addr("202.102.224.68");
	conf.IPSetting.ulGateway = inet_addr("192.168.12.254");
	conf.IPSetting.ulNetmask= inet_addr("255.255.255.0");

	if (argc!=3)
	{
		printf("parma number incorrect!\n");
		return 0;
	}
#ifdef WIN32
	//init winsock
	winsock_startup();
#endif
	//tcp communfd = socket(AF_INET, SOCK_STREAM, 0);
	communfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (-1 == communfd)
	{
		printf("socket error!\n");
		return 0;
	}
#ifdef WIN32
	memset(&servaddr, 0, sizeof(servaddr));
#else
	bzero(&servaddr, sizeof(servaddr));	
#endif

	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERVPORT);
#ifdef WIN32
	ipTmp = inet_addr(argv[2]);
	memcpy(&servaddr.sin_addr, (void *)&ipTmp, sizeof(servaddr.sin_addr));
#else
	inet_pton(AF_INET, argv[2], &servaddr.sin_addr);
#endif
	
	//tcp
	/*if (connect(communfd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr)) == -1)
	{
		printf("connect error!\n");
		return 0;
	}*/

	while (1)
	{
		int iNum, cmd, n;
		memset(buf, 0, sizeof(buf));
		iNum = printf("What do you want to do?\n\
			1: Default DHCP Client config.\n\
			2: Enable DHCP Client function.\n\
			3: Get DHCP Status\n\
			4: Get DHCP Client Support Interface Name\n\
			5: Remove Interface\n\
			9: exit."
			);
		printf("\nPlease select:");
		scanf("%d", &cmd);
		
		switch (cmd) /* 1: Default interface config   2: ... */
		{
		case 1:
			conf.ulMsgType = DHCPC_MSG_INTERFACE_CONFIG;
			memset(conf.szIfName, 0, sizeof(conf.szIfName));
			strcpy(conf.szIfName, argv[1]);
			conf.ulConfigStatus = DHCPC_CONFIG_DISABLE;
			memcpy(buf, (char *)&conf, sizeof(conf));
			
			//tcp
			//send(communfd, buf, sizeof(conf), 0);
			sendto(communfd, buf, sizeof(conf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
			/*n = recvfrom(communfd, buf, 1024, 0, NULL, NULL);
			buf[n] = 0;
			pMsg = (DhcpcMsgConfigStatus_t	 *)buf;
			if (pMsg->ulConfigStatus == 0x80000000)
			{	
				printf("\nDHCP Status = %s value = %x\n", "Interface Missed", pMsg->ulConfigStatus);
			}
			printf("Now Status:%s\n", pMsg->ulConfigStatus);*/
			break;
		case 2:
			conf.ulMsgType = DHCPC_MSG_INTERFACE_CONFIG;
			memset(conf.szIfName, 0, sizeof(conf.szIfName));
			strcpy(conf.szIfName, argv[1]);
			conf.ulConfigStatus = DHCPC_CONFIG_ENABLE;
			memcpy(buf, (char *)&conf, sizeof(conf));
			printf("Enable DHCP client!\n");
			sendto(communfd, buf, sizeof(conf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
			//whether the interface list is  overflow
			n = recvfrom(communfd, buf, 1024, 0, NULL, NULL);
			buf[n] = 0;
			pMsg = (DhcpcMsgConfigStatus_t	 *)buf;
			if (pMsg->ulConfigStatus == 0x80000000)
			{	
				printf("\nDHCP Status = %s value = %x\n", "Interface Missed", pMsg->ulConfigStatus);
			}

			if (pMsg->ulConfigStatus == DHCPC_STATUS_IF_OVERFLOW)
			{
				printf("\nInterface Overflow\n");
			}
			break;
		case 3:
			conf.ulMsgType = DHCPC_MSG_INTERFACE_STATUS;
			memset(conf.szIfName, 0, sizeof(conf.szIfName));
			strcpy(conf.szIfName, argv[1]);
			memcpy(buf, (char *)&conf, sizeof(conf));
			sendto(communfd, buf, sizeof(conf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
			n = recvfrom(communfd, buf, 1024, 0, NULL, NULL);
			buf[n] = 0;
			pMsg = (DhcpcMsgConfigStatus_t	 *)buf;
			if (pMsg->ulConfigStatus == 0x80000000)
			{	
				printf("\nDHCP Status = %s value = %x\n", "Interface Missed", pMsg->ulConfigStatus);
			}
			else
			{
				printf("\nDHCP Status = %s value = %x\n", 
					cszStatus[pMsg->ulConfigStatus], ntohl(pMsg->ulConfigStatus));
			}
			printf("Received Dns[0] = %d  Dns[1] = %d\n", pMsg->IPSetting.ulDNS[0], 
				pMsg->IPSetting.ulDNS[1]);
			break;
		case 4:			
			conf.ulMsgType = DHCPC_MSG_INTERFACE_LIST;
			memcpy(buf, (char *)&conf, sizeof(conf));
			sendto(communfd, buf, sizeof(conf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
			n = recvfrom(communfd, buf, 1024, 0, NULL, NULL);
			buf[n] = 0;
			ListMsg = (DhcpcMsgInterfaceList_t *) buf;
			printf("DHCP Client Now Support %d Interface\n", ListMsg->ulIfCount);
			for (i=0; i<ListMsg->ulIfCount; i++)
			{
				printf("Supported Interface: %s\n", ListMsg->szIfName[i]);
			}
			break;
		case 5:
			printf("Please input the interface name to remove:\n");
			scanf("%s", &sDeleteIfName);
			strcpy(conf.szIfName, sDeleteIfName);
			conf.ulMsgType = DHCPC_MSG_INTERFACE_CONFIG;
			conf.ulConfigStatus = DHCPC_CONFIG_REMOVE;
			memcpy(buf, (char *)&conf, sizeof(conf));
			sendto(communfd, buf, sizeof(conf), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
			n = recvfrom(communfd, buf, 1024, 0, NULL, NULL);
			buf[n] = 0;
			
			break;
		default:
			break;
		}

		if (9 == cmd)
		{
			return 0;
		}
	}
#ifdef WIN32
	closesocket(communfd);
#else
	close(communfd);
#endif
	return 1;
}

⌨️ 快捷键说明

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