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 UDP Connection
47 gSocket = socket(AF_INET, SOCK_DGRAM, 0);
48 if (gSocket == INVALID_SOCKET)
50 fprintf(stderr, "Could not create socket.\n");
58 memset((void *)&server, '\0', sizeof(struct sockaddr_in));
59 server.sin_family = AF_INET;
60 server.sin_port = htons(SERVER_PORT);
61 server.sin_addr.s_addr = htonl(0x7F00001);
64 memset((void *)&client, '\0', sizeof(struct sockaddr_in));
65 client.sin_family = AF_INET;
66 client.sin_port = htons(0);
67 client.sin_addr.s_addr = htonl(0x7F00001);
70 if( bind(gSocket, (struct sockaddr *)&client, sizeof(struct sockaddr_in)) == -1 )
72 fprintf(stderr, "Cannot bind address to socket.\n");
84 int SendRequest(int RequestID, int NumOutput, tOutValue **Output, int NumInput, tInValue **Input)
86 tRequestHeader *request;
92 // See ../syscalls.h for details of request format
93 requestLen = sizeof(tRequestHeader) + (NumOutput + NumInput) * sizeof(tRequestValue);
95 // Get total param length
96 for( i = 0; i < NumOutput; i ++ )
97 requestLen += Output[i]->Length;
100 request = malloc( requestLen );
101 value = request->Params;
102 data = (char*)&request->Params[ NumOutput + NumInput ];
105 request->ClientID = siSyscall_ClientID;
106 request->CallID = RequestID; // Syscall
107 request->NParams = NumOutput;
108 request->NReturn = NumInput;
111 for( i = 0; i < NumOutput; i ++ )
113 switch(Output[i]->Type)
115 case 'i': value->Type = ARG_TYPE_INT32; break;
116 case 'I': value->Type = ARG_TYPE_INT64; break;
117 case 'd': value->Type = ARG_TYPE_DATA; break;
121 value->Length = Output[i]->Length;
123 memcpy(data, Output[i]->Data, Output[i]->Length);
125 data += Output[i]->Length;
129 for( i = 0; i < NumInput; i ++ )
131 switch(Input[i]->Type)
133 case 'i': value->Type = ARG_TYPE_INT32; break;
134 case 'I': value->Type = ARG_TYPE_INT64; break;
135 case 'd': value->Type = ARG_TYPE_DATA; break;
139 value->Length = Input[i]->Length;
143 send(gSocket, request, requestLen, 0);
145 // Wait for a response
146 recv(gSocket, request, requestLen, 0);
148 // Parse response out