ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2019 AT&T Intellectual Property |
| 3 | // Copyright 2019 Nokia |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // |
| 17 | |
| 18 | package handlers |
| 19 | |
| 20 | // #cgo CFLAGS: -I../asn1codec/inc/ -I../asn1codec/e2ap_engine/ |
| 21 | // #cgo LDFLAGS: -L ../asn1codec/lib/ -L../asn1codec/e2ap_engine/ -le2ap_codec -lasncodec |
| 22 | // #include <asn1codec_utils.h> |
| 23 | // #include <x2setup_request_wrapper.h> |
| 24 | import "C" |
| 25 | import ( |
| 26 | "e2mgr/logger" |
| 27 | "fmt" |
| 28 | "github.com/pkg/errors" |
| 29 | "unsafe" |
| 30 | ) |
| 31 | |
| 32 | type X2PduRefinedResponse struct { |
| 33 | pduPrint string |
| 34 | } |
| 35 | |
is005q | 19bf35e | 2019-08-12 18:59:29 +0300 | [diff] [blame] | 36 | //func unpackX2apPduUPer(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*C.E2AP_PDU_t, error) { |
| 37 | // pdu := C.new_pdu(C.ulong(allocationBufferSize)) |
| 38 | // |
| 39 | // if pdu == nil { |
| 40 | // return nil, errors.New("allocation failure (pdu)") |
| 41 | // } |
| 42 | // |
| 43 | // logger.Debugf("#x2apPdu_asn1_unpacker.unpackX2apPduUPer - Packed pdu(%d):%x", packedBufferSize, packedBuf) |
| 44 | // |
| 45 | // errBuf := make([]C.char, maxMessageBufferSize) |
| 46 | // if !C.unpack_pdu_aux(pdu, C.ulong(packedBufferSize), (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.ulong(len(errBuf)), &errBuf[0], C.ATS_UNALIGNED_BASIC_PER) { |
| 47 | // return nil, errors.New(fmt.Sprintf("unpacking error: %s", C.GoString(&errBuf[0]))) |
| 48 | // } |
| 49 | // |
| 50 | // if logger.DebugEnabled() { |
| 51 | // C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0]) |
| 52 | // logger.Debugf("#x2apPdu_asn1_unpacker.unpackX2apPduUPer - PDU: %v packed size:%d", C.GoString(&errBuf[0]), packedBufferSize) |
| 53 | // } |
| 54 | // |
| 55 | // return pdu, nil |
| 56 | //} |
| 57 | |
ss412g | 07ef76d | 2019-08-12 17:26:40 +0300 | [diff] [blame] | 58 | func unpackX2apPdu(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*C.E2AP_PDU_t, error) { |
| 59 | pdu := C.new_pdu(C.ulong(allocationBufferSize)) |
| 60 | |
| 61 | if pdu == nil { |
| 62 | return nil, errors.New("allocation failure (pdu)") |
| 63 | } |
| 64 | |
| 65 | logger.Infof("#x2apPdu_asn1_unpacker.unpackX2apPdu - Packed pdu(%d):%x", packedBufferSize, packedBuf) |
| 66 | |
| 67 | errBuf := make([]C.char, maxMessageBufferSize) |
| 68 | if !C.per_unpack_pdu(pdu, C.ulong(packedBufferSize), (*C.uchar)(unsafe.Pointer(&packedBuf[0])), C.ulong(len(errBuf)), &errBuf[0]) { |
| 69 | return nil, errors.New(fmt.Sprintf("unpacking error: %s", C.GoString(&errBuf[0]))) |
| 70 | } |
| 71 | |
| 72 | if logger.DebugEnabled() { |
| 73 | C.asn1_pdu_printer(pdu, C.size_t(len(errBuf)), &errBuf[0]) |
| 74 | logger.Debugf("#x2apPdu_asn1_unpacker.unpackX2apPdu - PDU: %v packed size:%d", C.GoString(&errBuf[0]), packedBufferSize) |
| 75 | } |
| 76 | |
| 77 | return pdu, nil |
| 78 | } |
| 79 | |
| 80 | func unpackX2apPduAndRefine(logger *logger.Logger, allocationBufferSize int, packedBufferSize int, packedBuf []byte, maxMessageBufferSize int) (*X2PduRefinedResponse, error) { |
| 81 | pdu, err := unpackX2apPdu(logger, allocationBufferSize, packedBufferSize, packedBuf, maxMessageBufferSize) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | defer C.delete_pdu(pdu) |
| 87 | |
| 88 | var refinedResponse = "" |
| 89 | if logger.DebugEnabled() { |
| 90 | buf := make([]C.char, 16*maxMessageBufferSize) |
| 91 | C.asn1_pdu_printer(pdu, C.size_t(len(buf)), &buf[0]) |
| 92 | refinedResponse = C.GoString(&buf[0]) |
| 93 | } |
| 94 | |
| 95 | return &X2PduRefinedResponse{pduPrint: refinedResponse}, nil |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |