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

标签 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

最佳答案

读取最多读取 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/

相关文章:

c - 在标准 C 中分割字符串而不使用 strtok() 和 strsep()?

ruby - 在 bash 中打开一个命名管道,而不读取或写入它

c# - 异步使用 NamedPipeServerStream 和 NamedPipeClientStream

c - 合并排序算法中递归划分函数的目的是什么?

在 Windows 上将 valgrind 与 CLion 一起使用时,Clion 无法找到项目可执行文件

c++ - 有没有免费的 OCaml 到 C 的翻译器?

在C文件中执行嵌入式lua编译错误

C# 无法加载文件或程序集 : NuGet

.net - SQL 连接错误 : System. Data.SqlClient.SqlException (0x80131904)

windows - 命名管道名称可以有反斜杠吗?