blob: b9f81161fbc5e2ef5ecd994aff93315ef798926e [file] [log] [blame]
Jonathan Santos03fd8d02011-05-25 13:54:02 -04001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2
3/*
4 * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009-2011 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * 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
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
16 * GNU Affero General Public License for more details.
17 *
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/>.
20 *
21 */
22
23#ifndef OPENBSC_MGCP_H
24#define OPENBSC_MGCP_H
25
Jonathan Santos5a45b152011-08-17 15:33:57 -040026#include <osmocom/core/msgb.h>
27#include <osmocom/core/write_queue.h>
Jonathan Santos03fd8d02011-05-25 13:54:02 -040028
29#include "debug.h"
30
31#include <arpa/inet.h>
32
33#define RTP_PORT_DEFAULT 4000
34#define RTP_PORT_NET_DEFAULT 16000
35
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 */
55static 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 */
64struct mgcp_endpoint;
65struct mgcp_config;
66struct mgcp_trunk_config;
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
82typedef 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);
85typedef int (*mgcp_reset)(struct mgcp_config *cfg);
86
87#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;
98
99 /* dynamically allocated */
100 int range_start;
101 int range_end;
102 int last_port;
103};
104
105struct 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
124struct mgcp_config {
125 int source_port;
126 char *local_ip;
127 char *source_addr;
128 char *bts_ip;
129 char *call_agent_addr;
130
131 struct in_addr bts_in;
132
133 /* transcoder handling */
134 char *transcoder_ip;
135 struct in_addr transcoder_in;
136 int transcoder_remote_base;
137
Jonathan Santos5a45b152011-08-17 15:33:57 -0400138 struct osmo_wqueue gw_fd;
Jonathan Santos03fd8d02011-05-25 13:54:02 -0400139
140 struct mgcp_port_range bts_ports;
141 struct mgcp_port_range net_ports;
142 struct mgcp_port_range transcoder_ports;
143 int endp_dscp;
144
145 mgcp_change change_cb;
146 mgcp_policy policy_cb;
147 mgcp_reset reset_cb;
148 mgcp_realloc realloc_cb;
149 void *data;
150
151 uint32_t last_call_id;
152
153 /* trunk handling */
154 struct mgcp_trunk_config trunk;
155 struct llist_head trunks;
156
157 /* only used for start with a static configuration */
158 int last_net_port;
159 int last_bts_port;
160};
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);
166int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg);
167void mgcp_free_endp(struct mgcp_endpoint *endp);
168int mgcp_reset_transcoder(struct mgcp_config *cfg);
169
170/*
171 * format helper functions
172 */
173struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
174struct msgb *mgcp_create_response_with_data(int code, const char *txt, const char *msg, const char *trans, const char *data);
175
176/* adc helper */
177static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot)
178{
179 if (timeslot == 0) {
180 LOGP(DMGCP, LOGL_ERROR, "Timeslot should not be 0\n");
181 timeslot = 255;
182 }
183
184 return timeslot + (32 * multiplex);
185}
186
187static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot)
188{
189 *multiplex = endpoint / 32;
190 *timeslot = endpoint % 32;
191}
192
193
194#endif