Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
G
gdut-drcom
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
gdut-drcom
Commits
c4b1cd63
Commit
c4b1cd63
authored
Dec 24, 2016
by
chenhaowen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add: logger
parent
09522e49
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
123 additions
and
77 deletions
+123
-77
src/auth.c
src/auth.c
+94
-64
src/config.c
src/config.c
+11
-9
src/gdut-drcom.c
src/gdut-drcom.c
+3
-4
src/logger.c
src/logger.c
+13
-0
src/logger.h
src/logger.h
+2
-0
No files found.
src/auth.c
View file @
c4b1cd63
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
#include <stdint.h>
#include <stdint.h>
#include <sys/time.h>
#include <sys/time.h>
#include <time.h>
#include <time.h>
#include "logger.h"
#ifdef WIN32
#ifdef WIN32
#include <winsock2.h>
#include <winsock2.h>
...
@@ -53,6 +54,7 @@
...
@@ -53,6 +54,7 @@
#endif
#endif
#include "config.h"
#include "config.h"
/****local functions****/
/****local functions****/
static
int
make_keep_alive1_pkt1
(
uint8_t
*
buf
,
uint8_t
cnt
);
static
int
make_keep_alive1_pkt1
(
uint8_t
*
buf
,
uint8_t
cnt
);
static
int
make_keep_alive1_pkt2
(
uint8_t
*
buf
,
uint8_t
*
seed
,
\
static
int
make_keep_alive1_pkt2
(
uint8_t
*
buf
,
uint8_t
*
seed
,
\
...
@@ -67,17 +69,28 @@ static void gen_ka1_checksum(uint8_t *checksum, uint8_t *seed, uint8_t mode);
...
@@ -67,17 +69,28 @@ static void gen_ka1_checksum(uint8_t *checksum, uint8_t *seed, uint8_t mode);
static
void
gen_ka2_checksum
(
uint8_t
*
data
,
int
len
,
uint8_t
*
checksum
);
static
void
gen_ka2_checksum
(
uint8_t
*
data
,
int
len
,
uint8_t
*
checksum
);
static
uint32_t
drcomCRC32
(
uint8_t
*
data
,
int
len
);
static
uint32_t
drcomCRC32
(
uint8_t
*
data
,
int
len
);
static
void
print_as_hex
(
uint8_t
*
buf
,
int
len
);
//static void print_as_hex(uint8_t *buf, int len);
static
void
pkt_data_log
(
char
*
header
,
uint8_t
*
pkt_data
,
int
len
);
static
void
short_wait_sleep
(
int
second
);
static
void
short_wait_sleep
(
int
second
);
/****local functions****/
/****local functions****/
int
auth
(
void
)
int
auth
(
void
)
{
{
if
(
!
drcom_config
.
log_file
)
if
(
!
logger
)
{
{
drcom_config
.
log_file
=
stdout
;
logger
=
Logger_create
()
;
}
}
log_info
(
logger
,
"
\n
drcom_config.remote_ip = %s
\n
"
" drcom_config.remote_port = %d
\n
"
" drcom_config.keep_alive1_flag = %02hhx
\n
"
" drcom_config.enable_crypt = %d"
,
drcom_config
.
remote_ip
,
drcom_config
.
remote_port
,
drcom_config
.
keep_alive1_flag
,
drcom_config
.
enable_crypt
);
#ifdef WIN32
#ifdef WIN32
WORD
sockVersion
=
MAKEWORD
(
2
,
2
);
WORD
sockVersion
=
MAKEWORD
(
2
,
2
);
WSADATA
wsaData
;
WSADATA
wsaData
;
...
@@ -130,8 +143,7 @@ int auth(void)
...
@@ -130,8 +143,7 @@ int auth(void)
SOCKET
client_sockfd
;
SOCKET
client_sockfd
;
if
((
client_sockfd
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
))
==
INVALID_SOCKET
)
if
((
client_sockfd
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
))
==
INVALID_SOCKET
)
{
{
fprintf
(
stderr
,
"error!
\n
"
);
log_error
(
logger
,
"socket open error!"
);
fflush
(
stderr
);
return
-
1
;
return
-
1
;
}
}
...
@@ -150,8 +162,7 @@ int auth(void)
...
@@ -150,8 +162,7 @@ int auth(void)
HEART_BEAT_START:
HEART_BEAT_START:
fprintf
(
drcom_config
.
log_file
,
"gdut-drcom heart-beat started!
\n\n
"
);
log_info
(
logger
,
"gdut-drcom heart-beat started!"
);
fflush
(
drcom_config
.
log_file
);
kp1_cnt
=
1
;
kp1_cnt
=
1
;
kp2_cnt
=
0
;
kp2_cnt
=
0
;
srand
((
unsigned
int
)
time
(
NULL
));
srand
((
unsigned
int
)
time
(
NULL
));
...
@@ -163,10 +174,12 @@ HEART_BEAT_START:
...
@@ -163,10 +174,12 @@ HEART_BEAT_START:
length
=
make_keep_alive1_pkt1
(
pkt_data
,
kp1_cnt
);
length
=
make_keep_alive1_pkt1
(
pkt_data
,
kp1_cnt
);
sendto
(
client_sockfd
,
pkt_data
,
length
,
0
,
\
sendto
(
client_sockfd
,
pkt_data
,
length
,
0
,
\
(
struct
sockaddr
*
)
&
remote_addr
,
sizeof
(
remote_addr
));
(
struct
sockaddr
*
)
&
remote_addr
,
sizeof
(
remote_addr
));
fprintf
(
drcom_config
.
log_file
,
"<==[sended kap1_1 request %d] len = %d
\n
"
,
\
// fprintf(drcom_config.log_file, "<==[sended kap1_1 request %d] len = %d\n",\
kp1_cnt
,
length
);
// kp1_cnt, length);
fflush
(
drcom_config
.
log_file
);
// fflush(drcom_config.log_file);
print_as_hex
(
pkt_data
,
length
);
// print_as_hex(pkt_data, length);
pkt_data_log
(
"
\n
<== sended "
,
pkt_data
,
length
);
if
(
retry_cnt
>
5
)
if
(
retry_cnt
>
5
)
{
{
...
@@ -176,8 +189,7 @@ HEART_BEAT_START:
...
@@ -176,8 +189,7 @@ HEART_BEAT_START:
if
((
length
=
recvfrom
(
client_sockfd
,
pkt_data
,
1024
,
0
,
\
if
((
length
=
recvfrom
(
client_sockfd
,
pkt_data
,
1024
,
0
,
\
(
struct
sockaddr
*
)
&
remote_addr
,
&
sin_size
))
==
-
1
)
(
struct
sockaddr
*
)
&
remote_addr
,
&
sin_size
))
==
-
1
)
{
{
fprintf
(
drcom_config
.
log_file
,
"recv kap1_1 timeout, retry %d!
\n
"
,
retry_cnt
++
);
log_info
(
logger
,
"recv kap1_1 timeout, retry %d"
,
retry_cnt
++
);
fflush
(
drcom_config
.
log_file
);
}
}
else
else
{
{
...
@@ -185,13 +197,15 @@ HEART_BEAT_START:
...
@@ -185,13 +197,15 @@ HEART_BEAT_START:
}
}
}
}
retry_cnt
=
0
;
retry_cnt
=
0
;
fprintf
(
drcom_config
.
log_file
,
"==>[recieved kap1_1 response %d] len = %d
\n
"
,
\
// fprintf(drcom_config.log_file, "==>[recieved kap1_1 response %d] len = %d\n",\
kp1_cnt
,
length
);
// kp1_cnt, length);
fflush
(
drcom_config
.
log_file
);
// fflush(drcom_config.log_file);
print_as_hex
(
pkt_data
,
length
);
// print_as_hex(pkt_data,length);
pkt_data_log
(
"
\n
==> recieved"
,
pkt_data
,
length
);
memcpy
(
seed
,
pkt_data
+
8
,
4
);
memcpy
(
seed
,
pkt_data
+
8
,
4
);
memcpy
(
host_ip
,
pkt_data
+
12
,
4
);
memcpy
(
host_ip
,
pkt_data
+
12
,
4
);
// memcpy(drcom_config.host_ip, pkt_data+12, 4);
kp1_cnt
++
;
kp1_cnt
++
;
retry_cnt
=
1
;
retry_cnt
=
1
;
...
@@ -200,10 +214,11 @@ HEART_BEAT_START:
...
@@ -200,10 +214,11 @@ HEART_BEAT_START:
length
=
make_keep_alive1_pkt2
(
pkt_data
,
seed
,
host_ip
,
kp1_cnt
);
length
=
make_keep_alive1_pkt2
(
pkt_data
,
seed
,
host_ip
,
kp1_cnt
);
sendto
(
client_sockfd
,
pkt_data
,
length
,
0
,
\
sendto
(
client_sockfd
,
pkt_data
,
length
,
0
,
\
(
struct
sockaddr
*
)
&
remote_addr
,
sizeof
(
remote_addr
));
(
struct
sockaddr
*
)
&
remote_addr
,
sizeof
(
remote_addr
));
fprintf
(
drcom_config
.
log_file
,
"<==[sended kap1_2 request %d] len = %d
\n
"
,
\
// fprintf(drcom_config.log_file, "<==[sended kap1_2 request %d] len = %d\n",\
kp1_cnt
,
length
);
// kp1_cnt, length);
fflush
(
drcom_config
.
log_file
);
// fflush(drcom_config.log_file);
print_as_hex
(
pkt_data
,
length
);
// print_as_hex(pkt_data, length);
pkt_data_log
(
"
\n
<== sended"
,
pkt_data
,
length
);
length
=
0
;
length
=
0
;
if
(
retry_cnt
>
5
)
if
(
retry_cnt
>
5
)
...
@@ -213,8 +228,7 @@ HEART_BEAT_START:
...
@@ -213,8 +228,7 @@ HEART_BEAT_START:
if
((
length
=
recvfrom
(
client_sockfd
,
pkt_data
,
1024
,
0
,
\
if
((
length
=
recvfrom
(
client_sockfd
,
pkt_data
,
1024
,
0
,
\
(
struct
sockaddr
*
)
&
remote_addr
,
&
sin_size
))
==
-
1
)
(
struct
sockaddr
*
)
&
remote_addr
,
&
sin_size
))
==
-
1
)
{
{
fprintf
(
drcom_config
.
log_file
,
"recv kap1_2 timeout, retry %d!
\n
"
,
retry_cnt
++
);
log_info
(
logger
,
"recv kap1_2 timeout, retry %d!"
,
retry_cnt
++
);
fflush
(
drcom_config
.
log_file
);
}
}
else
else
{
{
...
@@ -222,10 +236,11 @@ HEART_BEAT_START:
...
@@ -222,10 +236,11 @@ HEART_BEAT_START:
}
}
}
}
retry_cnt
=
0
;
retry_cnt
=
0
;
fprintf
(
drcom_config
.
log_file
,
"==>[recieved kap1_2 response %d] len = %d
\n
"
,
\
// fprintf(drcom_config.log_file, "==>[recieved kap1_2 response %d] len = %d\n",\
kp1_cnt
,
length
);
// kp1_cnt, length);
fflush
(
drcom_config
.
log_file
);
// fflush(drcom_config.log_file);
print_as_hex
(
pkt_data
,
length
);
// print_as_hex(pkt_data,length);
pkt_data_log
(
"
\n
==> recieved"
,
pkt_data
,
length
);
kp1_cnt
++
;
kp1_cnt
++
;
retry_cnt
=
0
;
retry_cnt
=
0
;
...
@@ -239,10 +254,11 @@ HEART_BEAT_START:
...
@@ -239,10 +254,11 @@ HEART_BEAT_START:
length
=
make_keep_alive2_pkt1
(
pkt_data
,
kp2_cnt
,
ka2_flag
,
rand_num
,
ka2_key
);
length
=
make_keep_alive2_pkt1
(
pkt_data
,
kp2_cnt
,
ka2_flag
,
rand_num
,
ka2_key
);
sendto
(
client_sockfd
,
pkt_data
,
length
,
0
,
\
sendto
(
client_sockfd
,
pkt_data
,
length
,
0
,
\
(
struct
sockaddr
*
)
&
remote_addr
,
sizeof
(
remote_addr
));
(
struct
sockaddr
*
)
&
remote_addr
,
sizeof
(
remote_addr
));
fprintf
(
drcom_config
.
log_file
,
"<==[sended kap2_1 request %d] len = %d
\n
"
,
\
// fprintf(drcom_config.log_file, "<==[sended kap2_1 request %d] len = %d\n",\
kp2_cnt
,
length
);
// kp2_cnt, length);
fflush
(
drcom_config
.
log_file
);
// fflush(drcom_config.log_file);
print_as_hex
(
pkt_data
,
length
);
// print_as_hex(pkt_data, length);
pkt_data_log
(
"
\n
==> recieved"
,
pkt_data
,
length
);
if
(
retry_cnt
>
5
)
if
(
retry_cnt
>
5
)
{
{
...
@@ -252,28 +268,29 @@ HEART_BEAT_START:
...
@@ -252,28 +268,29 @@ HEART_BEAT_START:
if
((
length
=
recvfrom
(
client_sockfd
,
pkt_data
,
1024
,
0
,
\
if
((
length
=
recvfrom
(
client_sockfd
,
pkt_data
,
1024
,
0
,
\
(
struct
sockaddr
*
)
&
remote_addr
,
&
sin_size
))
==
-
1
)
(
struct
sockaddr
*
)
&
remote_addr
,
&
sin_size
))
==
-
1
)
{
{
fprintf
(
drcom_config
.
log_file
,
"recv kap2_1 timeout, retry %d!
\n
"
,
retry_cnt
++
);
log_info
(
logger
,
"recv kap2_1 timeout, retry %d!"
,
retry_cnt
++
);
fflush
(
drcom_config
.
log_file
);
}
}
else
else
{
{
if
(
pkt_data
[
0
]
==
0x07
&&
pkt_data
[
2
]
==
0x10
)
if
(
pkt_data
[
0
]
==
0x07
&&
pkt_data
[
2
]
==
0x10
)
{
{
memcpy
(
ka2_flag
,
pkt_data
+
6
,
2
);
memcpy
(
ka2_flag
,
pkt_data
+
6
,
2
);
fprintf
(
drcom_config
.
log_file
,
"==>[recieved kap2_1 response %d] len = %d
\n
"
,
\
// fprintf(drcom_config.log_file, "==>[recieved kap2_1 response %d] len = %d\n",\
kp2_cnt
,
length
);
// kp2_cnt, length);
fflush
(
drcom_config
.
log_file
);
// fflush(drcom_config.log_file);
print_as_hex
(
pkt_data
,
length
);
// print_as_hex(pkt_data, length);
pkt_data_log
(
"
\n
==> recieved"
,
pkt_data
,
length
);
kp2_cnt
++
;
kp2_cnt
++
;
continue
;
continue
;
}
}
break
;
break
;
}
}
}
}
fprintf
(
drcom_config
.
log_file
,
"==>[recieved kap2_1 response %d] len = %d
\n
"
,
\
// fprintf(drcom_config.log_file, "==>[recieved kap2_1 response %d] len = %d\n",\
kp2_cnt
,
length
);
// kp2_cnt, length);
fflush
(
drcom_config
.
log_file
);
// fflush(drcom_config.log_file);
print_as_hex
(
pkt_data
,
length
);
// print_as_hex(pkt_data,length);
pkt_data_log
(
"
\n
==> recieved"
,
pkt_data
,
length
);
memcpy
(
ka2_key
,
pkt_data
+
16
,
4
);
memcpy
(
ka2_key
,
pkt_data
+
16
,
4
);
kp2_cnt
++
;
kp2_cnt
++
;
...
@@ -282,10 +299,11 @@ HEART_BEAT_START:
...
@@ -282,10 +299,11 @@ HEART_BEAT_START:
length
=
make_keep_alive2_pkt2
(
pkt_data
,
kp2_cnt
,
ka2_flag
,
rand_num
,
ka2_key
,
host_ip
);
length
=
make_keep_alive2_pkt2
(
pkt_data
,
kp2_cnt
,
ka2_flag
,
rand_num
,
ka2_key
,
host_ip
);
sendto
(
client_sockfd
,
pkt_data
,
length
,
0
,
\
sendto
(
client_sockfd
,
pkt_data
,
length
,
0
,
\
(
struct
sockaddr
*
)
&
remote_addr
,
sizeof
(
remote_addr
));
(
struct
sockaddr
*
)
&
remote_addr
,
sizeof
(
remote_addr
));
fprintf
(
drcom_config
.
log_file
,
"<==[sended kap2_2 request %d] len = %d
\n
"
,
\
// fprintf(drcom_config.log_file, "<==[sended kap2_2 request %d] len = %d\n", \
kp2_cnt
,
length
);
// kp2_cnt, length);
fflush
(
drcom_config
.
log_file
);
// fflush(drcom_config.log_file);
print_as_hex
(
pkt_data
,
length
);
// print_as_hex(pkt_data, length);
pkt_data_log
(
"
\n
<== sended"
,
pkt_data
,
length
);
if
(
retry_cnt
>
5
)
if
(
retry_cnt
>
5
)
{
{
...
@@ -295,18 +313,18 @@ HEART_BEAT_START:
...
@@ -295,18 +313,18 @@ HEART_BEAT_START:
if
((
length
=
recvfrom
(
client_sockfd
,
pkt_data
,
1024
,
0
,
\
if
((
length
=
recvfrom
(
client_sockfd
,
pkt_data
,
1024
,
0
,
\
(
struct
sockaddr
*
)
&
remote_addr
,
&
sin_size
))
==
-
1
)
(
struct
sockaddr
*
)
&
remote_addr
,
&
sin_size
))
==
-
1
)
{
{
fprintf
(
drcom_config
.
log_file
,
"recv kap2_2 timeout, retry %d!
\n
"
,
retry_cnt
++
);
log_info
(
logger
,
"recv kap2_2 timeout, retry %d!"
,
retry_cnt
++
);
fflush
(
drcom_config
.
log_file
);
}
}
else
else
{
{
break
;
break
;
}
}
}
}
fprintf
(
drcom_config
.
log_file
,
"==>[recieved kap2_2 response %d] len = %d
\n
"
,
\
// fprintf(drcom_config.log_file, "==>[recieved kap2_2 response %d] len = %d\n",\
kp2_cnt
,
length
);
// kp2_cnt, length);
fflush
(
drcom_config
.
log_file
);
// fflush(drcom_config.log_file);
print_as_hex
(
pkt_data
,
length
);
// print_as_hex(pkt_data,length);
pkt_data_log
(
"
\n
==> recieved"
,
pkt_data
,
length
);
kp2_cnt
++
;
kp2_cnt
++
;
short_wait_sleep
(
17
);
short_wait_sleep
(
17
);
...
@@ -324,12 +342,33 @@ HEART_BEAT_START:
...
@@ -324,12 +342,33 @@ HEART_BEAT_START:
int
exit_auth
(
void
)
int
exit_auth
(
void
)
{
{
fprintf
(
drcom_config
.
log_file
,
"gdut-drcom heart-beat exiting!
\r\n
"
);
log_info
(
logger
,
"gdut-drcom heart-beat exiting!
"
);
fflush
(
drcom_config
.
log_file
);
Logger_free
(
logger
);
drcom_config
.
exit
=
1
;
drcom_config
.
exit
=
1
;
return
0
;
return
0
;
}
}
static
void
pkt_data_log
(
char
*
header
,
uint8_t
*
pktdata
,
int
len
)
{
static
char
log_buf
[
1024
];
int
i
,
index
=
0
;
strcpy
(
log_buf
,
header
);
index
+=
strlen
(
header
);
index
+=
sprintf
(
log_buf
+
index
,
" len: %d"
,
len
);
for
(
i
=
0
;
i
<
len
;
i
++
)
{
if
(
i
%
16
==
0
)
{
index
+=
sprintf
(
log_buf
+
index
,
"
\n
"
);
}
index
+=
sprintf
(
log_buf
+
index
,
"%02hhx "
,
*
(
pktdata
+
i
));
}
log_info
(
logger
,
log_buf
);
}
/*
static void print_as_hex(uint8_t *buf, int len)
static void print_as_hex(uint8_t *buf, int len)
{
{
int i;
int i;
...
@@ -342,6 +381,7 @@ static void print_as_hex(uint8_t *buf, int len)
...
@@ -342,6 +381,7 @@ static void print_as_hex(uint8_t *buf, int len)
fprintf(drcom_config.log_file, "\n\n");
fprintf(drcom_config.log_file, "\n\n");
fflush(drcom_config.log_file);
fflush(drcom_config.log_file);
}
}
*/
static
uint32_t
drcomCRC32
(
uint8_t
*
data
,
int
len
)
static
uint32_t
drcomCRC32
(
uint8_t
*
data
,
int
len
)
{
{
...
@@ -376,10 +416,6 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\
...
@@ -376,10 +416,6 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\
static
int
is_first
=
1
;
static
int
is_first
=
1
;
uint8_t
check_mode
=
seed
[
0
]
&
0x03
;
uint8_t
check_mode
=
seed
[
0
]
&
0x03
;
#ifdef DEBUG
fprintf
(
drcom_config
.
log_file
,
"check mode: %d
\n
"
,
check_mode
);
fflush
(
drcom_config
.
log_file
);
#endif
buf
[
index
++
]
=
0x07
;
//code
buf
[
index
++
]
=
0x07
;
//code
buf
[
index
++
]
=
cnt
;
//id
buf
[
index
++
]
=
cnt
;
//id
...
@@ -405,7 +441,6 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\
...
@@ -405,7 +441,6 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\
index
+=
4
;
index
+=
4
;
memcpy
(
buf
+
index
,
seed
,
4
);
memcpy
(
buf
+
index
,
seed
,
4
);
index
+=
4
;
index
+=
4
;
if
(
drcom_config
.
enable_crypt
==
0
)
if
(
drcom_config
.
enable_crypt
==
0
)
{
{
/*disable crypt*/
/*disable crypt*/
...
@@ -429,11 +464,6 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\
...
@@ -429,11 +464,6 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\
/*enable crypt*/
/*enable crypt*/
uint8_t
checksum
[
8
]
=
{
0
};
uint8_t
checksum
[
8
]
=
{
0
};
gen_ka1_checksum
(
checksum
,
seed
,
check_mode
);
gen_ka1_checksum
(
checksum
,
seed
,
check_mode
);
#ifdef DEBUG
fprintf
(
drcom_config
.
log_file
,
"checksum: "
);
fflush
(
drcom_config
.
log_file
);
print_as_hex
(
checksum
,
8
);
#endif
memcpy
(
buf
+
index
,
checksum
,
8
);
memcpy
(
buf
+
index
,
checksum
,
8
);
index
+=
8
;
index
+=
8
;
/*enable crypt*/
/*enable crypt*/
...
...
src/config.c
View file @
c4b1cd63
#include "config.h"
#include "config.h"
#include "logger.h"
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
...
@@ -25,19 +26,16 @@ static void strstrip(char *s, char *chars);
...
@@ -25,19 +26,16 @@ static void strstrip(char *s, char *chars);
void
set_remote_ip
(
char
*
ip
,
int
len
)
void
set_remote_ip
(
char
*
ip
,
int
len
)
{
{
memcpy
(
drcom_config
.
remote_ip
,
ip
,
len
);
memcpy
(
drcom_config
.
remote_ip
,
ip
,
len
);
printf
(
"set remote_ip = %s
\n
"
,
drcom_config
.
remote_ip
);
}
}
void
set_keep_alive1_flag
(
char
*
flag
,
int
len
)
void
set_keep_alive1_flag
(
char
*
flag
,
int
len
)
{
{
sscanf
(
flag
,
"%02hhx"
,
&
drcom_config
.
keep_alive1_flag
);
sscanf
(
flag
,
"%02hhx"
,
&
drcom_config
.
keep_alive1_flag
);
printf
(
"set keep_alive1_flag = %02hhx
\n
"
,
drcom_config
.
keep_alive1_flag
);
}
}
void
set_enable_crypt
(
int
enable
)
void
set_enable_crypt
(
int
enable
)
{
{
drcom_config
.
enable_crypt
=
enable
;
drcom_config
.
enable_crypt
=
enable
;
printf
(
"set enable_crypt = %d
\n
"
,
enable
);
}
}
void
get_version
(
char
*
version
)
void
get_version
(
char
*
version
)
...
@@ -47,10 +45,15 @@ void get_version(char* version)
...
@@ -47,10 +45,15 @@ void get_version(char* version)
void
set_log_file
(
char
*
log_file
,
int
len
)
void
set_log_file
(
char
*
log_file
,
int
len
)
{
{
FILE
*
logp
=
fopen
(
log_file
,
"a"
);
if
(
!
logger
)
{
logger
=
Logger_create
();
}
FILE
*
logp
=
fopen
(
log_file
,
"w"
);
if
(
logp
)
if
(
logp
)
{
{
drcom_config
.
log_file
=
logp
;
drcom_config
.
log_file
=
logp
;
logger
->
fp
=
logp
;
}
}
}
}
...
@@ -59,10 +62,6 @@ int parse_config(char *conf_file_name)
...
@@ -59,10 +62,6 @@ int parse_config(char *conf_file_name)
// char conf_file_name[256] = "/etc/gdut-drcom.conf";
// char conf_file_name[256] = "/etc/gdut-drcom.conf";
char
line_buf
[
1024
];
char
line_buf
[
1024
];
#ifdef DEBUG
fprintf
(
stdout
,
"conf_file_name = %s
\n
"
,
conf_file_name
);
fflush
(
stdout
);
#endif
FILE
*
fp
=
fopen
(
conf_file_name
,
"r"
);
FILE
*
fp
=
fopen
(
conf_file_name
,
"r"
);
if
(
fp
==
NULL
)
if
(
fp
==
NULL
)
...
@@ -75,7 +74,10 @@ int parse_config(char *conf_file_name)
...
@@ -75,7 +74,10 @@ int parse_config(char *conf_file_name)
while
(
!
feof
(
fp
))
while
(
!
feof
(
fp
))
{
{
memset
(
line_buf
,
0
,
sizeof
(
line_buf
));
memset
(
line_buf
,
0
,
sizeof
(
line_buf
));
fgets
(
line_buf
,
sizeof
(
line_buf
)
-
1
,
fp
);
if
(
fgets
(
line_buf
,
sizeof
(
line_buf
)
-
1
,
fp
)
==
NULL
)
{
continue
;
}
if
(
strlen
(
line_buf
)
>
0
)
if
(
strlen
(
line_buf
)
>
0
)
{
{
parse_line
(
line_buf
,
sizeof
(
line_buf
));
parse_line
(
line_buf
,
sizeof
(
line_buf
));
...
...
src/gdut-drcom.c
View file @
c4b1cd63
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
#include <getopt.h>
#include <getopt.h>
#include "auth.h"
#include "auth.h"
#include "config.h"
#include "config.h"
#include "logger.h"
static
void
print_help
(
char
*
name
);
static
void
print_help
(
char
*
name
);
...
@@ -12,6 +13,8 @@ int main(int argc, char *argv[])
...
@@ -12,6 +13,8 @@ int main(int argc, char *argv[])
char
conf_file_name
[
256
]
=
{
0
};
char
conf_file_name
[
256
]
=
{
0
};
drcom_config
.
log_file
=
stdout
;
drcom_config
.
log_file
=
stdout
;
logger
=
Logger_create
();
logger
->
level
=
LOG_DEBUG
;
if
(
argc
==
1
)
if
(
argc
==
1
)
{
{
...
@@ -80,10 +83,6 @@ int main(int argc, char *argv[])
...
@@ -80,10 +83,6 @@ int main(int argc, char *argv[])
// parse_config(conf_file_name);
// parse_config(conf_file_name);
// }
// }
#ifdef DEBUG
#ifdef DEBUG
fprintf
(
drcom_config
.
log_file
,
"drcom_config.remote_ip = %s
\n
"
,
drcom_config
.
remote_ip
);
fprintf
(
drcom_config
.
log_file
,
"drcom_config.remote_port = %d
\n
"
,
drcom_config
.
remote_port
);
fprintf
(
drcom_config
.
log_file
,
"drcom_config.keep_alive1_flag = %02hhx
\n
"
,
drcom_config
.
keep_alive1_flag
);
fprintf
(
drcom_config
.
log_file
,
"drcom_config.enable_crypt = %d
\n
"
,
drcom_config
.
enable_crypt
);
fflush
(
drcom_config
.
log_file
);
fflush
(
drcom_config
.
log_file
);
#endif
#endif
...
...
src/logger.c
View file @
c4b1cd63
#include "logger.h"
#include "logger.h"
#define LOGFILE_MAXSIZE (1024*1024)
Logger
*
logger
;
Logger
*
Logger_create
(
void
)
Logger
*
Logger_create
(
void
)
{
{
Logger
*
l
=
(
Logger
*
)
malloc
(
sizeof
(
Logger
));
Logger
*
l
=
(
Logger
*
)
malloc
(
sizeof
(
Logger
));
...
@@ -24,17 +28,26 @@ void Logger_free(Logger *l)
...
@@ -24,17 +28,26 @@ void Logger_free(Logger *l)
void
log_add
(
Logger
*
l
,
int
level
,
const
char
*
msg
)
void
log_add
(
Logger
*
l
,
int
level
,
const
char
*
msg
)
{
{
static
fpos_t
pos
;
if
(
level
<
l
->
level
)
return
;
if
(
level
<
l
->
level
)
return
;
time_t
meow
=
time
(
NULL
);
time_t
meow
=
time
(
NULL
);
char
buf
[
64
];
char
buf
[
64
];
strftime
(
buf
,
sizeof
(
buf
),
l
->
datetime_format
,
localtime
(
&
meow
));
strftime
(
buf
,
sizeof
(
buf
),
l
->
datetime_format
,
localtime
(
&
meow
));
fgetpos
(
l
->
fp
,
&
pos
);
if
(
pos
.
__pos
>
LOGFILE_MAXSIZE
)
{
rewind
(
l
->
fp
);
}
fprintf
(
l
->
fp
,
"[%d] %c, %s : %s
\n
"
,
fprintf
(
l
->
fp
,
"[%d] %c, %s : %s
\n
"
,
(
int
)
getpid
(),
(
int
)
getpid
(),
LOG_LEVEL_CHARS
[
level
],
LOG_LEVEL_CHARS
[
level
],
buf
,
buf
,
msg
);
msg
);
fflush
(
l
->
fp
);
}
}
void
log_debug
(
Logger
*
l
,
const
char
*
fmt
,
...)
void
log_debug
(
Logger
*
l
,
const
char
*
fmt
,
...)
...
...
src/logger.h
View file @
c4b1cd63
...
@@ -35,6 +35,8 @@ void log_info(Logger *l, const char *fmt, ...);
...
@@ -35,6 +35,8 @@ void log_info(Logger *l, const char *fmt, ...);
void
log_warn
(
Logger
*
l
,
const
char
*
fmt
,
...);
void
log_warn
(
Logger
*
l
,
const
char
*
fmt
,
...);
void
log_error
(
Logger
*
l
,
const
char
*
fmt
,
...);
void
log_error
(
Logger
*
l
,
const
char
*
fmt
,
...);
extern
Logger
*
logger
;
#ifdef __cplusplus
#ifdef __cplusplus
}
}
#endif
#endif
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment