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

📄 config.c

📁 一个Linux下无线网卡的设置工具
💻 C
📖 第 1 页 / 共 3 页
字号:
static int wpa_config_parse_cipher(int line, const char *value){	int val = 0, last;	char *start, *end, *buf;	buf = strdup(value);	if (buf == NULL)		return -1;	start = buf;	while (*start != '\0') {		while (*start == ' ' || *start == '\t')			start++;		if (*start == '\0')			break;		end = start;		while (*end != ' ' && *end != '\t' && *end != '\0')			end++;		last = *end == '\0';		*end = '\0';		if (strcmp(start, "CCMP") == 0)			val |= WPA_CIPHER_CCMP;		else if (strcmp(start, "TKIP") == 0)			val |= WPA_CIPHER_TKIP;		else if (strcmp(start, "WEP104") == 0)			val |= WPA_CIPHER_WEP104;		else if (strcmp(start, "WEP40") == 0)			val |= WPA_CIPHER_WEP40;		else if (strcmp(start, "NONE") == 0)			val |= WPA_CIPHER_NONE;		else {			wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",				   line, start);			free(buf);			return -1;		}		if (last)			break;		start = end + 1;	}	free(buf);	if (val == 0) {		wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",			   line);		return -1;	}	return val;}static char * wpa_config_write_cipher(int cipher){	int first = 1;	char *buf, *pos, *end;	pos = buf = malloc(50);	if (buf == NULL)		return NULL;	memset(buf, 0, 50);	end = buf + 50;	if (cipher & WPA_CIPHER_CCMP) {		pos += snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");		first = 0;	}	if (cipher & WPA_CIPHER_TKIP) {		pos += snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");		first = 0;	}	if (cipher & WPA_CIPHER_WEP104) {		pos += snprintf(pos, end - pos, "%sWEP104", first ? "" : " ");		first = 0;	}	if (cipher & WPA_CIPHER_WEP40) {		pos += snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");		first = 0;	}	if (cipher & WPA_CIPHER_NONE) {		pos += snprintf(pos, end - pos, "%sNONE", first ? "" : " ");		first = 0;	}	return buf;}static int wpa_config_parse_pairwise(const struct parse_data *data,				     struct wpa_ssid *ssid, int line,				     const char *value){	int val;	val = wpa_config_parse_cipher(line, value);	if (val == -1)		return -1;	if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {		wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "			   "(0x%x).", line, val);		return -1;	}	wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);	ssid->pairwise_cipher = val;	return 0;}static char * wpa_config_write_pairwise(const struct parse_data *data,					struct wpa_ssid *ssid){	return wpa_config_write_cipher(ssid->pairwise_cipher);}static int wpa_config_parse_group(const struct parse_data *data,				  struct wpa_ssid *ssid, int line,				  const char *value){	int val;	val = wpa_config_parse_cipher(line, value);	if (val == -1)		return -1;	if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |		    WPA_CIPHER_WEP40)) {		wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "			   "(0x%x).", line, val);		return -1;	}	wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);	ssid->group_cipher = val;	return 0;}static char * wpa_config_write_group(const struct parse_data *data,				     struct wpa_ssid *ssid){	return wpa_config_write_cipher(ssid->group_cipher);}static int wpa_config_parse_auth_alg(const struct parse_data *data,				     struct wpa_ssid *ssid, int line,				     const char *value){	int val = 0, last, errors = 0;	char *start, *end, *buf;	buf = strdup(value);	if (buf == NULL)		return -1;	start = buf;	while (*start != '\0') {		while (*start == ' ' || *start == '\t')			start++;		if (*start == '\0')			break;		end = start;		while (*end != ' ' && *end != '\t' && *end != '\0')			end++;		last = *end == '\0';		*end = '\0';		if (strcmp(start, "OPEN") == 0)			val |= WPA_AUTH_ALG_OPEN;		else if (strcmp(start, "SHARED") == 0)			val |= WPA_AUTH_ALG_SHARED;		else if (strcmp(start, "LEAP") == 0)			val |= WPA_AUTH_ALG_LEAP;		else {			wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",				   line, start);			errors++;		}		if (last)			break;		start = end + 1;	}	free(buf);	if (val == 0) {		wpa_printf(MSG_ERROR,			   "Line %d: no auth_alg values configured.", line);		errors++;	}	wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);	ssid->auth_alg = val;	return errors ? -1 : 0;}static char * wpa_config_write_auth_alg(const struct parse_data *data,					struct wpa_ssid *ssid){	int first = 1;	char *buf, *pos, *end;	pos = buf = malloc(30);	if (buf == NULL)		return NULL;	memset(buf, 0, 30);	end = buf + 30;	if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {		pos += snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");		first = 0;	}	if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {		pos += snprintf(pos, end - pos, "%sSHARED", first ? "" : " ");		first = 0;	}	if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {		pos += snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");		first = 0;	}	return buf;}static int wpa_config_parse_eap(const struct parse_data *data,				struct wpa_ssid *ssid, int line,				const char *value){	int last, errors = 0;	char *start, *end, *buf;	u8 *methods = NULL, *tmp;	size_t num_methods = 0;	buf = strdup(value);	if (buf == NULL)		return -1;	start = buf;	while (*start != '\0') {		while (*start == ' ' || *start == '\t')			start++;		if (*start == '\0')			break;		end = start;		while (*end != ' ' && *end != '\t' && *end != '\0')			end++;		last = *end == '\0';		*end = '\0';		tmp = methods;		methods = realloc(methods, num_methods + 1);		if (methods == NULL) {			free(tmp);			free(buf);			return -1;		}		methods[num_methods] = eap_get_type(start);		if (methods[num_methods] == EAP_TYPE_NONE) {			wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "				   "'%s'", line, start);			wpa_printf(MSG_ERROR, "You may need to add support for"				   " this EAP method during wpa_supplicant\n"				   "build time configuration.\n"				   "See README for more information.");			errors++;		} else if (methods[num_methods] == EAP_TYPE_LEAP)			ssid->leap++;		else			ssid->non_leap++;		num_methods++;		if (last)			break;		start = end + 1;	}	free(buf);	tmp = methods;	methods = realloc(methods, num_methods + 1);	if (methods == NULL) {		free(tmp);		return -1;	}	methods[num_methods] = EAP_TYPE_NONE;	num_methods++;	wpa_hexdump(MSG_MSGDUMP, "eap methods", methods, num_methods);	ssid->eap_methods = methods;	return errors ? -1 : 0;}static char * wpa_config_write_eap(const struct parse_data *data,				   struct wpa_ssid *ssid){	int first = 1;	char *buf, *pos, *end;	const u8 *eap_methods = ssid->eap_methods;	const char *name;	if (eap_methods == NULL)		return NULL;	pos = buf = malloc(100);	if (buf == NULL)		return NULL;	memset(buf, 0, 100);	end = buf + 100;	while (*eap_methods != EAP_TYPE_NONE) {		name = eap_get_name(*eap_methods);		if (name)			pos += snprintf(pos, end - pos, "%s%s",					first ? "" : " ", name);		first = 0;		eap_methods++;	}	return buf;}static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,				    const char *value, int idx){	char *buf, title[20];	buf = wpa_config_parse_string(value, len);	if (buf == NULL) {		wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",			   line, idx, value);		return -1;	}	if (*len > MAX_WEP_KEY_LEN) {		wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",			   line, idx, value);		free(buf);		return -1;	}	memcpy(key, buf, *len);	free(buf);	snprintf(title, sizeof(title), "wep_key%d", idx);	wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);	return 0;}static int wpa_config_parse_wep_key0(const struct parse_data *data,				     struct wpa_ssid *ssid, int line,				     const char *value){	return wpa_config_parse_wep_key(ssid->wep_key[0],					&ssid->wep_key_len[0], line,					value, 0);}static int wpa_config_parse_wep_key1(const struct parse_data *data,				     struct wpa_ssid *ssid, int line,				     const char *value){	return wpa_config_parse_wep_key(ssid->wep_key[1],					&ssid->wep_key_len[1], line,					value, 1);}static int wpa_config_parse_wep_key2(const struct parse_data *data,				     struct wpa_ssid *ssid, int line,				     const char *value){	return wpa_config_parse_wep_key(ssid->wep_key[2],					&ssid->wep_key_len[2], line,					value, 2);}static int wpa_config_parse_wep_key3(const struct parse_data *data,				     struct wpa_ssid *ssid, int line,				     const char *value){	return wpa_config_parse_wep_key(ssid->wep_key[3],					&ssid->wep_key_len[3], line,					value, 3);}static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx){	if (ssid->wep_key_len[idx] == 0)		return NULL;	return wpa_config_write_string(ssid->wep_key[idx],				       ssid->wep_key_len[idx]);}static char * wpa_config_write_wep_key0(const struct parse_data *data,					struct wpa_ssid *ssid){	return wpa_config_write_wep_key(ssid, 0);}static char * wpa_config_write_wep_key1(const struct parse_data *data,					struct wpa_ssid *ssid){	return wpa_config_write_wep_key(ssid, 1);}static char * wpa_config_write_wep_key2(const struct parse_data *data,					struct wpa_ssid *ssid){	return wpa_config_write_wep_key(ssid, 2);}static char * wpa_config_write_wep_key3(const struct parse_data *data,					struct wpa_ssid *ssid){	return wpa_config_write_wep_key(ssid, 3);}/* Helper macros for network block parser *//* OFFSET: Get offset of a variable within the wpa_ssid structure */#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)/* STR: Define a string variable for an ASCII string; f = field name */#define STR(f) .name = #f, .parser = wpa_config_parse_str, \	.writer = wpa_config_write_str, .param1 = OFFSET(f)/* STR_LEN: Define a string variable with a separate variable for storing the * data length. Unlike STR(), this can be used to store arbitrary binary data * (i.e., even nul termination character). */#define STR_LEN(f) STR(f), .param2 = OFFSET(f ## _len)/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length * explicitly specified. */#define STR_RANGE(f, min, max) STR_LEN(f), .param3 = (void *) (min), \	.param4 = (void *) (max)/* INT: Define an integer variable */#define INT(f) .name = #f, .parser = wpa_config_parse_int, \	.writer = wpa_config_write_int, \	.param1 = OFFSET(f), .param2 = (void *) 0/* INT: Define an integer variable with allowed value range */#define INT_RANGE(f, min, max) INT(f), .param3 = (void *) (min), \	.param4 = (void *) (max)/* FUNC: Define a configuration variable that uses a custom function for * parsing and writing the value. */#define FUNC(f) .name = #f, .parser = wpa_config_parse_ ## f, \		.writer = wpa_config_write_ ## f/* * Table of network configuration variables. This table is used to parse each * network configuration variable, e.g., each line in wpa_supplicant.conf file * that is inside a network block. * * This table is generated using the helper macros defined above and with * generous help from the C pre-processor. The field name is stored as a string * into .name and for STR and INT types, the offset of the target buffer within * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar * offset to the field containing the length of the configuration variable. * .param3 and .param4 can be used to mark the allowed range (length for STR * and value for INT). * * For each configuration line in wpa_supplicant.conf, the parser goes through * this table and select the entry that matches with the field name. The parser * function (.parser) is then called to parse the actual value of the field. * * This kind of mechanism makes it easy to add new configuration parameters, * since only one line needs to be added into this table and into the * struct wpa_ssid definition if the new variable is either a string or * integer. More complex types will need to use their own parser and writer * functions. */static const struct parse_data ssid_fields[] = {	{ STR_RANGE(ssid, 0, MAX_SSID_LEN) },	{ INT_RANGE(scan_ssid, 0, 1) },	{ FUNC(bssid) },	{ FUNC(psk), .key_data = 1 },	{ FUNC(proto) },	{ FUNC(key_mgmt) },	{ FUNC(pairwise) },	{ FUNC(group) },	{ FUNC(auth_alg) },	{ FUNC(eap) },	{ STR_LEN(identity) },	{ STR_LEN(anonymous_identity) },	{ STR_RANGE(eappsk, EAP_PSK_LEN, EAP_PSK_LEN), .key_data = 1 },	{ STR_LEN(nai) },	{ STR_LEN(password), .key_data = 1 },	{ STR(ca_cert) },	{ STR(ca_path) },	{ STR(client_cert) },	{ STR(private_key) },	{ STR(private_key_passwd), .key_data = 1 },	{ STR(dh_file) },	{ STR(subject_match) },	{ STR(altsubject_match) },	{ STR(ca_cert2) },	{ STR(ca_path2) },	{ STR(client_cert2) },	{ STR(private_key2) },	{ STR(private_key2_passwd), .key_data = 1 },	{ STR(dh_file2) },	{ STR(subject_match2) },	{ STR(altsubject_match2) },	{ STR(phase1) },	{ STR(phase2) },	{ STR(pcsc) },

⌨️ 快捷键说明

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