3 #include <sys/socket.h>
9 #include <sys/select.h>
13 #define CONNECT_TIMEOUT 10*1000
19 struct sockaddr_un addr;
24 int Net_int_Open(const char *Path);
27 tIf gaInterfaces[MAX_IFS];
30 int Net_Open(int IfNum, const char *Path)
32 if( IfNum >= MAX_IFS )
35 if(gaInterfaces[IfNum].FD != 0) return 1;
36 gaInterfaces[IfNum].addrlen = sizeof(gaInterfaces[IfNum].addr);
37 gaInterfaces[IfNum].FD = Net_int_Open(Path);
39 char cappath[] = "testif00.pcap";
40 sprintf(cappath, "testif%i.pcap", IfNum);
41 gaInterfaces[IfNum].CapFP = fopen(cappath, "w");
44 uint32_t magic_number; /* magic number */
45 uint16_t version_major; /* major version number */
46 uint16_t version_minor; /* minor version number */
47 int32_t thiszone; /* GMT to local correction */
48 uint32_t sigfigs; /* accuracy of timestamps */
49 uint32_t snaplen; /* max length of captured packets, in octets */
50 uint32_t network; /* data link type */
51 } __attribute__((packed)) hdr = {
59 fwrite(&hdr, sizeof(hdr), 1, gaInterfaces[IfNum].CapFP);
64 int Net_int_Open(const char *Path)
66 int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
67 struct sockaddr_un sa = {AF_UNIX, ""};
68 strcpy(sa.sun_path, Path);
70 if( bind(fd, (struct sockaddr*)&sa, sizeof(sa)) ) {
71 perror("NetTest_OpenUnix - bind");
79 void Net_Close(int IfNum)
81 assert(IfNum < MAX_IFS);
82 close(gaInterfaces[IfNum].FD);
83 gaInterfaces[IfNum].FD = 0;
84 fclose(gaInterfaces[IfNum].CapFP);
87 bool WaitOnFD(int FD, bool Write, unsigned int Timeout)
92 struct timeval timeout;
93 timeout.tv_sec = Timeout/1000;
94 timeout.tv_usec = (Timeout%1000) * 1000;
97 select(FD+1, NULL, &fds, NULL, &timeout);
99 select(FD+1, &fds, NULL, NULL, &timeout);
100 return FD_ISSET(FD, &fds);
103 bool Net_int_EnsureConnected(int IfNum)
105 assert(IfNum < MAX_IFS);
107 if( gaInterface_Clients[IfNum] == 0 )
109 //if( !WaitOnFD(gaInterface_Servers[IfNum], CONNECT_TIMEOUT) )
111 // fprintf(stderr, "ERROR: Client has not connected");
114 gaInterface_Clients[IfNum] = accept(gaInterface_Servers[IfNum], NULL, NULL);
115 if( gaInterface_Clients[IfNum] < 0 ) {
116 perror("Net_int_EnsureConnected - accept");
124 void Net_int_SavePacket(tIf *If, size_t size, const void *data)
126 struct timeval curtime;
127 gettimeofday(&curtime, NULL);
133 } __attribute__((packed)) hdr = {
134 curtime.tv_sec, curtime.tv_usec,
137 fwrite(&hdr, sizeof(hdr), 1, If->CapFP);
138 fwrite(data, size, 1, If->CapFP);
141 size_t Net_Receive(int IfNum, size_t MaxLen, void *DestBuf, unsigned int Timeout)
143 assert(IfNum < MAX_IFS);
144 tIf *If = &gaInterfaces[IfNum];
146 if( Net_int_EnsureConnected(IfNum) && WaitOnFD(If->FD, false, Timeout) )
148 size_t rv = recvfrom(If->FD, DestBuf, MaxLen, 0, &If->addr, &If->addrlen);
149 Net_int_SavePacket(If, rv, DestBuf);
155 void Net_Send(int IfNum, size_t Length, const void *Buf)
157 assert(IfNum < MAX_IFS);
158 tIf *If = &gaInterfaces[IfNum];
160 if( !WaitOnFD(If->FD, true, CONNECT_TIMEOUT) )
162 Net_int_SavePacket(If, Length, Buf);
163 int rv = sendto(If->FD, Buf, Length, 0, &If->addr, If->addrlen);
165 perror("Net_Send - send");