示例#1
0
int
make_option_mask(const struct dhcp_opt *dopts, size_t dopts_len,
    uint8_t *mask, const char *opts, int add)
{
	char *token, *o, *p, *t;
	const struct dhcp_opt *opt;
	int match;
	unsigned int n;
	size_t i;

	o = p = strdup(opts);
	if (opts == NULL)
		return -1;
	while ((token = strsep(&p, ", "))) {
		if (*token == '\0')
			continue;
		for (i = 0, opt = dopts; i < dopts_len; i++, opt++) {
			match = 0;
			if (strcmp(opt->var, token) == 0)
				match = 1;
			else {
				errno = 0;
				n = strtol(token, &t, 0);
				if (errno == 0 && !*t)
					if (opt->option == n)
						match = 1;
			}
			if (match) {
				if (add == 2 && !(opt->type & ADDRIPV4)) {
					free(o);
					errno = EINVAL;
					return -1;
				}
				if (add == 1 || add == 2)
					add_option_mask(mask,
					    opt->option);
				else
					del_option_mask(mask,
					    opt->option);
				break;
			}
		}
		if (!opt->option) {
			free(o);
			errno = ENOENT;
			return -1;
		}
	}
	free(o);
	return 0;
}
int make_option_mask(uint8_t *mask, char **opts, int add)
{
	char *token, *p = *opts, *t;
	const struct dhcp_opt *opt;
	int match, n;

	while ((token = strsep(&p, ", "))) {
		if (*token == '\0')
			continue;
		for (opt = dhcp_opts; opt->option; opt++) {
			if (!opt->var)
				continue;
			match = 0;
			if (strcmp(opt->var, token) == 0)
				match = 1;
			else {
				errno = 0;
				n = strtol(token, &t, 0);
				if (errno == 0 && !*t)
					if (opt->option == n)
						match = 1;
			}
			if (match) {	
				if (add == 1)
					add_option_mask(mask,
							opt->option);
				else
					del_option_mask(mask,
							opt->option);
				break;
			}
		}
		if (!opt->option) {
			*opts = token;
			errno = ENOENT;
			return -1;
		}
	}
	return 0;
}