Пример #1
0
signed ParseRule (int * argcp, char const ** argvp [], struct MMERule * rule, struct cspec * cspec)

{
	int argc = * argcp;
	char const ** argv = * argvp;
	union
	{
		uint32_t wide;
		uint16_t word;
		uint8_t byte [4];
	}
	temp;
	signed code;
	struct MMEClassifier * classifier = (struct MMEClassifier *) (& rule->CLASSIFIER);
	if ((code = lookup (* argv++, actions, SIZEOF (actions))) == -1)
	{
		assist (* -- argv, CLASSIFIER_ACTION_NAME, actions, SIZEOF (actions));
	}
	rule->MACTION = (uint8_t) (code);
	argc--;
	if ((code = lookup (* argv++, operands, SIZEOF (operands))) == -1)
	{
		assist (* -- argv, CLASSIFIER_OPERAND_NAME, operands, SIZEOF (operands));
	}
	rule->MOPERAND = (uint8_t) (code);
	argc--;
	while ((* argv) && (lookup (* argv, controls, SIZEOF (controls)) == -1))
	{
		if ((code = lookup (* argv++, fields, SIZEOF (fields))) == -1)
		{
			assist (* -- argv, CLASSIFIER_FIELD_NAME, fields, SIZEOF (fields));
		}
		classifier->CR_PID = (uint8_t) (code);
		argc--;
		if ((code = lookup (* argv++, operators, SIZEOF (operators))) == -1)
		{
			assist (* -- argv, CLASSIFIER_OPERATOR_NAME, operators, SIZEOF (operators));
		}
		classifier->CR_OPERAND = (uint8_t) (code);
		argc--;
		if (! argc || ! * argv)
		{
			error (1, ENOTSUP, "I have %s '%s' but no value", CLASSIFIER_OPERATOR_NAME, * -- argv);
		}
		switch (classifier->CR_PID)
		{
		case FIELD_ETH_SA:
		case FIELD_ETH_DA:
			bytespec (* argv++, classifier->CR_VALUE, ETHER_ADDR_LEN);
			break;
		case FIELD_IPV4_SA:
		case FIELD_IPV4_DA:
			ipv4spec (* argv++, classifier->CR_VALUE);
			break;
		case FIELD_IPV6_SA:
		case FIELD_IPV6_DA:
			ipv6spec (* argv++, classifier->CR_VALUE);
			break;
		case FIELD_VLAN_UP:
		case FIELD_IPV6_TC:
		case FIELD_IPV4_TOS:
		case FIELD_IPV4_PROT:
			classifier->CR_VALUE [0] = (uint8_t) (basespec (* argv++, 0, sizeof (classifier->CR_VALUE [0])));
			break;
		case FIELD_VLAN_ID:
		case FIELD_TCP_SP:
		case FIELD_TCP_DP:
		case FIELD_UDP_SP:
		case FIELD_UDP_DP:
		case FIELD_IP_SP:
		case FIELD_IP_DP:
			temp.word = (uint16_t) (basespec (* argv++, 0, sizeof (temp.word)));
			temp.word = htons (temp.word);
			memcpy (classifier->CR_VALUE, & temp, sizeof (temp.word));
			break;
		case FIELD_ETH_TYPE:
			temp.word = (uint16_t) (basespec (* argv++, 0, sizeof (temp.word)));
			temp.word = htons (temp.word);
			memcpy (classifier->CR_VALUE, & temp, sizeof (temp.word));
			break;
		case FIELD_IPV6_FL:
			temp.wide = (uint32_t) (basespec (* argv++, 0, sizeof (temp.wide))) & 0x000FFFFF;
			temp.wide = htonl (temp.wide);
			memcpy (classifier->CR_VALUE, & temp.byte [1], 3);
			break;
		case FIELD_HPAV_MME:
			bytespec (* argv++, classifier->CR_VALUE, sizeof (uint16_t) +  sizeof (uint8_t));
			temp.byte [0] = classifier->CR_VALUE [1];
			classifier->CR_VALUE [1] = classifier->CR_VALUE [2];
			classifier->CR_VALUE [2] = temp.byte [0];
			break;
		case FIELD_TCP_ACK:
			if ((code = lookup (* argv++, states, SIZEOF (states))) == -1)
			{
				assist (* -- argv, CLASSIFIER_STATE_NAME, states, SIZEOF (states));
			}
			memset (classifier->CR_VALUE, 0, sizeof (classifier->CR_VALUE));
			break;
		case FIELD_VLAN_TAG:
			if ((code = lookup (* argv++, states, SIZEOF (states))) == -1)
			{
				assist (* -- argv, CLASSIFIER_STATE_NAME, states, SIZEOF (states));
			}
			memset (classifier->CR_VALUE, 0, sizeof (classifier->CR_VALUE));
			classifier->CR_OPERAND ^= code;
			break;
		default:
			error (1, ENOTSUP, "%s", argv [- 2]);
			break;
		}
		rule->NUM_CLASSIFIERS++;
		classifier++;
		argc--;
	}
	memcpy (classifier, cspec, sizeof (* cspec));
	if ((code = lookup (* argv++, controls, SIZEOF (controls))) == -1)
	{
		assist (* -- argv, CLASSIFIER_CONTROL_NAME, controls, SIZEOF (controls));
	}
	rule->MCONTROL = (uint8_t) (code);
	argc--;
	if ((code = lookup (* argv++, volatilities, SIZEOF (volatilities))) == -1)
	{
		assist (* -- argv, CLASSIFIER_VOLATILITY_NAME, volatilities, SIZEOF (volatilities));
	}
	rule->MVOLATILITY = (uint8_t) (code);
	argc--;
	* argcp = argc;
	* argvp = argv;
	return (0);
}
Пример #2
0
int main (int argc, char const * argv []) 

