コード例 #1
0
        /// <summary>
        /// Register bios name for further call
        /// </summary>
        /// <returns>The name index</returns>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// throw when register name fails
        /// </exception>
        public byte RegisterName()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosTransport");
            }

            NCB ncb = new NCB();

            try
            {
                NetbiosUtility.InitNcb(ref ncb);
                ncb.ncb_command  = (byte)NcbCommand.NCBADDNAME;
                ncb.ncb_lana_num = networkAdapterId;
                ncb.ncb_name     = NetbiosUtility.ToNetbiosName(localNetbiosName);

                InvokeNetBios(ref ncb);

                if (ncb.ncb_retcode != (byte)NcbReturnCode.NRC_GOODRET)
                {
                    throw new InvalidOperationException("Failed in NCBADDNAME command, error is "
                                                        + ((NcbReturnCode)ncb.ncb_retcode).ToString());
                }

                this.ncbNum = ncb.ncb_num;
                return(ncb.ncb_num);
            }
            finally
            {
                NetbiosUtility.FreeNcbNativeFields(ref ncb);
            }
        }
コード例 #2
0
        /// <summary>
        /// Unregister the bios name
        /// </summary>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        public void UnRegisterName()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosTransport");
            }

            // if the local name in .ctr is null, exception is thrown,
            // the gc will dispose the object and this method will be invoked.
            if (this.localNetbiosName == null)
            {
                return;
            }

            NCB ncb = new NCB();

            try
            {
                NetbiosUtility.InitNcb(ref ncb);
                ncb.ncb_command  = (byte)NcbCommand.NCBDELNAME;
                ncb.ncb_lana_num = networkAdapterId;
                ncb.ncb_num      = ncbNum;
                ncb.ncb_name     = NetbiosUtility.ToNetbiosName(localNetbiosName);

                InvokeNetBios(ref ncb);
            }
            finally
            {
                NetbiosUtility.FreeNcbNativeFields(ref ncb);
            }
        }
コード例 #3
0
        /// <summary>
        /// Reset the adapter, it will clear all registered names, and reset the maxsession,
        /// maxName
        /// </summary>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// throw when reset adapter fails
        /// </exception>
        public void ResetAdapter()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosTransport");
            }

            NCB ncb = new NCB();

            try
            {
                NetbiosUtility.InitNcb(ref ncb);
                ncb.ncb_command  = (byte)NcbCommand.NCBRESET;
                ncb.ncb_lana_num = networkAdapterId;
                Marshal.WriteByte(ncb.ncb_callname, 0, this.maxSessionNum);
                Marshal.WriteByte(ncb.ncb_callname, 2, this.maxNames);

                InvokeNetBios(ref ncb);

                if (ncb.ncb_retcode != (byte)NcbReturnCode.NRC_GOODRET)
                {
                    throw new InvalidOperationException("Failed in NCBRESET command, error is "
                                                        + ((NcbReturnCode)ncb.ncb_retcode).ToString());
                }
            }
            finally
            {
                NetbiosUtility.FreeNcbNativeFields(ref ncb);
            }
        }
コード例 #4
0
        /// <summary>
        /// Listen to local netbios endpoint
        /// </summary>
        /// <returns>The connected session id</returns>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// throw when listen fails
        /// </exception>
        public byte Listen()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosTransport");
            }

            NCB ncb = new NCB();

            try
            {
                NetbiosUtility.InitNcb(ref ncb);
                ncb.ncb_command  = (byte)NcbCommand.NCBLISTEN;
                ncb.ncb_lana_num = networkAdapterId;
                //* means it can accept any connection.
                ncb.ncb_callname = NetbiosUtility.ToNetbiosName("*");
                ncb.ncb_name     = NetbiosUtility.ToNetbiosName(localNetbiosName);

                InvokeNetBios(ref ncb);

                if (ncb.ncb_retcode != (byte)NcbReturnCode.NRC_GOODRET)
                {
                    throw new InvalidOperationException("Failed in NCBLISTEN command, error is "
                                                        + ((NcbReturnCode)ncb.ncb_retcode).ToString());
                }
            }
            finally
            {
                NetbiosUtility.FreeNcbNativeFields(ref ncb);
            }

            return(ncb.ncb_lsn);
        }
コード例 #5
0
        /// <summary>
        /// Disconnect the session
        /// </summary>
        /// <param name="sessionId">The session id</param>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// throw when disconnect fails
        /// </exception>
        public void Disconnect(int sessionId)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosTransport");
            }

            NCB ncb = new NCB();

            try
            {
                NetbiosUtility.InitNcb(ref ncb);
                ncb.ncb_command  = (byte)NcbCommand.NCBHANGUP;
                ncb.ncb_lana_num = networkAdapterId;
                ncb.ncb_lsn      = (byte)sessionId;

                InvokeNetBios(ref ncb);

                //if remote machine disconnect the session first, local call disconnect will return NRC_SNUMOUT,
                //and this is not an error which we need to notify user.
                if ((ncb.ncb_retcode != (byte)NcbReturnCode.NRC_GOODRET) &&
                    (ncb.ncb_retcode != (byte)NcbReturnCode.NRC_SNUMOUT) &&
                    (ncb.ncb_retcode != (byte)NcbReturnCode.NRC_SCLOSED))
                {
                    throw new InvalidOperationException("Failed in NCBHANGUP command, error is "
                                                        + ((NcbReturnCode)ncb.ncb_retcode).ToString());
                }
            }
            finally
            {
                NetbiosUtility.FreeNcbNativeFields(ref ncb);
            }
        }
