log_udpdaemon.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "precompile.h"
  2. #include "log_udpdaemon.h"
  3. #include "log_hdr.h"
  4. #include "log_base.h"
  5. #include "log.h"
  6. #include <winpr/winsock.h>
  7. #include <winpr/synch.h>
  8. #include <winpr/thread.h>
  9. #include <winpr/string.h>
  10. typedef struct udplogdaemonfactory_t udplogdaemonfactory_t;
  11. typedef struct udplogdaemon_t {
  12. logbase_t base;
  13. char *ip;
  14. unsigned short port;
  15. struct sockaddr_in listen_addr;
  16. SOCKET listen_handle;
  17. volatile LONG stop;
  18. HANDLE run_thread;
  19. }udplogdaemon_t;
  20. struct udplogdaemonfactory_t
  21. {
  22. logfactory_t base;
  23. };
  24. static unsigned int __stdcall udplogdaemon_run(void* param)
  25. {
  26. udplogdaemon_t *log = (udplogdaemon_t *)param;
  27. for (;;) {
  28. struct sockaddr_in addr;
  29. int addrlen = sizeof(addr);
  30. char buf[MAX_LOG_LEN];
  31. int len;
  32. len = recvfrom(log->listen_handle, buf, sizeof(buf), 0, (struct sockaddr*)&addr, &addrlen);
  33. if (log->stop)
  34. break;
  35. if (len > 0 && len < MAX_LOG_LEN) {
  36. struct log_hdr *hdr;
  37. struct log_hdr_str *inst;
  38. struct log_hdr_str *msg;
  39. int msg_len;
  40. int inst_len;
  41. int n = 0;
  42. hdr = (struct log_hdr *)&buf[n];
  43. n += sizeof(struct log_hdr);
  44. inst = (struct log_hdr_str *)&buf[n];
  45. inst_len = ntohs(inst->len);
  46. n += 2 + inst_len;
  47. msg = (struct log_hdr_str*)&buf[n];
  48. msg_len = ntohs(msg->len);
  49. if (inst_len && msg_len) {
  50. msg->data[msg_len] = 0;
  51. inst->data[inst_len] = 0;
  52. xlog_log(inst->data, hdr->level, msg->data);
  53. }
  54. } else {
  55. DWORD dwError = WSAGetLastError();
  56. dwError = dwError + 0;
  57. //...
  58. }
  59. }
  60. return 0;
  61. }
  62. static void udplogdaemonfactory_destroy(void *self)
  63. {
  64. udplogdaemonfactory_t *fac = (udplogdaemonfactory_t *)self;
  65. if (fac->base.name)
  66. free(fac->base.name);
  67. free(fac);
  68. }
  69. static void* udplogdaemonfactory_create_log(void *self, const char* inst)
  70. {
  71. udplogdaemonfactory_t *fac = (udplogdaemonfactory_t *)self;
  72. udplogdaemon_t *log = MALLOC_T(udplogdaemon_t);
  73. log->base.level = XLOG_LEVEL_ALL;
  74. log->base.factory = self;
  75. log->base.inst_name = _strdup(inst);
  76. log->ip = 0;
  77. log->port = 0;
  78. log->listen_handle = INVALID_SOCKET;
  79. log->run_thread = NULL;
  80. return log;
  81. }
  82. static int udplogdaemonfactory_set_log_param(void *self,
  83. void *log,
  84. const char *key,
  85. const char *value)
  86. {
  87. udplogdaemonfactory_t *fac = (udplogdaemonfactory_t *)self;
  88. udplogdaemon_t *slog = (udplogdaemon_t*)log;
  89. if (_stricmp(key, "ip") == 0) {
  90. slog->ip = _strdup(value);
  91. } else if (_stricmp(key, "port") == 0) {
  92. int nport = atoi(value);
  93. if (nport <= 65535 && nport > 0)
  94. slog->port = (unsigned short)nport;
  95. } else if (_stricmp(key, "level") == 0) {
  96. if (_stricmp(value, "all") == 0) {
  97. slog->base.level = 0;
  98. } else if (_stricmp(value, "trace") == 0) {
  99. slog->base.level = 1;
  100. } else if (_stricmp(value, "debug") == 0) {
  101. slog->base.level = 2;
  102. } else if (_stricmp(value, "info") == 0) {
  103. slog->base.level = 3;
  104. } else if (_stricmp(value, "warn") == 0) {
  105. slog->base.level = 4;
  106. } else if (_stricmp(value, "error") == 0) {
  107. slog->base.level = 5;
  108. } else if (_stricmp(value, "fatal") == 0) {
  109. slog->base.level = 6;
  110. } else if (_stricmp(value, "none") == 0) {
  111. slog->base.level = 7;
  112. } else {
  113. return -1;
  114. }
  115. } else {
  116. return -1;
  117. }
  118. return 0;
  119. }
  120. static int udplogdaemonfactory_init_log(void *self, void *log)
  121. {
  122. udplogdaemonfactory_t *fac = (udplogdaemonfactory_t *)self;
  123. udplogdaemon_t *slog = (udplogdaemon_t*)log;
  124. memset(&slog->listen_addr, 0, sizeof(slog->listen_addr));
  125. slog->listen_addr.sin_addr.s_addr = inet_addr(slog->ip);
  126. slog->listen_addr.sin_port = htons(slog->port);
  127. slog->listen_addr.sin_family = AF_INET;
  128. slog->listen_handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  129. if (slog->listen_handle == INVALID_SOCKET)
  130. return -1;
  131. if (bind(slog->listen_handle, (struct sockaddr*)&slog->listen_addr, sizeof(slog->listen_addr)) != 0) {
  132. closesocket(slog->listen_handle);
  133. slog->listen_handle = INVALID_SOCKET;
  134. return -1;
  135. }
  136. {
  137. int buf_len = 1 << 20;
  138. setsockopt(slog->listen_handle, SOL_SOCKET, SO_RCVBUF, (char*)&buf_len, sizeof(buf_len));
  139. }
  140. slog->stop = 0;
  141. slog->run_thread = (HANDLE)_beginthreadex(NULL, 0, &udplogdaemon_run, slog, 0, NULL);
  142. if (slog->run_thread == NULL) {
  143. closesocket(slog->listen_handle);
  144. slog->listen_handle = INVALID_SOCKET;
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. static int udplogdaemonfactory_term_log(void *self, void *log)
  150. {
  151. udplogdaemonfactory_t *fac = (udplogdaemonfactory_t *)self;
  152. udplogdaemon_t *slog = (udplogdaemon_t*)log;
  153. if (slog->run_thread) {
  154. char buf[1];
  155. int buf_len = 1;
  156. int i, tries = 3;
  157. SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  158. InterlockedExchange(&slog->stop, 1);
  159. for (i = 0; i < tries; ++i)
  160. sendto(sock, buf, buf_len, 0, (struct sockaddr*)&slog->listen_addr, sizeof(slog->listen_addr));
  161. closesocket(sock);
  162. WaitForSingleObject(slog->run_thread, INFINITE);
  163. CloseHandle(slog->run_thread);
  164. slog->run_thread = 0;
  165. closesocket(slog->listen_handle);
  166. slog->listen_handle = INVALID_SOCKET;
  167. }
  168. if (slog->ip) {
  169. free(slog->ip);
  170. slog->ip = NULL;;
  171. }
  172. return 0;
  173. }
  174. static void udplogdaemonfactory_destroy_log(void *self, void *log)
  175. {
  176. udplogdaemon_t *slog = (udplogdaemon_t*)log;
  177. if (slog) {
  178. if (slog->base.inst_name)
  179. free(slog->base.inst_name);
  180. free(slog);
  181. }
  182. }
  183. static int udplogdaemonfactory_record_log(void *self,
  184. void *log,
  185. int level,
  186. unsigned long ts_low,
  187. unsigned long ts_high,
  188. const char *s,
  189. int sn)
  190. {
  191. return -1;
  192. }
  193. int udplogdaemonfactory_create(logfactory_t **p_fac)
  194. {
  195. udplogdaemonfactory_t *fac;
  196. if (!p_fac)
  197. return -1;
  198. fac = MALLOC_T(udplogdaemonfactory_t);
  199. fac->base.name = _strdup("udpdaemon");
  200. fac->base.record_log = &udplogdaemonfactory_record_log;
  201. fac->base.init_log = &udplogdaemonfactory_init_log;
  202. fac->base.set_log_param = &udplogdaemonfactory_set_log_param;
  203. fac->base.term_log = &udplogdaemonfactory_term_log;
  204. fac->base.create_log = &udplogdaemonfactory_create_log;
  205. fac->base.destroy_log = &udplogdaemonfactory_destroy_log;
  206. fac->base.destroy = &udplogdaemonfactory_destroy;
  207. *p_fac = &fac->base;
  208. return 0;
  209. }