File: console.h

package info (click to toggle)
mrd6 0.9.5-release-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,308 kB
  • ctags: 3,956
  • sloc: cpp: 25,728; perl: 462; makefile: 281; ansic: 142; sh: 67
file content (251 lines) | stat: -rw-r--r-- 5,325 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Multicast Routing Daemon (MRD)
 *   console.h
 *
 * Copyright (C) 2004, 2005
 *  Universidade de Aveiro, Instituto Telecomunicacoes - Polo Aveiro
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors:	Hugo Santos, <hsantos@av.it.pt>
 */

#ifndef _mrd_console_h_
#define _mrd_console_h_

#include <mrd/address.h>
#include <mrd/log.h>
#include <mrd/mrd.h>
#include <mrd/node.h>
#include <mrd/parser.h>
#include <mrd/timers.h>

#include <stack>
#include <string>
#include <set>
#include <deque>
#include <list>
#include <map>
#include <functional>

class mrd;

class console_connection;

class console_module : public mrd_module, public node {
public:
	console_module(mrd *m, void *dlh);
	~console_module();

	const char *description() const;

	bool check_startup();
	void shutdown();

	bool call_method(int id, base_stream &,
			 const std::vector<std::string> &);
	bool output_info(base_stream &ctx, const std::vector<std::string> &) const;

#ifndef CONSOLE_NO_TELNET
	void new_client(uint32_t);
#endif
	void new_unix_client(uint32_t);

	void release_connection(console_connection *);

	bool password_for(const inet6_addr &, const char *, std::string &) const;

#ifndef CONSOLE_NO_TELNET
	socket0<console_module> srvsock;
#endif
	socket0<console_module> unix_srvsock;

	std::list<console_connection *> connections;

	struct auth_desc {
		std::string username;
		std::string password;
	};

	typedef std::map<inet6_addr,
			 std::list<auth_desc>,
			 std::greater<inet6_addr> > allow_local_def;

	allow_local_def acl;

	bool allow_addr(const std::vector<std::string> &);
	bool deny_addr(const std::vector<std::string> &);
	bool allow_local(const std::vector<std::string> &);
	bool attach_log(base_stream &, const std::vector<std::string> &);

	bool show_history(base_stream &);

	console_connection *calling_connection(base_stream &) const;
};

extern console_module *console;

class console_log_node : public tb_log_node {
public:
	console_log_node(console_connection *);

	void rename(const char *);

	void log(int, int, const char *, bool newline);

	console_connection *_conn;
	std::string _buf;
};

class console_connection : public stream_flusher {
public:
	console_connection(mrd *core, int);
	virtual ~console_connection() {}

	virtual bool check_startup();
	void shutdown();
	void doom();

	virtual void release();

	void data_available(uint32_t);
	virtual void process_input(int) = 0;

	virtual bool process_line(const char *);

	void process_deep_line(parser_context *);

	enum {
		CONSISTENCY_ERROR = -3,
		INPUT_ERROR = -2,
		END_LINE = -1,
		OK = 0
	};

	int transform(parser_context *, node *, node::content_type,
		      node * &) const;
	int transform(parser_context *, node *, node::content_type,
		      node * &, std::string &) const;

	void writeclient(const char *);

	virtual void flushed(const char *, bool) = 0;

	virtual void dump_history(base_stream &) const;

	virtual void log(bool end);

	mrd *m_mrd;

	bool is_doomed;

	socket0<console_connection> sock;

	mutable base_stream _output;

	unsigned char buffer[1024];

	std::string bufbuffer;

	int advance_one(parser_context *, node *);
	int check_termination(parser_context *, node *);
	void dump_partial(const char *);
	void dump_partial(node *, parser_context *, bool);

	bool autoclose;
	console_log_node *clog;
};

class telnet_console_connection : public console_connection {
public:
	telnet_console_connection(mrd *core, int, const inet6_addr &, uint32_t);
	~telnet_console_connection();

	bool check_startup();
	void release();

	const inet6_addr &peeraddr() const { return c_peeraddr; }

	bool authenticate(const char *);

	void process_input(int);

	void flushed(const char *, bool);

	bool process_line(const char *);

	void dump_history(base_stream &) const;
	void log(bool end);
private:
	enum {
		NoState,
		WaitingPassword,
		GotAllData,
	};

	bool process_cmd();

	bool tabcomplete();

	void show_prompt();
	void set_prompt(const char *);

	void clearline();
	void redisplay_input();

	void cmd(char c, char opt);

	void release_connection();

	void history_up();
	void history_down();

	timer<telnet_console_connection> conn_timer;

	std::deque<unsigned char> ctlbuf;

	int pmode;

	bool will_echo;
	bool should_echo;
	bool input_is_updated;

	std::string inputbuf;
	std::string tmp_inputbuf;

	std::vector<std::string> history;
	int history_pos;

	int authenticate_state;
	std::string username;

	std::string prompt;

	inet6_addr c_peeraddr;
};

class unix_console_connection : public console_connection {
public:
	unix_console_connection(mrd *core, int);

	void process_input(int);

	void release();

	void flushed(const char *, bool);
};

#endif