.NET/ARPテーブルを取得し表示するサンプル・GetIpNetTable
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
]
開始行:
#navi(../)
*ARPテーブルを取得し表示するサンプル・GetIpNetTable API [...
iphlpapi.dll の GetIpNetTable API を使用しARPリストを取得...
#contents
* 関連記事 [#ka7de2a8]
-[[IPv4からMACアドレス取得するSendARP()APIを使ってみる>.N...
* 参考サイト [#fa1ec922]
- c# get arp cache~
https://gist.github.com/invictus-0x90/2e1ebcfefd8d7478e69...
- SIN@SAPPOROWORKSの覚書 ARPテーブルの取得 (C#)(F#)~
https://furuya02.hatenablog.com/entry/20111107/1399766912
- Microsoft GetIpNetTable 関数 (iphlpapi.h)~
https://learn.microsoft.com/ja-jp/windows/win32/api/iphlp...
- Microsoft MIB_IPNETTABLE structure (ipmib.h)~
https://learn.microsoft.com/en-us/windows/win32/api/ipmib...
- Microsoft MIB_IPNETROW_LH structure (ipmib.h)~
https://learn.microsoft.com/en-us/windows/win32/api/ipmib...
* 動作確認環境 [#qc949720]
- Windows 10 ver.22H2
- .NET6.0
- Visual Studio 2022
* C#サンプルコード [#e6de880e]
GetIpNetTable API を使用し取得したARPテーブル情報を表示す...
説明に関してはコメントを参照してください。
#ref(Program.cs)
using System.Runtime.InteropServices;
namespace ARPList
{
internal class Program
{
// GetIpNetTable 関数 (iphlpapi.h): https://lear...
[DllImport("iphlpapi.dll", EntryPoint = "GetIpNe...
static extern int GetIpNetTable(IntPtr pIpNetTab...
[StructLayout(LayoutKind.Sequential)]
struct MIB_IPNETROW
{
[MarshalAs(UnmanagedType.U4)]
public uint dwIndex;
[MarshalAs(UnmanagedType.U4)]
public uint dwPhysAddrLen;
[MarshalAs(UnmanagedType.U1)]
public byte mac0;
[MarshalAs(UnmanagedType.U1)]
public byte mac1;
[MarshalAs(UnmanagedType.U1)]
public byte mac2;
[MarshalAs(UnmanagedType.U1)]
public byte mac3;
[MarshalAs(UnmanagedType.U1)]
public byte mac4;
[MarshalAs(UnmanagedType.U1)]
public byte mac5;
[MarshalAs(UnmanagedType.U1)]
public byte mac6;
[MarshalAs(UnmanagedType.U1)]
public byte mac7;
[MarshalAs(UnmanagedType.U4)]
public uint dwAddr;
[MarshalAs(UnmanagedType.U4)]
public uint dwType;
}
private const int SUCCESS = 0;
private const int ERROR_INSUFFICIENT_BUFFER = 0x...
private const uint MIB_IPNET_TYPE_OTHER = 1;
private const uint MIB_IPNET_TYPE_INVALID = 2;
private const uint MIB_IPNET_TYPE_DYNAMIC = 3;
private const uint MIB_IPNET_TYPE_STATIC = 4;
static void Main(string[] args)
{
int result;
int requestBuffSize = 0;
// ARPテーブル格納用のメモリエリアを確保する
// 格納メモリ不足の ERROR_INSUFFICIENT_BUFFE...
// ERROR_INSUFFICIENT_BUFFER 以外が返却され...
// GetIpNetTable: https://learn.microsoft.co...
result = GetIpNetTable(IntPtr.Zero, ref requ...
if (result != ERROR_INSUFFICIENT_BUFFER)
{
Console.Error.WriteLine("GetIpNetTable A...
Environment.Exit((int)result);
}
try
{
// Marshal.AllocCoTaskMem: https://learn...
IntPtr resultBuff = Marshal.AllocCoTaskM...
// 確保した格納用メモリを引数に設定し Ge...
// 0(SUCCESS)以外が返却された場合は処理...
result = GetIpNetTable(resultBuff, ref r...
if (result != 0)
{
Environment.Exit(result);
}
// ARPエントリ数が0の場合、処理を終了する
int entries = Marshal.ReadInt32(resultBu...
if (entries == 0)
{
Environment.Exit(0);
}
// GetIpNetTable API から返却された MIB_...
// MIB_IPNETTABLE: https://learn.microso...
IntPtr currentPtr = IntPtr.Add(resultBuf...
// 取得したARP情報を格納するListオブジェ...
List<MIB_IPNETROW> arpTableList = new Li...
for (int i = 0; i < entries; i++)
{
// List<MIB_IPNETROW> に追加
arpTableList.Add((MIB_IPNETROW)Marsh...
// 次のデータの先頭に移動
currentPtr = IntPtr.Add(currentPtr, ...
}
// メモリの解放
Marshal.FreeHGlobal(resultBuff);
// 取得したARPテーブルからMACアドレス、I...
foreach (var item in arpTableList)
{
//IPアドレス
Byte[] b = BitConverter.GetBytes(ite...
string ip = string.Format("{0}.{1}.{...
string mac = string.Format("{0:x2}-{...
// TYPE
// MIB_IPNET_TYPE:https://learn.micr...
string type = string.Empty;
switch (item.dwType)
{
case MIB_IPNET_TYPE_OTHER:
type = "OTHER";
break;
case MIB_IPNET_TYPE_INVALID:
type = "INVALID";
break;
case MIB_IPNET_TYPE_DYNAMIC:
type = "DYNAMIC";
break;
case MIB_IPNET_TYPE_STATIC:
type = "STATIC";
break;
}
Console.WriteLine("| {0,-15} | {1} |...
}
}
catch (Exception e)
{
Console.Error.WriteLine("Exception throw...
Environment.Exit(1);
}
}
}
}
* 実行結果 [#d1174116]
マルチキャストアドレスも表示されていますね。~
ネットワーク上のARPキャッシュの情報が表示されました。
#br
#ref(01.png)
以上、GetIpNetTable API を使ってARPテーブル情報を表示する...
終了行:
#navi(../)
*ARPテーブルを取得し表示するサンプル・GetIpNetTable API [...
iphlpapi.dll の GetIpNetTable API を使用しARPリストを取得...
#contents
* 関連記事 [#ka7de2a8]
-[[IPv4からMACアドレス取得するSendARP()APIを使ってみる>.N...
* 参考サイト [#fa1ec922]
- c# get arp cache~
https://gist.github.com/invictus-0x90/2e1ebcfefd8d7478e69...
- SIN@SAPPOROWORKSの覚書 ARPテーブルの取得 (C#)(F#)~
https://furuya02.hatenablog.com/entry/20111107/1399766912
- Microsoft GetIpNetTable 関数 (iphlpapi.h)~
https://learn.microsoft.com/ja-jp/windows/win32/api/iphlp...
- Microsoft MIB_IPNETTABLE structure (ipmib.h)~
https://learn.microsoft.com/en-us/windows/win32/api/ipmib...
- Microsoft MIB_IPNETROW_LH structure (ipmib.h)~
https://learn.microsoft.com/en-us/windows/win32/api/ipmib...
* 動作確認環境 [#qc949720]
- Windows 10 ver.22H2
- .NET6.0
- Visual Studio 2022
* C#サンプルコード [#e6de880e]
GetIpNetTable API を使用し取得したARPテーブル情報を表示す...
説明に関してはコメントを参照してください。
#ref(Program.cs)
using System.Runtime.InteropServices;
namespace ARPList
{
internal class Program
{
// GetIpNetTable 関数 (iphlpapi.h): https://lear...
[DllImport("iphlpapi.dll", EntryPoint = "GetIpNe...
static extern int GetIpNetTable(IntPtr pIpNetTab...
[StructLayout(LayoutKind.Sequential)]
struct MIB_IPNETROW
{
[MarshalAs(UnmanagedType.U4)]
public uint dwIndex;
[MarshalAs(UnmanagedType.U4)]
public uint dwPhysAddrLen;
[MarshalAs(UnmanagedType.U1)]
public byte mac0;
[MarshalAs(UnmanagedType.U1)]
public byte mac1;
[MarshalAs(UnmanagedType.U1)]
public byte mac2;
[MarshalAs(UnmanagedType.U1)]
public byte mac3;
[MarshalAs(UnmanagedType.U1)]
public byte mac4;
[MarshalAs(UnmanagedType.U1)]
public byte mac5;
[MarshalAs(UnmanagedType.U1)]
public byte mac6;
[MarshalAs(UnmanagedType.U1)]
public byte mac7;
[MarshalAs(UnmanagedType.U4)]
public uint dwAddr;
[MarshalAs(UnmanagedType.U4)]
public uint dwType;
}
private const int SUCCESS = 0;
private const int ERROR_INSUFFICIENT_BUFFER = 0x...
private const uint MIB_IPNET_TYPE_OTHER = 1;
private const uint MIB_IPNET_TYPE_INVALID = 2;
private const uint MIB_IPNET_TYPE_DYNAMIC = 3;
private const uint MIB_IPNET_TYPE_STATIC = 4;
static void Main(string[] args)
{
int result;
int requestBuffSize = 0;
// ARPテーブル格納用のメモリエリアを確保する
// 格納メモリ不足の ERROR_INSUFFICIENT_BUFFE...
// ERROR_INSUFFICIENT_BUFFER 以外が返却され...
// GetIpNetTable: https://learn.microsoft.co...
result = GetIpNetTable(IntPtr.Zero, ref requ...
if (result != ERROR_INSUFFICIENT_BUFFER)
{
Console.Error.WriteLine("GetIpNetTable A...
Environment.Exit((int)result);
}
try
{
// Marshal.AllocCoTaskMem: https://learn...
IntPtr resultBuff = Marshal.AllocCoTaskM...
// 確保した格納用メモリを引数に設定し Ge...
// 0(SUCCESS)以外が返却された場合は処理...
result = GetIpNetTable(resultBuff, ref r...
if (result != 0)
{
Environment.Exit(result);
}
// ARPエントリ数が0の場合、処理を終了する
int entries = Marshal.ReadInt32(resultBu...
if (entries == 0)
{
Environment.Exit(0);
}
// GetIpNetTable API から返却された MIB_...
// MIB_IPNETTABLE: https://learn.microso...
IntPtr currentPtr = IntPtr.Add(resultBuf...
// 取得したARP情報を格納するListオブジェ...
List<MIB_IPNETROW> arpTableList = new Li...
for (int i = 0; i < entries; i++)
{
// List<MIB_IPNETROW> に追加
arpTableList.Add((MIB_IPNETROW)Marsh...
// 次のデータの先頭に移動
currentPtr = IntPtr.Add(currentPtr, ...
}
// メモリの解放
Marshal.FreeHGlobal(resultBuff);
// 取得したARPテーブルからMACアドレス、I...
foreach (var item in arpTableList)
{
//IPアドレス
Byte[] b = BitConverter.GetBytes(ite...
string ip = string.Format("{0}.{1}.{...
string mac = string.Format("{0:x2}-{...
// TYPE
// MIB_IPNET_TYPE:https://learn.micr...
string type = string.Empty;
switch (item.dwType)
{
case MIB_IPNET_TYPE_OTHER:
type = "OTHER";
break;
case MIB_IPNET_TYPE_INVALID:
type = "INVALID";
break;
case MIB_IPNET_TYPE_DYNAMIC:
type = "DYNAMIC";
break;
case MIB_IPNET_TYPE_STATIC:
type = "STATIC";
break;
}
Console.WriteLine("| {0,-15} | {1} |...
}
}
catch (Exception e)
{
Console.Error.WriteLine("Exception throw...
Environment.Exit(1);
}
}
}
}
* 実行結果 [#d1174116]
マルチキャストアドレスも表示されていますね。~
ネットワーク上のARPキャッシュの情報が表示されました。
#br
#ref(01.png)
以上、GetIpNetTable API を使ってARPテーブル情報を表示する...
ページ名: