2016-05-11 10 views
0

Ich habe ein InPipe für das Einlesen von Binärdaten und ein OutPipe für das Zurückschreiben der Binärdaten, die meine Firewall passieren.Named Pipe liest nur und schreibt nicht

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


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

Ich öffne beide Rohre in einer separaten Funktion.

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; 
} 

Aus irgendeinem Grunde, wie ich in Daten lesen, es wird über die Schreib überspringt und nicht stört, wenn es durch meinen Firewall oder nicht bestanden zu überprüfen. Ich schaute online und ich las Lösungen über Spülen, aber es half nicht.

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; 
} 

Hier ist mein Ausgang

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 

Und hier können Sie sehen, was die erwartete Ausgabe wie eigentlich

> 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

können Sie versuchen, wählen Sie bitte eine [Minimal, vollständig und prüfbare Beispiel] (htt zu erstellen p: //stackoverflow.com/help/mcve) und zeigen Sie uns? Wo im Programm druckst du die Ausgabe? Hast du überprüft, was "fread" (und auch 'fwrite') tatsächlich zurückgibt? –

+0

Oh, und Sie wissen, dass Pipes * streaming * sind, was bedeutet, dass Sie möglicherweise nicht alle Daten erhalten, die Sie in einem Leseaufruf verlangen, Sie müssen möglicherweise in einer Schleife lesen. –

+0

Was ist am anderen Ende der Rohre? – immibis

Antwort

0

Die Lese liest bis zu 1500 Byte Daten aussehen soll, aber Sie waren nur 60 Bytes geschrieben. Vielleicht möchten Sie dieses Problem beheben:

fwrite(buffer, 1, ret, OutPipe); 

Und die Schreib nur mit dem Filter überprüft true zurück, so zu helfen, Debug passieren, schlage ich ein Protokoll hinzufügen, wenn der Filter false zurück:

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

auch ist buffer nicht freigegeben, wenn die Funktion zurückkehrt, wird diese Speicherleck verursachen, fügen Sie diese bis zum Ende der Funktion:

free(buffer); 
Verwandte Themen