2 * AcessNative ld-acess dynamic linker
3 * - By John Hodge (thePowersGang)
11 # define DEBUG_S printf
14 # define DONT_INCLUDE_SYSCALL_NAMES
26 # include <sys/socket.h>
27 # include <netinet/in.h>
28 # include <sys/select.h>
31 #include "../syscalls.h"
36 void SendData(void *Data, int Length);
37 int ReadData(void *Dest, int MaxLen, int Timeout);
42 SOCKET gSocket = INVALID_SOCKET;
44 # define INVALID_SOCKET -1
45 int gSocket = INVALID_SOCKET;
47 // Client ID to pass to server
48 // TODO: Implement such that each thread gets a different one
49 int giSyscall_ClientID = 0;
50 struct sockaddr_in gSyscall_ServerAddr;
53 void Request_Preinit(void)
56 memset((void *)&gSyscall_ServerAddr, '\0', sizeof(struct sockaddr_in));
57 gSyscall_ServerAddr.sin_family = AF_INET;
58 gSyscall_ServerAddr.sin_port = htons(SERVER_PORT);
59 gSyscall_ServerAddr.sin_addr.s_addr = htonl(0x7F000001);
62 int _InitSyscalls(void)
65 /* Open windows connection */
66 if (WSAStartup(0x0101, &gWinsock) != 0)
68 fprintf(stderr, "Could not open Windows connection.\n");
74 // Open TCP Connection
75 gSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
77 // Open UDP Connection
78 gSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
80 if (gSocket == INVALID_SOCKET)
82 fprintf(stderr, "Could not create socket.\n");
90 if( connect(gSocket, (struct sockaddr *)&gSyscall_ServerAddr, sizeof(struct sockaddr_in)) < 0 )
92 fprintf(stderr, "[ERROR -] Cannot connect to server (localhost:%i)\n", SERVER_PORT);
93 perror("_InitSyscalls");
95 fprintf(stderr, "[ERROR -] - WSAGetLastError said %i", WSAGetLastError());
106 // Set client address
107 memset((void *)&client, '\0', sizeof(struct sockaddr_in));
108 client.sin_family = AF_INET;
109 client.sin_port = htons(0);
110 client.sin_addr.s_addr = htonl(0x7F000001);
112 if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 )
114 fprintf(stderr, "Cannot bind address to socket.\n");
116 closesocket(gSocket);
127 tRequestAuthHdr auth;
128 auth.pid = giSyscall_ClientID;
130 SendData(&auth, sizeof(auth));
131 int len = ReadData(&auth, sizeof(auth), 5);
133 fprintf(stderr, "Timeout waiting for auth response\n");
136 giSyscall_ClientID = auth.pid;
139 // Ask server for a client ID
140 if( !giSyscall_ClientID )
148 SendData(&req, sizeof(req));
150 len = ReadData(&req, sizeof(req), 5);
152 fprintf(stderr, "Unable to connect to server (localhost:%i)\n", SERVER_PORT);
156 giSyscall_ClientID = req.ClientID;
164 * \brief Close the syscall socket
165 * \note Used in acess_fork to get a different port number
167 void _CloseSyscalls(void)
170 closesocket(gSocket);
177 int SendRequest(tRequestHeader *Request, int RequestSize, int ResponseSize)
179 if( gSocket == INVALID_SOCKET )
185 Request->ClientID = giSyscall_ClientID;
189 for(i=0;i<RequestSize;i++)
191 printf("%02x ", ((uint8_t*)Request)[i]);
192 if( i % 16 == 15 ) printf("\n");
200 char *data = (char*)&Request->Params[Request->NParams];
201 DEBUG_S("Request #%i (%s) -", Request->CallID, casSYSCALL_NAMES[Request->CallID]);
202 for( i = 0; i < Request->NParams; i ++ )
204 switch(Request->Params[i].Type)
207 DEBUG_S(" 0x%08x", *(uint32_t*)data);
208 data += sizeof(uint32_t);
211 DEBUG_S(" 0x%016"PRIx64"", *(uint64_t*)data);
212 data += sizeof(uint64_t);
214 case ARG_TYPE_STRING:
215 DEBUG_S(" '%s'", (char*)data);
216 data += Request->Params[i].Length;
219 DEBUG_S(" %p:0x%x", (char*)data, Request->Params[i].Length);
220 if( !(Request->Params[i].Flags & ARG_FLAG_ZEROED) )
221 data += Request->Params[i].Length;
230 SendData(Request, RequestSize);
232 // Wait for a response (no timeout)
233 ReadData(Request, sizeof(*Request), 0);
235 size_t recvbytes = sizeof(*Request);
237 size_t expbytes = Request->MessageLength;
238 char *ptr = (void*)Request->Params;
239 while( recvbytes < expbytes )
241 size_t len = ReadData(ptr, expbytes - recvbytes, 1000);
248 if( recvbytes > expbytes ) {
255 char *data = (char*)&Request->Params[Request->NParams];
257 for( i = 0; i < Request->NParams; i ++ )
259 switch(Request->Params[i].Type)
262 DEBUG_S(" 0x%08x", *(uint32_t*)data);
263 data += sizeof(uint32_t);
266 DEBUG_S(" 0x%016"PRIx64"", *(uint64_t*)data);
267 data += sizeof(uint64_t);
269 case ARG_TYPE_STRING:
270 DEBUG_S(" '%s'", (char*)data);
271 data += Request->Params[i].Length;
274 DEBUG_S(" %p:0x%x", (char*)data, Request->Params[i].Length);
275 if( !(Request->Params[i].Flags & ARG_FLAG_ZEROED) )
276 data += Request->Params[i].Length;
286 void SendData(void *Data, int Length)
291 len = send(gSocket, Data, Length, 0);
293 len = sendto(gSocket, Data, Length, 0,
294 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
297 if( len != Length ) {
298 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
304 int ReadData(void *Dest, int MaxLength, int Timeout)
309 struct timeval *timeoutPtr;
312 FD_SET(gSocket, &fds);
323 ret = select(gSocket+1, &fds, NULL, NULL, timeoutPtr);
325 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
326 perror("ReadData - select");
331 printf("[ERROR %i] Timeout reading from socket\n", giSyscall_ClientID);
332 return -2; // Timeout
336 ret = recv(gSocket, Dest, MaxLength, 0);
338 ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
342 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
347 fprintf(stderr, "[ERROR %i] Connection closed.\n", giSyscall_ClientID);
349 closesocket(gSocket);
356 DEBUG_S("%i bytes read from socket\n", ret);