簡體   English   中英

命名管道僅在讀取而未在寫入

[英]Named Pipe is only reading and not writing

我有一個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

讀取最多可讀取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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM