Example #1
0
 static void ThrowForHR(string action, HRESULT_UPnP hr, object result)
 {
     if (hr != HRESULT_UPnP.S_OK)
     {
         throw new COMException("Action " + action + " returns " + hr + " " + result, (int)hr);
     }
 }
Example #2
0
        /// <summary>
        /// 現在設定されているポートマッピング情報を得る。
        /// </summary>
        /// <returns>ポートマッピング情報</returns>
        public List <UPnPPortMapping> GetGenericPortMappingEntries()
        {
            object outArgs = null;
            object result  = null;
            List <UPnPPortMapping> list = new List <UPnPPortMapping>(16);

            for (int i = 0; ; i++)
            {
                HRESULT_UPnP hresult = service.InvokeAction("GetGenericPortMappingEntry", new object[] { i }, ref outArgs, out result);
                if (hresult != HRESULT_UPnP.S_OK)
                {
                    break;
                }

                object[] array = (object[])outArgs;
                list.Add(new UPnPPortMapping
                {
                    RemoteHost             = (string)array[0],
                    ExternalPort           = (ushort)array[1],
                    Protocol               = (string)array[2],
                    InternalPort           = (ushort)array[3],
                    InternalClient         = IPAddress.Parse((string)array[4]),
                    Enabled                = (bool)array[5],
                    PortMappingDescription = (string)array[6],
                    LeaseDuration          = (uint)array[7],
                });
            }
            return(list);
        }
Example #3
0
        /// <summary>
        /// グローバルIPアドレスを得る。
        /// </summary>
        /// <returns>IPアドレス</returns>
        public System.Net.IPAddress GetExternalIPAddress()
        {
            object       outArgs = null;
            object       result  = null;
            HRESULT_UPnP hresult = service.InvokeAction("GetExternalIPAddress", new object[] { }, ref outArgs, out result);

            ThrowForHR("GetExternalIPAddress", hresult, result);
            return(IPAddress.Parse((string)((object[])outArgs)[0]));
        }
Example #4
0
        /// <summary>
        /// ポートマッピングを削除する。
        /// </summary>
        /// <param name="remoteHost">追加時に指定した通信相手。</param>
        /// <param name="externalPort">追加時に指定した外部ポート番号。</param>
        /// <param name="protocol">追加時に指定されたプロトコル。</param>
        public void DeletePortMapping(string remoteHost, ushort externalPort, string protocol)
        {
            if (string.IsNullOrEmpty(remoteHost))
            {
                remoteHost = "";
            }
            if (protocol != "TCP" && protocol != "UDP")
            {
                throw new ArgumentException("protocol must be \"TCP\" or \"UDP\"", protocol);
            }

            object       outArgs = null;
            object       result  = null;
            HRESULT_UPnP hresult = service.InvokeAction("DeletePortMapping", new object[] { remoteHost, externalPort, protocol }, ref outArgs, out result);

            ThrowForHR("DeletePortMapping", hresult, result);
        }
Example #5
0
        /// <summary>
        /// ポートマッピングを追加する。
        /// </summary>
        /// <param name="remoteHost">通信相手。通信先を限定する場合に指定。</param>
        /// <param name="externalPort">グローバルポート番号。</param>
        /// <param name="protocol">プロトコル名。"TCP" or "UDP"を指定。</param>
        /// <param name="internalPort">内部クライアントのポート番号。</param>
        /// <param name="internalClient">内部クライアントのIPアドレス。</param>
        /// <param name="description">説明。任意。</param>
        /// <param name="leaseDuration">リース期間(秒単位)。0を指定すると無期限</param>
        public void AddPortMapping(string remoteHost, ushort externalPort, string protocol, ushort internalPort, IPAddress internalClient, bool enabled, string description, uint leaseDuration)
        {
            if (string.IsNullOrEmpty(remoteHost))
            {
                remoteHost = "";
            }
            if (string.IsNullOrEmpty(description))
            {
                description = "";
            }
            if (protocol != "TCP" && protocol != "UDP")
            {
                throw new ArgumentException("protocol must be \"TCP\" or \"UDP\"", protocol);
            }

            object       outArgs = null;
            object       result  = null;
            HRESULT_UPnP hresult = service.InvokeAction("AddPortMapping",
                                                        new object[] { remoteHost, externalPort, protocol, internalPort, internalClient.ToString(), enabled, description, leaseDuration },
                                                        ref outArgs, out result);

            ThrowForHR("AddPortMapping", hresult, result);
        }
Example #6
0
 static void ThrowForHR(string action, HRESULT_UPnP hr, object result)
 {
     if (hr != HRESULT_UPnP.S_OK)
         throw new COMException("Action " + action + " returns " + hr + " " + result, (int)hr);
 }