| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* Virtual console TTY. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef __DEV_VTTY_H__ |
| 9 |
#define __DEV_VTTY_H__ |
| 10 |
|
| 11 |
#include <sys/types.h> |
| 12 |
#include <pthread.h> |
| 13 |
|
| 14 |
#include "vm.h" |
| 15 |
#include <stdio.h> |
| 16 |
|
| 17 |
/* 4 Kb should be enough for a keyboard buffer */ |
| 18 |
#define VTTY_BUFFER_SIZE 4096 |
| 19 |
|
| 20 |
/* Maximum listening socket number */ |
| 21 |
#define VTTY_MAX_FD 10 |
| 22 |
|
| 23 |
/* VTTY connection types */ |
| 24 |
enum { |
| 25 |
VTTY_TYPE_NONE = 0, |
| 26 |
VTTY_TYPE_TERM, |
| 27 |
VTTY_TYPE_TCP, |
| 28 |
VTTY_TYPE_SERIAL, |
| 29 |
}; |
| 30 |
|
| 31 |
/* VTTY connection states (for TCP) */ |
| 32 |
enum { |
| 33 |
VTTY_STATE_TCP_INVALID, /* connection is not working */ |
| 34 |
VTTY_STATE_TCP_WAITING, /* waiting for incoming connection */ |
| 35 |
VTTY_STATE_TCP_RUNNING, /* character reading/writing ok */ |
| 36 |
}; |
| 37 |
|
| 38 |
/* VTTY input states */ |
| 39 |
enum { |
| 40 |
VTTY_INPUT_TEXT, |
| 41 |
VTTY_INPUT_VT1, |
| 42 |
VTTY_INPUT_VT2, |
| 43 |
VTTY_INPUT_REMOTE, |
| 44 |
VTTY_INPUT_TELNET, |
| 45 |
VTTY_INPUT_TELNET_IYOU, |
| 46 |
VTTY_INPUT_TELNET_SB1, |
| 47 |
VTTY_INPUT_TELNET_SB2, |
| 48 |
VTTY_INPUT_TELNET_SB_TTYPE, |
| 49 |
VTTY_INPUT_TELNET_NEXT |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
/* Commmand line support utility */ |
| 54 |
typedef struct vtty_serial_option vtty_serial_option_t; |
| 55 |
struct vtty_serial_option { |
| 56 |
char *device; |
| 57 |
int baudrate, databits, parity, stopbits, hwflow; |
| 58 |
}; |
| 59 |
|
| 60 |
int vtty_parse_serial_option(vtty_serial_option_t *params, char *optarg); |
| 61 |
|
| 62 |
/* Virtual TTY structure */ |
| 63 |
typedef struct virtual_tty vtty_t; |
| 64 |
struct virtual_tty { |
| 65 |
vm_instance_t *vm; |
| 66 |
char *name; |
| 67 |
int type; |
| 68 |
int fd_array[VTTY_MAX_FD]; |
| 69 |
int fd_count; |
| 70 |
int tcp_port; |
| 71 |
int terminal_support; |
| 72 |
int input_state; |
| 73 |
int input_pending; |
| 74 |
int telnet_cmd, telnet_opt, telnet_qual; |
| 75 |
int managed_flush; |
| 76 |
u_char buffer[VTTY_BUFFER_SIZE]; |
| 77 |
u_int read_ptr,write_ptr; |
| 78 |
pthread_mutex_t lock; |
| 79 |
vtty_t *next,**pprev; |
| 80 |
void *priv_data; |
| 81 |
u_long user_arg; |
| 82 |
|
| 83 |
/* FD Pool (for TCP connections) */ |
| 84 |
fd_pool_t fd_pool; |
| 85 |
|
| 86 |
/* Read notification */ |
| 87 |
void (*read_notifier)(vtty_t *); |
| 88 |
|
| 89 |
/* Old text for replay */ |
| 90 |
u_char replay_buffer[VTTY_BUFFER_SIZE]; |
| 91 |
u_int replay_ptr; |
| 92 |
}; |
| 93 |
|
| 94 |
#define VTTY_LOCK(tty) pthread_mutex_lock(&(tty)->lock); |
| 95 |
#define VTTY_UNLOCK(tty) pthread_mutex_unlock(&(tty)->lock); |
| 96 |
|
| 97 |
/* create a virtual tty */ |
| 98 |
vtty_t *vtty_create(vm_instance_t *vm,char *name,int type,int tcp_port, |
| 99 |
const vtty_serial_option_t *option); |
| 100 |
|
| 101 |
/* delete a virtual tty */ |
| 102 |
void vtty_delete(vtty_t *vtty); |
| 103 |
|
| 104 |
/* Store arbritary data in the FIFO buffer */ |
| 105 |
int vtty_store_data(vtty_t *vtty,char *data, int len); |
| 106 |
|
| 107 |
/* read a character from the buffer (-1 if the buffer is empty) */ |
| 108 |
int vtty_get_char(vtty_t *vtty); |
| 109 |
|
| 110 |
/* print a character to vtty */ |
| 111 |
void vtty_put_char(vtty_t *vtty, char ch); |
| 112 |
|
| 113 |
/* Put a buffer to vtty */ |
| 114 |
void vtty_put_buffer(vtty_t *vtty,char *buf,size_t len); |
| 115 |
|
| 116 |
/* Flush VTTY output */ |
| 117 |
void vtty_flush(vtty_t *vtty); |
| 118 |
|
| 119 |
/* returns TRUE if a character is available in buffer */ |
| 120 |
int vtty_is_char_avail(vtty_t *vtty); |
| 121 |
|
| 122 |
/* write CTRL+C to buffer */ |
| 123 |
int vtty_store_ctrlc(vtty_t *); |
| 124 |
|
| 125 |
/* Initialize the VTTY thread */ |
| 126 |
int vtty_init(void); |
| 127 |
|
| 128 |
void vtty_set_ctrlhandler(int n); |
| 129 |
void vtty_set_telnetmsg(int n); |
| 130 |
|
| 131 |
#endif |