From 90e713969110306cd979f0edcbbd86a46d97518a Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Tue, 22 Mar 2016 19:17:17 +0100 Subject: mncc: Handle the hello message from NITB Make a simple version comparison and close the socket in case of a version mismatch. Begin to dispatch messages coming from the NITB and log (all) unhandled messages. --- src/mncc.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ src/mncc.h | 7 +++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/mncc.c b/src/mncc.c index 7539334..500d1e9 100644 --- a/src/mncc.c +++ b/src/mncc.c @@ -19,6 +19,7 @@ */ #include "mncc.h" +#include "mncc_protocol.h" #include "app.h" #include "logging.h" @@ -30,6 +31,38 @@ #include #include +static void close_connection(struct mncc_connection *conn) +{ + osmo_fd_unregister(&conn->fd); + close(conn->fd.fd); + osmo_timer_schedule(&conn->reconnect, 5, 0); + conn->state = MNCC_DISCONNECTED; + if (conn->on_disconnect) + conn->on_disconnect(conn); +} + +static void check_hello(struct mncc_connection *conn, char *buf, int rc) +{ + struct gsm_mncc_hello *hello; + + if (rc != sizeof(*hello)) { + LOGP(DMNCC, LOGL_ERROR, "Hello shorter than expected %d vs. %d\n", + rc, sizeof(*hello)); + return close_connection(conn); + } + + hello = (struct gsm_mncc_hello *) buf; + LOGP(DMNCC, LOGL_NOTICE, "Got hello message version %d\n", hello->version); + + if (hello->version != MNCC_SOCK_VERSION) { + LOGP(DMNCC, LOGL_NOTICE, "Incompatible version(%d) expected %d\n", + hello->version, MNCC_SOCK_VERSION); + return close_connection(conn); + } + + conn->state = MNCC_READY; +} + static void mncc_reconnect(void *data) { int rc; @@ -40,16 +73,19 @@ static void mncc_reconnect(void *data) if (rc < 0) { LOGP(DMNCC, LOGL_ERROR, "Failed to connect(%s). Retrying\n", conn->app->mncc.path); + conn->state = MNCC_DISCONNECTED; osmo_timer_schedule(&conn->reconnect, 5, 0); return; } LOGP(DMNCC, LOGL_NOTICE, "Reconnected to %s\n", conn->app->mncc.path); + conn->state = MNCC_WAIT_VERSION; } static int mncc_data(struct osmo_fd *fd, unsigned int what) { char buf[4096]; + uint32_t msg_type; int rc; struct mncc_connection *conn = fd->data; @@ -57,14 +93,27 @@ static int mncc_data(struct osmo_fd *fd, unsigned int what) if (rc <= 0) { LOGP(DMNCC, LOGL_ERROR, "Failed to read %d/%s. Re-connecting.\n", rc, strerror(errno)); - osmo_fd_unregister(fd); - close(fd->fd); - osmo_timer_schedule(&conn->reconnect, 5, 0); - if (conn->on_disconnect) - conn->on_disconnect(conn); - return 0; + goto bad_data; } + if (rc <= 4) { + LOGP(DMNCC, LOGL_ERROR, "Data too short with: %d\n", rc); + goto bad_data; + } + + memcpy(&msg_type, buf, 4); + switch (msg_type) { + case MNCC_SOCKET_HELLO: + check_hello(conn, buf, rc); + break; + default: + LOGP(DMNCC, LOGL_ERROR, "Unhandled message type %d/0x%x\n", + msg_type, msg_type); + break; + } + return 0; +bad_data: + close_connection(conn); return 0; } @@ -75,6 +124,7 @@ void mncc_connection_init(struct mncc_connection *conn, struct app_config *cfg) conn->fd.cb = mncc_data; conn->fd.data = conn; conn->app = cfg; + conn->state = MNCC_DISCONNECTED; } void mncc_connection_start(struct mncc_connection *conn) diff --git a/src/mncc.h b/src/mncc.h index d6afadc..5cbee4d 100644 --- a/src/mncc.h +++ b/src/mncc.h @@ -5,7 +5,14 @@ struct app_config; +enum { + MNCC_DISCONNECTED, + MNCC_WAIT_VERSION, + MNCC_READY, +}; + struct mncc_connection { + int state; struct app_config *app; struct osmo_fd fd; -- cgit v1.2.3