public override void MessageReceived(ChannelHandlerContext ctx, MessageEvent e) { ChannelBuffer buf = (ChannelBuffer)e.GetMessage(); // Read reply if (!ValidMessageLength(buf.ReadableBytes())) { e.GetChannel().Close(); return; } // handling fragment header for TCP, 4 bytes. byte[] fragmentHeader = Arrays.CopyOfRange(buf.Array(), 0, 4); int fragmentSize = XDR.FragmentSize(fragmentHeader); bool isLast = XDR.IsLastFragment(fragmentHeader); System.Diagnostics.Debug.Assert((fragmentSize == 28 && isLast == true)); XDR xdr = new XDR(); xdr.WriteFixedOpaque(Arrays.CopyOfRange(buf.Array(), 4, buf.ReadableBytes())); RpcReply reply = RpcReply.Read(xdr); if (reply.GetState() == RpcReply.ReplyState.MsgAccepted) { RpcAcceptedReply acceptedReply = (RpcAcceptedReply)reply; Handle(acceptedReply, xdr); } else { RpcDeniedReply deniedReply = (RpcDeniedReply)reply; Handle(deniedReply); } e.GetChannel().Close(); }
public virtual void TestUnprivilegedPort() { // Don't allow connections from unprivileged ports. Given that this test is // presumably not being run by root, this will be the case. int serverPort = StartRpcServer(false); XDR xdrOut = CreateGetportMount(); int bufsize = 2 * 1024 * 1024; byte[] buffer = new byte[bufsize]; xdrOut.WriteFixedOpaque(buffer); // Send the request to the server TestRequest(xdrOut, serverPort); // Verify the server rejected the request. Assert.Equal(0, resultSize); // Ensure that the NULL procedure does in fact succeed. xdrOut = new XDR(); CreatePortmapXDRheader(xdrOut, 0); int headerSize = xdrOut.Size(); buffer = new byte[bufsize]; xdrOut.WriteFixedOpaque(buffer); int requestSize = xdrOut.Size() - headerSize; // Send the request to the server TestRequest(xdrOut, serverPort); // Verify the server did not reject the request. Assert.Equal(requestSize, resultSize); }
internal static XDR CreateGetportMount() { XDR xdr_out = new XDR(); CreatePortmapXDRheader(xdr_out, 3); return(xdr_out); }
protected internal override void HandleInternal(ChannelHandlerContext ctx, RpcInfo info) { // This is just like what's done in RpcProgramMountd#handleInternal and // RpcProgramNfs3#handleInternal. RpcCall rpcCall = (RpcCall)info.Header(); int procedure = rpcCall.GetProcedure(); if (procedure != 0) { bool portMonitorSuccess = DoPortMonitoring(info.RemoteAddress()); if (!portMonitorSuccess) { SendRejectedReply(rpcCall, info.RemoteAddress(), ctx); return; } } resultSize = info.Data().ReadableBytes(); RpcAcceptedReply reply = RpcAcceptedReply.GetAcceptInstance(1234, new VerifierNone ()); XDR @out = new XDR(); reply.Write(@out); ChannelBuffer b = ChannelBuffers.WrappedBuffer(@out.AsReadOnlyWrap().Buffer()); RpcResponse rsp = new RpcResponse(b, info.RemoteAddress()); RpcUtil.SendRpcResponse(ctx, rsp); }
public SimpleTcpClient(string host, int port, XDR request, bool oneShot) { this.host = host; this.port = port; this.request = request; this.oneShot = oneShot; }
public virtual void TestSingleFrame() { RpcUtil.RpcFrameDecoder decoder = new RpcUtil.RpcFrameDecoder(); // Test "Length field is not received yet" ByteBuffer buffer = ByteBuffer.Allocate(1); ChannelBuffer buf = new ByteBufferBackedChannelBuffer(buffer); ChannelBuffer channelBuffer = (ChannelBuffer)decoder.Decode(Org.Mockito.Mockito.Mock <ChannelHandlerContext>(), Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel >(), buf); Assert.True(channelBuffer == null); // Test all bytes are not received yet byte[] fragment = new byte[4 + 9]; fragment[0] = unchecked ((byte)(1 << 7)); // final fragment fragment[1] = 0; fragment[2] = 0; fragment[3] = unchecked ((byte)10); // fragment size = 10 bytes Assert.True(XDR.IsLastFragment(fragment)); Assert.True(XDR.FragmentSize(fragment) == 10); buffer = ByteBuffer.Allocate(4 + 9); buffer.Put(fragment); buffer.Flip(); buf = new ByteBufferBackedChannelBuffer(buffer); channelBuffer = (ChannelBuffer)decoder.Decode(Org.Mockito.Mockito.Mock <ChannelHandlerContext >(), Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel>(), buf); Assert.True(channelBuffer == null); }
public static RpcAcceptedReply Read(int xid, RpcReply.ReplyState replyState, XDR xdr) { Verifier verifier = Verifier.ReadFlavorAndVerifier(xdr); RpcAcceptedReply.AcceptState acceptState = RpcAcceptedReply.AcceptState.FromValue (xdr.ReadInt()); return(new RpcAcceptedReply(xid, replyState, verifier, acceptState)); }
public SimpleUdpClient(string host, int port, XDR request, bool oneShot, DatagramSocket clientSocket) { this.host = host; this.port = port; this.request = request; this.oneShot = oneShot; this.clientSocket = clientSocket; }
public static RpcDeniedReply Read(int xid, RpcReply.ReplyState replyState, XDR xdr ) { Verifier verifier = Verifier.ReadFlavorAndVerifier(xdr); RpcDeniedReply.RejectState rejectState = RpcDeniedReply.RejectState.FromValue(xdr .ReadInt()); return(new RpcDeniedReply(xid, replyState, rejectState, verifier)); }
public override XDR Write(XDR xdr) { xdr.WriteInt(xid); xdr.WriteInt(messageType.GetValue()); xdr.WriteInt(replyState.GetValue()); Verifier.WriteFlavorAndVerifier(verifier, xdr); xdr.WriteInt(rejectState.GetValue()); return(xdr); }
internal static void TestRequest(XDR request, int serverPort) { // Reset resultSize so as to avoid interference from other tests in this class. resultSize = 0; SimpleTcpClient tcpClient = new SimpleTcpClient("localhost", serverPort, request, true); tcpClient.Run(); }
/// <exception cref="System.Exception"/> public override void MessageReceived(ChannelHandlerContext ctx, MessageEvent e) { RpcResponse r = (RpcResponse)e.GetMessage(); byte[] fragmentHeader = XDR.RecordMark(r.Data().ReadableBytes(), true); ChannelBuffer header = ChannelBuffers.WrappedBuffer(fragmentHeader); ChannelBuffer d = ChannelBuffers.WrappedBuffer(header, r.Data()); e.GetChannel().Write(d); }
public override void ChannelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) { // Send the request if (Log.IsDebugEnabled()) { Log.Debug("sending PRC request"); } ChannelBuffer outBuf = XDR.WriteMessageTcp(request, true); e.GetChannel().Write(outBuf); }
public override XDR Write(XDR xdr) { xdr.WriteInt(xid); xdr.WriteInt(RpcMessage.Type.RpcCall.GetValue()); xdr.WriteInt(2); xdr.WriteInt(program); xdr.WriteInt(version); xdr.WriteInt(procedure); Credentials.WriteFlavorAndCredentials(credentials, xdr); Verifier.WriteFlavorAndVerifier(verifier, xdr); return(xdr); }
public XDR AsReadOnlyWrap() { ByteBuffer b = buf.AsReadOnlyBuffer(); if (state == XDR.State.Writing) { b.Flip(); } XDR n = new XDR(b, XDR.State.Reading); return(n); }
/// <summary>Write an XDR message to a TCP ChannelBuffer</summary> public static ChannelBuffer WriteMessageTcp(XDR request, bool last) { Preconditions.CheckState(request.state == XDR.State.Writing); ByteBuffer b = request.buf.Duplicate(); b.Flip(); byte[] fragmentHeader = XDR.RecordMark(b.Limit(), last); ByteBuffer headerBuf = ByteBuffer.Wrap(fragmentHeader); // TODO: Investigate whether making a copy of the buffer is necessary. return(ChannelBuffers.CopiedBuffer(headerBuf, b)); }
protected internal static void SendRejectedReply(RpcCall call, EndPoint remoteAddress , ChannelHandlerContext ctx) { XDR @out = new XDR(); RpcDeniedReply reply = new RpcDeniedReply(call.GetXid(), RpcReply.ReplyState.MsgDenied , RpcDeniedReply.RejectState.AuthError, new VerifierNone()); reply.Write(@out); ChannelBuffer buf = ChannelBuffers.WrappedBuffer(@out.AsReadOnlyWrap().Buffer()); RpcResponse rsp = new RpcResponse(buf, remoteAddress); RpcUtil.SendRpcResponse(ctx, rsp); }
private void Handle(RpcAcceptedReply acceptedReply, XDR xdr) { RpcAcceptedReply.AcceptState acceptState = acceptedReply.GetAcceptState(); System.Diagnostics.Debug.Assert((acceptState == RpcAcceptedReply.AcceptState.Success )); bool answer = xdr.ReadBoolean(); if (answer != true) { Log.Warn("Portmap mapping registration failed, accept state:" + acceptState); } Log.Info("Portmap mapping registration succeeded"); }
private void SerializeInt(int times) { XDR w = new XDR(); for (int i = 0; i < times; ++i) { w.WriteInt(WriteValue); } XDR r = w.AsReadOnlyWrap(); for (int i_1 = 0; i_1 < times; ++i_1) { Assert.Equal(WriteValue, r.ReadInt()); } }
public virtual void TestFrames() { int serverPort = StartRpcServer(true); XDR xdrOut = CreateGetportMount(); int headerSize = xdrOut.Size(); int bufsize = 2 * 1024 * 1024; byte[] buffer = new byte[bufsize]; xdrOut.WriteFixedOpaque(buffer); int requestSize = xdrOut.Size() - headerSize; // Send the request to the server TestRequest(xdrOut, serverPort); // Verify the server got the request with right size Assert.Equal(requestSize, resultSize); }
/// <summary>Register the program with Portmap or Rpcbind</summary> protected internal virtual void Register(PortmapMapping mapEntry, bool set) { XDR mappingRequest = PortmapRequest.Create(mapEntry, set); SimpleUdpClient registrationClient = new SimpleUdpClient(host, RpcbPort, mappingRequest , registrationSocket); try { registrationClient.Run(); } catch (IOException e) { string request = set ? "Registration" : "Unregistration"; Log.Error(request + " failure with " + host + ":" + port + ", portmap entry: " + mapEntry); throw new RuntimeException(request + " failure", e); } }
private void SendAcceptedReply(RpcCall call, EndPoint remoteAddress, RpcAcceptedReply.AcceptState acceptState, ChannelHandlerContext ctx) { RpcAcceptedReply reply = RpcAcceptedReply.GetInstance(call.GetXid(), acceptState, Verifier.VerifierNone); XDR @out = new XDR(); reply.Write(@out); if (acceptState == RpcAcceptedReply.AcceptState.ProgMismatch) { @out.WriteInt(lowProgVersion); @out.WriteInt(highProgVersion); } ChannelBuffer b = ChannelBuffers.WrappedBuffer(@out.AsReadOnlyWrap().Buffer()); RpcResponse rsp = new RpcResponse(b, remoteAddress); RpcUtil.SendRpcResponse(ctx, rsp); }
public virtual void TestMultipleFrames() { RpcUtil.RpcFrameDecoder decoder = new RpcUtil.RpcFrameDecoder(); // Test multiple frames byte[] fragment1 = new byte[4 + 10]; fragment1[0] = 0; // not final fragment fragment1[1] = 0; fragment1[2] = 0; fragment1[3] = unchecked ((byte)10); // fragment size = 10 bytes NUnit.Framework.Assert.IsFalse(XDR.IsLastFragment(fragment1)); Assert.True(XDR.FragmentSize(fragment1) == 10); // decoder should wait for the final fragment ByteBuffer buffer = ByteBuffer.Allocate(4 + 10); buffer.Put(fragment1); buffer.Flip(); ChannelBuffer buf = new ByteBufferBackedChannelBuffer(buffer); ChannelBuffer channelBuffer = (ChannelBuffer)decoder.Decode(Org.Mockito.Mockito.Mock <ChannelHandlerContext>(), Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel >(), buf); Assert.True(channelBuffer == null); byte[] fragment2 = new byte[4 + 10]; fragment2[0] = unchecked ((byte)(1 << 7)); // final fragment fragment2[1] = 0; fragment2[2] = 0; fragment2[3] = unchecked ((byte)10); // fragment size = 10 bytes Assert.True(XDR.IsLastFragment(fragment2)); Assert.True(XDR.FragmentSize(fragment2) == 10); buffer = ByteBuffer.Allocate(4 + 10); buffer.Put(fragment2); buffer.Flip(); buf = new ByteBufferBackedChannelBuffer(buffer); channelBuffer = (ChannelBuffer)decoder.Decode(Org.Mockito.Mockito.Mock <ChannelHandlerContext >(), Org.Mockito.Mockito.Mock <Org.Jboss.Netty.Channel.Channel>(), buf); Assert.True(channelBuffer != null); // Complete frame should have to total size 10+10=20 Assert.Equal(20, channelBuffer.ReadableBytes()); }
public static RpcReply Read(XDR xdr) { int xid = xdr.ReadInt(); RpcMessage.Type messageType = RpcMessage.Type.FromValue(xdr.ReadInt()); Preconditions.CheckState(messageType == RpcMessage.Type.RpcReply); RpcReply.ReplyState stat = RpcReply.ReplyState.FromValue(xdr.ReadInt()); switch (stat) { case RpcReply.ReplyState.MsgAccepted: { return(RpcAcceptedReply.Read(xid, stat, xdr)); } case RpcReply.ReplyState.MsgDenied: { return(RpcDeniedReply.Read(xid, stat, xdr)); } } return(null); }
protected override object Decode(ChannelHandlerContext ctx, Org.Jboss.Netty.Channel.Channel channel, ChannelBuffer buf) { if (buf.ReadableBytes() < 4) { return(null); } buf.MarkReaderIndex(); byte[] fragmentHeader = new byte[4]; buf.ReadBytes(fragmentHeader); int length = XDR.FragmentSize(fragmentHeader); bool isLast = XDR.IsLastFragment(fragmentHeader); if (buf.ReadableBytes() < length) { buf.ResetReaderIndex(); return(null); } ChannelBuffer newFragment = buf.ReadSlice(length); if (currentFrame == null) { currentFrame = newFragment; } else { currentFrame = ChannelBuffers.WrappedBuffer(currentFrame, newFragment); } if (isLast) { ChannelBuffer completeFrame = currentFrame; currentFrame = null; return(completeFrame); } else { return(null); } }
/// <exception cref="System.Exception"/> public override void MessageReceived(ChannelHandlerContext ctx, MessageEvent e) { ChannelBuffer buf = (ChannelBuffer)e.GetMessage(); ByteBuffer b = buf.ToByteBuffer().AsReadOnlyBuffer(); XDR @in = new XDR(b, XDR.State.Reading); RpcInfo info = null; try { RpcCall callHeader = RpcCall.Read(@in); ChannelBuffer dataBuffer = ChannelBuffers.WrappedBuffer(@in.Buffer().Slice()); info = new RpcInfo(callHeader, dataBuffer, ctx, e.GetChannel(), e.GetRemoteAddress ()); } catch (Exception) { Log.Info("Malformed RPC request from " + e.GetRemoteAddress()); } if (info != null) { Channels.FireMessageReceived(ctx, info); } }
/// <exception cref="System.IO.IOException"/> public virtual void Run() { IPAddress IPAddress = Extensions.GetAddressByName(host); byte[] sendData = request.GetBytes(); byte[] receiveData = new byte[65535]; // Use the provided socket if there is one, else just make a new one. DatagramSocket socket = this.clientSocket == null ? new DatagramSocket() : this.clientSocket; try { DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.Length, IPAddress , port); socket.Send(sendPacket); socket.SetSoTimeout(500); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.Length ); socket.Receive(receivePacket); // Check reply status XDR xdr = new XDR(Arrays.CopyOfRange(receiveData, 0, receivePacket.GetLength())); RpcReply reply = RpcReply.Read(xdr); if (reply.GetState() != RpcReply.ReplyState.MsgAccepted) { throw new IOException("Request failed: " + reply.GetState()); } } finally { // If the client socket was passed in to this UDP client, it's on the // caller of this UDP client to close that socket. if (this.clientSocket == null) { socket.Close(); } } }
internal static void CreatePortmapXDRheader(XDR xdr_out, int procedure) { // Make this a method RpcCall.GetInstance(0, 100000, 2, procedure, new CredentialsNone(), new VerifierNone ()).Write(xdr_out); }
/// <summary>Write an XDR message to a UDP ChannelBuffer</summary> public static ChannelBuffer WriteMessageUdp(XDR response) { Preconditions.CheckState(response.state == XDR.State.Reading); // TODO: Investigate whether making a copy of the buffer is necessary. return(ChannelBuffers.CopiedBuffer(response.buf)); }
/// <summary>check if the rest of data has more than len bytes</summary> public static bool VerifyLength(XDR xdr, int len) { return(xdr.buf.Remaining() >= len); }