Example #1
0
 private static void bindingSetAuthInfo(RPC_C_AUTHN_LEVEL level, RPC_C_AUTHN[] authTypes,
                                        RpcHandle handle, string serverPrincipalName, NetworkCredential credentails)
 {
     if (credentails == null)
     {
         foreach (RPC_C_AUTHN atype in authTypes)
         {
             RPC_STATUS result = NativeMethods.RpcBindingSetAuthInfo2(handle.Handle, serverPrincipalName, level, atype, IntPtr.Zero, 0);
             if (result != RPC_STATUS.RPC_S_OK)
             {
                 RpcTrace.Warning("Unable to register {0}, result = {1}", atype, new RpcException(result).Message);
             }
         }
     }
     else
     {
         SEC_WINNT_AUTH_IDENTITY pSecInfo = new SEC_WINNT_AUTH_IDENTITY(credentails);
         foreach (RPC_C_AUTHN atype in authTypes)
         {
             RPC_STATUS result = NativeMethods.RpcBindingSetAuthInfo(handle.Handle, serverPrincipalName, level, atype, ref pSecInfo, 0);
             if (result != RPC_STATUS.RPC_S_OK)
             {
                 RpcTrace.Warning("Unable to register {0}, result = {1}", atype, new RpcException(result).Message);
             }
         }
     }
 }
Example #2
0
        private static Ptr <RPC_SERVER_INTERFACE> Configure(MIDL_SERVER_INFO temp, RpcHandle handle, Ptr <MIDL_SERVER_INFO> me, Guid iid,
                                                            Byte[] formatTypes,
                                                            Byte[] formatProc, ExplicitBytesExecute fnExecute)
        {
            Ptr <RPC_SERVER_INTERFACE> svrIface = handle.CreatePtr(CreateExplicitBytesServer(handle, me, iid));
            Ptr <MIDL_STUB_DESC>       stub     = handle.CreatePtr(new MIDL_STUB_DESC(handle, svrIface.Handle, formatTypes, true));

            temp.pStubDesc = stub.Handle;

            IntPtr ptrFunction = handle.PinFunction(fnExecute);

            temp.DispatchTable = handle.Pin(ptrFunction);

            temp.ProcString      = handle.Pin(formatProc);
            temp.FmtStringOffset = handle.Pin(new int[1] {
                0
            });

            temp.ThunkTable      = IntPtr.Zero;
            temp.pTransferSyntax = IntPtr.Zero;
            temp.nCount          = IntPtr.Zero;
            temp.pSyntaxInfo     = IntPtr.Zero;

            //Copy us back into the pinned address
            Marshal.StructureToPtr(temp, me.Handle, false);
            return(svrIface);
        }
        private Ptr<RPC_SERVER_INTERFACE> Configure(RpcHandle handle, Ptr<MIDL_SERVER_INFO> me, Guid iid,
                                                    Byte[] formatTypes, Byte[] formatProc, ushort[] formatProcOffsets,
                                                    Delegate[] funcs)
        {
            Ptr<RPC_SERVER_INTERFACE> svrIface = handle.CreatePtr(new RPC_SERVER_INTERFACE(handle, me, iid));
            Ptr<MIDL_STUB_DESC> stub = handle.CreatePtr(new MIDL_STUB_DESC(handle, svrIface.Handle, formatTypes, true));
            pStubDesc = stub.Handle;

            var dispatches = new IntPtr[funcs.Length];
            for (var i = 0; i < funcs.Length; ++i)
            {
              dispatches[i] = handle.PinFunction(funcs[i]);
            }
            DispatchTable = handle.Pin(dispatches);

            ProcString = handle.Pin(formatProc);
            FmtStringOffset = handle.Pin(formatProcOffsets.Clone());

            ThunkTable = IntPtr.Zero;
            pTransferSyntax = IntPtr.Zero;
            nCount = IntPtr.Zero;
            pSyntaxInfo = IntPtr.Zero;

            //Copy us back into the pinned address
            Marshal.StructureToPtr(this, me.Handle, false);
            return svrIface;
        }
 /// <summary>
 /// Constructs an RPC server for the given interface guid, the guid is used to identify multiple rpc
 /// servers/services within a single process.
 /// </summary>
 public RpcServerApi(Guid iid)
 {
     IID       = iid;
     _maxCalls = MAX_CALL_LIMIT;
     _handle   = new RpcServerHandle();
     ServerRegisterInterface(_handle, IID, RpcEntryPoint);
 }
Example #5
0
        private static void bindingFromStringBinding(RpcHandle handle, String bindingString)
        {
            RPC_STATUS result = NativeMethods.RpcBindingFromStringBinding(bindingString, out handle.Handle);

            Guard.Assert(result);
            RpcTrace.Verbose("RpcClient.bindingFromStringBinding({0} = {1})", handle.Handle, bindingString);
        }
Example #6
0
 private static void BindingSetAuthInfo(RpcProtectionLevel level, RpcAuthentication[] authTypes,
                                        RpcHandle handle, string serverPrincipalName, NetworkCredential credentails)
 {
     if (credentails == null)
     {
         foreach (RpcAuthentication atype in authTypes)
         {
             RpcError result = RpcBindingSetAuthInfo2(handle.Handle, serverPrincipalName, level, atype, IntPtr.Zero, 0);
             if (result != RpcError.RPC_S_OK)
             {
                 Log.Warning("Unable to register {0}, result = {1}", atype, new RpcException(result).Message);
             }
         }
     }
     else
     {
         SEC_WINNT_AUTH_IDENTITY pSecInfo = new SEC_WINNT_AUTH_IDENTITY(credentails);
         foreach (RpcAuthentication atype in authTypes)
         {
             RpcError result = RpcBindingSetAuthInfo(handle.Handle, serverPrincipalName, level, atype, ref pSecInfo, 0);
             if (result != RpcError.RPC_S_OK)
             {
                 Log.Warning("Unable to register {0}, result = {1}", atype, new RpcException(result).Message);
             }
         }
     }
 }
        // Fields up to now present in win2000 release.

        public MIDL_STUB_DESC(RpcHandle handle, IntPtr interfaceInfo, Byte[] formatTypes, bool serverSide)
        {
            RpcInterfaceInformation = interfaceInfo;
            pfnAllocate = RpcApi.AllocPtr.Handle;
            pfnFree = RpcApi.FreePtr.Handle;
            pAutoBindHandle = serverSide ? IntPtr.Zero : handle.Pin(new IntPtr());
            apfnNdrRundownRoutines = new IntPtr();
            aGenericBindingRoutinePairs = new IntPtr();
            apfnExprEval = new IntPtr();
            aXmitQuintuple = new IntPtr();
            pFormatTypes = handle.Pin(formatTypes);
            fCheckBounds = 1;
            Version = 0x50002u;
            pMallocFreeStruct = new IntPtr();
            MIDLVersion = 0x70001f4;
            CommFaultOffsets = serverSide
                                   ? IntPtr.Zero
                                   : handle.Pin(new COMM_FAULT_OFFSETS() {CommOffset = -1, FaultOffset = -1});
            aUserMarshalQuadruple = new IntPtr();
            NotifyRoutineTable = new IntPtr();
            mFlags = new IntPtr(0x00000001);
            CsRoutineTables = new IntPtr();
            ProxyServerInfo = new IntPtr();
            pExprInfo = new IntPtr();
        }
