You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1210 lines
36 KiB

9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
9 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
9 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
9 years ago
9 years ago
11 years ago
9 years ago
11 years ago
9 years ago
11 years ago
9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
9 years ago
10 years ago
9 years ago
11 years ago
10 years ago
9 years ago
9 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
10 years ago
9 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
9 years ago
10 years ago
10 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
9 years ago
9 years ago
11 years ago
9 years ago
9 years ago
11 years ago
10 years ago
9 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
9 years ago
9 years ago
  1. /*
  2. * udprelay.c - Setup UDP relay for both client and server
  3. *
  4. * Copyright (C) 2013 - 2015, Max Lv <max.c.lv@gmail.com>
  5. *
  6. * This file is part of the shadowsocks-libev.
  7. *
  8. * shadowsocks-libev is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * shadowsocks-libev is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with shadowsocks-libev; see the file COPYING. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. */
  22. #include <sys/stat.h>
  23. #include <sys/types.h>
  24. #include <fcntl.h>
  25. #include <locale.h>
  26. #include <signal.h>
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #ifndef __MINGW32__
  32. #include <arpa/inet.h>
  33. #include <errno.h>
  34. #include <netdb.h>
  35. #include <netinet/in.h>
  36. #include <netinet/tcp.h>
  37. #include <pthread.h>
  38. #endif
  39. #ifdef HAVE_CONFIG_H
  40. #include "config.h"
  41. #endif
  42. #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H) && defined(__linux__)
  43. #include <net/if.h>
  44. #include <sys/ioctl.h>
  45. #define SET_INTERFACE
  46. #endif
  47. #ifdef __MINGW32__
  48. #include "win32.h"
  49. #endif
  50. #include <libcork/core.h>
  51. #include <udns.h>
  52. #include "utils.h"
  53. #include "netutils.h"
  54. #include "cache.h"
  55. #include "udprelay.h"
  56. #ifdef UDPRELAY_REMOTE
  57. #define MAX_UDP_CONN_NUM 1024
  58. #else
  59. #define MAX_UDP_CONN_NUM 256
  60. #endif
  61. #ifdef UDPRELAY_REMOTE
  62. #ifdef UDPRELAY_LOCAL
  63. #error "UDPRELAY_REMOTE and UDPRELAY_LOCAL should not be both defined"
  64. #endif
  65. #endif
  66. #ifndef EAGAIN
  67. #define EAGAIN EWOULDBLOCK
  68. #endif
  69. #ifndef EWOULDBLOCK
  70. #define EWOULDBLOCK EAGAIN
  71. #endif
  72. #define BUF_SIZE MAX_UDP_PACKET_SIZE
  73. static void server_recv_cb(EV_P_ ev_io *w, int revents);
  74. static void remote_recv_cb(EV_P_ ev_io *w, int revents);
  75. static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents);
  76. static char *hash_key(const int af, const struct sockaddr_storage *addr);
  77. #ifdef UDPRELAY_REMOTE
  78. static void query_resolve_cb(struct sockaddr *addr, void *data);
  79. #endif
  80. static void close_and_free_remote(EV_P_ struct remote_ctx *ctx);
  81. static struct remote_ctx * new_remote(int fd, struct server_ctx * server_ctx);
  82. extern int verbose;
  83. static int server_num = 0;
  84. static struct server_ctx *server_ctx_list[MAX_REMOTE_NUM] = { NULL };
  85. #ifndef __MINGW32__
  86. static int setnonblocking(int fd)
  87. {
  88. int flags;
  89. if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
  90. flags = 0;
  91. }
  92. return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  93. }
  94. #endif
  95. #ifdef SET_INTERFACE
  96. static int setinterface(int socket_fd, const char * interface_name)
  97. {
  98. struct ifreq interface;
  99. memset(&interface, 0, sizeof(interface));
  100. strncpy(interface.ifr_name, interface_name, IFNAMSIZ);
  101. int res = setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, &interface,
  102. sizeof(struct ifreq));
  103. return res;
  104. }
  105. #endif
  106. #if defined(UDPRELAY_REMOTE) && defined(SO_BROADCAST)
  107. static int set_broadcast(int socket_fd)
  108. {
  109. int opt = 1;
  110. return setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt));
  111. }
  112. #endif
  113. #ifdef SO_NOSIGPIPE
  114. static int set_nosigpipe(int socket_fd)
  115. {
  116. int opt = 1;
  117. return setsockopt(socket_fd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
  118. }
  119. #endif
  120. #ifdef UDPRELAY_REDIR
  121. #ifndef IP_TRANSPARENT
  122. #define IP_TRANSPARENT 19
  123. #endif
  124. #ifndef IP_RECVORIGDSTADDR
  125. #define IP_RECVORIGDSTADDR 20
  126. #endif
  127. static int get_dstaddr(struct msghdr *msg, struct sockaddr_storage *dstaddr)
  128. {
  129. struct cmsghdr *cmsg;
  130. for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
  131. if (cmsg->cmsg_level == SOL_IP && cmsg->cmsg_type == IP_RECVORIGDSTADDR) {
  132. memcpy(dstaddr, CMSG_DATA(cmsg), sizeof(struct sockaddr_in));
  133. dstaddr->ss_family = AF_INET;
  134. return 0;
  135. } else if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IP_RECVORIGDSTADDR) {
  136. memcpy(dstaddr, CMSG_DATA(cmsg), sizeof(struct sockaddr_in6));
  137. dstaddr->ss_family = AF_INET6;
  138. return 0;
  139. }
  140. }
  141. return 1;
  142. }
  143. #endif
  144. static char *hash_key(const int af, const struct sockaddr_storage *addr)
  145. {
  146. int addr_len = sizeof(struct sockaddr_storage);
  147. int key_len = addr_len + sizeof(int);
  148. char key[key_len];
  149. memset(key, 0, key_len);
  150. memcpy(key, &af, sizeof(int));
  151. memcpy(key + sizeof(int), (const uint8_t *)addr, addr_len);
  152. return (char *)enc_md5((const uint8_t *)key, key_len, NULL);
  153. }
  154. static int parse_udprealy_header(const char * buf, const int buf_len,
  155. char *host, char *port,
  156. struct sockaddr_storage *storage)
  157. {
  158. const uint8_t atyp = *(uint8_t *)buf;
  159. int offset = 1;
  160. // get remote addr and port
  161. if (atyp == 1) {
  162. // IP V4
  163. size_t in_addr_len = sizeof(struct in_addr);
  164. if (buf_len > in_addr_len) {
  165. if (storage != NULL) {
  166. struct sockaddr_in *addr = (struct sockaddr_in *)storage;
  167. addr->sin_family = AF_INET;
  168. addr->sin_addr = *(struct in_addr *)(buf + offset);
  169. addr->sin_port = *(uint16_t *)(buf + offset + in_addr_len);
  170. }
  171. if (host != NULL) {
  172. dns_ntop(AF_INET, (const void *)(buf + offset),
  173. host, INET_ADDRSTRLEN);
  174. }
  175. offset += in_addr_len;
  176. }
  177. } else if (atyp == 3) {
  178. // Domain name
  179. uint8_t name_len = *(uint8_t *)(buf + offset);
  180. if (name_len < buf_len && name_len < 255 && name_len > 0) {
  181. if (host != NULL) {
  182. memcpy(host, buf + offset + 1, name_len);
  183. }
  184. offset += name_len + 1;
  185. }
  186. if (storage != NULL) {
  187. struct cork_ip ip;
  188. if (cork_ip_init(&ip, host) != -1) {
  189. if (ip.version == 4) {
  190. struct sockaddr_in *addr = (struct sockaddr_in *)storage;
  191. dns_pton(AF_INET, host, &(addr->sin_addr));
  192. addr->sin_port = *(uint16_t *)(buf + offset);
  193. addr->sin_family = AF_INET;
  194. } else if (ip.version == 6) {
  195. struct sockaddr_in6 *addr = (struct sockaddr_in6 *)storage;
  196. dns_pton(AF_INET, host, &(addr->sin6_addr));
  197. addr->sin6_port = *(uint16_t *)(buf + offset);
  198. addr->sin6_family = AF_INET6;
  199. }
  200. }
  201. }
  202. } else if (atyp == 4) {
  203. // IP V6
  204. size_t in6_addr_len = sizeof(struct in6_addr);
  205. if (buf_len > in6_addr_len) {
  206. if (storage != NULL) {
  207. struct sockaddr_in6 *addr = (struct sockaddr_in6 *)storage;
  208. addr->sin6_family = AF_INET6;
  209. addr->sin6_addr = *(struct in6_addr *)(buf + offset);
  210. addr->sin6_port = *(uint16_t *)(buf + offset + in6_addr_len);
  211. }
  212. if (host != NULL) {
  213. dns_ntop(AF_INET6, (const void *)(buf + offset),
  214. host, INET6_ADDRSTRLEN);
  215. }
  216. offset += in6_addr_len;
  217. }
  218. }
  219. if (offset == 1) {
  220. LOGE("[udp] invalid header with addr type %d", atyp);
  221. return 0;
  222. }
  223. if (port != NULL) {
  224. sprintf(port, "%d", ntohs(*(uint16_t *)(buf + offset)));
  225. }
  226. offset += 2;
  227. return offset;
  228. }
  229. static char *get_addr_str(const struct sockaddr *sa)
  230. {
  231. static char s[SS_ADDRSTRLEN];
  232. memset(s, 0, SS_ADDRSTRLEN);
  233. char addr[INET6_ADDRSTRLEN] = { 0 };
  234. char port[PORTSTRLEN] = { 0 };
  235. uint16_t p;
  236. switch (sa->sa_family) {
  237. case AF_INET:
  238. dns_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
  239. addr, INET_ADDRSTRLEN);
  240. p = ntohs(((struct sockaddr_in *)sa)->sin_port);
  241. sprintf(port, "%d", p);
  242. break;
  243. case AF_INET6:
  244. dns_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
  245. addr, INET6_ADDRSTRLEN);
  246. p = ntohs(((struct sockaddr_in *)sa)->sin_port);
  247. sprintf(port, "%d", p);
  248. break;
  249. default:
  250. strncpy(s, "Unknown AF", SS_ADDRSTRLEN);
  251. }
  252. int addr_len = strlen(addr);
  253. int port_len = strlen(port);
  254. memcpy(s, addr, addr_len);
  255. memcpy(s + addr_len + 1, port, port_len);
  256. s[addr_len] = ':';
  257. return s;
  258. }
  259. int create_remote_socket(int ipv6)
  260. {
  261. int remote_sock;
  262. if (ipv6) {
  263. // Try to bind IPv6 first
  264. struct sockaddr_in6 addr;
  265. memset(&addr, 0, sizeof(addr));
  266. addr.sin6_family = AF_INET6;
  267. addr.sin6_addr = in6addr_any;
  268. addr.sin6_port = 0;
  269. remote_sock = socket(AF_INET6, SOCK_DGRAM, 0);
  270. if (remote_sock == -1) {
  271. ERROR("[udp] cannot create socket");
  272. return -1;
  273. }
  274. if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
  275. FATAL("[udp] cannot bind remote");
  276. return -1;
  277. }
  278. } else {
  279. // Or else bind to IPv4
  280. struct sockaddr_in addr;
  281. memset(&addr, 0, sizeof(addr));
  282. addr.sin_family = AF_INET;
  283. addr.sin_addr.s_addr = INADDR_ANY;
  284. addr.sin_port = 0;
  285. remote_sock = socket(AF_INET, SOCK_DGRAM, 0);
  286. if (remote_sock == -1) {
  287. ERROR("[udp] cannot create socket");
  288. return -1;
  289. }
  290. if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
  291. FATAL("[udp] cannot bind remote");
  292. return -1;
  293. }
  294. }
  295. return remote_sock;
  296. }
  297. int create_server_socket(const char *host, const char *port)
  298. {
  299. struct addrinfo hints;
  300. struct addrinfo *result, *rp, *ipv4v6bindall;
  301. int s, server_sock;
  302. memset(&hints, 0, sizeof(struct addrinfo));
  303. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  304. hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */
  305. hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; /* For wildcard IP address */
  306. hints.ai_protocol = IPPROTO_UDP;
  307. s = getaddrinfo(host, port, &hints, &result);
  308. if (s != 0) {
  309. LOGE("[udp] getaddrinfo: %s", gai_strerror(s));
  310. return -1;
  311. }
  312. rp = result;
  313. /*
  314. On Linux, with net.ipv6.bindv6only = 0 (the default), getaddrinfo(NULL) with
  315. AI_PASSIVE returns 0.0.0.0 and :: (in this order). AI_PASSIVE was meant to
  316. return a list of addresses to listen on, but it is impossible to listen on
  317. 0.0.0.0 and :: at the same time, if :: implies dualstack mode.
  318. */
  319. if (!host) {
  320. ipv4v6bindall = result;
  321. /* Loop over all address infos found until a IPV6 address is found. */
  322. while (ipv4v6bindall) {
  323. if (ipv4v6bindall->ai_family == AF_INET6) {
  324. rp = ipv4v6bindall; /* Take first IPV6 address available */
  325. break;
  326. }
  327. ipv4v6bindall = ipv4v6bindall->ai_next; /* Get next address info, if any */
  328. }
  329. }
  330. for (/*rp = result*/; rp != NULL; rp = rp->ai_next) {
  331. server_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  332. if (server_sock == -1) {
  333. continue;
  334. }
  335. if (rp->ai_family == AF_INET6) {
  336. int ipv6only = host ? 1 : 0;
  337. setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only));
  338. }
  339. int opt = 1;
  340. setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  341. #ifdef SO_NOSIGPIPE
  342. set_nosigpipe(server_sock);
  343. #endif
  344. #ifdef UDPRELAY_REDIR
  345. if (setsockopt(server_sock, SOL_IP, IP_TRANSPARENT, &opt, sizeof(opt))) {
  346. FATAL("[udp] setsockopt IP_TRANSPARENT");
  347. }
  348. if (setsockopt(server_sock, IPPROTO_IP, IP_RECVORIGDSTADDR, &opt, sizeof(opt))) {
  349. FATAL("[udp] setsockopt IP_RECVORIGDSTADDR");
  350. }
  351. #endif
  352. s = bind(server_sock, rp->ai_addr, rp->ai_addrlen);
  353. if (s == 0) {
  354. /* We managed to bind successfully! */
  355. break;
  356. } else {
  357. ERROR("[udp] bind");
  358. }
  359. close(server_sock);
  360. }
  361. if (rp == NULL) {
  362. LOGE("[udp] cannot bind");
  363. return -1;
  364. }
  365. freeaddrinfo(result);
  366. return server_sock;
  367. }
  368. struct remote_ctx *new_remote(int fd, struct server_ctx *server_ctx)
  369. {
  370. struct remote_ctx *ctx = malloc(sizeof(struct remote_ctx));
  371. memset(ctx, 0, sizeof(struct remote_ctx));
  372. ctx->fd = fd;
  373. ctx->server_ctx = server_ctx;
  374. ev_io_init(&ctx->io, remote_recv_cb, fd, EV_READ);
  375. ev_timer_init(&ctx->watcher, remote_timeout_cb, server_ctx->timeout,
  376. server_ctx->timeout);
  377. return ctx;
  378. }
  379. struct server_ctx * new_server_ctx(int fd)
  380. {
  381. struct server_ctx *ctx = malloc(sizeof(struct server_ctx));
  382. memset(ctx, 0, sizeof(struct server_ctx));
  383. ctx->fd = fd;
  384. ev_io_init(&ctx->io, server_recv_cb, fd, EV_READ);
  385. return ctx;
  386. }
  387. #ifdef UDPRELAY_REMOTE
  388. struct query_ctx *new_query_ctx(const char *buf, const int buf_len)
  389. {
  390. struct query_ctx *ctx = malloc(sizeof(struct query_ctx));
  391. memset(ctx, 0, sizeof(struct query_ctx));
  392. ctx->buf = malloc(buf_len);
  393. ctx->buf_len = buf_len;
  394. memcpy(ctx->buf, buf, buf_len);
  395. return ctx;
  396. }
  397. void close_and_free_query(EV_P_ struct query_ctx *ctx)
  398. {
  399. if (ctx != NULL) {
  400. if (ctx->query != NULL) {
  401. resolv_cancel(ctx->query);
  402. ctx->query = NULL;
  403. }
  404. if (ctx->buf != NULL) {
  405. free(ctx->buf);
  406. }
  407. free(ctx);
  408. }
  409. }
  410. #endif
  411. void close_and_free_remote(EV_P_ struct remote_ctx *ctx)
  412. {
  413. if (ctx != NULL) {
  414. ev_timer_stop(EV_A_ & ctx->watcher);
  415. ev_io_stop(EV_A_ & ctx->io);
  416. if (ctx->src_fd != 0) {
  417. close(ctx->src_fd);
  418. }
  419. close(ctx->fd);
  420. free(ctx);
  421. }
  422. }
  423. static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
  424. {
  425. struct remote_ctx *remote_ctx = (struct remote_ctx *)(((void *)watcher)
  426. - sizeof(ev_io));
  427. if (verbose) {
  428. LOGI("[udp] connection timeout");
  429. }
  430. char *key = hash_key(remote_ctx->af, &remote_ctx->src_addr);
  431. cache_remove(remote_ctx->server_ctx->conn_cache, key);
  432. }
  433. #ifdef UDPRELAY_REMOTE
  434. static void query_resolve_cb(struct sockaddr *addr, void *data)
  435. {
  436. struct query_ctx *query_ctx = (struct query_ctx *)data;
  437. struct ev_loop *loop = query_ctx->server_ctx->loop;
  438. if (verbose) {
  439. LOGI("[udp] udns resolved");
  440. }
  441. query_ctx->query = NULL;
  442. if (addr == NULL) {
  443. LOGE("[udp] udns returned an error");
  444. } else {
  445. struct remote_ctx *remote_ctx = query_ctx->remote_ctx;
  446. int cache_hit = 0;
  447. if (remote_ctx == NULL) {
  448. int remotefd = create_remote_socket(addr->sa_family == AF_INET6);
  449. if (remotefd != -1) {
  450. setnonblocking(remotefd);
  451. #ifdef SO_BROADCAST
  452. set_broadcast(remotefd);
  453. #endif
  454. #ifdef SO_NOSIGPIPE
  455. set_nosigpipe(remotefd);
  456. #endif
  457. #ifdef SET_INTERFACE
  458. if (query_ctx->server_ctx->iface) {
  459. setinterface(remotefd, query_ctx->server_ctx->iface);
  460. }
  461. #endif
  462. remote_ctx = new_remote(remotefd, query_ctx->server_ctx);
  463. remote_ctx->src_addr = query_ctx->src_addr;
  464. if (addr->sa_family == AF_INET) {
  465. memcpy(&(remote_ctx->dst_addr), addr, sizeof(struct sockaddr_in));
  466. } else if (addr->sa_family == AF_INET6) {
  467. memcpy(&(remote_ctx->dst_addr), addr, sizeof(struct sockaddr_in6));
  468. }
  469. remote_ctx->server_ctx = query_ctx->server_ctx;
  470. remote_ctx->addr_header_len = query_ctx->addr_header_len;
  471. memcpy(remote_ctx->addr_header, query_ctx->addr_header,
  472. query_ctx->addr_header_len);
  473. } else {
  474. ERROR("[udp] bind() error");
  475. }
  476. } else {
  477. if (addr->sa_family == AF_INET) {
  478. memcpy(&(remote_ctx->dst_addr), addr, sizeof(struct sockaddr_in));
  479. } else if (addr->sa_family == AF_INET6) {
  480. memcpy(&(remote_ctx->dst_addr), addr, sizeof(struct sockaddr_in6));
  481. }
  482. cache_hit = 1;
  483. }
  484. if (remote_ctx != NULL) {
  485. size_t addr_len = get_sockaddr_len((struct sockaddr *)&remote_ctx->dst_addr);
  486. int s = sendto(remote_ctx->fd, query_ctx->buf, query_ctx->buf_len,
  487. 0, (struct sockaddr *)&remote_ctx->dst_addr, addr_len);
  488. if (s == -1) {
  489. ERROR("[udp] sendto_remote");
  490. if (!cache_hit) {
  491. close_and_free_remote(EV_A_ remote_ctx);
  492. }
  493. } else {
  494. // Add to conn cache
  495. if (!cache_hit) {
  496. char *key = hash_key(0, &remote_ctx->src_addr);
  497. cache_insert(query_ctx->server_ctx->conn_cache, key,
  498. (void *)remote_ctx);
  499. ev_io_start(EV_A_ & remote_ctx->io);
  500. ev_timer_start(EV_A_ & remote_ctx->watcher);
  501. }
  502. }
  503. }
  504. }
  505. // clean up
  506. close_and_free_query(EV_A_ query_ctx);
  507. }
  508. #endif
  509. static void remote_recv_cb(EV_P_ ev_io *w, int revents)
  510. {
  511. struct remote_ctx *remote_ctx = (struct remote_ctx *)w;
  512. struct server_ctx *server_ctx = remote_ctx->server_ctx;
  513. // server has been closed
  514. if (server_ctx == NULL) {
  515. LOGE("[udp] invalid server");
  516. close_and_free_remote(EV_A_ remote_ctx);
  517. return;
  518. }
  519. if (verbose) {
  520. LOGI("[udp] remote receive a packet");
  521. }
  522. struct sockaddr src_addr;
  523. socklen_t src_addr_len = sizeof(src_addr);
  524. char *buf = malloc(BUF_SIZE);
  525. // recv
  526. ssize_t buf_len = recvfrom(remote_ctx->fd, buf, BUF_SIZE, 0, &src_addr, &src_addr_len);
  527. if (buf_len == -1) {
  528. // error on recv
  529. // simply drop that packet
  530. if (verbose) {
  531. ERROR("[udp] server_recvfrom");
  532. }
  533. goto CLEAN_UP;
  534. }
  535. #ifdef UDPRELAY_LOCAL
  536. buf = ss_decrypt_all(BUF_SIZE, buf, &buf_len, server_ctx->method);
  537. if (buf == NULL) {
  538. if (verbose) {
  539. ERROR("[udp] server_ss_decrypt_all");
  540. }
  541. goto CLEAN_UP;
  542. }
  543. int len = parse_udprealy_header(buf, buf_len, NULL, NULL, NULL);
  544. if (len == 0) {
  545. LOGI("[udp] error in parse header");
  546. // error in parse header
  547. goto CLEAN_UP;
  548. }
  549. // server may return using a different address type other than the type we
  550. // have used during sending
  551. #if defined(UDPRELAY_TUNNEL) || defined(UDPRELAY_REDIR)
  552. // Construct packet
  553. buf_len -= len;
  554. memmove(buf, buf + len, buf_len);
  555. #else
  556. // Construct packet
  557. buf = realloc(buf, buf_len + 3);
  558. memmove(buf + 3, buf, buf_len);
  559. memset(buf, 0, 3);
  560. buf_len += 3;
  561. #endif
  562. #endif
  563. #ifdef UDPRELAY_REMOTE
  564. unsigned int addr_header_len = remote_ctx->addr_header_len;
  565. // Construct packet
  566. buf = realloc(buf, buf_len + addr_header_len);
  567. memmove(buf + addr_header_len, buf, buf_len);
  568. memcpy(buf, remote_ctx->addr_header, addr_header_len);
  569. buf_len += addr_header_len;
  570. buf = ss_encrypt_all(BUF_SIZE, buf, &buf_len, server_ctx->method);
  571. #endif
  572. size_t remote_src_addr_len = get_sockaddr_len((struct sockaddr *)&remote_ctx->src_addr);
  573. #ifdef UDPRELAY_REDIR
  574. size_t remote_dst_addr_len = get_sockaddr_len((struct sockaddr *)&remote_ctx->dst_addr);
  575. if (remote_ctx->src_fd == 0) {
  576. int src_sock = socket(remote_ctx->src_addr.ss_family, SOCK_DGRAM, 0);
  577. if (src_sock < 0) {
  578. ERROR("[udp] remote_recv_socket");
  579. }
  580. int opt = 1;
  581. if (setsockopt(src_sock, SOL_IP, IP_TRANSPARENT, &opt, sizeof(opt))) {
  582. ERROR("[udp] remote_recv_setsockopt");
  583. close(src_sock);
  584. goto CLEAN_UP;
  585. }
  586. if (setsockopt(src_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
  587. ERROR("[udp] remote_recv_setsockopt");
  588. close(src_sock);
  589. goto CLEAN_UP;
  590. }
  591. if (bind(src_sock, (struct sockaddr *)&remote_ctx->dst_addr, remote_dst_addr_len) != 0) {
  592. ERROR("[udp] remote_recv_bind");
  593. close(src_sock);
  594. goto CLEAN_UP;
  595. }
  596. remote_ctx->src_fd = src_sock;
  597. }
  598. int s = sendto(remote_ctx->src_fd, buf, buf_len, 0,
  599. (struct sockaddr *)&remote_ctx->src_addr, remote_src_addr_len);
  600. if (s == -1) {
  601. ERROR("[udp] remote_recv_sendto");
  602. goto CLEAN_UP;
  603. }
  604. #else
  605. int s = sendto(server_ctx->fd, buf, buf_len, 0,
  606. (struct sockaddr *)&remote_ctx->src_addr, remote_src_addr_len);
  607. if (s == -1) {
  608. ERROR("[udp] remote_recv_sendto");
  609. goto CLEAN_UP;
  610. }
  611. #endif
  612. // handle the UDP packet sucessfully,
  613. // triger the timer
  614. ev_timer_again(EV_A_ & remote_ctx->watcher);
  615. CLEAN_UP:
  616. free(buf);
  617. }
  618. static void server_recv_cb(EV_P_ ev_io *w, int revents)
  619. {
  620. struct server_ctx *server_ctx = (struct server_ctx *)w;
  621. struct sockaddr_storage src_addr;
  622. memset(&src_addr, 0, sizeof(struct sockaddr_storage));
  623. char *buf = malloc(BUF_SIZE);
  624. socklen_t src_addr_len = sizeof(struct sockaddr_storage);
  625. unsigned int offset = 0;
  626. #ifdef UDPRELAY_REDIR
  627. char control_buffer[64] = { 0 };
  628. struct msghdr msg;
  629. struct iovec iov[1];
  630. struct sockaddr_storage dst_addr;
  631. memset(&dst_addr, 0, sizeof(struct sockaddr_storage));
  632. msg.msg_name = &src_addr;
  633. msg.msg_namelen = src_addr_len;
  634. msg.msg_control = control_buffer;
  635. msg.msg_controllen = sizeof(control_buffer);
  636. iov[0].iov_base = buf;
  637. iov[0].iov_len = BUF_SIZE;
  638. msg.msg_iov = iov;
  639. msg.msg_iovlen = 1;
  640. ssize_t buf_len = recvmsg(server_ctx->fd, &msg, 0);
  641. if (buf_len == -1) {
  642. ERROR("[udp] server_recvmsg");
  643. goto CLEAN_UP;
  644. }
  645. if (get_dstaddr(&msg, &dst_addr)) {
  646. LOGE("[udp] unable to get dest addr");
  647. goto CLEAN_UP;
  648. }
  649. #else
  650. ssize_t buf_len =
  651. recvfrom(server_ctx->fd, buf, BUF_SIZE, 0, (struct sockaddr *)&src_addr,
  652. &src_addr_len);
  653. if (buf_len == -1) {
  654. // error on recv
  655. // simply drop that packet
  656. if (verbose) {
  657. ERROR("[udp] server_recvfrom");
  658. }
  659. goto CLEAN_UP;
  660. }
  661. #endif
  662. if (verbose) {
  663. LOGI("[udp] server receive a packet");
  664. }
  665. #ifdef UDPRELAY_REMOTE
  666. buf = ss_decrypt_all(BUF_SIZE, buf, &buf_len, server_ctx->method);
  667. if (buf == NULL) {
  668. if (verbose) {
  669. ERROR("[udp] server_ss_decrypt_all");
  670. }
  671. goto CLEAN_UP;
  672. }
  673. #endif
  674. #ifdef UDPRELAY_LOCAL
  675. #if !defined(UDPRELAY_TUNNEL) && !defined(UDPRELAY_REDIR)
  676. uint8_t frag = *(uint8_t *)(buf + 2);
  677. offset += 3;
  678. #endif
  679. #endif
  680. /*
  681. *
  682. * SOCKS5 UDP Request
  683. * +----+------+------+----------+----------+----------+
  684. * |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
  685. * +----+------+------+----------+----------+----------+
  686. * | 2 | 1 | 1 | Variable | 2 | Variable |
  687. * +----+------+------+----------+----------+----------+
  688. *
  689. * SOCKS5 UDP Response
  690. * +----+------+------+----------+----------+----------+
  691. * |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
  692. * +----+------+------+----------+----------+----------+
  693. * | 2 | 1 | 1 | Variable | 2 | Variable |
  694. * +----+------+------+----------+----------+----------+
  695. *
  696. * shadowsocks UDP Request (before encrypted)
  697. * +------+----------+----------+----------+
  698. * | ATYP | DST.ADDR | DST.PORT | DATA |
  699. * +------+----------+----------+----------+
  700. * | 1 | Variable | 2 | Variable |
  701. * +------+----------+----------+----------+
  702. *
  703. * shadowsocks UDP Response (before encrypted)
  704. * +------+----------+----------+----------+
  705. * | ATYP | DST.ADDR | DST.PORT | DATA |
  706. * +------+----------+----------+----------+
  707. * | 1 | Variable | 2 | Variable |
  708. * +------+----------+----------+----------+
  709. *
  710. * shadowsocks UDP Request and Response (after encrypted)
  711. * +-------+--------------+
  712. * | IV | PAYLOAD |
  713. * +-------+--------------+
  714. * | Fixed | Variable |
  715. * +-------+--------------+
  716. *
  717. */
  718. #ifdef UDPRELAY_REDIR
  719. char addr_header[256] = { 0 };
  720. int addr_header_len = 0;
  721. if (dst_addr.ss_family == AF_INET) {
  722. struct sockaddr_in *addr = (struct sockaddr_in *)&dst_addr;
  723. size_t addr_len = sizeof(struct in_addr);
  724. addr_header[addr_header_len++] = 1;
  725. memcpy(addr_header + addr_header_len, &addr->sin_addr, addr_len);
  726. addr_header_len += addr_len;
  727. memcpy(addr_header + addr_header_len, &addr->sin_port, 2);
  728. addr_header_len += 2;
  729. } else if (dst_addr.ss_family == AF_INET6) {
  730. struct sockaddr_in6 *addr = (struct sockaddr_in6 *)&dst_addr;
  731. size_t addr_len = sizeof(struct in6_addr);
  732. addr_header[addr_header_len++] = 4;
  733. memcpy(addr_header + addr_header_len, &addr->sin6_addr, addr_len);
  734. addr_header_len += addr_len;
  735. memcpy(addr_header + addr_header_len, &addr->sin6_port, 2);
  736. addr_header_len += 2;
  737. } else {
  738. LOGE("[udp] failed to parse tproxy addr");
  739. goto CLEAN_UP;
  740. }
  741. // reconstruct the buffer
  742. buf = realloc(buf, buf_len + addr_header_len);
  743. memmove(buf + addr_header_len, buf, buf_len);
  744. memcpy(buf, addr_header, addr_header_len);
  745. buf_len += addr_header_len;
  746. char *key = hash_key(dst_addr.ss_family, &src_addr);
  747. #elif UDPRELAY_TUNNEL
  748. char addr_header[256] = { 0 };
  749. char *host = server_ctx->tunnel_addr.host;
  750. char *port = server_ctx->tunnel_addr.port;
  751. uint16_t port_num = (uint16_t)atoi(port);
  752. uint16_t port_net_num = htons(port_num);
  753. int addr_header_len = 0;
  754. struct cork_ip ip;
  755. if (cork_ip_init(&ip, host) != -1) {
  756. if (ip.version == 4) {
  757. // send as IPv4
  758. struct in_addr host_addr;
  759. int host_len = sizeof(struct in_addr);
  760. if (dns_pton(AF_INET, host, &host_addr) == -1) {
  761. FATAL("IP parser error");
  762. }
  763. addr_header[addr_header_len++] = 1;
  764. memcpy(addr_header + addr_header_len, &host_addr, host_len);
  765. addr_header_len += host_len;
  766. } else if (ip.version == 6) {
  767. // send as IPv6
  768. struct in6_addr host_addr;
  769. int host_len = sizeof(struct in6_addr);
  770. if (dns_pton(AF_INET6, host, &host_addr) == -1) {
  771. FATAL("IP parser error");
  772. }
  773. addr_header[addr_header_len++] = 4;
  774. memcpy(addr_header + addr_header_len, &host_addr, host_len);
  775. addr_header_len += host_len;
  776. } else {
  777. FATAL("IP parser error");
  778. }
  779. } else {
  780. // send as domain
  781. int host_len = strlen(host);
  782. addr_header[addr_header_len++] = 3;
  783. addr_header[addr_header_len++] = host_len;
  784. memcpy(addr_header + addr_header_len, host, host_len);
  785. addr_header_len += host_len;
  786. }
  787. memcpy(addr_header + addr_header_len, &port_net_num, 2);
  788. addr_header_len += 2;
  789. // reconstruct the buffer
  790. buf = realloc(buf, buf_len + addr_header_len);
  791. memmove(buf + addr_header_len, buf, buf_len);
  792. memcpy(buf, addr_header, addr_header_len);
  793. buf_len += addr_header_len;
  794. char *key = hash_key(ip.version == 4 ? AF_INET : AF_INET6, &src_addr);
  795. #else
  796. char host[256] = { 0 };
  797. char port[64] = { 0 };
  798. struct sockaddr_storage storage;
  799. memset(&storage, 0, sizeof(struct sockaddr_storage));
  800. int addr_header_len = parse_udprealy_header(buf + offset,
  801. buf_len - offset, host, port,
  802. &storage);
  803. if (addr_header_len == 0) {
  804. // error in parse header
  805. goto CLEAN_UP;
  806. }
  807. char *addr_header = buf + offset;
  808. char *key = hash_key(storage.ss_family, &src_addr);
  809. #endif
  810. struct cache *conn_cache = server_ctx->conn_cache;
  811. struct remote_ctx *remote_ctx = NULL;
  812. cache_lookup(conn_cache, key, (void *)&remote_ctx);
  813. if (remote_ctx != NULL) {
  814. if (memcmp(&src_addr, &remote_ctx->src_addr, sizeof(src_addr))) {
  815. remote_ctx = NULL;
  816. }
  817. }
  818. if (remote_ctx == NULL) {
  819. if (verbose) {
  820. #ifdef UDPRELAY_REDIR
  821. char src[SS_ADDRSTRLEN];
  822. char dst[SS_ADDRSTRLEN];
  823. strcpy(src, get_addr_str((struct sockaddr *)&src_addr));
  824. strcpy(dst, get_addr_str((struct sockaddr *)&dst_addr));
  825. LOGI("[udp] cache miss: %s <-> %s", dst, src);
  826. #else
  827. LOGI("[udp] cache miss: %s:%s <-> %s", host, port,
  828. get_addr_str((struct sockaddr *)&src_addr));
  829. #endif
  830. }
  831. } else {
  832. if (verbose) {
  833. #ifdef UDPRELAY_REDIR
  834. char src[SS_ADDRSTRLEN];
  835. char dst[SS_ADDRSTRLEN];
  836. strcpy(src, get_addr_str((struct sockaddr *)&src_addr));
  837. strcpy(dst, get_addr_str((struct sockaddr *)&dst_addr));
  838. LOGI("[udp] cache hit: %s <-> %s", dst, src);
  839. #else
  840. LOGI("[udp] cache hit: %s:%s <-> %s", host, port,
  841. get_addr_str((struct sockaddr *)&src_addr));
  842. #endif
  843. }
  844. }
  845. #ifdef UDPRELAY_LOCAL
  846. #if !defined(UDPRELAY_TUNNEL) && !defined(UDPRELAY_REDIR)
  847. if (frag) {
  848. LOGE("[udp] drop a message since frag is not 0, but %d", frag);
  849. goto CLEAN_UP;
  850. }
  851. #endif
  852. const struct sockaddr *remote_addr = server_ctx->remote_addr;
  853. const int remote_addr_len = server_ctx->remote_addr_len;
  854. if (remote_ctx == NULL) {
  855. // Bind to any port
  856. int remotefd = create_remote_socket(remote_addr->sa_family == AF_INET6);
  857. if (remotefd < 0) {
  858. ERROR("[udp] udprelay bind() error");
  859. goto CLEAN_UP;
  860. }
  861. setnonblocking(remotefd);
  862. #ifdef SO_NOSIGPIPE
  863. set_nosigpipe(remotefd);
  864. #endif
  865. #ifdef SET_INTERFACE
  866. if (server_ctx->iface) {
  867. setinterface(remotefd, server_ctx->iface);
  868. }
  869. #endif
  870. // Init remote_ctx
  871. remote_ctx = new_remote(remotefd, server_ctx);
  872. remote_ctx->src_addr = src_addr;
  873. #ifdef UDPRELAY_REDIR
  874. memcpy(&(remote_ctx->dst_addr), &dst_addr, get_sockaddr_len((struct sockaddr *)&dst_addr));
  875. #endif
  876. remote_ctx->af = remote_addr->sa_family;
  877. remote_ctx->addr_header_len = addr_header_len;
  878. memcpy(remote_ctx->addr_header, addr_header, addr_header_len);
  879. // Add to conn cache
  880. cache_insert(conn_cache, key, (void *)remote_ctx);
  881. // Start remote io
  882. ev_io_start(EV_A_ & remote_ctx->io);
  883. ev_timer_start(EV_A_ & remote_ctx->watcher);
  884. }
  885. if (offset > 0) {
  886. buf_len -= offset;
  887. memmove(buf, buf + offset, buf_len);
  888. }
  889. buf = ss_encrypt_all(BUF_SIZE, buf, &buf_len, server_ctx->method);
  890. int s = sendto(remote_ctx->fd, buf, buf_len, 0, remote_addr, remote_addr_len);
  891. if (s == -1) {
  892. ERROR("[udp] sendto_remote");
  893. }
  894. #else
  895. int cache_hit = 0;
  896. int need_query = 0;
  897. if (remote_ctx != NULL) {
  898. cache_hit = 1;
  899. // detect destination mismatch
  900. if (remote_ctx->addr_header_len != addr_header_len
  901. || memcmp(addr_header, remote_ctx->addr_header, addr_header_len) != 0) {
  902. if (storage.ss_family == AF_INET || storage.ss_family == AF_INET6) {
  903. remote_ctx->dst_addr = storage;
  904. } else {
  905. need_query = 1;
  906. }
  907. }
  908. } else {
  909. if (storage.ss_family == AF_INET || storage.ss_family == AF_INET6) {
  910. int remotefd = create_remote_socket(storage.ss_family == AF_INET6);
  911. if (remotefd != -1) {
  912. setnonblocking(remotefd);
  913. #ifdef SO_BROADCAST
  914. set_broadcast(remotefd);
  915. #endif
  916. #ifdef SO_NOSIGPIPE
  917. set_nosigpipe(remotefd);
  918. #endif
  919. #ifdef SET_INTERFACE
  920. if (server_ctx->iface) {
  921. setinterface(remotefd, server_ctx->iface);
  922. }
  923. #endif
  924. remote_ctx = new_remote(remotefd, server_ctx);
  925. remote_ctx->src_addr = src_addr;
  926. remote_ctx->dst_addr = storage;
  927. remote_ctx->server_ctx = server_ctx;
  928. remote_ctx->addr_header_len = addr_header_len;
  929. memcpy(remote_ctx->addr_header, addr_header, addr_header_len);
  930. } else {
  931. ERROR("[udp] bind() error");
  932. goto CLEAN_UP;
  933. }
  934. }
  935. }
  936. if (remote_ctx != NULL && !need_query) {
  937. size_t addr_len = get_sockaddr_len((struct sockaddr *)&remote_ctx->dst_addr);
  938. int s = sendto(remote_ctx->fd, buf + addr_header_len,
  939. buf_len - addr_header_len, 0,
  940. (struct sockaddr *)&remote_ctx->dst_addr, addr_len);
  941. if (s == -1) {
  942. ERROR("[udp] sendto_remote");
  943. if (!cache_hit) {
  944. close_and_free_remote(EV_A_ remote_ctx);
  945. }
  946. } else {
  947. if (!cache_hit) {
  948. // Add to conn cache
  949. remote_ctx->af = remote_ctx->dst_addr.ss_family;
  950. char *key = hash_key(remote_ctx->af, &remote_ctx->src_addr);
  951. cache_insert(server_ctx->conn_cache, key,
  952. (void *)remote_ctx);
  953. ev_io_start(EV_A_ & remote_ctx->io);
  954. ev_timer_start(EV_A_ & remote_ctx->watcher);
  955. }
  956. }
  957. } else {
  958. struct addrinfo hints;
  959. memset(&hints, 0, sizeof(hints));
  960. hints.ai_family = AF_UNSPEC;
  961. hints.ai_socktype = SOCK_DGRAM;
  962. hints.ai_protocol = IPPROTO_UDP;
  963. struct query_ctx *query_ctx = new_query_ctx(buf + addr_header_len,
  964. buf_len -
  965. addr_header_len);
  966. query_ctx->server_ctx = server_ctx;
  967. query_ctx->addr_header_len = addr_header_len;
  968. query_ctx->src_addr = src_addr;
  969. memcpy(query_ctx->addr_header, addr_header, addr_header_len);
  970. if (need_query) {
  971. query_ctx->remote_ctx = remote_ctx;
  972. }
  973. struct ResolvQuery *query = resolv_query(host, query_resolve_cb,
  974. NULL, query_ctx, htons(atoi(port)));
  975. if (query == NULL) {
  976. ERROR("[udp] unable to create DNS query");
  977. close_and_free_query(EV_A_ query_ctx);
  978. goto CLEAN_UP;
  979. }
  980. query_ctx->query = query;
  981. }
  982. #endif
  983. CLEAN_UP:
  984. free(buf);
  985. }
  986. void free_cb(void *element)
  987. {
  988. struct remote_ctx *remote_ctx = (struct remote_ctx *)element;
  989. if (verbose) {
  990. LOGI("[udp] one connection freed");
  991. }
  992. close_and_free_remote(EV_DEFAULT, remote_ctx);
  993. }
  994. int init_udprelay(const char *server_host, const char *server_port,
  995. #ifdef UDPRELAY_LOCAL
  996. const struct sockaddr *remote_addr, const int remote_addr_len,
  997. #ifdef UDPRELAY_TUNNEL
  998. const ss_addr_t tunnel_addr,
  999. #endif
  1000. #endif
  1001. int method, int timeout, const char *iface)
  1002. {
  1003. // Inilitialize ev loop
  1004. struct ev_loop *loop = EV_DEFAULT;
  1005. // Inilitialize cache
  1006. struct cache *conn_cache;
  1007. cache_create(&conn_cache, MAX_UDP_CONN_NUM, free_cb);
  1008. //////////////////////////////////////////////////
  1009. // Setup server context
  1010. // Bind to port
  1011. int serverfd = create_server_socket(server_host, server_port);
  1012. if (serverfd < 0) {
  1013. FATAL("[udp] bind() error");
  1014. }
  1015. setnonblocking(serverfd);
  1016. struct server_ctx *server_ctx = new_server_ctx(serverfd);
  1017. #ifdef UDPRELAY_REMOTE
  1018. server_ctx->loop = loop;
  1019. #endif
  1020. server_ctx->timeout = min(timeout, MAX_UDP_TIMEOUT);
  1021. server_ctx->method = method;
  1022. server_ctx->iface = iface;
  1023. server_ctx->conn_cache = conn_cache;
  1024. #ifdef UDPRELAY_LOCAL
  1025. server_ctx->remote_addr = remote_addr;
  1026. server_ctx->remote_addr_len = remote_addr_len;
  1027. #ifdef UDPRELAY_TUNNEL
  1028. server_ctx->tunnel_addr = tunnel_addr;
  1029. #endif
  1030. #endif
  1031. ev_io_start(loop, &server_ctx->io);
  1032. server_ctx_list[server_num++] = server_ctx;
  1033. return 0;
  1034. }
  1035. void free_udprelay()
  1036. {
  1037. struct ev_loop *loop = EV_DEFAULT;
  1038. while (server_num-- > 0) {
  1039. struct server_ctx *server_ctx = server_ctx_list[server_num];
  1040. ev_io_stop(loop, &server_ctx->io);
  1041. close(server_ctx->fd);
  1042. cache_delete(server_ctx->conn_cache, 0);
  1043. free(server_ctx);
  1044. server_ctx_list[server_num] = NULL;
  1045. }
  1046. }