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;
51 /* Open windows connection */
52 if (WSAStartup(0x0101, &gWinsock) != 0)
54 fprintf(stderr, "Could not open Windows connection.\n");
60 // Open TCP Connection
61 gSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
63 // Open UDP Connection
64 gSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
66 if (gSocket == INVALID_SOCKET)
68 fprintf(stderr, "Could not create socket.\n");
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);
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);
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 fprintf(stderr, "[ERROR -] ", giSyscall_ClientID);
94 perror("_InitSyscalls");
103 giSyscall_ClientID = gSocket; // A bit of a hack really :(
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;
146 int SendRequest(tRequestHeader *Request, int RequestSize, int ResponseSize)
148 if( gSocket == INVALID_SOCKET )
154 Request->ClientID = giSyscall_ClientID;
158 for(i=0;i<RequestSize;i++)
160 printf("%02x ", ((uint8_t*)Request)[i]);
161 if( i % 16 == 15 ) printf("\n");
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 ++ )
172 switch(Request->Params[i].Type)
175 DEBUG_S(" 0x%08x", *(uint32_t*)data);
176 data += sizeof(uint32_t);
179 DEBUG_S(" 0x%016llx", *(uint64_t*)data);
180 data += sizeof(uint64_t);
182 case ARG_TYPE_STRING:
183 DEBUG_S(" '%s'", (char*)data);
184 data += Request->Params[i].Length;
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;
197 SendData(Request, RequestSize);
199 // Wait for a response (no timeout)
200 return ReadData(Request, ResponseSize, 0);
203 void SendData(void *Data, int Length)
208 len = send(Data, Length, 0);
210 len = sendto(gSocket, Data, Length, 0,
211 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
214 if( len != Length ) {
215 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
221 int ReadData(void *Dest, int MaxLength, int Timeout)
226 struct timeval *timeoutPtr;
229 FD_SET(gSocket, &fds);
240 ret = select(gSocket+1, &fds, NULL, NULL, timeoutPtr);
242 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
243 perror("ReadData - select");
248 printf("[ERROR %i] Timeout reading from socket\n", giSyscall_ClientID);
253 ret = recv(gSocket, Dest, MaxLength, 0);
255 ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
259 fprintf(stderr, "[ERROR %i] ", giSyscall_ClientID);
264 DEBUG_S("%i bytes read from socket\n", ret);