{
	extern struct channel channel;
	static char const * optv [] = 
	{
		"ei:qv",
		"action priority destination rate ttl operand condition [...] [device] [...]\n\n          where condition is field operator value",
		"CoQos Stream Utility",
		"e\tredirect stderr to stdout",

#if defined (WINPCAP) || defined (LIBPCAP)

		"i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",

#else

		"i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",

#endif

		"q\tquiet mode",
		"v\tverbose mode",
		(char const *) (0)
	};

#include "../plc/plc.c"

	struct connection connection;
	struct MMEClassifier * rule = (struct MMEClassifier *)(&connection.rule.CLASSIFIERS);
	uint16_t * word;
	uint8_t * byte;
	signed code;
	signed c;
	if (getenv (PLCDEVICE)) 
	{

#if defined (WINPCAP) || defined (LIBPCAP)

		channel.ifindex = atoi (getenv (PLCDEVICE));

#else

		channel.ifname = strdup (getenv (PLCDEVICE));

#endif

	}
	optind = 1;
	while ((c = getoptv (argc, argv, optv)) != -1) 
	{
		switch (c) 
		{
		case 'e':
			dup2 (STDOUT_FILENO, STDERR_FILENO);
			break;
		case 'i':

#if defined (WINPCAP) || defined (LIBPCAP)

			channel.ifindex = atoi (optarg);

#else

			channel.ifname = optarg;

#endif

			break;
		case 'q':
			_setbits (channel.flags, CHANNEL_SILENCE);
			_setbits (plc.flags, PLC_SILENCE);
			break;
		case 'v':
			_setbits (channel.flags, CHANNEL_VERBOSE);
			_setbits (plc.flags, PLC_VERBOSE);
			break;
		default:
			break;
		}
	}
	argc -= optind;
	argv += optind;
	memset (&connection, 0, sizeof (connection));
	if ((code = lookup (* argv++, actions, SIZEOF (actions))) == -1) 
	{
		assist (*--argv, CLASSIFIER_ACTION_NAME, actions, SIZEOF (actions));
	}
	connection.cspec.CONN_CAP = (uint8_t)(code);
	argc--;
	if (!argc) 
	{
		error (1, ECANCELED, "Expected Priority: 0-15");
	}
	connection.cspec.CONN_COQOS_PRIO = (uint8_t)(uintspec (* argv++, 0, 15));
	argc--;
	if (!argc) 
	{
		error (1, ECANCELED, "Expected Destination MAC Address");
	}
	if (!hexencode (connection.APP_DA, sizeof (connection.APP_DA), synonym (* argv++, devices, SIZEOF (devices)))) 
	{
		error (1, errno, "Invalid MAC=[%s]", *--argv);
	}
	argc--;
	if (!argc) 
	{
		error (1, ECANCELED, "Expected Data Rate: 10-9000 (kbps)");
	}
	connection.cspec.CONN_RATE = (uint16_t)(uintspec (* argv++, 1, 9000));
	argc--;
	if (!argc) 
	{
		error (1, ECANCELED, "Expected TTL: 10000-2000000 (microseconds)");
	}
	connection.cspec.CONN_TTL = (uint32_t)(uintspec (* argv++, 10000, 2000000));
	argc--;
	if ((code = lookup (* argv++, operands, SIZEOF (operands))) == -1) 
	{
		assist (*--argv, CLASSIFIER_OPERAND_NAME, operands, SIZEOF (operands));
	}
	connection.rule.MOPERAND = (uint8_t)(code);
	argc--;
	while ((* argv) && (lookup (* argv, controls, SIZEOF (controls)) == -1)) 
	{
		if ((code = lookup (* argv++, fields, SIZEOF (fields))) == -1) 
		{
			assist (*--argv, CLASSIFIER_FIELD_NAME, fields, SIZEOF (fields));
		}
		rule->CR_PID = (uint8_t)(code);
		argc--;
		if ((code = lookup (* argv++, operators, SIZEOF (operators))) == -1) 
		{
			assist (*--argv, CLASSIFIER_OPERATOR_NAME, operators, SIZEOF (operators));
		}
		rule->CR_OPERAND = (uint8_t)(code);
		argc--;
		if (!argc || !* argv) 
		{
			error (1, ENOTSUP, "Have %s '%s' without any value", CLASSIFIER_OPERATOR_NAME, *--argv);
		}
		switch (rule->CR_PID) 
		{
		case FIELD_ETH_SA:
		case FIELD_ETH_DA:
			bytespec (* argv++, rule->CR_VALUE, ETHER_ADDR_LEN);
			break;
		case FIELD_IPV4_SA:
		case FIELD_IPV4_DA:
			ipv4spec (* argv++, rule->CR_VALUE);
			break;
		case FIELD_IPV6_SA:
		case FIELD_IPV6_DA:
			ipv6spec (* argv++, rule->CR_VALUE);
			break;
		case FIELD_VLAN_UP:
		case FIELD_IPV4_TOS:
		case FIELD_IPV4_PROT:
			byte = (uint8_t *)(rule->CR_VALUE);
			*byte = (uint8_t)(basespec (* argv++, 0, sizeof (* byte)));
			break;
		case FIELD_VLAN_ID:
		case FIELD_TCP_SP:
		case FIELD_TCP_DP:
		case FIELD_UDP_SP:
		case FIELD_UDP_DP:
		case FIELD_IP_SP:
		case FIELD_IP_DP:
			word = (uint16_t *)(rule->CR_VALUE);
			*word = (uint16_t)(basespec (* argv++, 0, sizeof (* word)));
			*word = htons (*word);
			break;
		case FIELD_ETH_TYPE:
			word = (uint16_t *)(rule->CR_VALUE);
			*word = (uint16_t)(basespec (* argv++, 0, sizeof (* word)));
			*word = htons (*word);
			break;
		case FIELD_HPAV_MME:
			bytespec (* argv++, rule->CR_VALUE, sizeof (uint8_t) + sizeof (uint16_t));
			byte = (uint8_t *)(rule->CR_VALUE);
			HTOBE16 ((uint16_t)(* ++byte));
			break;
		case FIELD_IPV6_TC:
		case FIELD_IPV6_FL:
		case FIELD_TCP_ACK:
		default:
			error (1, ENOTSUP, "Field '%s' (0x%02X)", argv [-2], rule->CR_PID);
			break;
		}
		connection.rule.NUM_CLASSIFIERS++;
		if (connection.rule.NUM_CLASSIFIERS > RULE_MAX_CLASSIFIERS) 
		{
			error (1, ENOTSUP, "More than %d classifiers in rule", RULE_MAX_CLASSIFIERS);
		}
		rule++;
		argc--;
	}
	connection.cspec.CSPEC_VERSION = 0x0001;
	openchannel (&channel);
	if (!(plc.message = malloc (sizeof (* plc.message)))) 
	{
		error (1, errno, PLC_NOMEMORY);
	}
	if (!argc) 
	{
		add_conn (&plc, &connection);
	}
	while ((argc) && (* argv)) 
	{
		if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices)))) 
		{
			error (1, errno, PLC_BAD_MAC, * argv);
		}
		add_conn (&plc, &connection);
		argc--;
		argv++;
	}
	free (plc.message);
	closechannel (&channel);
	exit (0);
}
Пример #3
0
size_t memencode (void * memory, size_t extent, char const * format, char const * string)

