3 #include <sys/socket.h>
9 #include <sys/select.h>
12 #include <unistd.h> // unlink/...
13 #include <sys/time.h> // gettimeofday
15 #define CONNECT_TIMEOUT 10*1000
21 struct sockaddr_un addr;
26 int Net_int_Open(const char *Path);
29 tIf gaInterfaces[MAX_IFS];
32 int Net_Open(int IfNum, const char *Path)
34 if( IfNum >= MAX_IFS )
37 if(gaInterfaces[IfNum].FD != 0) return 1;
38 gaInterfaces[IfNum].addrlen = sizeof(gaInterfaces[IfNum].addr);
39 gaInterfaces[IfNum].FD = Net_int_Open(Path);
41 char cappath[] = "testif00.pcap";
42 sprintf(cappath, "testif%i.pcap", IfNum);
43 gaInterfaces[IfNum].CapFP = fopen(cappath, "w");
46 uint32_t magic_number; /* magic number */
47 uint16_t version_major; /* major version number */
48 uint16_t version_minor; /* minor version number */
49 int32_t thiszone; /* GMT to local correction */
50 uint32_t sigfigs; /* accuracy of timestamps */
51 uint32_t snaplen; /* max length of captured packets, in octets */
52 uint32_t network; /* data link type */
53 } __attribute__((packed)) hdr = {
61 fwrite(&hdr, sizeof(hdr), 1, gaInterfaces[IfNum].CapFP);
66 int Net_int_Open(const char *Path)
68 int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
69 struct sockaddr_un sa = {AF_UNIX, ""};
70 strcpy(sa.sun_path, Path);
72 if( bind(fd, (struct sockaddr*)&sa, sizeof(sa)) ) {
73 perror("NetTest_OpenUnix - bind");
81 void Net_Close(int IfNum)
83 assert(IfNum < MAX_IFS);
84 close(gaInterfaces[IfNum].FD);
85 gaInterfaces[IfNum].FD = 0;
86 fclose(gaInterfaces[IfNum].CapFP);
89 bool WaitOnFD(int FD, bool Write, unsigned int Timeout)
94 struct timeval timeout;
95 timeout.tv_sec = Timeout/1000;
96 timeout.tv_usec = (Timeout%1000) * 1000;
99 select(FD+1, NULL, &fds, NULL, &timeout);
101 select(FD+1, &fds, NULL, NULL, &timeout);
102 return FD_ISSET(FD, &fds);
105 bool Net_int_EnsureConnected(int IfNum)
107 assert(IfNum < MAX_IFS);
109 if( gaInterface_Clients[IfNum] == 0 )
111 //if( !WaitOnFD(gaInterface_Servers[IfNum], CONNECT_TIMEOUT) )
113 // fprintf(stderr, "ERROR: Client has not connected");
116 gaInterface_Clients[IfNum] = accept(gaInterface_Servers[IfNum], NULL, NULL);
117 if( gaInterface_Clients[IfNum] < 0 ) {
118 perror("Net_int_EnsureConnected - accept");
126 void Net_int_SavePacket(tIf *If, size_t size, const void *data)
128 struct timeval curtime;
129 gettimeofday(&curtime, NULL);
135 } __attribute__((packed)) hdr = {
136 curtime.tv_sec, curtime.tv_usec,
139 fwrite(&hdr, sizeof(hdr), 1, If->CapFP);
140 fwrite(data, size, 1, If->CapFP);
143 size_t Net_Receive(int IfNum, size_t MaxLen, void *DestBuf, unsigned int Timeout)
145 assert(IfNum < MAX_IFS);
146 tIf *If = &gaInterfaces[IfNum];
148 if( Net_int_EnsureConnected(IfNum) && WaitOnFD(If->FD, false, Timeout) )
150 size_t rv = recvfrom(If->FD, DestBuf, MaxLen, 0, (struct sockaddr*)&If->addr, &If->addrlen);
151 Net_int_SavePacket(If, rv, DestBuf);
157 void Net_Send(int IfNum, size_t Length, const void *Buf)
159 assert(IfNum < MAX_IFS);
160 tIf *If = &gaInterfaces[IfNum];
162 if( !WaitOnFD(If->FD, true, CONNECT_TIMEOUT) )
164 Net_int_SavePacket(If, Length, Buf);
165 int rv = sendto(If->FD, Buf, Length, 0, (struct sockaddr*)&If->addr, If->addrlen);
167 perror("Net_Send - send");