public static void interpreter(Stream stream, thinCLIProtocol tCLIprotocol) { string filename = ""; string path = ""; string msg = ""; while (!tCLIprotocol.dropOut) { Types.unmarshal_int32(stream); // total message length (unused here) Messages.tag t = Messages.unmarshal_tag(stream); switch (t) { case Messages.tag.Command: t = Messages.unmarshal_tag(stream); switch (t) { case Messages.tag.Print: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Print: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.PrintStderr: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: PrintStderr: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.Debug: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Debug: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.Exit: int code = Types.unmarshal_int(stream); tCLIprotocol.dGlobalDebug("Read: Command Exit " + code, tCLIprotocol); tCLIprotocol.dExit(code); break; case Messages.tag.Error: tCLIprotocol.dGlobalDebug("Read: Command Error", tCLIprotocol); string err_code = Types.unmarshal_string(stream); tCLIprotocol.dConsoleWriteLine("Error code: " + err_code); tCLIprotocol.dConsoleWrite("Error params: "); int length = Types.unmarshal_int(stream); for (int i = 0; i < length; i++) { string param = Types.unmarshal_string(stream); tCLIprotocol.dConsoleWrite(param); if (i != (length - 1)) tCLIprotocol.dConsoleWrite(", "); } tCLIprotocol.dConsoleWriteLine(""); break; case Messages.tag.Prompt: tCLIprotocol.dGlobalDebug("Read: Command Prompt", tCLIprotocol); string response = tCLIprotocol.dConsoleReadLine(); tCLIprotocol.dConsoleWriteLine("Read "+response); /* NB, 4+4+4 here for the blob, chunk and string length, put in by the marshal_string function. A franken-marshal. */ Types.marshal_int(stream, 4 + 4 + 4); // length Messages.marshal_tag(stream, Messages.tag.Blob); Messages.marshal_tag(stream, Messages.tag.Chunk); Types.marshal_string(stream, response); Types.marshal_int(stream, 4 + 4); // length Messages.marshal_tag(stream, Messages.tag.Blob); Messages.marshal_tag(stream, Messages.tag.End); break; case Messages.tag.Load: filename = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Load " + filename, tCLIprotocol); Messages.load(stream, filename, tCLIprotocol); break; case Messages.tag.HttpPut: filename = Types.unmarshal_string(stream); path = Types.unmarshal_string(stream); Uri uri = ParseUri(path, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: HttpPut " + filename + " -> " + uri, tCLIprotocol); Messages.http_put(stream, filename, uri, tCLIprotocol); break; case Messages.tag.HttpGet: filename = Types.unmarshal_string(stream); path = Types.unmarshal_string(stream); uri = ParseUri(path, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: HttpGet " + filename + " -> " + uri, tCLIprotocol); Messages.http_get(stream, filename, uri, tCLIprotocol); break; default: Messages.protocol_failure("Command", t, tCLIprotocol); break; } break; default: Messages.protocol_failure("Message", t, tCLIprotocol); break; } } }
public static void performCommand(string Body, thinCLIProtocol tCLIprotocol) { string body = Body; body += "username="******"\n"; body += "password="******"\n"; if (body.Length != 0) { body = body.Substring(0, body.Length - 1); // strip last "\n" } string header = "POST /cli HTTP/1.0\r\n"; string content_length = "content-length: " + Encoding.UTF8.GetBytes(body).Length + "\r\n"; string tosend = header + content_length + "\r\n" + body; try { Stream stream = Transport.connect(tCLIprotocol, tCLIprotocol.conf.hostname, tCLIprotocol.conf.port); if (stream == null) { // The SSL functions already tell us what happened tCLIprotocol.dExit(1); } byte[] message = Encoding.UTF8.GetBytes(tosend); stream.Write(message, 0, message.Length); stream.Flush(); Messages.version_handshake(stream, tCLIprotocol); interpreter(stream, tCLIprotocol); } catch (SocketException) { tCLIprotocol.dGlobalError("Connection to " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port + " refused."); tCLIprotocol.dExit(1); } catch (Exception e) { if (tCLIprotocol.conf.debug) throw e; tCLIprotocol.dGlobalError("Caught exception: " + e.Message); tCLIprotocol.dExit(1); } }
public static void version_handshake(Stream stream, thinCLIProtocol tCLIprotocol) { /* Check for the initial magic string */ byte[] magic = Types.unmarshal_n(stream, (uint)tCLIprotocol.magic_string.Length); for (int i = 0; i < tCLIprotocol.magic_string.Length; i++) { if (magic[i] != tCLIprotocol.magic_string[i]) { tCLIprotocol.dGlobalError("Failed to find a server on " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port); tCLIprotocol.dGlobalUsage(); tCLIprotocol.dExit(1); } } /* Read the remote version numbers */ int remote_major = Types.unmarshal_int(stream); int remote_minor = Types.unmarshal_int(stream); tCLIprotocol.dGlobalDebug("Remote host has version " + remote_major + "." + remote_minor, tCLIprotocol); tCLIprotocol.dGlobalDebug("Client has version " + tCLIprotocol.major + "." + tCLIprotocol.minor, tCLIprotocol); if (tCLIprotocol.major != remote_major) { tCLIprotocol.dGlobalError("Protocol version mismatch talking to server on " + tCLIprotocol.conf.hostname + ":" + tCLIprotocol.conf.port); tCLIprotocol.dGlobalUsage(); tCLIprotocol.dExit(1); } /* Tell the server our version numbers */ for (int i = 0; i < tCLIprotocol.magic_string.Length; i++) { stream.WriteByte((byte)tCLIprotocol.magic_string[i]); } Types.marshal_int(stream, tCLIprotocol.major); Types.marshal_int(stream, tCLIprotocol.minor); }
public static void protocol_failure(string msg, tag t, thinCLIProtocol tCLIprotocol) { tCLIprotocol.dGlobalError("Protocol failure: Reading " + msg + ": unexpected tag: " + t); tCLIprotocol.dExit(1); }
public static void protocol_failure(string msg, tag t, thinCLIProtocol tCLIprotocol) { tCLIprotocol.dGlobalError("Protocol failure: Reading " + msg + ": unexpected tag: " + t); tCLIprotocol.dExit(1); }
public static void interpreter(Stream stream, thinCLIProtocol tCLIprotocol) { string filename = ""; string path = ""; string msg = ""; while (!tCLIprotocol.dropOut) { Types.unmarshal_int32(stream); // total message length (unused here) Messages.tag t = Messages.unmarshal_tag(stream); switch (t) { case Messages.tag.Command: t = Messages.unmarshal_tag(stream); switch (t) { case Messages.tag.Print: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Print: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.PrintStderr: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: PrintStderr: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.Debug: msg = Types.unmarshal_string(stream); tCLIprotocol.dGlobalDebug("Read: Debug: " + msg, tCLIprotocol); tCLIprotocol.dConsoleWriteLine(msg); break; case Messages.tag.Exit: int code = Types.unmarshal_int(stream); tCLIprotocol.dGlobalDebug("Read: Command Exit " + code, tCLIprotocol); tCLIprotocol.dExit(code); break; case Messages.tag.Error: tCLIprotocol.dGlobalDebug("Read: Command Error", tCLIprotocol); string err_code = Types.unmarshal_string(stream); tCLIprotocol.dConsoleWriteLine("Error code: " + err_code); tCLIprotocol.dConsoleWrite("Error params: "); int length = Types.unmarshal_int(stream); for (int i = 0; i < length; i++) { string param = Types.unmarshal_string(stream); tCLIprotocol.dConsoleWrite(param); if (i != (length - 1)) { tCLIprotocol.dConsoleWrite(", "); } } tCLIprotocol.dConsoleWriteLine(""); break; case Messages.tag.Prompt: tCLIprotocol.dGlobalDebug("Read: Command Prompt", tCLIprotocol); string response = tCLIprotocol.dConsoleReadLine(); tCLIprotocol.dConsoleWriteLine("Read " + response); /* NB, 4+4+4 here for the blob, chunk and string length, put in by the marshal_string * function. A franken-marshal. */ Types.marshal_int(stream, 4 + 4 + 4); // length Messages.marshal_tag(stream, Messages.tag.Blob); Messages.marshal_tag(stream, Messages.tag.Chunk); Types.marshal_string(stream, response); Types.marshal_int(stream, 4 + 4); // length Messages.marshal_tag(stream, Messages.tag.Blob); Messages.marshal_tag(stream, Messages.tag.End); break; case Messages.tag.Load: filename = Types.unmarshal_string(stream); CheckPermitFiles(filename, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: Load " + filename, tCLIprotocol); Messages.load(stream, filename, tCLIprotocol); break; case Messages.tag.HttpPut: filename = Types.unmarshal_string(stream); CheckPermitFiles(filename, tCLIprotocol); path = Types.unmarshal_string(stream); Uri uri = ParseUri(path, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: HttpPut " + filename + " -> " + uri, tCLIprotocol); Messages.http_put(stream, filename, uri, tCLIprotocol); break; case Messages.tag.HttpGet: filename = Types.unmarshal_string(stream); CheckPermitFiles(filename, tCLIprotocol, true); path = Types.unmarshal_string(stream); uri = ParseUri(path, tCLIprotocol); tCLIprotocol.dGlobalDebug("Read: HttpGet " + filename + " -> " + uri, tCLIprotocol); Messages.http_get(stream, filename, uri, tCLIprotocol); break; default: Messages.protocol_failure("Command", t, tCLIprotocol); break; } break; default: Messages.protocol_failure("Message", t, tCLIprotocol); break; } } }