Skip to content

Commit

Permalink
vty cli: add basic infrastructure for CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Oct 27, 2010
1 parent 1f8faa5 commit 2babd20
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Expand Up @@ -23,6 +23,7 @@ noinst_HEADERS = knet.h \
utils.h \
vty.h \
vty_auth.h \
vty_cli.h \
vty_utils.h

sbin_PROGRAMS = kronosnetd
Expand All @@ -33,6 +34,7 @@ kronosnetd_SOURCES = \
utils.c \
vty.c \
vty_auth.c \
vty_cli.c \
vty_utils.c

maintainer-clean-local:
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile.am
Expand Up @@ -38,6 +38,7 @@ vty_test_SOURCES = \
vty_test.c \
../vty.c \
../vty_auth.c \
../vty_cli.c \
../vty_utils.c \
../netutils.c \
../utils.c
Expand Down
3 changes: 3 additions & 0 deletions vty.c
Expand Up @@ -14,6 +14,7 @@
#include "netutils.h"
#include "vty.h"
#include "vty_auth.h"
#include "vty_cli.h"
#include "vty_utils.h"

STATIC pthread_mutex_t knet_vty_mutex = PTHREAD_MUTEX_INITIALIZER;
Expand Down Expand Up @@ -74,6 +75,8 @@ static void *vty_accept_thread(void *arg)
if (vty->got_epipe)
goto out_clean;

knet_vty_cli_bind(vty);

out_clean:
if (src_ip[0])
addrtostr_free(src_ip);
Expand Down
2 changes: 2 additions & 0 deletions vty.h
Expand Up @@ -9,6 +9,7 @@

#define KNET_VTY_DEFAULT_MAX_CONN 4
#define KNET_VTY_TOTAL_MAX_CONN 16
#define KNET_VTY_CLI_TIMEOUT 60

struct knet_vty {
pthread_t vty_thread;
Expand All @@ -20,6 +21,7 @@ struct knet_vty {
int conn_num;
int active;
int got_epipe;
int idle;
};

int knet_vty_main_loop(const char *configfile, const char *ip_addr,
Expand Down
62 changes: 62 additions & 0 deletions vty_cli.c
@@ -0,0 +1,62 @@
#include "config.h"

#include <errno.h>
#include <sys/select.h>

#include "utils.h"
#include "vty.h"
#include "vty_cli.h"
#include "vty_utils.h"

static int knet_vty_process_buf(struct knet_vty *vty, unsigned char *buf, int buflen)
{
return 0;
}

void knet_vty_cli_bind(struct knet_vty *vty)
{
int se_result = 0;
fd_set rfds;
struct timeval tv;
unsigned char buf[VTY_MAX_BUFFER_SIZE];
int readlen;

while (se_result >= 0 && !vty->got_epipe) {
FD_ZERO (&rfds);
FD_SET (vty->vty_sock, &rfds);

tv.tv_sec = 1;
tv.tv_usec = 0;

se_result = select((vty->vty_sock + 1), &rfds, 0, 0, &tv);

if ((se_result == -1) || (vty->got_epipe))
goto out_clean;

if (se_result == 0) {
vty->idle++;
if (vty->idle >= KNET_VTY_CLI_TIMEOUT) {
log_info("vty(%d) connection timeout", vty->conn_num);
knet_vty_write(vty, "\n\nvty(%d) connection timeout\n\n", vty->conn_num);
goto out_clean;
}
continue;
}

if (!FD_ISSET(vty->vty_sock, &rfds))
continue;

vty->idle = 0;

memset(buf, 0 , sizeof(buf));
readlen = knet_vty_read(vty, buf, sizeof(buf));
if (readlen <= 0)
goto out_clean;

knet_vty_process_buf(vty, buf, readlen);
}

out_clean:

return;
}
8 changes: 8 additions & 0 deletions vty_cli.h
@@ -0,0 +1,8 @@
#ifndef __VTY_CLI_H__
#define __VTY_CLI_H__

#include "vty.h"

void knet_vty_cli_bind(struct knet_vty *vty);

#endif

0 comments on commit 2babd20

Please sign in to comment.