gpt4 book ai didi

c - 命名管道只能读不能写

转载 作者:行者123 更新时间:2023-11-30 17:01:16 25 4
gpt4 key购买 nike

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

关于c - 命名管道只能读不能写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37154227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com