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, "Cannot connect to server (localhost:%i)\n", SERVER_PORT);
93 perror("_InitSyscalls");
102 giSyscall_ClientID = gSocket; // A bit of a hack really :(
107 if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 )
109 fprintf(stderr, "Cannot bind address to socket.\n");
111 closesocket(gSocket);
121 // Ask server for a client ID
129 SendData(&req, sizeof(req));
131 len = ReadData(&req, sizeof(req), 5);
133 fprintf(stderr, "Unable to connect to server (localhost:%i)\n", SERVER_PORT);
137 giSyscall_ClientID = req.ClientID;
144 int SendRequest(tRequestHeader *Request, int RequestSize, int ResponseSize)
146 if( gSocket == INVALID_SOCKET )
152 Request->ClientID = giSyscall_ClientID;
156 for(i=0;i<RequestSize;i++)
158 printf("%02x ", ((uint8_t*)Request)[i]);
159 if( i % 16 == 15 ) printf("\n");
166 char *data = (char*)&Request->Params[Request->NParams];
167 DEBUG_S("Request #%i (%s) -", Request->CallID, casSYSCALL_NAMES[Request->CallID]);
168 for( i = 0; i < Request->NParams; i ++ )
170 switch(Request->Params[i].Type)
173 DEBUG_S(" 0x%08x", *(uint32_t*)data);
174 data += sizeof(uint32_t);
177 DEBUG_S(" 0x%016llx", *(uint64_t*)data);
178 data += sizeof(uint64_t);
180 case ARG_TYPE_STRING:
181 DEBUG_S(" '%s'", (char*)data);
182 data += Request->Params[i].Length;
185 DEBUG_S(" %p:0x%x", (char*)data, Request->Params[i].Length);
186 if( !(Request->Params[i].Flags & ARG_FLAG_ZEROED) )
187 data += Request->Params[i].Length;
195 SendData(Request, RequestSize);
197 // Wait for a response (no timeout)
198 return ReadData(Request, ResponseSize, 0);
201 void SendData(void *Data, int Length)
206 len = send(Data, Length, 0);
208 len = sendto(gSocket, Data, Length, 0,
209 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
212 if( len != Length ) {
218 int ReadData(void *Dest, int MaxLength, int Timeout)
223 struct timeval *timeoutPtr;
226 FD_SET(gSocket, &fds);
237 ret = select(gSocket+1, &fds, NULL, NULL, timeoutPtr);
239 perror("ReadData - select");
244 printf("Timeout reading from socket\n");
249 ret = recv(gSocket, Dest, MaxLength, 0);
251 ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
259 DEBUG_S("%i bytes read from socket\n", ret);