proxy.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _GNU_SOURCE
  19. #include "smartdns/proxy.h"
  20. #include "smartdns/util.h"
  21. #include "proxy.h"
  22. #include "client_socket.h"
  23. #include <sys/epoll.h>
  24. #include <sys/socket.h>
  25. int _dns_proxy_handshake(struct dns_server_info *server_info, struct epoll_event *event, unsigned long now)
  26. {
  27. struct epoll_event fd_event;
  28. proxy_handshake_state ret;
  29. int fd;
  30. int retval = -1;
  31. int epoll_op = EPOLL_CTL_MOD;
  32. pthread_mutex_lock(&server_info->lock);
  33. if (server_info->proxy == NULL) {
  34. pthread_mutex_unlock(&server_info->lock);
  35. return -1;
  36. }
  37. ret = proxy_conn_handshake(server_info->proxy);
  38. fd = server_info->fd;
  39. if (ret == PROXY_HANDSHAKE_OK) {
  40. pthread_mutex_unlock(&server_info->lock);
  41. return 0;
  42. }
  43. if (ret == PROXY_HANDSHAKE_ERR || fd < 0) {
  44. goto errout;
  45. }
  46. memset(&fd_event, 0, sizeof(fd_event));
  47. if (ret == PROXY_HANDSHAKE_CONNECTED) {
  48. fd_event.events = EPOLLIN;
  49. if (server_info->type == DNS_SERVER_UDP || server_info->type == DNS_SERVER_HTTP3 ||
  50. server_info->type == DNS_SERVER_QUIC) {
  51. epoll_ctl(client.epoll_fd, EPOLL_CTL_DEL, fd, NULL);
  52. event->events = 0;
  53. fd = proxy_conn_get_udpfd(server_info->proxy);
  54. if (fd < 0) {
  55. tlog(TLOG_ERROR, "get udp fd failed");
  56. goto errout;
  57. }
  58. set_fd_nonblock(fd, 1);
  59. if (server_info->so_mark >= 0) {
  60. unsigned int so_mark = server_info->so_mark;
  61. if (setsockopt(fd, SOL_SOCKET, SO_MARK, &so_mark, sizeof(so_mark)) != 0) {
  62. tlog(TLOG_DEBUG, "set socket mark failed, %s", strerror(errno));
  63. }
  64. }
  65. server_info->fd = fd;
  66. epoll_op = EPOLL_CTL_ADD;
  67. if (server_info->type == DNS_SERVER_UDP) {
  68. server_info->status = DNS_SERVER_STATUS_CONNECTED;
  69. } else {
  70. /* do handshake for quic */
  71. server_info->status = DNS_SERVER_STATUS_CONNECTING;
  72. fd_event.events |= EPOLLOUT;
  73. }
  74. } else {
  75. fd_event.events |= EPOLLOUT;
  76. }
  77. retval = 0;
  78. }
  79. if (ret == PROXY_HANDSHAKE_WANT_READ) {
  80. fd_event.events = EPOLLIN;
  81. } else if (ret == PROXY_HANDSHAKE_WANT_WRITE) {
  82. fd_event.events = EPOLLOUT | EPOLLIN;
  83. }
  84. fd_event.data.ptr = server_info;
  85. if (epoll_ctl(client.epoll_fd, epoll_op, fd, &fd_event) != 0) {
  86. tlog(TLOG_ERROR, "epoll ctl failed, %s", strerror(errno));
  87. goto errout;
  88. }
  89. pthread_mutex_unlock(&server_info->lock);
  90. return retval;
  91. errout:
  92. server_info->recv_buff.len = 0;
  93. server_info->send_buff.len = 0;
  94. if (server_info->proxy) {
  95. _dns_client_close_socket(server_info);
  96. }
  97. pthread_mutex_unlock(&server_info->lock);
  98. return -1;
  99. }