blob: 3d8e7116b5b42cf28ca3288badd416280c994235 [file] [log] [blame]
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2
3/*
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +01004 * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009-2011 by On-Waves
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020011 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * 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 Freytherf67945f2009-10-09 07:08:11 +020020 *
21 */
22
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +010023#ifndef OPENBSC_MGCP_H
24#define OPENBSC_MGCP_H
25
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010026#include <osmocom/core/msgb.h>
27#include <osmocom/core/write_queue.h>
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +010028
Holger Hans Peter Freythere3946f42010-09-20 01:07:23 +080029#include "debug.h"
30
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010031#include <arpa/inet.h>
32
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010033#define RTP_PORT_DEFAULT 4000
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +080034#define RTP_PORT_NET_DEFAULT 16000
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020035
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 */
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010055static inline int rtp_calculate_port(int multiplex, int base)
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020056{
57 return base + (multiplex * 2);
58}
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010059
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +010060
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010061/*
62 * Handling of MGCP Endpoints and the MGCP Config
63 */
64struct mgcp_endpoint;
65struct mgcp_config;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +010066struct mgcp_trunk_config;
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +010067
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +010068#define MGCP_ENDP_CRCX 1
69#define MGCP_ENDP_DLCX 2
70#define MGCP_ENDP_MDCX 3
71
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +010072/*
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 Freyther88ad7722011-02-28 00:56:17 +010082typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint);
83typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state);
84typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id);
Holger Hans Peter Freyther9bdcc9c2010-03-31 06:39:35 +020085typedef int (*mgcp_reset)(struct mgcp_config *cfg);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010086
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +080087#define PORT_ALLOC_STATIC 0
88#define PORT_ALLOC_DYNAMIC 1
89
90/**
91 * This holds information on how to allocate ports
92 */
93struct mgcp_port_range {
94 int mode;
95
96 /* pre-allocated from a base? */
97 int base_port;
Holger Hans Peter Freyther1be9f2f2010-08-05 07:20:09 +080098
99 /* dynamically allocated */
100 int range_start;
101 int range_end;
102 int last_port;
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +0800103};
104
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100105struct mgcp_trunk_config {
Holger Hans Peter Freyther0e939fe2011-02-28 12:11:02 +0100106 struct llist_head entry;
107
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100108 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
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100124struct mgcp_config {
125 int source_port;
126 char *local_ip;
127 char *source_addr;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100128 char *bts_ip;
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200129 char *call_agent_addr;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100130
131 struct in_addr bts_in;
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +0800132
Holger Hans Peter Freytherb98ba722010-09-19 04:21:39 +0800133 /* transcoder handling */
134 char *transcoder_ip;
135 struct in_addr transcoder_in;
136 int transcoder_remote_base;
137
Holger Hans Peter Freyther6f680102010-09-18 20:40:22 +0800138 struct write_queue gw_fd;
139
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +0800140 struct mgcp_port_range bts_ports;
141 struct mgcp_port_range net_ports;
Holger Hans Peter Freyther54aaa0f2010-09-17 23:35:53 +0800142 struct mgcp_port_range transcoder_ports;
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800143 int endp_dscp;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100144
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100145 mgcp_change change_cb;
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100146 mgcp_policy policy_cb;
Holger Hans Peter Freyther9bdcc9c2010-03-31 06:39:35 +0200147 mgcp_reset reset_cb;
Holger Hans Peter Freyther869e38e2010-08-06 17:54:27 +0800148 mgcp_realloc realloc_cb;
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100149 void *data;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100150
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800151 uint32_t last_call_id;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100152
153 /* trunk handling */
154 struct mgcp_trunk_config trunk;
Holger Hans Peter Freyther0e939fe2011-02-28 12:11:02 +0100155 struct llist_head trunks;
Holger Hans Peter Freyther44016fe2011-02-28 14:46:01 +0100156
157 /* only used for start with a static configuration */
158 int last_net_port;
159 int last_bts_port;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100160};
161
162/* config management */
163struct mgcp_config *mgcp_config_alloc(void);
164int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg);
165int mgcp_vty_init(void);
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +0100166int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg);
Holger Hans Peter Freyther154b9552010-02-26 13:27:51 +0100167void mgcp_free_endp(struct mgcp_endpoint *endp);
Holger Hans Peter Freytherf2eedff2010-09-19 04:36:07 +0800168int mgcp_reset_transcoder(struct mgcp_config *cfg);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100169
170/*
171 * format helper functions
172 */
173struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freyther6adac172011-01-06 16:19:55 +0100174struct msgb *mgcp_create_response_with_data(int code, const char *txt, const char *msg, const char *trans, const char *data);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100175
Holger Hans Peter Freyther616d2222010-03-31 09:27:04 +0200176/* adc helper */
177static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot)
178{
Holger Hans Peter Freythere3946f42010-09-20 01:07:23 +0800179 if (timeslot == 0) {
180 LOGP(DMGCP, LOGL_ERROR, "Timeslot should not be 0\n");
181 timeslot = 255;
182 }
183
Holger Hans Peter Freythercb8c35c2010-08-10 20:24:24 +0800184 return timeslot + (32 * multiplex);
Holger Hans Peter Freyther616d2222010-03-31 09:27:04 +0200185}
186
Holger Hans Peter Freyther82049d82010-08-28 17:59:15 +0800187static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot)
188{
189 *multiplex = endpoint / 32;
190 *timeslot = endpoint % 32;
Holger Hans Peter Freyther82049d82010-08-28 17:59:15 +0800191}
192
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100193
194#endif