Ejemplo n.º 1
0
            /// <summary>
            /// Marshales native pointer to <see cref="ProtoEnt"/> instance.
            /// </summary>
            /// <param name="ptr">Pointer returned by <see cref="getprotobyname"/> or <see cref="getprotobynumber"/>.</param>
            /// <remarks>The wrapper avoids freeing of pointer returned from winsoc native library. The returned pointer is managed by winsoc library and must not be freed by CLI.</remarks>
            internal static ProtoEnt FromIntPtr(IntPtr ptr)
            {
                if (ptr == IntPtr.Zero)
                return null;

                // marshall returned object to ProtoEnt class:
                ProtoEnt result = new ProtoEnt();
                Marshal.PtrToStructure(ptr, result);
                return result;
            }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets protocol number by name (tcp/udp/icmp etc')
        /// </summary>
        /// <param name="name">protocol name</param>
        /// <returns>protocol number</returns>
        public static int GetProtoByName(string name)
        {
            // Vorbedingung
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            //check first in protocols file
            if (File.Exists(etcFileHelper.GetFullFilename(etcFileHelper.ProtocolFilename)))
            {
                var protocols = etcFileHelper.ReadProtocols(etcFileHelper.GetFullFilename(etcFileHelper.ProtocolFilename));
                var protocol  = protocols.FirstOrDefault(i => i.Name.ToLower() == name.ToLower());
                if (protocol != null)
                {
                    return(protocol.ProtocolNumber);
                }
            }

            //failed, use API.

            int result = 0;

            int intResult = -1;

            if (int.TryParse(name, out intResult))
            {
                result = System.Convert.ToInt32(name, CultureInfo.InvariantCulture);
            }
            else
            {
                WSAData dummy      = new WSAData();
                int     sccuessful = NativeMethods.WSAStartup(0x0202, ref dummy);
                if (sccuessful != 0)
                {
                    throw CreateWSAException();
                }

                IntPtr intPtr = NativeMethods.GetProtocolByName(name);

                try
                {
                    if (intPtr != IntPtr.Zero)
                    {
                        ProtoEnt protoent = (ProtoEnt)Marshal.PtrToStructure(intPtr, typeof(ProtoEnt));
                        result = System.Convert.ToInt32(protoent.p_proto);
                    }
                    else
                    {
                        throw CreateWSAException();
                    }
                }
                finally
                {
                    sccuessful = NativeMethods.WSACleanup();
                    if (sccuessful != 0)
                    {
                        throw CreateWSAException();
                    }
                }
            }

            return(result);
        }