liudf 20170223 add wifidog mqtt related features; no compiled pass

This commit is contained in:
Dengfeng Liu 2017-02-23 20:21:42 +08:00
parent ae86ced5f0
commit 8168bbdb1f
5 changed files with 427 additions and 122 deletions

View File

@ -34,11 +34,12 @@ request:
response:
if these request has response, its format:
{response:"response_code"}
{response:"response_code",msg:"return message"}
response_code list
200 Ok
404 the request is not support
403 the request is forbidden
400 the request is bad
500 when execute the request, encounter internal error
for example, set pdomain baidu.com taobao.com qq.com for device 61ACC80118B6
@ -51,4 +52,4 @@ its ok response should be
topic
wifidog/61ACC80118B6/response/110
message
{response:"200"}
{response:"200",msg:"ok"}

View File

@ -36,22 +36,145 @@
#include <mosquitto.h>
#include "mqtt_thread.h"
#include "wdctl_thread.h"
#include "conf.h"
#include "debug.h"
static void
mqtt_response(struct mosquitto *mosq, const unsigned int req_id, const char *response_data, s_config *config)
send_mqtt_response(struct mosquitto *mosq, const unsigned int req_id, int res_id, const char *msg, const s_config *config)
{
char *topic = NULL;
char *res_data = NULL;
safe_asprintf(&topic, "wifidog/%s/response/%d", config->gw_id, req_id);
safe_asprintf(&res_data, "{response:\"%d\",msg:\"%s\"}", res_id, msg==NULL?"null":msg);
mosquitto_publish(mosq, NULL, topic, strlen(response_data), mqtt_response, 0, false);
free(topic);
free(res_data);
}
static void
void
set_trusted_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config)
{
if (!type || !value) {
debug(LOG_INFO, "set trusted operation, type or value is NULL");
send_mqtt_response(mosq, req_id, 400, "type or value is NULL", config);
return;
}
for(int i = 0; mqtt_set_type[i].type != NULL; i++) {
if (strcmp(mqtt_set_type[i].type, type) == 0) {
debug(LOG_DEBUG, "set trusted operation, type is %s value is %s", type, value);
mqtt_set_type[i].process_mqtt_set_type(value);
send_mqtt_response(mosq, req_id, 200, "Ok", config);
break;
}
}
}
void
del_trusted_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config)
{
if (!type || !value) {
debug(LOG_INFO, "del trusted operation, type or value is NULL");
send_mqtt_response(mosq, req_id, 400, "type or value is NULL", config);
return;
}
for(int i = 0; mqtt_del_type[i].type != NULL; i++) {
if (strcmp(mqtt_del_type[i].type, type) == 0) {
debug(LOG_DEBUG, "del trusted operation, type is %s value is %s", type, value);
mqtt_del_type[i].process_mqtt_del_type(value);
send_mqtt_response(mosq, req_id, 200, "Ok", config);
break;
}
}
}
void
clear_trusted_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config)
{
if (!type) {
debug(LOG_INFO, "clear trusted operaion, type is NULL");
send_mqtt_response(mosq, req_id, 400, "type is NULL", config);
return;
}
for(int i = 0; mqtt_clear_type[i].type != NULL; i++) {
if (strcmp(mqtt_clear_type[i].type, type) == 0) {
debug(LOG_DEBUG, "clear trusted operation, type is %s", type);
mqtt_clear_type[i].process_mqtt_clear_type();
send_mqtt_response(mosq, req_id, 200, "Ok", config);
break;
}
}
}
void
show_trusted_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config)
{
if (!type) {
debug(LOG_INFO, "show trusted operaion, type is NULL");
send_mqtt_response(mosq, req_id, 400, "type is NULL", config);
return;
}
for(int i = 0; mqtt_show_type[i].type != NULL; i++) {
if (strcmp(mqtt_show_type[i].type, type) == 0) {
debug(LOG_DEBUG, "show trusted operation, type is %s", type);
char *msg = mqtt_show_type[i].process_mqtt_show_type();
send_mqtt_response(mosq, req_id, 200, msg, config);
if (msg)
free(msg);
break;
}
}
}
void
save_rule_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config)
{
user_cfg_save();
send_mqtt_response(mosq, req_id, 200, "Ok", config);
}
void
get_status_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config)
{
}
void
reboot_device_op(void *mosq, const char *type, const char *value, const int req_id, s_config *config)
{
system("reboot");
}
static void
process_mqtt_reqeust(struct mosquitto *mosq, const unsigned int req_id, const char *data, s_config *config)
{
json_object *json_request = json_tokener_parse(data);
char *op = NULL;
if (!json_request) {
debug(LOG_INFO, "user request is not valid");
return;
}
char *op = json_object_get_string(json_object_object_get(json_request, "op"));
if (!op) {
debug(LOG_INFO, "No op item get");
return;
}
int i = 0;
for (; mqtt_op[i].operation != NULL; i++) {
if (strcmp(op, mqtt_op[i].op) == 0 && mqtt_op[i].process_mqtt_reqeust) {
char *type = json_object_get_string(json_object_object_get(json_request, "type"));
char *value = json_object_get_string(json_object_object_get(json_request, "value"));
mqtt_op[i].process_mqtt_reqeust(mosq, type, value, req_id, config);
break;
}
}
}
static unsigned int
@ -72,8 +195,11 @@ mqtt_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_
if (message->payloadlen) {
debug(LOG_DEBUG, "topic is %s, data is %s", message->topic, message->payload);
unsigned int req_id = get_topic_req_id(message->topic);
if (req_id)
if (req_id) {
process_mqtt_reqeust(mosq, req_id, message->payload, config);
}
else
debug(LOG_INFO, "error: can not get req_id");
}
}

