2016-05-11 73 views
0

我有一個InPipe意味着讀取二進制數據,而OutPipe意味着寫回通過我的防火牆的二進制數據。命名管道只能讀取而不能寫入

/// The input named pipe, "ToFirewall" 
static FILE* InPipe = NULL; 


/// The output named pipe, "FromFirewall" 
static FILE* OutPipe = NULL; 

我在一個單獨的函數中打開兩個管道。

static bool OpenPipes(void) 
{ 
    //ToFirewall 
    InPipe = fopen("ToFirewall", "rb"); 
    if(InPipe == NULL) 
    { 
     perror("ERROR, failed to open pipe ToFirewall:"); 
     return false; 
    } 

    OutPipe = fopen("FromFirewall", "wb"); 
    if(OutPipe == NULL) 
    { 
     perror("ERROR, failed to open pipe FromFirewall:"); 

      return false; 
     } 

    return true; 
} 

出於某種原因,我在數據讀取,它會跳過寫,不會刻意去檢查它是否通過我的防火牆通過與否。我在網上看了一下,看了有關沖洗的解決方案,但沒有幫助。

static void* FilterThread(void* args) { 
    OpenPipes(); 

    unsigned char* buffer = malloc(1500); 

    int ret = fread(buffer, 1, 1500, InPipe); 
    if(ret){ 
     fclose(InPipe); 
    } 


    //Check is FilterPacket will allow the packet through the firewall 
    if(FilterPacket(buffer, args)) { 
     fwrite(buffer, 1, 60, OutPipe); 
     fflush(OutPipe); 
    } 

//  fflush(OutPipe); 
    fclose(OutPipe); 

    return NULL; 
} 

這裏是我的輸出

RCVR: opened file output.bin 
SNDR: Waiting 200ms between packets 
SNDR: Number of packets: 18 
SNDR: Starting packet 0 
SNDR: Starting packet 1 
SNDR: Starting packet 2 
SNDR: Starting packet 3 
SNDR: Starting packet 4 
SNDR: Starting packet 5 
SNDR: Starting packet 6 
SNDR: Starting packet 7 
SNDR: Starting packet 8 
SNDR: Starting packet 9 
SNDR: Starting packet 10 
SNDR: Starting packet 11 
SNDR: Starting packet 12 
SNDR: Starting packet 13 
SNDR: Starting packet 14 
SNDR: Starting packet 15 
SNDR: Starting packet 16 
SNDR: Starting packet 17 
SNDR: Finished, wrote 18 packets to the pipe 

在這裏你可以看到預期的輸出實際上應該看起來像

> RCVR: opened file output.bin 
SNDR: Waiting 200ms between packets 
SNDR: Number of packets: 18 
SNDR: Starting packet 0 
SNDR: Starting packet 1 
RCVR: 129.21.37.11 -> 74.125.21.103 
SNDR: Starting packet 2 
RCVR: 74.125.21.103 -> 129.21.37.11 
SNDR: Starting packet 3 
SNDR: Starting packet 4 
RCVR: 129.21.37.11 -> 74.125.21.103 
SNDR: Starting packet 5 
SNDR: Starting packet 6 
RCVR: 74.125.21.103 -> 129.21.37.11 
SNDR: Starting packet 7 
RCVR: 129.21.37.28 -> 74.125.21.103 
SNDR: Starting packet 8 
SNDR: Starting packet 9 
RCVR: 129.21.37.11 -> 74.125.21.103 
SNDR: Starting packet 10 
RCVR: 74.125.21.103 -> 129.21.37.28 
SNDR: Starting packet 11 
SNDR: Starting packet 12 
RCVR: 74.125.21.103 -> 129.21.37.11 
SNDR: Starting packet 13 
RCVR: 129.21.37.28 -> 74.125.21.103 
SNDR: Starting packet 14 
SNDR: Starting packet 15 
SNDR: Starting packet 16 
RCVR: 129.21.37.11 -> 74.125.21.103 
SNDR: Starting packet 17 
RCVR: 74.125.21.103 -> 129.21.37.28 
SNDR: Finished, wrote 18 packets to the pipe 
FwSim, Commanding firewall to Exit 
RCVR: 74.125.21.103 -> 129.21.37.11 
Exiting 
+1

能否請您嘗試創建一個[最小,完整,可驗證的示例](HTT p://stackoverflow.com/help/mcve)並向我們展示?您在程序中的哪個位置打印輸出?你有沒有檢查'fread'(還有'fwrite')實際返回的結果? –

+0

哦,你知道pipe是* streaming *,意思是它可能不會給你所有在一次讀取調用中要求的數據,你可能需要循環讀取。 –

+0

管道的另一端有什麼? – immibis

回答

0

讀取讀取多達1500個字節的數據,但你只寫了60個字節。您可能要解決這個問題:

fwrite(buffer, 1, ret, OutPipe); 

和寫只會與過濾器發生檢查返回true,所以要幫助調試,我建議增加一個日誌,當過濾器返回false:

if(FilterPacket(buffer, args)) { 
    fwrite(buffer, 1, ret, OutPipe); 
    fflush(OutPipe); 
} else { 
    fprintf(stderr, "FilterPacket returned false for packet %s\n", buffer); 
} 

此外,buffer當函數返回沒有被釋放,這將導致內存泄漏,這增加了函數的末尾:

free(buffer);