* correctly handle VTY_READ/VTY_WRITE events and select loop integration of VTY

This commit is contained in:
Harald Welte 2009-03-10 19:46:16 +00:00
parent d256d4f19c
commit c63e51daac
3 changed files with 55 additions and 23 deletions

View File

@ -21,9 +21,25 @@
#define VTY_BUFSIZ 512 #define VTY_BUFSIZ 512
#define VTY_MAXHIST 20 #define VTY_MAXHIST 20
/* Vty events */
enum event {
VTY_SERV,
VTY_READ,
VTY_WRITE,
VTY_TIMEOUT_RESET,
#ifdef VTYSH
VTYSH_SERV,
VTYSH_READ,
VTYSH_WRITE
#endif /* VTYSH */
};
struct vty { struct vty {
FILE *file; FILE *file;
/* private data, specified by creator */
void *priv;
/* File descripter of this vty. */ /* File descripter of this vty. */
int fd; int fd;
@ -115,7 +131,7 @@ void vty_init (void);
void vty_init_vtysh (void); void vty_init_vtysh (void);
void vty_reset (void); void vty_reset (void);
struct vty *vty_new (void); struct vty *vty_new (void);
struct vty *vty_create (int vty_sock); struct vty *vty_create (int vty_sock, void *priv);
int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
int vty_out_newline(struct vty *); int vty_out_newline(struct vty *);
int vty_read(struct vty *vty); int vty_read(struct vty *vty);

View File

@ -35,6 +35,8 @@
#include <openbsc/paging.h> #include <openbsc/paging.h>
#include <openbsc/signal.h> #include <openbsc/signal.h>
#include <vty/buffer.h>
#define WRITE_CONNECTION(fd, msg...) \ #define WRITE_CONNECTION(fd, msg...) \
int ret; \ int ret; \
char buf[4096]; \ char buf[4096]; \
@ -129,7 +131,20 @@ int telnet_close_client(struct bsc_fd *fd) {
static int client_data(struct bsc_fd *fd, unsigned int what) static int client_data(struct bsc_fd *fd, unsigned int what)
{ {
struct telnet_connection *conn = fd->data; struct telnet_connection *conn = fd->data;
return vty_read(conn->vty); int rc;
if (what & BSC_FD_READ) {
conn->fd.when &= ~BSC_FD_READ;
rc = vty_read(conn->vty);
}
if (what & BSC_FD_WRITE) {
rc = buffer_flush_all(conn->vty->obuf, fd->fd);
if (rc == BUFFER_EMPTY)
conn->fd.when &= ~BSC_FD_WRITE;
}
return rc;
} }
static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) { static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
@ -157,13 +172,31 @@ static int telnet_new_connection(struct bsc_fd *fd, unsigned int what) {
print_welcome(new_connection); print_welcome(new_connection);
connection->vty = vty_create(new_connection); connection->vty = vty_create(new_connection, connection);
if (!connection->vty) if (!connection->vty)
return -1; return -1;
return 0; return 0;
} }
/* callback from VTY code */
void vty_event(enum event event, int sock, struct vty *vty)
{
struct telnet_connection *connection = vty->priv;
struct bsc_fd *bfd = &connection->fd;
switch (event) {
case VTY_READ:
bfd->when |= BSC_FD_READ;
break;
case VTY_WRITE:
bfd->when |= BSC_FD_WRITE;
break;
default:
break;
}
}
#if 0 #if 0
static int telnet_paging_callback(unsigned int subsys, unsigned int singal, static int telnet_paging_callback(unsigned int subsys, unsigned int singal,
void *handler_data, void *signal_data) void *handler_data, void *signal_data)

View File

@ -18,19 +18,6 @@
#include <vty/command.h> #include <vty/command.h>
#include <vty/buffer.h> #include <vty/buffer.h>
/* Vty events */
enum event {
VTY_SERV,
VTY_READ,
VTY_WRITE,
VTY_TIMEOUT_RESET,
#ifdef VTYSH
VTYSH_SERV,
VTYSH_READ,
VTYSH_WRITE
#endif /* VTYSH */
};
extern struct host host; extern struct host host;
/* Vector which store each vty structure. */ /* Vector which store each vty structure. */
@ -235,6 +222,7 @@ int vty_out_newline(struct vty *vty)
{ {
char *p = vty_newline(vty); char *p = vty_newline(vty);
buffer_put(vty->obuf, p, strlen(p)); buffer_put(vty->obuf, p, strlen(p));
return 0;
} }
int vty_config_lock(struct vty *vty) int vty_config_lock(struct vty *vty)
@ -255,12 +243,6 @@ int vty_config_unlock(struct vty *vty)
return vty->config; return vty->config;
} }
static void vty_event(enum event event, int sock, struct vty *vty)
{
fprintf(stdout, "vty_event(%d, %d, %p)\n", event, sock, vty);
buffer_flush_all(vty->obuf, sock);
}
/* Say hello to vty interface. */ /* Say hello to vty interface. */
void vty_hello(struct vty *vty) void vty_hello(struct vty *vty)
{ {
@ -1375,7 +1357,7 @@ int vty_read(struct vty *vty)
/* Create new vty structure. */ /* Create new vty structure. */
struct vty * struct vty *
vty_create (int vty_sock) vty_create (int vty_sock, void *priv)
{ {
struct vty *vty; struct vty *vty;
@ -1388,6 +1370,7 @@ vty_create (int vty_sock)
/* Allocate new vty structure and set up default values. */ /* Allocate new vty structure and set up default values. */
vty = vty_new (); vty = vty_new ();
vty->fd = vty_sock; vty->fd = vty_sock;
vty->priv = priv;
vty->type = VTY_TERM; vty->type = VTY_TERM;
if (no_password_check) if (no_password_check)
{ {