blob: e3472ea2bf7bf32bb8d3c23f8d9e8d13ae68464a [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* 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 Welteac9c0242015-09-10 21:18:16 +020021#include <stdint.h>
22
23#include <osmocom/core/msgb.h>
24
Harald Welte350814a2015-09-10 22:32:15 +020025//#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 Welteac9c0242015-09-10 21:18:16 +020029#include "hnbgw.h"
30
31extern int asn1_xer_print;
32
33static struct msgb *ranap_msgb_alloc(void)
34{
35 return msgb_alloc(1024, "RANAP Tx");
36}
37
Harald Weltecbaaeef2015-12-16 20:17:26 +010038struct msgb *ranap_generate_initiating_message(e_RANAP_ProcedureCode procedureCode,
Harald Welteac9c0242015-09-10 21:18:16 +020039 RANAP_Criticality_t criticality,
Harald Weltecbaaeef2015-12-16 20:17:26 +010040 asn_TYPE_descriptor_t *td, void *sptr)
Harald Welteac9c0242015-09-10 21:18:16 +020041{
42 RANAP_RANAP_PDU_t pdu;
Harald Weltecbaaeef2015-12-16 20:17:26 +010043 struct msgb *msg = ranap_msgb_alloc();
44 asn_enc_rval_t rval;
45 int rc;
Harald Welteac9c0242015-09-10 21:18:16 +020046
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 Weltecbaaeef2015-12-16 20:17:26 +010052 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 Welteac9c0242015-09-10 21:18:16 +020057 }
58
Harald Weltecbaaeef2015-12-16 20:17:26 +010059 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 Welteac9c0242015-09-10 21:18:16 +020070}
Harald Welteac9c0242015-09-10 21:18:16 +020071
72struct 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 Weltee2e5d4d2015-09-10 23:49:45 +0200103 msgb_put(msg, rval.encoded/8);
Harald Welteac9c0242015-09-10 21:18:16 +0200104
105 return msg;
106}
107
Harald Weltecbaaeef2015-12-16 20:17:26 +0100108struct msgb *ranap_generate_unsuccessful_outcome(
Harald Welteac9c0242015-09-10 21:18:16 +0200109 e_RANAP_ProcedureCode procedureCode,
110 RANAP_Criticality_t criticality,
111 asn_TYPE_descriptor_t * td,
112 void *sptr)
113{
Harald Welteac9c0242015-09-10 21:18:16 +0200114 RANAP_RANAP_PDU_t pdu;
Harald Weltecbaaeef2015-12-16 20:17:26 +0100115 struct msgb *msg = ranap_msgb_alloc();
116 asn_enc_rval_t rval;
117 int rc;
Harald Welteac9c0242015-09-10 21:18:16 +0200118
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 Weltecbaaeef2015-12-16 20:17:26 +0100124 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 Welteac9c0242015-09-10 21:18:16 +0200129 }
130
Harald Weltecbaaeef2015-12-16 20:17:26 +0100131 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 Welteac9c0242015-09-10 21:18:16 +0200138
Harald Weltecbaaeef2015-12-16 20:17:26 +0100139 msgb_put(msg, rval.encoded/8);
140
141 return msg;
Harald Welteac9c0242015-09-10 21:18:16 +0200142}
Harald Welteac9c0242015-09-10 21:18:16 +0200143
144RANAP_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}