3bef3d3e65030f687ced30a7085dc4fec924b248
[tpg/acess2.git] / AcessNative / ld-acess_src / request.c
1 /*
2  */
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6 #ifdef __WIN32__
7 # include <windows.h>
8 # include <winsock.h>
9 #else
10 # include <unistd.h>
11 # include <sys/socket.h>
12 # include <netinet/in.h>
13 #endif
14 #include "request.h"
15 #include "../syscalls.h"
16
17 #define USE_TCP 0
18
19 // === PROTOTYPES ===
20 void    SendData(void *Data, int Length);
21  int    ReadData(void *Dest, int MaxLen, int Timeout);
22
23 // === GLOBALS ===
24 #ifdef __WIN32__
25 WSADATA gWinsock;
26 SOCKET  gSocket = INVALID_SOCKET;
27 #else
28 # define INVALID_SOCKET -1
29  int    gSocket = INVALID_SOCKET;
30 #endif
31 // Client ID to pass to server
32 // TODO: Implement such that each thread gets a different one
33  int    giSyscall_ClientID = 0;
34 struct sockaddr_in      gSyscall_ServerAddr;
35
36 // === CODE ===
37 int _InitSyscalls()
38 {
39         
40         #ifdef __WIN32__
41         /* Open windows connection */
42         if (WSAStartup(0x0101, &gWinsock) != 0)
43         {
44                 fprintf(stderr, "Could not open Windows connection.\n");
45                 exit(0);
46         }
47         #endif
48         
49         #if USE_TCP
50         // Open TCP Connection
51         gSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
52         #else
53         // Open UDP Connection
54         gSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
55         #endif
56         if (gSocket == INVALID_SOCKET)
57         {
58                 fprintf(stderr, "Could not create socket.\n");
59                 #if __WIN32__
60                 WSACleanup();
61                 #endif
62                 exit(0);
63         }
64         
65         // Set server address
66         memset((void *)&gSyscall_ServerAddr, '\0', sizeof(struct sockaddr_in));
67         gSyscall_ServerAddr.sin_family = AF_INET;
68         gSyscall_ServerAddr.sin_port = htons(SERVER_PORT);
69         gSyscall_ServerAddr.sin_addr.s_addr = htonl(0x7F000001);
70         
71         #if 0
72         // Set client address
73         memset((void *)&client, '\0', sizeof(struct sockaddr_in));
74         client.sin_family = AF_INET;
75         client.sin_port = htons(0);
76         client.sin_addr.s_addr = htonl(0x7F000001);
77         #endif
78         
79         #if USE_TCP
80         if( connect(gSocket, (struct sockaddr *)&gSyscall_ServerAddr, sizeof(struct sockaddr_in)) < 0 )
81         {
82                 fprintf(stderr, "Cannot connect to server (localhost:%i)\n", SERVER_PORT);
83                 perror("_InitSyscalls");
84                 #if __WIN32__
85                 closesocket(gSocket);
86                 WSACleanup();
87                 #else
88                 close(gSocket);
89                 #endif
90                 exit(0);
91         }
92         #endif
93         
94         #if 0
95         // Bind
96         if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 )
97         {
98                 fprintf(stderr, "Cannot bind address to socket.\n");
99                 #if __WIN32__
100                 closesocket(gSocket);
101                 WSACleanup();
102                 #else
103                 close(gSocket);
104                 #endif
105                 exit(0);
106         }
107         #endif
108         
109         #if !USE_TCP
110         // Ask server for a client ID
111         {
112                 tRequestHeader  req;
113                  int    len;
114                 req.ClientID = 0;
115                 req.CallID = 0;
116                 req.NParams = 0;
117                 
118                 SendData(&req, sizeof(req));
119                 
120                 len = ReadData(&req, sizeof(req), 5);
121                 if( len == 0 ) {
122                         fprintf(stderr, "Unable to connect to server (localhost:%i)\n", SERVER_PORT);
123                         exit(-1);
124                 }
125                 
126                 giSyscall_ClientID = req.ClientID;
127         }
128         #endif
129         
130         return 0;
131 }
132
133 int SendRequest(tRequestHeader *Request, int RequestSize)
134 {
135         if( gSocket == INVALID_SOCKET )
136         {
137                 _InitSyscalls();                
138         }
139         
140         // Set header
141         Request->ClientID = giSyscall_ClientID;
142         
143         #if 0
144         {
145                 for(i=0;i<RequestSize;i++)
146                 {
147                         printf("%02x ", ((uint8_t*)Request)[i]);
148                         if( i % 16 == 15 )      printf("\n");
149                 }
150                 printf("\n");
151         }
152         #endif
153         
154         // Send it off
155         SendData(Request, RequestSize);
156         
157         // Wait for a response (no timeout)
158         return ReadData(Request, RequestSize, -1);
159 }
160
161 void SendData(void *Data, int Length)
162 {
163          int    len;
164         
165         #if USE_TCP
166         len = send(Data, Length, 0);
167         #else
168         len = sendto(gSocket, Data, Length, 0,
169                 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
170         #endif
171         
172         if( len != Length ) {
173                 perror("SendData");
174                 exit(-1);
175         }
176 }
177
178 int ReadData(void *Dest, int MaxLength, int Timeout)
179 {
180          int    ret;
181         fd_set  fds;
182         struct timeval  tv;
183         
184         FD_ZERO(&fds);
185         FD_SET(gSocket, &fds);
186         
187         tv.tv_sec = Timeout;
188         tv.tv_usec = 0;
189         
190         ret = select(1, &fds, NULL, NULL, &tv);
191         if( ret == -1 ) {
192                 perror("ReadData - select");
193                 exit(-1);
194         }
195         
196         if( !ret ) {
197                 printf("Timeout reading from socket\n");
198                 return 0;       // Timeout
199         }
200         
201         #if USE_TCP
202         ret = recv(gSocket, Dest, MaxLength, 0);
203         #else
204         ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
205         #endif
206         
207         if( ret < 0 ) {
208                 perror("ReadData");
209                 exit(-1);
210         }
211         
212         return ret;
213 }

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