-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoutput.c
296 lines (242 loc) · 5.71 KB
/
output.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "acarsx.h"
typedef struct {
unsigned char mode;
unsigned char addr[8];
unsigned char ack;
unsigned char label[3];
unsigned char bid;
unsigned char no[5];
unsigned char fid[7];
unsigned char bs, be;
unsigned char txt[250];
int err, lvl;
} acarsmsg_t;
static int sockfd = -1;
static FILE *fdout;
int init_output(char *logfilename, char *Rawaddr)
{
char *addr;
char *port;
struct addrinfo hints, *servinfo, *p;
int rv;
if (logfilename) {
fdout = fopen(logfilename, "a+");
if (fdout == NULL) {
fprintf(stderr, "Could not open : %s\n", logfilename);
return -1;
}
} else
fdout = stdout;
if (Rawaddr == NULL)
return 0;
if (Rawaddr[0] == '[') {
addr = Rawaddr;
port = strstr(addr, "]");
if (port == NULL) {
fprintf(stderr, "Invalid IPV6 address\n");
return -1;
}
port++;
if (*port != ':') {
fprintf(stderr, "Invalid address:port\n");
return -1;
}
port++;
} else {
addr = Rawaddr;
port = strstr(addr, ":");
if (port == NULL) {
fprintf(stderr, "Invalid address:port\n");
return -1;
}
*port = 0;
port++;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(addr, port, &hints, &servinfo)) != 0) {
fprintf(stderr, "Invalid/unknown address %s\n", addr);
return -1;
}
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd =
socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("socket");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("connect");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "failed to connect\n");
return -1;
}
freeaddrinfo(servinfo);
return 0;
}
static void printtime(time_t t)
{
struct tm *tmp;
if (t == 0)
return;
tmp = gmtime(&t);
fprintf(fdout, "%02d/%02d/%04d %02d:%02d:%02d",
tmp->tm_mday, tmp->tm_mon + 1, tmp->tm_year + 1900,
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
}
void outnet(acarsmsg_t * msg)
{
char pkt[500];
if (msg->no[0] == '\0')
strcpy(msg->no, "XXXX");
if (msg->fid[0] == '\0')
strcpy(msg->fid, "XXXXXX");
sprintf(pkt, "AC%1c %7s %1c %2s %1c %4s %6s %s",
msg->mode, msg->addr, msg->ack, msg->label, msg->bid, msg->no,
msg->fid, msg->txt);
write(sockfd, pkt, strlen(pkt));
}
static void printmsg(acarsmsg_t * msg, int chn, time_t t)
{
if (channel[chn].inmode == 3)
fprintf(fdout, "\n[CH#%1d F:%3.3f L:%3d E:%1d ", chn + 1,
channel[chn].Fr / 1000000.0, msg->lvl, msg->err);
else
fprintf(fdout, "\n[CH#%1d E:%1d ", chn + 1, msg->err);
if (channel[chn].inmode != 2)
printtime(t);
fprintf(fdout, " --- ]\n");
fprintf(fdout, "ACARS MODE:\t%1c\t", msg->mode);
fprintf(fdout, "Aircraft REG:\t%s\n", msg->addr);
fprintf(fdout, "MSG LABEL:\t%s\t", msg->label);
fprintf(fdout, "Block ID:\t%c\n", msg->bid);
fprintf(fdout, "Tech ACK:\t%c\n", msg->ack);
if (msg->no[0] != '\0' || msg->fid[0] != '\0') {
fprintf(fdout, "MSG NO.:\t%s\t", msg->no);
fprintf(fdout, "Flight ID:\t%s\n", msg->fid);
}
if (strlen(msg->txt) != 0) {
fprintf(fdout, "MSG Body:\n%s\n", msg->txt);
}
if (verbose && msg->be == 0x17)
fprintf(fdout, "------- End Of Message -----\n");
fflush(fdout);
}
static void printoneline(acarsmsg_t * msg, int chn, time_t t)
{
char txt[60];
char *pstr;
strncpy(txt, msg->txt, 60);
txt[59] = 0;
for (pstr = txt; *pstr != 0; pstr++)
if (*pstr == '\n' || *pstr == '\r')
*pstr = ' ';
if (channel[chn].inmode == 3)
fprintf(fdout, "#%1d (L:%3d E:%1d) ", chn + 1, msg->lvl,
msg->err);
else
fprintf(fdout, "#%1d (E:%1d) ", chn + 1, msg->err);
if (channel[chn].inmode != 2)
printtime(t);
/* uplink message, no flight id and no msg no */
if (msg->fid[0] == '\0' && msg->no[0] == '\0')
fprintf(fdout, " %7s %1c %2s ", msg->addr, msg->mode,
msg->label);
else
fprintf(fdout, " %7s %6s %1c %2s %4s ", msg->addr, msg->fid,
msg->mode, msg->label, msg->no);
fprintf(fdout, "%s", txt);
fprintf(fdout, "\n");
fflush(fdout);
}
void outputmsg(const msgblk_t * blk)
{
acarsmsg_t msg;
int i, k;
/* fill msg struct */
msg.lvl = blk->lvl;
msg.err = blk->err;
k = 0;
msg.mode = blk->txt[k];
k++;
for (i = 0; i < 7; i++, k++) {
msg.addr[i] = blk->txt[k];
}
msg.addr[7] = '\0';
/* ACK/NAK */
msg.ack = blk->txt[k];
if (msg.ack == 0x15)
msg.ack = '!';
k++;
msg.label[0] = blk->txt[k];
k++;
msg.label[1] = blk->txt[k];
if (msg.label[1] == 0x7f)
msg.label[1] = 'd';
k++;
msg.label[2] = '\0';
/*
* downlink: 0 - 9
* uplink: A - Z a - z 0x0(NULL)
*/
msg.bid = blk->txt[k];
if (msg.bid == 0)
msg.bid = ' ';
k++;
/* txt start */
msg.bs = blk->txt[k];
k++;
msg.no[0] = '\0';
msg.fid[0] = '\0';
msg.txt[0] = '\0';
/* Don't display uplink message */
if (((msg.bid >= 'A' && msg.bid <= 'Z') ||
(msg.bid >= 'a' && msg.bid <= 'z') || msg.bid == 0x0) && airflt)
return;
/* STX = 0x03 show that there is no application text */
if (msg.bs != 0x03) {
/* downlink message, has MSN and Flight id */
if (msg.bid >= '0' && msg.bid <= '9') {
/* message no */
for (i = 0; i < 4 && k < blk->len - 1; i++, k++) {
msg.no[i] = blk->txt[k];
}
msg.no[i] = '\0';
/* Flight id */
for (i = 0; i < 6 && k < blk->len - 1; i++, k++) {
msg.fid[i] = blk->txt[k];
}
msg.fid[i] = '\0';
}
/* Message txt */
for (i = 0; k < blk->len - 1; i++, k++)
msg.txt[i] = blk->txt[k];
msg.txt[i] = 0;
}
/* txt end */
msg.be = blk->txt[blk->len - 1];
if (sockfd > 0)
outnet(&msg);
switch (outtype) {
case 0:
break;
case 1:
printoneline(&msg, blk->chn, blk->t);
break;
case 2:
printmsg(&msg, blk->chn, blk->t);
break;
}
}