4 Copyright (C) Stefan Metzmacher 2008
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.
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.
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/>.
24 #include "midltests.h"
27 #error "please run 'vcvarsall.bat amd64' -midltests_tcp needs 64-bit support!"
30 #define MIDLTESTS_C_CODE 1
31 #include "midltests.idl"
34 #define LISTEN_IP "127.0.0.1"
38 #define FORWARD_IP "127.0.0.1"
42 #define CONNECT_IP "127.0.0.1"
45 struct NDRTcpThreadCtx;
47 struct NDRProxyThreadCtx {
48 const struct NDRTcpThreadCtx *ctx;
55 struct NDRTcpThreadCtx {
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 */
74 static void dump_packet(const char *ctx, const char *direction,
75 const unsigned char *buf, int len)
77 struct dcerpc_header *hdr = (struct dcerpc_header *)buf;
79 if (len < sizeof(struct dcerpc_header)) {
80 printf("%s:%s: invalid dcerpc pdu len(%d)\n",
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",
89 hdr->rpc_vers, hdr->rpc_vers_minor);
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);
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);
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);
123 printf("%s:%s: ptype[bind] flen[%d] call[%d] contexts[%d]\n\n"
124 ctx, direction, hdr->frag_length, hdr->call_id,
126 dump_data(buf + 24, len - 24);
132 case 12: /* bind ack */
134 printf("%s:%s: ptype[bind_ack] flen[%d] call[%d]\n\n",
135 ctx, direction, hdr->frag_length, hdr->call_id);
140 case 14: /* alter_req */
142 printf("%s:%s: ptype[alter_req] flen[%d] call[%d] contexts[%d]\n\n",
143 ctx, direction, hdr->frag_length, hdr->call_id,
145 //dump_data(buf + 24, len - 24);
151 case 15: /* alter_ack */
153 printf("%s:%s: ptype[alter_ack] flen[%d] call[%d]\n\n",
154 ctx, direction, hdr->frag_length, hdr->call_id);
160 printf("%s:%s: ptype[%d] flen[%d] call[%d]\n\n",
161 ctx, direction, hdr->ptype, hdr->frag_length, hdr->call_id);
167 static void change_packet(const char *ctx, BOOL ndr64,
168 unsigned char *buf, int len)
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
177 if (len < sizeof(struct dcerpc_header)) {
178 printf("%s: invalid dcerpc pdu len(%d)\n",
184 if (hdr->rpc_vers != 5 || hdr->rpc_vers_minor != 0) {
185 printf("%s: invalid dcerpc pdu len(%d) ver:%d min:%d\n",
187 hdr->rpc_vers, hdr->rpc_vers_minor);
192 if (hdr->frag_length != len) {
193 printf("%s: invalid dcerpc pdu len(%d) should be (%d)\n",
194 ctx, len, hdr->frag_length);
199 switch (hdr->ptype) {
201 case 14: /* alter_req */
206 ret = memcmp(&buf[0x60], ndr64_buf, 16);
212 if (is_ndr64 && !ndr64) {
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");
224 } else if (is_ndr64) {
225 printf("%s: got NDR64\n\n", ctx);
227 printf("%s: got NDR32\n\n", ctx);
229 //printf("%s: bind with %u pres\n", ctx, buf[24]);
235 DWORD WINAPI NDRProxyThread(LPVOID lpParameter)
237 struct NDRProxyThreadCtx *p = (struct NDRProxyThreadCtx *)lpParameter;
239 while (!p->ctx->stop) {
244 r = recv(p->InSocket, buf, sizeof(buf), 0);
246 ret = WSAGetLastError();
247 printf("%s: recv(in) failed[%d][%d]\n", p->ctx->name, r, ret);
252 change_packet(p->ctx->name, p->ctx->ndr64, buf, r);
255 dump_packet(p->ctx->name, "in => out", buf, r);
258 s = send(p->OutSocket, buf, r, 0);
260 ret = WSAGetLastError();
261 printf("%s: send(out) failed[%d][%d]\n", p->ctx->name, s, ret);
266 r = recv(p->OutSocket, buf, sizeof(buf), 0);
268 ret = WSAGetLastError();
269 printf("%s: recv(out) failed[%d][%d]\n", p->ctx->name, r, ret);
274 dump_packet(p->ctx->name, "out => in", buf, r);
277 s = send(p->InSocket, buf, r, 0);
279 ret = WSAGetLastError();
280 printf("%s: send(in) failed[%d][%d]\n", p->ctx->name, s, ret);
287 closesocket(p->InSocket);
288 closesocket(p->OutSocket);
290 printf("NDRTcpThread[%s] stop\n", p->ctx->name);
295 DWORD WINAPI NDRTcpThread(LPVOID lpParameter)
297 struct NDRTcpThreadCtx *ctx = (struct NDRTcpThreadCtx *)lpParameter;
300 struct sockaddr_in saServer;
301 struct sockaddr_in saClient;
303 //printf("NDRTcpThread[%s] start\n", ctx->name);
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);
314 saServer.sin_family = AF_INET;
315 saServer.sin_addr.s_addr = inet_addr(LISTEN_IP);
316 saServer.sin_port = htons(ctx->listen_port);
318 saClient.sin_family = AF_INET;
319 saClient.sin_addr.s_addr = inet_addr(FORWARD_IP);
320 saClient.sin_port = htons(ctx->client_port);
322 ret = bind(ListenSocket, (SOCKADDR*)&saServer, sizeof(saServer));
323 if (ret == SOCKET_ERROR) {
324 ret = WSAGetLastError();
325 printf("bind() failed[%d]\n", ret);
330 ret = listen(ListenSocket, 10);
331 if (ret == SOCKET_ERROR) {
332 ret = WSAGetLastError();
333 printf("listen() failed[%d]\n", ret);
339 struct sockaddr_in sa;
340 int sa_len = sizeof(sa);
341 struct NDRProxyThreadCtx *p = malloc(sizeof(*p));
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);
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);
357 closesocket(p->InSocket);
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);
366 closesocket(p->InSocket);
367 closesocket(p->OutSocket);
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");
385 //printf("NDRTcpThread[%s] stop\n", ctx->name);
389 printf("NDRTcpThread[%s] failed[%d]\n", ctx->name, ret);
394 struct NDRRpcThreadCtx {
399 DWORD WINAPI NDRRpcThread(LPVOID lpParameter)
401 struct NDRRpcThreadCtx *ctx = (struct NDRRpcThreadCtx *)lpParameter;
404 RPC_BINDING_VECTOR *pBindingVector;
406 #define RPC_MIN_CALLS 1
407 #define RPC_MAX_CALLS 20
409 //printf("NDRRpcThread[%s] start\n", ctx->name);
411 status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "5055", NULL);
413 printf("Failed to register ncacn_ip_tcp endpoint\n");
418 status = RpcServerInqBindings(&pBindingVector);
420 printf("Failed RpcServerInqBindings\n");
426 status = RpcEpRegister(srv_midltests_v0_0_s_ifspec, pBindingVector, NULL, "midltests server");
428 printf("Failed RpcEpRegister\n");
433 status = RpcServerRegisterIf(srv_midltests_v0_0_s_ifspec, NULL, NULL);
435 printf("Failed to register interface\n");
440 status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
442 printf("RpcServerListen returned error %d\n", status);
447 printf("NDRRpcThread[%s] stop\n", ctx->name);
451 printf("NDRRpcThread[%s] failed[%d]\n", ctx->name, ret);
456 int main(int argc, char **argv)
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);
469 ret = WSAStartup(wVersionRequested, &wsaData);
471 printf("WSAStartup failed with error: %d\n", ret);
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");
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");
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");
527 printf("Wait for setup of server threads\n");
529 ret = WaitForMultipleObjects(3, hThreadArray, TRUE, 3000);
530 if (ret == WAIT_TIMEOUT) {
533 printf("Failed to setup of server threads %d:%d\n",
534 ret, GetLastError());
540 printf("\nTest NDR32\n\n");
542 binding = "ncacn_ip_tcp:" CONNECT_IP "[5032]";
543 status = RpcBindingFromStringBinding(
545 &midltests_IfHandle);
547 printf("RpcBindingFromStringBinding returned %d\n", status);
555 ret = RpcExceptionCode();
556 printf("NDR32 Runtime error 0x%x\n", ret);
559 ctx_ndr32.stop = TRUE;
563 printf("\nTest NDR64\n\n");
564 binding = "ncacn_ip_tcp:" CONNECT_IP "[5064]";
565 status = RpcBindingFromStringBinding(
567 &midltests_IfHandle);
569 printf("RpcBindingFromStringBinding returned %d\n", status);
577 ret = RpcExceptionCode();
578 printf("Runtime error 0x%x\n", ret);
581 ctx_ndr64.stop = TRUE;
583 WaitForMultipleObjects(3, hThreadArray, TRUE, 2000);
586 printf("\nTest OK\n");
589 printf("\nTest FAILED[%d]\n", ret);