2017-04-19 106 views
0

我有一點問題。只有當相同的字符串/字試圖寫入時,我纔想寫入文件。在我的情況下,它的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

主要的第一行()有沒有被定義的類型聲明:「uci_context *」。第二行有一個未定義的函數'uci_init()'。到目前爲止,這對你的問題看起來不太好:( – ThingyWotsit

+0

uci_init()工作得很好:D。我需要它來獲取一些數據 –

+1

你好,歡迎來到StackOverflow。請花一些時間閱讀幫助頁面,特別是名爲[「我可以在這裏問什麼問題?「](http://stackoverflow.com/help/on-topic)和[」我應該避免問什麼類型的問題?「](http://stackoverflow.com/help/dont-ask)。更重要的是,請閱讀[Stack Overflow問題清單](http://meta.stackexchange.com/q/156810/204922)。您可能還想了解[最小,完整和可驗證的示例](http:// stackoverflow .com/help/mcve) –

回答

0

它仍然寫入文件的原因是您以逐行方式檢查文件。

假設你的文件有:

193.2.2.1:22 
193.2.2.1:80 

,並要檢查193.2.2.1:22193.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); 
} 
+0

OMG this brilliant。Thanks Alooooooot –

相關問題