Tools/NetTest - Fixed ip checksum, pcap packet trace, cleanup
[tpg/acess2.git] / Tools / NetTest_Runner / net.c
index 05c4ca4..231df50 100644 (file)
@@ -8,15 +8,16 @@
 #include <assert.h>
 #include <sys/select.h>
 #include "net.h"
-
+#include <stdint.h>
 
 #define CONNECT_TIMEOUT        10*1000
 #define MAX_IFS        4
 
 typedef struct {
-       int     FD;
+        int    FD;
        socklen_t       addrlen;
        struct sockaddr_un      addr;
+       FILE    *CapFP;
 } tIf;
 
 // === PROTOTYPES ===
@@ -34,6 +35,29 @@ int Net_Open(int IfNum, const char *Path)
        if(gaInterfaces[IfNum].FD != 0) return 1;
        gaInterfaces[IfNum].addrlen = sizeof(gaInterfaces[IfNum].addr);
        gaInterfaces[IfNum].FD = Net_int_Open(Path);
+       
+       char cappath[] = "testif00.pcap";
+       sprintf(cappath, "testif%i.pcap", IfNum);
+       gaInterfaces[IfNum].CapFP = fopen(cappath, "w");
+       {
+               struct {
+                       uint32_t magic_number;   /* magic number */
+                       uint16_t version_major;  /* major version number */
+                       uint16_t version_minor;  /* minor version number */
+                        int32_t  thiszone;       /* GMT to local correction */
+                       uint32_t sigfigs;        /* accuracy of timestamps */
+                       uint32_t snaplen;        /* max length of captured packets, in octets */
+                       uint32_t network;        /* data link type */
+               } __attribute__((packed)) hdr = {
+                       0xa1b2c3d4,
+                       2,4,
+                       0,
+                       0,
+                       65535,
+                       1
+               };
+               fwrite(&hdr, sizeof(hdr), 1, gaInterfaces[IfNum].CapFP);
+       }
        return 0;
 }
 
@@ -57,6 +81,7 @@ void Net_Close(int IfNum)
        assert(IfNum < MAX_IFS);
        close(gaInterfaces[IfNum].FD);
        gaInterfaces[IfNum].FD = 0;
+       fclose(gaInterfaces[IfNum].CapFP);
 }
 
 bool WaitOnFD(int FD, bool Write, unsigned int Timeout)
@@ -96,6 +121,23 @@ bool Net_int_EnsureConnected(int IfNum)
        return true;
 }
 
+void Net_int_SavePacket(tIf *If, size_t size, const void *data)
+{
+       struct timeval  curtime;
+       gettimeofday(&curtime, NULL);
+       struct {
+               uint32_t ts_sec;
+               uint32_t ts_usec;
+               uint32_t incl_len;
+               uint32_t orig_len;
+       } __attribute__((packed)) hdr = {
+               curtime.tv_sec, curtime.tv_usec,
+               size, size
+       };
+       fwrite(&hdr, sizeof(hdr), 1, If->CapFP);
+       fwrite(data, size, 1, If->CapFP);
+}
+
 size_t Net_Receive(int IfNum, size_t MaxLen, void *DestBuf, unsigned int Timeout)
 {
        assert(IfNum < MAX_IFS);
@@ -103,7 +145,9 @@ size_t Net_Receive(int IfNum, size_t MaxLen, void *DestBuf, unsigned int Timeout
        
        if( Net_int_EnsureConnected(IfNum) && WaitOnFD(If->FD, false, Timeout) )
        {
-               return recvfrom(If->FD, DestBuf, MaxLen, 0, &If->addr, &If->addrlen);
+               size_t rv = recvfrom(If->FD, DestBuf, MaxLen, 0, &If->addr, &If->addrlen);
+               Net_int_SavePacket(If, rv, DestBuf);
+               return rv;
        }
        return 0;
 }
@@ -115,6 +159,7 @@ void Net_Send(int IfNum, size_t Length, const void *Buf)
        
        if( !WaitOnFD(If->FD, true, CONNECT_TIMEOUT) )
                return ;
+       Net_int_SavePacket(If, Length, Buf);
        int rv = sendto(If->FD, Buf, Length, 0, &If->addr, If->addrlen);
        if( rv < 0 )
                perror("Net_Send - send");

UCC git Repository :: git.ucc.asn.au