Exemplo n.º 1
0
        protected override void OnStop()
        {
            base.OnStop();

            if (_mtu != _origMtu)
            {
                using (var cfg = NetworkAdapter.CreateInstance(_iface))
                {
                    Logger.Debug("Restoring the MTU of '{0}' to {1}.", _iface, _origMtu.HasValue ? _origMtu.ToString() : "<null>");
                    cfg.MTU = _origMtu;
                }
            }

            _remoteEp = null;
            _localIp  = null;
            _iface    = null;
            _mtu      = null;
            _origMtu  = null;
        }
Exemplo n.º 2
0
        protected override void OnSetProperty(string property, Variant value)
        {
            if (property == "MTU")
            {
                uint mtu = 0;

                if (value.GetVariantType() == Variant.VariantType.BitStream)
                {
                    var bs = (BitwiseStream)value;
                    bs.SeekBits(0, SeekOrigin.Begin);
                    ulong bits;
                    int   len = bs.ReadBits(out bits, 32);
                    mtu = Endian.Little.GetUInt32(bits, len);
                }
                else if (value.GetVariantType() == Variant.VariantType.ByteString)
                {
                    byte[] buf = (byte[])value;
                    int    len = Math.Min(buf.Length * 8, 32);
                    mtu = Endian.Little.GetUInt32(buf, len);
                }
                else
                {
                    throw new SoftException("Can't set MTU, 'value' is an unsupported type.");
                }

                if (MaxMTU >= mtu && mtu >= MinMTU)
                {
                    using (var cfg = NetworkAdapter.CreateInstance(_iface))
                    {
                        try
                        {
                            cfg.MTU = mtu;
                        }
                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                            if (ex is TypeInitializationException || ex is TargetInvocationException)
                            {
                                msg = ex.InnerException.Message;
                            }

                            string err = "Failed to change MTU of '{0}' to {1}. {2}".Fmt(_iface, mtu, msg);
                            Logger.Error(err);
                            var se = new SoftException(err, ex);
                            throw new SoftException(se);
                        }

                        _mtu = cfg.MTU;

                        if (!_mtu.HasValue || _mtu.Value != mtu)
                        {
                            string err = "Failed to change MTU of '{0}' to {1}. The change did not take effect.".Fmt(_iface, mtu);
                            Logger.Error(err);
                            throw new SoftException(err);
                        }
                        else
                        {
                            Logger.Debug("Changed MTU of '{0}' to {1}.", _iface, mtu);
                        }
                    }
                }
                else
                {
                    Logger.Debug("Not setting MTU of '{0}', value is out of range.", _iface);
                }
            }
        }
Exemplo n.º 3
0
        protected override void OnStart()
        {
            base.OnStart();

            IPEndPoint ep;
            IPAddress  local;
            string     iface;
            uint?      mtu;

            try
            {
                ep = ResolveHost();

                if (!AddressFamilySupported(ep.AddressFamily))
                {
                    throw new PeachException(string.Format("The resolved IP '{0}' for host '{1}' is not compatible with the {2} publisher.", ep, Host, _type));
                }

                local = Interface;
                if (Interface == null)
                {
                    local = GetLocalIp(ep);
                }

                if (local.IsIPv6LinkLocal && local.ScopeId == 0)
                {
                    local = GetScopeId(local);
                    Logger.Trace("Resolved link-local interface IP for {0} socket to {1}.", _type, local);
                }

                try
                {
                    iface = GetInterfaceName(local);
                    if (iface == null)
                    {
                        throw new PeachException("Could not resolve interface name for local IP '{0}'.".Fmt(local));
                    }

                    using (var cfg = NetworkAdapter.CreateInstance(iface))
                    {
                        mtu = cfg.MTU;
                    }
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                    if (ex is TypeInitializationException || ex is TargetInvocationException)
                    {
                        msg = ex.InnerException.Message;
                    }

                    iface = local.ToString();
                    mtu   = null;
                    Logger.Debug("Could not query the MTU of '{0}'. {1}", iface, msg);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Unable to start {0} publisher for {1}:{2}. {3}.", _type, Host, Port, ex.Message);
                throw new SoftException(ex);
            }

            _remoteEp = ep;
            _localIp  = local;
            _iface    = iface;
            _mtu      = mtu;
            _origMtu  = mtu;
        }