command.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <stdbool.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <json-c/json.h>
  8. #include <json-c/json_util.h>
  9. #include <json-c/json_object.h>
  10. #include <mosquitto.h>
  11. #include "common.h"
  12. #include "command.h"
  13. static volatile bool run =true;
  14. bool auth_user()
  15. {
  16. #if ENABLE_HTTP_COOKIE
  17. char*info=NULL;
  18. struct json_object *proot=NULL, *cookied_obj =NULL, *resp_obj = NULL;
  19. const char *cookied=NULL;
  20. const char *resp_string=NULL;
  21. char id[100];
  22. info = getenv("HTTP_COOKIE");
  23. if(info == NULL)
  24. {
  25. printf("%s\n\n","Content-Type:application/json;charset=UTF-8");
  26. resp_obj = json_object_new_object();
  27. json_object_object_add(resp_obj, "status", json_object_new_string("expired"));
  28. resp_string = json_object_to_json_string(resp_obj);
  29. printf("%s",resp_string);
  30. json_object_put(resp_obj);
  31. return false;
  32. }
  33. else
  34. {
  35. sscanf(info,"cookied=%s",id);
  36. proot = json_object_from_file("./cookie/cookied.json");
  37. if(proot == NULL)
  38. {
  39. printf("%s\n\n","Content-Type:application/json;charset=UTF-8");
  40. resp_obj = json_object_new_object();
  41. json_object_object_add(resp_obj, "status", json_object_new_string("expired"));
  42. resp_string = json_object_to_json_string(resp_obj);
  43. printf("%s",resp_string);
  44. json_object_put(resp_obj);
  45. return false;
  46. }
  47. json_object_object_get_ex(proot, "cookied",&cookied_obj);
  48. cookied = json_object_get_string(cookied_obj);
  49. if(strncmp(cookied, id, STR_MAX_LEN(cookied, id)) != 0)
  50. {
  51. json_object_put(proot);
  52. printf("%s\n\n","Content-Type:application/json;charset=UTF-8");
  53. resp_obj = json_object_new_object();
  54. json_object_object_add(resp_obj, "status", json_object_new_string("expired"));
  55. resp_string = json_object_to_json_string(resp_obj);
  56. printf("%s",resp_string);
  57. json_object_put(resp_obj);
  58. return false;
  59. }
  60. json_object_put(proot);
  61. }
  62. #endif
  63. return true;
  64. }
  65. void http_connect_callback(struct mosquitto *mosq, void *obj, int rc)
  66. {
  67. run = true;
  68. }
  69. void http_disconnect_callback(struct mosquitto *mosq, void *obj, int result)
  70. {
  71. run = false;
  72. }
  73. void http_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
  74. {
  75. printf("%s\n\n","Content-Type:application/json;charset=UTF-8");
  76. printf("%s",msg->payload);
  77. run =false;
  78. }
  79. void http_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
  80. {
  81. run = true;
  82. }
  83. void get_configure_by_type(char *pub_topic, char *sub_topic)
  84. {
  85. struct mosquitto *http_mosq;
  86. char*info=NULL;
  87. int max=0;
  88. char id[50];
  89. #if ENABLE_HTTP_COOKIE
  90. if (auth_user() ==false)
  91. return;
  92. #endif
  93. mosquitto_lib_init();
  94. snprintf(id, 50, "get_configure_%d", getpid());
  95. http_mosq = mosquitto_new(id, true, NULL);
  96. mosquitto_connect_callback_set(http_mosq, http_connect_callback);
  97. mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback);
  98. mosquitto_message_callback_set(http_mosq, http_message_callback);
  99. mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback);
  100. mosquitto_connect(http_mosq, "localhost", 1883, 600);
  101. mosquitto_subscribe(http_mosq, NULL, sub_topic, 1);
  102. mosquitto_publish(http_mosq, NULL, pub_topic, 0, "", 1, false);
  103. while(run ==true && max < MAX_TIMEOUT)
  104. {
  105. mosquitto_loop(http_mosq, 500, 1);
  106. max++;
  107. }
  108. mosquitto_disconnect(http_mosq);
  109. mosquitto_destroy(http_mosq);
  110. mosquitto_lib_cleanup();
  111. }
  112. void set_configure_by_type(char *pub_topic ,char *sub_topic)
  113. {
  114. char *info=NULL, *p=NULL, *pRequestMethod=NULL, *inputbuffer=NULL;
  115. int ContentLength,x,i=0;
  116. struct mosquitto *http_mosq;
  117. int max=0;
  118. char id[50];
  119. #if ENABLE_HTTP_COOKIE
  120. if (auth_user() ==false)
  121. return;
  122. #endif
  123. setvbuf(stdin, NULL , _IONBF, 0);
  124. pRequestMethod = getenv("REQUEST_METHOD");
  125. if(pRequestMethod == NULL)
  126. {
  127. printf("<p> request = null </p>");
  128. return;
  129. }
  130. if(strcasecmp(pRequestMethod, "POST") == 0)
  131. {
  132. p = getenv("CONTENT_LENGTH");
  133. if(p!=NULL)
  134. {
  135. ContentLength =atoi(p);
  136. }
  137. else
  138. {
  139. ContentLength = 0;
  140. }
  141. inputbuffer = (char *)malloc(ContentLength + 1);
  142. while(i < ContentLength)
  143. {
  144. x = fgetc(stdin);
  145. if(x == EOF)
  146. break;
  147. *(inputbuffer+i) =x;
  148. i++;
  149. }
  150. *(inputbuffer+i) = '\0';
  151. ContentLength = i;
  152. mosquitto_lib_init();
  153. snprintf(id, 50, "set_configure_%d", getpid());
  154. http_mosq = mosquitto_new(id, true, NULL);
  155. mosquitto_connect_callback_set(http_mosq, http_connect_callback);
  156. mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback);
  157. mosquitto_message_callback_set(http_mosq, http_message_callback);
  158. mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback);
  159. mosquitto_connect(http_mosq, "localhost", 1883, 600);
  160. mosquitto_subscribe(http_mosq, NULL, sub_topic, 1);
  161. mosquitto_publish(http_mosq, NULL, pub_topic, ContentLength, inputbuffer, 1, false);
  162. while(run ==true && max < MAX_TIMEOUT)
  163. {
  164. mosquitto_loop(http_mosq, 500, 1);
  165. max++;
  166. }
  167. mosquitto_disconnect(http_mosq);
  168. mosquitto_destroy(http_mosq);
  169. mosquitto_lib_cleanup();
  170. free(inputbuffer);
  171. }
  172. }
  173. void del_configure_by_type(char *pub_topic,char *sub_topic)
  174. {
  175. char *info=NULL, *p=NULL, *pRequestMethod=NULL, *inputbuffer=NULL;
  176. int ContentLength,x,i=0;
  177. struct mosquitto *http_mosq;
  178. int max=0;
  179. char id[50];
  180. #if ENABLE_HTTP_COOKIE
  181. if (auth_user() ==false)
  182. return;
  183. #endif
  184. setvbuf(stdin, NULL , _IONBF, 0);
  185. pRequestMethod = getenv("REQUEST_METHOD");
  186. if(pRequestMethod == NULL)
  187. {
  188. printf("<p> request = null </p>");
  189. return;
  190. }
  191. if(strcasecmp(pRequestMethod, "POST") == 0)
  192. {
  193. p = getenv("CONTENT_LENGTH");
  194. if(p!=NULL)
  195. {
  196. ContentLength =atoi(p);
  197. }
  198. else
  199. {
  200. ContentLength = 0;
  201. }
  202. inputbuffer = (char *)malloc(ContentLength + 1);
  203. while(i < ContentLength)
  204. {
  205. x = fgetc(stdin);
  206. if(x == EOF)
  207. break;
  208. *(inputbuffer+i) =x;
  209. i++;
  210. }
  211. *(inputbuffer+i) = '\0';
  212. ContentLength = i;
  213. mosquitto_lib_init();
  214. snprintf(id, 50, "del_configure_%d", getpid());
  215. http_mosq = mosquitto_new(id, true, NULL);
  216. mosquitto_connect_callback_set(http_mosq, http_connect_callback);
  217. mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback);
  218. mosquitto_message_callback_set(http_mosq, http_message_callback);
  219. mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback);
  220. mosquitto_connect(http_mosq, "localhost", 1883, 600);
  221. mosquitto_subscribe(http_mosq, NULL, sub_topic, 1);
  222. mosquitto_publish(http_mosq, NULL, pub_topic, ContentLength, inputbuffer, 1, false);
  223. while(run ==true && max < MAX_TIMEOUT)
  224. {
  225. mosquitto_loop(http_mosq, 500, 1);
  226. max++;
  227. }
  228. mosquitto_disconnect(http_mosq);
  229. mosquitto_destroy(http_mosq);
  230. mosquitto_lib_cleanup();
  231. free(inputbuffer);
  232. }
  233. }
  234. void add_configure_by_type(char *pub_topic , char *sub_topic)
  235. {
  236. char *info=NULL, *p=NULL, *pRequestMethod=NULL, *inputbuffer=NULL;
  237. int ContentLength,x,i=0;
  238. struct mosquitto *http_mosq;
  239. int max =0;
  240. char id[50];
  241. #if ENABLE_HTTP_COOKIE
  242. if (auth_user() ==false)
  243. return;
  244. #endif
  245. setvbuf(stdin, NULL , _IONBF, 0);
  246. pRequestMethod = getenv("REQUEST_METHOD");
  247. if(pRequestMethod == NULL)
  248. {
  249. printf("<p> request = null </p>");
  250. return;
  251. }
  252. if(strcasecmp(pRequestMethod, "POST") == 0)
  253. {
  254. p = getenv("CONTENT_LENGTH");
  255. if(p!=NULL)
  256. {
  257. ContentLength =atoi(p);
  258. }
  259. else
  260. {
  261. ContentLength = 0;
  262. }
  263. inputbuffer = (char *)malloc(ContentLength + 1);
  264. while(i < ContentLength)
  265. {
  266. x = fgetc(stdin);
  267. if(x == EOF)
  268. break;
  269. *(inputbuffer+i) =x;
  270. i++;
  271. }
  272. *(inputbuffer+i) = '\0';
  273. ContentLength = i;
  274. mosquitto_lib_init();
  275. snprintf(id, 50, "add_configure_%d", getpid());
  276. http_mosq = mosquitto_new(id, true, NULL);
  277. mosquitto_connect_callback_set(http_mosq, http_connect_callback);
  278. mosquitto_disconnect_callback_set(http_mosq, http_disconnect_callback);
  279. mosquitto_message_callback_set(http_mosq, http_message_callback);
  280. mosquitto_subscribe_callback_set(http_mosq, http_subscribe_callback);
  281. mosquitto_connect(http_mosq, "localhost", 1883, 600);
  282. mosquitto_subscribe(http_mosq, NULL, sub_topic, 1);
  283. mosquitto_publish(http_mosq, NULL, pub_topic, ContentLength, inputbuffer, 1, false);
  284. while(run ==true && max < MAX_TIMEOUT)
  285. {
  286. mosquitto_loop(http_mosq, 500, 1);
  287. max++;
  288. }
  289. mosquitto_disconnect(http_mosq);
  290. mosquitto_destroy(http_mosq);
  291. mosquitto_lib_cleanup();
  292. free(inputbuffer);
  293. }
  294. }