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] call[%d] plen[%d] ahint[%d]\n\n",
105 ctx, direction, hdr->frag_length, hdr->call_id,
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] call[%d] plen[%d] ahint[%d]\n\n",
114 ctx, direction, hdr->frag_length, hdr->call_id,
115 len - 24, *(DWORD *)(&buf[0x10]));
116 dump_data(buf + 24, len - 24);
122 printf("%s:%s: ptype[fault] flen[%d] call[%d] plen[%d]\n\n",
123 ctx, direction, hdr->ptype, hdr->frag_length, hdr->call_id,
125 dump_data(buf + 24, len - 24);
132 printf("%s:%s: ptype[bind] flen[%d] call[%d] contexts[%d]\n\n"
133 ctx, direction, hdr->frag_length, hdr->call_id,
135 dump_data(buf + 24, len - 24);
141 case 12: /* bind ack */
143 printf("%s:%s: ptype[bind_ack] flen[%d] call[%d]\n\n",
144 ctx, direction, hdr->frag_length, hdr->call_id);
149 case 14: /* alter_req */
151 printf("%s:%s: ptype[alter_req] flen[%d] call[%d] contexts[%d]\n\n",
152 ctx, direction, hdr->frag_length, hdr->call_id,
154 //dump_data(buf + 24, len - 24);
160 case 15: /* alter_ack */
162 printf("%s:%s: ptype[alter_ack] flen[%d] call[%d]\n\n",
163 ctx, direction, hdr->frag_length, hdr->call_id);
169 printf("%s:%s: ptype[%d] flen[%d] call[%d]\n\n",
170 ctx, direction, hdr->ptype, hdr->frag_length, hdr->call_id);
176 static void change_packet(const char *ctx, BOOL ndr64,
177 unsigned char *buf, int len)
179 struct dcerpc_header *hdr = (struct dcerpc_header *)buf;
180 BOOL is_ndr64 = FALSE;
181 const unsigned char ndr64_buf[] = {
182 0x33, 0x05, 0x71, 0x71, 0xBA, 0xBE, 0x37, 0x49,
183 0x83, 0x19, 0xB5, 0xDB, 0xEF, 0x9C, 0xCC, 0x36
186 if (len < sizeof(struct dcerpc_header)) {
187 printf("%s: invalid dcerpc pdu len(%d)\n",
193 if (hdr->rpc_vers != 5 || hdr->rpc_vers_minor != 0) {
194 printf("%s: invalid dcerpc pdu len(%d) ver:%d min:%d\n",
196 hdr->rpc_vers, hdr->rpc_vers_minor);
201 if (hdr->frag_length != len) {
202 printf("%s: invalid dcerpc pdu len(%d) should be (%d)\n",
203 ctx, len, hdr->frag_length);
208 switch (hdr->ptype) {
210 case 14: /* alter_req */
215 ret = memcmp(&buf[0x60], ndr64_buf, 16);
221 if (is_ndr64 && !ndr64) {
223 memset(&buf[0x60], 0xFF, 16);
224 printf("%s: disable NDR64\n\n", ctx);
225 } else if (!is_ndr64 && ndr64) {
226 printf("\n%s: got NDR32 downgrade\n\n", ctx);
227 #ifndef DONOT_FORCE_NDR64
228 printf("\n\tERROR!!!\n\n");
229 memset(&buf[0x34], 0xFF, 16);
230 printf("You may need to run 'vcvarsall.bat amd64' before 'nmake tcp'\n");
233 } else if (is_ndr64) {
234 printf("%s: got NDR64\n\n", ctx);
236 printf("%s: got NDR32\n\n", ctx);
238 //printf("%s: bind with %u pres\n", ctx, buf[24]);
244 static int sock_pending(SOCKET s)
250 ret = ioctlsocket(s, FIONREAD, &value);
256 /* this should not be reached */
268 * if no data is available check if the socket is in error state. For
269 * dgram sockets it's the way to return ICMP error messages of
270 * connected sockets to the caller.
272 ret = getsockopt(s, SOL_SOCKET, SO_ERROR, (char *)&error, &len);
282 DWORD WINAPI NDRProxyThread(LPVOID lpParameter)
284 struct NDRProxyThreadCtx *p = (struct NDRProxyThreadCtx *)lpParameter;
286 while (!p->ctx->stop) {
293 ret = sock_pending(p->InSocket);
298 r = recv(p->InSocket, buf, sizeof(buf), 0);
300 ret = WSAGetLastError();
301 printf("%s: recv(in) failed[%d][%d]\n", p->ctx->name, r, ret);
306 change_packet(p->ctx->name, p->ctx->ndr64, buf, r);
309 dump_packet(p->ctx->name, "in => out", buf, r);
313 s = send(p->OutSocket, buf, r, 0);
315 ret = WSAGetLastError();
316 printf("%s: send(out) failed[%d][%d]\n", p->ctx->name, s, ret);
321 ret = sock_pending(p->OutSocket);
326 r = recv(p->OutSocket, buf, sizeof(buf), 0);
328 ret = WSAGetLastError();
329 printf("%s: recv(out) failed[%d][%d]\n", p->ctx->name, r, ret);
334 dump_packet(p->ctx->name, "out => in", buf, r);
337 s = send(p->InSocket, buf, r, 0);
339 ret = WSAGetLastError();
340 printf("%s: send(in) failed[%d][%d]\n", p->ctx->name, s, ret);
348 closesocket(p->InSocket);
349 closesocket(p->OutSocket);
351 printf("NDRTcpThread[%s] stop\n", p->ctx->name);
356 DWORD WINAPI NDRTcpThread(LPVOID lpParameter)
358 struct NDRTcpThreadCtx *ctx = (struct NDRTcpThreadCtx *)lpParameter;
361 struct sockaddr_in saServer;
362 struct sockaddr_in saClient;
364 //printf("NDRTcpThread[%s] start\n", ctx->name);
367 ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
368 if (ListenSocket == INVALID_SOCKET) {
369 ret = WSAGetLastError();
370 printf("socket() failed[%d][%d]\n", ListenSocket, ret);
375 saServer.sin_family = AF_INET;
376 saServer.sin_addr.s_addr = inet_addr(LISTEN_IP);
377 saServer.sin_port = htons(ctx->listen_port);
379 saClient.sin_family = AF_INET;
380 saClient.sin_addr.s_addr = inet_addr(FORWARD_IP);
381 saClient.sin_port = htons(ctx->client_port);
383 ret = bind(ListenSocket, (SOCKADDR*)&saServer, sizeof(saServer));
384 if (ret == SOCKET_ERROR) {
385 ret = WSAGetLastError();
386 printf("bind() failed[%d]\n", ret);
391 ret = listen(ListenSocket, 10);
392 if (ret == SOCKET_ERROR) {
393 ret = WSAGetLastError();
394 printf("listen() failed[%d]\n", ret);
400 struct sockaddr_in sa;
401 int sa_len = sizeof(sa);
402 struct NDRProxyThreadCtx *p = malloc(sizeof(*p));
405 p->InSocket = accept(ListenSocket, (SOCKADDR *)&sa, &sa_len);
406 if (p->InSocket == INVALID_SOCKET) {
407 ret = WSAGetLastError();
408 printf("%s: accept() failed[%d][%d]\n", p->ctx->name, p->InSocket, ret);
413 p->OutSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
414 if (p->OutSocket == INVALID_SOCKET) {
415 ret = WSAGetLastError();
416 printf("%s: socket(out) failed[%d][%d]\n", p->ctx->name, p->OutSocket, ret);
418 closesocket(p->InSocket);
422 ret = connect(p->OutSocket, (SOCKADDR*)&saClient, sizeof(saClient));
423 if (ret == SOCKET_ERROR) {
424 ret = WSAGetLastError();
425 printf("%s: connect() failed[%d]\n", p->ctx->name, ret);
427 closesocket(p->InSocket);
428 closesocket(p->OutSocket);
432 p->hThread = CreateThread(
433 NULL, // default security attributes
434 0, // use default stack size
435 NDRProxyThread, // thread function name
436 p, // argument to thread function
437 0, // use default creation flags
438 &p->dwThreadId);// returns the thread identifier
439 if (p->hThread == NULL) {
440 printf("failed to create thread ndr32\n");
446 //printf("NDRTcpThread[%s] stop\n", ctx->name);
450 printf("NDRTcpThread[%s] failed[%d]\n", ctx->name, ret);
455 struct NDRRpcThreadCtx {
460 DWORD WINAPI NDRRpcThread(LPVOID lpParameter)
462 struct NDRRpcThreadCtx *ctx = (struct NDRRpcThreadCtx *)lpParameter;
465 RPC_BINDING_VECTOR *pBindingVector;
467 #define RPC_MIN_CALLS 1
468 #define RPC_MAX_CALLS 20
470 //printf("NDRRpcThread[%s] start\n", ctx->name);
472 status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "5055", NULL);
474 printf("Failed to register ncacn_ip_tcp endpoint\n");
479 status = RpcServerInqBindings(&pBindingVector);
481 printf("Failed RpcServerInqBindings\n");
487 status = RpcEpRegister(srv_midltests_v0_0_s_ifspec, pBindingVector, NULL, "midltests server");
489 printf("Failed RpcEpRegister\n");
494 status = RpcServerRegisterIf(srv_midltests_v0_0_s_ifspec, NULL, NULL);
496 printf("Failed to register interface\n");
501 status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
503 printf("RpcServerListen returned error %d\n", status);
508 printf("NDRRpcThread[%s] stop\n", ctx->name);
512 printf("NDRRpcThread[%s] failed[%d]\n", ctx->name, ret);
517 int main(int argc, char **argv)
520 struct NDRTcpThreadCtx ctx_ndr32;
521 struct NDRTcpThreadCtx ctx_ndr64;
522 struct NDRRpcThreadCtx ctx_rpc;
523 DWORD dwThreadIdArray[3];
524 HANDLE hThreadArray[3];
525 WORD wVersionRequested = MAKEWORD(2, 2);
530 ret = WSAStartup(wVersionRequested, &wsaData);
532 printf("WSAStartup failed with error: %d\n", ret);
537 ctx_ndr32.name = "ndr32";
538 ctx_ndr32.listen_port = 5032;
539 ctx_ndr32.client_port = 5055;
540 ctx_ndr32.ndr64 = FALSE;
541 ctx_ndr32.stop = FALSE;
542 hThreadArray[0] = CreateThread(
543 NULL, // default security attributes
544 0, // use default stack size
545 NDRTcpThread, // thread function name
546 &ctx_ndr32, // argument to thread function
547 0, // use default creation flags
548 &dwThreadIdArray[0]); // returns the thread identifier
549 if (hThreadArray[0] == NULL) {
550 printf("failed to create thread ndr32\n");
555 ctx_ndr64.name = "ndr64";
556 ctx_ndr64.listen_port = 5064;
557 ctx_ndr64.client_port = 5055;
558 ctx_ndr64.ndr64 = TRUE;
559 ctx_ndr64.stop = FALSE;
560 hThreadArray[1] = CreateThread(
561 NULL, // default security attributes
562 0, // use default stack size
563 NDRTcpThread, // thread function name
564 &ctx_ndr64, // argument to thread function
565 0, // use default creation flags
566 &dwThreadIdArray[1]); // returns the thread identifier
567 if (hThreadArray[1] == NULL) {
568 printf("failed to create thread ndr64\n");
573 ctx_rpc.name = "rpc";
574 ctx_rpc.listen_port = 5050;
575 hThreadArray[2] = CreateThread(
576 NULL, // default security attributes
577 0, // use default stack size
578 NDRRpcThread, // thread function name
579 &ctx_rpc, // argument to thread function
580 0, // use default creation flags
581 &dwThreadIdArray[2]); // returns the thread identifier
582 if (hThreadArray[2] == NULL) {
583 printf("failed to create thread rpc\n");
588 printf("Wait for setup of server threads\n");
590 ret = WaitForMultipleObjects(3, hThreadArray, TRUE, 3000);
591 if (ret == WAIT_TIMEOUT) {
594 printf("Failed to setup of server threads %d:%d\n",
595 ret, GetLastError());
601 printf("\nTest NDR32\n\n");
603 binding = "ncacn_ip_tcp:" CONNECT_IP "[5032]";
604 status = RpcBindingFromStringBinding(
606 &midltests_IfHandle);
608 printf("RpcBindingFromStringBinding returned %d\n", status);
616 ret = RpcExceptionCode();
617 printf("NDR32 Runtime error 0x%x\n", ret);
620 ctx_ndr32.stop = TRUE;
624 printf("\nTest NDR64\n\n");
625 binding = "ncacn_ip_tcp:" CONNECT_IP "[5064]";
626 status = RpcBindingFromStringBinding(
628 &midltests_IfHandle);
630 printf("RpcBindingFromStringBinding returned %d\n", status);
638 ret = RpcExceptionCode();
639 printf("Runtime error 0x%x\n", ret);
642 ctx_ndr64.stop = TRUE;
644 WaitForMultipleObjects(3, hThreadArray, TRUE, 2000);
647 printf("\nTest OK\n");
650 printf("\nTest FAILED[%d]\n", ret);