log_udpdaemon.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. }
  66. static void* udplogdaemonfactory_create_log(void *self, const char* inst)
  67. {
  68. udplogdaemonfactory_t *fac = (udplogdaemonfactory_t *)self;
  69. udplogdaemon_t *log = MALLOC_T(udplogdaemon_t);
  70. log->base.level = XLOG_LEVEL_ALL;
  71. log->base.factory = self;
  72. log->base.inst_name = _strdup(inst);
  73. log->ip = 0;
  74. log->port = 0;
  75. log->listen_handle = INVALID_SOCKET;
  76. log->run_thread = NULL;
  77. return log;
  78. }
  79. static int udplogdaemonfactory_set_log_param(void *self,
  80. void *log,
  81. const char *key,
  82. const char *value)
  83. {
  84. udplogdaemonfactory_t *fac = (udplogdaemonfactory_t *)self;
  85. udplogdaemon_t *slog = (udplogdaemon_t*)log;
  86. if (_stricmp(key, "ip") == 0) {
  87. slog->ip = _strdup(value);
  88. } else if (_stricmp(key, "port") == 0) {
  89. int nport = atoi(value);
  90. if (nport <= 65535 && nport > 0)
  91. slog->port = (unsigned short)nport;
  92. } else if (_stricmp(key, "level") == 0) {
  93. if (_stricmp(value, "all") == 0) {
  94. slog->base.level = 0;
  95. } else if (_stricmp(value, "trace") == 0) {
  96. slog->base.level = 1;
  97. } else if (_stricmp(value, "debug") == 0) {
  98. slog->base.level = 2;
  99. } else if (_stricmp(value, "info") == 0) {
  100. slog->base.level = 3;
  101. } else if (_stricmp(value, "warn") == 0) {
  102. slog->base.level = 4;
  103. } else if (_stricmp(value, "error") == 0) {
  104. slog->base.level = 5;
  105. } else if (_stricmp(value, "fatal") == 0) {
  106. slog->base.level = 6;
  107. } else if (_stricmp(value, "none") == 0) {
  108. slog->base.level = 7;
  109. } else {
  110. return -1;
  111. }
  112. } else {
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. static int udplogdaemonfactory_init_log(void *self, void *log)
  118. {
  119. udplogdaemonfactory_t *fac = (udplogdaemonfactory_t *)self;
  120. udplogdaemon_t *slog = (udplogdaemon_t*)log;
  121. memset(&slog->listen_addr, 0, sizeof(slog->listen_addr));
  122. slog->listen_addr.sin_addr.s_addr = inet_addr(slog->ip);
  123. slog->listen_addr.sin_port = htons(slog->port);
  124. slog->listen_addr.sin_family = AF_INET;
  125. slog->listen_handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  126. if (slog->listen_handle == INVALID_SOCKET)
  127. return -1;
  128. if (bind(slog->listen_handle, (struct sockaddr*)&slog->listen_addr, sizeof(slog->listen_addr)) != 0) {
  129. closesocket(slog->listen_handle);
  130. slog->listen_handle = INVALID_SOCKET;
  131. return -1;
  132. }
  133. {
  134. int buf_len = 1 << 20;
  135. setsockopt(slog->listen_handle, SOL_SOCKET, SO_RCVBUF, (char*)&buf_len, sizeof(buf_len));
  136. }
  137. slog->stop = 0;
  138. slog->run_thread = (HANDLE)_beginthreadex(NULL, 0, &udplogdaemon_run, slog, 0, NULL);
  139. if (slog->run_thread == NULL) {
  140. closesocket(slog->listen_handle);
  141. slog->listen_handle = INVALID_SOCKET;
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. static int udplogdaemonfactory_term_log(void *self, void *log)
  147. {
  148. udplogdaemonfactory_t *fac = (udplogdaemonfactory_t *)self;
  149. udplogdaemon_t *slog = (udplogdaemon_t*)log;
  150. if (slog->run_thread) {
  151. char buf[1];
  152. int buf_len = 1;
  153. int i, tries = 3;
  154. SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  155. InterlockedExchange(&slog->stop, 1);
  156. for (i = 0; i < tries; ++i)
  157. sendto(sock, buf, buf_len, 0, (struct sockaddr*)&slog->listen_addr, sizeof(slog->listen_addr));
  158. closesocket(sock);
  159. WaitForSingleObject(slog->run_thread, INFINITE);
  160. CloseHandle(slog->run_thread);
  161. slog->run_thread = 0;
  162. closesocket(slog->listen_handle);
  163. slog->listen_handle = INVALID_SOCKET;
  164. }
  165. if (slog->ip) {
  166. free(slog->ip);
  167. slog->ip = NULL;;
  168. }
  169. return 0;
  170. }
  171. static void udplogdaemonfactory_destroy_log(void *self, void *log)
  172. {
  173. udplogdaemon_t *slog = (udplogdaemon_t*)log;
  174. if (slog) {
  175. if (slog->base.inst_name)
  176. free(slog->base.inst_name);
  177. free(slog);
  178. }
  179. }
  180. static int udplogdaemonfactory_record_log(void *self,
  181. void *log,
  182. int level,
  183. unsigned long ts_low,
  184. unsigned long ts_high,
  185. const char *s,
  186. int sn)
  187. {
  188. return -1;
  189. }
  190. int udplogdaemonfactory_create(logfactory_t **p_fac)
  191. {
  192. udplogdaemonfactory_t *fac;
  193. if (!p_fac)
  194. return -1;
  195. fac = MALLOC_T(udplogdaemonfactory_t);
  196. fac->base.name = _strdup("udpdaemon");
  197. fac->base.record_log = &udplogdaemonfactory_record_log;
  198. fac->base.init_log = &udplogdaemonfactory_init_log;
  199. fac->base.set_log_param = &udplogdaemonfactory_set_log_param;
  200. fac->base.term_log = &udplogdaemonfactory_term_log;
  201. fac->base.create_log = &udplogdaemonfactory_create_log;
  202. fac->base.destroy_log = &udplogdaemonfactory_destroy_log;
  203. fac->base.destroy = &udplogdaemonfactory_destroy;
  204. *p_fac = &fac->base;
  205. return 0;
  206. }