forked from szpajder/dumpvdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clnp.c
155 lines (146 loc) · 4.21 KB
/
clnp.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
/*
* dumpvdl2 - a VDL Mode 2 message decoder and protocol analyzer
*
* Copyright (c) 2017 Tomasz Lemiech <szpajder@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "dumpvdl2.h"
#include "clnp.h"
#include "idrp.h"
#include "cotp.h"
static void parse_clnp_pdu_payload(clnp_pdu_t *pdu, uint8_t *buf, uint32_t len, uint32_t *msg_type) {
if(len == 0)
goto clnp_pdu_payload_unparsed;
pdu->proto = *buf;
switch(*buf) {
case SN_PROTO_ESIS:
// not implemented yet
break;
case SN_PROTO_IDRP:
pdu->data = parse_idrp_pdu(buf, len, msg_type);
break;
case SN_PROTO_CLNP:
debug_print("%s", "CLNP inside CLNP? Bailing out to avoid loop\n");
break;
default:
// assume X.224 COTP TPDU
pdu->data = parse_cotp_concatenated_pdu(buf, len, msg_type);
if(pdu->data != NULL)
pdu->proto = SN_PROTO_COTP;
break;
}
if(pdu->data != NULL) {
pdu->data_valid = 1;
return;
}
clnp_pdu_payload_unparsed:
pdu->data_valid = 0;
pdu->data = buf;
pdu->datalen = len;
}
clnp_pdu_t *parse_clnp_pdu(uint8_t *buf, uint32_t len, uint32_t *msg_type) {
static clnp_pdu_t *pdu = NULL;
if(len < CLNP_MIN_LEN) {
debug_print("Too short (len %u < min len %u)\n", len, CLNP_MIN_LEN);
return NULL;
}
if(pdu == NULL) {
pdu = XCALLOC(1, sizeof(clnp_pdu_t));
} else {
memset(pdu, 0, sizeof(clnp_pdu_t));
}
uint32_t hdrlen = buf[1];
if(len < hdrlen) {
debug_print("header truncated: buf_len %u < hdr_len %u\n", len, hdrlen);
return NULL;
}
buf += hdrlen; len -= hdrlen;
parse_clnp_pdu_payload(pdu, buf, len, msg_type);
return pdu;
}
clnp_pdu_t *parse_clnp_compressed_init_pdu(uint8_t *buf, uint32_t len, uint32_t *msg_type) {
static clnp_pdu_t *pdu = NULL;
if(len < CLNP_COMPRESSED_INIT_MIN_LEN) {
debug_print("Too short (len %u < min len %u)\n", len, CLNP_COMPRESSED_INIT_MIN_LEN);
return NULL;
}
if(pdu == NULL) {
pdu = XCALLOC(1, sizeof(clnp_pdu_t));
} else {
memset(pdu, 0, sizeof(clnp_pdu_t));
}
uint32_t hdrlen = CLNP_COMPRESSED_INIT_MIN_LEN;
if(buf[3] & 0x80) hdrlen += 1; // EXP flag = 1 means localRef/B octet is present
if(buf[0] & 0x10) hdrlen += 2; // odd PDU type means PDU identifier is present
debug_print("buf[0]: %02x buf[3]: %02x hdrlen: %u\n", buf[0], buf[3], hdrlen);
if(len < hdrlen) {
debug_print("header truncated: buf_len %u < hdr_len %u\n", len, hdrlen);
return NULL;
}
buf += hdrlen; len -= hdrlen;
parse_clnp_pdu_payload(pdu, buf, len, msg_type);
return pdu;
}
static void output_clnp_pdu(clnp_pdu_t *pdu) {
if(pdu == NULL) {
fprintf(outf, "-- NULL PDU\n");
return;
}
switch(pdu->proto) {
case SN_PROTO_ESIS:
if(pdu->data_valid) {
// output_esis(pdu->data);
} else {
// fprintf(outf, "-- Unparseable ES-IS PDU\n");
fprintf(outf, "ES-IS PDU:\n");
output_raw(pdu->data, pdu->datalen);
}
break;
case SN_PROTO_IDRP:
if(pdu->data_valid) {
output_idrp(pdu->data);
} else {
fprintf(outf, "-- Unparseable IDRP PDU\n");
output_raw(pdu->data, pdu->datalen);
}
break;
case SN_PROTO_CLNP:
fprintf(outf, "-- Nested CLNP PDU - ignored\n");
output_raw(pdu->data, pdu->datalen);
break;
case SN_PROTO_COTP:
if(pdu->data_valid) {
output_cotp_concatenated_pdu(pdu->data);
} else {
fprintf(outf, "-- Unparseable COTP TPDU\n");
output_raw(pdu->data, pdu->datalen);
}
break;
default:
fprintf(outf, "Unknown protocol 0x%02x\n", pdu->proto);
output_raw(pdu->data, pdu->datalen);
}
}
void output_clnp(clnp_pdu_t *pdu) {
fprintf(outf, "CLNP PDU:\n");
output_clnp_pdu(pdu);
}
void output_clnp_compressed(clnp_pdu_t *pdu) {
fprintf(outf, "CLNP PDU, compressed header:\n");
output_clnp_pdu(pdu);
}