midltests: add a midltests_tcp.exe tool
[obnox/samba/samba-obnox.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]\n\n",
105                       ctx, direction, hdr->frag_length,
106                       len - 24);
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]\n\n",
114                        ctx, direction, hdr->frag_length,
115                        len - 24);
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
172         if (len < sizeof(struct dcerpc_header)) {
173                 printf("%s: invalid dcerpc pdu len(%d)\n",
174                            ctx, len);
175                 fflush(stdout);
176                 return;
177         }
178
179         if (hdr->rpc_vers != 5 || hdr->rpc_vers_minor != 0) {
180                 printf("%s: invalid dcerpc pdu len(%d) ver:%d min:%d\n",
181                        ctx, len,
182                        hdr->rpc_vers, hdr->rpc_vers_minor);
183                 fflush(stdout);
184                 return;
185         }
186
187         if (hdr->frag_length != len) {
188                 printf("%s: invalid dcerpc pdu len(%d) should be (%d)\n",
189                        ctx, len, hdr->frag_length);
190                 fflush(stdout);
191                 return;
192         }
193
194         switch (hdr->ptype) {
195         case 11: /* bind */
196                 if (buf[24] == 3 && !ndr64) {
197                         buf[24+0x48] = 0xFF;
198                         printf("%s: disable NDR64\n\n", ctx);
199                 } else if (buf[24] < 3 && ndr64) {
200                         buf[24] = 0x00;
201                         printf("\n\tERROR!!!\n\n");
202                         printf("%s: disable NDR32\n", ctx);
203                         printf("\n");
204                         printf("You may need to run 'vcvarsall.bat amd64' before 'nmake tcp'\n");
205                 } else {
206                         printf("%s: got NDR64\n\n", ctx);
207                 }
208                 //printf("%s: bind with %u pres\n", ctx, buf[24]);
209                 fflush(stdout);
210                 break;
211         }
212 }
213
214 DWORD WINAPI NDRProxyThread(LPVOID lpParameter)
215 {
216         struct NDRProxyThreadCtx *p = (struct NDRProxyThreadCtx *)lpParameter;
217
218         while (!p->ctx->stop) {
219                 int r, s;
220                 int ret = -1;
221                 BYTE buf[5840];
222
223                 r = recv(p->InSocket, buf, sizeof(buf), 0);
224                 if (r <= 0) {
225                         ret = WSAGetLastError();
226                         printf("%s: recv(in) failed[%d][%d]\n", p->ctx->name, r, ret);
227                         fflush(stdout);
228                         goto next;
229                 }
230
231                 change_packet(p->ctx->name, p->ctx->ndr64, buf, r);
232                 fflush(stdout);
233
234                 dump_packet(p->ctx->name, "in => out", buf, r);
235                 fflush(stdout);
236
237                 s = send(p->OutSocket, buf, r, 0);
238                 if (s <= 0) {
239                         ret = WSAGetLastError();
240                         printf("%s: send(out) failed[%d][%d]\n", p->ctx->name, s, ret);
241                         fflush(stdout);
242                         goto next;
243                 }
244
245                 r = recv(p->OutSocket, buf, sizeof(buf), 0);
246                 if (r <= 0) {
247                         ret = WSAGetLastError();
248                         printf("%s: recv(out) failed[%d][%d]\n", p->ctx->name, r, ret);
249                         fflush(stdout);
250                         goto next;
251                 }
252
253                 dump_packet(p->ctx->name, "out => in", buf, r);
254                 fflush(stdout);
255
256                 s = send(p->InSocket, buf, r, 0);
257                 if (s <= 0) {
258                         ret = WSAGetLastError();
259                         printf("%s: send(in) failed[%d][%d]\n", p->ctx->name, s, ret);
260                         fflush(stdout);
261                         goto next;
262                 }
263
264         }
265 next:
266         closesocket(p->InSocket);
267         closesocket(p->OutSocket);
268
269         printf("NDRTcpThread[%s] stop\n", p->ctx->name);
270         fflush(stdout);
271         return 0;
272 }
273
274 DWORD WINAPI NDRTcpThread(LPVOID lpParameter)
275 {
276         struct NDRTcpThreadCtx *ctx = (struct NDRTcpThreadCtx *)lpParameter;
277         int ret = -1;
278         SOCKET ListenSocket;
279         struct sockaddr_in saServer;
280         struct sockaddr_in saClient;
281
282         //printf("NDRTcpThread[%s] start\n", ctx->name);
283         fflush(stdout);
284
285         ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
286         if (ListenSocket == INVALID_SOCKET) {
287                 ret = WSAGetLastError();
288                 printf("socket() failed[%d][%d]\n", ListenSocket, ret);
289                 fflush(stdout);
290                 goto failed;
291         }
292
293         saServer.sin_family = AF_INET;
294         saServer.sin_addr.s_addr = inet_addr(LISTEN_IP);
295         saServer.sin_port = htons(ctx->listen_port);
296
297         saClient.sin_family = AF_INET;
298         saClient.sin_addr.s_addr = inet_addr(FORWARD_IP);
299         saClient.sin_port = htons(ctx->client_port);
300
301         ret = bind(ListenSocket, (SOCKADDR*)&saServer, sizeof(saServer));
302         if (ret == SOCKET_ERROR) {
303                 ret = WSAGetLastError();
304                 printf("bind() failed[%d]\n", ret);
305                 fflush(stdout);
306                 goto failed;
307         }
308
309         ret = listen(ListenSocket, 10);
310         if (ret == SOCKET_ERROR) {
311                 ret = WSAGetLastError();
312                 printf("listen() failed[%d]\n", ret);
313                 fflush(stdout);
314                 goto failed;
315         }
316
317         while (!ctx->stop) {
318                 struct sockaddr_in sa;
319                 int sa_len = sizeof(sa);
320                 struct NDRProxyThreadCtx *p = malloc(sizeof(*p));
321                 p->ctx = ctx;
322
323                 p->InSocket = accept(ListenSocket, (SOCKADDR *)&sa, &sa_len);
324                 if (p->InSocket == INVALID_SOCKET) {
325                         ret = WSAGetLastError();
326                         printf("%s: accept() failed[%d][%d]\n", p->ctx->name, p->InSocket, ret);
327                         fflush(stdout);
328                         continue;
329                 }
330
331                 p->OutSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
332                 if (p->OutSocket == INVALID_SOCKET) {
333                         ret = WSAGetLastError();
334                         printf("%s: socket(out) failed[%d][%d]\n", p->ctx->name, p->OutSocket, ret);
335                         fflush(stdout);
336                         closesocket(p->InSocket);
337                         continue;
338                 }
339
340                 ret = connect(p->OutSocket, (SOCKADDR*)&saClient, sizeof(saClient));
341                 if (ret == SOCKET_ERROR) {
342                         ret = WSAGetLastError();
343                         printf("%s: connect() failed[%d]\n", p->ctx->name, ret);
344                         fflush(stdout);
345                         closesocket(p->InSocket);
346                         closesocket(p->OutSocket);
347                         continue;
348                 }
349
350                 p->hThread = CreateThread(
351                         NULL,           // default security attributes
352                         0,              // use default stack size
353                         NDRProxyThread, // thread function name
354                         p,              // argument to thread function
355                         0,              // use default creation flags
356                         &p->dwThreadId);// returns the thread identifier
357                 if (p->hThread == NULL) {
358                         printf("failed to create thread ndr32\n");
359                         fflush(stdout);
360                         return -1;
361                 }
362         }
363
364         //printf("NDRTcpThread[%s] stop\n", ctx->name);
365         fflush(stdout);
366         return 0;
367 failed:
368         printf("NDRTcpThread[%s] failed[%d]\n", ctx->name, ret);
369         fflush(stdout);
370         return ret;
371 }
372
373 struct NDRRpcThreadCtx {
374         const char *name;
375         short listen_port;
376 };
377
378 DWORD WINAPI NDRRpcThread(LPVOID lpParameter)
379 {
380         struct NDRRpcThreadCtx *ctx = (struct NDRRpcThreadCtx *)lpParameter;
381         int ret = -1;
382         RPC_STATUS status;
383         RPC_BINDING_VECTOR *pBindingVector;
384
385 #define RPC_MIN_CALLS 1
386 #define RPC_MAX_CALLS 20
387
388         //printf("NDRRpcThread[%s] start\n", ctx->name);
389         fflush(stdout);
390         status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "5055", NULL);
391         if (status) {
392                 printf("Failed to register ncacn_ip_tcp endpoint\n");
393                 fflush(stdout);
394                 return status;
395         }
396
397         status = RpcServerInqBindings(&pBindingVector);
398         if (status) {
399                 printf("Failed RpcServerInqBindings\n");
400                 fflush(stdout);
401                 return status;
402         }
403
404 #if 0
405         status = RpcEpRegister(srv_midltests_v0_0_s_ifspec, pBindingVector, NULL, "midltests server");
406         if (status) {
407                 printf("Failed RpcEpRegister\n");
408                 fflush(stdout);
409                 return status;
410         }
411 #endif
412         status = RpcServerRegisterIf(srv_midltests_v0_0_s_ifspec, NULL, NULL);
413         if (status) {
414                 printf("Failed to register interface\n");
415                 fflush(stdout);
416                 return status;
417         }
418
419         status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
420         if (status) {
421                 printf("RpcServerListen returned error %d\n", status);
422                 fflush(stdout);
423                 return status;
424         }
425
426         printf("NDRRpcThread[%s] stop\n", ctx->name);
427         fflush(stdout);
428         return 0;
429 failed:
430         printf("NDRRpcThread[%s] failed[%d]\n", ctx->name, ret);
431         fflush(stdout);
432         return ret;
433 }
434
435 int main(int argc, char **argv)
436 {
437         int ret;
438         struct NDRTcpThreadCtx ctx_ndr32;
439         struct NDRTcpThreadCtx ctx_ndr64;
440         struct NDRRpcThreadCtx ctx_rpc;
441         DWORD   dwThreadIdArray[3];
442         HANDLE  hThreadArray[3];
443         WORD wVersionRequested = MAKEWORD(2, 2);
444         WSADATA wsaData;
445         char *binding;
446         RPC_STATUS status;
447
448         ret = WSAStartup(wVersionRequested, &wsaData);
449         if (ret != 0) {
450                 printf("WSAStartup failed with error: %d\n", ret);
451                 fflush(stdout);
452                 return -1;
453         }
454
455         ctx_ndr32.name = "ndr32";
456         ctx_ndr32.listen_port = 5032;
457         ctx_ndr32.client_port = 5055;
458         ctx_ndr32.ndr64 = FALSE;
459         ctx_ndr32.stop = FALSE;
460         hThreadArray[0] = CreateThread(
461                 NULL,                   // default security attributes
462                 0,                      // use default stack size
463                 NDRTcpThread,           // thread function name
464                 &ctx_ndr32,             // argument to thread function
465                 0,                      // use default creation flags
466                 &dwThreadIdArray[0]);   // returns the thread identifier
467         if (hThreadArray[0] == NULL) {
468                 printf("failed to create thread ndr32\n");
469                 fflush(stdout);
470                 return -1;
471         }
472
473         ctx_ndr64.name = "ndr64";
474         ctx_ndr64.listen_port = 5064;
475         ctx_ndr64.client_port = 5055;
476         ctx_ndr64.ndr64 = TRUE;
477         ctx_ndr64.stop = FALSE;
478         hThreadArray[1] = CreateThread(
479                 NULL,                   // default security attributes
480                 0,                      // use default stack size
481                 NDRTcpThread,           // thread function name
482                 &ctx_ndr64,             // argument to thread function
483                 0,                      // use default creation flags
484                 &dwThreadIdArray[1]);   // returns the thread identifier
485         if (hThreadArray[1] == NULL) {
486                 printf("failed to create thread ndr64\n");
487                 fflush(stdout);
488                 return -1;
489         }
490
491         ctx_rpc.name = "rpc";
492         ctx_rpc.listen_port = 5050;
493         hThreadArray[2] = CreateThread(
494                 NULL,                   // default security attributes
495                 0,                      // use default stack size
496                 NDRRpcThread,           // thread function name
497                 &ctx_rpc,               // argument to thread function
498                 0,                      // use default creation flags
499                 &dwThreadIdArray[2]);   // returns the thread identifier
500         if (hThreadArray[2] == NULL) {
501                 printf("failed to create thread rpc\n");
502                 fflush(stdout);
503                 return -1;
504         }
505
506         printf("Wait for setup of server threads\n");
507         fflush(stdout);
508         ret = WaitForMultipleObjects(3, hThreadArray, TRUE, 3000);
509         if (ret == WAIT_TIMEOUT) {
510                 /* OK */
511         } else {
512                 printf("Failed to setup of server threads %d:%d\n",
513                         ret, GetLastError());
514                 fflush(stdout);
515                 return -1;
516         }
517         ret = 0;
518
519         printf("\nTest NDR32\n\n");
520         fflush(stdout);
521         binding = "ncacn_ip_tcp:" CONNECT_IP "[5032]";
522         status = RpcBindingFromStringBinding(
523                         binding,
524                         &midltests_IfHandle);
525         if (status) {
526                 printf("RpcBindingFromStringBinding returned %d\n", status);
527                 fflush(stdout);
528                 return status;
529         }
530
531         RpcTryExcept {
532                 midltests();
533         } RpcExcept(1) {
534                 ret = RpcExceptionCode();
535                 printf("NDR32 Runtime error 0x%x\n", ret);
536                 fflush(stdout);
537         } RpcEndExcept
538         ctx_ndr32.stop = TRUE;
539
540         Sleep(250);
541
542         printf("\nTest NDR64\n\n");
543         binding = "ncacn_ip_tcp:" CONNECT_IP "[5064]";
544         status = RpcBindingFromStringBinding(
545                         binding,
546                         &midltests_IfHandle);
547         if (status) {
548                 printf("RpcBindingFromStringBinding returned %d\n", status);
549                 fflush(stdout);
550                 return status;
551         }
552
553         RpcTryExcept {
554                 midltests();
555         } RpcExcept(1) {
556                 ret = RpcExceptionCode();
557                 printf("Runtime error 0x%x\n", ret);
558                 fflush(stdout);
559         } RpcEndExcept
560         ctx_ndr64.stop = TRUE;
561
562         WaitForMultipleObjects(3, hThreadArray, TRUE, 2000);
563
564         if (ret == 0) {
565                 printf("\nTest OK\n");
566                 fflush(stdout);
567         } else {
568                 printf("\nTest FAILED[%d]\n", ret);
569                 fflush(stdout);
570         }
571
572         return ret;
573 }