Exemple #1
0
void _setConfigurations(cJSON *root, s_config *config, char *http_packet)
{
	cJSON *valueSetObj= cJSON_CreateObject();
	cJSON *attribute;
	cJSON *key;
	cJSON *item;
	int array_size;
	int i;
	char cmd[MAX_BUF];
	char result[MAX_BUF];
	char ErrMesg[B_5_BUF];
	cJSON *transaction_id = cJSON_GetObjectItem(root, "transaction_id");
	char *conf_version;
	char *radio_index;
	char *profile_index;
	char *status;
	int flag =0;
	if(!transaction_id) {
		debug(LOG_ERR, "Can not find transaction_id parameter: %s", cJSON_GetErrorPtr());
		create_http_json(valueSetObj, NULL, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{transaction_id}", config->sn, http_packet);
		return;

	}
	cJSON *valueSet = cJSON_GetObjectItem(root, "valueSet");
	if(!valueSet) {
		debug(LOG_ERR, "Get valueSet faild[%s]", cJSON_GetErrorPtr());
		create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{valueSet}", config->sn, http_packet);
		return;
	}
	/*
	if((key = cJSON_GetObjectItem(valueSet, "conf_version")) == NULL || (conf_version = key->valuestring) == NULL) {
		create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{conf_version}", config->sn, http_packet);
		return;
	}
	*/
	ErrMesg[0]=0;
	//setDeviceInfo//
	{
		if((attribute = cJSON_GetObjectItem(valueSet, "setDeviceInfo")) != NULL) {
			if((key = cJSON_GetObjectItem(attribute, "apname")) != NULL && key->valuestring != NULL) {
					/*snprintf(cmd, MAX_BUF, "config apname %s\n", key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_apname(key->valuestring, result, sizeof(result)/sizeof(result[0]));
				}
		}
	}
	//setRadioInfo//
	{
		if((attribute = cJSON_GetObjectItem(valueSet, "setRadioInfo")) != NULL) {
			array_size = cJSON_GetArraySize(attribute);
			debug(LOG_DEBUG, "Array size of paras is %d",array_size);
 			for(i=0; i< array_size; i++) {
				item = cJSON_GetArrayItem(attribute, i);
				//debug(LOG_DEBUG, "%s\n",item->valuestring);
				if((key = cJSON_GetObjectItem(item, "radio_index")) == NULL || (radio_index = radio_keywords[parse_radio_keywords(key->valuestring)].value) == NULL) {
					create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{radio_index}", config->sn, http_packet);
					return;
				}
				/*
				if((key = cJSON_GetObjectItem(item, "radio")) != NULL && key->valuestring != NULL) {
					snprintf(cmd, MAX_BUF, "config interface wlan %s radio %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					//debug(LOG_INFO, "result is %d", strlen(result));
				}
				*/
				if((key = cJSON_GetObjectItem(item, "mode")) != NULL && key->valuestring != NULL) {
					/*snprintf(cmd, MAX_BUF, "config interface wlan %s mode %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
			
					set_mode(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				
				if((key = cJSON_GetObjectItem(item, "power")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s power %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_power(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				
				if((key = cJSON_GetObjectItem(item, "channel")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s channel %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_channel(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				if((key = cJSON_GetObjectItem(item, "channelwidth")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s channelwidth %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_channelwidth(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
	
				}
				if((key = cJSON_GetObjectItem(item, "max-wireless-clients")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s max-wireless-clients %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_max_assoc(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				
				if((key = cJSON_GetObjectItem(item, "client-isolation")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s client-isolation %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_ap_isolate(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
					
				}
				/*
				if((key = cJSON_GetObjectItem(item, "rate")) != NULL && key->valuestring != NULL) {
					snprintf(cmd, MAX_BUF, "config interface wlan %s rate %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
				}	
				*/
 			}
		}
	}
	//setSSIDInfo//
	{
		if((attribute = cJSON_GetObjectItem(valueSet, "setSSIDInfo")) != NULL) {
			array_size = cJSON_GetArraySize(attribute);
			debug(LOG_DEBUG, "Array size of paras is %d",array_size);
 			for(i=0; i< array_size; i++) {
				item = cJSON_GetArrayItem(attribute, i);
				if((key = cJSON_GetObjectItem(item, "radio_index")) == NULL || (radio_index = radio_keywords[parse_radio_keywords(key->valuestring)].value) == NULL) {
					create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{radio_index}", config->sn, http_packet);
					return;
				}
				if((key = cJSON_GetObjectItem(item, "profile_index")) == NULL || (profile_index = key->valuestring) == NULL) {
					create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{profile_index}", config->sn, http_packet);
					return;
				}
				if((key = cJSON_GetObjectItem(item, "status")) == NULL || (status = key->valuestring) == NULL) {
					create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{status}", config->sn, http_packet);
					return;
				}
				if(atoi(profile_index)>0) {
					char vifs[128];
					char viname[32];
					sprintf(viname, "wl%d.%d", atoi(radio_index), atoi(profile_index));
					read_vifs(atoi(radio_index), vifs, sizeof(vifs)/sizeof(vifs[0]));
	
					if(strstr(vifs, viname) && atoi(status)==0) {
						del_virtual_interface(atoi(radio_index), atoi(profile_index));
					}
					else if(!strstr(vifs, viname) && atoi(status)==1) {
						add_virtual_interface(atoi(radio_index), atoi(profile_index));
					}
					else if((!strstr(vifs, viname) && atoi(status)==0) || atoi(profile_index)>3) {
						continue;
					}
					
				}
				if((key = cJSON_GetObjectItem(item, "hide-network-name")) != NULL && key->valuestring != NULL) {
					set_status(atoi(radio_index), atoi(profile_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);	
				}
				if((key = cJSON_GetObjectItem(item, "ssid")) != NULL && key->valuestring != NULL) {
					set_ssid(atoi(radio_index), atoi(profile_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
						
				}
				if((key = cJSON_GetObjectItem(item, "authentication")) != NULL && key->valuestring != NULL) {
					
					set_security_mode(atoi(radio_index), atoi(profile_index), authentication_keywords[parse_authentication_keywords(key->valuestring)].value, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				
				if((key = cJSON_GetObjectItem(item, "encryption")) != NULL && key->valuestring != NULL) {
					set_crypto(atoi(radio_index), atoi(profile_index), encryption_keywords[parse_encryption_keywords(key->valuestring)].value, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}

				if((key = cJSON_GetObjectItem(item, "presharedkey")) != NULL && key->valuestring != NULL) {
					set_wpa_psk(atoi(radio_index), atoi(profile_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				flag =1;
 			}
			
		}
		
 	}
 	{
 		//apply_setting();
 		/*
 		int result;
		pthread_t tid_init_service = 0;
		config->upgrade_lock = 1;
 		debug(LOG_INFO, "create a new thread (thread_init_service)");
		result = pthread_create(&tid_init_service, NULL, (void *)thread_init_service, NULL);
		if (result != 0) {
			debug(LOG_ERR, "FATAL: Failed to create a new thread (init_service) - exiting");
			exit(1);
		}*/
		system("nvram commit");
		
 	}
 
	if(strlen(ErrMesg)) {
		create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "1000", ErrMesg, config->sn, http_packet);
	}
	else {
		create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "success", "0", NULL, config->sn, http_packet);
	}
	if(flag) {
		safe_encrypt_http_send(config->httpfd, http_packet, strlen(http_packet), 0); 
		shutdown(config->httpfd, SHUT_RDWR);
		system("reboot");
	}
		
			
}
Exemple #2
0
int main(int argc, char *argv[])
{
	char buff[1024];
	size_t len;
	fd_set rfds;
	struct timeval tv;
	struct knet_host_search print_search;
	int logpipefd[2];

	if (argc < 3) {
		print_usage(argv[0]);
		exit(EXIT_FAILURE);
	}

	if (socketpair(AF_UNIX, SOCK_STREAM, IPPROTO_IP, knet_sock) != 0) {
		printf("Unable to create socket\n");
		exit(EXIT_FAILURE);
	}

	if (pipe(logpipefd)) {
		printf("Unable to create log pipe\n");
		exit(EXIT_FAILURE);
	}

	knet_h = NULL;

	if (signal(SIGINT, sigint_handler) == SIG_ERR) {
		printf("Unable to configure SIGINT handler\n");
		exit(EXIT_FAILURE);
	}

	set_debug(argc, argv);

	memset(&knet_handle_cfg, 0, sizeof(struct knet_handle_cfg));
	knet_handle_cfg.to_net_fd = knet_sock[0];
	knet_handle_cfg.node_id = 1;
	knet_handle_cfg.log_fd = logpipefd[1];
	knet_handle_cfg.default_log_level = loglevel;

	if ((knet_h = knet_handle_new(&knet_handle_cfg)) == NULL) {
		printf("Unable to create new knet_handle_t\n");
		exit(EXIT_FAILURE);
	}

	if (set_crypto(argc, argv)) {
		memset(knet_handle_crypto_cfg.private_key, 0, KNET_MAX_KEY_LEN);
		knet_handle_crypto_cfg.private_key_len = KNET_MAX_KEY_LEN;	
		if (knet_handle_crypto(knet_h, &knet_handle_crypto_cfg)) {
			printf("Unable to init crypto\n");
			exit(EXIT_FAILURE);
		}
	} else {
		printf("Crypto not activated\n");
	}

	argv_to_hosts(argc, argv);
	knet_handle_setfwd(knet_h, 1);	

	while (1) {
		ssize_t wlen;
		knet_host_foreach(knet_h, print_link, &print_search);

		printf("Sending 'Hello World!' frame\n");
		wlen = write(knet_sock[1], "Hello World!", 13);
		if (wlen != 13)
			printf("Unable to send Hello World! to socket!\n");

		tv.tv_sec = 5;
		tv.tv_usec = 0;

 select_loop:
		FD_ZERO(&rfds);
		FD_SET(knet_sock[1], &rfds);
		FD_SET(logpipefd[0], &rfds);

		len = select(FD_SETSIZE, &rfds, NULL, NULL, &tv);

		/* uncomment this to replicate the one-message problem */
		/* usleep(500000); */

		if (len < 0) {
			printf("Unable select over knet_handle_t\n");
			exit(EXIT_FAILURE);
		} else if (FD_ISSET(knet_sock[1], &rfds)) {
			len = read(knet_sock[1], buff, sizeof(buff));
			printf("Received data (%zu bytes): '%s'\n", len, buff);
		} else if (FD_ISSET(logpipefd[0], &rfds)) {
			struct knet_log_msg msg;
			size_t bytes_read = 0;

			while (bytes_read < sizeof(struct knet_log_msg)) {
				len = read(logpipefd[0], &msg + bytes_read,
					   sizeof(struct knet_log_msg) - bytes_read);
				if (len <= 0) {
					printf("Error from log fd, unable to read data\n");
					exit(EXIT_FAILURE);
				}
				bytes_read += len;
			}

			printf("[%s] %s: %s\n",
			       knet_get_loglevel_name(msg.msglevel),
			       knet_get_subsystem_name(msg.subsystem),
			       msg.msg);
		}

		if ((tv.tv_sec > 0) || (tv.tv_usec > 0))
			goto select_loop;
	}

	/* FIXME: allocated hosts should be free'd */

	return 0;
}