hostapd.c

来自「hostapd无线AP工具」· C语言 代码 · 共 835 行 · 第 1/2 页

C
835
字号
	/*	 * Fetch the SSID from the system and use it or,	 * if one was specified in the config file, verify they	 * match.	 */	ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));	if (ssid_len < 0) {		printf("Could not read SSID from system\n");		return -1;	}	if (conf->ssid_set) {		/*		 * If SSID is specified in the config file and it differs		 * from what is being used then force installation of the		 * new SSID.		 */		set_ssid = (conf->ssid_len != ssid_len ||			    memcmp(conf->ssid, ssid, ssid_len) != 0);	} else {		/*		 * No SSID in the config file; just use the one we got		 * from the system.		 */		set_ssid = 0;		conf->ssid_len = ssid_len;		memcpy(conf->ssid, ssid, conf->ssid_len);		conf->ssid[conf->ssid_len] = '\0';	}	printf("Using interface %s with hwaddr " MACSTR " and ssid '%s'\n",	       hapd->conf->iface, MAC2STR(hapd->own_addr), hapd->conf->ssid);	if (hostapd_setup_wpa_psk(conf)) {		printf("WPA-PSK setup failed.\n");		return -1;	}	/* Set SSID for the kernel driver (to be used in beacon and probe	 * response frames) */	if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid,					 conf->ssid_len)) {		printf("Could not set SSID for kernel driver\n");		return -1;	}	if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MSGDUMPS))		conf->radius->msg_dumps = 1;	hapd->radius = radius_client_init(hapd, conf->radius);	if (hapd->radius == NULL) {		printf("RADIUS client initialization failed.\n");		return -1;	}	if (conf->radius_server_clients) {		struct radius_server_conf srv;		memset(&srv, 0, sizeof(srv));		srv.client_file = conf->radius_server_clients;		srv.auth_port = conf->radius_server_auth_port;		srv.hostapd_conf = conf;		srv.eap_sim_db_priv = hapd->eap_sim_db_priv;		srv.ssl_ctx = hapd->ssl_ctx;		srv.ipv6 = conf->radius_server_ipv6;		hapd->radius_srv = radius_server_init(&srv);		if (hapd->radius_srv == NULL) {			printf("RADIUS server initialization failed.\n");			return -1;		}	}	if (hostapd_acl_init(hapd)) {		printf("ACL initialization failed.\n");		return -1;	}	if (ieee802_1x_init(hapd)) {		printf("IEEE 802.1X initialization failed.\n");		return -1;	}	if (hapd->conf->wpa && wpa_init(hapd)) {		printf("WPA initialization failed.\n");		return -1;	}	if (accounting_init(hapd)) {		printf("Accounting initialization failed.\n");		return -1;	}	if (hapd->conf->ieee802_11f &&	    (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {		printf("IEEE 802.11F (IAPP) initialization failed.\n");		return -1;	}	if (hostapd_wireless_event_init(hapd) < 0)		return -1;	if (hostapd_flush_old_stations(hapd))		return -1;	if (hostapd_ctrl_iface_init(hapd)) {		printf("Failed to setup control interface\n");		ret = -1;	}	return ret;}struct driver {	struct driver *next;	char *name;	const struct driver_ops *ops;};static struct driver *drivers = NULL;void driver_register(const char *name, const struct driver_ops *ops){	struct driver *d;	d = malloc(sizeof(struct driver));	if (d == NULL) {		printf("Failed to register driver %s!\n", name);		return;	}	d->name = strdup(name);	if (d->name == NULL) {		printf("Failed to register driver %s!\n", name);		free(d);		return;	}	d->ops = ops;	d->next = drivers;	drivers = d;}void driver_unregister(const char *name){	struct driver *p, **pp;	for (pp = &drivers; (p = *pp) != NULL; pp = &p->next) {		if (strcasecmp(p->name, name) == 0) {			*pp = p->next;			p->next = NULL;			free(p->name);			free(p);			break;		}	}}static void driver_unregister_all(void){	struct driver *p, *pp;	p = drivers;	drivers = NULL;	while (p) {		pp = p;		p = p->next;		free(pp->name);		free(pp);	}}const struct driver_ops * driver_lookup(const char *name){	struct driver *p;	if (strcmp(name, "default") == 0) {		p = drivers;		while (p && p->next)			p = p->next;		return p->ops;	}	for (p = drivers; p != NULL; p = p->next) {		if (strcasecmp(p->name, name) == 0)			return p->ops;	}	return NULL;}static void show_version(void){	fprintf(stderr,		"hostapd v" VERSION_STR "\n"		"User space daemon for IEEE 802.11 AP management,\n"		"IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"		"Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi> "		"and contributors\n");}static void usage(void){	show_version();	fprintf(stderr,		"\n"		"usage: hostapd [-hdBKt] <configuration file(s)>\n"		"\n"		"options:\n"		"   -h   show this usage\n"		"   -d   show more debug messages (-dd for even more)\n"		"   -B   run daemon in the background\n"		"   -K   include key data in debug messages\n"		"   -t   include timestamps in some debug messages\n"		"   -v   show hostapd version\n");	exit(1);}static hostapd * hostapd_init(const char *config_file){	hostapd *hapd;	hapd = malloc(sizeof(*hapd));	if (hapd == NULL) {		printf("Could not allocate memory for hostapd data\n");		goto fail;	}	memset(hapd, 0, sizeof(*hapd));	hapd->config_fname = strdup(config_file);	if (hapd->config_fname == NULL) {		printf("Could not allocate memory for config_fname\n");		goto fail;	}	hapd->conf = hostapd_config_read(hapd->config_fname);	if (hapd->conf == NULL) {		goto fail;	}	if (hapd->conf->individual_wep_key_len > 0) {		/* use key0 in individual key and key1 in broadcast key */		hapd->default_wep_key_idx = 1;	}#ifdef EAP_TLS_FUNCS	if (hapd->conf->eap_server &&	    (hapd->conf->ca_cert || hapd->conf->server_cert)) {		hapd->ssl_ctx = tls_init(NULL);		if (hapd->ssl_ctx == NULL) {			printf("Failed to initialize TLS\n");			goto fail;		}		if (tls_global_ca_cert(hapd->ssl_ctx, hapd->conf->ca_cert)) {			printf("Failed to load CA certificate (%s)\n",				hapd->conf->ca_cert);			goto fail;		}		if (tls_global_client_cert(hapd->ssl_ctx,					   hapd->conf->server_cert)) {			printf("Failed to load server certificate (%s)\n",				hapd->conf->server_cert);			goto fail;		}		if (tls_global_private_key(hapd->ssl_ctx,					   hapd->conf->private_key,					   hapd->conf->private_key_passwd)) {			printf("Failed to load private key (%s)\n",			       hapd->conf->private_key);			goto fail;		}		if (tls_global_set_verify(hapd->ssl_ctx,					  hapd->conf->check_crl)) {			printf("Failed to enable check_crl\n");			goto fail;		}	}#endif /* EAP_TLS_FUNCS */	if (hapd->conf->eap_sim_db) {		hapd->eap_sim_db_priv =			eap_sim_db_init(hapd->conf->eap_sim_db);		if (hapd->eap_sim_db_priv == NULL) {			printf("Failed to initialize EAP-SIM database "			       "interface\n");			goto fail;		}	}	if (hapd->conf->assoc_ap)		hapd->assoc_ap_state = WAIT_BEACON;	/* FIX: need to fix this const vs. not */	hapd->driver = (struct driver_ops *) hapd->conf->driver;	return hapd;fail:	if (hapd) {		if (hapd->ssl_ctx)			tls_deinit(hapd->ssl_ctx);		if (hapd->conf)			hostapd_config_free(hapd->conf);		free(hapd->config_fname);		free(hapd);	}	return NULL;}void register_drivers(void);int main(int argc, char *argv[]){	struct hapd_interfaces interfaces;	int ret = 1, i, j;	int c, debug = 0, daemonize = 0;	for (;;) {		c = getopt(argc, argv, "BdhKtv");		if (c < 0)			break;		switch (c) {		case 'h':			usage();			break;		case 'd':			debug++;			break;		case 'B':			daemonize++;			break;		case 'K':			wpa_debug_show_keys++;			break;		case 't':			wpa_debug_timestamp++;			break;		case 'v':			show_version();			exit(1);			break;		default:			usage();			break;		}	}	if (optind == argc)		usage();	register_drivers();		/* NB: generated by Makefile */	interfaces.count = argc - optind;	interfaces.hapd = malloc(interfaces.count * sizeof(hostapd *));	if (interfaces.hapd == NULL) {		printf("malloc failed\n");		exit(1);	}	eloop_init(&interfaces);	eloop_register_signal(SIGHUP, handle_reload, NULL);	eloop_register_signal(SIGINT, handle_term, NULL);	eloop_register_signal(SIGTERM, handle_term, NULL);	eloop_register_signal(SIGUSR1, handle_dump_state, NULL);	for (i = 0; i < interfaces.count; i++) {		printf("Configuration file: %s\n", argv[optind + i]);		interfaces.hapd[i] = hostapd_init(argv[optind + i]);		if (!interfaces.hapd[i])			goto out;		for (j = 0; j < debug; j++) {			if (interfaces.hapd[i]->conf->logger_stdout_level > 0)				interfaces.hapd[i]->conf->					logger_stdout_level--;			interfaces.hapd[i]->conf->debug++;		}		if (hostapd_setup_interface(interfaces.hapd[i]))			goto out;		wpa_debug_level -= interfaces.hapd[0]->conf->debug;	}	if (daemonize && daemon(0, 0)) {		perror("daemon");		goto out;	}	openlog("hostapd", 0, LOG_DAEMON);	eloop_run();	for (i = 0; i < interfaces.count; i++) {		hostapd_free_stas(interfaces.hapd[i]);		hostapd_flush_old_stations(interfaces.hapd[i]);	}	ret = 0; out:	for (i = 0; i < interfaces.count; i++) {		if (!interfaces.hapd[i])			continue;		hostapd_cleanup(interfaces.hapd[i]);		free(interfaces.hapd[i]);	}	free(interfaces.hapd);	eloop_destroy();	closelog();	driver_unregister_all();	return ret;}

⌨️ 快捷键说明

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