Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 4 | * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2009-2011 by On-Waves |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 9 | * it under the terms of the GNU Affero General Public License as published by |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 10 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 17 | * |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #ifndef OPENBSC_MGCP_H |
| 24 | #define OPENBSC_MGCP_H |
| 25 | |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 26 | #include "cellmgr_debug.h" |
| 27 | |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 28 | #include <osmocore/msgb.h> |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 29 | #include <osmocore/write_queue.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 30 | |
| 31 | #include <arpa/inet.h> |
| 32 | |
| 33 | #define RTP_PORT_DEFAULT 4000 |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 34 | #define RTP_PORT_NET_DEFAULT 16000 |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame] | 35 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 36 | /** |
| 37 | * Calculate the RTP audio port for the given multiplex |
| 38 | * and the direction. This allows a semi static endpoint |
| 39 | * to port calculation removing the need for the BSC |
| 40 | * and the MediaGateway to communicate. |
| 41 | * |
| 42 | * Port usage explained: |
| 43 | * base + (multiplex * 2) + 0 == local port to wait for network packets |
| 44 | * base + (multiplex * 2) + 1 == local port for rtcp |
| 45 | * |
| 46 | * The above port will receive packets from the BTS that need |
| 47 | * to be patched and forwarded to the network. |
| 48 | * The above port will receive packets from the network that |
| 49 | * need to be patched and forwarded to the BTS. |
| 50 | * |
| 51 | * We assume to have a static BTS IP address so we can differentiate |
| 52 | * network and BTS. |
| 53 | * |
| 54 | */ |
| 55 | static inline int rtp_calculate_port(int multiplex, int base) |
| 56 | { |
| 57 | return base + (multiplex * 2); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /* |
| 62 | * Handling of MGCP Endpoints and the MGCP Config |
| 63 | */ |
| 64 | struct mgcp_endpoint; |
| 65 | struct mgcp_config; |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 66 | struct mgcp_trunk_config; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 67 | |
| 68 | #define MGCP_ENDP_CRCX 1 |
| 69 | #define MGCP_ENDP_DLCX 2 |
| 70 | #define MGCP_ENDP_MDCX 3 |
| 71 | |
| 72 | /* |
| 73 | * what to do with the msg? |
| 74 | * - continue as usual? |
| 75 | * - reject and send a failure code? |
| 76 | * - defer? do not send anything |
| 77 | */ |
| 78 | #define MGCP_POLICY_CONT 4 |
| 79 | #define MGCP_POLICY_REJECT 5 |
| 80 | #define MGCP_POLICY_DEFER 6 |
| 81 | |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 82 | typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint); |
| 83 | typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state); |
| 84 | typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 85 | typedef int (*mgcp_reset)(struct mgcp_config *cfg); |
| 86 | |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 87 | #define PORT_ALLOC_STATIC 0 |
| 88 | #define PORT_ALLOC_DYNAMIC 1 |
| 89 | |
| 90 | /** |
| 91 | * This holds information on how to allocate ports |
| 92 | */ |
| 93 | struct mgcp_port_range { |
| 94 | int mode; |
| 95 | |
| 96 | /* pre-allocated from a base? */ |
| 97 | int base_port; |
| 98 | |
| 99 | /* dynamically allocated */ |
| 100 | int range_start; |
| 101 | int range_end; |
| 102 | int last_port; |
| 103 | }; |
| 104 | |
| 105 | struct mgcp_trunk_config { |
| 106 | struct llist_head entry; |
| 107 | |
| 108 | struct mgcp_config *cfg; |
| 109 | |
| 110 | int trunk_nr; |
| 111 | int trunk_type; |
| 112 | |
| 113 | char *audio_name; |
| 114 | int audio_payload; |
| 115 | int audio_loop; |
| 116 | |
| 117 | /* spec handling */ |
| 118 | int force_realloc; |
| 119 | |
| 120 | unsigned int number_endpoints; |
| 121 | struct mgcp_endpoint *endpoints; |
| 122 | |
| 123 | /* Special MGW handling */ |
| 124 | int target_trunk_start; |
Holger Hans Peter Freyther | 672f321 | 2011-03-01 13:52:57 +0100 | [diff] [blame] | 125 | int voice_base; |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 126 | int endp_offset; |
| 127 | int vad_enabled; |
| 128 | |
| 129 | int digital_inp_gain; |
| 130 | int digital_out_gain; |
| 131 | int upstr_agc_enbl; |
| 132 | int upstr_adp_rate; |
| 133 | int upstr_max_gain; |
| 134 | int upstr_target_lvl; |
| 135 | int dwnstr_agc_enbl; |
| 136 | int dwnstr_adp_rate; |
| 137 | int dwnstr_max_gain; |
| 138 | int dwnstr_target_lvl; |
| 139 | }; |
| 140 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 141 | struct mgcp_config { |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 142 | int source_port; |
| 143 | char *local_ip; |
| 144 | char *source_addr; |
| 145 | char *bts_ip; |
| 146 | char *call_agent_addr; |
| 147 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 148 | struct in_addr bts_in; |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 149 | |
| 150 | /* transcoder handling */ |
| 151 | char *transcoder_ip; |
| 152 | struct in_addr transcoder_in; |
| 153 | int transcoder_remote_base; |
| 154 | |
| 155 | struct write_queue gw_fd; |
| 156 | |
| 157 | struct mgcp_port_range bts_ports; |
| 158 | struct mgcp_port_range net_ports; |
| 159 | struct mgcp_port_range transcoder_ports; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 160 | int endp_dscp; |
| 161 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 162 | mgcp_change change_cb; |
| 163 | mgcp_policy policy_cb; |
| 164 | mgcp_reset reset_cb; |
Holger Hans Peter Freyther | e40bc38 | 2010-09-18 03:11:00 +0800 | [diff] [blame] | 165 | mgcp_realloc realloc_cb; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 166 | void *data; |
Holger Hans Peter Freyther | d86208d | 2010-08-04 06:11:27 +0800 | [diff] [blame] | 167 | |
Holger Hans Peter Freyther | 5b08401 | 2010-08-08 07:51:51 +0800 | [diff] [blame] | 168 | uint32_t last_call_id; |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 169 | |
| 170 | /* trunk handling */ |
| 171 | struct mgcp_trunk_config trunk; |
| 172 | struct llist_head trunks; |
| 173 | |
| 174 | /* only used for start with a static configuration */ |
| 175 | int last_net_port; |
| 176 | int last_bts_port; |
| 177 | |
| 178 | /* MGW handling */ |
| 179 | int configure_trunks; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | /* config management */ |
| 183 | struct mgcp_config *mgcp_config_alloc(void); |
| 184 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg); |
| 185 | int mgcp_vty_init(void); |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 186 | int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 187 | void mgcp_free_endp(struct mgcp_endpoint *endp); |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 188 | int mgcp_reset_transcoder(struct mgcp_config *cfg); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * format helper functions |
| 192 | */ |
| 193 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
Holger Hans Peter Freyther | 7e0936e | 2011-01-06 19:48:55 +0100 | [diff] [blame] | 194 | struct msgb *mgcp_create_response_with_data(int code, const char *txt, const char *msg, const char *trans, const char *data); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 195 | |
| 196 | /* adc helper */ |
| 197 | static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot) |
| 198 | { |
Holger Hans Peter Freyther | dcc3312 | 2011-03-01 01:24:10 +0100 | [diff] [blame] | 199 | if (timeslot == 0) { |
| 200 | LOGP(DMGCP, LOGL_ERROR, "Timeslot should not be 0\n"); |
| 201 | timeslot = 255; |
| 202 | } |
| 203 | |
| 204 | return timeslot + (32 * multiplex); |
| 205 | } |
| 206 | |
| 207 | static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot) |
| 208 | { |
| 209 | *multiplex = endpoint / 32; |
| 210 | *timeslot = endpoint % 32; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | |
| 214 | #endif |