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");
96 if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 )
98 fprintf(stderr, "Cannot bind address to socket.\n");
100 closesocket(gSocket);
110 // Ask server for a client ID
119 SendData(&req, sizeof(req));
121 len = ReadData(&req, sizeof(req), 5);
123 giSyscall_ClientID = req.ClientID;
130 int SendRequest(int RequestID, int NumOutput, tOutValue **Output, int NumInput, tInValue **Input)
132 tRequestHeader *request;
133 tRequestValue *value;
138 if( gSocket == INVALID_SOCKET )
143 // See ../syscalls.h for details of request format
144 requestLen = sizeof(tRequestHeader) + (NumOutput + NumInput) * sizeof(tRequestValue);
146 // Get total param length
147 for( i = 0; i < NumOutput; i ++ )
148 requestLen += Output[i]->Length;
151 request = malloc( requestLen );
152 value = request->Params;
153 data = (char*)&request->Params[ NumOutput + NumInput ];
156 request->ClientID = giSyscall_ClientID;
157 request->CallID = RequestID; // Syscall
158 request->NParams = NumOutput;
159 request->NReturn = NumInput;
162 for( i = 0; i < NumOutput; i ++ )
164 switch(Output[i]->Type)
166 case 'i': value->Type = ARG_TYPE_INT32; break;
167 case 'I': value->Type = ARG_TYPE_INT64; break;
168 case 'd': value->Type = ARG_TYPE_DATA; break;
169 case 's': value->Type = ARG_TYPE_DATA; break;
171 fprintf(stderr, __FILE__" SendRequest: Unknown output type '%c'\n",
175 value->Length = Output[i]->Length;
177 memcpy(data, Output[i]->Data, Output[i]->Length);
180 data += Output[i]->Length;
184 for( i = 0; i < NumInput; i ++ )
186 switch(Input[i]->Type)
188 case 'i': value->Type = ARG_TYPE_INT32; break;
189 case 'I': value->Type = ARG_TYPE_INT64; break;
190 case 'd': value->Type = ARG_TYPE_DATA; break;
192 fprintf(stderr, " SendRequest: Unknown input type '%c'\n",
196 value->Length = Input[i]->Length;
200 printf("value = %p\n", value);
202 for(i=0;i<requestLen;i++)
204 printf("%02x ", ((uint8_t*)request)[i]);
205 if( i % 16 == 15 ) printf("\n");
212 SendData(request, requestLen);
214 // Wait for a response (no timeout)
215 requestLen = ReadData(request, requestLen, -1);
217 // Parse response out
218 if( request->NParams != NumInput ) {
219 fprintf(stderr, "SendRequest: Unexpected number of values retured (%i, exp %i)\n",
220 request->NParams, NumInput
232 void SendData(void *Data, int Length)
237 len = send(Data, Length, 0);
239 len = sendto(gSocket, Data, Length, 0,
240 (struct sockaddr*)&gSyscall_ServerAddr, sizeof(gSyscall_ServerAddr));
243 if( len != Length ) {
249 int ReadData(void *Dest, int MaxLength, int Timeout)
256 FD_SET(gSocket, &fds);
261 ret = select(1, &fds, NULL, NULL, &tv);
263 perror("ReadData - select");
268 printf("Timeout reading from socket\n");
273 ret = recv(gSocket, Dest, MaxLength, 0);
275 ret = recvfrom(gSocket, Dest, MaxLength, 0, NULL, 0);