forked from jcyfkimi/aprs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocal.toudp.c
129 lines (113 loc) · 2.52 KB
/
local.toudp.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
/* local.toudp.c v1.0 by [email protected] 2015.12.19
connect to 127.0.0.1 tcp 14580 port, login
send all packets to udp
127.0.0.1 14583
*/
#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 PORT 14580
int debug = 0;
int r_fd;
#include "sendudp.c"
void relayaprs(char *buf, int len)
{
FILE *fp;
fp = fopen("/usr/src/aprs/local.udpdest", "r");
if (fp == NULL) {
fprintf(stderr, "open host error\n");
return;
}
char hbuf[MAXLEN];
while (fgets(hbuf, MAXLEN, fp)) {
char *p;
if (strlen(hbuf) < 5)
continue;
if (hbuf[strlen(hbuf) - 1] == '\n')
hbuf[strlen(hbuf) - 1] = 0;
p = strchr(hbuf, ':');
if (p) {
*p = 0;
p++;
sendudp(buf, len, hbuf, atoi(p));
} else
sendudp(buf, len, hbuf, PORT);
}
fclose(fp);
}
#include "passcode.c"
void Process(char *server, char *call)
{
char buffer[MAXLEN];
int n;
int optval;
socklen_t optlen = sizeof(optval);
r_fd = Tcp_connect(server, "14580");
optval = 1;
Setsockopt(r_fd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
optval = 3;
Setsockopt(r_fd, SOL_TCP, TCP_KEEPCNT, &optval, optlen);
optval = 2;
Setsockopt(r_fd, SOL_TCP, TCP_KEEPIDLE, &optval, optlen);
optval = 2;
Setsockopt(r_fd, SOL_TCP, TCP_KEEPINTVL, &optval, optlen);
snprintf(buffer, MAXLEN, "user %s pass %d vers aprsfwd 1.5 filter b/B*/VR2*/XX9*\r\n", call, passcode(call));
Write(r_fd, buffer, strlen(buffer));
while (1) {
n = Readline(r_fd, buffer, MAXLEN);
if (n <= 0) {
exit(0);
}
if (buffer[0] == '#')
continue;
buffer[n] = 0;
relayaprs(buffer, n);
}
}
void usage()
{
printf("\nlocal.toudp v1.0 - aprs relay by [email protected]\n");
printf("\nlocal.toudp x.x.x.x CALL\n\n");
exit(0);
}
int main(int argc, char *argv[])
{
char *call = "CNGD-1";
char *server = "127.0.0.1";
signal(SIGCHLD, SIG_IGN);
if (argc == 3) {
server = argv[1];
call = argv[2];
} else if (argc != 1) {
printf("local.toudp server call\n");
exit(0);
}
#ifndef DEBUG
daemon_init("local.toudp", 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
}
#endif
Process(server, call);
return (0);
}