我有一点问题。我想写一个文件只有当我尝试编写的相同字符串/单词已经存在时才会写入文件。在我的情况下,它的IP地址和端口用":"分隔。如果我手动写入文件,例如193. 。。:80和193。。。:22,它决定了它们但仍然写到文件。我的错误在哪里:
#ifdef DEBUG
#define INITIAL_ALLOC 2
#else
#define INITIAL_ALLOC 512
#endif
void BlockIP(char * s);
char *read_line(FILE *fin);
void *right_line(int p,char * s,int ssh,int http);
int main(int argc, char const *argv[])
{
//struct uci_context *uci;
//uci = uci_init();
char *ip = "193.2.2.1 193.2.6.6 193.168.1.1 193.5.5.5"
//int uci_port = atoi(ucix_get_option(uci, "pack_mon", "manual_blocking", "port"));
const char s[2] = " ";
char *token;
token = strtok(ip, s);
while( token != NULL )
{
BlockIP(token);
token = strtok(NULL, s);
}
return(0);
}
void BlockIP(char * s){
int ssh = 22;
int http = 80;
char sshbuffer[2000]= "\0";
char httpbuffer[2000]= "\0";
char cmd2[2000] = "\0";
char cmd[2000] = "\0";
snprintf(cmd, sizeof(cmd),"iptables -I INPUT 1 -p tcp -s %s --dport 80 -j REJECT",s);
snprintf(cmd2, sizeof(cmd2),"iptables -I INPUT 1 -p tcp -s %s --dport 22 -j REJECT",s);
snprintf(sshbuffer, sizeof(sshbuffer),"%s:%d", s,ssh);
snprintf(httpbuffer, sizeof(httpbuffer),"%s:%d", s,http);
FILE *fp ,*ft;
fp = popen(cmd, "r");
pclose(fp);
ft = popen(cmd2, "r");
pclose(ft);
FILE *fin;
char *line;
fin = fopen("/tmp/mymonlog", "r");
if ( fin ) {
while ( line = read_line(fin) ) {
if (strstr(line, sshbuffer)){
printf("Already exist %s\n",line);
}else{
right_line(1,s,ssh,http);
}
if(strstr(line, httpbuffer)){
printf("Already exist %s\n",line);
}else{
right_line(2,s,ssh,http);
}
free(line);
}
}
fclose(fin);
}
char *read_line(FILE *fin) {
char *buffer;
char *tmp;
int read_chars = 0;
int bufsize = INITIAL_ALLOC;
char *line = malloc(bufsize);
if ( !line ) {
return NULL;
}
buffer = line;
while ( fgets(buffer, bufsize - read_chars, fin) ) {
read_chars = strlen(line);
if ( line[read_chars - 1] == '\n' ) {
line[read_chars - 1] = '\0';
return line;
}
else {
bufsize = 2 * bufsize;
tmp = realloc(line, bufsize);
if ( tmp ) {
line = tmp;
buffer = line + read_chars;
}
else {
free(line);
return NULL;
}
}
}
return NULL;
}
void *right_line(int p,char * s,int ssh,int http){
FILE *pFile,*tFile;
if(p == 1){
pFile=fopen("/tmp/mymonlog", "a");
if(pFile==NULL) {
perror("Error opening file.");
}else {
fprintf(pFile, "%s:%d\n", s,ssh);
}
fclose(pFile);
}else if(p == 2){
tFile=fopen("/tmp/mymonlog", "a");
if(tFile==NULL) {
perror("Error opening file.");
}
else {
fprintf(tFile, "%s:%d\n", s,http);
}
fclose(tFile);
}
}
答案 0 :(得分:1)
它仍然写入文件的原因是您以逐行方式检查文件。
假设您的文件包含:
193.2.2.1:22
193.2.2.1:80
您要检查193.2.2.1:22
和193.2.2.1:80
首先,您阅读193.2.2.1:22
行并执行
strstr("193.2.2.1:22", "193.2.2.1:22")
这是匹配的,因此不会写入文件。
但是你做了
strstr("193.2.2.1:22", "193.2.2.1:80")
不匹配,因此将写入该文件。
然后您从文件中读取下一行193.2.2.1:80
并执行
strstr("193.2.2.1:80", "193.2.2.1:22")
不匹配,因此将写入该文件。
然后你做
strstr("193.2.2.1:80", "193.2.2.1:80")
这是匹配的,因此不会写入文件。
现在您的文件是:
193.2.2.1:22
193.2.2.1:80
193.2.2.1:80
193.2.2.1:22
<强>结论:强>
不要逐行检查。在决定写入文件之前,您需要检查文件中的所有行。
一个简单的修复可能是:
int save_ssh = 1;
int save_http = 1;
if ( fin ) {
while ( line = read_line(fin) ) {
if (strstr(line, sshbuffer)){
printf("Already exist %s\n",line);
save_ssh = 0;
}
if(strstr(line, httpbuffer)){
printf("Already exist %s\n",line);
save_http = 0;
}
free(line);
}
fclose(fin);
if (save_ssh) right_line(1,s,ssh,http);
if (save_http) right_line(2,s,ssh,http);
}