Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Achou.Wang
accel-ppp
提交
436fcaaf
A
accel-ppp
项目概览
Achou.Wang
/
accel-ppp
通知
6
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
accel-ppp
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
436fcaaf
编写于
12月 25, 2010
作者:
D
Dmitry Kozlov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cli: implemented persist history
上级
c2ac7118
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
81 addition
and
6 deletion
+81
-6
accel-pptpd/cli/telnet.c
accel-pptpd/cli/telnet.c
+81
-6
未找到文件。
accel-pptpd/cli/telnet.c
浏览文件 @
436fcaaf
...
...
@@ -53,6 +53,7 @@ struct buffer_t
{
struct
list_head
entry
;
int
size
;
struct
buffer_t
*
p_buf
;
uint8_t
buf
[
0
];
};
...
...
@@ -63,9 +64,15 @@ static LIST_HEAD(clients);
static
uint8_t
*
recv_buf
;
static
uint8_t
*
temp_buf
;
static
int
conf_history_len
=
100
;
static
const
char
*
conf_history_file
=
"/var/run/accel-pptp/history"
;
static
LIST_HEAD
(
history
);
static
int
history_len
;
static
pthread_mutex_t
history_lock
=
PTHREAD_MUTEX_INITIALIZER
;
static
void
disconnect
(
struct
telnet_client_t
*
cln
)
{
struct
buffer_t
*
b
;
struct
buffer_t
*
b
,
*
b2
;
log_debug
(
"cli: disconnect
\n
"
);
...
...
@@ -85,11 +92,22 @@ static void disconnect(struct telnet_client_t *cln)
_free
(
b
);
}
pthread_mutex_lock
(
&
history_lock
);
while
(
!
list_empty
(
&
cln
->
history
))
{
b
=
list_entry
(
cln
->
history
.
next
,
typeof
(
*
b
),
entry
);
b
=
list_entry
(
cln
->
history
.
prev
,
typeof
(
*
b
),
entry
);
list_del
(
&
b
->
entry
);
_free
(
b
);
if
(
!
b
->
p_buf
)
{
if
(
history_len
==
conf_history_len
)
{
b2
=
list_entry
(
history
.
next
,
typeof
(
*
b2
),
entry
);
list_del
(
&
b2
->
entry
);
_free
(
b2
);
}
else
history_len
++
;
list_add_tail
(
&
b
->
entry
,
&
history
);
}
else
_free
(
b
);
}
pthread_mutex_unlock
(
&
history_lock
);
_free
(
cln
->
cmdline
);
_free
(
cln
);
...
...
@@ -238,10 +256,10 @@ static int load_history(struct telnet_client_t *cln)
return
-
1
;
if
(
send_prompt
(
cln
))
return
-
1
;
memcpy
(
cln
->
cmdline
,
b
->
buf
,
b
->
size
);
memcpy
(
cln
->
cmdline
,
b
->
p_buf
?
b
->
p_buf
->
buf
:
b
->
buf
,
b
->
size
);
cln
->
cmdline_pos
=
b
->
size
;
cln
->
cmdline_len
=
b
->
size
;
if
(
telnet_send
(
cln
,
b
->
buf
,
b
->
size
))
if
(
telnet_send
(
cln
,
b
->
p_buf
?
b
->
p_buf
->
buf
:
b
->
buf
,
b
->
size
))
return
-
1
;
return
0
;
...
...
@@ -277,6 +295,7 @@ static int telnet_input_char(struct telnet_client_t *cln, uint8_t c)
}
}
else
if
(
cln
->
cmdline_len
)
{
b
=
_malloc
(
sizeof
(
*
b
)
+
cln
->
cmdline_len
);
b
->
p_buf
=
NULL
;
memcpy
(
b
->
buf
,
cln
->
cmdline
,
cln
->
cmdline_len
);
b
->
size
=
cln
->
cmdline_len
;
list_add
(
&
b
->
entry
,
cln
->
history
.
next
);
...
...
@@ -514,7 +533,7 @@ static int serv_read(struct triton_md_handler_t *h)
socklen_t
size
=
sizeof
(
addr
);
int
sock
;
struct
telnet_client_t
*
conn
;
struct
buffer_t
*
b
;
struct
buffer_t
*
b
,
*
b2
;
while
(
1
)
{
sock
=
accept
(
h
->
fd
,
(
struct
sockaddr
*
)
&
addr
,
&
size
);
...
...
@@ -543,8 +562,19 @@ static int serv_read(struct triton_md_handler_t *h)
INIT_LIST_HEAD
(
&
conn
->
history
);
b
=
_malloc
(
sizeof
(
*
b
)
+
RECV_BUF_SIZE
);
b
->
p_buf
=
b
;
b
->
size
=
0
;
list_add_tail
(
&
b
->
entry
,
&
conn
->
history
);
pthread_mutex_lock
(
&
history_lock
);
list_for_each_entry
(
b
,
&
history
,
entry
)
{
b2
=
_malloc
(
sizeof
(
*
b
));
b2
->
p_buf
=
b
;
b2
->
size
=
b
->
size
;
list_add
(
&
b2
->
entry
,
conn
->
history
.
next
);
}
pthread_mutex_unlock
(
&
history_lock
);
conn
->
history_pos
=
conn
->
history
.
next
;
conn
->
cli_client
.
cmdline
=
conn
->
cmdline
;
...
...
@@ -644,6 +674,43 @@ static void start_server(const char *host, int port)
triton_context_wakeup
(
&
serv_ctx
);
}
static
void
save_history_file
(
void
)
{
int
fd
;
struct
buffer_t
*
b
;
fd
=
open
(
conf_history_file
,
O_WRONLY
|
O_TRUNC
|
O_CREAT
,
S_IREAD
|
S_IWRITE
);
if
(
!
fd
)
return
;
list_for_each_entry
(
b
,
&
history
,
entry
)
{
b
->
buf
[
b
->
size
]
=
'\n'
;
write
(
fd
,
b
->
buf
,
b
->
size
+
1
);
}
close
(
fd
);
}
static
void
load_history_file
(
void
)
{
struct
buffer_t
*
b
;
FILE
*
f
;
f
=
fopen
(
conf_history_file
,
"r"
);
if
(
!
f
)
return
;
while
(
fgets
((
char
*
)
temp_buf
,
RECV_BUF_SIZE
,
f
))
{
b
=
_malloc
(
sizeof
(
*
b
)
+
strlen
((
char
*
)
temp_buf
));
b
->
p_buf
=
NULL
;
b
->
size
=
strlen
((
char
*
)
temp_buf
)
-
1
;
memcpy
(
b
->
buf
,
temp_buf
,
b
->
size
);
list_add_tail
(
&
b
->
entry
,
&
history
);
}
fclose
(
f
);
}
static
void
__init
init
(
void
)
{
const
char
*
opt
;
...
...
@@ -664,11 +731,19 @@ static void __init init(void)
if
(
port
<=
0
)
goto
err_fmt
;
opt
=
conf_get_opt
(
"cli"
,
"history-file"
);
if
(
opt
)
conf_history_file
=
opt
;
recv_buf
=
malloc
(
RECV_BUF_SIZE
);
temp_buf
=
malloc
(
RECV_BUF_SIZE
);
load_history_file
();
start_server
(
host
,
port
);
atexit
(
save_history_file
);
return
;
err_fmt:
log_emerg
(
"cli: telnet: invalid format
\n
"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录