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

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