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

📄 netstuff.c

📁 IPcop软路由防火墙 FREE 开源源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* SmoothWall libsmooth. * * This program is distributed under the terms of the GNU General Public * Licence.  See the file COPYING for details. * * (c) Lawrence Manning, 2001 * Contains network library functions. *  * $Id: netstuff.c,v 1.19.2.7 2004/11/05 23:40:17 alanh Exp $ *  */#include "libsmooth.h"#include <signal.h>extern FILE *flog;extern char *mylog;extern char **ctr;newtComponent networkform;newtComponent addressentry;newtComponent netmaskentry;newtComponent statictyperadio;newtComponent dhcptyperadio;newtComponent pppoetyperadio;newtComponent pptptyperadio;newtComponent dhcphostnameentry;/* acceptable character filter for IP and netmaks entry boxes */static int ip_input_filter(newtComponent entry, void * data, int ch, int cursor){	if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '\r' || ch >= NEWT_KEY_EXTRA_BASE)		return ch;	return 0;}/* This is a groovie dialog for showing network info.  Takes a keyvalue list, * a colour and a dhcp flag.  Shows the current settings, and rewrites them * if necessary.  DHCP flag sets wether to show the dhcp checkbox. */int changeaddress(struct keyvalue *kv, char *colour, int typeflag,	char *defaultdhcphostname){	char *addressresult;	char *netmaskresult;	char *dhcphostnameresult;	struct newtExitStruct es;	newtComponent header;	newtComponent addresslabel;	newtComponent netmasklabel;	newtComponent dhcphostnamelabel;	newtComponent ok, cancel;		char message[1000];	char temp[STRING_SIZE];	char addressfield[STRING_SIZE];	char netmaskfield[STRING_SIZE];	char typefield[STRING_SIZE];	char dhcphostnamefield[STRING_SIZE];	int error;	int result = 0;	char type[STRING_SIZE];	int startstatictype = 0;	int startdhcptype = 0;	int startpppoetype = 0;	int startpptptype = 0;			/* Build some key strings. */	sprintf(addressfield, "%s_ADDRESS", colour);	sprintf(netmaskfield, "%s_NETMASK", colour);	sprintf(typefield, "%s_TYPE", colour);	sprintf(dhcphostnamefield, "%s_DHCP_HOSTNAME", colour);			sprintf(message, ctr[TR_INTERFACE], colour);	newtCenteredWindow(44, (typeflag ? 18 : 12), message);		networkform = newtForm(NULL, NULL, 0);	sprintf(message, ctr[TR_ENTER_THE_IP_ADDRESS_INFORMATION], colour);	header = newtTextboxReflowed(1, 1, message, 42, 0, 0, 0);	newtFormAddComponent(networkform, header);	/* See if we need a dhcp checkbox.  If we do, then we shift the contents	 * of the window down two rows to make room. */	if (typeflag)	{		strcpy(temp, "STATIC"); findkey(kv, typefield, temp);		if (strcmp(temp, "STATIC") == 0) startstatictype = 1;		if (strcmp(temp, "DHCP") == 0) startdhcptype = 1;		if (strcmp(temp, "PPPOE") == 0) startpppoetype = 1;		if (strcmp(temp, "PPTP") == 0) startpptptype = 1;		statictyperadio = newtRadiobutton(2, 4, ctr[TR_STATIC], startstatictype, NULL);		dhcptyperadio = newtRadiobutton(2, 5, "DHCP", startdhcptype, statictyperadio);		pppoetyperadio = newtRadiobutton(2, 6, "PPPOE", startpppoetype, dhcptyperadio);		pptptyperadio = newtRadiobutton(2, 7, "PPTP", startpptptype, pppoetyperadio);		newtFormAddComponents(networkform, statictyperadio, dhcptyperadio, 			pppoetyperadio, pptptyperadio, NULL);		newtComponentAddCallback(statictyperadio, networkdialogcallbacktype, NULL);		newtComponentAddCallback(dhcptyperadio, networkdialogcallbacktype, NULL);		newtComponentAddCallback(pppoetyperadio, networkdialogcallbacktype, NULL);		newtComponentAddCallback(pptptyperadio, networkdialogcallbacktype, NULL);		dhcphostnamelabel = newtTextbox(2, 9, 18, 1, 0);		newtTextboxSetText(dhcphostnamelabel, ctr[TR_DHCP_HOSTNAME]);		strcpy(temp, defaultdhcphostname);		findkey(kv, dhcphostnamefield, temp);		dhcphostnameentry = newtEntry(20, 9, temp, 20, &dhcphostnameresult, 0);		newtFormAddComponent(networkform, dhcphostnamelabel);				newtFormAddComponent(networkform, dhcphostnameentry);			if (startdhcptype == 0)			newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);	}	/* Address */	addresslabel = newtTextbox(2, (typeflag ? 11 : 4) + 0, 18, 1, 0);	newtTextboxSetText(addresslabel, ctr[TR_IP_ADDRESS_PROMPT]);	strcpy(temp, "");	findkey(kv, addressfield, temp);	addressentry = newtEntry(20, (typeflag ? 11 : 4) + 0, temp, 20, &addressresult, 0);	newtEntrySetFilter(addressentry, ip_input_filter, NULL);	if (typeflag == 1 && startstatictype == 0 && startpptptype == 0 )		newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);	newtFormAddComponent(networkform, addresslabel);	newtFormAddComponent(networkform, addressentry);		/* Netmask */	netmasklabel = newtTextbox(2, (typeflag ? 11 : 4) + 1, 18, 1, 0);	newtTextboxSetText(netmasklabel, ctr[TR_NETMASK_PROMPT]);	strcpy(temp, "255.255.255.0"); findkey(kv, netmaskfield, temp);	netmaskentry = newtEntry(20, (typeflag ? 11 : 4) + 1, temp, 20, &netmaskresult, 0);	newtEntrySetFilter(netmaskentry, ip_input_filter, NULL);	if (typeflag == 1 && startstatictype == 0 && startpptptype == 0 ) 		newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);	newtFormAddComponent(networkform, netmasklabel);	newtFormAddComponent(networkform, netmaskentry);	/* Buttons. */	ok = newtButton(8, (typeflag ? 14 : 7), ctr[TR_OK]);	cancel = newtButton(26, (typeflag ? 14 : 7), ctr[TR_CANCEL]);	newtFormAddComponents(networkform, ok, cancel, NULL);	newtRefresh();	newtDrawForm(networkform);	do	{		error = 0;		newtFormRun(networkform, &es);			if (es.u.co == ok)		{			/* OK was pressed; verify the contents of each entry. */			strcpy(message, ctr[TR_INVALID_FIELDS]);						strcpy(type, "STATIC");			if (typeflag)				gettype(type);			if (strcmp(type, "STATIC") == 0 || strcmp(type, "PPTP") == 0 )			{						if (inet_addr(addressresult) == INADDR_NONE)				{					strcat(message, ctr[TR_IP_ADDRESS_CR]);					error = 1;				}				if (inet_addr(netmaskresult) == INADDR_NONE)				{					strcat(message, ctr[TR_NETWORK_MASK_CR]);					error = 1;				}			}			if (strcmp(type, "DHCP") == 0)			{				if (!strlen(dhcphostnameresult))				{					strcat(message, ctr[TR_DHCP_HOSTNAME_CR]);					error = 1;				}			}			if (error)				errorbox(message);			else			{				/* No errors!  Set new values, depending on dhcp flag etc. */				if (typeflag)				{					replacekeyvalue(kv, dhcphostnamefield, dhcphostnameresult);					if (strcmp(type, "STATIC") != 0 && strcmp(type, "PPTP") != 0)					{						replacekeyvalue(kv, addressfield, "0.0.0.0");						replacekeyvalue(kv, netmaskfield, "0.0.0.0");					}					else					{						replacekeyvalue(kv, addressfield, addressresult);						replacekeyvalue(kv, netmaskfield, netmaskresult);					}					replacekeyvalue(kv, typefield, type);									}				else				{					replacekeyvalue(kv, addressfield, addressresult);					replacekeyvalue(kv, netmaskfield, netmaskresult);				}								setnetaddress(kv, colour);				result = 1;			}		}				}	while (error);	newtFormDestroy(networkform);	newtPopWindow();			return result;}/* for pppoe: return string thats type STATIC, DHCP or PPPOE */int gettype(char *type){	newtComponent selected = newtRadioGetCurrent(statictyperadio);		if (selected == statictyperadio)		strcpy(type, "STATIC");	else if (selected == dhcptyperadio)		strcpy(type, "DHCP");	else if (selected == pppoetyperadio)		strcpy(type, "PPPOE");	else if (selected == pptptyperadio)		strcpy(type, "PPTP");	else		strcpy(type, "ERROR");		return 0;}/* 0.9.9: calculates broadcast too. */int setnetaddress(struct keyvalue *kv, char *colour){	char addressfield[STRING_SIZE];	char netaddressfield[STRING_SIZE];			char netmaskfield[STRING_SIZE];	char broadcastfield[STRING_SIZE];	char address[STRING_SIZE];	char netmask[STRING_SIZE];	unsigned long int intaddress;	unsigned long int intnetaddress;	unsigned long int intnetmask;	unsigned long int intbroadcast;	struct in_addr temp;	char *netaddress;	char *broadcast;			/* Build some key strings. */	sprintf(addressfield, "%s_ADDRESS", colour);	sprintf(netaddressfield, "%s_NETADDRESS", colour);	sprintf(netmaskfield, "%s_NETMASK", colour);	sprintf(broadcastfield, "%s_BROADCAST", colour);	strcpy(address, ""); findkey(kv, addressfield, address);		strcpy(netmask, ""); findkey(kv, netmaskfield, netmask);			/* Calculate netaddress. Messy.. */	intaddress = inet_addr(address);	intnetmask = inet_addr(netmask);		intnetaddress = intaddress & intnetmask;	temp.s_addr = intnetaddress;		netaddress = inet_ntoa(temp);		replacekeyvalue(kv, netaddressfield, netaddress);		intbroadcast = intnetaddress | ~intnetmask;	temp.s_addr = intbroadcast;	broadcast = inet_ntoa(temp);			replacekeyvalue(kv, broadcastfield, broadcast);		return 1;}	/* Called when dhcp flag is toggled.  Toggle disabled state of other 3 * controls. */void networkdialogcallbacktype(newtComponent cm, void *data){	char type[STRING_SIZE];		gettype(type);	if (strcmp(type, "STATIC") != 0  && strcmp(type, "PPTP") != 0 )	{		newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);		newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);	}	else	{		newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);		newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);	}	if (strcmp(type, "DHCP") == 0)		newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);	else		newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);					newtRefresh();	newtDrawForm(networkform);	}int interfacecheck(struct keyvalue *kv, char *colour){	char temp[STRING_SIZE];	char colourfields[NETCHANGE_TOTAL][STRING_SIZE];	int c;	sprintf(colourfields[ADDRESS], "%s_ADDRESS", colour);	sprintf(colourfields[NETADDRESS], "%s_NETADDRESS", colour);	sprintf(colourfields[NETMASK], "%s_NETMASK", colour);	for (c = 0; c < 3; c++)	{		strcpy(temp, ""); findkey(kv, colourfields[c], temp);

⌨️ 快捷键说明

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