#include <assert.h>
#include <sys/select.h>
#include "net.h"
-
+#include <stdint.h>
+#include <unistd.h> // unlink/...
+#include <sys/time.h> // gettimeofday
#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 ===
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;
}
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)
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);
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, (struct sockaddr*)&If->addr, &If->addrlen);
+ Net_int_SavePacket(If, rv, DestBuf);
+ return rv;
}
return 0;
}
if( !WaitOnFD(If->FD, true, CONNECT_TIMEOUT) )
return ;
- int rv = sendto(If->FD, Buf, Length, 0, &If->addr, If->addrlen);
+ Net_int_SavePacket(If, Length, Buf);
+ int rv = sendto(If->FD, Buf, Length, 0, (struct sockaddr*)&If->addr, If->addrlen);
if( rv < 0 )
perror("Net_Send - send");
}