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 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 38 | static struct msgb *_ranap_gen_msg(RANAP_RANAP_PDU_t *pdu) |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 39 | { |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 40 | struct msgb *msg = ranap_msgb_alloc(); |
| 41 | asn_enc_rval_t rval; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 42 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 43 | if (!msg) |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 44 | return NULL; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 45 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 46 | rval = aper_encode_to_buffer(&asn_DEF_RANAP_RANAP_PDU, pdu, |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 47 | msg->data, msgb_tailroom(msg)); |
| 48 | if (rval.encoded < 0) { |
| 49 | LOGP(DMAIN, LOGL_ERROR, "Error encoding type: %s\n", |
| 50 | rval.failed_type->name); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | msgb_put(msg, rval.encoded/8); |
| 55 | |
| 56 | return msg; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 57 | } |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 58 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 59 | struct msgb *ranap_generate_initiating_message(e_RANAP_ProcedureCode procedureCode, |
| 60 | RANAP_Criticality_t criticality, |
| 61 | asn_TYPE_descriptor_t *td, void *sptr) |
| 62 | { |
| 63 | RANAP_RANAP_PDU_t pdu; |
| 64 | int rc; |
| 65 | |
| 66 | memset(&pdu, 0, sizeof(pdu)); |
| 67 | |
| 68 | pdu.present = RANAP_RANAP_PDU_PR_initiatingMessage; |
| 69 | pdu.choice.initiatingMessage.procedureCode = procedureCode; |
| 70 | pdu.choice.initiatingMessage.criticality = criticality; |
| 71 | rc = ANY_fromType_aper(&pdu.choice.initiatingMessage.value, td, sptr); |
| 72 | if (rc < 0) { |
| 73 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | return _ranap_gen_msg(&pdu); |
| 78 | } |
| 79 | |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 80 | struct msgb *ranap_generate_successful_outcome( |
| 81 | e_RANAP_ProcedureCode procedureCode, |
| 82 | RANAP_Criticality_t criticality, |
| 83 | asn_TYPE_descriptor_t * td, |
| 84 | void *sptr) |
| 85 | { |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 86 | RANAP_RANAP_PDU_t pdu; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 87 | int rc; |
| 88 | |
| 89 | memset(&pdu, 0, sizeof(pdu)); |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 90 | |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 91 | pdu.present = RANAP_RANAP_PDU_PR_successfulOutcome; |
| 92 | pdu.choice.successfulOutcome.procedureCode = procedureCode; |
| 93 | pdu.choice.successfulOutcome.criticality = criticality; |
| 94 | rc = ANY_fromType_aper(&pdu.choice.successfulOutcome.value, td, sptr); |
| 95 | if (rc < 0) { |
| 96 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 97 | return NULL; |
| 98 | } |
| 99 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 100 | return _ranap_gen_msg(&pdu); |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 103 | struct msgb *ranap_generate_unsuccessful_outcome( |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 104 | e_RANAP_ProcedureCode procedureCode, |
| 105 | RANAP_Criticality_t criticality, |
| 106 | asn_TYPE_descriptor_t * td, |
| 107 | void *sptr) |
| 108 | { |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 109 | RANAP_RANAP_PDU_t pdu; |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 110 | int rc; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 111 | |
| 112 | memset(&pdu, 0, sizeof(pdu)); |
| 113 | |
| 114 | pdu.present = RANAP_RANAP_PDU_PR_unsuccessfulOutcome; |
Harald Welte | c16117a | 2015-12-16 20:30:11 +0100 | [diff] [blame] | 115 | pdu.choice.unsuccessfulOutcome.procedureCode = procedureCode; |
| 116 | pdu.choice.unsuccessfulOutcome.criticality = criticality; |
| 117 | rc = ANY_fromType_aper(&pdu.choice.unsuccessfulOutcome.value, td, sptr); |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 118 | if (rc < 0) { |
| 119 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
Harald Welte | cbaaeef | 2015-12-16 20:17:26 +0100 | [diff] [blame] | 120 | return NULL; |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Harald Welte | 8dacb07 | 2015-12-16 20:27:14 +0100 | [diff] [blame] | 123 | return _ranap_gen_msg(&pdu); |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 124 | } |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 125 | |
Harald Welte | c16117a | 2015-12-16 20:30:11 +0100 | [diff] [blame] | 126 | struct msgb *ranap_generate_outcome( |
| 127 | e_RANAP_ProcedureCode procedureCode, |
| 128 | RANAP_Criticality_t criticality, |
| 129 | asn_TYPE_descriptor_t * td, |
| 130 | void *sptr) |
| 131 | { |
| 132 | RANAP_RANAP_PDU_t pdu; |
| 133 | int rc; |
| 134 | |
| 135 | memset(&pdu, 0, sizeof(pdu)); |
| 136 | |
| 137 | pdu.present = RANAP_RANAP_PDU_PR_outcome; |
| 138 | pdu.choice.outcome.procedureCode = procedureCode; |
| 139 | pdu.choice.outcome.criticality = criticality; |
| 140 | rc = ANY_fromType_aper(&pdu.choice.outcome.value, td, sptr); |
| 141 | if (rc < 0) { |
| 142 | LOGP(DMAIN, LOGL_ERROR, "Error in ANY_fromType_aper\n"); |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | return _ranap_gen_msg(&pdu); |
| 147 | } |
| 148 | |
| 149 | |
Harald Welte | ac9c024 | 2015-09-10 21:18:16 +0200 | [diff] [blame] | 150 | RANAP_IE_t *ranap_new_ie(RANAP_ProtocolIE_ID_t id, |
| 151 | RANAP_Criticality_t criticality, |
| 152 | asn_TYPE_descriptor_t * type, void *sptr) |
| 153 | { |
| 154 | |
| 155 | RANAP_IE_t *buff; |
| 156 | |
| 157 | if ((buff = malloc(sizeof(*buff))) == NULL) { |
| 158 | // Possible error on malloc |
| 159 | return NULL; |
| 160 | } |
| 161 | memset((void *)buff, 0, sizeof(*buff)); |
| 162 | |
| 163 | buff->id = id; |
| 164 | buff->criticality = criticality; |
| 165 | |
| 166 | ANY_fromType_aper(&buff->value, type, sptr); |
| 167 | |
| 168 | if (asn1_xer_print) |
| 169 | if (xer_fprint(stdout, &asn_DEF_RANAP_IE, buff) < 0) { |
| 170 | free(buff); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | return buff; |
| 175 | } |