123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <stdbool.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <json-c/json.h>
- #include <json-c/json_util.h>
- #include <json-c/json_object.h>
- #include <mosquitto.h>
- #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("<p> request = null </p>");
- 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("<p> request = null </p>");
- 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("<p> request = null </p>");
- 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);
- }
- }
|