Example #1
0
        /// <summary>
        /// This function should be called first to get a connection to the Hypervisor. If necessary, authentication will be performed fetching credentials via the callback See virConnectOpen for notes about environment variables which can have an effect on opening drivers
        /// </summary>
        /// <param name="name">URI of the hypervisor</param>
        /// <param name="auth">Authenticate callback parameters</param>
        /// <param name="flags">Open flags</param>
        /// <returns>a pointer to the hypervisor connection or NULL in case of error URIs are documented at http://libvirt.org/uri.html </returns>
        public static IntPtr OpenAuth(string name, ref ConnectAuth auth, int flags)
        {
            // Create a structure that hold cbdata and the callback target
            OpenAuthManagedCB cbAndUserData = new OpenAuthManagedCB();

            cbAndUserData.cbdata    = auth.cbdata;
            cbAndUserData.cbManaged = auth.cb;
            // Pass the structure as cbdata
            IntPtr cbAndUserDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cbAndUserData));

            Marshal.StructureToPtr(cbAndUserData, cbAndUserDataPtr, true);

            // Create the real ConnectAuth structure, it will call OpenAuthCallbackFromUnmanaged via callback
            ConnectAuthUnmanaged connectAuth = new ConnectAuthUnmanaged();

            connectAuth.cbdata    = cbAndUserDataPtr;
            connectAuth.cb        = OpenAuthCallbackFromUnmanaged;
            connectAuth.CredTypes = auth.CredTypes;

            return(OpenAuth(name, ref connectAuth, flags));
        }
Example #2
0
        /// <summary>
        /// This function should be called first to get a connection to the Hypervisor. If necessary, authentication will be performed fetching credentials via the callback See virConnectOpen for notes about environment variables which can have an effect on opening drivers
        /// </summary>
        /// <param name="name">URI of the hypervisor</param>
        /// <param name="auth">Authenticate callback parameters</param>
        /// <param name="flags">Open flags</param>
        /// <returns>a pointer to the hypervisor connection or NULL in case of error URIs are documented at http://libvirt.org/uri.html </returns>
        public static IntPtr OpenAuth(string name, ref ConnectAuth auth, int flags)
        {
            // Create a structure that hold cbdata and the callback target
            OpenAuthManagedCB cbAndUserData = new OpenAuthManagedCB();
            cbAndUserData.cbdata = auth.cbdata;
            cbAndUserData.cbManaged = auth.cb;
            // Pass the structure as cbdata
            IntPtr cbAndUserDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(cbAndUserData));
            Marshal.StructureToPtr(cbAndUserData, cbAndUserDataPtr, true);

            // Create the real ConnectAuth structure, it will call OpenAuthCallbackFromUnmanaged via callback
            ConnectAuthUnmanaged connectAuth = new ConnectAuthUnmanaged();
            connectAuth.cbdata = cbAndUserDataPtr;
            connectAuth.cb = OpenAuthCallbackFromUnmanaged;
            connectAuth.CredTypes = auth.CredTypes;

            return OpenAuth(name, ref connectAuth, flags);
        }
    protected virtual void OnButton1Clicked(object sender, System.EventArgs e)
    {
        // Remove all items
        domainListStore.Clear ();

        // Fill a structure to pass username and password to callbacks
        AuthData authData = new AuthData { password = entry3.Text, user_name = entry2.Text };
        IntPtr authDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(authData));
        Marshal.StructureToPtr(authData, authDataPtr, false);

        // Fill a virConnectAuth structure
        ConnectAuth auth = new ConnectAuth
        {
            cbdata = authDataPtr,                  // The authData structure
            cb = AuthCallback,                  // the method called by callbacks
            CredTypes = new[]
                            {
                                ConnectCredentialType.VIR_CRED_AUTHNAME,
                                ConnectCredentialType.VIR_CRED_PASSPHRASE
                            }          // The list of credentials types
        };

        // Request the connection
        IntPtr conn = Connect.OpenAuth(entry1.Text, ref auth, 0);
        Marshal.FreeHGlobal(authDataPtr);

        if (conn != IntPtr.Zero)
        {
            // Get the number of defined (not running) domains
            int numOfDefinedDomains = Connect.NumOfDefinedDomains(conn);
            string[] definedDomainNames = new string[numOfDefinedDomains];
            if (Connect.ListDefinedDomains(conn, ref definedDomainNames, numOfDefinedDomains) == -1)
            {
                ShowError("Unable to list defined domains");
                goto cleanup;
            }

            // Add the domain names to the listbox
            foreach (string domainName in definedDomainNames)
            {
                AddDomainInTreeView(domainName);
            }

            // Get the number of running domains
            int numOfRunningDomain = Connect.NumOfDomains(conn);
            int[] runningDomainIDs = new int[numOfRunningDomain];
            if (Connect.ListDomains(conn, runningDomainIDs, numOfRunningDomain) == -1)
            {
                ShowError("Unable to list running domains");
                goto cleanup;
            }

            // Add the domain names to the listbox
            foreach (int runningDomainID in runningDomainIDs)
            {
                IntPtr domainPtr = Domain.LookupByID(conn, runningDomainID);
                if (domainPtr == IntPtr.Zero)
                {
                    ShowError("Unable to lookup domains by id");
                    goto cleanup;
                }
                string domainName = Domain.GetName(domainPtr);
                Domain.Free(domainPtr);
                if (string.IsNullOrEmpty(domainName))
                {
                    ShowError("Unable to get domain name");
                    goto cleanup;
                }
                AddDomainInTreeView(domainName);
            }

        cleanup:
            Connect.Close(conn);
        }
        else
        {
            ShowError("Unable to connect");
        }
    }
Example #4
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            lbDomains.Items.Clear();

            // Fill a structure to pass username and password to callbacks
            AuthData authData = new AuthData { password = tbPassword.Text, user_name = tbUsername.Text };
            IntPtr authDataPtr = Marshal.AllocHGlobal(Marshal.SizeOf(authData));
            Marshal.StructureToPtr(authData, authDataPtr, true);
            // Fill a virConnectAuth structure
            ConnectAuth auth = new ConnectAuth
            {
                cbdata = authDataPtr,               // The authData structure
                cb = AuthCallback,                  // the method called by callbacks
                CredTypes = new[]
                                {
                                    ConnectCredentialType.VIR_CRED_AUTHNAME,
                                    ConnectCredentialType.VIR_CRED_PASSPHRASE
                                }          // The list of credentials types
            };

            // Request the connection
            IntPtr conn = Connect.OpenAuth(tbURI.Text, ref auth, 0);
            Marshal.FreeHGlobal(authDataPtr);

            if (conn != IntPtr.Zero)
            {
                // Get the number of defined (not running) domains
                int numOfDefinedDomains = Connect.NumOfDefinedDomains(conn);
                string[] definedDomainNames = new string[numOfDefinedDomains];
                if (Connect.ListDefinedDomains(conn, ref definedDomainNames, numOfDefinedDomains) == -1)
                {
                    MessageBox.Show("Unable to list defined domains", "List defined domains failed", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    goto cleanup;
                }

                // Add the domain names to the listbox
                foreach (string domainName in definedDomainNames)
                    lbDomains.Items.Add(domainName);

                // Get the number of running domains
                int numOfRunningDomain = Connect.NumOfDomains(conn);
                int[] runningDomainIDs = new int[numOfRunningDomain];
                if (Connect.ListDomains(conn, runningDomainIDs, numOfRunningDomain) == -1)
                {
                    MessageBox.Show("Unable to list running domains", "List running domains failed", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                    goto cleanup;
                }

                // Add the domain names to the listbox
                foreach (int runningDomainID in runningDomainIDs)
                {
                    IntPtr domainPtr = Domain.LookupByID(conn, runningDomainID);
                    if (domainPtr == IntPtr.Zero)
                    {
                        MessageBox.Show("Unable to lookup domains by id", "Lookup domain failed", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                        goto cleanup;
                    }
                    string domainName = Domain.GetName(domainPtr);
                    Domain.Free(domainPtr);
                    if (string.IsNullOrEmpty(domainName))
                    {
                        MessageBox.Show("Unable to get domain name", "Get domain name failed", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                        goto cleanup;
                    }
                    lbDomains.Items.Add(domainName);
                }

            cleanup:
                Connect.Close(conn);
            }
            else
            {
                MessageBox.Show("Unable to connect", "Connection failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }