#include #include #include #include #include #include #include #include #include #include #include "common.h" #include "command.h" static volatile bool run =true; bool auth_user() { #if ENABLE_HTTP_COOKIE char*info=NULL; struct json_object *proot=NULL, *cookied_obj =NULL, *resp_obj = NULL; const char *cookied=NULL; const char *resp_string=NULL; char id[100]; info = getenv("HTTP_COOKIE"); if(info == NULL) { printf("%s\n\n","Content-Type:application/json;charset=UTF-8"); resp_obj = json_object_new_object(); json_object_object_add(resp_obj, "status", json_object_new_string("expired")); resp_string = json_object_to_json_string(resp_obj); printf("%s",resp_string); json_object_put(resp_obj); return false; } else { sscanf(info,"cookied=%s",id); proot = json_object_from_file("./cookie/cookied.json"); if(proot == NULL) { printf("%s\n\n","Content-Type:application/json;charset=UTF-8"); resp_obj = json_object_new_object(); json_object_object_add(resp_obj, "status", json_object_new_string("expired")); resp_string = json_object_to_json_string(resp_obj); printf("%s",resp_string); json_object_put(resp_obj); return false; } json_object_object_get_ex(proot, "cookied",&cookied_obj); cookied = json_object_get_string(cookied_obj); if(strncmp(cookied, id, STR_MAX_LEN(cookied, id)) != 0) { json_object_put(proot); printf("%s\n\n","Content-Type:application/json;charset=UTF-8"); resp_obj = json_object_new_object(); json_object_object_add(resp_obj, "status", json_object_new_string("expired")); resp_string = json_object_to_json_string(resp_obj); printf("%s",resp_string); json_object_put(resp_obj); return false; } json_object_put(proot); } #endif return true; } void http_connect_callback(struct mosquitto *mosq, void *obj, int rc) { run = true; } void http_disconnect_callback(struct mosquitto *mosq, void *obj, int result) { run = false; } void http_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) { printf("%s\n\n","Content-Type:application/json;charset=UTF-8"); printf("%s",msg->payload); run =false; } void http_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos) { run = true; } void get_configure_by_type(char *pub_topic, char *sub_topic) { struct mosquitto *http_mosq; char*info=NULL; int max=0; char id[50]; #if ENABLE_HTTP_COOKIE if (auth_user() ==false) return; #endif mosquitto_lib_init(); snprintf(id, 50, "get_configure_%d", getpid()); http_mosq = mosquitto_new(id, true, NULL); mosquitto_connect_callback_set(http_mosq, http_connect_callback); mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback); mosquitto_message_callback_set(http_mosq, http_message_callback); mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback); mosquitto_connect(http_mosq, "localhost", 1883, 600); mosquitto_subscribe(http_mosq, NULL, sub_topic, 1); mosquitto_publish(http_mosq, NULL, pub_topic, 0, "", 1, false); while(run ==true && max < MAX_TIMEOUT) { mosquitto_loop(http_mosq, 500, 1); max++; } mosquitto_disconnect(http_mosq); mosquitto_destroy(http_mosq); mosquitto_lib_cleanup(); } void set_configure_by_type(char *pub_topic ,char *sub_topic) { char *info=NULL, *p=NULL, *pRequestMethod=NULL, *inputbuffer=NULL; int ContentLength,x,i=0; struct mosquitto *http_mosq; int max=0; char id[50]; #if ENABLE_HTTP_COOKIE if (auth_user() ==false) return; #endif setvbuf(stdin, NULL , _IONBF, 0); pRequestMethod = getenv("REQUEST_METHOD"); if(pRequestMethod == NULL) { printf("

request = null

