7 # define DEBUG_S printf
10 # define DONT_INCLUDE_SYSCALL_NAMES
22 # include <sys/socket.h>
23 # include <netinet/in.h>
26 #include "../syscalls.h"
31 void SendData(void *Data, int Length);
32 int ReadData(void *Dest, int MaxLen, int Timeout);
37 SOCKET gSocket = INVALID_SOCKET;
39 # define INVALID_SOCKET -1
40 int gSocket = INVALID_SOCKET;
42 // Client ID to pass to server
43 // TODO: Implement such that each thread gets a different one
44 int giSyscall_ClientID = 0;
45 struct sockaddr_in gSyscall_ServerAddr;
48 void Request_Preinit(void)
51 memset((void *)&gSyscall_ServerAddr, '\0', sizeof(struct sockaddr_in));
52 gSyscall_ServerAddr.sin_family = AF_INET;
53 gSyscall_ServerAddr.sin_port = htons(SERVER_PORT);
54 gSyscall_ServerAddr.sin_addr.s_addr = htonl(0x7F000001);
57 int _InitSyscalls(void)
60 /* Open windows connection */
61 if (WSAStartup(0x0101, &gWinsock) != 0)
63 fprintf(stderr, "Could not open Windows connection.\n");
69 // Open TCP Connection
70 gSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
72 // Open UDP Connection
73 gSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
75 if (gSocket == INVALID_SOCKET)
77 fprintf(stderr, "Could not create socket.\n");
85 if( connect(gSocket, (struct sockaddr *)&gSyscall_ServerAddr, sizeof(struct sockaddr_in)) < 0 )
87 fprintf(stderr, "[ERROR -] Cannot connect to server (localhost:%i)\n", SERVER_PORT);
88 perror("_InitSyscalls");
90 fprintf(stderr, "[ERROR -] - WSAGetLastError said %i", WSAGetLastError());
101 // Set client address
102 memset((void *)&client, '\0', sizeof(struct sockaddr_in));
103 client.sin_family = AF_INET;
104 client.sin_port = htons(0);
105 client.sin_addr.s_addr = htonl(0x7F000001);
107 if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 )
109 fprintf(stderr, "Cannot bind address to socket.\n");
111 closesocket(gSocket);
122 tRequestAuthHdr auth;
123 auth.pid = giSyscall_ClientID;
125 SendData(&auth, sizeof(auth));
126 int len = ReadData(&auth, sizeof(auth), 5);
128 fprintf(stderr, "Timeout waiting for auth response\n");
131 giSyscall_ClientID = auth.pid;
134 // Ask server for a client ID
135 if( !giSyscall_ClientID )
143 SendData(&req, sizeof(req));
145 len = ReadData(&req, sizeof(req), 5);
147 fprintf(stderr, "Unable to connect to server (localhost:%i)\n", SERVER_PORT);
151 giSyscall_ClientID = req.ClientID;
159 * \brief Close the syscall socket
160 * \note Used in acess_fork to get a different port number
162 void _CloseSyscalls(void)
165 closesocket(gSocket);
172 int SendRequest(tRequestHeader *Request, int RequestSize, int ResponseSize)
174 if( gSocket == INVALID_SOCKET )
180 Request->ClientID = giSyscall_ClientID;
184 for(i=0;i<RequestSize;i++)
186 printf("%02x ", ((uint8_t*)Request)[i]);
187 if( i % 16 == 15 ) printf("\n");
195 char *data = (char*)&Request->Params[Request->NParams];
196 DEBUG_S("Request #%i (%s) -", Request->CallID, casSYSCALL_NAMES[Request->CallID]);
197 for( i = 0; i < Request->NParams; i ++ )
199 switch(Request->Params[i].Type)
202 DEBUG_S(" 0x%08x", *(uint32_t*)data);
203 data += sizeof(uint32_t);
206 DEBUG_S(" 0x%016"PRIx64"", *(uint64_t*)data);
207 data += sizeof(uint64_t);
209 case ARG_TYPE_STRING:
210 DEBUG_S(" '%s'", (char*)data);
211 data += Request->Params[i].Length;
214 DEBUG_S(" %p:0x%x", (char*)data, Request->Params[i].Length);
215 if( !(Request->Params[i].Flags & ARG_FLAG_ZEROED) )
216 data += Request->Params[i].Length;
225 SendData(Request, RequestSize);
227 if( Request->CallID == SYS_EXIT ) return 0;
229 // Wait for a response (no timeout)
230 ReadData(Request, sizeof(*Request), 0);
232 size_t recvbytes = sizeof(*Request), expbytes = Request->MessageLength;
233 char *ptr = (void*)Request->Params;
234 while( recvbytes < expbytes )
236 size_t len = ReadData(ptr, expbytes - recvbytes, 1000);
243 if( recvbytes > expbytes ) {
249 void SendData(void *Data, int Length)
254 len = send(gSocket, Data, Length, 0);
256 len = sendto(gSocket, Data, Length, 0,
257 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
260 if( len != Length ) {
261 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
267 int ReadData(void *Dest, int MaxLength, int Timeout)
272 struct timeval *timeoutPtr;
275 FD_SET(gSocket, &fds);
286 ret = select(gSocket+1, &fds, NULL, NULL, timeoutPtr);
288 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
289 perror("ReadData - select");
294 printf("[ERROR %i] Timeout reading from socket\n", giSyscall_ClientID);
295 return -2; // Timeout
299 ret = recv(gSocket, Dest, MaxLength, 0);
301 ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
305 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
310 fprintf(stderr, "[ERROR %i] Connection closed.\n", giSyscall_ClientID);
312 closesocket(gSocket);
319 DEBUG_S("%i bytes read from socket\n", ret);