11 # include <sys/socket.h>
12 # include <netinet/in.h>
15 #include "../syscalls.h"
17 #define SERVER_PORT 0xACE
22 SOCKET gSocket = INVALID_SOCKET;
24 # define INVALID_SOCKET -1
25 int gSocket = INVALID_SOCKET;
27 // Client ID to pass to server
28 // TODO: Implement such that each thread gets a different one
29 static int siSyscall_ClientID = 0;
34 struct sockaddr_in server;
35 struct sockaddr_in client;
38 /* Open windows connection */
39 if (WSAStartup(0x0101, &gWinsock) != 0)
41 fprintf(stderr, "Could not open Windows connection.\n");
46 // Open TCP Connection
47 gSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
48 // Open UDP Connection
49 //gSocket = socket(AF_INET, SOCK_DGRAM, 0);
50 if (gSocket == INVALID_SOCKET)
52 fprintf(stderr, "Could not create socket.\n");
60 memset((void *)&server, '\0', sizeof(struct sockaddr_in));
61 server.sin_family = AF_INET;
62 server.sin_port = htons(SERVER_PORT);
63 server.sin_addr.s_addr = htonl(0x7F00001);
66 memset((void *)&client, '\0', sizeof(struct sockaddr_in));
67 client.sin_family = AF_INET;
68 client.sin_port = htons(0);
69 client.sin_addr.s_addr = htonl(0x7F00001);
71 if( connect(gSocket, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0 )
73 fprintf(stderr, "Cannot connect to server (localhost:%i)\n", SERVER_PORT);
74 perror("_InitSyscalls");
86 if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 )
88 fprintf(stderr, "Cannot bind address to socket.\n");
102 int SendRequest(int RequestID, int NumOutput, tOutValue **Output, int NumInput, tInValue **Input)
104 tRequestHeader *request;
105 tRequestValue *value;
110 if( gSocket == INVALID_SOCKET )
115 // See ../syscalls.h for details of request format
116 requestLen = sizeof(tRequestHeader) + (NumOutput + NumInput) * sizeof(tRequestValue);
118 // Get total param length
119 for( i = 0; i < NumOutput; i ++ )
120 requestLen += Output[i]->Length;
123 request = malloc( requestLen );
124 value = request->Params;
125 data = (char*)&request->Params[ NumOutput + NumInput ];
128 request->ClientID = siSyscall_ClientID;
129 request->CallID = RequestID; // Syscall
130 request->NParams = NumOutput;
131 request->NReturn = NumInput;
134 for( i = 0; i < NumOutput; i ++ )
136 switch(Output[i]->Type)
138 case 'i': value->Type = ARG_TYPE_INT32; break;
139 case 'I': value->Type = ARG_TYPE_INT64; break;
140 case 'd': value->Type = ARG_TYPE_DATA; break;
141 case 's': value->Type = ARG_TYPE_DATA; break;
143 fprintf(stderr, __FILE__" SendRequest: Unknown output type '%c'\n",
147 value->Length = Output[i]->Length;
149 memcpy(data, Output[i]->Data, Output[i]->Length);
152 data += Output[i]->Length;
156 for( i = 0; i < NumInput; i ++ )
158 switch(Input[i]->Type)
160 case 'i': value->Type = ARG_TYPE_INT32; break;
161 case 'I': value->Type = ARG_TYPE_INT64; break;
162 case 'd': value->Type = ARG_TYPE_DATA; break;
164 fprintf(stderr, " SendRequest: Unknown input type '%c'\n",
168 value->Length = Input[i]->Length;
172 printf("value = %p\n", value);
174 for(i=0;i<requestLen;i++)
176 printf("%02x ", ((uint8_t*)request)[i]);
177 if( i % 16 == 15 ) printf("\n");
184 if( send(gSocket, request, requestLen, 0) != requestLen ) {
185 fprintf(stderr, "SendRequest: send() failed\n");
186 perror("SendRequest - send");
191 // Wait for a response
192 requestLen = recv(gSocket, request, requestLen, 0);
193 if( requestLen < 0 ) {
194 fprintf(stderr, "SendRequest: revc() failed\n");
195 perror("SendRequest - recv");
200 // Parse response out
201 if( request->NParams != NumInput ) {
202 fprintf(stderr, "SendRequest: Unexpected number of values retured (%i, exp %i)\n",
203 request->NParams, NumInput