--- /dev/null
+/*
+ * Simple Named Pipe Client
+ * (C) 2005 Jelmer Vernooij <jelmer@samba.org>
+ * (C) 2009 Stefan Metzmacher <metze@samba.org>
+ * Published to the public domain
+ */
+
+#include <stdio.h>
+#include <windows.h>
+
+#define NPECHO_CMD_SLEEP 0
+#define NPECHO_CMD_READ 1
+#define NPECHO_CMD_WRITE 2
+#define NPECHO_CMD_NEXT_CTRL 3
+
+int main(int argc, char *argv[])
+{
+ HANDLE h;
+ DWORD numio = 0;
+ DWORD i;
+ DWORD status = 0;
+ BYTE buffer[0xFFFF];
+ struct {
+ DWORD cmd;
+ DWORD val;
+ } cmds[256];
+ DWORD num_cmds = 0;
+
+ memset(buffer, 0, sizeof(buffer));
+
+ if (argc == 9999) {
+ goto usage;
+ }
+
+ printf("create message mode named pipe\n");
+ h = CreateNamedPipe("\\\\.\\pipe\\NPECHO_MESSAGE",
+ PIPE_ACCESS_DUPLEX,
+ PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
+ PIPE_UNLIMITED_INSTANCES,
+ 4096,
+ 4096,
+ 0,
+ NULL);
+ if (h == INVALID_HANDLE_VALUE) {
+ printf("Error opening: %d\n", GetLastError());
+ return -1;
+ }
+
+wait_for_client:
+ printf("Wait for client\n");
+ ConnectNamedPipe(h, NULL);
+ printf("Connected\n");
+
+next_ctrl:
+ if (!ReadFile(h, cmds, sizeof(cmds), &numio, NULL)) {
+ printf("Error ReadFile: %d\n", GetLastError());
+ goto disconnect;
+ }
+
+ if ((numio % sizeof(cmds[0])) != 0) {
+ status = ERROR_INVALID_PARAMETER;
+ printf("Invalid numread: %d\n", numio);
+ goto send_status;
+ }
+
+ num_cmds = numio / sizeof(cmds[0]);
+
+ /* check the instructions */
+ for (i=0; i < num_cmds; i++) {
+ switch (cmds[i].cmd) {
+ case NPECHO_CMD_SLEEP:
+ break;
+ case NPECHO_CMD_READ:
+ case NPECHO_CMD_WRITE:
+ if (cmds[i].val > 0xFFFF) {
+ status = ERROR_INVALID_PARAMETER;
+ printf("cmd[%d] Invalid value: %d\n",
+ i, cmds[i].val);
+ goto send_status;
+ }
+ break;
+ case NPECHO_CMD_NEXT_CTRL:
+ break;
+ default:
+ status = STATUS_ILLEGAL_INSTRUCTION;
+ printf("cmd[%d] Invalid cmd: %d\n",
+ i, cmds[i].cmd);
+ goto send_status;
+ }
+ }
+
+ status = 0;
+ if (!WriteFile(h, &status, sizeof(status), &numio, NULL)) {
+ printf("Error writing: %d\n", GetLastError());
+ goto disconnect;
+ }
+
+ /* now do what the client told us */
+ for (i=0; i < num_cmds; i++) {
+ switch (cmds[i].cmd) {
+ case NPECHO_CMD_SLEEP:
+ Sleep(cmds[i].val);
+ break;
+ case NPECHO_CMD_READ:
+ if (!ReadFile(h, buffer, cmds[i].val, &numio, NULL)) {
+ printf("Error reading: %d\n", GetLastError());
+ goto disconnect;
+ }
+ break;
+ case NPECHO_CMD_WRITE:
+ buffer[0] = i;
+ if (!WriteFile(h, buffer, cmds[i].val, &numio, NULL)) {
+ printf("Error writing: %d\n", GetLastError());
+ goto disconnect;
+ }
+ buffer[0] = 0;
+ break;
+ case NPECHO_CMD_NEXT_CTRL:
+ goto next_ctrl;
+ }
+ }
+disconnect:
+ FlushFileBuffers(h);
+ DisconnectNamedPipe(h);
+
+ goto wait_for_client;
+
+ CloseHandle(h);
+
+ return 0;
+send_status:
+ if (!WriteFile(h, &status, sizeof(status), &numio, NULL)) {
+ printf("Error writing: %d\n", GetLastError());
+ return -1;
+ }
+ return -1;
+usage:
+ printf("Usage: %s pipename [mode]\n", argv[0]);
+ printf(" Where pipename is something like \\\\servername\\PIPE\\NPECHO\n");
+ printf(" Where mode is 'byte' or 'message'\n");
+ return -1;
+}