forked from rchovan/npreal2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
161 lines (137 loc) · 3.11 KB
/
misc.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <string.h>
#include "misc.h"
#include "npreal2d.h"
//TODO: shared misc library is added into project. Some other shared code should remove into this library for reusibility.
void _log_event_backup(char *log_pathname, char *msg)
{
#define MAX_BACKUP_FILE 16
#define MAX_LOG_SIZE 10485760L
FILE * fd;
time_t t;
struct tm *tt;
char tmp[256];
unsigned long sz = 0;
static int bak_no = 0;
t = time(0);
tt = localtime(&t);
/*
* Open Log file as append mode.
*/
fd = fopen(log_pathname, "a+");
if ( fd )
{
sprintf(tmp, "%02d-%02d-%4d %02d:%02d:%02d ",
tt->tm_mon + 1, tt->tm_mday, tt->tm_year+1900,
tt->tm_hour, tt->tm_min, tt->tm_sec);
fputs(tmp, fd);
fputs(msg, fd);
fputs("\n", fd);
fseek(fd, 0L, SEEK_END);
sz = ftell(fd);
fclose(fd);
if(sz > (MAX_LOG_SIZE)){
//TODO: Solve strange problem that I call below command and get program crash.
//sprintf(tmp, "mv --backup= %s.bak %s.bak.old", EventLog, EventLog);
//system(tmp);
//if( bak_no==0 ){
{
int f_no=1;
FILE * bak_fd;
struct stat st_last={0};
struct stat st_curr;
// Look for the available backup number to save.
while(1){
sprintf(tmp, "%s.~%d~", log_pathname, f_no);
bak_fd = fopen(tmp, "r");
if( bak_fd==NULL ){
bak_no = f_no;
break;
}
fclose(bak_fd);
stat( tmp, &st_curr );
if( st_curr.st_mtime < st_last.st_mtime ){
bak_no = f_no;
break;
}
st_last = st_curr;
f_no++;
if( f_no>MAX_BACKUP_FILE ){
f_no = 1;
}
}
}
//sprintf(tmp, "cp %s %s.~%d~", EventLog, EventLog, bak_no++);
sprintf(tmp, "cp %s %s.~%d~", log_pathname, log_pathname, bak_no);
system(tmp);
sprintf(tmp, "rm -rf %s", log_pathname);
system(tmp);
//if( bak_no>MAX_BACKUP_FILE ){
// bak_no = 1;
//}
}
}
}
int ipv4_str_to_ip(char *str, ulong *ip)
{
int i;
unsigned long m;
/* if is space, I will save as 0xFFFFFFFF */
*ip = 0xFFFFFFFFL;
for (i = 0; i < 4; i++)
{
if ((*str < '0') || (*str > '9'))
return NP_RET_ERROR;
m = *str++ - '0';
if ((*str >= '0') && (*str <= '9'))
{
m = m * 10;
m += (*str++ - '0');
if ((*str >= '0') && (*str <= '9'))
{
m = m * 10;
m += (*str++ - '0');
if ((*str >= '0') && (*str <= '9'))
return NP_RET_ERROR;
}
}
if (m > 255)
return NP_RET_ERROR;
if ((*str++ != '.') && (i < 3))
return NP_RET_ERROR;
m <<= (i * 8);
if (i == 0)
m |= 0xFFFFFF00L;
else if ( i == 1 )
m |= 0xFFFF00FFL;
else if ( i == 2 )
m |= 0xFF00FFFFL;
else
m |= 0x00FFFFFFL;
*ip &= m;
}
return NP_RET_SUCCESS;
}
int ipv6_str_to_ip(char *str, unsigned char *ip)
{
int i;
char tmp[IP6_ADDR_LEN + 1];
memset(ip, 0x0, 16);
for (i = 0; i < IP6_ADDR_LEN; i++, str++)
{
if (((*str >= '0') && (*str <= '9')) ||
((*str >= 'a') && (*str <= 'f')) ||
((*str >= 'A') && (*str <= 'F')) || (*str == ':'))
tmp[i] = *str;
else
break;
}
tmp[i] = '\0';
if (!inet_pton(AF_INET6, tmp, ip))
return NP_RET_ERROR;
return NP_RET_SUCCESS;
}