forked from BI7ILX/aprs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudptoaprs.c
183 lines (166 loc) · 3.59 KB
/
udptoaprs.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
/*
udptoaprs [ -d ] 呼号
参数含义:呼号,用来连接china.aprs2.net服务器
功能:
从 127.0.0.1 14581 UDP端口接收数据
使用TCP转发给china.aprs2.net
注意:BA BD BG BH BR BI BY VR +数字 的才转发
*/
#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 <ctype.h>
#include "sock.h"
int debug = 0;
#define MAXLEN 16384
int checkcall(char *call)
{
char *p;
if (strlen(call) < 5)
return 0;
p = call;
// 第3位必须是数字
if (!isdigit(*(p + 2)))
return 0;
// BA BD BG BH BI BL BM BR BV BX BY
if (*p == 'B') {
if (*(p + 1) == 'A' ||
*(p + 1) == 'D' ||
*(p + 1) == 'G' ||
*(p + 1) == 'H' ||
*(p + 1) == 'I' || *(p + 1) == 'L' || *(p + 1) == 'M' || *(p + 1) == 'R' || *(p + 1) == 'V' || *(p + 1) == 'X' || *(p + 1) == 'Y')
return 1;
}
// VR2
if (*p == 'V') {
if (*(p + 1) == 'R' && *(p + 2) == '2')
return 1;
}
// XX9
if (*p == 'X') {
if (*(p + 1) == 'X' && *(p + 2) == '9')
return 1;
}
return 0;
}
void Process(int u_fd, int r_fd)
{
fd_set rset;
char buff[MAXLEN];
struct timeval tv;
int m, n;
int max_fd;
char call[MAXLEN];
int enable = 1;
setsockopt(r_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&enable, sizeof(enable));
while (1) {
FD_ZERO(&rset);
FD_SET(u_fd, &rset);
FD_SET(r_fd, &rset);
max_fd = max(u_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)) {
n = recv(r_fd, buff, MAXLEN, 0);
if ((n <= 0) && (errno == EINTR))
continue;
if (n <= 0) {
err_sys("recv get %d from tcp server\n", n);
exit(0);
}
buff[n] = 0;
if (debug)
fprintf(stderr, "S: %s", buff);
}
if (FD_ISSET(u_fd, &rset)) {
n = recv(u_fd, buff, MAXLEN, 0);
if (n < 0) {
err_sys("recv get %d from udp client\n", n);
exit(0);
}
if (n == 0)
continue;
buff[n] = 0;
if (debug)
fprintf(stderr, "C: %s", buff);
strcpy(call, buff);
char *p;
p = strchr(call, '>');
if (p)
*p = 0;
if (checkcall(call) == 0) {
if (debug)
fprintf(stderr, "!! %s is not a china call!!!\n", call);
continue;
}
Write(r_fd, buff, n);
}
}
}
#include "passcode.c"
void usage()
{
printf("\nudptoaprs [ -d ] your_call_sign\n");
exit(0);
}
int main(int argc, char *argv[])
{
int r_fd, u_fd;
int llen;
char buf[MAXLEN];
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 != 1)
usage();
signal(SIGCHLD, SIG_IGN);
if (debug == 0) {
daemon_init("udptoaprs", LOG_DAEMON);
while (1) {
int pid;
pid = fork();
if (pid == 0) // i am child, will do the job
break;
else if (pid == -1) // error
exit(0);
else
wait(NULL); // i am parent, wait for child
sleep(2); // if child exit, wait 2 second, and rerun
}
}
err_msg("starting\n");
u_fd = Udp_server("127.0.0.1", "14581", (socklen_t *) & llen);
r_fd = Tcp_connect("asia.aprs2.net", "14580");
snprintf(buf, MAXLEN, "user %s pass %d vers udptoaprs 1.0 filter b/B*/VR2*/XX9*\r\n", argv[i], passcode(argv[i]));
Write(r_fd, buf, strlen(buf));
if (debug) {
fprintf(stderr, "C: %s", buf);
fprintf(stderr, "u_fd=%d, r_fd=%d\n", u_fd, r_fd);
}
Process(u_fd, r_fd);
return 0;
}