Vdehist library

From VirtualSquare

Jump to: navigation, search

Libvdehist is a library to manage history and command completion on vde management lines. It is currently used by vdeterm and vdetelweb. vdehist is a layer that works between a terminal line and a management connection. feeding all the data from the terminal line and the management connection to the library, all the issues related with history management, command completion get automagically solved.

The main library functions are:

  • struct vdehiststat *vdehist_new(int termfd, int mgmtfd): this call starts up a terminal/management vdehist session.
  • void vdehist_mgmt_to_term(struct vdehiststat *st): should be called when data from management is ready
  • int vdehist_term_to_mgmt(struct vdehiststat *st): should be called when data from terminal is ready. Returns nonzero on end-of-file.
  • void vdehist_free(struct vdehiststat *st): closes the vdehist session.

The standard use of vdehist is the following (a good clear example of vdehist usage is vdeterm.c)

...open the file/socket (put the terminal in ~ICANON ~ECHO mode)
vdehst=vdehist_new(STDIN_FILENO,fd);
write(STDOUT_FILENO,prompt,strlen(prompt)+1); /* print the first prompt */
while (1)
...select/poll 
if /*data available from mgmt*/
  vdehist_mgmt_to_term(vdehst);
if /*data available from term*/ {
  if (vdehist_term_to_mgmt(vdehst) != 0)
    break;
}
vdehist_free(vdehst);

It is possible to redefine the routines used for sending/receiving data to/from the terminal and management line.

typedef ssize_t (* ssize_fun)();
extern ssize_fun vdehist_vderead;
extern ssize_fun vdehist_vdewrite;
extern ssize_fun vdehist_termread;
extern ssize_fun vdehist_termwrite;

These function pointers are initialized to the standard system calls read and write but can be redefined. In vdetelweb we have used this feature to exchange data with the terminal by lwipv6.

Another feature used in vdetelweb is a login procedure. When vdehist_new is called with mgmtfd < 0, vdehist starts in HIST_NOCMD state. In all the status different by HIST_COMMAND, history and command completion is inactive and all the lines get sent to a login procedure. If HIST_PASSWDFLAG flag is set in the status, there is no echo of the typed character. The login function must be loaded in the vdehist_logincmd variable. The signature of a login function is:

 char *vdehist_login(char *cmd,int len,struct vdehiststat *s);
 ....
 vdehist_logincmd=vdehist_login;

cmd and len are the typed line and its length, respectively. A login function returns the line to be sent to the management, NULL if nothing should be sent, "logout" to leave. A login function receives all the login/password data before opening a connection to the managment socket, then when the authentication phase succeeds it opens a management connection, updates the mgmtfd of the vdehost session by vdehist_setmgmtfd and sets the session status to HIST_COMMAND by vdehist_setstatus.

Personal tools