Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 1 | /* common RANAP code */ |
| 2 | |
| 3 | /* (C) 2015 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
| 23 | #include <osmocom/core/msgb.h> |
| 24 | |
Harald Welte | 350814a | 2015-09-10 22:32:15 +0200 | [diff] [blame] | 25 | //#include "ranap_common.h" |
| 26 | #include "ranap/RANAP_RANAP-PDU.h" |
| 27 | #include "ranap/RANAP_ProtocolIE-ID.h" |
| 28 | #include "ranap/RANAP_IE.h" |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 29 | #include "hnbgw.h" |
| 30 | |
| 31 | extern int asn1_xer_print; |
| 32 | |
| 33 | static struct msgb *ranap_msgb_alloc(void) |
| 34 | { |
| 35 | return msgb_alloc(1024, "RANAP Tx"); |
| 36 | } |
| 37 | |
| 38 | #if 0 |
| 39 | ssize_t ranap_generate_initiating_message(uint8_t ** buffer, |
| 40 | uint32_t * length, |
| 41 | e_RANAP_ProcedureCode procedureCode, |
| 42 | RANAP_Criticality_t criticality, |
| 43 | asn_TYPE_descriptor_t * td, void *sptr) |
| 44 | { |
| 45 | RANAP_RANAP_PDU_t pdu; |
| 46 | ssize_t encoded; |
| 47 | |
| 48 | memset(&pdu, 0, sizeof(pdu)); |
| 49 | |
| 50 | pdu.present = RANAP_RANAP_PDU_PR_initiatingMessage; |
| 51 | pdu.choice.initiatingMessage.procedureCode = procedureCode; |
| 52 | pdu.choice.initiatingMessage.criticality = criticality; |
| 53 | ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr); |
| 54 | |
| 55 | if (asn1_xer_print) |
| 56 | xer_fprint(stdout, &asn_DEF_RANAP_RAMAP_PDU, (void *)&pdu); |
| 57 | |
| 58 | if ((encoded = |
| 59 | aper_encode_to_new_buffer(&asn_DEF_RANAP_RANAP_PDU, 0, &pdu, |
| 60 | (void **)buffer)) < 0) { |
| 61 | return -1; |
| 62 | } |
| 63 | |
| 64 | *length = encoded; |
| 65 | return encoded; |
| 66 | } |
| 67 | #endif |
| 68 | |
| 69 | struct msgb *ranap_generate_successful_outcome( |
| 70 | e_RANAP_ProcedureCode procedureCode, |
| 71 | RANAP_Criticality_t criticality, |
| 72 | asn_TYPE_descriptor_t * td, |
| 73 | void *sptr) |
| 74 | { |
| 75 | |
| 76 | RANAP_RANAP_PDU_t pdu; |
| 77 | struct msgb *msg = ranap_msgb_alloc(); |
| 78 | asn_enc_rval_t rval; |
| 79 | int rc; |
| 80 | |
| 81 | memset(&pdu, 0, sizeof(pdu)); |
| 82 | pdu.present = RANAP_RANAP_PDU_PR_successfulOutcome; |
| 83 | pdu.choice.successfulOutcome.procedureCode = procedureCode; |
| 84 | pdu.choice.successfulOutcome.criticality = criticality; |
| 85 | rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr); |
| 86 | if (rc < 0) { |
| 87 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
| 88 | msgb_free(msg); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | rval = aper_encode_to_buffer(&asn_DEF_RANAP_RANAP_PDU, &pdu, |
| 93 | msg->data, msgb_tailroom(msg)); |
| 94 | if (rval.encoded < 0) { |
| 95 | LOGP(DMAIN, LOGL_ERROR, "Error encoding type %s\n", rval.failed_type->name); |
| 96 | msgb_free(msg); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Harald Welte | e2e5d4d | 2015-09-10 23:49:45 +0200 | [diff] [blame] | 100 | msgb_put(msg, rval.encoded/8); |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 101 | |
| 102 | return msg; |
| 103 | } |
| 104 | |
| 105 | #if 0 |
| 106 | ssize_t ranap_generate_unsuccessful_outcome(uint8_t ** buffer, |
| 107 | uint32_t * length, |
| 108 | e_RANAP_ProcedureCode procedureCode, |
| 109 | RANAP_Criticality_t criticality, |
| 110 | asn_TYPE_descriptor_t * td, |
| 111 | void *sptr) |
| 112 | { |
| 113 | |
| 114 | RANAP_RANAP_PDU_t pdu; |
| 115 | ssize_t encoded; |
| 116 | |
| 117 | memset(&pdu, 0, sizeof(pdu)); |
| 118 | |
| 119 | pdu.present = RANAP_RANAP_PDU_PR_unsuccessfulOutcome; |
| 120 | pdu.choice.successfulOutcome.procedureCode = procedureCode; |
| 121 | pdu.choice.successfulOutcome.criticality = criticality; |
| 122 | ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr); |
| 123 | |
| 124 | if ((encoded = |
| 125 | aper_encode_to_new_buffer(&asn_DEF_RANAP_RANAP_PDU, 0, &pdu, |
| 126 | (void **)buffer)) < 0) { |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | *length = encoded; |
| 131 | |
| 132 | return encoded; |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | RANAP_IE_t *ranap_new_ie(RANAP_ProtocolIE_ID_t id, |
| 137 | RANAP_Criticality_t criticality, |
| 138 | asn_TYPE_descriptor_t * type, void *sptr) |
| 139 | { |
| 140 | |
| 141 | RANAP_IE_t *buff; |
| 142 | |
| 143 | if ((buff = malloc(sizeof(*buff))) == NULL) { |
| 144 | // Possible error on malloc |
| 145 | return NULL; |
| 146 | } |
| 147 | memset((void *)buff, 0, sizeof(*buff)); |
| 148 | |
| 149 | buff->id = id; |
| 150 | buff->criticality = criticality; |
| 151 | |
| 152 | ANY_fromType_aper(&buff->value, type, sptr); |
| 153 | |
| 154 | if (asn1_xer_print) |
| 155 | if (xer_fprint(stdout, &asn_DEF_RANAP_IE, buff) < 0) { |
| 156 | free(buff); |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | return buff; |
| 161 | } |