telnet: deal gracefully with vty being closed by vty layer

also: ignore vty's that are not terminals (e.g. VTY_FILE)
diff --git a/openbsc/src/telnet_interface.c b/openbsc/src/telnet_interface.c
index 7a67fe1..ba57470 100644
--- a/openbsc/src/telnet_interface.c
+++ b/openbsc/src/telnet_interface.c
@@ -146,6 +146,10 @@
 		rc = vty_read(conn->vty);
 	}
 
+	/* vty might have been closed from vithin vty_read() */
+	if (!conn->vty)
+		return rc;
+
 	if (what & BSC_FD_WRITE) {
 		rc = buffer_flush_all(conn->vty->obuf, fd->fd);
 		if (rc == BUFFER_EMPTY)
@@ -192,6 +196,9 @@
 	struct telnet_connection *connection = vty->priv;
 	struct bsc_fd *bfd = &connection->fd;
 
+	if (vty->type != VTY_TERM)
+		return;
+
 	switch (event) {
 	case VTY_READ:
 		bfd->when |= BSC_FD_READ;
@@ -199,6 +206,11 @@
 	case VTY_WRITE:
 		bfd->when |= BSC_FD_WRITE;
 		break;
+	case VTY_CLOSED:
+		/* vty layer is about to free() vty */
+		connection->vty = NULL;
+		telnet_close_client(bfd);
+		break;
 	default:
 		break;
 	}