Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 1 | /* SCCP User SAP helper functions (move to libosmo-sigtran?) */ |
| 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 | |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include <osmocom/sigtran/sccp_sap.h> |
| 24 | #include <osmocom/sigtran/sua.h> |
| 25 | |
| 26 | #include "sccp_helpers.h" |
| 27 | |
Harald Welte | 8c572fe | 2015-12-26 08:42:07 +0100 | [diff] [blame] | 28 | void sccp_make_addr_pc_ssn(struct osmo_sccp_addr *addr, uint32_t pc, uint32_t ssn) |
| 29 | { |
| 30 | addr->presence = OSMO_SCCP_ADDR_T_SSN | OSMO_SCCP_ADDR_T_PC; |
| 31 | addr->ssn = ssn; |
| 32 | addr->pc = pc; |
| 33 | } |
| 34 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 35 | int sccp_tx_unitdata(struct osmo_sua_link *link, |
| 36 | const struct osmo_sccp_addr *calling_addr, |
| 37 | const struct osmo_sccp_addr *called_addr, |
| 38 | uint8_t *data, unsigned int len) |
| 39 | { |
| 40 | struct msgb *msg = msgb_alloc(1024, "sccp_tx_unitdata"); |
| 41 | struct osmo_scu_prim *prim; |
| 42 | struct osmo_scu_unitdata_param *param; |
| 43 | |
| 44 | prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); |
| 45 | param = &prim->u.unitdata; |
Harald Welte | 8c572fe | 2015-12-26 08:42:07 +0100 | [diff] [blame] | 46 | sccp_make_addr_pc_ssn(¶m->calling_addr, 1, OSMO_SCCP_SSN_RANAP); |
| 47 | sccp_make_addr_pc_ssn(¶m->called_addr, 2, OSMO_SCCP_SSN_RANAP); |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 48 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_REQUEST, msg); |
| 49 | |
| 50 | msg->l2h = msgb_put(msg, len); |
| 51 | memcpy(msg->l2h, data, len); |
| 52 | |
| 53 | return osmo_sua_user_link_down(link, &prim->oph); |
| 54 | } |
| 55 | |
| 56 | int sccp_tx_unitdata_msg(struct osmo_sua_link *link, |
| 57 | const struct osmo_sccp_addr *calling_addr, |
| 58 | const struct osmo_sccp_addr *called_addr, |
| 59 | struct msgb *msg) |
| 60 | { |
| 61 | int rc; |
| 62 | |
| 63 | rc = sccp_tx_unitdata(link, calling_addr, called_addr, |
| 64 | msg->data, msgb_length(msg)); |
| 65 | msgb_free(msg); |
| 66 | |
| 67 | return rc; |
| 68 | } |
| 69 | |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 70 | int sccp_tx_conn_req(struct osmo_sua_link *link, uint32_t conn_id, |
| 71 | const struct osmo_sccp_addr *calling_addr, |
| 72 | const struct osmo_sccp_addr *called_addr, |
| 73 | uint8_t *data, unsigned int len) |
| 74 | { |
| 75 | struct msgb *msg = msgb_alloc(1024, "sccp_tx_conn_req"); |
| 76 | struct osmo_scu_prim *prim; |
| 77 | |
| 78 | prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); |
| 79 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 80 | OSMO_SCU_PRIM_N_CONNECT, |
| 81 | PRIM_OP_REQUEST, msg); |
Harald Welte | 8c572fe | 2015-12-26 08:42:07 +0100 | [diff] [blame] | 82 | sccp_make_addr_pc_ssn(&prim->u.connect.calling_addr, 1, OSMO_SCCP_SSN_RANAP); |
Harald Welte | c4338de | 2015-12-24 00:40:52 +0100 | [diff] [blame] | 83 | prim->u.connect.sccp_class = 2; |
| 84 | prim->u.connect.conn_id = conn_id; |
| 85 | |
| 86 | if (data && len) { |
| 87 | msg->l2h = msgb_put(msg, len); |
| 88 | memcpy(msg->l2h, data, len); |
| 89 | } |
| 90 | |
| 91 | return osmo_sua_user_link_down(link, &prim->oph); |
| 92 | } |
| 93 | |
| 94 | int sccp_tx_conn_req_msg(struct osmo_sua_link *link, uint32_t conn_id, |
| 95 | const struct osmo_sccp_addr *calling_addr, |
| 96 | const struct osmo_sccp_addr *called_addr, |
| 97 | struct msgb *msg) |
| 98 | { |
| 99 | int rc; |
| 100 | |
| 101 | rc = sccp_tx_conn_req(link, conn_id, calling_addr, called_addr, |
| 102 | msg->data, msgb_length(msg)); |
| 103 | msgb_free(msg); |
| 104 | |
| 105 | return rc; |
| 106 | } |
| 107 | |
| 108 | int sccp_tx_data(struct osmo_sua_link *link, uint32_t conn_id, |
| 109 | uint8_t *data, unsigned int len) |
| 110 | { |
| 111 | struct msgb *msg = msgb_alloc(1024, "sccp_tx_data"); |
| 112 | struct osmo_scu_prim *prim; |
| 113 | |
| 114 | prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim)); |
| 115 | osmo_prim_init(&prim->oph, SCCP_SAP_USER, |
| 116 | OSMO_SCU_PRIM_N_DATA, |
| 117 | PRIM_OP_REQUEST, msg); |
| 118 | prim->u.data.conn_id = conn_id; |
| 119 | |
| 120 | msg->l2h = msgb_put(msg, len); |
| 121 | memcpy(msg->l2h, data, len); |
| 122 | |
| 123 | return osmo_sua_user_link_down(link, &prim->oph); |
| 124 | } |
| 125 | |
| 126 | int sccp_tx_data_msg(struct osmo_sua_link *link, uint32_t conn_id, |
| 127 | struct msgb *msg) |
| 128 | { |
| 129 | int rc; |
| 130 | |
| 131 | rc = sccp_tx_data(link, conn_id, msg->data, msgb_length(msg)); |
| 132 | msgb_free(msg); |
| 133 | |
| 134 | return rc; |
| 135 | } |