命名管道只是阅读而不是写

时间:2016-05-11 06:03:22

标签: c named-pipes

我有一个用于读取二进制数据的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 个答案:

答案 0 :(得分: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);