View File

@ -28,6 +28,74 @@
#ifndef _MQTT_THREAD_H_
#define _MQTT_THREAD_H_
#include "wdctl_thread.h"
void set_trusted_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config);
void del_trusted_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config);
void clear_trusted_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config);
void show_trusted_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config);
void save_rule_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config);
void get_status_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config);
void reboot_device_op(void *mosq, const char *type, const char *value, const int req_id, const s_config *config);
struct wifidog_mqtt_op {
char *operation,
void (*process_mqtt_op)(void *mosq, int type, const char *value, const int req_id, const s_config *config)
} mqtt_op [] = {
{"set_trusted", set_trusted_op},
{"del_trusted", del_trusted_op},
{"clear_trusted", clear_trusted_op},
{"show_trusted", show_trusted_op},
{"save_rule", save_rule_op},
{"get_status", get_status_op},
{"reboot", reboot_device_op},
{NULL, NULL}
};
struct wifidog_mqtt_add_type {
char *type,
void (*process_mqtt_set_type)(const char *args)
} mqtt_set_type [] = {
{"domain", add_trusted_domains},
{"pdomain", add_trusted_pdomains},
{"ip", add_trusted_iplist},
{"mac", add_trusted_maclist},
{NULL, NULL}
};
struct wifidog_mqtt_del_type {
char *type,
void (*process_mqtt_del_type)(const char *args)
} mqtt_del_type [] = {
{"domain", del_trusted_domains},
{"pdomain", del_trusted_pdomains},
{"ip", del_trusted_iplist},
{"mac", del_trusted_maclist},
{NULL, NULL}
};
struct wifidog_mqtt_clear_type {
char *type,
void (*process_mqtt_clear_type)(void)
} mqtt_clear_type [] = {
{"domain", clear_trusted_domains},
{"pdomain", clear_trusted_pdomains},
{"ip", clear_trusted_iplist},
{"mac", clear_trusted_maclist},
{NULL, NULL}
};
struct wifidog_mqtt_show_type {
char *type,
void (*process_mqtt_show_type)(void)
} mqtt_show_type [] = {
{"domain", show_trusted_domains},
{"pdomains", show_trusted_pdomains},
{"ip", show_trusted_iplist},
{"mac", show_trusted_maclist},
{NULL, NULL}
};
void thread_mqtt(void *arg);
#endif

View File

@ -503,32 +503,40 @@ wdctl_reset(int fd, const char *arg)
}
//>>> liudf added 20151225
void add_trusted_pdomains(const char *arg)
{
debug(LOG_DEBUG, "Argument: %s ", arg);
parse_trusted_pan_domain_string(arg);
fw_set_pan_domains_trusted();
}
static void
wdctl_add_trusted_pan_domains(int fd, const char *arg)
{
debug(LOG_DEBUG, "Entering wdctl_add_trusted_pan_domains ...");
debug(LOG_DEBUG, "Argument: %s ", arg);
parse_trusted_pan_domain_string(arg);
fw_set_pan_domains_trusted();
add_trusted_pdomains(arg);
write_to_socket(fd, "Yes", 3);
debug(LOG_DEBUG, "Exiting wdctl_add_trusted_pan_domains...");
}
void del_trusted_pdomains(const char *arg)
{
debug(LOG_DEBUG, "Argument: %s ", arg);
parse_del_trusted_pan_domain_string(arg);
fw_set_pan_domains_trusted();
}
static void
wdctl_del_trusted_pan_domains(int fd, const char *arg)
{
debug(LOG_DEBUG, "Entering wdctl_del_trusted_pan_domains ...");
debug(LOG_DEBUG, "Argument: %s ", arg);
parse_del_trusted_pan_domain_string(arg);
fw_set_pan_domains_trusted();
del_trusted_pdomains(arg);
write_to_socket(fd, "Yes", 3);
@ -536,20 +544,29 @@ wdctl_del_trusted_pan_domains(int fd, const char *arg)
}
void clear_trusted_pdomains(void)
{
clear_trusted_pan_domains();
fw_clear_pan_domains_trusted();
}
static void
wdctl_clear_trusted_pan_domains(int fd)
{
debug(LOG_DEBUG, "Entering wdctl_clear_trusted_pan_domains ...");
clear_trusted_pan_domains();
fw_clear_pan_domains_trusted();
clear_trusted_pdomains();
write_to_socket(fd, "Yes", 3);
debug(LOG_DEBUG, "Exiting wdctl_clear_trusted_pan_domains...");
}
char *show_trusted_pdomains()
{
return NULL;
}
// todo
static void
wdctl_show_trusted_pan_domains(int fd)
@ -557,6 +574,17 @@ wdctl_show_trusted_pan_domains(int fd)
write_to_socket(fd, "Yes", 3);
}
char *show_trusted_iplist()
{
return NULL;
}
void add_trusted_iplist(const char *arg)
{
add_trusted_ip_list(arg);
fw_refresh_user_domains_trusted();
}
static void
wdctl_add_trusted_iplist(int fd, const char *arg)
{
@ -564,10 +592,7 @@ wdctl_add_trusted_iplist(int fd, const char *arg)
debug(LOG_DEBUG, "Argument: %s ", arg);
debug(LOG_DEBUG, "parse trusted ip list");
add_trusted_ip_list(arg);
fw_refresh_user_domains_trusted();
add_trusted_iplist(arg);
write_to_socket(fd, "Yes", 3);
@ -575,6 +600,12 @@ wdctl_add_trusted_iplist(int fd, const char *arg)
}
void del_trusted_iplist(const char *arg)
{
add_trusted_ip_list(arg);
fw_refresh_user_domains_trusted();
}
static void
wdctl_del_trusted_iplist(int fd, const char *arg)
{
@ -582,10 +613,7 @@ wdctl_del_trusted_iplist(int fd, const char *arg)
debug(LOG_DEBUG, "Argument: %s ", arg);
debug(LOG_DEBUG, "parse&del trusted ip list");
add_trusted_ip_list(arg);
fw_refresh_user_domains_trusted();
del_trusted_iplist(arg);
write_to_socket(fd, "Yes", 3);
@ -593,21 +621,31 @@ wdctl_del_trusted_iplist(int fd, const char *arg)
}
void clear_trusted_iplist(void)
{
clear_trusted_ip_list();
fw_refresh_user_domains_trusted();
}
static void
wdctl_clear_trusted_iplist(int fd)
{
debug(LOG_DEBUG, "Entering wdctl_clear_trusted_domains...");
clear_trusted_ip_list();
fw_refresh_user_domains_trusted();
clear_trusted_iplist();
write_to_socket(fd, "Yes", 3);
debug(LOG_DEBUG, "Exiting wdctl_clear_trusted_domains...");
}
void add_trusted_domains(const char *arg)
{
parse_user_trusted_domain_string(arg);
parse_user_trusted_domain_list();
fw_refresh_user_domains_trusted();
}
static void
wdctl_add_trusted_domains(int fd, const char *arg)
{
@ -616,33 +654,29 @@ wdctl_add_trusted_domains(int fd, const char *arg)
debug(LOG_DEBUG, "Argument: %s ", arg);
debug(LOG_DEBUG, "parse trusted domains");
parse_user_trusted_domain_string(arg);
debug(LOG_DEBUG, "parse trusted domains ip");
parse_user_trusted_domain_list();
add_trusted_domains(arg);
write_to_socket(fd, "Yes", 3);
fw_refresh_user_domains_trusted();
debug(LOG_DEBUG, "Exiting wdctl_add_trusted_domains...");
}
void del_trusted_domains(const char *arg)
{
parse_del_trusted_domain_string(arg);
fw_refresh_user_domains_trusted();
}
static void
wdctl_del_trusted_domains(int fd, const char *arg)
{
debug(LOG_DEBUG, "Entering wdctl_del_trusted_domains...");
debug(LOG_DEBUG, "Argument: %s ", arg);
debug(LOG_DEBUG, "parse & del trusted domains");
parse_del_trusted_domain_string(arg);
del_trusted_domains(arg);
write_to_socket(fd, "Yes", 3);
fw_refresh_user_domains_trusted();
write_to_socket(fd, "Yes", 3);
debug(LOG_DEBUG, "Exiting wdctl_del_trusted_domains...");
}
@ -662,21 +696,30 @@ wdctl_reparse_trusted_domains(int fd)
debug(LOG_DEBUG, "Exiting wdctl_reparse_trusted_domains...");
}
void clear_trusted_domains()
{
clear_trusted_domains();
fw_refresh_user_domains_trusted();
}
static void
wdctl_clear_trusted_domains(int fd)
{
debug(LOG_DEBUG, "Entering wdctl_clear_trusted_domains...");
clear_trusted_domains();
fw_refresh_user_domains_trusted();
write_to_socket(fd, "Yes", 3);
debug(LOG_DEBUG, "Exiting wdctl_clear_trusted_domains...");
}
char *show_trusted_domains(void)
{
return get_trusted_domains_text();
}
static void
wdctl_show_trusted_domains(int fd)
{
@ -691,12 +734,16 @@ wdctl_show_trusted_domains(int fd)
free(status);
}
void add_domain_ip(const char *args)
{
add_domain_ip_pair(args, USER_TRUSTED_DOMAIN);
fw_refresh_user_domains_trusted();
}
static void
wdctl_add_domain_ip(int fd, const char *args)
{
add_domain_ip_pair(args, USER_TRUSTED_DOMAIN);
fw_refresh_user_domains_trusted();
add_domain_ip(args);
write_to_socket(fd, "Yes", 3);
}
@ -748,6 +795,13 @@ wdctl_clear_roam_maclist(int fd)
write_to_socket(fd, "Yes", 3);
}
void del_trusted_maclist(const char *args)
{
parse_del_trusted_mac_list(args);
fw_clear_trusted_maclist();
fw_set_trusted_maclist();
}
// trusted maclist
static void
@ -756,38 +810,41 @@ wdctl_del_trusted_maclist(int fd, const char *args)
debug(LOG_DEBUG, "Entering wdctl_del_trusted_maclist...");
debug(LOG_DEBUG, "Argument: %s ", args);
debug(LOG_DEBUG, "parse&del trusted maclist");
parse_del_trusted_mac_list(args);
fw_clear_trusted_maclist();
fw_set_trusted_maclist();
del_trusted_maclist(args);
write_to_socket(fd, "Yes", 3);
debug(LOG_DEBUG, "Exiting wdctl_add_trusted_maclist...");
}
void add_trusted_maclist(const char *args)
{
parse_trusted_mac_list(args);
fw_clear_trusted_maclist();
fw_set_trusted_maclist();
}
static void
wdctl_add_trusted_maclist(int fd, const char *args)
{
debug(LOG_DEBUG, "Entering wdctl_add_trusted_maclist...");
debug(LOG_DEBUG, "Argument: %s ", args);
debug(LOG_DEBUG, "parse trusted maclist");
parse_trusted_mac_list(args);
fw_clear_trusted_maclist();
fw_set_trusted_maclist();
add_trusted_maclist(args);
write_to_socket(fd, "Yes", 3);
debug(LOG_DEBUG, "Exiting wdctl_add_trusted_maclist...");
}
char *show_trusted_maclist()
{
return get_trusted_maclist_text();
}
static void
wdctl_show_trusted_maclist(int fd)
{
@ -802,16 +859,30 @@ wdctl_show_trusted_maclist(int fd)
free(status);
}
void
clear_trusted_maclist(void)
{
clear_trusted_mac_list();
fw_clear_trusted_maclist();
}
static void
wdctl_clear_trusted_maclist(int fd)
{
clear_trusted_mac_list();
fw_clear_trusted_maclist();
clear_trusted_maclist();
write_to_socket(fd, "Yes", 3);
}
void
del_untrusted_maclist(const char *args)
{
parse_del_untrusted_mac_list(args);
fw_clear_untrusted_maclist();
fw_set_untrusted_maclist();
}
// untrusted maclist
static void
wdctl_del_untrusted_maclist(int fd, const char *args)
@ -819,18 +890,23 @@ wdctl_del_untrusted_maclist(int fd, const char *args)
debug(LOG_DEBUG, "Entering wdctl_del_untrusted_maclist...");
debug(LOG_DEBUG, "Argument: %s ", args);
debug(LOG_DEBUG, "parse&del untrusted maclist");
parse_del_untrusted_mac_list(args);
fw_clear_untrusted_maclist();
fw_set_untrusted_maclist();
del_untrusted_maclist(args);
write_to_socket(fd, "Yes", 3);
debug(LOG_DEBUG, "Exiting wdctl_add_untrusted_maclist...");
}
void
add_untrusted_maclist(const char *args)
{
parse_untrusted_mac_list(args);
fw_clear_untrusted_maclist();
fw_set_untrusted_maclist();
}
static void
wdctl_add_untrusted_maclist(int fd, const char *args)
{
@ -838,11 +914,7 @@ wdctl_add_untrusted_maclist(int fd, const char *args)
debug(LOG_DEBUG, "Argument: %s ", args);
debug(LOG_DEBUG, "parse untrusted maclist");
parse_untrusted_mac_list(args);
fw_clear_untrusted_maclist();
fw_set_untrusted_maclist();
add_untrusted_maclist(args);
write_to_socket(fd, "Yes", 3);
@ -863,62 +935,73 @@ wdctl_show_untrusted_maclist(int fd)
free(status);
}
void clear_untrusted_maclist(void)
{
clear_untrusted_mac_list();
fw_clear_untrusted_maclist();
}
static void
wdctl_clear_untrusted_maclist(int fd)
{
clear_untrusted_mac_list();
fw_clear_untrusted_maclist();
clear_untrusted_maclist();
write_to_socket(fd, "Yes", 3);
}
void
user_cfg_save(void)
{
const char *trusted_maclist = NULL,
*untrusted_maclist = NULL,
*trusted_domains = NULL,
*trusted_pan_domains = NULL,
*trusted_iplist = NULL;
iptables_fw_save_online_clients();
trusted_maclist = get_serialize_maclist(TRUSTED_MAC);
untrusted_maclist = get_serialize_maclist(UNTRUSTED_MAC);
trusted_domains = get_serialize_trusted_domains();
trusted_iplist = get_serialize_iplist();
trusted_pan_domains = get_serialize_trusted_pan_domains();
if(trusted_pan_domains) {
uci_set_value("wifidog", "wifidog", "trusted_pan_domains", trusted_pan_domains);
} else {
uci_del_value("wifidog", "wifidog", "trusted_pan_domains");
}
if(trusted_domains) {
uci_set_value("wifidog", "wifidog", "trusted_domains", trusted_domains);
} else {
uci_del_value("wifidog", "wifidog", "trusted_domains");
}
if(trusted_iplist) {
uci_set_value("wifidog", "wifidog", "trusted_iplist", trusted_iplist);
} else {
uci_del_value("wifidog", "wifidog", "trusted_iplist");
}
if(trusted_maclist) {
uci_set_value("wifidog", "wifidog", "trusted_maclist", trusted_maclist);
} else {
uci_del_value("wifidog", "wifidog", "trusted_maclist");
}
if(untrusted_maclist) {
uci_set_value("wifidog", "wifidog", "untrusted_maclist", untrusted_maclist);
} else {
uci_del_value("wifidog", "wifidog", "untrusted_maclist");
}
}
static void
wdctl_user_cfg_save(int fd)
{
const char *trusted_maclist = NULL,
*untrusted_maclist = NULL,
*trusted_domains = NULL,
*trusted_pan_domains = NULL,
*trusted_iplist = NULL;
iptables_fw_save_online_clients();
trusted_maclist = get_serialize_maclist(TRUSTED_MAC);
untrusted_maclist = get_serialize_maclist(UNTRUSTED_MAC);
trusted_domains = get_serialize_trusted_domains();
trusted_iplist = get_serialize_iplist();
trusted_pan_domains = get_serialize_trusted_pan_domains();
if(trusted_pan_domains) {
uci_set_value("wifidog", "wifidog", "trusted_pan_domains", trusted_pan_domains);
} else {
uci_del_value("wifidog", "wifidog", "trusted_pan_domains");
}
if(trusted_domains) {
uci_set_value("wifidog", "wifidog", "trusted_domains", trusted_domains);
} else {
uci_del_value("wifidog", "wifidog", "trusted_domains");
}
if(trusted_iplist) {
uci_set_value("wifidog", "wifidog", "trusted_iplist", trusted_iplist);
} else {
uci_del_value("wifidog", "wifidog", "trusted_iplist");
}
if(trusted_maclist) {
uci_set_value("wifidog", "wifidog", "trusted_maclist", trusted_maclist);
} else {
uci_del_value("wifidog", "wifidog", "trusted_maclist");
}
if(untrusted_maclist) {
uci_set_value("wifidog", "wifidog", "untrusted_maclist", untrusted_maclist);
} else {
uci_del_value("wifidog", "wifidog", "untrusted_maclist");
}
user_cfg_save();
write_to_socket(fd, "Yes", 3);
}

View File

@ -35,4 +35,31 @@ void thread_wdctl(void *arg);
void close_wdctl_socket();
void user_cfg_save(void);
void clear_untrusted_maclist(void);
void add_untrusted_maclist(const char *args);
void del_untrusted_maclist(const char *args);
char *show_trusted_maclist(void);
void clear_trusted_maclist(void);
void add_trusted_maclist(const char *args);
void del_trusted_maclist(const char *args);
char *show_trusted_domains(void);
void clear_trusted_domains(void);
void add_trusted_domains(const char *args);
void del_trusted_domains(const char *args);
char *show_trusted_iplist(void);
void clear_trusted_iplist(void);
void del_trusted_iplist(const char *args);
void add_trusted_iplist(const char *args);
char *show_trusted_pdomains(void);
void clear_trusted_pdomains(void);
void add_trusted_pdomains(const char *args);
void del_trusted_pdomains(const char *args);
#endif