Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2010 by On-Waves |
| 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 General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 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 General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <openbsc/bsc_nat.h> |
| 23 | #include <openbsc/gsm_data.h> |
| 24 | #include <openbsc/bssap.h> |
| 25 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 1f27839 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 26 | #include <openbsc/mgcp.h> |
| 27 | #include <openbsc/mgcp_internal.h> |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 28 | |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 29 | #include <osmocore/talloc.h> |
| 30 | |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 31 | #include <netinet/in.h> |
| 32 | #include <arpa/inet.h> |
| 33 | |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 34 | #include <errno.h> |
| 35 | #include <unistd.h> |
| 36 | |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 37 | int bsc_mgcp_assign(struct sccp_connections *con, struct msgb *msg) |
| 38 | { |
| 39 | struct tlv_parsed tp; |
| 40 | u_int16_t cic; |
| 41 | u_int8_t timeslot; |
| 42 | u_int8_t multiplex; |
| 43 | |
| 44 | if (!msg->l3h) { |
| 45 | LOGP(DNAT, LOGL_ERROR, "Assignment message should have l3h pointer.\n"); |
| 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | if (msgb_l3len(msg) < 3) { |
| 50 | LOGP(DNAT, LOGL_ERROR, "Assignment message has not enough space for GSM0808.\n"); |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 55 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)) { |
| 56 | LOGP(DNAT, LOGL_ERROR, "Circuit identity code not found in assignment message.\n"); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | cic = ntohs(*(u_int16_t *)TLVP_VAL(&tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE)); |
| 61 | timeslot = cic & 0x1f; |
| 62 | multiplex = (cic & ~0x1f) >> 5; |
| 63 | |
| 64 | con->msc_timeslot = (32 * multiplex) + timeslot; |
| 65 | con->bsc_timeslot = con->msc_timeslot; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void bsc_mgcp_clear(struct sccp_connections *con) |
| 70 | { |
| 71 | con->msc_timeslot = -1; |
| 72 | con->bsc_timeslot = -1; |
| 73 | } |
Holger Hans Peter Freyther | 1f27839 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 74 | |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 75 | void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int i) |
| 76 | { |
| 77 | if (nat->bsc_endpoints[i].transaction_id) { |
| 78 | talloc_free(nat->bsc_endpoints[i].transaction_id); |
| 79 | nat->bsc_endpoints[i].transaction_id = NULL; |
| 80 | } |
| 81 | |
| 82 | nat->bsc_endpoints[i].bsc = NULL; |
| 83 | mgcp_free_endp(&nat->mgcp_cfg->endpoints[i]); |
| 84 | } |
| 85 | |
Holger Hans Peter Freyther | 1f27839 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 86 | void bsc_mgcp_free_endpoints(struct bsc_nat *nat) |
| 87 | { |
| 88 | int i; |
| 89 | |
| 90 | for (i = 1; i < nat->mgcp_cfg->number_endpoints; ++i) |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 91 | bsc_mgcp_free_endpoint(nat, i); |
Holger Hans Peter Freyther | 1f27839 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 92 | } |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 93 | |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 94 | struct bsc_connection *bsc_mgcp_find_con(struct bsc_nat *nat, int endpoint) |
| 95 | { |
| 96 | struct sccp_connections *sccp; |
| 97 | |
| 98 | llist_for_each_entry(sccp, &nat->sccp_connections, list_entry) { |
| 99 | if (sccp->msc_timeslot == -1) |
| 100 | continue; |
| 101 | if (mgcp_timeslot_to_endpoint(0, sccp->msc_timeslot) != endpoint) |
| 102 | continue; |
| 103 | |
| 104 | return sccp->bsc; |
| 105 | } |
| 106 | |
| 107 | LOGP(DMGCP, LOGL_ERROR, "Failed to find the connection.\n"); |
| 108 | return NULL; |
| 109 | } |
| 110 | |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 111 | int bsc_mgcp_policy_cb(struct mgcp_config *cfg, int endpoint, int state, const char *transaction_id) |
| 112 | { |
| 113 | struct bsc_nat *nat; |
| 114 | struct bsc_endpoint *bsc_endp; |
| 115 | struct bsc_connection *bsc_con; |
| 116 | struct mgcp_endpoint *mgcp_endp; |
| 117 | struct msgb *bsc_msg; |
| 118 | |
| 119 | nat = cfg->data; |
| 120 | bsc_endp = &nat->bsc_endpoints[endpoint]; |
| 121 | mgcp_endp = &nat->mgcp_cfg->endpoints[endpoint]; |
| 122 | |
| 123 | bsc_con = bsc_mgcp_find_con(nat, endpoint); |
| 124 | |
| 125 | if (!bsc_con) { |
| 126 | LOGP(DMGCP, LOGL_ERROR, "Did not find BSC for a new connection on 0x%x for %d\n", endpoint, state); |
| 127 | |
| 128 | switch (state) { |
| 129 | case MGCP_ENDP_CRCX: |
| 130 | return MGCP_POLICY_REJECT; |
| 131 | break; |
| 132 | case MGCP_ENDP_DLCX: |
| 133 | return MGCP_POLICY_CONT; |
| 134 | break; |
| 135 | case MGCP_ENDP_MDCX: |
| 136 | return MGCP_POLICY_CONT; |
| 137 | break; |
| 138 | default: |
| 139 | LOGP(DMGCP, LOGL_FATAL, "Unhandled state: %d\n", state); |
| 140 | return MGCP_POLICY_CONT; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (bsc_endp->transaction_id) { |
| 146 | LOGP(DMGCP, LOGL_ERROR, "One transaction with id '%s' on 0x%x\n", |
| 147 | bsc_endp->transaction_id, endpoint); |
| 148 | talloc_free(bsc_endp->transaction_id); |
| 149 | } |
| 150 | |
Holger Hans Peter Freyther | 8afdcba | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 151 | /* we need to generate a new and patched message */ |
| 152 | bsc_msg = bsc_mgcp_rewrite((char *) nat->mgcp_msg, nat->mgcp_length, |
| 153 | nat->mgcp_cfg->source_addr, mgcp_endp->rtp_port); |
| 154 | if (!bsc_msg) { |
| 155 | LOGP(DMGCP, LOGL_ERROR, "Failed to patch the msg.\n"); |
| 156 | return MGCP_POLICY_CONT; |
| 157 | } |
| 158 | |
| 159 | |
Holger Hans Peter Freyther | e85bc49 | 2010-04-04 18:11:49 +0200 | [diff] [blame] | 160 | bsc_endp->transaction_id = talloc_strdup(nat, transaction_id); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 161 | bsc_endp->bsc = bsc_con; |
Holger Hans Peter Freyther | 8afdcba | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 162 | bsc_endp->pending_delete = 0; |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 163 | |
| 164 | /* we need to update some bits */ |
| 165 | if (state == MGCP_ENDP_CRCX) { |
| 166 | struct sockaddr_in sock; |
| 167 | socklen_t len = sizeof(sock); |
Holger Hans Peter Freyther | 0befa6d | 2010-04-06 11:17:56 +0200 | [diff] [blame] | 168 | if (getpeername(bsc_con->write_queue.bfd.fd, (struct sockaddr *) &sock, &len) != 0) { |
Holger Hans Peter Freyther | 75749d0 | 2010-04-06 11:17:07 +0200 | [diff] [blame] | 169 | LOGP(DMGCP, LOGL_ERROR, "Can not get the peername...%d/%s\n", |
| 170 | errno, strerror(errno)); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 171 | } else { |
| 172 | mgcp_endp->bts = sock.sin_addr; |
| 173 | } |
Holger Hans Peter Freyther | 8afdcba | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 174 | } else if (state == MGCP_ENDP_DLCX) { |
| 175 | /* we will free the endpoint now in case the BSS does not respond */ |
| 176 | bsc_endp->pending_delete = 1; |
| 177 | mgcp_free_endp(mgcp_endp); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | bsc_write_mgcp_msg(bsc_con, bsc_msg); |
| 181 | return MGCP_POLICY_DEFER; |
| 182 | } |
| 183 | |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 184 | /* |
| 185 | * We have received a msg from the BSC. We will see if we know |
| 186 | * this transaction and if it belongs to the BSC. Then we will |
| 187 | * need to patch the content to point to the local network and we |
| 188 | * need to update the I: that was assigned by the BSS. |
| 189 | */ |
| 190 | void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg) |
| 191 | { |
| 192 | struct msgb *output; |
| 193 | struct bsc_endpoint *bsc_endp = NULL; |
| 194 | struct mgcp_endpoint *endp = NULL; |
Holger Hans Peter Freyther | 65b745e | 2010-04-06 11:06:11 +0200 | [diff] [blame] | 195 | int i, code; |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 196 | char transaction_id[60]; |
| 197 | |
| 198 | /* Some assumption that our buffer is big enough.. and null terminate */ |
| 199 | if (msgb_l2len(msg) > 2000) { |
| 200 | LOGP(DMGCP, LOGL_ERROR, "MGCP message too long.\n"); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | msg->l2h[msgb_l2len(msg)] = '\0'; |
| 205 | |
| 206 | if (bsc_mgcp_parse_response((const char *) msg->l2h, &code, transaction_id) != 0) { |
| 207 | LOGP(DMGCP, LOGL_ERROR, "Failed to parse response code.\n"); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) { |
| 212 | if (bsc->nat->bsc_endpoints[i].bsc != bsc) |
| 213 | continue; |
Holger Hans Peter Freyther | f9c8d0f | 2010-04-05 18:00:05 +0200 | [diff] [blame] | 214 | /* no one listening? a bug? */ |
| 215 | if (!bsc->nat->bsc_endpoints[i].transaction_id) |
| 216 | continue; |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 217 | if (strcmp(transaction_id, bsc->nat->bsc_endpoints[i].transaction_id) != 0) |
| 218 | continue; |
| 219 | |
| 220 | endp = &bsc->nat->mgcp_cfg->endpoints[i]; |
| 221 | bsc_endp = &bsc->nat->bsc_endpoints[i]; |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | if (!bsc_endp) { |
Holger Hans Peter Freyther | c12e193 | 2010-04-05 17:58:52 +0200 | [diff] [blame] | 226 | LOGP(DMGCP, LOGL_ERROR, "Could not find active endpoint: %s for msg: '%s'\n", |
| 227 | transaction_id, (const char *) msg->l2h); |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
Holger Hans Peter Freyther | 8afdcba | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 231 | /* make it point to our endpoint if it was not deleted */ |
| 232 | if (bsc_endp->pending_delete) { |
| 233 | bsc_endp->bsc = NULL; |
| 234 | bsc_endp->pending_delete = 0; |
| 235 | } else { |
| 236 | endp->ci = bsc_mgcp_extract_ci((const char *) msg->l2h); |
| 237 | } |
| 238 | |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 239 | /* free some stuff */ |
| 240 | talloc_free(bsc_endp->transaction_id); |
| 241 | bsc_endp->transaction_id = NULL; |
| 242 | |
Holger Hans Peter Freyther | 8afdcba | 2010-04-07 09:53:54 +0200 | [diff] [blame] | 243 | /* |
| 244 | * rewrite the information. In case the endpoint was deleted |
| 245 | * there should be nothing for us to rewrite so putting endp->rtp_port |
| 246 | * with the value of 0 should be no problem. |
| 247 | */ |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 248 | output = bsc_mgcp_rewrite((char * ) msg->l2h, msgb_l2len(msg), |
| 249 | bsc->nat->mgcp_cfg->source_addr, endp->rtp_port); |
Holger Hans Peter Freyther | 55edb78 | 2010-04-05 22:56:49 +0200 | [diff] [blame] | 250 | |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 251 | if (!output) { |
| 252 | LOGP(DMGCP, LOGL_ERROR, "Failed to rewrite MGCP msg.\n"); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | if (write_queue_enqueue(&bsc->nat->mgcp_queue, output) != 0) { |
| 257 | LOGP(DMGCP, LOGL_ERROR, "Failed to queue MGCP msg.\n"); |
| 258 | msgb_free(output); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]) |
| 263 | { |
| 264 | /* we want to parse two strings */ |
| 265 | return sscanf(str, "%3d %59s\n", code, transaction) != 2; |
| 266 | } |
| 267 | |
| 268 | int bsc_mgcp_extract_ci(const char *str) |
| 269 | { |
| 270 | int ci; |
| 271 | char *res = strstr(str, "I: "); |
| 272 | if (!res) |
| 273 | return CI_UNUSED; |
| 274 | |
Holger Hans Peter Freyther | 81bda81 | 2010-04-05 09:07:39 +0200 | [diff] [blame] | 275 | if (sscanf(res, "I: %d", &ci) != 1) |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 276 | return CI_UNUSED; |
| 277 | return ci; |
| 278 | } |
| 279 | |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 280 | /* we need to replace some strings... */ |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 281 | struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port) |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 282 | { |
| 283 | static const char *ip_str = "c=IN IP4 "; |
| 284 | static const char *aud_str = "m=audio "; |
| 285 | |
| 286 | char buf[128]; |
| 287 | char *running, *token; |
| 288 | struct msgb *output; |
| 289 | |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 290 | if (length > 4096 - 128) { |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 291 | LOGP(DMGCP, LOGL_ERROR, "Input is too long.\n"); |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | output = msgb_alloc_headroom(4096, 128, "MGCP rewritten"); |
| 296 | if (!output) { |
| 297 | LOGP(DMGCP, LOGL_ERROR, "Failed to allocate new MGCP msg.\n"); |
| 298 | return NULL; |
| 299 | } |
| 300 | |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 301 | running = input; |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 302 | output->l2h = output->data; |
Holger Hans Peter Freyther | 3237065 | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 303 | for (token = strsep(&running, "\n"); running; token = strsep(&running, "\n")) { |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 304 | int len = strlen(token); |
Holger Hans Peter Freyther | 3237065 | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 305 | int cr = len > 0 && token[len - 1] == '\r'; |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 306 | |
| 307 | if (strncmp(ip_str, token, (sizeof ip_str) - 1) == 0) { |
| 308 | output->l3h = msgb_put(output, strlen(ip_str)); |
| 309 | memcpy(output->l3h, ip_str, strlen(ip_str)); |
| 310 | output->l3h = msgb_put(output, strlen(ip)); |
| 311 | memcpy(output->l3h, ip, strlen(ip)); |
Holger Hans Peter Freyther | 3237065 | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 312 | |
| 313 | if (cr) { |
| 314 | output->l3h = msgb_put(output, 2); |
| 315 | output->l3h[0] = '\r'; |
| 316 | output->l3h[1] = '\n'; |
| 317 | } else { |
| 318 | output->l3h = msgb_put(output, 1); |
| 319 | output->l3h[0] = '\n'; |
| 320 | } |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 321 | } else if (strncmp(aud_str, token, (sizeof aud_str) - 1) == 0) { |
| 322 | int payload; |
| 323 | if (sscanf(token, "m=audio %*d RTP/AVP %d", &payload) != 1) { |
| 324 | LOGP(DMGCP, LOGL_ERROR, "Could not parsed audio line.\n"); |
| 325 | msgb_free(output); |
| 326 | return NULL; |
| 327 | } |
| 328 | |
Holger Hans Peter Freyther | 3237065 | 2010-04-04 19:34:44 +0200 | [diff] [blame] | 329 | snprintf(buf, sizeof(buf)-1, "m=audio %d RTP/AVP %d%s", |
| 330 | port, payload, cr ? "\r\n" : "\n"); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 331 | buf[sizeof(buf)-1] = '\0'; |
| 332 | |
| 333 | output->l3h = msgb_put(output, strlen(buf)); |
| 334 | memcpy(output->l3h, buf, strlen(buf)); |
| 335 | } else { |
| 336 | output->l3h = msgb_put(output, len + 1); |
| 337 | memcpy(output->l3h, token, len); |
| 338 | output->l3h[len] = '\n'; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return output; |
| 343 | } |
| 344 | |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 345 | static int mgcp_do_read(struct bsc_fd *fd) |
| 346 | { |
| 347 | struct bsc_nat *nat; |
| 348 | struct msgb *msg, *resp; |
| 349 | int rc; |
| 350 | |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 351 | nat = fd->data; |
| 352 | |
| 353 | rc = read(fd->fd, nat->mgcp_msg, sizeof(nat->mgcp_msg) - 1); |
| 354 | if (rc <= 0) { |
| 355 | LOGP(DMGCP, LOGL_ERROR, "Failed to read errno: %d\n", errno); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | nat->mgcp_msg[rc] = '\0'; |
| 360 | nat->mgcp_length = rc; |
| 361 | |
| 362 | msg = msgb_alloc(sizeof(nat->mgcp_msg), "MGCP GW Read"); |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 363 | if (!msg) { |
| 364 | LOGP(DMGCP, LOGL_ERROR, "Failed to create buffer.\n"); |
| 365 | return -1; |
| 366 | } |
| 367 | |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 368 | msg->l2h = msgb_put(msg, rc); |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 369 | memcpy(msg->l2h, nat->mgcp_msg, msgb_l2len(msg)); |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 370 | resp = mgcp_handle_message(nat->mgcp_cfg, msg); |
| 371 | msgb_free(msg); |
| 372 | |
| 373 | /* we do have a direct answer... e.g. AUEP */ |
| 374 | if (resp) { |
| 375 | if (write_queue_enqueue(&nat->mgcp_queue, resp) != 0) { |
| 376 | LOGP(DMGCP, LOGL_ERROR, "Failed to enqueue msg.\n"); |
| 377 | msgb_free(resp); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static int mgcp_do_write(struct bsc_fd *bfd, struct msgb *msg) |
| 385 | { |
| 386 | int rc; |
| 387 | |
| 388 | rc = write(bfd->fd, msg->data, msg->len); |
| 389 | |
| 390 | if (rc != msg->len) { |
| 391 | LOGP(DMGCP, LOGL_ERROR, "Failed to write msg to MGCP CallAgent.\n"); |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | return rc; |
| 396 | } |
| 397 | |
| 398 | int bsc_mgcp_init(struct bsc_nat *nat) |
| 399 | { |
| 400 | int on; |
| 401 | struct sockaddr_in addr; |
| 402 | |
| 403 | if (!nat->mgcp_cfg->call_agent_addr) { |
| 404 | LOGP(DMGCP, LOGL_ERROR, "The BSC nat requires the call agent ip to be set.\n"); |
| 405 | return -1; |
| 406 | } |
| 407 | |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 408 | if (nat->mgcp_cfg->bts_ip) { |
| 409 | LOGP(DMGCP, LOGL_ERROR, "Do not set the BTS ip for the nat.\n"); |
| 410 | return -1; |
| 411 | } |
| 412 | |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 413 | nat->mgcp_queue.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 414 | if (nat->mgcp_queue.bfd.fd < 0) { |
| 415 | LOGP(DMGCP, LOGL_ERROR, "Failed to create MGCP socket. errno: %d\n", errno); |
| 416 | return -1; |
| 417 | } |
| 418 | |
| 419 | on = 1; |
| 420 | setsockopt(nat->mgcp_queue.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 421 | |
| 422 | addr.sin_family = AF_INET; |
| 423 | addr.sin_port = htons(nat->mgcp_cfg->source_port); |
| 424 | inet_aton(nat->mgcp_cfg->source_addr, &addr.sin_addr); |
| 425 | |
| 426 | if (bind(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 427 | LOGP(DMGCP, LOGL_ERROR, "Failed to bind. errno: %d\n", errno); |
| 428 | close(nat->mgcp_queue.bfd.fd); |
| 429 | nat->mgcp_queue.bfd.fd = -1; |
| 430 | return -1; |
| 431 | } |
| 432 | |
| 433 | addr.sin_port = htons(2727); |
| 434 | inet_aton(nat->mgcp_cfg->call_agent_addr, &addr.sin_addr); |
| 435 | if (connect(nat->mgcp_queue.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 436 | LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n", |
| 437 | nat->mgcp_cfg->call_agent_addr, errno); |
| 438 | close(nat->mgcp_queue.bfd.fd); |
| 439 | nat->mgcp_queue.bfd.fd = -1; |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | write_queue_init(&nat->mgcp_queue, 10); |
| 444 | nat->mgcp_queue.bfd.when = BSC_FD_READ; |
| 445 | nat->mgcp_queue.bfd.data = nat; |
| 446 | nat->mgcp_queue.read_cb = mgcp_do_read; |
| 447 | nat->mgcp_queue.write_cb = mgcp_do_write; |
| 448 | |
| 449 | if (bsc_register_fd(&nat->mgcp_queue.bfd) != 0) { |
| 450 | LOGP(DMGCP, LOGL_ERROR, "Failed to register MGCP fd.\n"); |
| 451 | close(nat->mgcp_queue.bfd.fd); |
| 452 | nat->mgcp_queue.bfd.fd = -1; |
| 453 | return -1; |
| 454 | } |
| 455 | |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 456 | /* some more MGCP config handling */ |
Holger Hans Peter Freyther | 88628f5 | 2010-04-06 11:14:03 +0200 | [diff] [blame] | 457 | nat->mgcp_cfg->audio_payload = -1; |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 458 | nat->mgcp_cfg->data = nat; |
| 459 | nat->mgcp_cfg->policy_cb = bsc_mgcp_policy_cb; |
| 460 | nat->bsc_endpoints = talloc_zero_array(nat, |
| 461 | struct bsc_endpoint, |
| 462 | nat->mgcp_cfg->number_endpoints + 1); |
| 463 | |
Holger Hans Peter Freyther | 3468be3 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 464 | return 0; |
| 465 | } |
Holger Hans Peter Freyther | c0ff1d0 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 466 | |
| 467 | void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc) |
| 468 | { |
| 469 | int i; |
| 470 | for (i = 1; i < bsc->nat->mgcp_cfg->number_endpoints; ++i) { |
| 471 | struct bsc_endpoint *bsc_endp = &bsc->nat->bsc_endpoints[i]; |
| 472 | |
| 473 | if (bsc_endp->bsc != bsc) |
| 474 | continue; |
| 475 | |
| 476 | bsc_endp->bsc = NULL; |
| 477 | bsc_endp->pending_delete = 0; |
| 478 | if (bsc_endp->transaction_id) |
| 479 | talloc_free(bsc_endp->transaction_id); |
| 480 | bsc_endp->transaction_id = NULL; |
| 481 | mgcp_free_endp(&bsc->nat->mgcp_cfg->endpoints[i]); |
| 482 | } |
| 483 | } |