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