UPNPポートマッピングAndroid実装


この段はずっとUPNPプロトコルを研究して、頭が爆発して、upnpのjarバッグを見つけて、InternetGatewayDeviceを発見するのにとても便利だと感じます.ルータを見つけてポートマッピングを行うウィジェットを以下に書きます.
package com.example.laozhou.upnptest;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;


import net.sbbi.upnp.impls.InternetGatewayDevice;
import net.sbbi.upnp.messages.ActionResponse;
import net.sbbi.upnp.messages.UPNPResponseException;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Locale;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        new Thread(discoverUPNP).start();
    }

    Runnable discoverUPNP = new Runnable() {

        @Override
        public void run() {
            int discoveryTiemout = 5000; // 5 secs
            try {
                System.out.println("Looking for Internet Gateway Device...");
                InternetGatewayDevice[] IGDs = InternetGatewayDevice.getDevices(discoveryTiemout);
                if (IGDs != null) {
                    for (int i = 0; i < IGDs.length; i++) {
                        InternetGatewayDevice testIGD = IGDs[i];
                        System.out.println("\t
Found device " + testIGD.getIGDRootDevice().getModelName()); System.out.println("External IP address: " + testIGD.getExternalIPAddress()); // now let's open the port int portNum = 12345; System.out.println("
Trying to map dummy port " + portNum + "..."); // String localHostIP = getIpAddress(); String localHostIP = "192.168.2.201"; System.out.println("localHostIP " + localHostIP); boolean mapped = testIGD.addPortMapping("chinavideo mapping", null, portNum, portNum, localHostIP, 0, "TCP"); System.out.println("AddPortState: " + mapped); if (mapped) { System.out.println("Port " + portNum + " mapped to " + localHostIP); System.out.println("Current mappings count is " + testIGD.getNatMappingsCount()); // checking on the device ActionResponse resp = testIGD.getSpecificPortMappingEntry(null, portNum, "TCP"); if (resp != null) { System.out.println("Port " + portNum + " mapping confirmation received from device"); } } } System.out.println("
Done."); } else { System.out.println("Unable to find IGD on your network"); } } catch (IOException ex) { System.err.println("IOException occured during discovery or ports mapping " + ex.getMessage()); } catch (UPNPResponseException respEx) { System.err.println("UPNP device unhappy " + respEx.getDetailErrorCode() + " " + respEx.getDetailErrorDescription()); } } }; private String getIpAddress() { WifiManager wifiManager = (WifiManager) Main2Activity.this.getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ipaddress = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)); return ipaddress; } public String getLocalIpAddress(){ try { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { Log.d("ipAddress:",inetAddress.getHostAddress().toString()); return inetAddress.getHostAddress().toString(); } } } } catch (Exception ex) { Log.e("IP Address", ex.toString()); } return null; } }

ここで、ポート番号は自分で定義することができます.私にはテスト可能なインタフェースがないので、ローカルエリアネットワーク内の1台のマシンのインタフェースをルータにマッピングしました.
使用するパッケージはupnplib-mobile.jar