tunnel.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "tunnel.h"
  2. #include "server.h"
  3. #include "client.h"
  4. #include "buffer.h"
  5. #include <signal.h>
  6. #include <unistd.h>
  7. static int pid = -1;
  8. static void
  9. set_terminated(int siga) {
  10. int buffer = 1;
  11. write(pid, &buffer, 1);
  12. fprintf(stderr, "%s Receive exit signal,please wait.\n", get_time());
  13. struct sigaction sa;
  14. sa.sa_handler = SIG_IGN;
  15. sigaction(SIGTERM, &sa, 0);
  16. sigaction(SIGINT, &sa, 0);
  17. }
  18. static void
  19. deal_signal() {
  20. struct sigaction sa, oldsa;
  21. sa.sa_handler = SIG_IGN;
  22. sigaction(SIGPIPE, &sa, 0);
  23. sa.sa_handler = set_terminated;
  24. sa.sa_flags = SA_NODEFER;
  25. sigemptyset(&sa.sa_mask);
  26. //kill
  27. sigaction(SIGTERM, &sa, &oldsa);
  28. //ctrl + c
  29. sigaction(SIGINT, &sa, &oldsa);
  30. }
  31. static int
  32. check_port(int port_1, int port_2) {
  33. if (port_1 <= 0 || port_1 > 65535 || port_1 <= 0 || port_2 >= 65535) {
  34. return 0;
  35. }
  36. return 1;
  37. }
  38. char *
  39. get_time() {
  40. //not for mul theread
  41. static char st[50] = { 0 };
  42. time_t tNow = time(NULL);
  43. struct tm* ptm = localtime(&tNow);
  44. strftime(st, 50, "%Y-%m-%d %H:%M:%S", ptm);
  45. return st;
  46. }
  47. static void
  48. do_server(int p1, int p2, int pid) {
  49. struct server_param sp;
  50. sp.listen_port[0] = p1;
  51. sp.listen_port[1] = p2;
  52. sp.pid = pid;
  53. pthread_t tid = start_server(&sp);
  54. pthread_join(tid, NULL);
  55. fprintf(stderr, "%s SERVER EXIT SUCCESS.................\n", get_time());
  56. }
  57. static void
  58. do_client(const char* ip, int p1, int p2, int pid) {
  59. struct client_param cp;
  60. cp.p1 = p1;
  61. cp.p2 = p2;
  62. cp.pid = pid;
  63. strncpy(cp.remote_ip, ip, sizeof(cp.remote_ip));
  64. pthread_t tid = start_client(&cp);
  65. pthread_join(tid, NULL);
  66. fprintf(stderr, "%s CLIENT EXIT SUCCESS.................\n", get_time());
  67. }
  68. static void
  69. error_param_tip() {
  70. fprintf(stderr, "Usage:\n1.transfer -s port1[bind port for ssh server] prot2[bind port for ssh client]\n2.transfer -c remoteIp[remote server ip] port1[connect port to remote server] prot2[connect port to local ssh server]\n");
  71. }
  72. int
  73. main(int argc, char *argv[]) {
  74. //usage:
  75. //-s port1[bind port for ssh server] prot2[bind port for ssh client]
  76. //-c port1[connect port to local ssh server] prot2[connect port to remote server]
  77. if (argc <= 1){
  78. error_param_tip();
  79. return 0;
  80. }
  81. int port_1, port_2;
  82. int fd[2];
  83. if (pipe(fd)) {
  84. fprintf(stderr, "%s create pipe error.................\n", get_time());
  85. return -1;
  86. }
  87. pid = fd[1];
  88. deal_signal();
  89. if (argc == 4){
  90. if (strncmp(argv[1], "-s", 2) == 0){
  91. //do server
  92. port_1 = atoi(argv[2]);
  93. port_2 = atoi(argv[3]);
  94. if (!check_port(port_1, port_2)) {
  95. error_param_tip();
  96. goto __fails;
  97. }
  98. do_server(port_1, port_2, fd[0]);
  99. } else{
  100. goto __fails;
  101. }
  102. } else if (argc == 5) {
  103. if (strncmp(argv[1], "-c", 2) == 0){
  104. // do client
  105. const char* ip = argv[2];
  106. port_1 = atoi(argv[3]);
  107. port_2 = atoi(argv[4]);
  108. if (!check_port(port_1, port_2)) {
  109. error_param_tip();
  110. goto __fails;
  111. }
  112. do_client(ip, port_1, port_2, fd[0]);
  113. } else{
  114. error_param_tip();
  115. }
  116. }
  117. __fails:
  118. close(fd[0]);
  119. close(fd[1]);
  120. return 0;
  121. }