quagga作为一个路由器软件,自然要提供人机接口。
quagga提供snmp管理接口,而且,自然就会有对应的命令行管理格式,当然一般路由软件不会提供界面形式的,也许有webui,然而quagga并没有。
我们要看的就是这个命令行处理的代码 command。
接触过类似命令行的朋友肯定有一点点好奇吧,那么数量庞大的命令和参数输入,还可以提供提示和自动补齐,这肯定不是一件很简单的事情。
下面是一个配置示例:
!
interface bge0
ip ospf authentication message-digest
ip ospf message-digest-key md5 ABCDEFGHIJK
!
router ospf
network 192.168.0.0/ area 0.0.0.1
area 0.0.0.1 authentication message-digest
哦哦,看到这样的命令,实在是头疼。
嗯,不废话了,我们还是看代码吧,看command是怎么处理这令人头疼的命令行吧:
void cmd_init(int terminal) {
...... cmdvec = vector_init(VECTOR_MIN_SIZE); /* Install top nodes. */
install_node(&view_node, NULL);
install_node(&enable_node, NULL); /* Each node's basic commands. */
install_element(VIEW_NODE, &show_version_cmd); .....
}
这个就是命令行初始化的简化版本。
quagg使用了非常常见的树形列表来描述所有的命令, cmdvec包含所有的顶层命令节点,节点下面是当前节点的所包含的命令元素.
struct cmd_node
{
/* Node index. */
enum node_type node; /* Prompt character at vty interface. */
const char *prompt; /* Is this node's configuration goes to vtysh ? */
int vtysh; /* Node's configuration write function */
int (*func) (struct vty *); /* Vector of this node's command list. */
vector cmd_vector;
};
上面已经举过命令行的具体例子,解释和执行命令行的函数如下:
extern vector cmd_make_strvec (const char *);
extern int cmd_execute_command (vector, struct vty *, struct cmd_element **, int);
通过查找匹配,找到对应的函数执行:
/* Execute matched command. */
return (*matched_element->func)(matched_element, vty, argc, argv);
执行的函数由如下的宏声明:
/* helper defines for end-user DEFUN* macros */
#define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
struct cmd_element cmdname = \
{ \
.string = cmdstr, \
.func = funcname, \
.doc = helpstr, \
.attr = attrs, \
.daemon = dnum, \
}; #define DEFUN_CMD_FUNC_DECL(funcname) \
static int funcname (struct cmd_element *, struct vty *, int, const char *[]); #define DEFUN_CMD_FUNC_TEXT(funcname) \
static int funcname \
(struct cmd_element *self __attribute__ ((unused)), \
struct vty *vty __attribute__ ((unused)), \
int argc __attribute__ ((unused)), \
const char *argv[] __attribute__ ((unused)) ) #define DEFUN(funcname, cmdname, cmdstr, helpstr) \
DEFUN_CMD_FUNC_DECL(funcname) \
DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, , ) \
DEFUN_CMD_FUNC_TEXT(funcname)
然后看一个具体命令行声明:
/* Configration from terminal */
DEFUN(config_terminal,
config_terminal_cmd,
"configure terminal",
"Configuration from vty interface\n"
"Configuration terminal\n") {
if (vty_config_lock(vty)) vty->node = CONFIG_NODE;
else {
vty_out(vty, "VTY configuration is locked by other VTY%s", VTY_NEWLINE);
return CMD_WARNING;
}
return CMD_SUCCESS;
}
这是进入配置模式的命令。
嗯,这个就看到这里吧!