diff options
author | Holger Hans Peter Freyther <zecke@selfish.org> | 2012-12-10 15:50:09 +0100 |
---|---|---|
committer | Holger Hans Peter Freyther <zecke@selfish.org> | 2012-12-16 11:33:27 +0100 |
commit | 2d115d4bbb5b725c84bb00e7dd9ac9130e3b0789 (patch) | |
tree | b48cd8709c9e3fa0d68a06ded33cecd895f14e46 /src | |
parent | d74ac335748ad2fe4971fd7dcca653284b9bbc17 (diff) |
mgcp: Merge the current MGCP code from OpenBSC
There were several changes in the upstream code. These include
statistics, DTMF/RQNT, changes in the parsing code and re-transmission
handling. The last item is the main reason to do the merge now.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/mgcp/mgcp_network.c | 152 | ||||
-rw-r--r-- | src/mgcp/mgcp_protocol.c | 761 | ||||
-rw-r--r-- | src/mgcp/mgcp_vty.c | 211 | ||||
-rw-r--r-- | src/mgcp_ss7.c | 2 |
5 files changed, 700 insertions, 428 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index e681537..b8cacc1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,7 +9,7 @@ mgcp_mgw_SOURCES = mgcp_ss7.c mgcp_ss7_vty.c mgcp_hw.c thread.c debug.c \ mgcp/mgcp_protocol.c mgcp/mgcp_network.c mgcp/mgcp_vty.c \ dtmf_scheduler.c mgcp_mgw_LDADD = $(LAFORGE_LIBS) $(NEXUSWARE_C7_LIBS) $(NEXUSWARE_UNIPORTE_LIBS) \ - $(LIBOSMOVTY_LIBS) $(LIBOSMOCORE_LIBS) -lpthread -lcrypto + $(LIBOSMOVTY_LIBS) $(LIBOSMOCORE_LIBS) -lpthread -lcrypto -lrt cellmgr_ng_SOURCES = main.c mtp_layer3.c thread.c input/ipaccess.c pcap.c \ bss_patch.c bssap_sccp.c bsc_sccp.c bsc_ussd.c links.c \ diff --git a/src/mgcp/mgcp_network.c b/src/mgcp/mgcp_network.c index f930238..3bbc600 100644 --- a/src/mgcp/mgcp_network.c +++ b/src/mgcp/mgcp_network.c @@ -2,8 +2,8 @@ /* The protocol implementation */ /* - * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org> - * (C) 2009-2011 by On-Waves + * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> + * (C) 2009-2012 by On-Waves * All Rights Reserved * * This program is free software; you can redistribute it and/or modify @@ -38,12 +38,18 @@ #warning "Make use of the rtp proxy code" /* attempt to determine byte order */ -#include <sys/types.h> #include <sys/param.h> #include <limits.h> +#include <time.h> #ifndef __BYTE_ORDER -#error "__BYTE_ORDER should be defined by someone" +# ifdef __APPLE__ +# define __BYTE_ORDER __DARWIN_BYTE_ORDER +# define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN +# define __BIG_ENDIAN __DARWIN_BIG_ENDIAN +# else +# error "__BYTE_ORDER should be defined by someone" +# endif #endif /* according to rtp_proxy.c RFC 3550 */ @@ -68,6 +74,10 @@ struct rtp_hdr { uint32_t ssrc; } __attribute__((packed)); +#define RTP_SEQ_MOD (1 << 16) +#define RTP_MAX_DROPOUT 3000 +#define RTP_MAX_MISORDER 100 + enum { DEST_NETWORK = 0, @@ -82,6 +92,29 @@ enum { #define DUMMY_LOAD 0x23 +/** + * This does not need to be a precision timestamp and + * is allowed to wrap quite fast. The returned value is + * milli seconds now. + */ +uint32_t get_current_ts(void) +{ + struct timespec tp; + uint64_t ret; + + memset(&tp, 0, sizeof(tp)); + if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) + LOGP(DMGCP, LOGL_NOTICE, + "Getting the clock failed.\n"); + + /* convert it to useconds */ + ret = tp.tv_sec; + ret *= 1000; + ret += tp.tv_nsec / 1000 / 1000; + + return ret; +} + static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len) { struct sockaddr_in out; @@ -100,10 +133,20 @@ int mgcp_send_dummy(struct mgcp_endpoint *endp) endp->net_end.rtp_port, buf, 1); } +/** + * The RFC 3550 Appendix A assumes there are multiple sources but + * some of the supported endpoints (e.g. the nanoBTS) can only handle + * one source and this code will patch packages to appear as if there + * is only one source. + * There is also no probation period for new sources. Every package + * we receive will be seen as a switch in streams. + */ static void patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state, int payload, struct sockaddr_in *addr, char *data, int len) { - uint16_t seq; + uint32_t arrival_time; + int32_t transit, d; + uint16_t seq, udelta; uint32_t timestamp; struct rtp_hdr *rtp_hdr; @@ -113,15 +156,19 @@ static void patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *s rtp_hdr = (struct rtp_hdr *) data; seq = ntohs(rtp_hdr->sequence); timestamp = ntohl(rtp_hdr->timestamp); + arrival_time = get_current_ts(); if (!state->initialized) { - state->seq_no = seq - 1; + state->base_seq = seq; + state->max_seq = seq - 1; state->ssrc = state->orig_ssrc = rtp_hdr->ssrc; state->initialized = 1; state->last_timestamp = timestamp; + state->jitter = 0; + state->transit = arrival_time - timestamp; } else if (state->ssrc != rtp_hdr->ssrc) { state->ssrc = rtp_hdr->ssrc; - state->seq_offset = (state->seq_no + 1) - seq; + state->seq_offset = (state->max_seq + 1) - seq; state->timestamp_offset = state->last_timestamp - timestamp; #warning "Always allow to patch the SSRC" state->patch = 1; @@ -141,11 +188,35 @@ static void patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *s rtp_hdr->timestamp = htonl(timestamp); } - /* seq changed, now compare if we have lost something */ - if (state->seq_no + 1u != seq) - state->lost_no = abs(seq - (state->seq_no + 1)); - state->seq_no = seq; + /* + * The below takes the shape of the validation from Appendix A. Check + * if there is something weird with the sequence number, otherwise check + * for a wrap around in the sequence number. + */ + udelta = seq - state->max_seq; + if (udelta < RTP_MAX_DROPOUT) { + if (seq < state->max_seq) + state->cycles += RTP_SEQ_MOD; + } else if (udelta <= RTP_SEQ_MOD + RTP_MAX_MISORDER) { + LOGP(DMGCP, LOGL_NOTICE, + "RTP seqno made a very large jump on 0x%x delta: %u\n", + ENDPOINT_NUMBER(endp), udelta); + } + + /* + * calculate the jitter between the two packages. The TS should be + * taken closer to the read function. This was taken from the + * Appendix A of RFC 3550. The local timestamp has a usec resolution. + */ + transit = arrival_time - timestamp; + d = transit - state->transit; + state->transit = transit; + if (d < 0) + d = -d; + state->jitter += d - ((state->jitter + 8) >> 4); + + state->max_seq = seq; state->last_timestamp = timestamp; if (payload < 0) @@ -214,7 +285,7 @@ static int send_to(struct mgcp_endpoint *endp, int dest, int is_rtp, &endp->taps[MGCP_TAP_NET_OUT], buf, rc); return udp_send(endp->net_end.rtp.fd, &endp->net_end.addr, endp->net_end.rtp_port, buf, rc); - } else { + } else if (!tcfg->omit_rtcp) { return udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr, endp->net_end.rtcp_port, buf, rc); } @@ -227,15 +298,17 @@ static int send_to(struct mgcp_endpoint *endp, int dest, int is_rtp, &endp->taps[MGCP_TAP_BTS_OUT], buf, rc); return udp_send(endp->bts_end.rtp.fd, &endp->bts_end.addr, endp->bts_end.rtp_port, buf, rc); - } else { + } else if (!tcfg->omit_rtcp) { return udp_send(endp->bts_end.rtcp.fd, &endp->bts_end.addr, endp->bts_end.rtcp_port, buf, rc); } } + + return 0; } -static int recevice_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr, - char *buf, int bufsize) +static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr, + char *buf, int bufsize) { int rc; socklen_t slen = sizeof(*addr); @@ -266,14 +339,16 @@ static int rtp_data_net(struct osmo_fd *fd, unsigned int what) endp = (struct mgcp_endpoint *) fd->data; - rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf)); + rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); if (rc <= 0) return -1; if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) { LOGP(DMGCP, LOGL_ERROR, - "Data from wrong address %s on 0x%x\n", - inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp)); + "Endpoint 0x%x data from wrong address %s vs. ", + ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr)); + LOGPC(DMGCP, LOGL_ERROR, + "%s\n", inet_ntoa(endp->net_end.addr)); return -1; } @@ -294,6 +369,7 @@ static int rtp_data_net(struct osmo_fd *fd, unsigned int what) proto = fd == &endp->net_end.rtp ? PROTO_RTP : PROTO_RTCP; endp->net_end.packets += 1; + endp->net_end.octets += rc; forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc); if (endp->is_transcoded) @@ -338,7 +414,7 @@ static int rtp_data_bts(struct osmo_fd *fd, unsigned int what) endp = (struct mgcp_endpoint *) fd->data; - rc = recevice_from(endp, fd->fd, &addr, buf, sizeof(buf)); + rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf)); if (rc <= 0) return -1; @@ -372,6 +448,7 @@ static int rtp_data_bts(struct osmo_fd *fd, unsigned int what) /* do this before the loop handling */ endp->bts_end.packets += 1; + endp->bts_end.octets += rc; forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc); if (endp->is_transcoded) @@ -389,7 +466,7 @@ static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_ int rc, proto; cfg = _endp->cfg; - rc = recevice_from(_endp, fd->fd, &addr, buf, sizeof(buf)); + rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf)); if (rc <= 0) return -1; @@ -575,3 +652,38 @@ int mgcp_free_rtp_port(struct mgcp_rtp_end *end) return 0; } + + +void mgcp_state_calc_loss(struct mgcp_rtp_state *state, + struct mgcp_rtp_end *end, uint32_t *expected, + int *loss) +{ + *expected = state->cycles + state->max_seq; + *expected = *expected - state->base_seq + 1; + + if (!state->initialized) { + *expected = 0; + *loss = 0; + return; + } + + /* + * Make sure the sign is correct and use the biggest + * positive/negative number that fits. + */ + *loss = *expected - end->packets; + if (*expected < end->packets) { + if (*loss > 0) + *loss = INT_MIN; + } else { + if (*loss < 0) + *loss = INT_MAX; + } +} + +uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state) +{ + if (!state->initialized) + return 0; + return state->jitter >> 4; +} diff --git a/src/mgcp/mgcp_protocol.c b/src/mgcp/mgcp_protocol.c index 92559a3..f0f3c03 100644 --- a/src/mgcp/mgcp_protocol.c +++ b/src/mgcp/mgcp_protocol.c @@ -36,61 +36,41 @@ #include <mgcp/mgcp.h> #include <mgcp/mgcp_internal.h> -/** - * Macro for tokenizing MGCP messages and SDP in one go. - * - */ -#define MSG_TOKENIZE_START \ - line_start = 0; \ - for (i = 0; i < msgb_l3len(msg); ++i) { \ - /* we have a line end */ \ - if (msg->l3h[i] == '\n') { \ - /* skip the first line */ \ - if (line_start == 0) { \ - line_start = i + 1; \ - continue; \ - } \ - \ - /* check if we have a proper param */ \ - if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \ - } else if (i - line_start > 2 \ - && islower(msg->l3h[line_start]) \ - && msg->l3h[line_start + 1] == '=') { \ - } else if (i - line_start < 3 \ - || msg->l3h[line_start + 1] != ':' \ - || msg->l3h[line_start + 2] != ' ') \ - goto error; \ - \ - msg->l3h[i] = '\0'; \ - if (msg->l3h[i-1] == '\r') \ - msg->l3h[i-1] = '\0'; - -#define MSG_TOKENIZE_END \ - line_start = i + 1; \ - } \ - } +#define for_each_line(line, save) \ + for (line = strtok_r(NULL, "\r\n", &save); line;\ + line = strtok_r(NULL, "\r\n", &save)) static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end); +struct mgcp_parse_data { + struct mgcp_config *cfg; + struct mgcp_endpoint *endp; + char *trans; + char *save; + int found; +}; + struct mgcp_request { char *name; - struct msgb *(*handle_request) (struct mgcp_config *cfg, struct msgb *msg); + struct msgb *(*handle_request) (struct mgcp_parse_data *data); char *debug_name; }; #define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \ { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME }, -static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg); -static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg); -static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg); -static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg); -static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg); -static struct msgb *handle_noti_req(struct mgcp_config *cfg, struct msgb *msg); +static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data); +static struct msgb *handle_create_con(struct mgcp_parse_data *data); +static struct msgb *handle_delete_con(struct mgcp_parse_data *data); +static struct msgb *handle_modify_con(struct mgcp_parse_data *data); +static struct msgb *handle_rsip(struct mgcp_parse_data *data); +static struct msgb *handle_noti_req(struct mgcp_parse_data *data); static void create_transcoder(struct mgcp_endpoint *endp); static void delete_transcoder(struct mgcp_endpoint *endp); +static int mgcp_analyze_header(struct mgcp_parse_data *parse, char *data); + static uint32_t generate_call_id(struct mgcp_trunk_config *tcfg) { int i; @@ -137,9 +117,21 @@ static struct msgb *mgcp_msgb_alloc(void) return msg; } -struct msgb *mgcp_create_response_with_data(int code, const char *txt, - const char *msg, const char *trans, - const char *data) +static struct msgb *do_retransmission(const struct mgcp_endpoint *endp) +{ + struct msgb *msg = mgcp_msgb_alloc(); + if (!msg) + return NULL; + + msg->l2h = msgb_put(msg, strlen(endp->last_response)); + memcpy(msg->l2h, endp->last_response, msgb_l2len(msg)); + return msg; +} + +static struct msgb *create_resp(struct mgcp_endpoint *endp, int code, + const char *txt, const char *msg, + const char *trans, const char *param, + const char *sdp) { int len; struct msgb *res; @@ -148,25 +140,50 @@ struct msgb *mgcp_create_response_with_data(int code, const char *txt, if (!res) return NULL; - if (data) { - len = snprintf((char *) res->data, 2048, "%d %s%s\r\n%s", code, trans, txt, data); - } else { - len = snprintf((char *) res->data, 2048, "%d %s%s\r\n", code, trans, txt); + len = snprintf((char *) res->data, 2048, "%d %s%s%s\r\n%s", + code, trans, txt, param ? param : "", sdp ? sdp : ""); + if (len < 0) { + LOGP(DMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n"); + msgb_free(res); + return NULL; } res->l2h = msgb_put(res, len); - LOGP(DMGCP, LOGL_DEBUG, "Sending response: code: %d for '%s'\n", code, res->l2h); + LOGP(DMGCP, LOGL_DEBUG, "Generated response: code: %d for '%s'\n", code, res->l2h); + + /* + * Remember the last transmission per endpoint. + */ + if (endp) { + struct mgcp_trunk_config *tcfg = endp->tcfg; + talloc_free(endp->last_response); + talloc_free(endp->last_trans); + endp->last_trans = talloc_strdup(tcfg->endpoints, trans); + endp->last_response = talloc_strndup(tcfg->endpoints, + (const char *) res->l2h, + msgb_l2len(res)); + } + return res; } -static struct msgb *create_ok_response(int code, const char *msg, const char *trans) +static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp, + int code, const char *msg, + const char *trans, const char *param) { - return mgcp_create_response_with_data(code, " OK", msg, trans, NULL); + return create_resp(endp, code, " OK", msg, trans, param, NULL); } -static struct msgb *create_err_response(int code, const char *msg, const char *trans) +static struct msgb *create_ok_response(struct mgcp_endpoint *endp, + int code, const char *msg, const char *trans) { - return mgcp_create_response_with_data(code, " FAIL", msg, trans, NULL); + return create_ok_resp_with_param(endp, code, msg, trans, NULL); +} + +static struct msgb *create_err_response(struct mgcp_endpoint *endp, + int code, const char *msg, const char *trans) +{ + return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL); } static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, @@ -181,13 +198,15 @@ static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, snprintf(sdp_record, sizeof(sdp_record) - 1, "I: %u\n\n" "v=0\r\n" + "o=- %u 23 IN IP4 %s\r\n" "c=IN IP4 %s\r\n" + "t=0 0\r\n" "m=audio %d RTP/AVP %d\r\n" "a=rtpmap:%d %s\r\n", - endp->ci, addr, endp->net_end.local_port, - endp->bts_end.payload_type, endp->bts_end.payload_type, - endp->tcfg->audio_name); - return mgcp_create_response_with_data(200, " OK", msg, trans_id, sdp_record); + endp->ci, endp->ci, addr, addr, + endp->net_end.local_port, endp->bts_end.payload_type, + endp->bts_end.payload_type, endp->tcfg->audio_name); + return create_resp(endp, 200, " OK", msg, trans_id, NULL, sdp_record); } /* @@ -197,68 +216,51 @@ static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp, */ struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg) { - int code; + struct mgcp_parse_data pdata; + int i, code, handled = 0; struct msgb *resp = NULL; + char *data; if (msgb_l2len(msg) < 4) { - LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len); + LOGP(DMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len); return NULL; } /* attempt to treat it as a response */ if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) { LOGP(DMGCP, LOGL_DEBUG, "Response: Code: %d\n", code); - } else { - int i, handled = 0; - msg->l3h = &msg->l2h[4]; - for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) - if (strncmp(mgcp_requests[i].name, (const char *) &msg->l2h[0], 4) == 0) { - handled = 1; - resp = mgcp_requests[i].handle_request(cfg, msg); - break; - } - if (!handled) { - LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]); - } + return NULL; } - return resp; -} + msg->l3h = &msg->l2h[4]; -/* string tokenizer for the poor */ -static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length) -{ - int i, found = 0; - - int whitespace = 1; - for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) { - /* if we have a space we found an end */ - if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') { - if (!whitespace) { - ++found; - whitespace = 1; - ptrs->length = i - ptrs->start - 1; - ++ptrs; - --ptrs_length; - } else { - /* skip any number of whitespace */ - } - /* line end... stop */ - if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') - break; - } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') { - /* line end, be done */ + /* + * Check for a duplicate message and respond. + * FIXME: Verify that the msg->l3h is NULL terminated. + */ + memset(&pdata, 0, sizeof(pdata)); + pdata.cfg = cfg; + data = strtok_r((char *) msg->l3h, "\r\n", &pdata.save); + pdata.found = mgcp_analyze_header(&pdata, data); + if (pdata.endp && pdata.trans + && pdata.endp->last_trans + && strcmp(pdata.endp->last_trans, pdata.trans) == 0) { + return do_retransmission(pdata.endp); + } + + for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) { + if (strncmp(mgcp_requests[i].name, (const char *) &msg->l2h[0], 4) == 0) { + handled = 1; + resp = mgcp_requests[i].handle_request(&pdata); break; - } else if (whitespace) { - whitespace = 0; - ptrs->start = i; } } - if (ptrs_length == 0) - return -1; - return found; + if (!handled) + LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]); + + return resp; } /** @@ -355,48 +357,56 @@ static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char * return endp; } -int mgcp_analyze_header(struct mgcp_config *cfg, struct msgb *msg, - struct mgcp_msg_ptr *ptr, int size, - const char **transaction_id, struct mgcp_endpoint **endp) +/** + * @returns 0 when the status line was complete and transaction_id and + * endp out parameters are set. + */ +static int mgcp_analyze_header(struct mgcp_parse_data *pdata, char *data) { - int found; + int i = 0; + char *elem, *save; - *transaction_id = "000000"; + pdata->trans = "000000"; - if (size < 3) { - LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n"); - return -1; + for (elem = strtok_r(data, " ", &save); elem; + elem = strtok_r(NULL, " ", &save)) { + switch (i) { + case 0: + pdata->trans = elem; + break; + case 1: + pdata->endp = find_endpoint(pdata->cfg, elem); + if (!pdata->endp) { + LOGP(DMGCP, LOGL_ERROR, + "Unable to find Endpoint `%s'\n", elem); + return -1; + } + break; + case 2: + if (strcmp("MGCP", elem)) { + LOGP(DMGCP, LOGL_ERROR, + "MGCP header parsing error\n"); + return -1; + } + break; + case 3: + if (strcmp("1.0", elem)) { + LOGP(DMGCP, LOGL_ERROR, "MGCP version `%s' " + "not supported\n", elem); + return -1; + } + break; + } + i++; } - found = find_msg_pointers(msg, ptr, size); - - if (found <= 3) { - LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found); + if (i != 4) { + LOGP(DMGCP, LOGL_ERROR, "MGCP status line too short.\n"); + pdata->trans = "000000"; + pdata->endp = NULL; return -1; } - /* - * replace the space with \0. the main method gurantess that - * we still have + 1 for null termination - */ - msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0'; - msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0'; - msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0'; - msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0'; - - if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0 - || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) { - LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n", - (const char *)&msg->l3h[ptr[3].start], - (const char *)&msg->l3h[ptr[2].start]); - return -1; - } - - *transaction_id = (const char *)&msg->l3h[ptr[0].start]; - if (endp) { - *endp = find_endpoint(cfg, (const char *)&msg->l3h[ptr[1].start]); - return *endp == NULL; - } return 0; } @@ -426,18 +436,12 @@ static int verify_ci(const struct mgcp_endpoint *endp, return 0; } -static struct msgb *handle_audit_endpoint(struct mgcp_config *cfg, struct msgb *msg) +static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p) { - struct mgcp_msg_ptr data_ptrs[6]; - int found; - const char *trans_id; - struct mgcp_endpoint *endp; - - found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); - if (found != 0) - return create_err_response(500, "AUEP", trans_id); + if (p->found != 0) + return create_err_response(NULL, 500, "AUEP", p->trans); else - return create_ok_response(200, "AUEP", trans_id); + return create_ok_response(p->endp, 200, "AUEP", p->trans); } static int parse_conn_mode(const char *msg, int *conn_mode) @@ -529,62 +533,72 @@ static int allocate_ports(struct mgcp_endpoint *endp) return 0; } -static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg) +static struct msgb *handle_create_con(struct mgcp_parse_data *p) { - struct mgcp_msg_ptr data_ptrs[6]; - int found, i, line_start; - const char *trans_id; struct mgcp_trunk_config *tcfg; - struct mgcp_endpoint *endp; + struct mgcp_endpoint *endp = p->endp; int error_code = 400; - found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); - if (found != 0) - return create_err_response(510, "CRCX", trans_id); + const char *local_options = NULL; + const char *callid = NULL; + const char *mode = NULL; + char *line; - tcfg = endp->tcfg; + if (p->found != 0) + return create_err_response(NULL, 510, "CRCX", p->trans); + + /* parse CallID C: and LocalParameters L: */ + for_each_line(line, p->save) { + switch (line[0]) { + case 'L': + local_options = (const char *) line + 3; + break; + case 'C': + callid = (const char *) line + 3; + break; + case 'M': + mode = (const char *) line + 3; + break; + default: + LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", + *line, *line, ENDPOINT_NUMBER(endp)); + break; + } + } + + tcfg = p->endp->tcfg; + + /* Check required data */ + if (!callid || !mode) { + LOGP(DMGCP, LOGL_ERROR, "Missing callid and mode in CRCX on 0x%x\n", + ENDPOINT_NUMBER(endp)); + return create_err_response(endp, 400, "CRCX", p->trans); + } if (endp->allocated) { if (tcfg->force_realloc) { LOGP(DMGCP, LOGL_NOTICE, "Endpoint 0x%x already allocated. Forcing realloc.\n", ENDPOINT_NUMBER(endp)); mgcp_free_endp(endp); - if (cfg->realloc_cb) - cfg->realloc_cb(tcfg, ENDPOINT_NUMBER(endp)); + if (p->cfg->realloc_cb) + p->cfg->realloc_cb(tcfg, ENDPOINT_NUMBER(endp)); } else { LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp)); - return create_err_response(400, "CRCX", trans_id); + return create_err_response(endp, 400, "CRCX", p->trans); } } - /* parse CallID C: and LocalParameters L: */ - MSG_TOKENIZE_START - switch (msg->l3h[line_start]) { - case 'L': - endp->local_options = talloc_strdup(tcfg->endpoints, - (const char *)&msg->l3h[line_start + 3]); - break; - case 'C': - endp->callid = talloc_strdup(tcfg->endpoints, - (const char *)&msg->l3h[line_start + 3]); - break; - case 'M': - if (parse_conn_mode((const char *)&msg->l3h[line_start + 3], - &endp->conn_mode) != 0) { + /* copy some parameters */ + endp->callid = talloc_strdup(tcfg->endpoints, callid); + + if (local_options) + endp->local_options = talloc_strdup(tcfg->endpoints, local_options); + + if (parse_conn_mode(mode, &endp->conn_mode) != 0) { error_code = 517; goto error2; - } - - endp->orig_mode = endp->conn_mode; - break; - default: - LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", - msg->l3h[line_start], msg->l3h[line_start], - ENDPOINT_NUMBER(endp)); - break; } - MSG_TOKENIZE_END /* initialize */ endp->net_end.rtp_port = endp->net_end.rtcp_port = endp->bts_end.rtp_port = endp->bts_end.rtcp_port = 0; @@ -605,13 +619,16 @@ static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg) endp->bts_end.payload_type = tcfg->audio_payload; /* policy CB */ - if (cfg->policy_cb) { - switch (cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, trans_id)) { + if (p->cfg->policy_cb) { + int rc; + rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp), + MGCP_ENDP_CRCX, p->trans); + switch (rc) { case MGCP_POLICY_REJECT: LOGP(DMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n", ENDPOINT_NUMBER(endp)); mgcp_free_endp(endp); - return create_err_response(400, "CRCX", trans_id); + return create_err_response(endp, 400, "CRCX", p->trans); break; case MGCP_POLICY_DEFER: /* stop processing */ @@ -627,115 +644,106 @@ static struct msgb *handle_create_con(struct mgcp_config *cfg, struct msgb *msg) LOGP(DMGCP, LOGL_DEBUG, "Creating endpoint on: 0x%x CI: %u port: %u/%u\n", ENDPOINT_NUMBER(endp), endp->ci, endp->net_end.local_port, endp->bts_end.local_port); - if (cfg->change_cb) - cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX); + if (p->cfg->change_cb) + p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX); create_transcoder(endp); - return create_response_with_sdp(endp, "CRCX", trans_id); -error: - LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n", - osmo_hexdump(msg->l3h, msgb_l3len(msg)), - ENDPOINT_NUMBER(endp), line_start, i); - return create_err_response(error_code, "CRCX", trans_id); - + return create_response_with_sdp(endp, "CRCX", p->trans); error2: mgcp_free_endp(endp); LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp)); - return create_err_response(error_code, "CRCX", trans_id); + return create_err_response(endp, error_code, "CRCX", p->trans); } -static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg) +static struct msgb *handle_modify_con(struct mgcp_parse_data *p) { - struct mgcp_msg_ptr data_ptrs[6]; - int found, i, line_start; - const char *trans_id; - struct mgcp_endpoint *endp; + struct mgcp_endpoint *endp = p->endp; int error_code = 500; int silent = 0; + char *line; - found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); - if (found != 0) - return create_err_response(510, "MDCX", trans_id); + if (p->found != 0) + return create_err_response(NULL, 510, "MDCX", p->trans); if (endp->ci == CI_UNUSED) { - LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp)); - return create_err_response(400, "MDCX", trans_id); + LOGP(DMGCP, LOGL_ERROR, "Endpoint is not " + "holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp)); + return create_err_response(endp, 400, "MDCX", p->trans); } - MSG_TOKENIZE_START - switch (msg->l3h[line_start]) { - case 'C': { - if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0) - goto error3; - break; - } - case 'I': { - if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0) - goto error3; - break; - } - case 'L': - /* skip */ - break; - case 'M': - if (parse_conn_mode((const char *)&msg->l3h[line_start + 3], - &endp->conn_mode) != 0) { - error_code = 517; - goto error3; + for_each_line(line, p->save) { + switch (line[0]) { + case 'C': { + if (verify_call_id(endp, line + 3) != 0) + goto error3; + break; } - endp->orig_mode = endp->conn_mode; - break; - case 'Z': - silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0; - break; - case '\0': - /* SDP file begins */ - break; - case 'a': - case 'o': - case 's': - case 't': - case 'v': - /* skip these SDP attributes */ - break; - case 'm': { - int port; - int payload; - const char *param = (const char *)&msg->l3h[line_start]; - - if (sscanf(param, "m=audio %d RTP/AVP %d", &port, &payload) == 2) { - endp->net_end.rtp_port = htons(port); - endp->net_end.rtcp_port = htons(port + 1); - endp->net_end.payload_type = payload; + case 'I': { + if (verify_ci(endp, line + 3) != 0) + goto error3; + break; } - break; - } - case 'c': { - char ipv4[16]; - const char *param = (const char *)&msg->l3h[line_start]; + case 'L': + /* skip */ + break; + case 'M': + if (parse_conn_mode(line + 3, &endp->conn_mode) != 0) { + error_code = 517; + goto error3; + } + endp->orig_mode = endp->conn_mode; + break; + case 'Z': + silent = strcmp("noanswer", line + 3) == 0; + break; + case '\0': + /* SDP file begins */ + break; + case 'a': + case 'o': + case 's': + case 't': + case 'v': + /* skip these SDP attributes */ + break; + case 'm': { + int port; + int payload; + + if (sscanf(line, "m=audio %d RTP/AVP %d", &port, &payload) == 2) { + endp->net_end.rtp_port = htons(port); + endp->net_end.rtcp_port = htons(port + 1); + endp->net_end.payload_type = payload; + } + break; + } + case 'c': { + char ipv4[16]; - if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) { - inet_aton(ipv4, &endp->net_end.addr); + if (sscanf(line, "c=IN IP4 %15s", ipv4) == 1) { + inet_aton(ipv4, &endp->net_end.addr); + } + break; + } + default: + LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", + line[0], line[0], ENDPOINT_NUMBER(endp)); + break; } - break; - } - default: - LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", - msg->l3h[line_start], msg->l3h[line_start], - ENDPOINT_NUMBER(endp)); - break; } - MSG_TOKENIZE_END /* policy CB */ - if (cfg->policy_cb) { - switch (cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, trans_id)) { + if (p->cfg->policy_cb) { + int rc; + rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), + MGCP_ENDP_MDCX, p->trans); + switch (rc) { case MGCP_POLICY_REJECT: LOGP(DMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n", ENDPOINT_NUMBER(endp)); if (silent) goto out_silent; - return create_err_response(400, "MDCX", trans_id); + return create_err_response(endp, 400, "MDCX", p->trans); break; case MGCP_POLICY_DEFER: /* stop processing */ @@ -750,77 +758,70 @@ static struct msgb *handle_modify_con(struct mgcp_config *cfg, struct msgb *msg) /* modify */ LOGP(DMGCP, LOGL_DEBUG, "Modified endpoint on: 0x%x Server: %s:%u\n", ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port)); - if (cfg->change_cb) - cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX); + if (p->cfg->change_cb) + p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX); if (silent) goto out_silent; - return create_response_with_sdp(endp, "MDCX", trans_id); - -error: - LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n", - osmo_hexdump(msg->l3h, msgb_l3len(msg)), - ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]); - return create_err_response(error_code, "MDCX", trans_id); + return create_response_with_sdp(endp, "MDCX", p->trans); error3: - return create_err_response(error_code, "MDCX", trans_id); + return create_err_response(endp, error_code, "MDCX", p->trans); out_silent: return NULL; } -static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg) +static struct msgb *handle_delete_con(struct mgcp_parse_data *p) { - struct mgcp_msg_ptr data_ptrs[6]; - int found, i, line_start; - const char *trans_id; - struct mgcp_endpoint *endp; + struct mgcp_endpoint *endp = p->endp; int error_code = 400; int silent = 0; + char *line; + char stats[1048]; - found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); - if (found != 0) - return create_err_response(error_code, "DLCX", trans_id); + if (p->found != 0) + return create_err_response(NULL, error_code, "DLCX", p->trans); - if (!endp->allocated) { - LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp)); - return create_err_response(400, "DLCX", trans_id); + if (!p->endp->allocated) { + LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", + ENDPOINT_NUMBER(endp)); + return create_err_response(endp, 400, "DLCX", p->trans); } - MSG_TOKENIZE_START - switch (msg->l3h[line_start]) { - case 'C': { - if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0) - goto error3; - break; - } - case 'I': { - if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0) - goto error3; - break; - case 'Z': - silent = strcmp("noanswer", (const char *)&msg->l3h[line_start + 3]) == 0; - break; - } - default: - LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", - msg->l3h[line_start], msg->l3h[line_start], - ENDPOINT_NUMBER(endp)); - break; + for_each_line(line, p->save) { + switch (line[0]) { + case 'C': + if (verify_call_id(endp, line + 3) != 0) + goto error3; + break; + case 'I': + if (verify_ci(endp, line + 3) != 0) + goto error3; + break; + case 'Z': + silent = strcmp("noanswer", line + 3) == 0; + break; + default: + LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n", + line[0], line[0], ENDPOINT_NUMBER(endp)); + break; + } } - MSG_TOKENIZE_END /* policy CB */ - if (cfg->policy_cb) { - switch (cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, trans_id)) { + if (p->cfg->policy_cb) { + int rc; + rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp), + MGCP_ENDP_DLCX, p->trans); + switch (rc) { case MGCP_POLICY_REJECT: LOGP(DMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n", ENDPOINT_NUMBER(endp)); if (silent) goto out_silent; - return create_err_response(400, "DLCX", trans_id); + return create_err_response(endp, 400, "DLCX", p->trans); break; case MGCP_POLICY_DEFER: /* stop processing */ @@ -837,45 +838,34 @@ static struct msgb *handle_delete_con(struct mgcp_config *cfg, struct msgb *msg) LOGP(DMGCP, LOGL_DEBUG, "Deleted endpoint on: 0x%x Server: %s:%u\n", ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port)); + /* save the statistics of the current call */ + mgcp_format_stats(endp, stats, sizeof(stats)); + delete_transcoder(endp); mgcp_free_endp(endp); - if (cfg->change_cb) - cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX); + if (p->cfg->change_cb) + p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX); if (silent) goto out_silent; - return create_ok_response(250, "DLCX", trans_id); - -error: - LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n", - osmo_hexdump(msg->l3h, msgb_l3len(msg)), - ENDPOINT_NUMBER(endp), line_start, i); - return create_err_response(error_code, "DLCX", trans_id); + return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats); error3: - return create_err_response(error_code, "DLCX", trans_id); + return create_err_response(endp, error_code, "DLCX", p->trans); out_silent: return NULL; } -static struct msgb *handle_rsip(struct mgcp_config *cfg, struct msgb *msg) +static struct msgb *handle_rsip(struct mgcp_parse_data *p) { - struct mgcp_msg_ptr data_ptrs[6]; - const char *trans_id; - struct mgcp_endpoint *endp; - int found; - - found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), - &trans_id, &endp); - if (found != 0) { + if (p->found != 0) { LOGP(DMGCP, LOGL_ERROR, "Failed to find the endpoint.\n"); return NULL; } - if (cfg->reset_cb) - cfg->reset_cb(endp->tcfg); - + if (p->cfg->reset_cb) + p->cfg->reset_cb(p->endp->tcfg); return NULL; } @@ -893,53 +883,40 @@ static char extract_tone(const char *line) * can also request when the notification should be send and such. We don't * do this right now. */ -static struct msgb *handle_noti_req(struct mgcp_config *cfg, struct msgb *msg) +static struct msgb *handle_noti_req(struct mgcp_parse_data *p) { - struct mgcp_msg_ptr data_ptrs[6]; - const char *trans_id; - struct mgcp_endpoint *endp; - int found, res = 0, i, line_start; + int res = 0; + char *line; char tone = 0; - found = mgcp_analyze_header(cfg, msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp); - if (found != 0) - return create_err_response(400, "RQNT", trans_id); + if (p->found != 0) + return create_err_response(NULL, 400, "RQNT", p->trans); - if (!endp->allocated) { - LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp)); - return create_err_response(400, "RQNT", trans_id); - } - - MSG_TOKENIZE_START - switch (msg->l3h[line_start]) { - case 'S': - tone = extract_tone((const char *)&msg->l3h[line_start]); - break; + for_each_line(line, p->save) { + switch (line[0]) { + case 'S': + tone = extract_tone(line); + break; + } } - MSG_TOKENIZE_END /* we didn't see a signal request with a tone */ if (tone == CHAR_MAX) - return create_ok_response(200, "RQNT", trans_id); + return create_ok_response(p->endp, 200, "RQNT", p->trans); - if (cfg->rqnt_cb) - res = cfg->rqnt_cb(endp, tone, (const char *) msg->l3h); + if (p->cfg->rqnt_cb) + res = p->cfg->rqnt_cb(p->endp, tone); return res == 0 ? - create_ok_response(200, "RQNT", trans_id) : - create_err_response(res, "RQNT", trans_id); - -error: - LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n", - osmo_hexdump(msg->l3h, msgb_l3len(msg)), - ENDPOINT_NUMBER(endp), line_start, i); - return create_err_response(400, "RQNT", trans_id); + create_ok_response(p->endp, 200, "RQNT", p->trans) : + create_err_response(p->endp, res, "RQNT", p->trans); } static void trunk_init(struct mgcp_trunk_config *trunk) { trunk->audio_name = talloc_strdup(trunk->cfg, "AMR/8000"); trunk->audio_payload = 126; + trunk->omit_rtcp = 0; /* MGW handling */ trunk->target_trunk_start = 1; @@ -1063,6 +1040,7 @@ static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end) } end->packets = 0; + end->octets = 0; memset(&end->addr, 0, sizeof(end->addr)); end->rtp_port = end->rtcp_port = 0; end->payload_type = -1; @@ -1111,15 +1089,11 @@ void mgcp_free_endp(struct mgcp_endpoint *endp) endp->ci = CI_UNUSED; endp->allocated = 0; - if (endp->callid) { - talloc_free(endp->callid); - endp->callid = NULL; - } + talloc_free(endp->callid); + endp->callid = NULL; - if (endp->local_options) { - talloc_free(endp->local_options); - endp->local_options = NULL; - } + talloc_free(endp->local_options); + endp->local_options = NULL; mgcp_rtp_end_reset(&endp->bts_end); mgcp_rtp_end_reset(&endp->net_end); @@ -1193,6 +1167,36 @@ static void send_dlcx(struct mgcp_endpoint *endp, int endpoint) send_trans(endp->cfg, buf, len); } +static int send_agent(struct mgcp_config *cfg, const char *buf, int len) +{ + return write(cfg->gw_fd.bfd.fd, buf, len); +} + +int mgcp_send_reset_all(struct mgcp_config *cfg) +{ + static const char mgcp_reset[] = { + "RSIP 1 *@mgw MGCP 1.0\r\n" + }; + + return send_agent(cfg, mgcp_reset, sizeof mgcp_reset -1); +} + +int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint) +{ + char buf[128]; + int len; + + len = snprintf(buf, sizeof(buf), + "RSIP 39 %x@mgw MGCP 1.0\r\n" + , endpoint); + if (len < 0) + return len; + + buf[sizeof(buf) - 1] = '\0'; + + return send_agent(endp->cfg, buf, len); +} + static void create_transcoder(struct mgcp_endpoint *endp) { int port; @@ -1239,3 +1243,18 @@ int mgcp_reset_transcoder(struct mgcp_config *cfg) return send_trans(cfg, mgcp_reset, sizeof mgcp_reset -1); } + +void mgcp_format_stats(struct mgcp_endpoint *endp, char *msg, size_t size) +{ + uint32_t expected, jitter; + int ploss; + mgcp_state_calc_loss(&endp->net_state, &endp->net_end, + &expected, &ploss); + jitter = mgcp_state_calc_jitter(&endp->net_state); + + snprintf(msg, size, "\r\nP: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%d", + endp->bts_end.packets, endp->bts_end.octets, + endp->net_end.packets, endp->net_end.octets, + ploss, jitter); + msg[size - 1] = '\0'; +} diff --git a/src/mgcp/mgcp_vty.c b/src/mgcp/mgcp_vty.c index 2c791de..5a2362a 100644 --- a/src/mgcp/mgcp_vty.c +++ b/src/mgcp/mgcp_vty.c @@ -88,6 +88,10 @@ static void bsc_replace_string(void *ctx, char **dst, const char *newstr) *dst = talloc_strdup(ctx, newstr); } + + +#define RTCP_OMIT_STR "Drop RTCP packets in both directions\n" + struct mgcp_config *g_cfg = NULL; static struct mgcp_trunk_config *find_trunk(struct mgcp_config *cfg, @@ -108,19 +112,19 @@ static struct mgcp_trunk_config *find_trunk(struct mgcp_config *cfg, */ struct cmd_node mgcp_node = { MGCP_NODE, - "%s(mgcp)#", + "%s(config-mgcp)# ", 1, }; struct cmd_node vtrunk_node = { VTRUNK_NODE, - "%s(vtrunk)#", + "%s(config-mgcp-vtrunk)#", 1, }; struct cmd_node trunk_node = { TRUNK_NODE, - "%s(trunk)#", + "%s(config-mgcp-trunk)# ", 1, }; @@ -148,7 +152,7 @@ static int config_write_mgcp(struct vty *vty) vty_out(vty, " rtp ip-dscp %d%s", g_cfg->endp_dscp, VTY_NEWLINE); if (g_cfg->call_agent_addr) - vty_out(vty, " call agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE); + vty_out(vty, " call-agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE); if (g_cfg->transcoder_ip) vty_out(vty, " transcoder-mgw %s%s", g_cfg->transcoder_ip, VTY_NEWLINE); @@ -191,13 +195,12 @@ static void dump_trunk(struct vty *vty, struct mgcp_trunk_config *cfg) vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u on %s " - "traffic received bts: %u/%u remote: %u/%u transcoder: %u/%u%s", + "traffic received bts: %u remote: %u transcoder: %u/%u%s", i, endp->ci, ntohs(endp->net_end.rtp_port), ntohs(endp->net_end.rtcp_port), ntohs(endp->bts_end.rtp_port), ntohs(endp->bts_end.rtcp_port), inet_ntoa(endp->bts_end.addr), - endp->bts_end.packets, endp->bts_state.lost_no, - endp->net_end.packets, endp->net_state.lost_no, + endp->bts_end.packets, endp->net_end.packets, endp->trans_net.packets, endp->trans_bts.packets, VTY_NEWLINE); } @@ -228,8 +231,10 @@ DEFUN(cfg_mgcp, DEFUN(cfg_mgcp_local_ip, cfg_mgcp_local_ip_cmd, - "local ip IP", - "Set the IP to be used in SDP records") + "local ip A.B.C.D", + "Local options for the SDP record\n" + IP_STR + "IPv4 Address to use in SDP record\n") { struct hostent *hosts; struct in_addr *addr; @@ -247,8 +252,10 @@ DEFUN(cfg_mgcp_local_ip, DEFUN(cfg_mgcp_mgw_ip, cfg_mgcp_mgw_ip_cmd, - "mgw ip IP", - "Set the IP of the MGW for RTP forwarding") + "mgw ip A.B.C.D", + "BTS Audio source/destination options\n" + IP_STR + "IPv4 Address of the BTS\n") { struct hostent *hosts; struct in_addr *addr; @@ -265,10 +272,13 @@ DEFUN(cfg_mgcp_mgw_ip, return CMD_SUCCESS; } +#define BIND_STR "Listen/Bind related socket option\n" DEFUN(cfg_mgcp_bind_ip, cfg_mgcp_bind_ip_cmd, "bind ip A.B.C.D", - "Bind the MGCP to this local addr") + BIND_STR + IP_STR + "IPv4 Address to bind to\n") { bsc_replace_string(g_cfg, &g_cfg->source_addr, argv[0]); return CMD_SUCCESS; @@ -277,7 +287,9 @@ DEFUN(cfg_mgcp_bind_ip, DEFUN(cfg_mgcp_bind_port, cfg_mgcp_bind_port_cmd, "bind port <0-65534>", - "Bind the MGCP to this port") + BIND_STR + "Port information\n" + "UDP port to listen for MGCP messages\n") { unsigned int port = atoi(argv[0]); g_cfg->source_port = port; @@ -287,7 +299,9 @@ DEFUN(cfg_mgcp_bind_port, DEFUN(cfg_mgcp_bind_early, cfg_mgcp_bind_early_cmd, "bind early (0|1)", - "Bind all RTP ports early") + BIND_STR + "Bind local ports on start up\n" + "Bind on demand\n" "Bind on startup\n") { vty_out(vty, "bind early is deprecated, remove it from the config.\n"); return CMD_WARNING; @@ -309,20 +323,28 @@ static void parse_range(struct mgcp_port_range *range, const char **argv) } +#define RTP_STR "RTP configuration\n" +#define BTS_START_STR "First UDP port allocated for the BTS side\n" +#define NET_START_STR "First UDP port allocated for the NET side\n" +#define UDP_PORT_STR "UDP Port number\n" DEFUN(cfg_mgcp_rtp_bts_base_port, cfg_mgcp_rtp_bts_base_port_cmd, "rtp bts-base <0-65534>", - "Base port to use") + RTP_STR + BTS_START_STR + UDP_PORT_STR) { parse_base(&g_cfg->bts_ports, argv); return CMD_SUCCESS; } +#define RANGE_START_STR "Start of the range of ports\n" +#define RANGE_END_STR "End of the range of ports\n" DEFUN(cfg_mgcp_rtp_bts_range, cfg_mgcp_rtp_bts_range_cmd, "rtp bts-range <0-65534> <0-65534>", - "Range of ports to allocate for endpoints\n" - "Start of the range of ports\n" "End of the range of ports\n") + RTP_STR "Range of ports to use for the BTS side\n" + RANGE_START_STR RANGE_END_STR) { parse_range(&g_cfg->bts_ports, argv); return CMD_SUCCESS; @@ -331,8 +353,8 @@ DEFUN(cfg_mgcp_rtp_bts_range, DEFUN(cfg_mgcp_rtp_net_range, cfg_mgcp_rtp_net_range_cmd, "rtp net-range <0-65534> <0-65534>", - "Range of ports to allocate for endpoints\n" - "Start of the range of ports\n" "End of the range of ports\n") + RTP_STR "Range of ports to use for the NET side\n" + RANGE_START_STR RANGE_END_STR) { parse_range(&g_cfg->net_ports, argv); return CMD_SUCCESS; @@ -341,20 +363,21 @@ DEFUN(cfg_mgcp_rtp_net_range, DEFUN(cfg_mgcp_rtp_net_base_port, cfg_mgcp_rtp_net_base_port_cmd, "rtp net-base <0-65534>", - "Base port to use for network port\n" "Port\n") + RTP_STR NET_START_STR UDP_PORT_STR) { parse_base(&g_cfg->net_ports, argv); return CMD_SUCCESS; } ALIAS_DEPRECATED(cfg_mgcp_rtp_bts_base_port, cfg_mgcp_rtp_base_port_cmd, - "rtp base <0-65534>", "Base port to use") + "rtp base <0-65534>", + RTP_STR BTS_START_STR UDP_PORT_STR) DEFUN(cfg_mgcp_rtp_transcoder_range, cfg_mgcp_rtp_transcoder_range_cmd, "rtp transcoder-range <0-65534> <0-65534>", - "Range of ports to allocate for the transcoder\n" - "Start of the range of ports\n" "End of the range of ports\n") + RTP_STR "Range of ports to use for the Transcoder\n" + RANGE_START_STR RANGE_END_STR) { parse_range(&g_cfg->transcoder_ports, argv); return CMD_SUCCESS; @@ -363,7 +386,8 @@ DEFUN(cfg_mgcp_rtp_transcoder_range, DEFUN(cfg_mgcp_rtp_transcoder_base, cfg_mgcp_rtp_transcoder_base_cmd, "rtp transcoder-base <0-65534>", - "Base port for the transcoder range\n" "Port\n") + RTP_STR "First UDP port allocated for the Transcoder side\n" + UDP_PORT_STR) { parse_base(&g_cfg->transcoder_ports, argv); return CMD_SUCCESS; @@ -372,7 +396,8 @@ DEFUN(cfg_mgcp_rtp_transcoder_base, DEFUN(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_dscp_cmd, "rtp ip-dscp <0-255>", - "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.") + RTP_STR + "Apply IP_TOS to the audio stream\n" "The DSCP value\n") { int dscp = atoi(argv[0]); g_cfg->endp_dscp = dscp; @@ -381,9 +406,13 @@ DEFUN(cfg_mgcp_rtp_ip_dscp, ALIAS_DEPRECATED(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_tos_cmd, "rtp ip-tos <0-255>", - "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.") + RTP_STR + "Apply IP_TOS to the audio stream\n" "The DSCP value\n") +#define SDP_STR "SDP File related options\n" +#define AUDIO_STR "Audio payload options\n" + DEFUN(cfg_vtrunk_number_endp, cfg_vtrunk_number_endp_cmd, "number endpoints <0-65534>", @@ -406,15 +435,24 @@ DEFUN(cfg_vtrunk_number_endp, return CMD_SUCCESS; } + +#define CALL_AGENT_STR "Callagent information\n" DEFUN(cfg_mgcp_agent_addr, cfg_mgcp_agent_addr_cmd, - "call agent ip IP", - "Set the address of the call agent.") + "call-agent ip A.B.C.D", + CALL_AGENT_STR IP_STR + "IPv4 Address of the callagent\n") { bsc_replace_string(g_cfg, &g_cfg->call_agent_addr, argv[0]); return CMD_SUCCESS; } +ALIAS_DEPRECATED(cfg_mgcp_agent_addr, cfg_mgcp_agent_addr_cmd_old, + "call agent ip A.B.C.D", + CALL_AGENT_STR CALL_AGENT_STR IP_STR + "IPv4 Address of the callagent\n") + + DEFUN(cfg_mgcp_transcoder, cfg_mgcp_transcoder_cmd, "transcoder-mgw A.B.C.D", @@ -429,8 +467,8 @@ DEFUN(cfg_mgcp_transcoder, DEFUN(cfg_mgcp_no_transcoder, cfg_mgcp_no_transcoder_cmd, - NO_STR "transcoder-mgw", - "Disable the transcoding\n") + "no transcoder-mgw", + NO_STR "Disable the transcoding\n") { if (g_cfg->transcoder_ip) { LOGP(DMGCP, LOGL_NOTICE, "Disabling transcoding on future calls.\n"); @@ -509,6 +547,10 @@ static void config_write_trunk_common(struct vty *vty, tcfg->audio_name, VTY_NEWLINE); vty_out(vty, " loop %d%s", tcfg->audio_loop, VTY_NEWLINE); + if (tcfg->omit_rtcp) + vty_out(vty, " rtcp-omit%s", VTY_NEWLINE); + else + vty_out(vty, " no rtcp-omit%s", VTY_NEWLINE); } static int config_write_trunk(struct vty *vty) @@ -541,8 +583,8 @@ static int config_write_vtrunk(struct vty *vty) DEFUN(cfg_trunk_payload_number, cfg_trunk_payload_number_cmd, - "sdp audio payload number <1-255>", - "SDP related\n" "Audio\n" "Payload\n" "Payload Number\n") + "sdp audio-payload number <1-255>", + SDP_STR AUDIO_STR "Number\n" "Payload Number\n") { struct mgcp_trunk_config *trunk = vty->index; unsigned int payload = atoi(argv[0]); @@ -551,10 +593,14 @@ DEFUN(cfg_trunk_payload_number, return CMD_SUCCESS; } +ALIAS_DEPRECATED(cfg_trunk_payload_number, cfg_trunk_payload_number_cmd_old, + "sdp audio payload number <1-255>", + SDP_STR AUDIO_STR AUDIO_STR "Number\n" "Payload Number\n") + DEFUN(cfg_trunk_payload_name, cfg_trunk_payload_name_cmd, - "sdp audio payload name NAME", - "SDP related\n" "Audio\n" "Payload\n" "Payload Name\n") + "sdp audio-payload name NAME", + SDP_STR AUDIO_STR "Payload\n" "Payload Name\n") { struct mgcp_trunk_config *trunk = vty->index; @@ -562,10 +608,16 @@ DEFUN(cfg_trunk_payload_name, return CMD_SUCCESS; } +ALIAS_DEPRECATED(cfg_trunk_payload_name, cfg_trunk_payload_name_cmd_old, + "sdp audio payload name NAME", + SDP_STR AUDIO_STR AUDIO_STR "Payload\n" "Payload Name\n") + + DEFUN(cfg_trunk_loop, cfg_trunk_loop_cmd, "loop (0|1)", - "Loop the audio") + "Loop audio for all endpoints on this trunk\n" + "Don't Loop\n" "Loop\n") { struct mgcp_trunk_config *trunk = vty->index; @@ -573,6 +625,26 @@ DEFUN(cfg_trunk_loop, return CMD_SUCCESS; } +DEFUN(cfg_trunk_omit_rtcp, + cfg_trunk_omit_rtcp_cmd, + "rtcp-omit", + RTCP_OMIT_STR) +{ + struct mgcp_trunk_config *trunk = vty->index; + trunk->omit_rtcp = 1; + return CMD_SUCCESS; +} + +DEFUN(cfg_trunk_no_omit_rtcp, + cfg_trunk_no_omit_rtcp_cmd, + "no rtcp-omit", + NO_STR RTCP_OMIT_STR) +{ + struct mgcp_trunk_config *trunk = vty->index; + trunk->omit_rtcp = 0; + return CMD_SUCCESS; +} + #define TRUNK_TYPE_STR "Virtual trunk\nE1 trunk\n" #define TRUNK_IDENT_STR "Trunk identifier depending on the type\n" @@ -716,12 +788,70 @@ DEFUN(free_endp, free_endp_cmd, return CMD_SUCCESS; } +DEFUN(reset_endp, reset_endp_cmd, + "reset-endpoint (virtual|e1) IDENT NUMBER", + "Reset the given endpoint\n" + TRUNK_TYPE_STR + TRUNK_IDENT_STR + "Endpoint number in hex.\n") +{ + struct mgcp_trunk_config *trunk; + struct mgcp_endpoint *endp; + int endp_no, rc; + + trunk = find_trunk(g_cfg, argv[0], argv[1]); + if (!trunk) { + vty_out(vty, "%%Trunk %d not found in the config.%s", + atoi(argv[0]), VTY_NEWLINE); + return CMD_WARNING; + } + + if (!trunk->endpoints) { + vty_out(vty, "%%Trunk %d has no endpoints allocated.%s", + trunk->trunk_nr, VTY_NEWLINE); + return CMD_WARNING; + } + + endp_no = strtoul(argv[2], NULL, 16); + if (endp_no < 1 || endp_no >= trunk->number_endpoints) { + vty_out(vty, "Endpoint number %s/%d is invalid.%s", + argv[1], endp_no, VTY_NEWLINE); + return CMD_WARNING; + } + + endp = &trunk->endpoints[endp_no]; + rc = mgcp_send_reset_ep(endp, ENDPOINT_NUMBER(endp)); + if (rc < 0) { + vty_out(vty, "Error %d sending reset.%s", rc, VTY_NEWLINE); + return CMD_WARNING; + } + return CMD_SUCCESS; +} + +DEFUN(reset_all_endp, reset_all_endp_cmd, + "reset-all-endpoints", + "Reset all endpoints\n") +{ + int rc; + + rc = mgcp_send_reset_all(g_cfg); + if (rc < 0) { + vty_out(vty, "Error %d during endpoint reset.%s", + rc, VTY_NEWLINE); + return CMD_WARNING; + } + return CMD_SUCCESS; +} + + int mgcp_vty_init(void) { install_element_ve(&show_mgcp_cmd); install_element(ENABLE_NODE, &loop_endp_cmd); install_element(ENABLE_NODE, &tap_call_cmd); install_element(ENABLE_NODE, &free_endp_cmd); + install_element(ENABLE_NODE, &reset_endp_cmd); + install_element(ENABLE_NODE, &reset_all_endp_cmd); install_element(CONFIG_NODE, &cfg_mgcp_cmd); install_node(&mgcp_node, config_write_mgcp); @@ -744,6 +874,7 @@ int mgcp_vty_init(void) install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_dscp_cmd); install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd); install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd); + install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd_old); install_element(MGCP_NODE, &cfg_mgcp_transcoder_cmd); install_element(MGCP_NODE, &cfg_mgcp_no_transcoder_cmd); install_element(MGCP_NODE, &cfg_mgcp_transcoder_remote_base_cmd); @@ -756,7 +887,11 @@ int mgcp_vty_init(void) install_element(VTRUNK_NODE, &cfg_vtrunk_number_endp_cmd); install_element(VTRUNK_NODE, &cfg_trunk_payload_number_cmd); install_element(VTRUNK_NODE, &cfg_trunk_payload_name_cmd); + install_element(VTRUNK_NODE, &cfg_trunk_payload_number_cmd_old); + install_element(VTRUNK_NODE, &cfg_trunk_payload_name_cmd_old); install_element(VTRUNK_NODE, &cfg_trunk_loop_cmd); + install_element(VTRUNK_NODE, &cfg_trunk_omit_rtcp_cmd); + install_element(VTRUNK_NODE, &cfg_trunk_no_omit_rtcp_cmd); install_element(MGCP_NODE, &cfg_mgcp_trunk_cmd); install_node(&trunk_node, config_write_trunk); @@ -765,7 +900,13 @@ int mgcp_vty_init(void) install_element(TRUNK_NODE, &ournode_end_cmd); install_element(TRUNK_NODE, &cfg_trunk_payload_number_cmd); install_element(TRUNK_NODE, &cfg_trunk_payload_name_cmd); + install_element(TRUNK_NODE, &cfg_trunk_payload_number_cmd_old); + install_element(TRUNK_NODE, &cfg_trunk_payload_name_cmd_old); install_element(TRUNK_NODE, &cfg_trunk_loop_cmd); + install_element(TRUNK_NODE, &cfg_trunk_omit_rtcp_cmd); + install_element(TRUNK_NODE, &cfg_trunk_no_omit_rtcp_cmd); + install_element(TRUNK_NODE, &cfg_trunk_omit_rtcp_cmd); + install_element(TRUNK_NODE, &cfg_trunk_no_omit_rtcp_cmd); return 0; } diff --git a/src/mgcp_ss7.c b/src/mgcp_ss7.c index c9e9b4f..46cd185 100644 --- a/src/mgcp_ss7.c +++ b/src/mgcp_ss7.c @@ -643,7 +643,7 @@ static int mgcp_ss7_policy(struct mgcp_trunk_config *tcfg, int endp_no, int stat return rc; } -static int mgcp_dtmf_cb(struct mgcp_endpoint *endp, char tone, const char *data) +static int mgcp_dtmf_cb(struct mgcp_endpoint *endp, char tone) { mgcp_ss7_exec(endp, MGCP_SS7_DTMF, tone); return 0; |