{
	if (!strcmp (format, "byte"))
	{
		uint8_t * number = (uint8_t *)(memory);
		if (extent < sizeof (* number))
		{
			error (1, ECANCELED, "Overflow at %s %s", format, string);
		}
		* number = (uint8_t)(basespec (string, 0, sizeof (* number)));
		return (sizeof (* number));
	}
	if (!strcmp (format, "word"))
	{
		uint16_t * number = (uint16_t *)(memory);
		if (extent < sizeof (* number))
		{
			error (1, ECANCELED, "Overflow at %s %s", format, string);
		}
		* number = (uint16_t)(basespec (string, 0, sizeof (* number)));
		* number = HTOLE16 (* number);
		return (sizeof (* number));
	}
	if (!strcmp (format, "long"))
	{
		uint32_t * number = (uint32_t *)(memory);
		if (extent < sizeof (* number))
		{
			error (1, ECANCELED, "Overflow at %s %s", format, string);
		}
		* number = (uint32_t)(basespec (string, 0, sizeof (* number)));
		* number = HTOLE32 (* number);
		return (sizeof (* number));
	}
	if (!strcmp (format, "huge"))
	{
		uint64_t * number = (uint64_t *)(memory);
		if (extent < sizeof (* number))
		{
			error (1, ECANCELED, "Overflow at %s %s", format, string);
		}
		* number = (uint64_t)(basespec (string, 0, sizeof (* number)));
		* number = HTOLE64 (* number);
		return (sizeof (* number));
	}
	if (!strcmp (format, "text"))
	{
		extent = (unsigned)(strlen (string));
		memcpy (memory, string, extent);
		return (extent);
	}
	if (!strcmp (format, "data"))
	{
		extent = (unsigned)(dataspec (string, memory, extent));
		return (extent);
	}
	if (!strcmp (format, "fill"))
	{
		extent = (unsigned)(uintspec (string, 0, extent));
		memset (memory, ~0, extent);
		return (extent);
	}
	if (!strcmp (format, "zero"))
	{
		extent = (unsigned)(uintspec (string, 0, extent));
		memset (memory, 0, extent);
		return (extent);
	}
	if (!strcmp (format, "skip"))
	{
		extent = (unsigned)(uintspec (string, 0, extent));
		return (extent);
	}

#if 1

/*
 *   tr-069 specific fields that don't really belong in the PIB;
 */

	if (!strcmp (format, "adminusername") || !strcmp (format, "adminpassword") || !strcmp (format, "accessusername"))
	{
		return (memstring (memory, extent, format, string, PIB_NAME_LEN + 1));
	}
	if (!strcmp (format, "accesspassword"))
	{
		return (memstring (memory, extent, format, string, PIB_HFID_LEN + 1));
	}
	if (!strcmp (format, "username") || !strcmp (format, "password") || !strcmp (format, "url"))
	{
		return (memstring (memory, extent, format, string, PIB_TEXT_LEN + 1));
	}

#endif

#if 1

/*
 *   HPAV specific fields that belong in the PIB;
 */

	if (!strcmp (format, "hfid"))
	{
		return (memstring (memory, extent, format, string, PIB_HFID_LEN));
	}
	if (!strcmp (format, "mac"))
	{
		return (bytespec (string, memory, ETHER_ADDR_LEN));
	}
	if (!strcmp (format, "key"))
	{
		return (bytespec (string, memory, PIB_KEY_LEN));
	}

#endif

	error (1, ENOTSUP, "%s", format);
	return (0);
}