c5e89efc98de16e41eac0c22e51382d8ff75ee78
[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                  int    i;
155                 char    *data = (char*)&Request->Params[Request->NParams];
156                 printf("Request #%i\n", Request->CallID);
157                 for( i = 0; i < Request->NParams; i ++ )
158                 {
159                         printf("%i: ", i);
160                         switch(Request->Params[i].Type)
161                         {
162                         case ARG_TYPE_INT32:
163                                 printf("INT32 %x", *(uint32_t*)data);
164                                 data += sizeof(uint32_t);
165                                 break;
166                         case ARG_TYPE_INT64:
167                                 printf("INT64 %llx", *(uint64_t*)data);
168                                 data += sizeof(uint64_t);
169                                 break;
170                         case ARG_TYPE_STRING:
171                                 printf("STRING '%s'", (char*)data);
172                                 data += Request->Params[i].Length;
173                                 break;
174                         case ARG_TYPE_DATA:
175                                 printf("DATA %i %p", Request->Params[i].Length, (char*)data);
176                                 data += Request->Params[i].Length;
177                                 break;
178                         }
179                         printf("\n");
180                 }
181         }
182         
183         // Send it off
184         SendData(Request, RequestSize);
185         
186         // Wait for a response (no timeout)
187         return ReadData(Request, RequestSize, 0);
188 }
189
190 void SendData(void *Data, int Length)
191 {
192          int    len;
193         
194         #if USE_TCP
195         len = send(Data, Length, 0);
196         #else
197         len = sendto(gSocket, Data, Length, 0,
198                 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
199         #endif
200         
201         if( len != Length ) {
202                 perror("SendData");
203                 exit(-1);
204         }
205 }
206
207 int ReadData(void *Dest, int MaxLength, int Timeout)
208 {
209          int    ret;
210         fd_set  fds;
211         struct timeval  tv;
212         struct timeval  *timeoutPtr;
213         
214         FD_ZERO(&fds);
215         FD_SET(gSocket, &fds);
216         
217         if( Timeout ) {
218                 tv.tv_sec = Timeout;
219                 tv.tv_usec = 0;
220                 timeoutPtr = &tv;
221         }
222         else {
223                 timeoutPtr = NULL;
224         }
225         
226         ret = select(gSocket+1, &fds, NULL, NULL, timeoutPtr);
227         if( ret == -1 ) {
228                 perror("ReadData - select");
229                 exit(-1);
230         }
231         
232         if( !ret ) {
233                 printf("Timeout reading from socket\n");
234                 return 0;       // Timeout
235         }
236         
237         #if USE_TCP
238         ret = recv(gSocket, Dest, MaxLength, 0);
239         #else
240         ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
241         #endif
242         
243         if( ret < 0 ) {
244                 perror("ReadData");
245                 exit(-1);
246         }
247         
248         printf("%i bytes read from socket\n", ret);
249         
250         return ret;
251 }

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