MIB_TCP6TABLE
構造体サイズ=各フィールドのバイト数(x64/x86 で異なる場合は x64/x86 と併記)。x64/x86 列=フィールドのバイトオフセット(HSPで dupptr / lpoke / wpoke 等に使用)。
フィールド
| フィールド | 型 | サイズ | x64 | x86 | 説明 |
|---|---|---|---|---|---|
| dwNumEntries | DWORD | 4 | +0 | +0 | 配列内の TCP 接続の数を指定する値です。 |
| table | MIB_TCP6ROW | 52 | +4 | +4 | TCP 接続のエントリを格納する MIB_TCP6ROW 構造体の配列です。 |
公式ドキュメント
MIB_TCP6TABLE 構造体は、ローカル コンピューター上の IPv6 の TCP 接続のテーブルを格納します。
解説(Remarks)
MIB_TCP6TABLE 構造体は、Windows Vista 以降で定義されています。
GetTcp6Table 関数は、ローカル コンピューター上の IPv6 TCP 接続テーブルを取得し、この情報を MIB_TCP6TABLE 構造体で返します。
MIB_TCP6TABLE 構造体には、MIB_TCP6ROW 構造体の配列が含まれます。
MIB_TCP6TABLE 構造体では、dwNumEntries メンバーと table メンバー内の最初の MIB_TCP6ROW 配列エントリとの間に、位置合わせのためのパディングが入ることがあります。また、table メンバー内の MIB_TCP6ROW 配列エントリ同士の間にも、位置合わせのためのパディングが存在する場合があります。MIB_TCP6ROW 配列エントリにアクセスする際は、パディングが存在する可能性を前提にしてください。
Examples
次の例では、IPv6 の TCP 接続テーブルを MIB_TCP6TABLE 構造体として取得し、MIB_TCP6ROW 構造体で表される各接続の状態を出力します。
#define UNICODE 1
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
// Need to link with Iphlpapi.lib and Ws2_32.lib
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */
int wmain()
{
// Declare and initialize variables
PMIB_TCP6TABLE pTcpTable;
DWORD dwSize = 0;
DWORD dwRetVal = 0;
wchar_t ipstringbuffer[46];
int i;
pTcpTable = (MIB_TCP6TABLE *) MALLOC(sizeof (MIB_TCP6TABLE));
if (pTcpTable == NULL) {
wprintf(L"Error allocating memory\n");
return 1;
}
dwSize = sizeof (MIB_TCP6TABLE);
// Make an initial call to GetTcp6Table to
// get the necessary size into the dwSize variable
if ((dwRetVal = GetTcp6Table(pTcpTable, &dwSize, TRUE)) ==
ERROR_INSUFFICIENT_BUFFER) {
FREE(pTcpTable);
pTcpTable = (MIB_TCP6TABLE *) MALLOC(dwSize);
if (pTcpTable == NULL) {
wprintf(L"Error allocating memory\n");
return 1;
}
}
// Make a second call to GetTcp6Table to get
// the actual data we require
if ((dwRetVal = GetTcp6Table(pTcpTable, &dwSize, TRUE)) == NO_ERROR) {
wprintf(L"\tNumber of entries: %d\n", (int) pTcpTable->dwNumEntries);
for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {
wprintf(L"\n\tTCP[%d] State: %ld - ", i,
pTcpTable->table[i].State);
switch (pTcpTable->table[i].State) {
case MIB_TCP_STATE_CLOSED:
wprintf(L"CLOSED\n");
break;
case MIB_TCP_STATE_LISTEN:
wprintf(L"LISTEN\n");
break;
case MIB_TCP_STATE_SYN_SENT:
wprintf(L"SYN-SENT\n");
break;
case MIB_TCP_STATE_SYN_RCVD:
wprintf(L"SYN-RECEIVED\n");
break;
case MIB_TCP_STATE_ESTAB:
wprintf(L"ESTABLISHED\n");
break;
case MIB_TCP_STATE_FIN_WAIT1:
wprintf(L"FIN-WAIT-1\n");
break;
case MIB_TCP_STATE_FIN_WAIT2:
wprintf(L"FIN-WAIT-2 \n");
break;
case MIB_TCP_STATE_CLOSE_WAIT:
wprintf(L"CLOSE-WAIT\n");
break;
case MIB_TCP_STATE_CLOSING:
wprintf(L"CLOSING\n");
break;
case MIB_TCP_STATE_LAST_ACK:
wprintf(L"LAST-ACK\n");
break;
case MIB_TCP_STATE_TIME_WAIT:
wprintf(L"TIME-WAIT\n");
break;
case MIB_TCP_STATE_DELETE_TCB:
wprintf(L"DELETE-TCB\n");
break;
default:
wprintf(L"UNKNOWN dwState value\n");
break;
}
if (InetNtop(AF_INET6, &pTcpTable->table[i].LocalAddr, ipstringbuffer, 46) == NULL)
wprintf(L" InetNtop function failed for local IPv6 address\n");
else
wprintf(L"\tTCP[%d] Local Addr: %s\n", i, ipstringbuffer);
wprintf(L"\tTCP[%d] Local Scope ID: %d \n", i,
ntohl (pTcpTable->table[i].dwLocalScopeId));
wprintf(L"\tTCP[%d] Local Port: %d \n", i,
ntohs((u_short)pTcpTable->table[i].dwLocalPort));
if (InetNtop(AF_INET6, &pTcpTable->table[i].RemoteAddr, ipstringbuffer, 46) == NULL)
wprintf(L" InetNtop function failed for remote IPv6 address\n");
else
wprintf(L"\tTCP[%d] Remote Addr: %s\n", i, ipstringbuffer);
wprintf(L"\tTCP[%d] Remote Scope ID: %d \n", i,
ntohl(pTcpTable->table[i].dwRemoteScopeId));
wprintf(L"\tTCP[%d] Remote Port: %d\n", i,
ntohs((u_short)pTcpTable->table[i].dwRemotePort));
}
} else {
wprintf(L"\tGetTcp6Table failed with %d\n", dwRetVal);
FREE(pTcpTable);
return 1;
}
if (pTcpTable != NULL) {
FREE(pTcpTable);
pTcpTable = NULL;
}
return 0;
}
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での定義
#include <windows.h>
// IN6_ADDR (x64 16 / x86 16 バイト)
typedef struct IN6_ADDR {
_u_e__Union u;
} IN6_ADDR;
// MIB_TCP6ROW (x64 52 / x86 52 バイト)
typedef struct MIB_TCP6ROW {
MIB_TCP_STATE State;
IN6_ADDR LocalAddr;
DWORD dwLocalScopeId;
DWORD dwLocalPort;
IN6_ADDR RemoteAddr;
DWORD dwRemoteScopeId;
DWORD dwRemotePort;
} MIB_TCP6ROW;
// MIB_TCP6TABLE (x64 56 / x86 56 バイト)
typedef struct MIB_TCP6TABLE {
DWORD dwNumEntries;
MIB_TCP6ROW table[1];
} MIB_TCP6TABLE;using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct IN6_ADDR
{
public _u_e__Union u;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MIB_TCP6ROW
{
public int State;
public IN6_ADDR LocalAddr;
public uint dwLocalScopeId;
public uint dwLocalPort;
public IN6_ADDR RemoteAddr;
public uint dwRemoteScopeId;
public uint dwRemotePort;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct MIB_TCP6TABLE
{
public uint dwNumEntries;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public MIB_TCP6ROW[] table;
}Imports System.Runtime.InteropServices
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public Structure IN6_ADDR
Public u As _u_e__Union
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public Structure MIB_TCP6ROW
Public State As Integer
Public LocalAddr As IN6_ADDR
Public dwLocalScopeId As UInteger
Public dwLocalPort As UInteger
Public RemoteAddr As IN6_ADDR
Public dwRemoteScopeId As UInteger
Public dwRemotePort As UInteger
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)>
Public Structure MIB_TCP6TABLE
Public dwNumEntries As UInteger
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=1)> Public table() As MIB_TCP6ROW
End Structureimport ctypes
from ctypes import wintypes
class IN6_ADDR(ctypes.Structure):
_fields_ = [
("u", _u_e__Union),
]
class MIB_TCP6ROW(ctypes.Structure):
_fields_ = [
("State", ctypes.c_int),
("LocalAddr", IN6_ADDR),
("dwLocalScopeId", wintypes.DWORD),
("dwLocalPort", wintypes.DWORD),
("RemoteAddr", IN6_ADDR),
("dwRemoteScopeId", wintypes.DWORD),
("dwRemotePort", wintypes.DWORD),
]
class MIB_TCP6TABLE(ctypes.Structure):
_fields_ = [
("dwNumEntries", wintypes.DWORD),
("table", MIB_TCP6ROW * 1),
]#[repr(C)]
pub struct IN6_ADDR {
pub u: _u_e__Union,
}
#[repr(C)]
pub struct MIB_TCP6ROW {
pub State: i32,
pub LocalAddr: IN6_ADDR,
pub dwLocalScopeId: u32,
pub dwLocalPort: u32,
pub RemoteAddr: IN6_ADDR,
pub dwRemoteScopeId: u32,
pub dwRemotePort: u32,
}
#[repr(C)]
pub struct MIB_TCP6TABLE {
pub dwNumEntries: u32,
pub table: [MIB_TCP6ROW; 1],
}import "golang.org/x/sys/windows"
type IN6_ADDR struct {
u _u_e__Union
}
type MIB_TCP6ROW struct {
State int32
LocalAddr IN6_ADDR
dwLocalScopeId uint32
dwLocalPort uint32
RemoteAddr IN6_ADDR
dwRemoteScopeId uint32
dwRemotePort uint32
}
type MIB_TCP6TABLE struct {
dwNumEntries uint32
table [1]MIB_TCP6ROW
}type
IN6_ADDR = record
u: _u_e__Union;
end;
MIB_TCP6ROW = record
State: Integer;
LocalAddr: IN6_ADDR;
dwLocalScopeId: DWORD;
dwLocalPort: DWORD;
RemoteAddr: IN6_ADDR;
dwRemoteScopeId: DWORD;
dwRemotePort: DWORD;
end;
MIB_TCP6TABLE = record
dwNumEntries: DWORD;
table: array[0..0] of MIB_TCP6ROW;
end;const IN6_ADDR = extern struct {
u: _u_e__Union,
};
const MIB_TCP6ROW = extern struct {
State: i32,
LocalAddr: IN6_ADDR,
dwLocalScopeId: u32,
dwLocalPort: u32,
RemoteAddr: IN6_ADDR,
dwRemoteScopeId: u32,
dwRemotePort: u32,
};
const MIB_TCP6TABLE = extern struct {
dwNumEntries: u32,
table: [1]MIB_TCP6ROW,
};type
IN6_ADDR {.bycopy.} = object
u: _u_e__Union
MIB_TCP6ROW {.bycopy.} = object
State: int32
LocalAddr: IN6_ADDR
dwLocalScopeId: uint32
dwLocalPort: uint32
RemoteAddr: IN6_ADDR
dwRemoteScopeId: uint32
dwRemotePort: uint32
MIB_TCP6TABLE {.bycopy.} = object
dwNumEntries: uint32
table: array[1, MIB_TCP6ROW]struct IN6_ADDR
{
_u_e__Union u;
}
struct MIB_TCP6ROW
{
int State;
IN6_ADDR LocalAddr;
uint dwLocalScopeId;
uint dwLocalPort;
IN6_ADDR RemoteAddr;
uint dwRemoteScopeId;
uint dwRemotePort;
}
struct MIB_TCP6TABLE
{
uint dwNumEntries;
MIB_TCP6ROW[1] table;
}HSP用 定義
HSP3.7/3.8 は構造体機能が無いため4byte整数配列(dim)+peek/poke で操作(32/64bitでサイズ・位置が異なる場合はタブで分割)。IronHSP は NSTRUCT(#defstruct/stdim/->)で32/64bit共通。
; HSP3.7/3.8 は構造体機能が無いため、4byte整数の配列変数で操作します。(x64 レイアウト)
; MIB_TCP6TABLE サイズ: 56 バイト(x64)
dim st, 14 ; 4byte整数×14(構造体サイズ 56 / 4 切り上げ)
; dwNumEntries : DWORD (+0, 4byte) st.0 = 値 / 値 = st.0 (lpoke/lpeek も可)
; table : MIB_TCP6ROW (+4, 52byte) varptr(st)+4 を基点に操作(52byte:入れ子/配列)
; ※4byte境界の整数は添字 st.N(N=オフセット/4)で読み書き可。それ以外は peek/poke 系を使用。; IronHSP は NSTRUCT(構造体)をサポート。32bit/64bit どちらでも同じコードで動作します。
; ※GUID・入れ子構造体はデフォルト型でないため、依存する #defstruct を先に定義(下記に同梱)。
#defstruct global IN6_ADDR
#field byte u 16
#endstruct
#defstruct global MIB_TCP6ROW
#field int State
#field IN6_ADDR LocalAddr
#field int dwLocalScopeId
#field int dwLocalPort
#field IN6_ADDR RemoteAddr
#field int dwRemoteScopeId
#field int dwRemotePort
#endstruct
#defstruct global MIB_TCP6TABLE
#field int dwNumEntries
#field MIB_TCP6ROW table 1
#endstruct
stdim st, MIB_TCP6TABLE ; NSTRUCT 変数を確保
st->dwNumEntries = 100
mes "dwNumEntries=" + st->dwNumEntries