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.
This commit is contained in:
Holger Hans Peter Freyther 2016-03-22 19:17:17 +01:00
parent 45f0fa09d2
commit 90e7139691
2 changed files with 63 additions and 6 deletions

View File

@ -19,6 +19,7 @@
*/
#include "mncc.h"
#include "mncc_protocol.h"
#include "app.h"
#include "logging.h"
@ -30,6 +31,38 @@
#include <errno.h>
#include <unistd.h>
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)

View File

@ -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;