testprogs/win32: make it possible to run in byte or message mode in npecho_server2
[metze/samba/wip.git] / testprogs / win32 / npecho / npecho_server2.c
1 /*
2  * Simple Named Pipe Client
3  * (C) 2005 Jelmer Vernooij <jelmer@samba.org>
4  * (C) 2009 Stefan Metzmacher <metze@samba.org>
5  * Published to the public domain
6  */
7
8 #include <windows.h>
9 #include <stdio.h>
10
11 #define ECHODATA "Black Dog"
12
13 int main(int argc, char *argv[])
14 {
15         HANDLE h;
16         DWORD numread = 0;
17         char *outbuffer = malloc(sizeof(ECHODATA));
18         BOOL msgmode = FALSE;
19         DWORD type = 0;
20
21         if (argc == 1) {
22                 goto usage;
23         } else if (argc >= 3) {
24                 if (strcmp(argv[2], "byte") == 0) {
25                         msgmode = FALSE;
26                 } else if (strcmp(argv[2], "message") == 0) {
27                         msgmode = TRUE;
28                 } else {
29                         goto usage;
30                 }
31         }
32
33         if (msgmode == TRUE) {
34                 printf("using message mode\n");
35                 type = PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT;
36         } else {
37                 printf("using byte mode\n");
38                 type = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT;
39         }
40
41         h = CreateNamedPipe(argv[1],
42                             PIPE_ACCESS_DUPLEX,
43                             type,
44                             PIPE_UNLIMITED_INSTANCES,
45                             1024,
46                             1024,
47                             0,
48                             NULL);
49         if (h == INVALID_HANDLE_VALUE) {
50                 printf("Error opening: %d\n", GetLastError());
51                 return -1;
52         }
53
54         ConnectNamedPipe(h, NULL);
55
56         if (!WriteFile(h, ECHODATA, sizeof(ECHODATA), &numread, NULL)) {
57                 printf("Error writing: %d\n", GetLastError());
58                 return -1;
59         }
60
61         if (!WriteFile(h, ECHODATA, sizeof(ECHODATA), &numread, NULL)) {
62                 printf("Error writing: %d\n", GetLastError());
63                 return -1;
64         }
65
66         FlushFileBuffers(h);
67         DisconnectNamedPipe(h);
68         CloseHandle(h);
69
70         return 0;
71 usage:
72         printf("Usage: %s pipename [mode]\n", argv[0]);
73         printf("  Where pipename is something like \\\\servername\\PIPE\\NPECHO\n");
74         printf("  Where mode is 'byte' or 'message'\n");
75         return -1;
76 }