Example #8
0
        private static RPC_SERVER_INTERFACE CreateExplicitBytesServer(RpcHandle handle, Ptr <MIDL_SERVER_INFO> pServer, Guid iid)
        {
            var server = new RPC_SERVER_INTERFACE();

            server.Length      = (uint)Marshal.SizeOf(typeof(RPC_SERVER_INTERFACE));
            server.InterfaceId = new RPC_SYNTAX_IDENTIFIER()
            {
                SyntaxGUID = iid, SyntaxVersion = ExplicitBytesConstants.INTERFACE_VERSION
            };
            server.TransferSyntax = new RPC_SYNTAX_IDENTIFIER()
            {
                SyntaxGUID = SYNTAX.SYNTAX_IID, SyntaxVersion = SYNTAX.SYNTAX_VERSION
            };

            var fnTable = new RPC_DISPATCH_TABLE();

            fnTable.DispatchTableCount = 1;
            fnTable.DispatchTable      =
                handle.Pin(new RPC_DISPATCH_TABLE_Entry()
            {
                DispatchMethod = RpcRuntime.ServerEntry.Handle, Zero = IntPtr.Zero
            });
            fnTable.Reserved = IntPtr.Zero;

            server.DispatchTable           = handle.Pin(fnTable);
            server.RpcProtseqEndpointCount = 0u;
            server.RpcProtseqEndpoint      = IntPtr.Zero;
            server.DefaultManagerEpv       = IntPtr.Zero;
            server.InterpreterInfo         = pServer.Handle;
            server.Flags = 0x04000000u;
            return(server);
        }
        private static void ServerRegisterInterface(RpcHandle handle, Guid iid, RpcExecute fnExec, int maxCalls, int maxRequestBytes, bool allowAnonTcp)
        {
            const int RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH = 0x0010;
            int       flags  = 0;
            IntPtr    fnAuth = IntPtr.Zero;

            if (allowAnonTcp)
            {
                flags  = RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH;
                fnAuth = hAuthCall.Handle;
            }

            Ptr <RPC_SERVER_INTERFACE> sIf = MIDL_SERVER_INFO.Create(handle, iid, RpcApi.TYPE_FORMAT, RpcApi.FUNC_FORMAT, fnExec);

            if (!allowAnonTcp && maxRequestBytes < 0)
            {
                RpcException.Assert(RpcServerRegisterIf(sIf.Handle, IntPtr.Zero, IntPtr.Zero));
            }
            else
            {
                RpcException.Assert(RpcServerRegisterIf2(sIf.Handle, IntPtr.Zero, IntPtr.Zero, flags,
                                                         maxCalls <= 0 ? MAX_CALL_LIMIT : maxCalls,
                                                         maxRequestBytes <= 0 ? 80 * 1024 : maxRequestBytes, fnAuth));
            }

            handle.Handle = sIf.Handle;
        }
        private Ptr <RPC_SERVER_INTERFACE> Configure(RpcHandle handle, Ptr <MIDL_SERVER_INFO> me, Guid iid,
                                                     Byte[] formatTypes, Byte[] formatProc, ushort[] formatProcOffsets,
                                                     Delegate[] funcs)
        {
            Ptr <RPC_SERVER_INTERFACE> svrIface = handle.CreatePtr(new RPC_SERVER_INTERFACE(handle, me, iid));
            Ptr <MIDL_STUB_DESC>       stub     = handle.CreatePtr(new MIDL_STUB_DESC(handle, svrIface.Handle, formatTypes, true));

            pStubDesc = stub.Handle;

            var dispatches = new IntPtr[funcs.Length];

            for (var i = 0; i < funcs.Length; ++i)
            {
                dispatches[i] = handle.PinFunction(funcs[i]);
            }
            DispatchTable = handle.Pin(dispatches);

            ProcString      = handle.Pin(formatProc);
            FmtStringOffset = handle.Pin(formatProcOffsets.Clone());

            ThunkTable      = IntPtr.Zero;
            pTransferSyntax = IntPtr.Zero;
            nCount          = IntPtr.Zero;
            pSyntaxInfo     = IntPtr.Zero;

            //Copy us back into the pinned address
            Marshal.StructureToPtr(this, me.Handle, false);
            return(svrIface);
        }
Example #11
0
        // Fields up to now present in win2000 release.

        public MIDL_STUB_DESC(RpcHandle handle, IntPtr interfaceInfo, Byte[] formatTypes, bool serverSide)
        {
            RpcInterfaceInformation = interfaceInfo;
            pfnAllocate             = RpcApi.AllocPtr.Handle;
            pfnFree                     = RpcApi.FreePtr.Handle;
            pAutoBindHandle             = serverSide ? IntPtr.Zero : handle.Pin(new IntPtr());
            apfnNdrRundownRoutines      = new IntPtr();
            aGenericBindingRoutinePairs = new IntPtr();
            apfnExprEval                = new IntPtr();
            aXmitQuintuple              = new IntPtr();
            pFormatTypes                = handle.Pin(formatTypes);
            fCheckBounds                = 1;
            Version                     = 0x50002u;
            pMallocFreeStruct           = new IntPtr();
            MIDLVersion                 = 0x70001f4;
            CommFaultOffsets            = serverSide
                                   ? IntPtr.Zero
                                   : handle.Pin(new COMM_FAULT_OFFSETS()
            {
                CommOffset = -1, FaultOffset = -1
            });
            aUserMarshalQuadruple = new IntPtr();
            NotifyRoutineTable    = new IntPtr();
            mFlags          = new IntPtr(0x00000001);
            CsRoutineTables = new IntPtr();
            ProxyServerInfo = new IntPtr();
            pExprInfo       = new IntPtr();
        }
Example #12
0
        public RPC_SERVER_INTERFACE(RpcHandle handle, Ptr <MIDL_SERVER_INFO> pServer, Guid iid)
        {
            Length      = (uint)Marshal.SizeOf(typeof(RPC_SERVER_INTERFACE));
            InterfaceId = new RPC_SYNTAX_IDENTIFIER()
            {
                SyntaxGUID = iid, SyntaxVersion = RPC_VERSION.INTERFACE_VERSION
            };
            TransferSyntax = new RPC_SYNTAX_IDENTIFIER()
            {
                SyntaxGUID = IID_SYNTAX, SyntaxVersion = RPC_VERSION.SYNTAX_VERSION
            };

            RPC_DISPATCH_TABLE fnTable = new RPC_DISPATCH_TABLE();

            fnTable.DispatchTableCount = 2;
            fnTable.DispatchTable      =
                handle.Pin(new RPC_DISPATCH_TABLE_Entry()
            {
                DispatchMethod      = RpcApi.ServerEntry.Handle,
                DispatchAsyncMethod = RpcApi.AsyncServerEntry.Handle,
                Zero = IntPtr.Zero
            });
            fnTable.Reserved = IntPtr.Zero;

            DispatchTable           = handle.Pin(fnTable);
            RpcProtseqEndpointCount = 0u;
            RpcProtseqEndpoint      = IntPtr.Zero;
            DefaultManagerEpv       = IntPtr.Zero;
            InterpreterInfo         = pServer.Handle;
            Flags = 0x04000000u;
        }
Example #13
0
 protected void ServerRegisterInterface(IntPtr sIfHandle, RpcHandle handle, IntPtr mgrTypeUuid, IntPtr mgrEpv)
 {
     //TODO: make server isolated of other process services, make it not static as possible
     Guard.Assert(NativeMethods.RpcServerRegisterIf(sIfHandle, mgrTypeUuid, mgrEpv));
     //RPC_IF_CALLBACK_FN security = null;
     // Guard.Assert(NativeMethods.RpcServerRegisterIfEx(sIf.Handle, IntPtr.Zero, IntPtr.Zero,  InterfacRegistrationFlags.RPC_IF_AUTOLISTEN, MAX_CALL_LIMIT, ref security));
     handle.Handle = sIfHandle;
 }
        public static Ptr<RPC_SERVER_INTERFACE> Create(RpcHandle handle, Guid iid, Byte[] formatTypes,
                                                      Byte[] formatProc,
                                                      ExplicitBytesExecute fnExecute)
        {
            Ptr<MIDL_SERVER_INFO> pServer = handle.CreatePtr(new MIDL_SERVER_INFO());

            MIDL_SERVER_INFO temp = new MIDL_SERVER_INFO();
            return Configure(temp,handle, pServer, iid, formatTypes, formatProc, fnExecute);
        }
Example #15
0
        private void PrepareRpcHandleAndBindingString(Guid iid, RpcProtseq protocol, string server, string endpoint)
        {
            _handle   = new RpcClientHandle();
            IID       = iid;
            _protocol = protocol;
            Log.Verbose("RpcClient('{0}:{1}')", server, endpoint);

            _binding = StringBindingCompose(protocol, server, endpoint, null);
        }
        private static void ServerRegisterInterface(RpcHandle handle, Guid iid, RpcExecute fnExec)
        {
            Log.Verbose("ServerRegisterInterface({0})", iid);
            Ptr <RPC_SERVER_INTERFACE> sIf = MIDL_SERVER_INFO.Create(handle, iid, RpcApi.TYPE_FORMAT, RpcApi.FUNC_FORMAT,
                                                                     fnExec);

            RpcException.Assert(RpcServerRegisterIf(sIf.Handle, IntPtr.Zero, IntPtr.Zero));
            handle.Handle = sIf.Handle;
        }
        internal static Ptr<RPC_SERVER_INTERFACE> Create(RpcHandle handle, Guid iid, Byte[] formatTypes,
                                                         Byte[] formatProc, ushort[] formatProcOffsets,
                                                         Delegate[] funcs)
        {
            Ptr<MIDL_SERVER_INFO> pServer = handle.CreatePtr(new MIDL_SERVER_INFO());

            MIDL_SERVER_INFO temp = new MIDL_SERVER_INFO();
            return temp.Configure(handle, pServer, iid, formatTypes, formatProc, formatProcOffsets, funcs);
        }
Example #18
0
        public Client(EndpointBindingInfo endpointBindingInfo)
        {
            _handle   = new RpcClientHandle();
            _protocol = endpointBindingInfo.Protseq;
            _binding  = stringBindingCompose(endpointBindingInfo, null);
            RpcTrace.Verbose("Client('{0}:{1}')", endpointBindingInfo.NetworkAddr, endpointBindingInfo.EndPoint);

            bindingFromStringBinding(_handle, _binding);
        }
        internal static Ptr<RPC_SERVER_INTERFACE> Create(RpcHandle handle, Guid iid, Byte[] formatTypes,
                                                         Byte[] formatProc,
                                                         RpcExecute fnExecute)
        {
            Ptr<MIDL_SERVER_INFO> pServer = handle.CreatePtr(new MIDL_SERVER_INFO());

            MIDL_SERVER_INFO temp = new MIDL_SERVER_INFO();
            return temp.Configure(handle, pServer, iid, formatTypes, formatProc, fnExecute);
        }
Example #20
0
        public static Ptr <RPC_SERVER_INTERFACE> Create(RpcHandle handle, Guid iid, Byte[] formatTypes,
                                                        Byte[] formatProc,
                                                        ExplicitBytesExecute fnExecute)
        {
            Ptr <MIDL_SERVER_INFO> pServer = handle.CreatePtr(new MIDL_SERVER_INFO());

            MIDL_SERVER_INFO temp = new MIDL_SERVER_INFO();

            return(Configure(temp, handle, pServer, iid, formatTypes, formatProc, fnExecute));
        }
Example #21
0
        /// <summary>
        /// Connects to the provided server interface with the given protocol and server:endpoint
        /// </summary>
        public RpcClientApi(Guid iid, RpcProtseq protocol, string server, string endpoint)
        {
            _handle   = new RpcClientHandle();
            IID       = iid;
            _protocol = protocol;
            Log.Verbose("RpcClient('{0}:{1}')", server, endpoint);

            _binding = StringBindingCompose(protocol, server, endpoint, null);
            Connect();
        }
        /// <summary>
        /// Connects to the provided server interface with the given protocol and server:endpoint
        /// </summary>
        public RpcClientApi(Guid iid, RpcProtseq protocol, string server, string endpoint)
        {
            _handle = new RpcClientHandle();
            IID = iid;
            _protocol = protocol;
            Log.Verbose("RpcClient('{0}:{1}')", server, endpoint);

            _binding = StringBindingCompose(protocol, server, endpoint, null);
            Connect();
        }
        internal static Ptr <RPC_SERVER_INTERFACE> Create(RpcHandle handle, Guid iid, Byte[] formatTypes,
                                                          Byte[] formatProc,
                                                          RpcExecute fnExecute)
        {
            Ptr <MIDL_SERVER_INFO> pServer = handle.CreatePtr(new MIDL_SERVER_INFO());

            MIDL_SERVER_INFO temp = new MIDL_SERVER_INFO();

            return(temp.Configure(handle, pServer, iid, formatTypes, formatProc, fnExecute));
        }
        internal static Ptr <RPC_SERVER_INTERFACE> Create(RpcHandle handle, Guid iid, Byte[] formatTypes,
                                                          Byte[] formatProc, ushort[] formatProcOffsets,
                                                          Delegate[] funcs)
        {
            Ptr <MIDL_SERVER_INFO> pServer = handle.CreatePtr(new MIDL_SERVER_INFO());

            MIDL_SERVER_INFO temp = new MIDL_SERVER_INFO();

            return(temp.Configure(handle, pServer, iid, formatTypes, formatProc, formatProcOffsets, funcs));
        }
        /// <summary>
        /// Constructs an RPC server for the given interface guid, the guid is used to identify multiple rpc
        /// servers/services within a single process.
        /// </summary>
        public RpcServerApi(Guid iid, int maxCalls, int maxRequestBytes, bool allowAnonTcp)
        {
            IID = iid;
            _maxCalls = maxCalls;
            _handle = new RpcServerHandle();

            // Guid.Empty to avoid registration of any interface allowing access to AddProtocol/AddAuthentication
            // without creating a channel
            if (!Guid.Empty.Equals(iid))
                ServerRegisterInterface(_handle, IID, RpcEntryPoint, maxCalls, maxRequestBytes, allowAnonTcp);
        }
        /// <summary>
        /// Constructs an RPC server for the given interface guid, the guid is used to identify multiple rpc
        /// servers/services within a single process.
        /// </summary>
        public RpcServerApi(Guid iid, int maxCalls, int maxRequestBytes, bool allowAnonTcp)
        {
            IID       = iid;
            _maxCalls = maxCalls;
            _handle   = new RpcServerHandle();

            // Guid.Empty to avoid registration of any interface allowing access to AddProtocol/AddAuthentication
            // without creating a channel
            if (!Guid.Empty.Equals(iid))
            {
                ServerRegisterInterface(_handle, IID, RpcEntryPoint, maxCalls, maxRequestBytes, allowAnonTcp);
            }
        }
Example #27
0
        /// <summary>
        /// Constructs an RPC server for the given interface guid, the guid is used to identify multiple rpc
        /// servers/services within a single process.
        /// </summary>
        public RpcServerApi2(Guid iid, int maxCalls, int maxRequestBytes, bool allowAnonTcp)
        {
            IID = iid;
              _maxCalls = maxCalls;
              _handle = new RpcServerHandle();

              // Guid.Empty to avoid registration of any interface allowing access to AddProtocol/AddAuthentication
              // without creating a channel

              var funcs = new Delegate[] {
              new RpcExecute(RpcEntryPoint),
              new RpcExecuteAsync2(RpcEntryPoint2)
            };

              if (!Guid.Empty.Equals(iid))
            ServerRegisterInterface(_handle, IID, funcs, maxCalls, maxRequestBytes, allowAnonTcp);
        }
Example #28
0
        /// <summary>
        /// Constructs an RPC server for the given interface guid, the guid is used to identify multiple rpc
        /// servers/services within a single process.
        /// </summary>
        public RpcServerApi2(Guid iid, int maxCalls, int maxRequestBytes, bool allowAnonTcp)
        {
            IID       = iid;
            _maxCalls = maxCalls;
            _handle   = new RpcServerHandle();

            // Guid.Empty to avoid registration of any interface allowing access to AddProtocol/AddAuthentication
            // without creating a channel

            var funcs = new Delegate[] {
                new RpcExecute(RpcEntryPoint),
                new RpcExecuteAsync2(RpcEntryPoint2)
            };

            if (!Guid.Empty.Equals(iid))
            {
                ServerRegisterInterface(_handle, IID, funcs, maxCalls, maxRequestBytes, allowAnonTcp);
            }
        }
        private static RPC_SERVER_INTERFACE CreateExplicitBytesServer(RpcHandle handle, Ptr<MIDL_SERVER_INFO> pServer, Guid iid)
        {
            var server = new RPC_SERVER_INTERFACE();
            server.Length = (uint)Marshal.SizeOf(typeof(RPC_SERVER_INTERFACE));
            server.InterfaceId = new RPC_SYNTAX_IDENTIFIER() { SyntaxGUID = iid, SyntaxVersion = ExplicitBytesConstants.INTERFACE_VERSION };
            server.TransferSyntax = new RPC_SYNTAX_IDENTIFIER() { SyntaxGUID = SYNTAX.SYNTAX_IID, SyntaxVersion = SYNTAX.SYNTAX_VERSION };

            var fnTable = new RPC_DISPATCH_TABLE();
            fnTable.DispatchTableCount = 1;
            fnTable.DispatchTable =
                handle.Pin(new RPC_DISPATCH_TABLE_Entry() { DispatchMethod = RpcRuntime.ServerEntry.Handle, Zero = IntPtr.Zero });
            fnTable.Reserved = IntPtr.Zero;

            server.DispatchTable = handle.Pin(fnTable);
            server.RpcProtseqEndpointCount = 0u;
            server.RpcProtseqEndpoint = IntPtr.Zero;
            server.DefaultManagerEpv = IntPtr.Zero;
            server.InterpreterInfo = pServer.Handle;
            server.Flags = 0x04000000u;
            return server;
        }
        public RPC_SERVER_INTERFACE(RpcHandle handle, Ptr<MIDL_SERVER_INFO> pServer, Guid iid)
        {
            Length = (uint) Marshal.SizeOf(typeof (RPC_CLIENT_INTERFACE));
            InterfaceId = new RPC_SYNTAX_IDENTIFIER() {SyntaxGUID = iid, SyntaxVersion = RPC_VERSION.INTERFACE_VERSION};
            TransferSyntax = new RPC_SYNTAX_IDENTIFIER()
                                 {SyntaxGUID = IID_SYNTAX, SyntaxVersion = RPC_VERSION.SYNTAX_VERSION};

            RPC_DISPATCH_TABLE fnTable = new RPC_DISPATCH_TABLE();
            fnTable.DispatchTableCount = 1;
            fnTable.DispatchTable =
                handle.Pin(new RPC_DISPATCH_TABLE_Entry()
                               {DispatchMethod = RpcApi.ServerEntry.Handle, Zero = IntPtr.Zero});
            fnTable.Reserved = IntPtr.Zero;

            DispatchTable = handle.Pin(fnTable);
            RpcProtseqEndpointCount = 0u;
            RpcProtseqEndpoint = IntPtr.Zero;
            DefaultManagerEpv = IntPtr.Zero;
            InterpreterInfo = pServer.Handle;
            Flags = 0x04000000u;
        }
        private static Ptr<RPC_SERVER_INTERFACE> Configure(MIDL_SERVER_INFO temp, RpcHandle handle, Ptr<MIDL_SERVER_INFO> me, Guid iid,
                                                    Byte[] formatTypes,
                                                    Byte[] formatProc, ExplicitBytesExecute fnExecute)
        {
            Ptr<RPC_SERVER_INTERFACE> svrIface = handle.CreatePtr(CreateExplicitBytesServer(handle, me, iid));
            Ptr<MIDL_STUB_DESC> stub = handle.CreatePtr(new MIDL_STUB_DESC(handle, svrIface.Handle, formatTypes, true));
            temp.pStubDesc = stub.Handle;

            IntPtr ptrFunction = handle.PinFunction(fnExecute);
            temp.DispatchTable = handle.Pin(ptrFunction);

            temp.ProcString = handle.Pin(formatProc);
            temp.FmtStringOffset = handle.Pin(new int[1] { 0 });

            temp.ThunkTable = IntPtr.Zero;
            temp.pTransferSyntax = IntPtr.Zero;
            temp.nCount = IntPtr.Zero;
            temp.pSyntaxInfo = IntPtr.Zero;

            //Copy us back into the pinned address
            Marshal.StructureToPtr( temp, me.Handle, false);
            return svrIface;
        }
        private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input)
        {
            RpcTrace.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length);
            Ptr <MIDL_STUB_DESC> pStub;

            if (!handle.GetPtr(out pStub))
            {
                pStub =
                    handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(ClientInterfaceFactory.CreatExplicitBytesClient(iid)),
                                                        RpcRuntime.TYPE_FORMAT,
                                                        false));
            }
            int    szResponse = 0;
            IntPtr response, result;

            using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input))
            {
                if (RpcRuntime.Is64BitProcess)
                {
                    try
                    {
                        result = NativeMethods.NdrClientCall2x64(pStub.Handle, RpcRuntime.FUNC_FORMAT_PTR.Handle, handle.Handle,
                                                                 input.Length,
                                                                 pInputBuffer.Handle, out szResponse, out response);
                    }
                    catch (SEHException ex)
                    {
                        RpcTrace.Error(ex);
                        Guard.Assert(ex.ErrorCode);
                        throw;
                    }
                }
                else
                {
                    using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[10]))
                    {
                        pStack32.Data[0] = handle.Handle.ToInt32();
                        pStack32.Data[1] = input.Length;
                        pStack32.Data[2] = pInputBuffer.Handle.ToInt32();
                        pStack32.Data[3] = pStack32.Handle.ToInt32() + (sizeof(int) * 6);
                        pStack32.Data[4] = pStack32.Handle.ToInt32() + (sizeof(int) * 8);
                        pStack32.Data[5] = 0; //reserved
                        pStack32.Data[6] = 0; //output: int dwSizeResponse
                        pStack32.Data[8] = 0; //output: byte* lpResponse

                        try
                        {
                            result = NativeMethods.NdrClientCall2x86(pStub.Handle, RpcRuntime.FUNC_FORMAT_PTR.Handle, pStack32.Handle);
                        }
                        catch (SEHException ex)
                        {
                            RpcTrace.Error(ex);
                            Guard.Assert(ex.ErrorCode);
                            throw;
                        }

                        szResponse = pStack32.Data[6];
                        response   = new IntPtr(pStack32.Data[8]);
                    }
                }
                GC.KeepAlive(pInputBuffer);
            }

            // on local machine
            // if 32 bit server just throws Exception in server side handler
            // then 32 bits client can see "-2147467259" debug view of pointer result
            // then 64 bits client can see "2147500037" debug view of pointer result
            // then 32 bits client can do ToInt32 and things work fine
            // then 64 bits client can do ToInt32 and get arithmetic overflow exception
            // hence using ToInt64 for 64 bits client
            if (IntPtr.Size == 8)
            {
                Guard.Assert(result.ToInt64());
            }
            else
            {
                Guard.Assert(result.ToInt32());
            }

            RpcTrace.Verbose("InvokeRpc.InvokeRpc response on {0}, received {1} bytes", handle.Handle, szResponse);
            byte[] output = new byte[szResponse];
            if (szResponse > 0 && response != IntPtr.Zero)
            {
                Marshal.Copy(response, output, 0, output.Length);
            }
            RpcRuntime.Free(response);

            return(output);
        }
Example #33
0
 protected void ServerRegisterInterface(IntPtr sIfHandle, RpcHandle handle)
 {
     ServerRegisterInterface(sIfHandle, handle, IntPtr.Zero, IntPtr.Zero);
 }
Example #34
0
        private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input)
        {
            Log.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length);
            Ptr <MIDL_STUB_DESC> pStub;

            if (!handle.GetPtr(out pStub))
            {
                pStub =
                    handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)),
                                                        RpcApi.TYPE_FORMAT,
                                                        false));
            }
            int    szResponse = 0;
            IntPtr response, result;

            using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input))
            {
                if (RpcApi.Is64BitProcess)
                {
                    try
                    {
                        result = NdrClientCall2x64(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, handle.Handle,
                                                   input.Length,
                                                   pInputBuffer.Handle, out szResponse, out response);
                    }
                    catch (SEHException ex)
                    {
                        RpcException.Assert(ex.ErrorCode);
                        throw;
                    }
                }
                else
                {
                    using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[10]))
                    {
                        pStack32.Data[0] = handle.Handle.ToInt32();
                        pStack32.Data[1] = input.Length;
                        pStack32.Data[2] = pInputBuffer.Handle.ToInt32();
                        pStack32.Data[3] = pStack32.Handle.ToInt32() + (sizeof(int) * 6);
                        pStack32.Data[4] = pStack32.Handle.ToInt32() + (sizeof(int) * 8);
                        pStack32.Data[5] = 0; //reserved
                        pStack32.Data[6] = 0; //output: int dwSizeResponse
                        pStack32.Data[8] = 0; //output: byte* lpResponse

                        try
                        {
                            result = NdrClientCall2x86(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, pStack32.Handle);
                        }
                        catch (SEHException ex)
                        {
                            RpcException.Assert(ex.ErrorCode);
                            throw;
                        }

                        szResponse = pStack32.Data[6];
                        response   = new IntPtr(pStack32.Data[8]);
                    }
                }
                GC.KeepAlive(pInputBuffer);
            }
            RpcException.Assert(result.ToInt32());
            Log.Verbose("InvokeRpc.InvokeRpc response on {0}, recieved {1} bytes", handle.Handle, szResponse);
            byte[] output = new byte[szResponse];
            if (szResponse > 0 && response != IntPtr.Zero)
            {
                Marshal.Copy(response, output, 0, output.Length);
            }
            RpcApi.Free(response);

            return(output);
        }
Example #35
0
 protected void ServerRegisterInterface(IntPtr sIfHandle, RpcHandle handle, IntPtr mgrTypeUuid, IntPtr mgrEpv)
 {
     //TODO: make server isolated of other process services, make it not static as possible
     Guard.Assert(NativeMethods.RpcServerRegisterIf(sIfHandle, mgrTypeUuid, mgrEpv));
     //RPC_IF_CALLBACK_FN security = null;
     // Guard.Assert(NativeMethods.RpcServerRegisterIfEx(sIf.Handle, IntPtr.Zero, IntPtr.Zero,  InterfacRegistrationFlags.RPC_IF_AUTOLISTEN, MAX_CALL_LIMIT, ref security));
     handle.Handle = sIfHandle;
 }
Example #36
0
        public static void BindingFromStringBinding(RpcHandle handle, String bindingString)
        {
            RpcError result = RpcBindingFromStringBinding(bindingString, out handle.Handle);

            RpcException.Assert(result);
        }
Example #37
0
 public Server()
 {
     _maxCalls = MAX_CALL_LIMIT;
     _handle = new RpcServerHandle();
 }
Example #38
0
        private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input)
        {
            RpcTrace.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length);
            Ptr<MIDL_STUB_DESC> pStub;
            if (!handle.GetPtr(out pStub))
            {
                pStub =
                    handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(ClientInterfaceFactory.CreatExplicitBytesClient(iid)),
                                                        RpcRuntime.TYPE_FORMAT,
                                                        false));
            }
            int szResponse = 0;
            IntPtr response, result;

            using (Ptr<byte[]> pInputBuffer = new Ptr<byte[]>(input))
            {
                if (RpcRuntime.Is64BitProcess)
                {
                    try
                    {
                        result = NativeMethods.NdrClientCall2x64(pStub.Handle, RpcRuntime.FUNC_FORMAT_PTR.Handle, handle.Handle,
                                                   input.Length,
                                                   pInputBuffer.Handle, out szResponse, out response);
                    }
                    catch (SEHException ex)
                    {
                        RpcTrace.Error(ex);
                        Guard.Assert(ex.ErrorCode);
                        throw;
                    }
                }
                else
                {
                    using (Ptr<Int32[]> pStack32 = new Ptr<Int32[]>(new Int32[10]))
                    {
                        pStack32.Data[0] = handle.Handle.ToInt32();
                        pStack32.Data[1] = input.Length;
                        pStack32.Data[2] = pInputBuffer.Handle.ToInt32();
                        pStack32.Data[3] = pStack32.Handle.ToInt32() + (sizeof (int)*6);
                        pStack32.Data[4] = pStack32.Handle.ToInt32() + (sizeof (int)*8);
                        pStack32.Data[5] = 0; //reserved
                        pStack32.Data[6] = 0; //output: int dwSizeResponse
                        pStack32.Data[8] = 0; //output: byte* lpResponse

                        try
                        {
                            result = NativeMethods.NdrClientCall2x86(pStub.Handle, RpcRuntime.FUNC_FORMAT_PTR.Handle, pStack32.Handle);
                        }
                        catch (SEHException ex)
                        {

                            RpcTrace.Error(ex);
                            Guard.Assert(ex.ErrorCode);
                            throw;
                        }

                        szResponse = pStack32.Data[6];
                        response = new IntPtr(pStack32.Data[8]);
                    }
                }
                GC.KeepAlive(pInputBuffer);
            }

            // on local machine
            // if 32 bit server just throws Exception in server side handler
            // then 32 bits client can see "-2147467259" debug view of pointer result
            // then 64 bits client can see "2147500037" debug view of pointer result
            // then 32 bits client can do ToInt32 and things work fine
            // then 64 bits client can do ToInt32 and get arithmetic overflow exception
            // hence using ToInt64 for 64 bits client
            if (IntPtr.Size == 8)
            {
                Guard.Assert(result.ToInt64());
            }
            else
            {
                Guard.Assert(result.ToInt32());
            }

            RpcTrace.Verbose("InvokeRpc.InvokeRpc response on {0}, received {1} bytes", handle.Handle, szResponse);
            byte[] output = new byte[szResponse];
            if (szResponse > 0 && response != IntPtr.Zero)
            {
                Marshal.Copy(response, output, 0, output.Length);
            }
            RpcRuntime.Free(response);

            return output;
        }
        private static void ServerRegisterInterface(RpcHandle handle, Guid iid, RpcExecute fnExec, int maxCalls, int maxRequestBytes, bool allowAnonTcp)
        {
            const int RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH = 0x0010;
            int flags = 0;
            IntPtr fnAuth = IntPtr.Zero;
            if (allowAnonTcp)
            {
                flags = RPC_IF_ALLOW_CALLBACKS_WITH_NO_AUTH;
                fnAuth = hAuthCall.Handle;
            }

            Ptr<RPC_SERVER_INTERFACE> sIf = MIDL_SERVER_INFO.Create(handle, iid, RpcApi.TYPE_FORMAT, RpcApi.FUNC_FORMAT, fnExec);

            if (!allowAnonTcp && maxRequestBytes < 0)
                RpcException.Assert(RpcServerRegisterIf(sIf.Handle, IntPtr.Zero, IntPtr.Zero));
            else
                RpcException.Assert(RpcServerRegisterIf2(sIf.Handle, IntPtr.Zero, IntPtr.Zero, flags, 
                    maxCalls <= 0 ? MAX_CALL_LIMIT : maxCalls, 
                    maxRequestBytes <= 0 ? 80 * 1024 : maxRequestBytes, fnAuth));

            handle.Handle = sIf.Handle;
        }
Example #40
0
        private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input)
        {
            Log.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length);
              Ptr<MIDL_STUB_DESC> pStub;
              if (!handle.GetPtr(out pStub))
              {
            pStub =
            handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)),
                                                RpcApi.TYPE_FORMAT,
                                                false));
              }
              int szResponse = 0;
              var pszResponse = new Ptr<int>(szResponse);
              IntPtr response = IntPtr.Zero;
              var pResponse = new Ptr<IntPtr>(response);
               IntPtr result;

              using (Ptr<byte[]> pInputBuffer = new Ptr<byte[]>(input))
              {
            if (RpcApi.Is64BitProcess)
            {
              try
              {
            result = RpcApi.NdrClientCall2x64(
              pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, handle.Handle,
              input.Length, pInputBuffer.Handle, out szResponse, out response
              );
              }
              catch (SEHException ex)
              {
            int internalError = ex.ErrorCode;
            if ((uint)RpcError.RPC_E_FAIL == (uint)internalError)
            {
              if ((int)RpcError.RPC_S_OK != Marshal.GetExceptionCode())
              {
                internalError = Marshal.GetExceptionCode();
              }
            }
            RpcException.Assert(internalError);
            throw;
              }
            }
            else
            {
              using (Ptr<Int32[]> pStack32 = new Ptr<Int32[]>(new Int32[6]))
              {
            pStack32.Data[0] = handle.Handle.ToInt32();
            pStack32.Data[1] = input.Length;
            pStack32.Data[2] = pInputBuffer.Handle.ToInt32();
            pStack32.Data[3] = pszResponse.Handle.ToInt32();
            pStack32.Data[4] = pResponse.Handle.ToInt32();
            pStack32.Data[5] = 0; // reserved

            try
            {
              result = RpcApi.NdrClientCall2x86(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, pStack32.Handle);
            }
            catch (SEHException ex)
            {
              int internalError = ex.ErrorCode;
              if ((uint)RpcError.RPC_E_FAIL == (uint)internalError)
              {
                if ((int)RpcError.RPC_S_OK != Marshal.GetExceptionCode())
                {
                  internalError = Marshal.GetExceptionCode();
                }
              }
              RpcException.Assert(internalError);
              throw;
            }
              }
            }
            GC.KeepAlive(pInputBuffer);
              }
              RpcException.Assert(result.ToInt32());

              szResponse = pszResponse.Data;
              response = pResponse.Data;
              byte[] output = new byte[szResponse];
              if (szResponse > 0 && pResponse.Handle != IntPtr.Zero)
              {
            Marshal.Copy(response, output, 0, output.Length);
              }
              RpcApi.Free(response);

              return output;
        }
Example #41
0
        private static void InvokeRpcAsync(RpcHandle handle, Guid iid, byte[] input, APICallback callback)
        {
            Log.Verbose("InvokeRpcAsync on {0}, sending {1} bytes", handle.Handle, input.Length);
            Ptr <MIDL_STUB_DESC> pStub;

            if (!handle.GetPtr(out pStub))
            {
                pStub = handle.CreatePtr(new MIDL_STUB_DESC(
                                             handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false));
            }

            int    cbOutput  = 0;
            var    pcbOutput = new Ptr <int>(cbOutput);
            IntPtr result    = IntPtr.Zero;
            IntPtr output    = IntPtr.Zero;
            var    pOutput   = new Ptr <IntPtr>(output);

            using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input))
            {
                AsyncCallback cbRountine = (IntPtr pAsync, IntPtr pContext, RpcAsyncEvent _event) =>
                {
                    RPC_ASYNC_STATE async      = (RPC_ASYNC_STATE)Marshal.PtrToStructure(pAsync, typeof(RPC_ASYNC_STATE));
                    var             myCallback = (APICallback)Marshal.GetDelegateForFunctionPointer(async.UserInfo, typeof(APICallback));

                    RpcError hr = RpcApi.RpcAsyncCompleteCall(pAsync, ref result);
                    if (RpcError.RPC_S_OK == hr)
                    {
                        if (RpcError.RPC_S_OK == (RpcError)result.ToInt32())
                        {
                            ExecuteAsyncResponse response;
                            try
                            {
                                cbOutput = pcbOutput.Data;
                                byte[] _output = new byte[cbOutput];
                                Marshal.Copy(pOutput.Data, _output, 0, cbOutput);

                                response.result   = RpcError.RPC_S_OK;
                                response.response = _output;
                            }
                            catch (Exception e)
                            {
                                response.result   = RpcError.RPC_E_FAIL;
                                response.response = null;
                            }
                            finally
                            {
                                RpcApi.Free(pOutput.Data);
                            }

                            myCallback(response);
                        }
                        else
                        {
                            ExecuteAsyncResponse response;
                            response.result   = (RpcError)result.ToInt32();
                            response.response = null;
                            myCallback(response);
                        }
                    }
                    else
                    {
                        ExecuteAsyncResponse response;
                        response.result   = hr;
                        response.response = null;
                        myCallback(response);
                    }
                };

                var pCallbackRoutine = new FunctionPtr <AsyncCallback>(cbRountine);

                RPC_ASYNC_STATE asyncState   = new RPC_ASYNC_STATE();
                var             _pAsyncState = new Ptr <RPC_ASYNC_STATE>(asyncState);
                RpcApi.RpcAsyncInitializeHandle(_pAsyncState.Handle, Marshal.SizeOf(typeof(RPC_ASYNC_STATE)));
                asyncState.Event                 = _pAsyncState.Data.Event;
                asyncState.Flags                 = _pAsyncState.Data.Flags;
                asyncState.Lock                  = _pAsyncState.Data.Lock;
                asyncState.RuntimeInfo           = _pAsyncState.Data.RuntimeInfo;
                asyncState.Signature             = _pAsyncState.Data.Signature;
                asyncState.Size                  = _pAsyncState.Data.Size;
                asyncState.StubInfo              = _pAsyncState.Data.StubInfo;
                asyncState.NotificationType      = RpcNotificationTypes.RpcNotificationTypeCallback;
                asyncState.u.NotificationRoutine = pCallbackRoutine.Handle;

                var ptrCB = Marshal.GetFunctionPointerForDelegate(callback);
                asyncState.UserInfo = ptrCB;

                var pAsyncState = new Ptr <RPC_ASYNC_STATE>(asyncState);

                if (RpcApi.Is64BitProcess)
                {
                    try
                    {
                        result = RpcApi.NdrAsyncClientCallx64(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pAsyncState.Handle, handle.Handle,
                                                              input.Length, pInputBuffer.Handle, out cbOutput, out output);
                    }
                    catch (SEHException ex)
                    {
                        RpcException.Assert(ex.ErrorCode);
                        throw;
                    }
                }
                else
                {
                    using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[7]))
                    {
                        pStack32.Data[0] = pAsyncState.Handle.ToInt32();
                        pStack32.Data[1] = handle.Handle.ToInt32();
                        pStack32.Data[2] = input.Length;
                        pStack32.Data[3] = pInputBuffer.Handle.ToInt32();
                        pStack32.Data[4] = pcbOutput.Handle.ToInt32();
                        pStack32.Data[5] = pOutput.Handle.ToInt32();
                        pStack32.Data[6] = 0; //reserved

                        try
                        {
                            result = RpcApi.NdrAsyncClientCallx86(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pStack32.Handle);
                        }
                        catch (SEHException ex)
                        {
                            Log.Verbose("exception on {0}", ex);
                            RpcException.Assert(ex.ErrorCode);
                            throw;
                        }
                    }
                }

                GC.KeepAlive(pInputBuffer);
            }
            RpcException.Assert(result.ToInt32());
            Log.Verbose("InvokeRpc.InvokeRpc response on {0}", handle.Handle);
        }
