11 # include <sys/socket.h>
12 # include <netinet/in.h>
15 #include "../syscalls.h"
20 void SendData(void *Data, int Length);
21 int ReadData(void *Dest, int MaxLen, int Timeout);
26 SOCKET gSocket = INVALID_SOCKET;
28 # define INVALID_SOCKET -1
29 int gSocket = INVALID_SOCKET;
31 // Client ID to pass to server
32 // TODO: Implement such that each thread gets a different one
33 int giSyscall_ClientID = 0;
34 struct sockaddr_in gSyscall_ServerAddr;
41 /* Open windows connection */
42 if (WSAStartup(0x0101, &gWinsock) != 0)
44 fprintf(stderr, "Could not open Windows connection.\n");
50 // Open TCP Connection
51 gSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
53 // Open UDP Connection
54 gSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
56 if (gSocket == INVALID_SOCKET)
58 fprintf(stderr, "Could not create socket.\n");
66 memset((void *)&gSyscall_ServerAddr, '\0', sizeof(struct sockaddr_in));
67 gSyscall_ServerAddr.sin_family = AF_INET;
68 gSyscall_ServerAddr.sin_port = htons(SERVER_PORT);
69 gSyscall_ServerAddr.sin_addr.s_addr = htonl(0x7F000001);
73 memset((void *)&client, '\0', sizeof(struct sockaddr_in));
74 client.sin_family = AF_INET;
75 client.sin_port = htons(0);
76 client.sin_addr.s_addr = htonl(0x7F000001);
80 if( connect(gSocket, (struct sockaddr *)&gSyscall_ServerAddr, sizeof(struct sockaddr_in)) < 0 )
82 fprintf(stderr, "Cannot connect to server (localhost:%i)\n", SERVER_PORT);
83 perror("_InitSyscalls");
92 giSyscall_ClientID = gSocket; // A bit of a hack really :(
97 if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 )
99 fprintf(stderr, "Cannot bind address to socket.\n");
101 closesocket(gSocket);
111 // Ask server for a client ID
119 SendData(&req, sizeof(req));
121 len = ReadData(&req, sizeof(req), 5);
123 fprintf(stderr, "Unable to connect to server (localhost:%i)\n", SERVER_PORT);
127 giSyscall_ClientID = req.ClientID;
134 int SendRequest(tRequestHeader *Request, int RequestSize, int ResponseSize)
136 if( gSocket == INVALID_SOCKET )
142 Request->ClientID = giSyscall_ClientID;
146 for(i=0;i<RequestSize;i++)
148 printf("%02x ", ((uint8_t*)Request)[i]);
149 if( i % 16 == 15 ) printf("\n");
156 char *data = (char*)&Request->Params[Request->NParams];
157 printf("Request #%i (%s) -", Request->CallID, casSYSCALL_NAMES[Request->CallID]);
158 for( i = 0; i < Request->NParams; i ++ )
160 switch(Request->Params[i].Type)
163 printf(" 0x%08x", *(uint32_t*)data);
164 data += sizeof(uint32_t);
167 printf(" 0x%016llx", *(uint64_t*)data);
168 data += sizeof(uint64_t);
170 case ARG_TYPE_STRING:
171 printf(" '%s'", (char*)data);
172 data += Request->Params[i].Length;
175 printf(" %p:0x%x", (char*)data, Request->Params[i].Length);
176 if( !(Request->Params[i].Flags & ARG_FLAG_ZEROED) )
177 data += Request->Params[i].Length;
185 SendData(Request, RequestSize);
187 // Wait for a response (no timeout)
188 return ReadData(Request, ResponseSize, 0);
191 void SendData(void *Data, int Length)
196 len = send(Data, Length, 0);
198 len = sendto(gSocket, Data, Length, 0,
199 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
202 if( len != Length ) {
208 int ReadData(void *Dest, int MaxLength, int Timeout)
213 struct timeval *timeoutPtr;
216 FD_SET(gSocket, &fds);
227 ret = select(gSocket+1, &fds, NULL, NULL, timeoutPtr);
229 perror("ReadData - select");
234 printf("Timeout reading from socket\n");
239 ret = recv(gSocket, Dest, MaxLength, 0);
241 ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);
249 printf("%i bytes read from socket\n", ret);