forked from BI7ILX/aprs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaprstcp.c
240 lines (209 loc) · 5.76 KB
/
aprstcp.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* aprstcp v1.0 by [email protected] 2015.12.19
aprstcp [ -d ] local_ip local_port remote_ip remote_port
功能:
从 14580 tcp端口接收数据
使用TCP转发给asia.aprs2.net
使用UDP转发给以下端口
127.0.0.1 14582
127.0.0.1 14583
120.25.100.30 14580 (aprs.helloce.net)
106.15.35.48 14580 (欧讯服务器)
如果SSID中有-13,发给
114.55.54.60 14580(lewei50.com)
aprscmdtcp从14590 tcp端口接收数据,并且会处理命令的传递
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include "sock.h"
#include <ctype.h>
#define MAXLEN 16384
#define MAXADDRLEN 100
#define PORT 14580
int debug = 0;
char *laddr, *lport, *raddr, *rport;
unsigned long fwd, rfwd;
int r_fd, c_fd;
char scaddr[MAXADDRLEN], sladdr[MAXADDRLEN], sraddr[MAXADDRLEN], srcaddr[MAXADDRLEN];
#include "printaddr.c"
void PrintStats(void)
{
syslog(LOG_INFO, "%s->%s ", scaddr, sladdr);
syslog(LOG_INFO, "==> %s->%s\n", srcaddr, sraddr);
syslog(LOG_INFO, "===> %8lu bytes\n", fwd);
syslog(LOG_INFO, "<=== %8lu bytes\n", rfwd);
}
#include "sendudp.c"
void relayaprs(char *buf, int len)
{
sendudp(buf, len, "120.25.100.30", 14580); // forward to aprs.hellocq.net
sendudp(buf, len, "106.15.35.48", 14580); // forward to ouxun server
sendudp(buf, len, "127.0.0.1", 14582); // udptolog
sendudp(buf, len, "127.0.0.1", 14583); // udptomysql
if (strstr(buf, "-13>"))
sendudp(buf, len, "114.55.54.60", 14580); // forward -13 to lewei50.comI
}
void Process(int c_fd)
{
fd_set rset;
struct timeval tv;
int m, n;
int max_fd;
struct sockaddr_in6 sa;
int salen;
fwd = rfwd = 0;
r_fd = Tcp_connect(raddr, rport);
scaddr[0] = sladdr[0] = sraddr[0] = srcaddr[0] = 0;
salen = sizeof(sa);
if (getpeername(c_fd, (struct sockaddr *)&sa, (socklen_t *) & salen) == 0)
strncpy(scaddr, PrintAddr((struct sockaddr *)&sa), MAXADDRLEN);
salen = sizeof(sa);
if (getsockname(c_fd, (struct sockaddr *)&sa, (socklen_t *) & salen) == 0)
strncpy(sladdr, PrintAddr((struct sockaddr *)&sa), MAXADDRLEN);
salen = sizeof(sa);
if (getpeername(r_fd, (struct sockaddr *)&sa, (socklen_t *) & salen) == 0)
strncpy(sraddr, PrintAddr((struct sockaddr *)&sa), MAXADDRLEN);
salen = sizeof(sa);
if (getsockname(r_fd, (struct sockaddr *)&sa, (socklen_t *) & salen) == 0)
strncpy(srcaddr, PrintAddr((struct sockaddr *)&sa), MAXADDRLEN);
int optval = 1;
socklen_t optlen = sizeof(optval);
setsockopt(c_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&optval, optlen);
setsockopt(r_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&optval, optlen);
setsockopt(c_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
setsockopt(r_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
optval = 3;
setsockopt(c_fd, SOL_TCP, TCP_KEEPCNT, &optval, optlen);
setsockopt(r_fd, SOL_TCP, TCP_KEEPCNT, &optval, optlen);
optval = 120;
setsockopt(c_fd, SOL_TCP, TCP_KEEPIDLE, &optval, optlen);
setsockopt(r_fd, SOL_TCP, TCP_KEEPIDLE, &optval, optlen);
optval = 5;
setsockopt(c_fd, SOL_TCP, TCP_KEEPINTVL, &optval, optlen);
setsockopt(r_fd, SOL_TCP, TCP_KEEPINTVL, &optval, optlen);
while (1) {
FD_ZERO(&rset);
FD_SET(c_fd, &rset);
FD_SET(r_fd, &rset);
max_fd = max(c_fd, r_fd);
tv.tv_sec = 300;
tv.tv_usec = 0;
m = Select(max_fd + 1, &rset, NULL, NULL, &tv);
if (m == 0)
continue;
if (FD_ISSET(r_fd, &rset)) {
char buffer[MAXLEN];
n = recv(r_fd, buffer, MAXLEN, 0);
if (n <= 0) {
PrintStats();
exit(0);
}
buffer[n] = 0;
if (debug)
fprintf(stderr, "from server: %s", buffer);
Write(c_fd, buffer, n);
rfwd += n;
}
if (FD_ISSET(c_fd, &rset)) {
static char buffer[MAXLEN];
static int lastread = 0;
n = recv(c_fd, buffer + lastread, MAXLEN - lastread - 1, 0);
if (n <= 0) {
PrintStats();
exit(0);
}
buffer[lastread + n] = 0;
if (debug)
fprintf(stderr, "from client: %s", buffer);
Write(r_fd, buffer + lastread, n);
fwd += n;
char *p, *s;
n = lastread + n;
p = buffer;
while (1) {
if ((p - buffer) >= n)
break;
s = strchr(p, '\n');
if (s == NULL)
s = strchr(p, '\r');
if (s == NULL)
break;
relayaprs(p, s - p + 1);
p = s + 1;
}
if ((p - buffer) < n) {
lastread = n - (p - buffer);
memcpy(buffer, p, lastread);
} else
lastread = 0;
}
}
}
void usage()
{
printf("\naprstcp v1.0 - aprs relay by [email protected]\n");
printf("aprstcp [ -d ] [ local_ip local_port remote_ip remote_port ]\n");
printf("default is: aprstcp 0.0.0.0 14580 asia.aprs2.net 14580\n\n");
exit(0);
}
int main(int argc, char *argv[])
{
int listen_fd;
int llen;
int i = 1;
int got_one = 0;
do {
got_one = 1;
if (argc - i == 0)
break;
if (strcmp(argv[i], "-h") == 0)
usage();
else if (strcmp(argv[i], "-d") == 0)
debug = 1;
else
got_one = 0;
if (got_one)
i++;
} while (got_one);
if (argc - i == 0) {
laddr = "0.0.0.0";
lport = "14580";
raddr = "asia.aprs2.net";
rport = "14580";
} else if (argc - i == 4) {
laddr = argv[i];
lport = argv[i + 1];
raddr = argv[i + 2];
rport = argv[i + 3];
} else
usage();
printf("aprstcp %s:%s -> %s:%s\n", laddr, lport, raddr, rport);
signal(SIGCHLD, SIG_IGN);
if (debug == 0)
daemon_init("aprstcp", LOG_DAEMON);
listen_fd = Tcp_listen(laddr, lport, (socklen_t *) & llen);
while (1) {
struct sockaddr sa;
int slen;
slen = sizeof(sa);
c_fd = Accept(listen_fd, &sa, (socklen_t *) & slen);
if (debug)
Process(c_fd);
else {
if (Fork() == 0) {
Close(listen_fd);
Process(c_fd);
}
}
Close(c_fd);
}
}