midltests: print out the alloc_hint for requests and responses
[ira/wip.git] / testprogs / win32 / midltests / midltests_tcp.c
1 /*
2    MIDLTESTS client.
3
4    Copyright (C) Stefan Metzmacher 2008
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <winsock.h>
24 #include "midltests.h"
25
26 #ifndef _M_AMD64
27 #error "please run 'vcvarsall.bat amd64' -midltests_tcp needs 64-bit support!"
28 #endif
29
30 #define MIDLTESTS_C_CODE 1
31 #include "midltests.idl"
32
33 #ifndef LISTEN_IP
34 #define LISTEN_IP "127.0.0.1"
35 #endif
36
37 #ifndef FORWARD_IP
38 #define FORWARD_IP "127.0.0.1"
39 #endif
40
41 #ifndef CONNECT_IP
42 #define CONNECT_IP "127.0.0.1"
43 #endif
44
45 struct NDRTcpThreadCtx;
46
47 struct NDRProxyThreadCtx {
48         const struct NDRTcpThreadCtx *ctx;
49         SOCKET InSocket;
50         SOCKET OutSocket;
51         DWORD dwThreadId;
52         HANDLE hThread;
53 };
54
55 struct NDRTcpThreadCtx {
56         const char *name;
57         short listen_port;
58         short client_port;
59         BOOL ndr64;
60         BOOL stop;
61 };
62
63 struct dcerpc_header {
64         BYTE rpc_vers;          /* RPC version */
65         BYTE rpc_vers_minor;    /* Minor version */
66         BYTE ptype;             /* Packet type */
67         BYTE pfc_flags;         /* Fragmentation flags */
68         BYTE drep[4];           /* NDR data representation */
69         short frag_length;      /* Total length of fragment */
70         short auth_length;      /* authenticator length */
71         DWORD call_id;          /* Call identifier */
72 };
73
74 static void dump_packet(const char *ctx, const char *direction,
75                         const unsigned char *buf, int len)
76 {
77         struct dcerpc_header *hdr = (struct dcerpc_header *)buf;
78
79         if (len < sizeof(struct dcerpc_header)) {
80                 printf("%s:%s: invalid dcerpc pdu len(%d)\n",
81                        ctx, direction, len);
82                 fflush(stdout);
83                 return;
84         }
85
86         if (hdr->rpc_vers != 5 || hdr->rpc_vers_minor != 0) {
87                 printf("%s:%s: invalid dcerpc pdu len(%d) ver:%d min:%d\n",
88                        ctx, direction, len,
89                        hdr->rpc_vers, hdr->rpc_vers_minor);
90                 fflush(stdout);
91                 return;
92         }
93
94         if (hdr->frag_length != len) {
95                 printf("%s:%s: invalid dcerpc pdu len(%d) should be (%d)\n",
96                        ctx, direction, len, hdr->frag_length);
97                 fflush(stdout);
98                 return;
99         }
100
101
102         switch (hdr->ptype) {
103         case 0: /* request */
104                 printf("%s:%s: ptype[request] flen[%d] plen[%d] ahint[%d]\n\n",
105                       ctx, direction, hdr->frag_length,
106                       len - 24, *(DWORD *)(&buf[0x10]));
107                 dump_data(buf + 24, len - 24);
108                 printf("\n");
109                 fflush(stdout);
110                 break;
111
112         case 2: /* response */
113                 printf("\n%s:%s: ptype[response] flen[%d] plen[%d] ahint[%d]\n\n",
114                        ctx, direction, hdr->frag_length,
115                        len - 24, *(DWORD *)(&buf[0x10]));
116                 dump_data(buf + 24, len - 24);
117                 printf("\n");
118                 fflush(stdout);
119                 break;
120
121         case 11: /* bind */
122 #if 0
123                 printf("%s:%s: ptype[bind] flen[%d] call[%d] contexts[%d]\n\n"
124                        ctx, direction, hdr->frag_length, hdr->call_id,
125                        buf[24]);
126                 dump_data(buf + 24, len - 24);
127                 printf("\n");
128                 fflush(stdout);
129 #endif
130                 break;
131
132         case 12: /* bind ack */
133 #if 0
134                 printf("%s:%s: ptype[bind_ack] flen[%d] call[%d]\n\n",
135                        ctx, direction, hdr->frag_length, hdr->call_id);
136                 fflush(stdout);
137 #endif
138                 break;
139
140         case 14: /* alter_req */
141 #if 1
142                 printf("%s:%s: ptype[alter_req] flen[%d] call[%d] contexts[%d]\n\n",
143                            ctx, direction, hdr->frag_length, hdr->call_id,
144                            buf[24]);
145                 //dump_data(buf + 24, len - 24);
146                 printf("\n");
147                 fflush(stdout);
148 #endif
149                 break;
150
151         case 15: /* alter_ack */
152 #if 1
153                 printf("%s:%s: ptype[alter_ack] flen[%d] call[%d]\n\n",
154                        ctx, direction, hdr->frag_length, hdr->call_id);
155                 fflush(stdout);
156 #endif
157                 break;
158
159         default:
160                 printf("%s:%s: ptype[%d] flen[%d] call[%d]\n\n",
161                        ctx, direction, hdr->ptype, hdr->frag_length, hdr->call_id);
162                 fflush(stdout);
163                 break;
164         }
165 }
166
167 static void change_packet(const char *ctx, BOOL ndr64,
168                           unsigned char *buf, int len)
169 {
170         struct dcerpc_header *hdr = (struct dcerpc_header *)buf;
171         BOOL is_ndr64 = FALSE;
172         const unsigned char ndr64_buf[] = {
173                 0x33, 0x05, 0x71, 0x71, 0xBA, 0xBE, 0x37, 0x49,
174                 0x83, 0x19, 0xB5, 0xDB, 0xEF, 0x9C, 0xCC, 0x36
175         };
176
177         if (len < sizeof(struct dcerpc_header)) {
178                 printf("%s: invalid dcerpc pdu len(%d)\n",
179                            ctx, len);
180                 fflush(stdout);
181                 return;
182         }
183
184         if (hdr->rpc_vers != 5 || hdr->rpc_vers_minor != 0) {
185                 printf("%s: invalid dcerpc pdu len(%d) ver:%d min:%d\n",
186                        ctx, len,
187                        hdr->rpc_vers, hdr->rpc_vers_minor);
188                 fflush(stdout);
189                 return;
190         }
191
192         if (hdr->frag_length != len) {
193                 printf("%s: invalid dcerpc pdu len(%d) should be (%d)\n",
194                        ctx, len, hdr->frag_length);
195                 fflush(stdout);
196                 return;
197         }
198
199         switch (hdr->ptype) {
200         case 11: /* bind */
201         case 14: /* alter_req */
202
203                 if (buf[24] >= 2) {
204                         int ret;
205
206                         ret = memcmp(&buf[0x60], ndr64_buf, 16);
207                         if (ret == 0) {
208                                 is_ndr64 = TRUE;
209                         }
210                 }
211
212                 if (is_ndr64 && !ndr64) {
213                         buf[24+0x48] = 0xFF;
214                         memset(&buf[0x60], 0xFF, 16);
215                         printf("%s: disable NDR64\n\n", ctx);
216                 } else if (!is_ndr64 && ndr64) {
217                         printf("\n%s: got NDR32 downgrade\n\n", ctx);
218 #ifndef DONOT_FORCE_NDR64
219                         printf("\n\tERROR!!!\n\n");
220                         memset(&buf[0x34], 0xFF, 16);
221                         printf("You may need to run 'vcvarsall.bat amd64' before 'nmake tcp'\n");
222 #endif
223                         printf("\n");
224                 } else if (is_ndr64) {
225                         printf("%s: got NDR64\n\n", ctx);
226                 } else {
227                         printf("%s: got NDR32\n\n", ctx);
228                 }
229                 //printf("%s: bind with %u pres\n", ctx, buf[24]);
230                 fflush(stdout);
231                 break;
232         }
233 }
234
235 DWORD WINAPI NDRProxyThread(LPVOID lpParameter)
236 {
237         struct NDRProxyThreadCtx *p = (struct NDRProxyThreadCtx *)lpParameter;
238
239         while (!p->ctx->stop) {
240                 int r, s;
241                 int ret = -1;
242                 BYTE buf[5840];
243
244                 r = recv(p->InSocket, buf, sizeof(buf), 0);
245                 if (r <= 0) {
246                         ret = WSAGetLastError();
247                         printf("%s: recv(in) failed[%d][%d]\n", p->ctx->name, r, ret);
248                         fflush(stdout);
249                         goto next;
250                 }
251
252                 change_packet(p->ctx->name, p->ctx->ndr64, buf, r);
253                 fflush(stdout);
254
255                 dump_packet(p->ctx->name, "in => out", buf, r);
256                 fflush(stdout);
257
258                 s = send(p->OutSocket, buf, r, 0);
259                 if (s <= 0) {
260                         ret = WSAGetLastError();
261                         printf("%s: send(out) failed[%d][%d]\n", p->ctx->name, s, ret);
262                         fflush(stdout);
263                         goto next;
264                 }
265
266                 r = recv(p->OutSocket, buf, sizeof(buf), 0);
267                 if (r <= 0) {
268                         ret = WSAGetLastError();
269                         printf("%s: recv(out) failed[%d][%d]\n", p->ctx->name, r, ret);
270                         fflush(stdout);
271                         goto next;
272                 }
273
274                 dump_packet(p->ctx->name, "out => in", buf, r);
275                 fflush(stdout);
276
277                 s = send(p->InSocket, buf, r, 0);
278                 if (s <= 0) {
279                         ret = WSAGetLastError();
280                         printf("%s: send(in) failed[%d][%d]\n", p->ctx->name, s, ret);
281                         fflush(stdout);
282                         goto next;
283                 }
284
285         }
286 next:
287         closesocket(p->InSocket);
288         closesocket(p->OutSocket);
289
290         printf("NDRTcpThread[%s] stop\n", p->ctx->name);
291         fflush(stdout);
292         return 0;
293 }
294
295 DWORD WINAPI NDRTcpThread(LPVOID lpParameter)
296 {
297         struct NDRTcpThreadCtx *ctx = (struct NDRTcpThreadCtx *)lpParameter;
298         int ret = -1;
299         SOCKET ListenSocket;
300         struct sockaddr_in saServer;
301         struct sockaddr_in saClient;
302
303         //printf("NDRTcpThread[%s] start\n", ctx->name);
304         fflush(stdout);
305
306         ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
307         if (ListenSocket == INVALID_SOCKET) {
308                 ret = WSAGetLastError();
309                 printf("socket() failed[%d][%d]\n", ListenSocket, ret);
310                 fflush(stdout);
311                 goto failed;
312         }
313
314         saServer.sin_family = AF_INET;
315         saServer.sin_addr.s_addr = inet_addr(LISTEN_IP);
316         saServer.sin_port = htons(ctx->listen_port);
317
318         saClient.sin_family = AF_INET;
319         saClient.sin_addr.s_addr = inet_addr(FORWARD_IP);
320         saClient.sin_port = htons(ctx->client_port);
321
322         ret = bind(ListenSocket, (SOCKADDR*)&saServer, sizeof(saServer));
323         if (ret == SOCKET_ERROR) {
324                 ret = WSAGetLastError();
325                 printf("bind() failed[%d]\n", ret);
326                 fflush(stdout);
327                 goto failed;
328         }
329
330         ret = listen(ListenSocket, 10);
331         if (ret == SOCKET_ERROR) {
332                 ret = WSAGetLastError();
333                 printf("listen() failed[%d]\n", ret);
334                 fflush(stdout);
335                 goto failed;
336         }
337
338         while (!ctx->stop) {
339                 struct sockaddr_in sa;
340                 int sa_len = sizeof(sa);
341                 struct NDRProxyThreadCtx *p = malloc(sizeof(*p));
342                 p->ctx = ctx;
343
344                 p->InSocket = accept(ListenSocket, (SOCKADDR *)&sa, &sa_len);
345                 if (p->InSocket == INVALID_SOCKET) {
346                         ret = WSAGetLastError();
347                         printf("%s: accept() failed[%d][%d]\n", p->ctx->name, p->InSocket, ret);
348                         fflush(stdout);
349                         continue;
350                 }
351
352                 p->OutSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
353                 if (p->OutSocket == INVALID_SOCKET) {
354                         ret = WSAGetLastError();
355                         printf("%s: socket(out) failed[%d][%d]\n", p->ctx->name, p->OutSocket, ret);
356                         fflush(stdout);
357                         closesocket(p->InSocket);
358                         continue;
359                 }
360
361                 ret = connect(p->OutSocket, (SOCKADDR*)&saClient, sizeof(saClient));
362                 if (ret == SOCKET_ERROR) {
363                         ret = WSAGetLastError();
364                         printf("%s: connect() failed[%d]\n", p->ctx->name, ret);
365                         fflush(stdout);
366                         closesocket(p->InSocket);
367                         closesocket(p->OutSocket);
368                         continue;
369                 }
370
371                 p->hThread = CreateThread(
372                         NULL,           // default security attributes
373                         0,              // use default stack size
374                         NDRProxyThread, // thread function name
375                         p,              // argument to thread function
376                         0,              // use default creation flags
377                         &p->dwThreadId);// returns the thread identifier
378                 if (p->hThread == NULL) {
379                         printf("failed to create thread ndr32\n");
380                         fflush(stdout);
381                         return -1;
382                 }
383         }
384
385         //printf("NDRTcpThread[%s] stop\n", ctx->name);
386         fflush(stdout);
387         return 0;
388 failed:
389         printf("NDRTcpThread[%s] failed[%d]\n", ctx->name, ret);
390         fflush(stdout);
391         return ret;
392 }
393
394 struct NDRRpcThreadCtx {
395         const char *name;
396         short listen_port;
397 };
398
399 DWORD WINAPI NDRRpcThread(LPVOID lpParameter)
400 {
401         struct NDRRpcThreadCtx *ctx = (struct NDRRpcThreadCtx *)lpParameter;
402         int ret = -1;
403         RPC_STATUS status;
404         RPC_BINDING_VECTOR *pBindingVector;
405
406 #define RPC_MIN_CALLS 1
407 #define RPC_MAX_CALLS 20
408
409         //printf("NDRRpcThread[%s] start\n", ctx->name);
410         fflush(stdout);
411         status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "5055", NULL);
412         if (status) {
413                 printf("Failed to register ncacn_ip_tcp endpoint\n");
414                 fflush(stdout);
415                 return status;
416         }
417
418         status = RpcServerInqBindings(&pBindingVector);
419         if (status) {
420                 printf("Failed RpcServerInqBindings\n");
421                 fflush(stdout);
422                 return status;
423         }
424
425 #if 0
426         status = RpcEpRegister(srv_midltests_v0_0_s_ifspec, pBindingVector, NULL, "midltests server");
427         if (status) {
428                 printf("Failed RpcEpRegister\n");
429                 fflush(stdout);
430                 return status;
431         }
432 #endif
433         status = RpcServerRegisterIf(srv_midltests_v0_0_s_ifspec, NULL, NULL);
434         if (status) {
435                 printf("Failed to register interface\n");
436                 fflush(stdout);
437                 return status;
438         }
439
440         status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
441         if (status) {
442                 printf("RpcServerListen returned error %d\n", status);
443                 fflush(stdout);
444                 return status;
445         }
446
447         printf("NDRRpcThread[%s] stop\n", ctx->name);
448         fflush(stdout);
449         return 0;
450 failed:
451         printf("NDRRpcThread[%s] failed[%d]\n", ctx->name, ret);
452         fflush(stdout);
453         return ret;
454 }
455
456 int main(int argc, char **argv)
457 {
458         int ret;
459         struct NDRTcpThreadCtx ctx_ndr32;
460         struct NDRTcpThreadCtx ctx_ndr64;
461         struct NDRRpcThreadCtx ctx_rpc;
462         DWORD   dwThreadIdArray[3];
463         HANDLE  hThreadArray[3];
464         WORD wVersionRequested = MAKEWORD(2, 2);
465         WSADATA wsaData;
466         char *binding;
467         RPC_STATUS status;
468
469         ret = WSAStartup(wVersionRequested, &wsaData);
470         if (ret != 0) {
471                 printf("WSAStartup failed with error: %d\n", ret);
472                 fflush(stdout);
473                 return -1;
474         }
475
476         ctx_ndr32.name = "ndr32";
477         ctx_ndr32.listen_port = 5032;
478         ctx_ndr32.client_port = 5055;
479         ctx_ndr32.ndr64 = FALSE;
480         ctx_ndr32.stop = FALSE;
481         hThreadArray[0] = CreateThread(
482                 NULL,                   // default security attributes
483                 0,                      // use default stack size
484                 NDRTcpThread,           // thread function name
485                 &ctx_ndr32,             // argument to thread function
486                 0,                      // use default creation flags
487                 &dwThreadIdArray[0]);   // returns the thread identifier
488         if (hThreadArray[0] == NULL) {
489                 printf("failed to create thread ndr32\n");
490                 fflush(stdout);
491                 return -1;
492         }
493
494         ctx_ndr64.name = "ndr64";
495         ctx_ndr64.listen_port = 5064;
496         ctx_ndr64.client_port = 5055;
497         ctx_ndr64.ndr64 = TRUE;
498         ctx_ndr64.stop = FALSE;
499         hThreadArray[1] = CreateThread(
500                 NULL,                   // default security attributes
501                 0,                      // use default stack size
502                 NDRTcpThread,           // thread function name
503                 &ctx_ndr64,             // argument to thread function
504                 0,                      // use default creation flags
505                 &dwThreadIdArray[1]);   // returns the thread identifier
506         if (hThreadArray[1] == NULL) {
507                 printf("failed to create thread ndr64\n");
508                 fflush(stdout);
509                 return -1;
510         }
511
512         ctx_rpc.name = "rpc";
513         ctx_rpc.listen_port = 5050;
514         hThreadArray[2] = CreateThread(
515                 NULL,                   // default security attributes
516                 0,                      // use default stack size
517                 NDRRpcThread,           // thread function name
518                 &ctx_rpc,               // argument to thread function
519                 0,                      // use default creation flags
520                 &dwThreadIdArray[2]);   // returns the thread identifier
521         if (hThreadArray[2] == NULL) {
522                 printf("failed to create thread rpc\n");
523                 fflush(stdout);
524                 return -1;
525         }
526
527         printf("Wait for setup of server threads\n");
528         fflush(stdout);
529         ret = WaitForMultipleObjects(3, hThreadArray, TRUE, 3000);
530         if (ret == WAIT_TIMEOUT) {
531                 /* OK */
532         } else {
533                 printf("Failed to setup of server threads %d:%d\n",
534                         ret, GetLastError());
535                 fflush(stdout);
536                 return -1;
537         }
538         ret = 0;
539
540         printf("\nTest NDR32\n\n");
541         fflush(stdout);
542         binding = "ncacn_ip_tcp:" CONNECT_IP "[5032]";
543         status = RpcBindingFromStringBinding(
544                         binding,
545                         &midltests_IfHandle);
546         if (status) {
547                 printf("RpcBindingFromStringBinding returned %d\n", status);
548                 fflush(stdout);
549                 return status;
550         }
551
552         RpcTryExcept {
553                 midltests();
554         } RpcExcept(1) {
555                 ret = RpcExceptionCode();
556                 printf("NDR32 Runtime error 0x%x\n", ret);
557                 fflush(stdout);
558         } RpcEndExcept
559         ctx_ndr32.stop = TRUE;
560
561         Sleep(250);
562
563         printf("\nTest NDR64\n\n");
564         binding = "ncacn_ip_tcp:" CONNECT_IP "[5064]";
565         status = RpcBindingFromStringBinding(
566                         binding,
567                         &midltests_IfHandle);
568         if (status) {
569                 printf("RpcBindingFromStringBinding returned %d\n", status);
570                 fflush(stdout);
571                 return status;
572         }
573
574         RpcTryExcept {
575                 midltests();
576         } RpcExcept(1) {
577                 ret = RpcExceptionCode();
578                 printf("Runtime error 0x%x\n", ret);
579                 fflush(stdout);
580         } RpcEndExcept
581         ctx_ndr64.stop = TRUE;
582
583         WaitForMultipleObjects(3, hThreadArray, TRUE, 2000);
584
585         if (ret == 0) {
586                 printf("\nTest OK\n");
587                 fflush(stdout);
588         } else {
589                 printf("\nTest FAILED[%d]\n", ret);
590                 fflush(stdout);
591         }
592
593         return ret;
594 }