Commit 1f40a557 authored by chenhaowen's avatar chenhaowen

add: option of log file path

parent 565bb5de
...@@ -145,8 +145,8 @@ int auth(void) ...@@ -145,8 +145,8 @@ int auth(void)
HEART_BEAT_START: HEART_BEAT_START:
fprintf(stdout, "gdut-drcom heart-beat started!\n\n"); fprintf(drcom_config.log_file, "gdut-drcom heart-beat started!\n\n");
fflush(stdout); 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));
...@@ -158,9 +158,9 @@ HEART_BEAT_START: ...@@ -158,9 +158,9 @@ 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(stdout, "<==[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(stdout); fflush(drcom_config.log_file);
print_as_hex(pkt_data, length); print_as_hex(pkt_data, length);
if (retry_cnt > 5) if (retry_cnt > 5)
...@@ -171,8 +171,8 @@ HEART_BEAT_START: ...@@ -171,8 +171,8 @@ 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(stdout, "recv kap1_1 timeout, retry %d!\n", retry_cnt++); fprintf(drcom_config.log_file, "recv kap1_1 timeout, retry %d!\n", retry_cnt++);
fflush(stdout); fflush(drcom_config.log_file);
} }
else else
{ {
...@@ -180,9 +180,9 @@ HEART_BEAT_START: ...@@ -180,9 +180,9 @@ HEART_BEAT_START:
} }
} }
retry_cnt = 0; retry_cnt = 0;
fprintf(stdout, "==>[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(stdout); fflush(drcom_config.log_file);
print_as_hex(pkt_data,length); print_as_hex(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);
...@@ -195,9 +195,9 @@ HEART_BEAT_START: ...@@ -195,9 +195,9 @@ 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(stdout, "<==[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(stdout); fflush(drcom_config.log_file);
print_as_hex(pkt_data, length); print_as_hex(pkt_data, length);
length = 0; length = 0;
...@@ -208,8 +208,8 @@ HEART_BEAT_START: ...@@ -208,8 +208,8 @@ 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(stdout, "recv kap1_2 timeout, retry %d!\n", retry_cnt++); fprintf(drcom_config.log_file, "recv kap1_2 timeout, retry %d!\n", retry_cnt++);
fflush(stdout); fflush(drcom_config.log_file);
} }
else else
{ {
...@@ -217,9 +217,9 @@ HEART_BEAT_START: ...@@ -217,9 +217,9 @@ HEART_BEAT_START:
} }
} }
retry_cnt = 0; retry_cnt = 0;
fprintf(stdout, "==>[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(stdout); fflush(drcom_config.log_file);
print_as_hex(pkt_data,length); print_as_hex(pkt_data,length);
kp1_cnt++; kp1_cnt++;
...@@ -234,9 +234,9 @@ HEART_BEAT_START: ...@@ -234,9 +234,9 @@ 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(stdout, "<==[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(stdout); fflush(drcom_config.log_file);
print_as_hex(pkt_data, length); print_as_hex(pkt_data, length);
if (retry_cnt > 5) if (retry_cnt > 5)
...@@ -247,17 +247,17 @@ HEART_BEAT_START: ...@@ -247,17 +247,17 @@ 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(stdout, "recv kap2_1 timeout, retry %d!\n", retry_cnt++); fprintf(drcom_config.log_file, "recv kap2_1 timeout, retry %d!\n", retry_cnt++);
fflush(stdout); 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(stdout, "==>[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(stdout); fflush(drcom_config.log_file);
print_as_hex(pkt_data, length); print_as_hex(pkt_data, length);
kp2_cnt++; kp2_cnt++;
continue; continue;
...@@ -265,9 +265,9 @@ HEART_BEAT_START: ...@@ -265,9 +265,9 @@ HEART_BEAT_START:
break; break;
} }
} }
fprintf(stdout, "==>[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(stdout); fflush(drcom_config.log_file);
print_as_hex(pkt_data,length); print_as_hex(pkt_data,length);
memcpy(ka2_key, pkt_data+16, 4); memcpy(ka2_key, pkt_data+16, 4);
kp2_cnt++; kp2_cnt++;
...@@ -277,9 +277,9 @@ HEART_BEAT_START: ...@@ -277,9 +277,9 @@ 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(stdout, "<==[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(stdout); fflush(drcom_config.log_file);
print_as_hex(pkt_data, length); print_as_hex(pkt_data, length);
if (retry_cnt > 5) if (retry_cnt > 5)
...@@ -290,17 +290,17 @@ HEART_BEAT_START: ...@@ -290,17 +290,17 @@ 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(stdout, "recv kap2_2 timeout, retry %d!\n", retry_cnt++); fprintf(drcom_config.log_file, "recv kap2_2 timeout, retry %d!\n", retry_cnt++);
fflush(stdout); fflush(drcom_config.log_file);
} }
else else
{ {
break; break;
} }
} }
fprintf(stdout, "==>[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(stdout); fflush(drcom_config.log_file);
print_as_hex(pkt_data,length); print_as_hex(pkt_data,length);
kp2_cnt++; kp2_cnt++;
...@@ -323,11 +323,11 @@ static void print_as_hex(uint8_t *buf, int len) ...@@ -323,11 +323,11 @@ static void print_as_hex(uint8_t *buf, int len)
for (i=0; i<len; i++) for (i=0; i<len; i++)
{ {
if (i%16 == 0 && i!=0) if (i%16 == 0 && i!=0)
fprintf(stdout, "\n"); fprintf(drcom_config.log_file, "\n");
fprintf(stdout, "%02hhx ", *(buf+i)); fprintf(drcom_config.log_file, "%02hhx ", *(buf+i));
} }
fprintf(stdout, "\n\n"); fprintf(drcom_config.log_file, "\n\n");
fflush(stdout); fflush(drcom_config.log_file);
} }
static uint32_t drcomCRC32(uint8_t *data, int len) static uint32_t drcomCRC32(uint8_t *data, int len)
...@@ -364,8 +364,8 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\ ...@@ -364,8 +364,8 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\
uint8_t check_mode = seed[0] & 0x03; uint8_t check_mode = seed[0] & 0x03;
#ifdef DEBUG #ifdef DEBUG
fprintf(stdout, "check mode: %d\n", check_mode); fprintf(drcom_config.log_file, "check mode: %d\n", check_mode);
fflush(stdout); fflush(drcom_config.log_file);
#endif #endif
buf[index++] = 0x07; //code buf[index++] = 0x07; //code
...@@ -417,8 +417,8 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\ ...@@ -417,8 +417,8 @@ static int make_keep_alive1_pkt2(uint8_t *buf, uint8_t *seed,\
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 #ifdef DEBUG
fprintf(stdout, "checksum: "); fprintf(drcom_config.log_file, "checksum: ");
fflush(stdout); fflush(drcom_config.log_file);
print_as_hex(checksum, 8); print_as_hex(checksum, 8);
#endif #endif
memcpy(buf+index, checksum, 8); memcpy(buf+index, checksum, 8);
......
...@@ -44,6 +44,15 @@ void get_version(char* version) ...@@ -44,6 +44,15 @@ void get_version(char* version)
memcpy(version, VERSION, strlen(VERSION)); memcpy(version, VERSION, strlen(VERSION));
} }
void set_log_file(char * log_file, int len)
{
FILE * logp = fopen(log_file, "w");
if (logp)
{
drcom_config.log_file = logp;
}
}
int parse_config(char *conf_file_name) 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";
......
#ifndef __CONFIG_H__ #ifndef __CONFIG_H__
#define __CONFIG_H__ #define __CONFIG_H__
#include <stdio.h>
enum CRYPT_TYPE { enum CRYPT_TYPE {
DISABLE_CRYPT=0, DISABLE_CRYPT=0,
ENABLE_CRYPT=1, ENABLE_CRYPT=1,
...@@ -12,6 +14,7 @@ struct config_s { ...@@ -12,6 +14,7 @@ struct config_s {
int remote_port; int remote_port;
unsigned char keep_alive1_flag; unsigned char keep_alive1_flag;
int enable_crypt; int enable_crypt;
FILE * log_file;
}; };
extern struct config_s drcom_config; extern struct config_s drcom_config;
...@@ -21,5 +24,6 @@ void set_remote_ip(char *ip, int len); ...@@ -21,5 +24,6 @@ void set_remote_ip(char *ip, int len);
void set_keep_alive1_flag(char *flag, int len); void set_keep_alive1_flag(char *flag, int len);
void set_enable_crypt(int enable); void set_enable_crypt(int enable);
void get_version(char * version); void get_version(char * version);
void set_log_file(char * log_file, int len);
#endif //__CONFIG_H__ #endif //__CONFIG_H__
...@@ -15,6 +15,8 @@ int main(int argc, char *argv[]) ...@@ -15,6 +15,8 @@ int main(int argc, char *argv[])
{ {
char conf_file_name[256] = {0}; char conf_file_name[256] = {0};
drcom_config.log_file = stdout;
if (argc == 1) if (argc == 1)
{ {
print_help(argv[0]); print_help(argv[0]);
...@@ -30,6 +32,7 @@ int main(int argc, char *argv[]) ...@@ -30,6 +32,7 @@ int main(int argc, char *argv[])
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{"config-file", required_argument, 0, 'c'}, {"config-file", required_argument, 0, 'c'},
{"version", no_argument, 0, 'v'}, {"version", no_argument, 0, 'v'},
{"log-file", required_argument, 0, 'l'},
{"remote-ip", required_argument, 0, 0}, {"remote-ip", required_argument, 0, 0},
{"remote-port", required_argument, 0, 1}, {"remote-port", required_argument, 0, 1},
{"keep-alive1-flag", required_argument, 0, 2}, {"keep-alive1-flag", required_argument, 0, 2},
...@@ -67,6 +70,9 @@ int main(int argc, char *argv[]) ...@@ -67,6 +70,9 @@ int main(int argc, char *argv[])
fprintf(stdout, "version: %s\n", VERSION); fprintf(stdout, "version: %s\n", VERSION);
exit(0); exit(0);
break; break;
case 'l':
set_log_file(optarg, strlen(optarg));
break;
case '?': case '?':
break; break;
default: default:
...@@ -78,11 +84,11 @@ int main(int argc, char *argv[]) ...@@ -78,11 +84,11 @@ int main(int argc, char *argv[])
// parse_config(conf_file_name); // parse_config(conf_file_name);
// } // }
#ifdef DEBUG #ifdef DEBUG
fprintf(stdout, "drcom_config.remote_ip = %s\n", drcom_config.remote_ip); fprintf(drcom_config.log_file, "drcom_config.remote_ip = %s\n", drcom_config.remote_ip);
fprintf(stdout, "drcom_config.remote_port = %d\n", drcom_config.remote_port); fprintf(drcom_config.log_file, "drcom_config.remote_port = %d\n", drcom_config.remote_port);
fprintf(stdout, "drcom_config.keep_alive1_flag = %02hhx\n", drcom_config.keep_alive1_flag); fprintf(drcom_config.log_file, "drcom_config.keep_alive1_flag = %02hhx\n", drcom_config.keep_alive1_flag);
fprintf(stdout, "drcom_config.enable_crypt = %d\n", drcom_config.enable_crypt); fprintf(drcom_config.log_file, "drcom_config.enable_crypt = %d\n", drcom_config.enable_crypt);
fflush(stdout); fflush(drcom_config.log_file);
#endif #endif
auth(); auth();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment