ネットワークインターフェース情報の取得(4)
JavaのNetworkInterfaceにある、getByInetAddress()メソッドを実装するために、C++のIfInfoクラスと、JNIにメソッドを追加しました。
ifinfo.h
static IfInfo findInterfaceByAddress(String address);
ifinfo.cpp
/* * インターフェースの一覧から指定のIPアドレスがバインドされたインターフェース情報を取得する * param : IPアドレス(String) * * return : IfInfo */ IfInfo IfInfo::findInterfaceByAddress(String address) { IfInfo if_table; interface_list net_list = getIfList(); for (if_iter if_ent = net_list.begin(); if_ent != net_list.end(); if_ent++) { address_list ad_list = if_ent->getIpAddressList(); for (al_iter al_ent = ad_list.begin(); al_ent != ad_list.end(); al_ent++) { if (al_ent->getIpAddress().compare(address) == 0) { return *if_ent; } } } return if_table; }
IfInfo.java
public static native IfInfo getInterfaceByAddress(String address);
jninet.cpp
JNIEXPORT jobject JNICALL Java_net_IfInfo_getInterfaceByAddress(JNIEnv *env, jclass cls, jstring address) { jobject one_interface = NULL; const jchar *words = env->GetStringChars(address, NULL); _bstr_t address_str = _bstr_t((wchar_t *)words); env->ReleaseStringChars(address, words); String address_value = String((_TCHAR *)address_str); IfInfo info = IfInfo::findInterfaceByAddress(address_value); if (info.getIndex() > 0) { one_interface = load_class(env, info); } return one_interface; }
それで、Javaのテストプログラムに
ni = IfInfo.getInterfaceByAddress("192.168.xxx.xxx"); System.out.println("Found interface:"); print(ni);
を追加して、動作確認はOKでした。