-
Notifications
You must be signed in to change notification settings - Fork 8
/
testparse.c
126 lines (118 loc) · 3.91 KB
/
testparse.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
/* test parse_cooked_ax25 */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <signal.h>
#include <errno.h>
#include <linux/ax25.h>
#include <netdb.h>
#include "libax25ext.h"
static void print_it(FILE *f,
struct ax_calls *calls,
u_char *data,
int len);
static void drop_unused_digis(struct ax_calls *);
int
main(void)
{
unsigned char buf[2048],*bp;
int buflen, *lp = &buflen;
while (fgets(buf,sizeof(buf),stdin)) {
unsigned char obuf[2048], *op;
int r,olen = sizeof(obuf), *olp = &olen;
struct ax_calls calls;
bp = buf;
buflen = strlen(buf);
if (buf[buflen-1] == '\n')
buf[--buflen] = '\0';
op = obuf;
olen = sizeof(obuf);
bzero(obuf,sizeof(obuf));
fprintf(stderr,"================================\ninput: %s\n",buf);
switch(r=parse_cooked_ax25(&bp,lp,&calls)) {
case PK_VALID:
case PK_VALDIGI:
fprintf(stderr,"valid%s packet. n_digis: %d next_digi: %d type: %d pid: %d\n",
(r==PK_VALDIGI)?" repeatable":"",
calls.ax_n_digis, calls.ax_next_digi, calls.ax_type, calls.ax_pid);
fprintf(stderr,"data @0x%0x (len %d): %s\n",bp,buflen,bp);
fprintf(stderr,"---- print_it ----\n");
print_it(stderr,&calls,bp,buflen);
fprintf(stderr,"---- gen_cooked thing: ----\n");
fprintf(stderr,"gen_cooked rc: %d\n",gen_cooked_ax25(&op,olp,&calls));
fprintf(stderr,"cooked@0x%0x (len %d): %s\n",obuf,olen,obuf);/* ?? */
fprintf(stderr,"data @0x%0x (len %d): %s\n", bp, buflen, bp);
strncpy(&obuf[sizeof(obuf)-olen],bp,buflen);
printf("%s\n",obuf);
fprintf(stderr,"after dropping unused digis:\n");
drop_unused_digis(&calls);
fprintf(stderr," n_digis: %d next_digi: %d type: %d pid: %d\n",
calls.ax_n_digis, calls.ax_next_digi, calls.ax_type, calls.ax_pid);
print_it(stderr,&calls,bp,buflen);
break;
case PK_INVALID:
fprintf(stderr,"failed to parse\n");
break;
default:
fprintf(stderr,"bogus return value\n");
break;
}
}
}
/* some defines that really belong in a header file! */
#define ALEN 6 /* index to where the SSID and flags go */
#define AXLEN 7 /* length of a callsign */
/* SSID mask and various flags that are stuffed into the SSID byte */
#define SSID 0x1E
#define HDLCAEB 0x01
#define REPEATED 0x80
#define UI 0x03 /* unnumbered information (unproto) */
#define PID_NO_L3 0xF0 /* no level-3 (text) */
#define C 0x80
#define SSSID_SPARE 0x40 /* must be set if not used */
#define ESSID_SPARE 0x20
static void
print_it(FILE *f,
struct ax_calls *calls,
u_char *data,
int len)
{
int j;
char asc_from[12],asc_to[12];
if (f == NULL)
return;
fprintf(stderr,"print_it: n_digis %d next_digi %d\n",
calls->ax_n_digis,calls->ax_next_digi);
strncpy(asc_to,ax25_ntoa_pretty(&calls->ax_to_call),sizeof(asc_to));
strncpy(asc_from,ax25_ntoa_pretty(&calls->ax_from_call),sizeof(asc_from));
fprintf(f,"%s>%s",asc_from,asc_to);
for (j = 0; j < calls->ax_n_digis; j++) {
fprintf(stderr,"\nprint_it: digi[%d]: %s %s\n",j,
(calls->ax_digi_call[j].ax25_call[ALEN]&REPEATED)?"REPEATED":"",
(calls->ax_digi_call[j].ax25_call[ALEN]&HDLCAEB)?"HDLCAEB":"");
fprintf(f,",%s%s",ax25_ntoa_pretty(&calls->ax_digi_call[j]),
(calls->ax_digi_call[j].ax25_call[ALEN]&REPEATED
&& (j == calls->ax_next_digi-1))?"*":"");
}
fprintf(f,":%.*s\n",len,data);
}
static void
drop_unused_digis(struct ax_calls *calls)
{
if (!calls->ax_n_digis || calls->ax_next_digi >= calls->ax_n_digis)
return; /* all digis are used up */
calls->ax_n_digis = calls->ax_next_digi; /* truncate the list */
calls->ax_digi_call[calls->ax_next_digi-1].ax25_call[ALEN] |= HDLCAEB;
}