AcessNative - Allowed the kernel executable to also start the user app
[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         if( Request->CallID == SYS_EXIT )       return 0;
200
201         // Wait for a response (no timeout)
202         return ReadData(Request, ResponseSize, 0);
203 }
204
205 void SendData(void *Data, int Length)
206 {
207          int    len;
208         
209         #if USE_TCP
210         len = send(Data, Length, 0);
211         #else
212         len = sendto(gSocket, Data, Length, 0,
213                 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
214         #endif
215         
216         if( len != Length ) {
217                 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
218                 perror("SendData");
219                 exit(-1);
220         }
221 }
222
223 int ReadData(void *Dest, int MaxLength, int Timeout)
224 {
225          int    ret;
226         fd_set  fds;
227         struct timeval  tv;
228         struct timeval  *timeoutPtr;
229         
230         FD_ZERO(&fds);
231         FD_SET(gSocket, &fds);
232         
233         if( Timeout ) {
234                 tv.tv_sec = Timeout;
235                 tv.tv_usec = 0;
236                 timeoutPtr = &tv;
237         }
238         else {
239                 timeoutPtr = NULL;
240         }
241         
242         ret = select(gSocket+1, &fds, NULL, NULL, timeoutPtr);
243         if( ret == -1 ) {
244                 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
245                 perror("ReadData - select");
246                 exit(-1);
247         }
248         
249         if( !ret ) {
250                 printf("[ERROR %i] Timeout reading from socket\n", giSyscall_ClientID);
251                 return 0;       // Timeout
252         }
253         
254         #if USE_TCP
255         ret = recv(gSocket, Dest, MaxLength, 0);
256         #else
257         ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
258         #endif
259         
260         if( ret < 0 ) {
261                 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
262                 perror("ReadData");
263                 exit(-1);
264         }
265         
266         DEBUG_S("%i bytes read from socket\n", ret);
267         
268         return ret;
269 }

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