コード例 #6
0
        /// <summary>
        /// Receive data
        /// </summary>
        /// <param name="sessionId">Specified the session identify</param>
        /// <returns>The received data</returns>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// throw when receive encounters error.
        /// </exception>
        public byte[] Receive(int sessionId)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosTransport");
            }

            NCB ncb = new NCB();

            try
            {
                NetbiosUtility.InitNcb(ref ncb);
                ncb.ncb_command  = (byte)NcbCommand.NCBRECV;
                ncb.ncb_lana_num = networkAdapterId;
                ncb.ncb_lsn      = (byte)sessionId;
                ncb.ncb_buffer   = Marshal.AllocHGlobal(maxBufferSize);
                ncb.ncb_length   = maxBufferSize;

                InvokeNetBios(ref ncb);

                if (ncb.ncb_retcode == (byte)NcbReturnCode.NRC_SCLOSED)
                {
                    return(null);
                }

                if (ncb.ncb_retcode != (byte)NcbReturnCode.NRC_GOODRET)
                {
                    throw new InvalidOperationException("Failed in NCBRECV command, error is "
                                                        + ((NcbReturnCode)ncb.ncb_retcode).ToString());
                }

                byte[] receivedData = new byte[ncb.ncb_length];
                Marshal.Copy(ncb.ncb_buffer, receivedData, 0, receivedData.Length);

                return(receivedData);
            }
            finally
            {
                NetbiosUtility.FreeNcbNativeFields(ref ncb);
            }
        }
コード例 #7
0
        /// <summary>
        /// Get the adapter id
        /// </summary>
        /// <returns>The adapter id</returns>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// throw when Get adapterId fails
        /// </exception>
        public byte GetAdapterId()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosTransport");
            }

            NCB ncb = new NCB();

            try
            {
                NetbiosUtility.InitNcb(ref ncb);
                ncb.ncb_command = (byte)NcbCommand.NCBENUM;
                ncb.ncb_buffer  = Marshal.AllocHGlobal(maxBufferSize);
                ncb.ncb_length  = maxBufferSize;

                InvokeNetBios(ref ncb);

                if (ncb.ncb_retcode != (byte)NcbReturnCode.NRC_GOODRET)
                {
                    throw new InvalidOperationException("Failed in NCBENUM command, error is "
                                                        + ((NcbReturnCode)ncb.ncb_retcode).ToString());
                }

                LANA_ENUM lenum = new LANA_ENUM();
                lenum.length  = Marshal.ReadByte(ncb.ncb_buffer, 0);
                lenum.lanaNum = new byte[lenum.length];

                for (int i = 0; i < lenum.length; i++)
                {
                    lenum.lanaNum[i] = Marshal.ReadByte(ncb.ncb_buffer, i + 1);
                }

                return(lenum.lanaNum[adapterIndex]);
            }
            finally
            {
                NetbiosUtility.FreeNcbNativeFields(ref ncb);
            }
        }
コード例 #8
0
        /// <summary>
        /// Send data
        /// </summary>
        /// <param name="sessionId">The session id</param>
        /// <param name="buffer">The data buffer</param>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// throw when the buffer is larger than the max buffer size
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// throw when call native netbios api fails
        /// </exception>
        public void Send(int sessionId, byte[] buffer)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosTransport");
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            NCB ncb = new NCB();

            try
            {
                NetbiosUtility.InitNcb(ref ncb);
                ncb.ncb_command = (byte)NcbCommand.NCBSEND;
                ncb.ncb_buffer  = Marshal.AllocHGlobal(buffer.Length);
                Marshal.Copy(buffer, 0, ncb.ncb_buffer, buffer.Length);
                ncb.ncb_length   = (ushort)buffer.Length;
                ncb.ncb_lana_num = networkAdapterId;
                ncb.ncb_lsn      = (byte)sessionId;

                InvokeNetBios(ref ncb);

                if (ncb.ncb_retcode != (byte)NcbReturnCode.NRC_GOODRET)
                {
                    throw new InvalidOperationException("Failed in NCBSEND command, error is "
                                                        + ((NcbReturnCode)ncb.ncb_retcode).ToString());
                }
            }
            finally
            {
                NetbiosUtility.FreeNcbNativeFields(ref ncb);
            }
        }
コード例 #9
0
        /// <summary>
        /// Connect to remote machine
        /// </summary>
        /// <param name="remoteName">The remote machine bios name</param>
        /// <returns>The session id</returns>
        /// <exception cref="ObjectDisposedException">
        /// thrown when this object is disposed.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// throw when connect fails.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// thrown when remoteName is null.
        /// </exception>
        public int Connect(string remoteName)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("NetbiosTransport");
            }

            if (remoteName == null)
            {
                throw new ArgumentNullException("remoteName");
            }

            NCB ncb = new NCB();

            try
            {
                NetbiosUtility.InitNcb(ref ncb);
                ncb.ncb_command  = (byte)NcbCommand.NCBCALL;
                ncb.ncb_lana_num = networkAdapterId;
                ncb.ncb_name     = NetbiosUtility.ToNetbiosName(localNetbiosName);
                ncb.ncb_callname = NetbiosUtility.ToNetbiosName(remoteName);

                InvokeNetBios(ref ncb);

                if (ncb.ncb_retcode != (byte)NcbReturnCode.NRC_GOODRET)
                {
                    throw new InvalidOperationException("Failed in NCBCALL command, error is "
                                                        + ((NcbReturnCode)ncb.ncb_retcode).ToString());
                }
            }
            finally
            {
                NetbiosUtility.FreeNcbNativeFields(ref ncb);
            }

            return(ncb.ncb_lsn);
        }