Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 4 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2009-2010 by On-Waves |
Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 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 General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 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 General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 24 | #ifndef OPENBSC_MGCP_H |
| 25 | #define OPENBSC_MGCP_H |
| 26 | |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 27 | #include <osmocore/msgb.h> |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 28 | |
Holger Hans Peter Freyther | e3946f4 | 2010-09-20 01:07:23 +0800 | [diff] [blame^] | 29 | #include "debug.h" |
| 30 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 31 | #include <arpa/inet.h> |
| 32 | |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 33 | #define RTP_PORT_DEFAULT 4000 |
Holger Hans Peter Freyther | 314584a | 2010-08-05 04:10:21 +0800 | [diff] [blame] | 34 | #define RTP_PORT_NET_DEFAULT 16000 |
Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 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 | */ |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 55 | static inline int rtp_calculate_port(int multiplex, int base) |
Holger Hans Peter Freyther | f67945f | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 56 | { |
| 57 | return base + (multiplex * 2); |
| 58 | } |
Holger Hans Peter Freyther | e095502 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 59 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 60 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 61 | /* |
| 62 | * Handling of MGCP Endpoints and the MGCP Config |
| 63 | */ |
| 64 | struct mgcp_endpoint; |
| 65 | struct mgcp_config; |
Holger Hans Peter Freyther | 77f7afe | 2010-02-03 09:54:43 +0100 | [diff] [blame] | 66 | |
Holger Hans Peter Freyther | 77f7afe | 2010-02-03 09:54:43 +0100 | [diff] [blame] | 67 | #define MGCP_ENDP_CRCX 1 |
| 68 | #define MGCP_ENDP_DLCX 2 |
| 69 | #define MGCP_ENDP_MDCX 3 |
| 70 | |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 71 | /* |
| 72 | * what to do with the msg? |
| 73 | * - continue as usual? |
| 74 | * - reject and send a failure code? |
| 75 | * - defer? do not send anything |
| 76 | */ |
| 77 | #define MGCP_POLICY_CONT 4 |
| 78 | #define MGCP_POLICY_REJECT 5 |
| 79 | #define MGCP_POLICY_DEFER 6 |
| 80 | |
Holger Hans Peter Freyther | 869e38e | 2010-08-06 17:54:27 +0800 | [diff] [blame] | 81 | typedef int (*mgcp_realloc)(struct mgcp_config *cfg, int endpoint); |
Holger Hans Peter Freyther | 58ff219 | 2010-08-05 03:08:35 +0800 | [diff] [blame] | 82 | typedef int (*mgcp_change)(struct mgcp_config *cfg, int endpoint, int state); |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 83 | typedef int (*mgcp_policy)(struct mgcp_config *cfg, int endpoint, int state, const char *transactio_id); |
Holger Hans Peter Freyther | 9bdcc9c | 2010-03-31 06:39:35 +0200 | [diff] [blame] | 84 | typedef int (*mgcp_reset)(struct mgcp_config *cfg); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 85 | |
Holger Hans Peter Freyther | 15e7389 | 2010-08-05 07:10:56 +0800 | [diff] [blame] | 86 | #define PORT_ALLOC_STATIC 0 |
| 87 | #define PORT_ALLOC_DYNAMIC 1 |
| 88 | |
| 89 | /** |
| 90 | * This holds information on how to allocate ports |
| 91 | */ |
| 92 | struct mgcp_port_range { |
| 93 | int mode; |
| 94 | |
| 95 | /* pre-allocated from a base? */ |
| 96 | int base_port; |
Holger Hans Peter Freyther | 1be9f2f | 2010-08-05 07:20:09 +0800 | [diff] [blame] | 97 | |
| 98 | /* dynamically allocated */ |
| 99 | int range_start; |
| 100 | int range_end; |
| 101 | int last_port; |
Holger Hans Peter Freyther | 15e7389 | 2010-08-05 07:10:56 +0800 | [diff] [blame] | 102 | }; |
| 103 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 104 | struct mgcp_config { |
| 105 | int source_port; |
| 106 | char *local_ip; |
| 107 | char *source_addr; |
| 108 | unsigned int number_endpoints; |
| 109 | char *bts_ip; |
Holger Hans Peter Freyther | b79994c | 2010-03-31 11:46:41 +0200 | [diff] [blame] | 110 | char *call_agent_addr; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 111 | |
| 112 | struct in_addr bts_in; |
| 113 | char *audio_name; |
| 114 | int audio_payload; |
| 115 | int audio_loop; |
Holger Hans Peter Freyther | 15e7389 | 2010-08-05 07:10:56 +0800 | [diff] [blame] | 116 | |
| 117 | struct mgcp_port_range bts_ports; |
| 118 | struct mgcp_port_range net_ports; |
Holger Hans Peter Freyther | d0c3229 | 2010-07-27 20:34:45 +0800 | [diff] [blame] | 119 | int endp_dscp; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 120 | |
Holger Hans Peter Freyther | 408cc4a | 2010-04-07 10:51:27 +0200 | [diff] [blame] | 121 | /* spec handling */ |
| 122 | int force_realloc; |
| 123 | |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 124 | mgcp_change change_cb; |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 125 | mgcp_policy policy_cb; |
Holger Hans Peter Freyther | 9bdcc9c | 2010-03-31 06:39:35 +0200 | [diff] [blame] | 126 | mgcp_reset reset_cb; |
Holger Hans Peter Freyther | 869e38e | 2010-08-06 17:54:27 +0800 | [diff] [blame] | 127 | mgcp_realloc realloc_cb; |
Holger Hans Peter Freyther | fe86d3c | 2010-02-26 13:37:05 +0100 | [diff] [blame] | 128 | void *data; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 129 | |
| 130 | struct mgcp_endpoint *endpoints; |
Holger Hans Peter Freyther | 4634013 | 2010-08-06 08:26:54 +0800 | [diff] [blame] | 131 | uint32_t last_call_id; |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | /* config management */ |
| 135 | struct mgcp_config *mgcp_config_alloc(void); |
| 136 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg); |
| 137 | int mgcp_vty_init(void); |
| 138 | int mgcp_endpoints_allocate(struct mgcp_config *cfg); |
Holger Hans Peter Freyther | 154b955 | 2010-02-26 13:27:51 +0100 | [diff] [blame] | 139 | void mgcp_free_endp(struct mgcp_endpoint *endp); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * format helper functions |
| 143 | */ |
| 144 | struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg); |
Holger Hans Peter Freyther | 6414a0c | 2010-02-21 10:50:46 +0100 | [diff] [blame] | 145 | struct msgb *mgcp_create_response_with_data(int code, const char *msg, const char *trans, const char *data); |
Holger Hans Peter Freyther | 7bdc637 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 146 | |
Holger Hans Peter Freyther | 616d222 | 2010-03-31 09:27:04 +0200 | [diff] [blame] | 147 | /* adc helper */ |
| 148 | static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot) |
| 149 | { |
Holger Hans Peter Freyther | e3946f4 | 2010-09-20 01:07:23 +0800 | [diff] [blame^] | 150 | if (timeslot == 0) { |
| 151 | LOGP(DMGCP, LOGL_ERROR, "Timeslot should not be 0\n"); |
| 152 | timeslot = 255; |
| 153 | } |
| 154 | |
Holger Hans Peter Freyther | cb8c35c | 2010-08-10 20:24:24 +0800 | [diff] [blame] | 155 | return timeslot + (32 * multiplex); |
Holger Hans Peter Freyther | 616d222 | 2010-03-31 09:27:04 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Holger Hans Peter Freyther | 82049d8 | 2010-08-28 17:59:15 +0800 | [diff] [blame] | 158 | static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot) |
| 159 | { |
| 160 | *multiplex = endpoint / 32; |
| 161 | *timeslot = endpoint % 32; |
Holger Hans Peter Freyther | 82049d8 | 2010-08-28 17:59:15 +0800 | [diff] [blame] | 162 | } |
| 163 | |
Holger Hans Peter Freyther | 62e836c | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 164 | |
| 165 | #endif |