Esempio n. 1
0
        /// <summary>
        /// Initializes a new OVPN Object.
        /// Also set a LogEventDelegate so that the first log lines are reveived.
        /// </summary>
        /// <param name="config">Path to configuration file</param>
        /// <param name="earlyLogEvent">Delegate to a event processor</param>
        /// <param name="earlyLogLevel">Log level</param>
        /// <seealso cref="OVPNConnection.logs"/>
        public OVPNServiceConnection(string config,
                                     OVPNLogManager.LogEventDelegate earlyLogEvent, int earlyLogLevel)
            : base(earlyLogEvent, earlyLogLevel)
        {
            if (config == null)
            {
                throw new ArgumentNullException("Config file is null");
            }
            if (!new FileInfo(config).Exists)
            {
                throw new FileNotFoundException("Config file \"" + config + "\" does not exist");
            }

            OVPNConfigFile cf = new OVPNConfigFile(config);


            //management 127.0.0.1 11194
            foreach (string directive in new String[] {
                "management-query-passwords", "management-hold",
                "management-signal", "management-forget-disconnect",
                "pkcs11-id-management", "management"
            })
            {
                if (!cf.exists(directive))
                {
                    throw new ArgumentException("The directive '" + directive
                                                + "' is needed in '" + config + "'");
                }
            }

            int port;

            string[] args = cf.get("management");
            if (args.GetUpperBound(0) != 2)
            {
                throw new ArgumentException("The directive 'management'"
                                            + " is invalid in '" + config + "'");
            }

            if (!int.TryParse(args[2], out port))
            {
                throw new ArgumentException("The port '" + args[0]
                                            + "' is invalid in '" + config + "'");
            }

            this.port = port;
            this.host = args[1];
        }
        /// <summary>
        /// Initializes a new OVPN Object.
        /// Also set a LogEventDelegate so that the first log lines are reveived.
        /// </summary>
        /// <param name="config">Path to configuration file</param>
        /// <param name="earlyLogEvent">Delegate to a event processor</param>
        /// <param name="earlyLogLevel">Log level</param>
        /// <seealso cref="OVPNConnection.logs"/>
        public OVPNServiceConnection(string config,
            OVPNLogManager.LogEventDelegate earlyLogEvent, int earlyLogLevel)
            : base(earlyLogEvent, earlyLogLevel)
        {
            if (config == null)
                throw new ArgumentNullException("Config file is null");
            if (!new FileInfo(config).Exists)
                throw new FileNotFoundException("Config file \"" + config + "\" does not exist");

            OVPNConfigFile cf = new OVPNConfigFile(config);


            //management 127.0.0.1 11194
            foreach (string directive in new String[]{ 
                "management-query-passwords", "management-hold",
                "management-signal", "management-forget-disconnect",
                "pkcs11-id-management", "management"}) {

                if(!cf.exists(directive))
                    throw new ArgumentException("The directive '" + directive
                        + "' is needed in '" + config + "'");
            }

            int port;
            string[] args = cf.get("management");
            if(args.GetUpperBound(0) != 2)
                throw new ArgumentException("The directive 'management'"
                            + " is invalid in '" + config + "'");

            if(!int.TryParse(args[2], out port))
                throw new ArgumentException("The port '" + args[0]
                        + "' is invalid in '" + config + "'");

            this.port = port;
            this.host = args[1];
        }