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]\n\n",
105 ctx, direction, hdr->frag_length,
107 dump_data(buf + 24, len - 24);
112 case 2: /* response */
113 printf("\n%s:%s: ptype[response] flen[%d] plen[%d]\n\n",
114 ctx, direction, hdr->frag_length,
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;
172 if (len < sizeof(struct dcerpc_header)) {
173 printf("%s: invalid dcerpc pdu len(%d)\n",
179 if (hdr->rpc_vers != 5 || hdr->rpc_vers_minor != 0) {
180 printf("%s: invalid dcerpc pdu len(%d) ver:%d min:%d\n",
182 hdr->rpc_vers, hdr->rpc_vers_minor);
187 if (hdr->frag_length != len) {
188 printf("%s: invalid dcerpc pdu len(%d) should be (%d)\n",
189 ctx, len, hdr->frag_length);
194 switch (hdr->ptype) {
196 if (buf[24] == 3 && !ndr64) {
198 printf("%s: disable NDR64\n\n", ctx);
199 } else if (buf[24] < 3 && ndr64) {
201 printf("\n\tERROR!!!\n\n");
202 printf("%s: disable NDR32\n", ctx);
204 printf("You may need to run 'vcvarsall.bat amd64' before 'nmake tcp'\n");
206 printf("%s: got NDR64\n\n", ctx);
208 //printf("%s: bind with %u pres\n", ctx, buf[24]);
214 DWORD WINAPI NDRProxyThread(LPVOID lpParameter)
216 struct NDRProxyThreadCtx *p = (struct NDRProxyThreadCtx *)lpParameter;
218 while (!p->ctx->stop) {
223 r = recv(p->InSocket, buf, sizeof(buf), 0);
225 ret = WSAGetLastError();
226 printf("%s: recv(in) failed[%d][%d]\n", p->ctx->name, r, ret);
231 change_packet(p->ctx->name, p->ctx->ndr64, buf, r);
234 dump_packet(p->ctx->name, "in => out", buf, r);
237 s = send(p->OutSocket, buf, r, 0);
239 ret = WSAGetLastError();
240 printf("%s: send(out) failed[%d][%d]\n", p->ctx->name, s, ret);
245 r = recv(p->OutSocket, buf, sizeof(buf), 0);
247 ret = WSAGetLastError();
248 printf("%s: recv(out) failed[%d][%d]\n", p->ctx->name, r, ret);
253 dump_packet(p->ctx->name, "out => in", buf, r);
256 s = send(p->InSocket, buf, r, 0);
258 ret = WSAGetLastError();
259 printf("%s: send(in) failed[%d][%d]\n", p->ctx->name, s, ret);
266 closesocket(p->InSocket);
267 closesocket(p->OutSocket);
269 printf("NDRTcpThread[%s] stop\n", p->ctx->name);
274 DWORD WINAPI NDRTcpThread(LPVOID lpParameter)
276 struct NDRTcpThreadCtx *ctx = (struct NDRTcpThreadCtx *)lpParameter;
279 struct sockaddr_in saServer;
280 struct sockaddr_in saClient;
282 //printf("NDRTcpThread[%s] start\n", ctx->name);
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);
293 saServer.sin_family = AF_INET;
294 saServer.sin_addr.s_addr = inet_addr(LISTEN_IP);
295 saServer.sin_port = htons(ctx->listen_port);
297 saClient.sin_family = AF_INET;
298 saClient.sin_addr.s_addr = inet_addr(FORWARD_IP);
299 saClient.sin_port = htons(ctx->client_port);
301 ret = bind(ListenSocket, (SOCKADDR*)&saServer, sizeof(saServer));
302 if (ret == SOCKET_ERROR) {
303 ret = WSAGetLastError();
304 printf("bind() failed[%d]\n", ret);
309 ret = listen(ListenSocket, 10);
310 if (ret == SOCKET_ERROR) {
311 ret = WSAGetLastError();
312 printf("listen() failed[%d]\n", ret);
318 struct sockaddr_in sa;
319 int sa_len = sizeof(sa);
320 struct NDRProxyThreadCtx *p = malloc(sizeof(*p));
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);
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);
336 closesocket(p->InSocket);
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);
345 closesocket(p->InSocket);
346 closesocket(p->OutSocket);
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");
364 //printf("NDRTcpThread[%s] stop\n", ctx->name);
368 printf("NDRTcpThread[%s] failed[%d]\n", ctx->name, ret);
373 struct NDRRpcThreadCtx {
378 DWORD WINAPI NDRRpcThread(LPVOID lpParameter)
380 struct NDRRpcThreadCtx *ctx = (struct NDRRpcThreadCtx *)lpParameter;
383 RPC_BINDING_VECTOR *pBindingVector;
385 #define RPC_MIN_CALLS 1
386 #define RPC_MAX_CALLS 20
388 //printf("NDRRpcThread[%s] start\n", ctx->name);
390 status = RpcServerUseProtseqEp("ncacn_ip_tcp", RPC_MAX_CALLS, "5055", NULL);
392 printf("Failed to register ncacn_ip_tcp endpoint\n");
397 status = RpcServerInqBindings(&pBindingVector);
399 printf("Failed RpcServerInqBindings\n");
405 status = RpcEpRegister(srv_midltests_v0_0_s_ifspec, pBindingVector, NULL, "midltests server");
407 printf("Failed RpcEpRegister\n");
412 status = RpcServerRegisterIf(srv_midltests_v0_0_s_ifspec, NULL, NULL);
414 printf("Failed to register interface\n");
419 status = RpcServerListen(RPC_MIN_CALLS, RPC_MAX_CALLS, FALSE);
421 printf("RpcServerListen returned error %d\n", status);
426 printf("NDRRpcThread[%s] stop\n", ctx->name);
430 printf("NDRRpcThread[%s] failed[%d]\n", ctx->name, ret);
435 int main(int argc, char **argv)
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);
448 ret = WSAStartup(wVersionRequested, &wsaData);
450 printf("WSAStartup failed with error: %d\n", ret);
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");
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");
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");
506 printf("Wait for setup of server threads\n");
508 ret = WaitForMultipleObjects(3, hThreadArray, TRUE, 3000);
509 if (ret == WAIT_TIMEOUT) {
512 printf("Failed to setup of server threads %d:%d\n",
513 ret, GetLastError());
519 printf("\nTest NDR32\n\n");
521 binding = "ncacn_ip_tcp:" CONNECT_IP "[5032]";
522 status = RpcBindingFromStringBinding(
524 &midltests_IfHandle);
526 printf("RpcBindingFromStringBinding returned %d\n", status);
534 ret = RpcExceptionCode();
535 printf("NDR32 Runtime error 0x%x\n", ret);
538 ctx_ndr32.stop = TRUE;
542 printf("\nTest NDR64\n\n");
543 binding = "ncacn_ip_tcp:" CONNECT_IP "[5064]";
544 status = RpcBindingFromStringBinding(
546 &midltests_IfHandle);
548 printf("RpcBindingFromStringBinding returned %d\n", status);
556 ret = RpcExceptionCode();
557 printf("Runtime error 0x%x\n", ret);
560 ctx_ndr64.stop = TRUE;
562 WaitForMultipleObjects(3, hThreadArray, TRUE, 2000);
565 printf("\nTest OK\n");
568 printf("\nTest FAILED[%d]\n", ret);