Win32 API 日本語リファレンス
ホームNetworking.WinSock › TCP_HDR

TCP_HDR

構造体
サイズx64: 20 バイト / x86: 20 バイトパッキング1

サイズ=各フィールドのバイト数(x64/x86 で異なる場合は x64/x86 と併記)。x64/x86 列=フィールドのバイトオフセット(HSPで dupptr / lpoke / wpoke 等に使用)。

フィールド

フィールドサイズx64x86説明
th_sportWORD2+0+0送信元ポート番号をネットワークバイトオーダーで示す。
th_dportWORD2+2+2宛先ポート番号をネットワークバイトオーダーで示す。
th_seqDWORD4+4+4シーケンス番号を示す。
th_ackDWORD4+8+8確認応答番号(ACK番号)を示す。
_bitfieldBYTE1+12+12データオフセット(ヘッダー長)と予約ビットをまとめたビットフィールド。
th_flagsBYTE1+13+13制御フラグ(SYN/ACK/FIN/RST等)のビットマスク。
th_winWORD2+14+14受信ウィンドウサイズを示す。
th_sumWORD2+16+16TCPセグメントのチェックサムを示す。
th_urpWORD2+18+18緊急ポインタを示す。URGフラグ時に有効。

各言語での定義

#include <windows.h>

// TCP_HDR  (x64 20 / x86 20 バイト)
#pragma pack(push, 1)
typedef struct TCP_HDR {
    WORD th_sport;
    WORD th_dport;
    DWORD th_seq;
    DWORD th_ack;
    BYTE _bitfield;
    BYTE th_flags;
    WORD th_win;
    WORD th_sum;
    WORD th_urp;
} TCP_HDR;
#pragma pack(pop)
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
public struct TCP_HDR
{
    public ushort th_sport;
    public ushort th_dport;
    public uint th_seq;
    public uint th_ack;
    public byte _bitfield;
    public byte th_flags;
    public ushort th_win;
    public ushort th_sum;
    public ushort th_urp;
}
Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential, Pack:=1, CharSet:=CharSet.Unicode)>
Public Structure TCP_HDR
    Public th_sport As UShort
    Public th_dport As UShort
    Public th_seq As UInteger
    Public th_ack As UInteger
    Public _bitfield As Byte
    Public th_flags As Byte
    Public th_win As UShort
    Public th_sum As UShort
    Public th_urp As UShort
End Structure
import ctypes
from ctypes import wintypes

class TCP_HDR(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ("th_sport", ctypes.c_ushort),
        ("th_dport", ctypes.c_ushort),
        ("th_seq", wintypes.DWORD),
        ("th_ack", wintypes.DWORD),
        ("_bitfield", ctypes.c_ubyte),
        ("th_flags", ctypes.c_ubyte),
        ("th_win", ctypes.c_ushort),
        ("th_sum", ctypes.c_ushort),
        ("th_urp", ctypes.c_ushort),
    ]
#[repr(C, packed(1))]
pub struct TCP_HDR {
    pub th_sport: u16,
    pub th_dport: u16,
    pub th_seq: u32,
    pub th_ack: u32,
    pub _bitfield: u8,
    pub th_flags: u8,
    pub th_win: u16,
    pub th_sum: u16,
    pub th_urp: u16,
}
import "golang.org/x/sys/windows"

type TCP_HDR struct {
	th_sport uint16
	th_dport uint16
	th_seq uint32
	th_ack uint32
	_bitfield byte
	th_flags byte
	th_win uint16
	th_sum uint16
	th_urp uint16
}
type
  TCP_HDR = packed record
    th_sport: Word;
    th_dport: Word;
    th_seq: DWORD;
    th_ack: DWORD;
    _bitfield: Byte;
    th_flags: Byte;
    th_win: Word;
    th_sum: Word;
    th_urp: Word;
  end;
const TCP_HDR = extern struct {
    th_sport: u16,
    th_dport: u16,
    th_seq: u32,
    th_ack: u32,
    _bitfield: u8,
    th_flags: u8,
    th_win: u16,
    th_sum: u16,
    th_urp: u16,
};
type
  TCP_HDR {.packed.} = object
    th_sport: uint16
    th_dport: uint16
    th_seq: uint32
    th_ack: uint32
    _bitfield: uint8
    th_flags: uint8
    th_win: uint16
    th_sum: uint16
    th_urp: uint16
align(1)
struct TCP_HDR
{
    ushort th_sport;
    ushort th_dport;
    uint th_seq;
    uint th_ack;
    ubyte _bitfield;
    ubyte th_flags;
    ushort th_win;
    ushort th_sum;
    ushort th_urp;
}

HSP用 定義

HSP3.7/3.8 は構造体機能が無いため4byte整数配列(dim)+peek/poke で操作(32/64bitでサイズ・位置が異なる場合はタブで分割)。IronHSP は NSTRUCT(#defstruct/stdim/->)で32/64bit共通。

; HSP3.7/3.8 は構造体機能が無いため、4byte整数の配列変数で操作します。(x64 レイアウト)
; TCP_HDR サイズ: 20 バイト(x64)
dim st, 5    ; 4byte整数×5(構造体サイズ 20 / 4 切り上げ)
; th_sport : WORD (+0, 2byte)  wpoke st,0,値  /  値 = wpeek(st,0)
; th_dport : WORD (+2, 2byte)  wpoke st,2,値  /  値 = wpeek(st,2)
; th_seq : DWORD (+4, 4byte)  st.1 = 値  /  値 = st.1   (lpoke/lpeek も可)
; th_ack : DWORD (+8, 4byte)  st.2 = 値  /  値 = st.2   (lpoke/lpeek も可)
; _bitfield : BYTE (+12, 1byte)  poke st,12,値  /  値 = peek(st,12)
; th_flags : BYTE (+13, 1byte)  poke st,13,値  /  値 = peek(st,13)
; th_win : WORD (+14, 2byte)  wpoke st,14,値  /  値 = wpeek(st,14)
; th_sum : WORD (+16, 2byte)  wpoke st,16,値  /  値 = wpeek(st,16)
; th_urp : WORD (+18, 2byte)  wpoke st,18,値  /  値 = wpeek(st,18)
; ※4byte境界の整数は添字 st.N(N=オフセット/4)で読み書き可。それ以外は peek/poke 系を使用。
; IronHSP は NSTRUCT(構造体)をサポート。32bit/64bit どちらでも同じコードで動作します。
#defstruct global TCP_HDR, pack=1
    #field short th_sport
    #field short th_dport
    #field int th_seq
    #field int th_ack
    #field byte _bitfield
    #field byte th_flags
    #field short th_win
    #field short th_sum
    #field short th_urp
#endstruct

stdim st, TCP_HDR        ; NSTRUCT 変数を確保
st->th_sport = 100
mes "th_sport=" + st->th_sport