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