Example #42
0
        private static void InvokeRpcAsync(RpcHandle handle, Guid iid, byte[] input, APICallback callback)
        {
            Log.Verbose("InvokeRpcAsync on {0}, sending {1} bytes", handle.Handle, input.Length);
              Ptr<MIDL_STUB_DESC> pStub;
              if (!handle.GetPtr(out pStub))
              {
            pStub = handle.CreatePtr(new MIDL_STUB_DESC(
              handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)), RpcApi.TYPE_FORMAT, false));
              }

              int cbOutput = 0;
              var pcbOutput = new Ptr<int>(cbOutput);
              IntPtr result = IntPtr.Zero;
              IntPtr output = IntPtr.Zero;
              var pOutput = new Ptr<IntPtr>(output);

              using (Ptr<byte[]> pInputBuffer = new Ptr<byte[]>(input))
              {
            AsyncCallback cbRountine = (IntPtr pAsync, IntPtr pContext, RpcAsyncEvent _event) =>
            {
              RPC_ASYNC_STATE async = (RPC_ASYNC_STATE)Marshal.PtrToStructure(pAsync, typeof(RPC_ASYNC_STATE));
              var myCallback = (APICallback)Marshal.GetDelegateForFunctionPointer(async.UserInfo, typeof(APICallback));

              RpcError hr = RpcApi.RpcAsyncCompleteCall(pAsync, ref result);
              if (RpcError.RPC_S_OK == hr)
              {
            if (RpcError.RPC_S_OK == (RpcError) result.ToInt32())
            {
              ExecuteAsyncResponse response;
              try
              {
                cbOutput = pcbOutput.Data;
                byte[] _output = new byte[cbOutput];
                Marshal.Copy(pOutput.Data, _output, 0, cbOutput);

                response.result = RpcError.RPC_S_OK;
                response.response = _output;
              }
              catch (Exception e)
              {
                response.result = RpcError.RPC_E_FAIL;
                response.response = null;
              }
              finally
              {
                RpcApi.Free(pOutput.Data);
              }

              myCallback(response);
            }
            else
            {
              ExecuteAsyncResponse response;
              response.result = (RpcError)result.ToInt32();
              response.response = null;
              myCallback(response);
            }
              }
              else
              {
            ExecuteAsyncResponse response;
            response.result = hr;
            response.response = null;
            myCallback(response);
              }
            };

            var pCallbackRoutine = new FunctionPtr<AsyncCallback>(cbRountine);

            RPC_ASYNC_STATE asyncState = new RPC_ASYNC_STATE();
            var _pAsyncState = new Ptr<RPC_ASYNC_STATE>(asyncState);
            RpcApi.RpcAsyncInitializeHandle(_pAsyncState.Handle, Marshal.SizeOf(typeof(RPC_ASYNC_STATE)));
            asyncState.Event = _pAsyncState.Data.Event;
            asyncState.Flags = _pAsyncState.Data.Flags;
            asyncState.Lock = _pAsyncState.Data.Lock;
            asyncState.RuntimeInfo = _pAsyncState.Data.RuntimeInfo;
            asyncState.Signature = _pAsyncState.Data.Signature;
            asyncState.Size = _pAsyncState.Data.Size;
            asyncState.StubInfo = _pAsyncState.Data.StubInfo;
            asyncState.NotificationType = RpcNotificationTypes.RpcNotificationTypeCallback;
            asyncState.u.NotificationRoutine = pCallbackRoutine.Handle;

            var ptrCB = Marshal.GetFunctionPointerForDelegate(callback);
            asyncState.UserInfo = ptrCB;

            var pAsyncState = new Ptr<RPC_ASYNC_STATE>(asyncState);

            if (RpcApi.Is64BitProcess)
            {
              try
              {
            result = RpcApi.NdrAsyncClientCallx64(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pAsyncState.Handle, handle.Handle,
              input.Length, pInputBuffer.Handle, out cbOutput, out output);
              }
              catch (SEHException ex)
              {
            RpcException.Assert(ex.ErrorCode);
            throw;
              }
            }
            else
            {
              using (Ptr<Int32[]> pStack32 = new Ptr<Int32[]>(new Int32[7]))
              {
            pStack32.Data[0] = pAsyncState.Handle.ToInt32();
            pStack32.Data[1] = handle.Handle.ToInt32();
            pStack32.Data[2] = input.Length;
            pStack32.Data[3] = pInputBuffer.Handle.ToInt32();
            pStack32.Data[4] = pcbOutput.Handle.ToInt32();
            pStack32.Data[5] = pOutput.Handle.ToInt32();
            pStack32.Data[6] = 0; //reserved

            try
            {
              result = RpcApi.NdrAsyncClientCallx86(pStub.Handle, RpcApi.ASYNC_FUNC_FORMAT_PTR.Handle, pStack32.Handle);
            }
            catch (SEHException ex)
            {
              Log.Verbose("exception on {0}", ex);
              RpcException.Assert(ex.ErrorCode);
              throw;
            }
              }
            }

            GC.KeepAlive(pInputBuffer);
              }
              RpcException.Assert(result.ToInt32());
              Log.Verbose("InvokeRpc.InvokeRpc response on {0}", handle.Handle);
        }
 private static void BindingFromStringBinding(RpcHandle handle, String bindingString)
 {
     RpcError result = RpcBindingFromStringBinding(bindingString, out handle.Handle);
     RpcException.Assert(result);
 }
 private static void BindingSetAuthInfo(RpcProtectionLevel level, RpcAuthentication[] authTypes, 
     RpcHandle handle, string serverPrincipalName, NetworkCredential credentails)
 {
     if (credentails == null)
     {
         foreach (RpcAuthentication atype in authTypes)
         {
             RpcError result = RpcBindingSetAuthInfo2(handle.Handle, serverPrincipalName, level, atype, IntPtr.Zero, 0);
             if (result != RpcError.RPC_S_OK)
                 Log.Warning("Unable to register {0}, result = {1}", atype, new RpcException(result).Message);
         }
     }
     else
     {
         SEC_WINNT_AUTH_IDENTITY pSecInfo = new SEC_WINNT_AUTH_IDENTITY(credentails);
         foreach (RpcAuthentication atype in authTypes)
         {
             RpcError result = RpcBindingSetAuthInfo(handle.Handle, serverPrincipalName, level, atype, ref pSecInfo, 0);
             if (result != RpcError.RPC_S_OK)
                 Log.Warning("Unable to register {0}, result = {1}", atype, new RpcException(result).Message);
         }
     }
 }
        private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input)
        {
            Log.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length);
            Ptr<MIDL_STUB_DESC> pStub;
            if (!handle.GetPtr(out pStub))
            {
                pStub =
                    handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)),
                                                        RpcApi.TYPE_FORMAT,
                                                        false));
            }
            int szResponse = 0;
            IntPtr response, result;

            using (Ptr<byte[]> pInputBuffer = new Ptr<byte[]>(input))
            {
                if (RpcApi.Is64BitProcess)
                {
                    try
                    {
                        result = NdrClientCall2x64(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, handle.Handle,
                                                   input.Length,
                                                   pInputBuffer.Handle, out szResponse, out response);
                    }
                    catch (SEHException ex)
                    {
                        RpcException.Assert(ex.ErrorCode);
                        throw;
                    }
                }
                else
                {
                    using (Ptr<Int32[]> pStack32 = new Ptr<Int32[]>(new Int32[10]))
                    {
                        pStack32.Data[0] = handle.Handle.ToInt32();
                        pStack32.Data[1] = input.Length;
                        pStack32.Data[2] = pInputBuffer.Handle.ToInt32();
                        pStack32.Data[3] = pStack32.Handle.ToInt32() + (sizeof (int)*6);
                        pStack32.Data[4] = pStack32.Handle.ToInt32() + (sizeof (int)*8);
                        pStack32.Data[5] = 0; //reserved
                        pStack32.Data[6] = 0; //output: int dwSizeResponse
                        pStack32.Data[8] = 0; //output: byte* lpResponse

                        try
                        {
                            result = NdrClientCall2x86(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, pStack32.Handle);
                        }
                        catch (SEHException ex)
                        {
                            RpcException.Assert(ex.ErrorCode);
                            throw;
                        }

                        szResponse = pStack32.Data[6];
                        response = new IntPtr(pStack32.Data[8]);
                    }
                }
                GC.KeepAlive(pInputBuffer);
            }
            RpcException.Assert(result.ToInt32());
            Log.Verbose("InvokeRpc.InvokeRpc response on {0}, recieved {1} bytes", handle.Handle, szResponse);
            byte[] output = new byte[szResponse];
            if (szResponse > 0 && response != IntPtr.Zero)
            {
                Marshal.Copy(response, output, 0, output.Length);
            }
            RpcApi.Free(response);

            return output;
        }
