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.

920 lines
23 KiB

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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <fcntl.h>
  4. #include <locale.h>
  5. #include <signal.h>
  6. #include <string.h>
  7. #include <strings.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #ifndef __MINGW32__
  11. #include <arpa/inet.h>
  12. #include <errno.h>
  13. #include <netdb.h>
  14. #include <netinet/in.h>
  15. #include <netinet/tcp.h>
  16. #include <pthread.h>
  17. #endif
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H) && defined(__linux__)
  22. #include <net/if.h>
  23. #include <sys/ioctl.h>
  24. #define SET_INTERFACE
  25. #endif
  26. #ifdef __MINGW32__
  27. #include "win32.h"
  28. #endif
  29. #include "utils.h"
  30. #include "udprelay.h"
  31. #include "cache.h"
  32. #ifdef UDPRELAY_REMOTE
  33. #ifdef UDPRELAY_LOCAL
  34. #error "UDPRELAY_REMOTE and UDPRELAY_LOCAL should not be both defined"
  35. #endif
  36. #endif
  37. #ifndef EAGAIN
  38. #define EAGAIN EWOULDBLOCK
  39. #endif
  40. #ifndef EWOULDBLOCK
  41. #define EWOULDBLOCK EAGAIN
  42. #endif
  43. #define BUF_SIZE MAX_UDP_PACKET_SIZE
  44. extern int verbose;
  45. #ifndef __MINGW32__
  46. static int setnonblocking(int fd)
  47. {
  48. int flags;
  49. if (-1 ==(flags = fcntl(fd, F_GETFL, 0)))
  50. flags = 0;
  51. return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  52. }
  53. #endif
  54. #ifdef SET_INTERFACE
  55. static int setinterface(int socket_fd, const char* interface_name)
  56. {
  57. struct ifreq interface;
  58. memset(&interface, 0, sizeof(interface));
  59. strncpy(interface.ifr_name, interface_name, IFNAMSIZ);
  60. int res = setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, &interface, sizeof(struct ifreq));
  61. return res;
  62. }
  63. #endif
  64. static char *hash_key(const char *header, const int header_len, const struct sockaddr *addr)
  65. {
  66. char key[384];
  67. // calculate hash key
  68. // assert header_len < 256
  69. memset(key, 0, 384);
  70. memcpy(key, addr, sizeof(struct sockaddr));
  71. memcpy(key + sizeof(struct sockaddr), header, header_len);
  72. return (char*) enc_md5((const uint8_t *)key, sizeof(struct sockaddr) + header_len, NULL);
  73. }
  74. static int parse_udprealy_header(const char* buf, const int buf_len, char *host, char *port)
  75. {
  76. const uint8_t atyp = *(uint8_t*)buf;
  77. int offset = 1;
  78. // get remote addr and port
  79. if (atyp == 1)
  80. {
  81. // IP V4
  82. size_t in_addr_len = sizeof(struct in_addr);
  83. if (buf_len > in_addr_len)
  84. {
  85. if (host != NULL)
  86. {
  87. inet_ntop(AF_INET, (const void *)(buf + offset),
  88. host, INET_ADDRSTRLEN);
  89. }
  90. offset += in_addr_len;
  91. }
  92. }
  93. else if (atyp == 3)
  94. {
  95. // Domain name
  96. uint8_t name_len = *(uint8_t *)(buf + offset);
  97. if (name_len < buf_len && name_len < 255 && name_len > 0)
  98. {
  99. if (host != NULL)
  100. {
  101. memcpy(host, buf + offset + 1, name_len);
  102. }
  103. offset += name_len + 1;
  104. }
  105. }
  106. else if (atyp == 4)
  107. {
  108. // IP V6
  109. size_t in6_addr_len = sizeof(struct in6_addr);
  110. if (buf_len > in6_addr_len)
  111. {
  112. if (host != NULL)
  113. {
  114. inet_ntop(AF_INET6, (const void*)(buf + offset),
  115. host, INET6_ADDRSTRLEN);
  116. }
  117. offset += in6_addr_len;
  118. }
  119. }
  120. if (offset == 1)
  121. {
  122. LOGE("invalid header with addr type %d", atyp);
  123. return 0;
  124. }
  125. if (port != NULL)
  126. {
  127. sprintf(port, "%d", ntohs(*(uint16_t *)(buf + offset)));
  128. }
  129. offset += 2;
  130. return offset;
  131. }
  132. static char *get_addr_str(const struct sockaddr *sa)
  133. {
  134. static char s[SS_ADDRSTRLEN];
  135. memset(s, 0, SS_ADDRSTRLEN);
  136. char addr[INET6_ADDRSTRLEN] = {0};
  137. char port[PORTSTRLEN] = {0};
  138. uint16_t p;
  139. switch(sa->sa_family)
  140. {
  141. case AF_INET:
  142. inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
  143. addr, INET_ADDRSTRLEN);
  144. p = ntohs(((struct sockaddr_in *)sa)->sin_port);
  145. sprintf(port, "%d", p);
  146. break;
  147. case AF_INET6:
  148. inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
  149. addr, INET6_ADDRSTRLEN);
  150. p = ntohs(((struct sockaddr_in *)sa)->sin_port);
  151. sprintf(port, "%d", p);
  152. break;
  153. default:
  154. strncpy(s, "Unknown AF", SS_ADDRSTRLEN);
  155. }
  156. int addr_len = strlen(addr);
  157. int port_len = strlen(port);
  158. memcpy(s, addr, addr_len);
  159. memcpy(s + addr_len + 1, port, port_len);
  160. s[addr_len] = ':';
  161. return s;
  162. }
  163. int create_remote_socket(int ipv6)
  164. {
  165. int remote_sock;
  166. if (ipv6)
  167. {
  168. // Try to bind IPv6 first
  169. struct sockaddr_in6 addr;
  170. memset(&addr, 0, sizeof(struct sockaddr_in6));
  171. addr.sin6_family = AF_INET6;
  172. addr.sin6_addr = in6addr_any;
  173. addr.sin6_port = htons(0);
  174. remote_sock = socket(AF_INET6, SOCK_DGRAM , 0);
  175. if (remote_sock != -1)
  176. {
  177. if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != -1)
  178. {
  179. return remote_sock;
  180. }
  181. }
  182. }
  183. // Then bind to IPv4
  184. struct sockaddr_in addr;
  185. memset(&addr, 0, sizeof(addr));
  186. addr.sin_family = AF_INET;
  187. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  188. addr.sin_port = htons(0);
  189. remote_sock = socket(AF_INET, SOCK_DGRAM , 0);
  190. if (remote_sock == -1)
  191. {
  192. ERROR("Cannot create socket.");
  193. return -1;
  194. }
  195. if (bind(remote_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0)
  196. {
  197. FATAL("Cannot bind remote.");
  198. return -1;
  199. }
  200. return remote_sock;
  201. }
  202. int create_server_socket(const char *host, const char *port)
  203. {
  204. struct addrinfo hints;
  205. struct addrinfo *result, *rp;
  206. int s, server_sock;
  207. memset(&hints, 0, sizeof(struct addrinfo));
  208. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  209. hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */
  210. s = getaddrinfo(host, port, &hints, &result);
  211. if (s != 0)
  212. {
  213. LOGE("getaddrinfo: %s", gai_strerror(s));
  214. return -1;
  215. }
  216. for (rp = result; rp != NULL; rp = rp->ai_next)
  217. {
  218. server_sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  219. if (server_sock == -1)
  220. continue;
  221. int opt = 1;
  222. setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
  223. #ifdef SO_NOSIGPIPE
  224. setsockopt(server_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
  225. #endif
  226. s = bind(server_sock, rp->ai_addr, rp->ai_addrlen);
  227. if (s == 0)
  228. {
  229. /* We managed to bind successfully! */
  230. break;
  231. }
  232. else
  233. {
  234. ERROR("bind");
  235. }
  236. close(server_sock);
  237. }
  238. if (rp == NULL)
  239. {
  240. LOGE("Could not bind");
  241. return -1;
  242. }
  243. freeaddrinfo(result);
  244. return server_sock;
  245. }
  246. struct remote_ctx *new_remote(int fd, struct server_ctx *server_ctx)
  247. {
  248. struct remote_ctx *ctx = malloc(sizeof(struct remote_ctx));
  249. memset(ctx, 0, sizeof(struct remote_ctx));
  250. ctx->fd = fd;
  251. ctx->server_ctx = server_ctx;
  252. ev_io_init(&ctx->io, remote_recv_cb, fd, EV_READ);
  253. ev_timer_init(&ctx->watcher, remote_timeout_cb, server_ctx->timeout, server_ctx->timeout * 5);
  254. return ctx;
  255. }
  256. struct server_ctx * new_server_ctx(int fd)
  257. {
  258. struct server_ctx *ctx = malloc(sizeof(struct server_ctx));
  259. memset(ctx, 0, sizeof(struct server_ctx));
  260. ctx->fd = fd;
  261. ev_io_init(&ctx->io, server_recv_cb, fd, EV_READ);
  262. return ctx;
  263. }
  264. #ifdef UDPRELAY_REMOTE
  265. struct query_ctx *new_query_ctx(asyncns_query_t *query,
  266. const char *buf, const int buf_len)
  267. {
  268. struct query_ctx *ctx = malloc(sizeof(struct query_ctx));
  269. memset(ctx, 0, sizeof(struct query_ctx));
  270. ctx->buf = malloc(buf_len);
  271. ctx->buf_len = buf_len;
  272. memcpy(ctx->buf, buf, buf_len);
  273. ctx->query = query;
  274. return ctx;
  275. }
  276. void close_and_free_query(EV_P_ struct query_ctx *ctx)
  277. {
  278. if (ctx != NULL)
  279. {
  280. if (ctx->buf != NULL)
  281. {
  282. free(ctx->buf);
  283. }
  284. free(ctx);
  285. }
  286. }
  287. #endif
  288. void close_and_free_remote(EV_P_ struct remote_ctx *ctx)
  289. {
  290. if (ctx != NULL)
  291. {
  292. ev_timer_stop(EV_A_ &ctx->watcher);
  293. ev_io_stop(EV_A_ &ctx->io);
  294. close(ctx->fd);
  295. free(ctx);
  296. }
  297. }
  298. static void remote_timeout_cb(EV_P_ ev_timer *watcher, int revents)
  299. {
  300. struct remote_ctx *remote_ctx = (struct remote_ctx *) (((void*)watcher)
  301. - sizeof(ev_io));
  302. if (verbose)
  303. {
  304. LOGD("UDP connection timeout");
  305. }
  306. char *key = hash_key(remote_ctx->addr_header,
  307. remote_ctx->addr_header_len, &remote_ctx->src_addr);
  308. cache_remove(remote_ctx->server_ctx->conn_cache, key);
  309. }
  310. #ifdef UDPRELAY_REMOTE
  311. static void query_resolve_cb(EV_P_ ev_io *w, int revents)
  312. {
  313. int err;
  314. struct addrinfo *result, *rp;
  315. struct resolve_ctx *resolve_ctx = (struct resolve_ctx*)w;
  316. asyncns_t *asyncns = resolve_ctx->asyncns;
  317. err = asyncns_handle(asyncns);
  318. if (err == ASYNCNS_HANDLE_AGAIN)
  319. {
  320. // try again
  321. return;
  322. }
  323. else if (err == ASYNCNS_HANDLE_ERROR)
  324. {
  325. // asyncns error
  326. FATAL("asyncns exit unexpectedly.");
  327. }
  328. asyncns_query_t *query = asyncns_getnext(asyncns);
  329. struct query_ctx *query_ctx= (struct query_ctx*) asyncns_getuserdata(asyncns, query);
  330. if (!asyncns_isdone(asyncns, query))
  331. {
  332. // wait reolver
  333. return;
  334. }
  335. if (verbose)
  336. {
  337. LOGD("[udp] asyncns resolved.");
  338. }
  339. query_ctx->query = NULL;
  340. err = asyncns_getaddrinfo_done(asyncns, query, &result);
  341. if (err)
  342. {
  343. ERROR("getaddrinfo");
  344. }
  345. else
  346. {
  347. // Use IPV4 address if possible
  348. for (rp = result; rp != NULL; rp = rp->ai_next)
  349. {
  350. if (rp->ai_family == AF_INET) break;
  351. }
  352. if (rp == NULL)
  353. {
  354. rp = result;
  355. }
  356. int remotefd = create_remote_socket(rp->ai_family == AF_INET6);
  357. if (remotefd != -1)
  358. {
  359. setnonblocking(remotefd);
  360. #ifdef SO_NOSIGPIPE
  361. int opt = 1;
  362. setsockopt(remotefd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
  363. #endif
  364. #ifdef SET_INTERFACE
  365. if (query_ctx->server_ctx->iface)
  366. setinterface(remotefd, query_ctx->server_ctx->iface);
  367. #endif
  368. struct remote_ctx *remote_ctx = new_remote(remotefd, query_ctx->server_ctx);
  369. remote_ctx->src_addr = query_ctx->src_addr;
  370. remote_ctx->dst_addr = *rp->ai_addr;
  371. remote_ctx->server_ctx = query_ctx->server_ctx;
  372. remote_ctx->addr_header_len = query_ctx->addr_header_len;
  373. memcpy(remote_ctx->addr_header, query_ctx->addr_header, query_ctx->addr_header_len);
  374. // Add to conn cache
  375. char *key = hash_key(remote_ctx->addr_header,
  376. remote_ctx->addr_header_len, &remote_ctx->src_addr);
  377. cache_insert(query_ctx->server_ctx->conn_cache, key, (void *)remote_ctx);
  378. ev_io_start(EV_A_ &remote_ctx->io);
  379. int s = sendto(remote_ctx->fd, query_ctx->buf, query_ctx->buf_len, 0, &remote_ctx->dst_addr, sizeof(remote_ctx->dst_addr));
  380. if (s == -1)
  381. {
  382. ERROR("udprelay_sendto_remote");
  383. close_and_free_remote(EV_A_ remote_ctx);
  384. }
  385. }
  386. else
  387. {
  388. ERROR("udprelay bind() error..");
  389. }
  390. }
  391. // clean up
  392. asyncns_freeaddrinfo(result);
  393. close_and_free_query(EV_A_ query_ctx);
  394. }
  395. #endif
  396. static void remote_recv_cb (EV_P_ ev_io *w, int revents)
  397. {
  398. struct remote_ctx *remote_ctx = (struct remote_ctx *)w;
  399. struct server_ctx *server_ctx = remote_ctx->server_ctx;
  400. // server has been closed
  401. if (server_ctx == NULL)
  402. {
  403. LOGE("invalid server.");
  404. close_and_free_remote(EV_A_ remote_ctx);
  405. return;
  406. }
  407. if (verbose)
  408. {
  409. LOGD("[udp] remote receive a packet");
  410. }
  411. // triger the timer
  412. ev_timer_again(EV_A_ &remote_ctx->watcher);
  413. struct sockaddr src_addr;
  414. socklen_t src_addr_len = sizeof(src_addr);
  415. char *buf = malloc(BUF_SIZE);
  416. // recv
  417. ssize_t buf_len = recvfrom(remote_ctx->fd, buf, BUF_SIZE, 0, &src_addr, &src_addr_len);
  418. if (buf_len == -1)
  419. {
  420. // error on recv
  421. // simply drop that packet
  422. if (verbose)
  423. {
  424. ERROR("udprelay_server_recvfrom");
  425. }
  426. goto CLEAN_UP;
  427. }
  428. #ifdef UDPRELAY_LOCAL
  429. buf = ss_decrypt_all(BUF_SIZE, buf, &buf_len, server_ctx->method);
  430. if (buf == NULL)
  431. {
  432. if (verbose)
  433. {
  434. ERROR("udprelay_server_ss_decrypt_all");
  435. }
  436. goto CLEAN_UP;
  437. }
  438. int len = parse_udprealy_header(buf, buf_len, NULL, NULL);
  439. if (len == 0)
  440. {
  441. LOGD("[udp] Error in parse header");
  442. // error in parse header
  443. goto CLEAN_UP;
  444. }
  445. // server may return using a different address type other than the type we
  446. // have used during sending
  447. #ifdef UDPRELAY_TUNNEL
  448. // Construct packet
  449. buf_len -= len;
  450. memmove(buf, buf + len, buf_len);
  451. #else
  452. // Construct packet
  453. char *tmpbuf = malloc(buf_len + 3);
  454. memset(tmpbuf, 0, 3);
  455. memcpy(tmpbuf + 3, buf, buf_len);
  456. free(buf);
  457. buf = tmpbuf;
  458. buf_len += 3;
  459. #endif
  460. #endif
  461. #ifdef UDPRELAY_REMOTE
  462. unsigned int addr_header_len = remote_ctx->addr_header_len;
  463. // Construct packet
  464. char *tmpbuf = malloc(buf_len + addr_header_len);
  465. memcpy(tmpbuf, remote_ctx->addr_header, addr_header_len);
  466. memcpy(tmpbuf + addr_header_len, buf, buf_len);
  467. free(buf);
  468. buf = tmpbuf;
  469. buf_len += addr_header_len;
  470. buf = ss_encrypt_all(BUF_SIZE, buf, &buf_len, server_ctx->method);
  471. #endif
  472. int s = sendto(server_ctx->fd, buf, buf_len, 0, &remote_ctx->src_addr, sizeof(remote_ctx->src_addr));
  473. if (s == -1)
  474. {
  475. ERROR("udprelay_sendto_local");
  476. }
  477. CLEAN_UP:
  478. free(buf);
  479. }
  480. static void server_recv_cb (EV_P_ ev_io *w, int revents)
  481. {
  482. struct server_ctx *server_ctx = (struct server_ctx *)w;
  483. struct sockaddr src_addr;
  484. char *buf = malloc(BUF_SIZE);
  485. socklen_t src_addr_len = sizeof(src_addr);
  486. unsigned int offset = 0;
  487. ssize_t buf_len = recvfrom(server_ctx->fd, buf, BUF_SIZE, 0, &src_addr, &src_addr_len);
  488. if (buf_len == -1)
  489. {
  490. // error on recv
  491. // simply drop that packet
  492. if (verbose)
  493. {
  494. ERROR("udprelay_server_recvfrom");
  495. }
  496. goto CLEAN_UP;
  497. }
  498. if (verbose)
  499. {
  500. LOGD("[udp] server receive a packet.");
  501. }
  502. #ifdef UDPRELAY_REMOTE
  503. buf = ss_decrypt_all(BUF_SIZE, buf, &buf_len, server_ctx->method);
  504. if (buf == NULL)
  505. {
  506. if (verbose)
  507. {
  508. ERROR("udprelay_server_ss_decrypt_all");
  509. }
  510. goto CLEAN_UP;
  511. }
  512. #endif
  513. #ifdef UDPRELAY_LOCAL
  514. #ifndef UDPRELAY_TUNNEL
  515. uint8_t frag = *(uint8_t*)(buf + 2);
  516. offset += 3;
  517. #endif
  518. #endif
  519. /*
  520. *
  521. * SOCKS5 UDP Request
  522. * +----+------+------+----------+----------+----------+
  523. * |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
  524. * +----+------+------+----------+----------+----------+
  525. * | 2 | 1 | 1 | Variable | 2 | Variable |
  526. * +----+------+------+----------+----------+----------+
  527. *
  528. * SOCKS5 UDP Response
  529. * +----+------+------+----------+----------+----------+
  530. * |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
  531. * +----+------+------+----------+----------+----------+
  532. * | 2 | 1 | 1 | Variable | 2 | Variable |
  533. * +----+------+------+----------+----------+----------+
  534. *
  535. * shadowsocks UDP Request (before encrypted)
  536. * +------+----------+----------+----------+
  537. * | ATYP | DST.ADDR | DST.PORT | DATA |
  538. * +------+----------+----------+----------+
  539. * | 1 | Variable | 2 | Variable |
  540. * +------+----------+----------+----------+
  541. *
  542. * shadowsocks UDP Response (before encrypted)
  543. * +------+----------+----------+----------+
  544. * | ATYP | DST.ADDR | DST.PORT | DATA |
  545. * +------+----------+----------+----------+
  546. * | 1 | Variable | 2 | Variable |
  547. * +------+----------+----------+----------+
  548. *
  549. * shadowsocks UDP Request and Response (after encrypted)
  550. * +-------+--------------+
  551. * | IV | PAYLOAD |
  552. * +-------+--------------+
  553. * | Fixed | Variable |
  554. * +-------+--------------+
  555. *
  556. */
  557. #ifdef UDPRELAY_TUNNEL
  558. char addr_header[256] = {0};
  559. char* host = server_ctx->tunnel_addr.host;
  560. char* port = server_ctx->tunnel_addr.port;
  561. int host_len = strlen(host);
  562. uint16_t port_num = (uint16_t)atoi(port);
  563. uint16_t port_net_num = htons(port_num);
  564. int addr_header_len = 2 + host_len + 2;
  565. // initialize the addr header
  566. addr_header[0] = 3;
  567. addr_header[1] = host_len;
  568. memcpy(addr_header + 2, host, host_len);
  569. memcpy(addr_header + 2 + host_len, &port_net_num, 2);
  570. // reconstruct the buffer
  571. char *tmp = malloc(buf_len + addr_header_len);
  572. memcpy(tmp, addr_header, addr_header_len);
  573. memcpy(tmp + addr_header_len, buf, buf_len);
  574. free(buf);
  575. buf = tmp;
  576. buf_len += addr_header_len;
  577. #else
  578. char host[256] = {0};
  579. char port[64] = {0};
  580. int addr_header_len = parse_udprealy_header(buf + offset,
  581. buf_len - offset, host, port);
  582. if (addr_header_len == 0)
  583. {
  584. // error in parse header
  585. goto CLEAN_UP;
  586. }
  587. char *addr_header = buf + offset;
  588. #endif
  589. char *key = hash_key(addr_header, addr_header_len, &src_addr);
  590. struct cache *conn_cache = server_ctx->conn_cache;
  591. struct remote_ctx *remote_ctx = NULL;
  592. cache_lookup(conn_cache, key, (void*)&remote_ctx);
  593. if (remote_ctx != NULL)
  594. {
  595. if (memcmp(&src_addr, &remote_ctx->src_addr, sizeof(src_addr))
  596. || strcmp(addr_header, remote_ctx->addr_header) != 0)
  597. {
  598. remote_ctx = NULL;
  599. }
  600. }
  601. if (remote_ctx == NULL)
  602. {
  603. if (verbose)
  604. {
  605. LOGD("[udp] cache missed: %s:%s <-> %s", host, port, get_addr_str(&src_addr));
  606. }
  607. }
  608. else
  609. {
  610. if (verbose)
  611. {
  612. LOGD("[udp] cache hit: %s:%s <-> %s", host, port, get_addr_str(&src_addr));
  613. }
  614. }
  615. #ifdef UDPRELAY_LOCAL
  616. #ifndef UDPRELAY_TUNNEL
  617. if (frag)
  618. {
  619. LOGE("drop a message since frag is not 0, but %d", frag);
  620. goto CLEAN_UP;
  621. }
  622. #endif
  623. if (remote_ctx == NULL)
  624. {
  625. struct addrinfo hints;
  626. struct addrinfo *result;
  627. memset(&hints, 0, sizeof(struct addrinfo));
  628. hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
  629. hints.ai_socktype = SOCK_DGRAM; /* We want a UDP socket */
  630. int s = getaddrinfo(server_ctx->remote_host, server_ctx->remote_port,
  631. &hints, &result);
  632. if (s != 0 || result == NULL)
  633. {
  634. LOGE("getaddrinfo: %s", gai_strerror(s));
  635. goto CLEAN_UP;
  636. }
  637. // Bind to any port
  638. int remotefd = create_remote_socket(result->ai_family == AF_INET6);
  639. if (remotefd < 0)
  640. {
  641. ERROR("udprelay bind() error..");
  642. // remember to free addrinfo
  643. freeaddrinfo(result);
  644. goto CLEAN_UP;
  645. }
  646. setnonblocking(remotefd);
  647. #ifdef SO_NOSIGPIPE
  648. int opt = 1;
  649. setsockopt(remotefd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt));
  650. #endif
  651. #ifdef SET_INTERFACE
  652. if (server_ctx->iface)
  653. setinterface(remotefd, server_ctx->iface);
  654. #endif
  655. // Init remote_ctx
  656. remote_ctx = new_remote(remotefd, server_ctx);
  657. remote_ctx->src_addr = src_addr;
  658. remote_ctx->dst_addr = *result->ai_addr;
  659. remote_ctx->addr_header_len = addr_header_len;
  660. memcpy(remote_ctx->addr_header, addr_header, addr_header_len);
  661. // Add to conn cache
  662. cache_insert(conn_cache, key, (void *)remote_ctx);
  663. // Start remote io
  664. ev_io_start(EV_A_ &remote_ctx->io);
  665. // clean up
  666. freeaddrinfo(result);
  667. }
  668. if (offset > 0)
  669. {
  670. buf_len -= offset;
  671. memmove(buf, buf + offset, buf_len);
  672. }
  673. buf = ss_encrypt_all(BUF_SIZE, buf, &buf_len, server_ctx->method);
  674. int s = sendto(remote_ctx->fd, buf, buf_len, 0, &remote_ctx->dst_addr, sizeof(remote_ctx->dst_addr));
  675. if (s == -1)
  676. {
  677. ERROR("udprelay_sendto_remote");
  678. }
  679. #else
  680. if (remote_ctx == NULL)
  681. {
  682. struct addrinfo hints;
  683. asyncns_query_t *query;
  684. memset(&hints, 0, sizeof(hints));
  685. hints.ai_family = AF_UNSPEC;
  686. hints.ai_socktype = SOCK_STREAM;
  687. query = asyncns_getaddrinfo(server_ctx->asyncns,
  688. host, port, &hints);
  689. if (query == NULL)
  690. {
  691. ERROR("udp_asyncns_getaddrinfo");
  692. goto CLEAN_UP;
  693. }
  694. struct query_ctx *query_ctx = new_query_ctx(query, buf + addr_header_len,
  695. buf_len - addr_header_len);
  696. query_ctx->server_ctx = server_ctx;
  697. query_ctx->addr_header_len = addr_header_len;
  698. query_ctx->src_addr = src_addr;
  699. memcpy(query_ctx->addr_header, addr_header, addr_header_len);
  700. asyncns_setuserdata(server_ctx->asyncns, query, query_ctx);
  701. }
  702. else
  703. {
  704. int s = sendto(remote_ctx->fd, buf + addr_header_len,
  705. buf_len - addr_header_len, 0, &remote_ctx->dst_addr, sizeof(remote_ctx->dst_addr));
  706. if (s == -1)
  707. {
  708. ERROR("udprelay_sendto_remote");
  709. }
  710. }
  711. #endif
  712. CLEAN_UP:
  713. free(buf);
  714. }
  715. void free_cb(void *element)
  716. {
  717. struct remote_ctx *remote_ctx = (struct remote_ctx *)element;
  718. if (verbose)
  719. {
  720. LOGD("free a remote ctx");
  721. }
  722. close_and_free_remote(EV_DEFAULT, remote_ctx);
  723. }
  724. int udprelay_init(const char *server_host, const char *server_port,
  725. #ifdef UDPRELAY_LOCAL
  726. const char *remote_host, const char *remote_port,
  727. #ifdef UDPRELAY_TUNNEL
  728. const ss_addr_t tunnel_addr,
  729. #endif
  730. #endif
  731. #ifdef UDPRELAY_REMOTE
  732. int dns_thread_num,
  733. #endif
  734. int method, int timeout, const char *iface)
  735. {
  736. // Inilitialize ev loop
  737. struct ev_loop *loop = EV_DEFAULT;
  738. // Inilitialize cache
  739. struct cache *conn_cache;
  740. cache_create(&conn_cache, MAX_UDP_CONN_NUM, free_cb);
  741. //////////////////////////////////////////////////
  742. // Setup server context
  743. #ifdef UDPRELAY_REMOTE
  744. // setup asyncns
  745. asyncns_t *asyncns;
  746. if (!(asyncns = asyncns_new(dns_thread_num)))
  747. {
  748. FATAL("asyncns failed");
  749. }
  750. struct resolve_ctx *resolve_ctx = malloc(sizeof(struct resolve_ctx));
  751. resolve_ctx->asyncns = asyncns;
  752. int asyncnsfd = asyncns_fd(asyncns);
  753. ev_io_init (&resolve_ctx->io, query_resolve_cb, asyncnsfd, EV_READ);
  754. ev_io_start (loop, &resolve_ctx->io);
  755. #endif
  756. // Bind to port
  757. int serverfd = create_server_socket(server_host, server_port);
  758. if (serverfd < 0)
  759. {
  760. FATAL("udprelay bind() error..");
  761. }
  762. setnonblocking(serverfd);
  763. struct server_ctx *server_ctx = new_server_ctx(serverfd);
  764. server_ctx->timeout = timeout;
  765. server_ctx->method = method;
  766. server_ctx->iface = iface;
  767. server_ctx->conn_cache = conn_cache;
  768. #ifdef UDPRELAY_LOCAL
  769. server_ctx->remote_host = remote_host;
  770. server_ctx->remote_port = remote_port;
  771. #ifdef UDPRELAY_TUNNEL
  772. server_ctx->tunnel_addr = tunnel_addr;
  773. #endif
  774. #endif
  775. #ifdef UDPRELAY_REMOTE
  776. server_ctx->asyncns = asyncns;
  777. #endif
  778. ev_io_start(loop, &server_ctx->io);
  779. return 0;
  780. }