"); return; } if(strcasecmp(pRequestMethod, "POST") == 0) { p = getenv("CONTENT_LENGTH"); if(p!=NULL) { ContentLength =atoi(p); } else { ContentLength = 0; } inputbuffer = (char *)malloc(ContentLength + 1); while(i < ContentLength) { x = fgetc(stdin); if(x == EOF) break; *(inputbuffer+i) =x; i++; } *(inputbuffer+i) = '\0'; ContentLength = i; mosquitto_lib_init(); snprintf(id, 50, "set_configure_%d", getpid()); http_mosq = mosquitto_new(id, true, NULL); mosquitto_connect_callback_set(http_mosq, http_connect_callback); mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback); mosquitto_message_callback_set(http_mosq, http_message_callback); mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback); mosquitto_connect(http_mosq, "localhost", 1883, 600); mosquitto_subscribe(http_mosq, NULL, sub_topic, 1); mosquitto_publish(http_mosq, NULL, pub_topic, ContentLength, inputbuffer, 1, false); while(run ==true && max < MAX_TIMEOUT) { mosquitto_loop(http_mosq, 500, 1); max++; } mosquitto_disconnect(http_mosq); mosquitto_destroy(http_mosq); mosquitto_lib_cleanup(); free(inputbuffer); } } void del_configure_by_type(char *pub_topic,char *sub_topic) { char *info=NULL, *p=NULL, *pRequestMethod=NULL, *inputbuffer=NULL; int ContentLength,x,i=0; struct mosquitto *http_mosq; int max=0; char id[50]; #if ENABLE_HTTP_COOKIE if (auth_user() ==false) return; #endif setvbuf(stdin, NULL , _IONBF, 0); pRequestMethod = getenv("REQUEST_METHOD"); if(pRequestMethod == NULL) { printf("

request = null

"); return; } if(strcasecmp(pRequestMethod, "POST") == 0) { p = getenv("CONTENT_LENGTH"); if(p!=NULL) { ContentLength =atoi(p); } else { ContentLength = 0; } inputbuffer = (char *)malloc(ContentLength + 1); while(i < ContentLength) { x = fgetc(stdin); if(x == EOF) break; *(inputbuffer+i) =x; i++; } *(inputbuffer+i) = '\0'; ContentLength = i; mosquitto_lib_init(); snprintf(id, 50, "del_configure_%d", getpid()); http_mosq = mosquitto_new(id, true, NULL); mosquitto_connect_callback_set(http_mosq, http_connect_callback); mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback); mosquitto_message_callback_set(http_mosq, http_message_callback); mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback); mosquitto_connect(http_mosq, "localhost", 1883, 600); mosquitto_subscribe(http_mosq, NULL, sub_topic, 1); mosquitto_publish(http_mosq, NULL, pub_topic, ContentLength, inputbuffer, 1, false); while(run ==true && max < MAX_TIMEOUT) { mosquitto_loop(http_mosq, 500, 1); max++; } mosquitto_disconnect(http_mosq); mosquitto_destroy(http_mosq); mosquitto_lib_cleanup(); free(inputbuffer); } } void add_configure_by_type(char *pub_topic , char *sub_topic) { char *info=NULL, *p=NULL, *pRequestMethod=NULL, *inputbuffer=NULL; int ContentLength,x,i=0; struct mosquitto *http_mosq; int max =0; char id[50]; #if ENABLE_HTTP_COOKIE if (auth_user() ==false) return; #endif setvbuf(stdin, NULL , _IONBF, 0); pRequestMethod = getenv("REQUEST_METHOD"); if(pRequestMethod == NULL) { printf("

request = null

"); return; } if(strcasecmp(pRequestMethod, "POST") == 0) { p = getenv("CONTENT_LENGTH"); if(p!=NULL) { ContentLength =atoi(p); } else { ContentLength = 0; } inputbuffer = (char *)malloc(ContentLength + 1); while(i < ContentLength) { x = fgetc(stdin); if(x == EOF) break; *(inputbuffer+i) =x; i++; } *(inputbuffer+i) = '\0'; ContentLength = i; mosquitto_lib_init(); snprintf(id, 50, "add_configure_%d", getpid()); http_mosq = mosquitto_new(id, true, NULL); mosquitto_connect_callback_set(http_mosq, http_connect_callback); mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback); mosquitto_message_callback_set(http_mosq, http_message_callback); mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback); mosquitto_connect(http_mosq, "localhost", 1883, 600); mosquitto_subscribe(http_mosq, NULL, sub_topic, 1); mosquitto_publish(http_mosq, NULL, pub_topic, ContentLength, inputbuffer, 1, false); while(run ==true && max < MAX_TIMEOUT) { mosquitto_loop(http_mosq, 500, 1); max++; } mosquitto_disconnect(http_mosq); mosquitto_destroy(http_mosq); mosquitto_lib_cleanup(); free(inputbuffer); } }