05c4ca458602d5214fa77c3ba43941becc7e9afc
[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
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 } tIf;
21
22 // === PROTOTYPES ===
23  int    Net_int_Open(const char *Path);
24
25 // === GLOBALS ===
26 tIf     gaInterfaces[MAX_IFS];
27
28 // === CODE ===
29 int Net_Open(int IfNum, const char *Path)
30 {
31         if( IfNum >= MAX_IFS )
32                 return 1;
33         
34         if(gaInterfaces[IfNum].FD != 0) return 1;
35         gaInterfaces[IfNum].addrlen = sizeof(gaInterfaces[IfNum].addr);
36         gaInterfaces[IfNum].FD = Net_int_Open(Path);
37         return 0;
38 }
39
40 int Net_int_Open(const char *Path)
41 {
42         int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
43         struct sockaddr_un      sa = {AF_UNIX, ""};
44         strcpy(sa.sun_path, Path);
45         unlink(Path);
46         if( bind(fd, (struct sockaddr*)&sa, sizeof(sa)) ) {
47                 perror("NetTest_OpenUnix - bind");
48                 close(fd);
49                 return -1;
50         }
51         
52         return fd;
53 }
54
55 void Net_Close(int IfNum)
56 {
57         assert(IfNum < MAX_IFS);
58         close(gaInterfaces[IfNum].FD);
59         gaInterfaces[IfNum].FD = 0;
60 }
61
62 bool WaitOnFD(int FD, bool Write, unsigned int Timeout)
63 {
64         fd_set  fds;
65         FD_ZERO(&fds);
66         FD_SET(FD, &fds);
67         struct timeval  timeout;
68         timeout.tv_sec = Timeout/1000;
69         timeout.tv_usec = (Timeout%1000) * 1000;
70         
71         if( Write )
72                 select(FD+1, NULL, &fds, NULL, &timeout);
73         else
74                 select(FD+1, &fds, NULL, NULL, &timeout);
75         return FD_ISSET(FD, &fds);
76 }
77
78 bool Net_int_EnsureConnected(int IfNum)
79 {
80         assert(IfNum < MAX_IFS);
81         #if 0
82         if( gaInterface_Clients[IfNum] == 0 )
83         {
84                 //if( !WaitOnFD(gaInterface_Servers[IfNum], CONNECT_TIMEOUT) )
85                 //{
86                 //      fprintf(stderr, "ERROR: Client has not connected");
87                 //      return false;
88                 //}
89                 gaInterface_Clients[IfNum] = accept(gaInterface_Servers[IfNum], NULL, NULL);
90                 if( gaInterface_Clients[IfNum] < 0 ) {
91                         perror("Net_int_EnsureConnected - accept");
92                         return false;
93                 }
94         }
95         #endif
96         return true;
97 }
98
99 size_t Net_Receive(int IfNum, size_t MaxLen, void *DestBuf, unsigned int Timeout)
100 {
101         assert(IfNum < MAX_IFS);
102         tIf     *If = &gaInterfaces[IfNum];
103         
104         if( Net_int_EnsureConnected(IfNum) && WaitOnFD(If->FD, false, Timeout) )
105         {
106                 return recvfrom(If->FD, DestBuf, MaxLen, 0, &If->addr, &If->addrlen);
107         }
108         return 0;
109 }
110
111 void Net_Send(int IfNum, size_t Length, const void *Buf)
112 {
113         assert(IfNum < MAX_IFS);
114         tIf     *If = &gaInterfaces[IfNum];
115         
116         if( !WaitOnFD(If->FD, true, CONNECT_TIMEOUT) )
117                 return ;
118         int rv = sendto(If->FD, Buf, Length, 0, &If->addr, If->addrlen);
119         if( rv < 0 )
120                 perror("Net_Send - send");
121 }
122
123

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