7 # define DEBUG_S printf
10 # define DONT_INCLUDE_SYSCALL_NAMES
21 # include <sys/socket.h>
22 # include <netinet/in.h>
25 #include "../syscalls.h"
30 void SendData(void *Data, int Length);
31 int ReadData(void *Dest, int MaxLen, int Timeout);
36 SOCKET gSocket = INVALID_SOCKET;
38 # define INVALID_SOCKET -1
39 int gSocket = INVALID_SOCKET;
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;
47 void Request_Preinit(void)
50 memset((void *)&gSyscall_ServerAddr, '\0', sizeof(struct sockaddr_in));
51 gSyscall_ServerAddr.sin_family = AF_INET;
52 gSyscall_ServerAddr.sin_port = htons(SERVER_PORT);
55 int _InitSyscalls(void)
58 /* Open windows connection */
59 if (WSAStartup(0x0101, &gWinsock) != 0)
61 fprintf(stderr, "Could not open Windows connection.\n");
67 // Open TCP Connection
68 gSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
70 // Open UDP Connection
71 gSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
73 if (gSocket == INVALID_SOCKET)
75 fprintf(stderr, "Could not create socket.\n");
84 memset((void *)&client, '\0', sizeof(struct sockaddr_in));
85 client.sin_family = AF_INET;
86 client.sin_port = htons(0);
87 client.sin_addr.s_addr = htonl(0x7F000001);
91 if( connect(gSocket, (struct sockaddr *)&gSyscall_ServerAddr, sizeof(struct sockaddr_in)) < 0 )
93 fprintf(stderr, "[ERROR -] Cannot connect to server (localhost:%i)\n", SERVER_PORT);
94 fprintf(stderr, "[ERROR -] ", giSyscall_ClientID);
95 perror("_InitSyscalls");
108 if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 )
110 fprintf(stderr, "Cannot bind address to socket.\n");
112 closesocket(gSocket);
122 // Ask server for a client ID
123 if( !giSyscall_ClientID )
131 SendData(&req, sizeof(req));
133 len = ReadData(&req, sizeof(req), 5);
135 fprintf(stderr, "Unable to connect to server (localhost:%i)\n", SERVER_PORT);
139 giSyscall_ClientID = req.ClientID;
147 * \brief Close the syscall socket
148 * \note Used in acess_fork to get a different port number
150 void _CloseSyscalls(void)
153 closesocket(gSocket);
160 int SendRequest(tRequestHeader *Request, int RequestSize, int ResponseSize)
162 if( gSocket == INVALID_SOCKET )
168 Request->ClientID = giSyscall_ClientID;
172 for(i=0;i<RequestSize;i++)
174 printf("%02x ", ((uint8_t*)Request)[i]);
175 if( i % 16 == 15 ) printf("\n");
182 char *data = (char*)&Request->Params[Request->NParams];
183 DEBUG_S("Request #%i (%s) -", Request->CallID, casSYSCALL_NAMES[Request->CallID]);
184 for( i = 0; i < Request->NParams; i ++ )
186 switch(Request->Params[i].Type)
189 DEBUG_S(" 0x%08x", *(uint32_t*)data);
190 data += sizeof(uint32_t);
193 DEBUG_S(" 0x%016llx", *(uint64_t*)data);
194 data += sizeof(uint64_t);
196 case ARG_TYPE_STRING:
197 DEBUG_S(" '%s'", (char*)data);
198 data += Request->Params[i].Length;
201 DEBUG_S(" %p:0x%x", (char*)data, Request->Params[i].Length);
202 if( !(Request->Params[i].Flags & ARG_FLAG_ZEROED) )
203 data += Request->Params[i].Length;
211 SendData(Request, RequestSize);
213 if( Request->CallID == SYS_EXIT ) return 0;
215 // Wait for a response (no timeout)
216 return ReadData(Request, ResponseSize, 0);
219 void SendData(void *Data, int Length)
224 len = send(Data, Length, 0);
226 len = sendto(gSocket, Data, Length, 0,
227 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
230 if( len != Length ) {
231 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
237 int ReadData(void *Dest, int MaxLength, int Timeout)
242 struct timeval *timeoutPtr;
245 FD_SET(gSocket, &fds);
256 ret = select(gSocket+1, &fds, NULL, NULL, timeoutPtr);
258 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
259 perror("ReadData - select");
264 printf("[ERROR %i] Timeout reading from socket\n", giSyscall_ClientID);
269 ret = recv(gSocket, Dest, MaxLength, 0);
271 ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
275 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
280 DEBUG_S("%i bytes read from socket\n", ret);