Harald Welte | 3a19440 | 2017-07-22 17:07:51 +0200 | [diff] [blame^] | 1 | |
| 2 | #include "Octetstring.hh" |
| 3 | #include "Error.hh" |
| 4 | #include "Logger.hh" |
| 5 | |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | namespace BSSGP__Helper__Functions { |
| 9 | |
| 10 | /* convert a buffer filled with TLVs that have variable-length "length" fields (Osmocom TvLV) into a |
| 11 | * buffer filled with TLVs that have fixed 16-bit length values (TL16V format) */ |
| 12 | static OCTETSTRING transcode_tlv_part(OCTETSTRING const &in) |
| 13 | { |
| 14 | const unsigned char *in_ptr = (const unsigned char *)in; |
| 15 | int in_len = in.lengthof(); |
| 16 | int ofs = 0; |
| 17 | uint16_t data_len; |
| 18 | OCTETSTRING out(0, (const unsigned char *)""); |
| 19 | |
| 20 | while (ofs < in_len) { |
| 21 | int remain_len = in_len - ofs; |
| 22 | int tl_length; |
| 23 | |
| 24 | if (remain_len < 2) { |
| 25 | TTCN_error("Remaining input length (%d) insufficient for Tag+Length", remain_len); |
| 26 | break; |
| 27 | } |
| 28 | |
| 29 | /* copy over tag */ |
| 30 | if (in_ptr[ofs+1] & 0x80) { |
| 31 | /* E bit is set, 7-bit length field */ |
| 32 | data_len = in_ptr[ofs+1] & 0x7F; |
| 33 | tl_length = 2; |
| 34 | } else { |
| 35 | /* E bit is not set, 15 bit length field */ |
| 36 | if (in_len < 3) { |
| 37 | TTCN_error("Remaining input length insufficient for 2-octet length"); |
| 38 | break; |
| 39 | } |
| 40 | data_len = in_ptr[ofs+1] << 8 | in_ptr[ofs+2]; |
| 41 | tl_length = 3; |
| 42 | } |
| 43 | if (in_len < tl_length + data_len) { |
| 44 | TTCN_error("Remaining input length insufficient for TLV value length"); |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | /* Tag + 16bit length */ |
| 49 | uint8_t hdr_buf[3]; |
| 50 | hdr_buf[0] = in_ptr[ofs+0]; |
| 51 | hdr_buf[1] = data_len >> 8; |
| 52 | hdr_buf[2] = data_len & 0xff; |
| 53 | |
| 54 | OCTETSTRING tlv_hdr(3, hdr_buf); |
| 55 | out += tlv_hdr; |
| 56 | |
| 57 | if (data_len) { |
| 58 | /* append octet string of current TLV to output octetstring */ |
| 59 | OCTETSTRING tlv_val(data_len, in_ptr + ofs + tl_length); |
| 60 | out += tlv_val; |
| 61 | } |
| 62 | |
| 63 | /* advance input offset*/ |
| 64 | ofs += data_len + tl_length; |
| 65 | } |
| 66 | |
| 67 | return out; |
| 68 | } |
| 69 | |
| 70 | #define BSSGP_PDUT_DL_UNITDATA 0x00 |
| 71 | #define BSSGP_PDUT_UL_UNITDATA 0x01 |
| 72 | |
| 73 | /* expand all the variable-length "length" fields of a BSSGP message (Osmocom TvLV) into |
| 74 | * statlc TL16V format */ |
| 75 | OCTETSTRING f__BSSGP__preprocess__pdu(OCTETSTRING const &in) |
| 76 | { |
| 77 | const unsigned char *in_ptr = (const unsigned char *)in; |
| 78 | int in_len = in.lengthof(); |
| 79 | uint8_t pdu_type = in_ptr[0]; |
| 80 | uint8_t static_hdr_len = 1; |
| 81 | |
| 82 | if (pdu_type == BSSGP_PDUT_DL_UNITDATA || pdu_type == BSSGP_PDUT_UL_UNITDATA) |
| 83 | static_hdr_len = 8; |
| 84 | |
| 85 | if (in_len < static_hdr_len) |
| 86 | TTCN_error("BSSGP message is shorter (%u bytes) than minimum header length (%u bytes) for msg_type 0x%02x", |
| 87 | in_len, static_hdr_len, pdu_type); |
| 88 | |
| 89 | /* prefix = non-TLV section of header */ |
| 90 | OCTETSTRING prefix(static_hdr_len, in_ptr); |
| 91 | OCTETSTRING tlv_part_in(in_len - static_hdr_len, in_ptr + static_hdr_len); |
| 92 | |
| 93 | return prefix + transcode_tlv_part(tlv_part_in); |
| 94 | } |
| 95 | |
| 96 | } |