Example #46
0
        private static byte[] InvokeRpc(RpcHandle handle, Guid iid, byte[] input)
        {
            Log.Verbose("InvokeRpc on {0}, sending {1} bytes", handle.Handle, input.Length);
            Ptr <MIDL_STUB_DESC> pStub;

            if (!handle.GetPtr(out pStub))
            {
                pStub =
                    handle.CreatePtr(new MIDL_STUB_DESC(handle, handle.Pin(new RPC_CLIENT_INTERFACE(iid)),
                                                        RpcApi.TYPE_FORMAT,
                                                        false));
            }
            int    szResponse  = 0;
            var    pszResponse = new Ptr <int>(szResponse);
            IntPtr response    = IntPtr.Zero;
            var    pResponse   = new Ptr <IntPtr>(response);
            IntPtr result;

            using (Ptr <byte[]> pInputBuffer = new Ptr <byte[]>(input))
            {
                if (RpcApi.Is64BitProcess)
                {
                    try
                    {
                        result = RpcApi.NdrClientCall2x64(
                            pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, handle.Handle,
                            input.Length, pInputBuffer.Handle, out szResponse, out response
                            );
                    }
                    catch (SEHException ex)
                    {
                        int internalError = ex.ErrorCode;
                        if ((uint)RpcError.RPC_E_FAIL == (uint)internalError)
                        {
                            if ((int)RpcError.RPC_S_OK != Marshal.GetExceptionCode())
                            {
                                internalError = Marshal.GetExceptionCode();
                            }
                        }
                        RpcException.Assert(internalError);
                        throw;
                    }
                }
                else
                {
                    using (Ptr <Int32[]> pStack32 = new Ptr <Int32[]>(new Int32[6]))
                    {
                        pStack32.Data[0] = handle.Handle.ToInt32();
                        pStack32.Data[1] = input.Length;
                        pStack32.Data[2] = pInputBuffer.Handle.ToInt32();
                        pStack32.Data[3] = pszResponse.Handle.ToInt32();
                        pStack32.Data[4] = pResponse.Handle.ToInt32();
                        pStack32.Data[5] = 0; // reserved

                        try
                        {
                            result = RpcApi.NdrClientCall2x86(pStub.Handle, RpcApi.FUNC_FORMAT_PTR.Handle, pStack32.Handle);
                        }
                        catch (SEHException ex)
                        {
                            int internalError = ex.ErrorCode;
                            if ((uint)RpcError.RPC_E_FAIL == (uint)internalError)
                            {
                                if ((int)RpcError.RPC_S_OK != Marshal.GetExceptionCode())
                                {
                                    internalError = Marshal.GetExceptionCode();
                                }
                            }
                            RpcException.Assert(internalError);
                            throw;
                        }
                    }
                }
                GC.KeepAlive(pInputBuffer);
            }
            RpcException.Assert(result.ToInt32());

            szResponse = pszResponse.Data;
            response   = pResponse.Data;
            byte[] output = new byte[szResponse];
            if (szResponse > 0 && pResponse.Handle != IntPtr.Zero)
            {
                Marshal.Copy(response, output, 0, output.Length);
            }
            RpcApi.Free(response);

            return(output);
        }
Example #47
0
 public Server()
 {
     _maxCalls = MAX_CALL_LIMIT;
     _handle   = new RpcServerHandle();
 }
Example #48
0
 protected void ServerRegisterInterface(IntPtr sIfHandle, RpcHandle handle)
 {
     ServerRegisterInterface(sIfHandle, handle, IntPtr.Zero, IntPtr.Zero);
 }