pespin has submitted this change. ( https://gerrit.osmocom.org/c/osmo-mgw/+/39717?usp=email )
Change subject: mgw: Store Command as enum in struct mgcp_request_data ...................................................................... mgw: Store Command as enum in struct mgcp_request_data This simplifies code further, and will simpify more code since we'll have some easy way to check for the command we are in during a request. Change-Id: I2d4b5ddb93376b59413b34c9668c41157ab05497 --- M include/osmocom/mgcp/mgcp.h M include/osmocom/mgcp/mgcp_protocol.h M src/libosmo-mgcp/mgcp_protocol.c 3 files changed, 67 insertions(+), 76 deletions(-) Approvals: osmith: Looks good to me, but someone else must approve Jenkins Builder: Verified daniel: Looks good to me, but someone else must approve pespin: Looks good to me, approved diff --git a/include/osmocom/mgcp/mgcp.h b/include/osmocom/mgcp/mgcp.h index 4dff4d0..41eee42 100644 --- a/include/osmocom/mgcp/mgcp.h +++ b/include/osmocom/mgcp/mgcp.h @@ -50,11 +50,6 @@ struct mgcp_config; struct mgcp_trunk; struct mgcp_rtp_end; - -#define MGCP_ENDP_CRCX 1 -#define MGCP_ENDP_DLCX 2 -#define MGCP_ENDP_MDCX 3 - /* * what to do with the msg? * - continue as usual? diff --git a/include/osmocom/mgcp/mgcp_protocol.h b/include/osmocom/mgcp/mgcp_protocol.h index 427fe5f..4fed248 100644 --- a/include/osmocom/mgcp/mgcp_protocol.h +++ b/include/osmocom/mgcp/mgcp_protocol.h @@ -8,6 +8,18 @@ #include <osmocom/mgcp/mgcp_common.h> #include <osmocom/mgcp/mgcp_codec.h> +enum mgcp_verb { + MGCP_VERB_CRCX, + MGCP_VERB_MDCX, + MGCP_VERB_DLCX, + MGCP_VERB_AUEP, + MGCP_VERB_RQNT, + MGCP_VERB_RSIP, +}; +extern const struct value_string mgcp_verb_names[]; +static inline const char *mgcp_verb_name(enum mgcp_verb val) +{ return get_value_string(mgcp_verb_names, val); } + #define MGCP_PARSE_SDP_PTIME_UNSET (-1) #define MGCP_PARSE_SDP_MAXPTIME_UNSET (-1) diff --git a/src/libosmo-mgcp/mgcp_protocol.c b/src/libosmo-mgcp/mgcp_protocol.c index 35d0811..36d9ebf 100644 --- a/src/libosmo-mgcp/mgcp_protocol.c +++ b/src/libosmo-mgcp/mgcp_protocol.c @@ -59,6 +59,16 @@ return debug_last_endpoint_name; } +const struct value_string mgcp_verb_names[] = { + { MGCP_VERB_CRCX, "CRCX" }, + { MGCP_VERB_MDCX, "MDCX" }, + { MGCP_VERB_DLCX, "DLCX" }, + { MGCP_VERB_AUEP, "AUEP" }, + { MGCP_VERB_RSIP, "RSIP" }, + { MGCP_VERB_RQNT, "RQNT" }, + {} +}; + const struct value_string mgcp_connection_mode_strs[] = { { MGCP_CONN_NONE, "none" }, { MGCP_CONN_RECV_SEND, "sendrecv" }, @@ -81,7 +91,8 @@ /* Request data passed to the request handler */ struct mgcp_request_data { - /* request name (e.g. "MDCX") */ + enum mgcp_verb verb; + /* Verb string (e.g. "MDCX") */ char name[4+1]; /* parsing results from the MGCP header (trans id, endpoint name ...) */ @@ -103,55 +114,12 @@ int mgcp_cause; }; -/* Request handler specification, here we specify an array with function - * pointers to the various MGCP requests implemented below */ -struct mgcp_request { - /* request name (e.g. "MDCX") */ - char *name; - - /* function pointer to the request handler */ - struct msgb *(*handle_request)(struct mgcp_request_data *data); - - /* a human readable name that describes the request */ - char *debug_name; -}; - static struct msgb *handle_audit_endpoint(struct mgcp_request_data *data); static struct msgb *handle_create_con(struct mgcp_request_data *data); static struct msgb *handle_delete_con(struct mgcp_request_data *data); static struct msgb *handle_modify_con(struct mgcp_request_data *data); static struct msgb *handle_rsip(struct mgcp_request_data *data); static struct msgb *handle_noti_req(struct mgcp_request_data *data); -static const struct mgcp_request mgcp_requests[] = { - { .name = "AUEP", .handle_request = handle_audit_endpoint, .debug_name = "AuditEndpoint" }, - { - .name = "CRCX", - .handle_request = handle_create_con, - .debug_name = "CreateConnection", - }, - { - .name = "DLCX", - .handle_request = handle_delete_con, - .debug_name = "DeleteConnection", - }, - { - .name = "MDCX", - .handle_request = handle_modify_con, - .debug_name = "ModifiyConnection", - }, - { - .name = "RQNT", - .handle_request = handle_noti_req, - .debug_name = "NotificationRequest", - }, - - /* SPEC extension */ - { - .name = "RSIP", - .handle_request = handle_rsip, - .debug_name = "ReSetInProgress", - }, -}; /* Initalize transcoder */ static int setup_rtp_processing(struct mgcp_endpoint *endp, @@ -356,7 +324,7 @@ struct rate_ctr_group *rate_ctrs = cfg->ratectr.mgcp_general_ctr_group; struct mgcp_parse_data pdata; struct mgcp_request_data rq; - int rc, i, code, handled = 0; + int rc, code; struct msgb *resp = NULL; char *data; @@ -385,12 +353,23 @@ return NULL; } - - /* Parse message, extract endpoint name and transaction identifier and request name etc. */ + /* Initialize parsing data. */ memset(&pdata, 0, sizeof(pdata)); memset(&rq, 0, sizeof(rq)); pdata.cfg = cfg; + + /* Parse command name: */ memcpy(rq.name, (const char *)&msg->l2h[0], sizeof(rq.name)-1); + rc = get_string_value(mgcp_verb_names, rq.name); + if (rc < 0) { + LOGP(DLMGCP, LOGL_ERROR, "%s: failed to parse command name in MCGP message\n", rq.name); + rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_MSG_PARSE)); + rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_UNHANDLED)); + return create_err_response(cfg, NULL, 504, rq.name, "000000"); + } + rq.verb = rc; + + /* Parse message, extract endpoint name and transaction identifier and request name etc. */ msg->l3h = &msg->l2h[4]; data = mgcp_strline((char *)msg->l3h, &pdata.save); rc = mgcp_parse_header(&pdata, data); @@ -443,31 +422,36 @@ } } - /* Find an appropriate handler for the current request and execute it */ - for (i = 0; i < ARRAY_SIZE(mgcp_requests); i++) { - if (strcmp(mgcp_requests[i].name, rq.name) == 0) { - /* Execute request handler */ - if (rq.endp) - LOGP(DLMGCP, LOGL_INFO, - "%s: executing request handler \"%s\" for endpoint resource \"%s\"\n", rq.name, - mgcp_requests[i].debug_name, rq.endp->name); - else - LOGP(DLMGCP, LOGL_INFO, - "%s: executing request handler \"%s\" for trunk resource of endpoint \"%s\"\n", - rq.name, mgcp_requests[i].debug_name, pdata.epname); - resp = mgcp_requests[i].handle_request(&rq); - handled = 1; - break; - } + /* Execute request handler */ + if (rq.endp) + LOGP(DLMGCP, LOGL_INFO, "%s: executing request handler for endpoint resource \"%s\"\n", + rq.name, rq.endp->name); + else + LOGP(DLMGCP, LOGL_INFO, "%s: executing request handler for trunk resource of endpoint \"%s\"\n", + rq.name, pdata.epname); + switch (rq.verb) { + case MGCP_VERB_AUEP: + resp = handle_audit_endpoint(&rq); + break; + case MGCP_VERB_CRCX: + resp = handle_create_con(&rq); + break; + case MGCP_VERB_DLCX: + resp = handle_delete_con(&rq); + break; + case MGCP_VERB_MDCX: + resp = handle_modify_con(&rq); + break; + case MGCP_VERB_RQNT: + resp = handle_noti_req(&rq); + break; + case MGCP_VERB_RSIP: + resp = handle_rsip(&rq); + break; + default: + OSMO_ASSERT(0); } - - /* Check if the MGCP request was handled and increment rate counters accordingly. */ - if (handled) { - rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_HANDLED)); - } else { - rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_UNHANDLED)); - LOGP(DLMGCP, LOGL_ERROR, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]); - } + rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_HANDLED)); return resp; } -- To view, visit https://gerrit.osmocom.org/c/osmo-mgw/+/39717?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: merged Gerrit-Project: osmo-mgw Gerrit-Branch: master Gerrit-Change-Id: I2d4b5ddb93376b59413b34c9668c41157ab05497 Gerrit-Change-Number: 39717 Gerrit-PatchSet: 2 Gerrit-Owner: pespin <pes...@sysmocom.de> Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: daniel <dwillm...@sysmocom.de> Gerrit-Reviewer: osmith <osm...@sysmocom.de> Gerrit-Reviewer: pespin <pes...@sysmocom.de>