Makefile - Fix mtest target (and clean up utest targets)
[tpg/acess2.git] / Tools / NetTest_Runner / net.c
1 /*
2  */
3 #include <sys/socket.h>
4 #include <sys/un.h>
5 #include <string.h>
6 #include <stdbool.h>
7 #include <stdio.h>
8 #include <assert.h>
9 #include <sys/select.h>
10 #include "net.h"
11 #include <stdint.h>
12
13 #define CONNECT_TIMEOUT 10*1000
14 #define MAX_IFS 4
15
16 typedef struct {
17          int    FD;
18         socklen_t       addrlen;
19         struct sockaddr_un      addr;
20         FILE    *CapFP;
21 } tIf;
22
23 // === PROTOTYPES ===
24  int    Net_int_Open(const char *Path);
25
26 // === GLOBALS ===
27 tIf     gaInterfaces[MAX_IFS];
28
29 // === CODE ===
30 int Net_Open(int IfNum, const char *Path)
31 {
32         if( IfNum >= MAX_IFS )
33                 return 1;
34         
35         if(gaInterfaces[IfNum].FD != 0) return 1;
36         gaInterfaces[IfNum].addrlen = sizeof(gaInterfaces[IfNum].addr);
37         gaInterfaces[IfNum].FD = Net_int_Open(Path);
38         
39         char cappath[] = "testif00.pcap";
40         sprintf(cappath, "testif%i.pcap", IfNum);
41         gaInterfaces[IfNum].CapFP = fopen(cappath, "w");
42         {
43                 struct {
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 = {
52                         0xa1b2c3d4,
53                         2,4,
54                         0,
55                         0,
56                         65535,
57                         1
58                 };
59                 fwrite(&hdr, sizeof(hdr), 1, gaInterfaces[IfNum].CapFP);
60         }
61         return 0;
62 }
63
64 int Net_int_Open(const char *Path)
65 {
66         int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
67         struct sockaddr_un      sa = {AF_UNIX, ""};
68         strcpy(sa.sun_path, Path);
69         unlink(Path);
70         if( bind(fd, (struct sockaddr*)&sa, sizeof(sa)) ) {
71                 perror("NetTest_OpenUnix - bind");
72                 close(fd);
73                 return -1;
74         }
75         
76         return fd;
77 }
78
79 void Net_Close(int IfNum)
80 {
81         assert(IfNum < MAX_IFS);
82         close(gaInterfaces[IfNum].FD);
83         gaInterfaces[IfNum].FD = 0;
84         fclose(gaInterfaces[IfNum].CapFP);
85 }
86
87 bool WaitOnFD(int FD, bool Write, unsigned int Timeout)
88 {
89         fd_set  fds;
90         FD_ZERO(&fds);
91         FD_SET(FD, &fds);
92         struct timeval  timeout;
93         timeout.tv_sec = Timeout/1000;
94         timeout.tv_usec = (Timeout%1000) * 1000;
95         
96         if( Write )
97                 select(FD+1, NULL, &fds, NULL, &timeout);
98         else
99                 select(FD+1, &fds, NULL, NULL, &timeout);
100         return FD_ISSET(FD, &fds);
101 }
102
103 bool Net_int_EnsureConnected(int IfNum)
104 {
105         assert(IfNum < MAX_IFS);
106         #if 0
107         if( gaInterface_Clients[IfNum] == 0 )
108         {
109                 //if( !WaitOnFD(gaInterface_Servers[IfNum], CONNECT_TIMEOUT) )
110                 //{
111                 //      fprintf(stderr, "ERROR: Client has not connected");
112                 //      return false;
113                 //}
114                 gaInterface_Clients[IfNum] = accept(gaInterface_Servers[IfNum], NULL, NULL);
115                 if( gaInterface_Clients[IfNum] < 0 ) {
116                         perror("Net_int_EnsureConnected - accept");
117                         return false;
118                 }
119         }
120         #endif
121         return true;
122 }
123
124 void Net_int_SavePacket(tIf *If, size_t size, const void *data)
125 {
126         struct timeval  curtime;
127         gettimeofday(&curtime, NULL);
128         struct {
129                 uint32_t ts_sec;
130                 uint32_t ts_usec;
131                 uint32_t incl_len;
132                 uint32_t orig_len;
133         } __attribute__((packed)) hdr = {
134                 curtime.tv_sec, curtime.tv_usec,
135                 size, size
136         };
137         fwrite(&hdr, sizeof(hdr), 1, If->CapFP);
138         fwrite(data, size, 1, If->CapFP);
139 }
140
141 size_t Net_Receive(int IfNum, size_t MaxLen, void *DestBuf, unsigned int Timeout)
142 {
143         assert(IfNum < MAX_IFS);
144         tIf     *If = &gaInterfaces[IfNum];
145         
146         if( Net_int_EnsureConnected(IfNum) && WaitOnFD(If->FD, false, Timeout) )
147         {
148                 size_t rv = recvfrom(If->FD, DestBuf, MaxLen, 0, &If->addr, &If->addrlen);
149                 Net_int_SavePacket(If, rv, DestBuf);
150                 return rv;
151         }
152         return 0;
153 }
154
155 void Net_Send(int IfNum, size_t Length, const void *Buf)
156 {
157         assert(IfNum < MAX_IFS);
158         tIf     *If = &gaInterfaces[IfNum];
159         
160         if( !WaitOnFD(If->FD, true, CONNECT_TIMEOUT) )
161                 return ;
162         Net_int_SavePacket(If, Length, Buf);
163         int rv = sendto(If->FD, Buf, Length, 0, &If->addr, If->addrlen);
164         if( rv < 0 )
165                 perror("Net_Send - send");
166 }
167
168

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