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

📄 ctrl_iface.c

📁 一个Linux下无线网卡的设置工具
💻 C
📖 第 1 页 / 共 3 页
字号:
		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "			   "id=%d", id);		return -1;	}	if (ssid != wpa_s->current_ssid && wpa_s->current_ssid)		wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);	/* Mark all other networks disabled and trigger reassociation */	ssid = wpa_s->conf->ssid;	while (ssid) {		ssid->disabled = id != ssid->id;		ssid = ssid->next;	}	wpa_s->reassociate = 1;	wpa_supplicant_req_scan(wpa_s, 0, 0);	return 0;}static int wpa_supplicant_ctrl_iface_enable_network(	struct wpa_supplicant *wpa_s, char *cmd){	int id;	struct wpa_ssid *ssid;	/* cmd: "<network id>" */	id = atoi(cmd);	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);	ssid = wpa_config_get_network(wpa_s->conf, id);	if (ssid == NULL) {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "			   "id=%d", id);		return -1;	}	if (wpa_s->current_ssid == NULL && ssid->disabled) {		/*		 * Try to reassociate since there is no current configuration		 * and a new network was made available. */		wpa_s->reassociate = 1;		wpa_supplicant_req_scan(wpa_s, 0, 0);	}	ssid->disabled = 0;	return 0;}static int wpa_supplicant_ctrl_iface_disable_network(	struct wpa_supplicant *wpa_s, char *cmd){	int id;	struct wpa_ssid *ssid;	/* cmd: "<network id>" */	id = atoi(cmd);	wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);	ssid = wpa_config_get_network(wpa_s->conf, id);	if (ssid == NULL) {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "			   "id=%d", id);		return -1;	}	if (ssid == wpa_s->current_ssid)		wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);	ssid->disabled = 1;	return 0;}static int wpa_supplicant_ctrl_iface_add_network(	struct wpa_supplicant *wpa_s, char *buf, size_t buflen){	struct wpa_ssid *ssid;	wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");	ssid = wpa_config_add_network(wpa_s->conf);	if (ssid == NULL)		return -1;	ssid->disabled = 1;	wpa_config_set_network_defaults(ssid);	return snprintf(buf, buflen, "%d\n", ssid->id);}static int wpa_supplicant_ctrl_iface_remove_network(	struct wpa_supplicant *wpa_s, char *cmd){	int id;	struct wpa_ssid *ssid;	/* cmd: "<network id>" */	id = atoi(cmd);	wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);	ssid = wpa_config_get_network(wpa_s->conf, id);	if (ssid == NULL ||	    wpa_config_remove_network(wpa_s->conf, id) < 0) {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "			   "id=%d", id);		return -1;	}	if (ssid == wpa_s->current_ssid)		wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);	return 0;}static int wpa_supplicant_ctrl_iface_set_network(	struct wpa_supplicant *wpa_s, char *cmd){	int id;	struct wpa_ssid *ssid;	char *name, *value;	/* cmd: "<network id> <variable name> <value>" */	name = strchr(cmd, ' ');	if (name == NULL)		return -1;	*name++ = '\0';	value = strchr(name, ' ');	if (value == NULL)		return -1;	*value++ = '\0';	id = atoi(cmd);	wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",		   id, name);	wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",			      (u8 *) value, strlen(value));	ssid = wpa_config_get_network(wpa_s->conf, id);	if (ssid == NULL) {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "			   "id=%d", id);		return -1;	}	if (wpa_config_set(ssid, name, value, 0) < 0) {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "			   "variable '%s'", name);		return -1;	}	if ((strcmp(name, "psk") == 0 && value[0] == '"' && ssid->ssid_len) ||	    (strcmp(name, "ssid") == 0 && ssid->passphrase))		wpa_config_update_psk(ssid);	return 0;}static int wpa_supplicant_ctrl_iface_get_network(	struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen){	int id;	struct wpa_ssid *ssid;	char *name, *value;	/* cmd: "<network id> <variable name>" */	name = strchr(cmd, ' ');	if (name == NULL)		return -1;	*name++ = '\0';	id = atoi(cmd);	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",		   id, name);	ssid = wpa_config_get_network(wpa_s->conf, id);	if (ssid == NULL) {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "			   "id=%d", id);		return -1;	}	value = wpa_config_get(ssid, name);	if (value == NULL) {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "			   "variable '%s'", name);		return -1;	}	snprintf(buf, buflen, "%s", value);	free(value);	return strlen(buf);}static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s){	int ret;	if (!wpa_s->conf->update_config) {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "			   "to update configuration (update_config=0)");		return -1;	}	ret = wpa_config_write(wpa_s->confname, wpa_s->conf);	if (ret) {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "			   "update configuration");	} else {		wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"			   " updated");	}	return ret;}static int wpa_supplicant_ctrl_iface_get_capability(	struct wpa_supplicant *wpa_s, const char *field, char *buf,	size_t buflen){	struct wpa_driver_capa capa;	int res, first = 1;	char *pos, *end;	wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s'", field);	if (strcmp(field, "eap") == 0) {		return eap_get_names(buf, buflen);	}	res = wpa_drv_get_capa(wpa_s, &capa);	pos = buf;	end = pos + buflen;	if (strcmp(field, "pairwise") == 0) {		if (res < 0)			return snprintf(buf, buflen, "CCMP TKIP NONE");		if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {			pos += snprintf(pos, end - pos, "%sCCMP",					first ? "" : " ");			first = 0;		}		if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {			pos += snprintf(pos, end - pos, "%sTKIP",					first ? "" : " ");			first = 0;		}		if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {			pos += snprintf(pos, end - pos, "%sNONE",					first ? "" : " ");			first = 0;		}		return pos - buf;	}	if (strcmp(field, "group") == 0) {		if (res < 0)			return snprintf(buf, buflen, "CCMP TKIP WEP104 WEP40");		if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {			pos += snprintf(pos, end - pos, "%sCCMP",					first ? "" : " ");			first = 0;		}		if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {			pos += snprintf(pos, end - pos, "%sTKIP",					first ? "" : " ");			first = 0;		}		if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {			pos += snprintf(pos, end - pos, "%sWEP104",					first ? "" : " ");			first = 0;		}		if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {			pos += snprintf(pos, end - pos, "%sWEP40",					first ? "" : " ");			first = 0;		}		return pos - buf;	}	if (strcmp(field, "key_mgmt") == 0) {		if (res < 0) {			return snprintf(buf, buflen, "WPA-PSK WPA-EAP "					"IEEE8021X WPA-NONE NONE");		}		pos += snprintf(pos, end - pos, "NONE IEEE8021X");		if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |				     WPA_DRIVER_CAPA_KEY_MGMT_WPA2))			pos += snprintf(pos, end - pos, " WPA-EAP");		if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |				     WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK))			pos += snprintf(pos, end - pos, " WPA-PSK");		if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE)			pos += snprintf(pos, end - pos, " WPA-NONE");		return pos - buf;	}	if (strcmp(field, "proto") == 0) {		if (res < 0)			return snprintf(buf, buflen, "RSN WPA");		if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |				     WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {			pos += snprintf(pos, end - pos, "%sRSN",					first ? "" : " ");			first = 0;		}		if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |				     WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {			pos += snprintf(pos, end - pos, "%sWPA",					first ? "" : " ");			first = 0;		}		return pos - buf;	}	if (strcmp(field, "auth_alg") == 0) {		if (res < 0)			return snprintf(buf, buflen, "OPEN SHARED LEAP");		if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {			pos += snprintf(pos, end - pos, "%sOPEN",					first ? "" : " ");			first = 0;		}		if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {			pos += snprintf(pos, end - pos, "%sSHARED",					first ? "" : " ");			first = 0;		}		if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {			pos += snprintf(pos, end - pos, "%sLEAP",					first ? "" : " ");			first = 0;		}		return pos - buf;	}	wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",		   field);	return -1;}static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,					      void *sock_ctx){	struct wpa_supplicant *wpa_s = eloop_ctx;	char buf[256];	int res;	CTRL_IFACE_SOCK from;	socklen_t fromlen = sizeof(from);	char *reply;	const int reply_size = 2048;	int reply_len;	int new_attached = 0, ctrl_rsp = 0;	res = recvfrom(sock, buf, sizeof(buf) - 1, 0,		       (struct sockaddr *) &from, &fromlen);	if (res < 0) {		perror("recvfrom(ctrl_iface)");		return;	}	buf[res] = '\0';	if (strncmp(buf, WPA_CTRL_RSP, strlen(WPA_CTRL_RSP)) == 0 ||	    strncmp(buf, "SET_NETWORK ", 12) == 0) {		wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",				      (u8 *) buf, res);	} else {		wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface", (u8 *) buf, res);	}	reply = malloc(reply_size);	if (reply == NULL) {		sendto(sock, "FAIL\n", 5, 0, (struct sockaddr *) &from,		       fromlen);		return;	}	memcpy(reply, "OK\n", 3);	reply_len = 3;	if (strcmp(buf, "PING") == 0) {		memcpy(reply, "PONG\n", 5);		reply_len = 5;	} else if (strcmp(buf, "MIB") == 0) {		reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);		if (reply_len >= 0) {			res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,					       reply_size - reply_len);			if (res < 0)				reply_len = -1;			else				reply_len += res;		}	} else if (strncmp(buf, "STATUS", 6) == 0) {		reply_len = wpa_supplicant_ctrl_iface_status(			wpa_s, buf + 6, reply, reply_size);	} else if (strcmp(buf, "PMKSA") == 0) {		reply_len = pmksa_cache_list(wpa_s->wpa, reply, reply_size);	} else if (strncmp(buf, "SET ", 4) == 0) {		if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))			reply_len = -1;	} else if (strcmp(buf, "LOGON") == 0) {		eapol_sm_notify_logoff(wpa_s->eapol, FALSE);	} else if (strcmp(buf, "LOGOFF") == 0) {		eapol_sm_notify_logoff(wpa_s->eapol, TRUE);	} else if (strcmp(buf, "REASSOCIATE") == 0) {		wpa_s->disconnected = 0;		wpa_s->reassociate = 1;		wpa_supplicant_req_scan(wpa_s, 0, 0);	} else if (strncmp(buf, "PREAUTH ", 8) == 0) {		if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))			reply_len = -1;	} else if (strcmp(buf, "ATTACH") == 0) {		if (wpa_supplicant_ctrl_iface_attach(wpa_s, &from, fromlen))			reply_len = -1;		else			new_attached = 1;	} else if (strcmp(buf, "DETACH") == 0) {		if (wpa_supplicant_ctrl_iface_detach(wpa_s, &from, fromlen))			reply_len = -1;	} else if (strncmp(buf, "LEVEL ", 6) == 0) {		if (wpa_supplicant_ctrl_iface_level(wpa_s, &from, fromlen,						    buf + 6))			reply_len = -1;	} else if (strncmp(buf, WPA_CTRL_RSP, strlen(WPA_CTRL_RSP)) == 0) {		if (wpa_supplicant_ctrl_iface_ctrl_rsp(			    wpa_s, buf + strlen(WPA_CTRL_RSP)))			reply_len = -1;		else			ctrl_rsp = 1;	} else if (strcmp(buf, "RECONFIGURE") == 0) {		if (wpa_supplicant_reload_configuration(wpa_s))			reply_len = -1;	} else if (strcmp(buf, "TERMINATE") == 0) {		eloop_terminate();	} else if (strncmp(buf, "BSSID ", 6) == 0) {		if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))			reply_len = -1;	} else if (strcmp(buf, "LIST_NETWORKS") == 0) {		reply_len = wpa_supplicant_ctrl_iface_list_networks(			wpa_s, reply, reply_size);	} else if (strcmp(buf, "DISCONNECT") == 0) {		wpa_s->disconnected = 1;		wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);	} else if (strcmp(buf, "SCAN") == 0) {		wpa_s->scan_req = 2;		wpa_supplicant_req_scan(wpa_s, 0, 0);	} else if (strcmp(buf, "SCAN_RESULTS") == 0) {		reply_len = wpa_supplicant_ctrl_iface_scan_results(			wpa_s, reply, reply_size);	} else if (strncmp(buf, "SELECT_NETWORK ", 15) == 0) {		if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))			reply_len = -1;	} else if (strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {		if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))			reply_len = -1;	} else if (strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {		if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))			reply_len = -1;	} else if (strcmp(buf, "ADD_NETWORK") == 0) {		reply_len = wpa_supplicant_ctrl_iface_add_network(			wpa_s, reply, reply_size);	} else if (strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {		if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))			reply_len = -1;	} else if (strncmp(buf, "SET_NETWORK ", 12) == 0) {		if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))			reply_len = -1;	} else if (strncmp(buf, "GET_NETWORK ", 12) == 0) {		reply_len = wpa_supplicant_ctrl_iface_get_network(			wpa_s, buf + 12, reply, reply_size);	} else if (strcmp(buf, "SAVE_CONFIG") == 0) {		if (wpa_supplicant_ctrl_iface_save_config(wpa_s))			reply_len = -1;	} else if (strncmp(buf, "GET_CAPABILITY ", 15) == 0) {		reply_len = wpa_supplicant_ctrl_iface_get_capability(			wpa_s, buf + 15, reply, reply_size);	} else {		memcpy(reply, "UNKNOWN COMMAND\n", 16);		reply_len = 16;	}	if (reply_len < 0) {		memcpy(reply, "FAIL\n", 5);		reply_len = 5;	}	sendto(sock, reply, reply_len, 0, (struct sockaddr *) &from, fromlen);	free(reply);	if (new_attached)		eapol_sm_notify_ctrl_attached(wpa_s->eapol);	if (ctrl_rsp)		eapol_sm_notify_ctrl_response(wpa_s->eapol);}#ifndef CONFIG_CTRL_IFACE_UDPstatic char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s){	char *buf;	size_t len;	if (wpa_s->conf->ctrl_interface == NULL)		return NULL;	len = strlen(wpa_s->conf->ctrl_interface) + strlen(wpa_s->ifname) + 2;	buf = malloc(len);	if (buf == NULL)		return NULL;	snprintf(buf, len, "%s/%s",		 wpa_s->conf->ctrl_interface, wpa_s->ifname);#ifdef __CYGWIN__	{		/* Windows/WinPcap uses interface names that are not suitable		 * as a file name - convert invalid chars to underscores */		char *pos = buf;		while (*pos) {			if (*pos == '\\')				*pos = '_';			pos++;		}	}#endif /* __CYGWIN__ */	return buf;}#endif /* CONFIG_CTRL_IFACE_UDP *//**

⌨️ 快捷键说明

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