2006/10/02(Mon)mod_netstat.hsp

はてブ数 2006/10/02 0:44 プログラミング::HSP3 つーさ

netstatみたいなモジュール。

netstat っても、コネクションの状態を表示するだけで、
統計情報とかルーティングテーブルとかは知らんけど。

IPアドレスからホスト名を引く逆引きモジュも付けた。
一緒に入れとくと、ホスト名を引けます。っつーかこっちの方が利用価値が高そう。
mod_dnsptr.hspの方はwin2000以降の対応。
使い方はサンプル参照。

#addition 初めて使った。
ところで "addition" って名詞なんですけど。
なんで #add とか #addtional やないねん。(どうでもいい

ヴァー 凝りすぎて時間無駄にしたー><;
なーにしてんだろなーω と、ちょっと悲しくなったり。

/* mod_gethostbyaddr.hsp */
// DNS逆引きモジュール
#ifndef GetHostByAddr
#module mod_gethostbyaddr
  #uselib "wsock32.dll"
    #cfunc inet_addr "inet_addr" sptr
    #func _WSAStartup "WSAStartup" int,int
    #func _WSACleanup "WSACleanup"
    #func _gethostbyaddr "gethostbyaddr" int,int,int
  #deffunc init@mod_gethostbyaddr
    dim wsadata,100: _WSAStartup 2,varptr(wsadata) : return
  #deffunc deinit@mod_gethostbyaddr onexit
    _WSACleanup return
  #defcfunc GetHostByAddr str ip
    addr = inet_addr(ip)
    _gethostbyaddr varptr(addr), 4, 2
    if stat = 0 : return ""
    dupptr p,stat,4
    return strf("%s",p)
#global
    init@mod_gethostbyaddr
#endif
/* mod_netstat.hsp */
// netstat のようなモジュール
#addition "gethostbyaddr.hsp"

#ifndef mod_netstat
#module mod_netstat
  #uselib "iphlpapi.dll"
    #func _GetTcpTable "GetTcpTable" int,int,int
    #func _GetUdpTable "GetUdpTable" int,int,int
    #define ctype inet_ntoa(%1) (""+((%1)>> 0&$FF)+"."+((%1)>> 8&$FF)+"."+((%1)>>16&$FF)+"."+((%1)>>24&$FF) )
    #define ctype htons(%1) ( (%1)>>8&$FF | ((%1)&$FF)<<8 )

  #define global ctype TCP_NUMENTRIES(%1) (%1(0))
  #define global ctype TCP_STATE(%1,%2) StateStrings@mod_netstat( %1(%2*5+1) )
  #define global ctype TCP_LOCALIP(%1,%2) inet_ntoa@mod_netstat( %1(%2*5+2) )
  #define global ctype TCP_LOCALPORT(%1,%2) htons@mod_netstat( %1(%2*5+3) )
  #define global ctype TCP_REMORTEIP(%1,%2) inet_ntoa@mod_netstat( %1(%2*5+4) )
  #define global ctype TCP_REMORTEPORT(%1,%2) htons@mod_netstat( %1(%2*5+5) )
  #define global ctype UDP_NUMENTRIES(%1) (%1(0))
  #define global ctype UDP_LOCALIP(%1,%2) inet_ntoa@mod_netstat( %1(%2*2+1) )
  #define global ctype UDP_LOCALPORT(%1,%2) htons@mod_netstat( %1(%2*2+2) )
  #define global ctype UDP_STATE(%1,%2) StateStrings@mod_netstat(2)
  #ifdef GetHostByAddr
  #define global ctype TCP_LOCALHOST(%1,%2) GetHostByAddr(TCP_LOCALIP(%1,%2))
  #define global ctype TCP_REMORTEHOST(%1,%2) GetHostByAddr(TCP_REMORTEIP(%1,%2))
  #define global ctype UDP_LOCALHOST(%1,%2) GetHostByAddr(UDP_LOCALIP(%1,%2))
  #endif

  #deffunc _init_mod_netstat_@mod_netstat
    StateStrings   = "","CLOSED","LISTENING","SYN_SENT","SYN_RECEIVED"
    StateStrings.5 = "ESTABLISHED","FIN_WAIT_1","FIN_WAIT_2","CLOSE_WAIT","CLOSING"
    StateStrings.10= "LAST_ACK","TIME_WAIT"
    return

  #deffunc GetTCPTable array TcpTable
    dwSize = 0
    _GetTcpTable 0, varptr(dwSize), 1
    dim TcpTable, dwSize/4+1
    _GetTcpTable varptr(TcpTable), varptr(dwSize), 1
    return TcpTable.0
    
  #deffunc GetUDPTable array UdpTable
    dwSize = 0
    _GetUdpTable 0, varptr(dwSize), 1
    dim UdpTable, dwSize/4+1
    _GetUdpTable varptr(UdpTable), varptr(dwSize), 1
    return UdpTable.0
#global
_init_mod_netstat_@mod_netstat
#endif

//// sample
#if 0
    sdim buf,32768

    GetTCPTable TcpTable
    repeat TCP_NUMENTRIES(TcpTable)
        buf += "TCP: "+ TCP_LOCALIP(TcpTable, cnt)   +":"+ TCP_LOCALPORT(TcpTable, cnt)
;       buf += "TCP: "+ TCP_LOCALHOST(TcpTable, cnt) +":"+ TCP_LOCALPORT(TcpTable, cnt)
        buf += " ⇔ " + TCP_REMORTEIP(TcpTable, cnt)   +":"+ TCP_REMORTEPORT(TcpTable, cnt)
;       buf += " ⇔ " + TCP_REMORTEHOST(TcpTable, cnt) +":"+ TCP_REMORTEPORT(TcpTable, cnt)
        buf += " ["   + TCP_STATE(TcpTable, cnt) +"]\\n"
    loop

    GetUDPTable UdpTable
    repeat UDP_NUMENTRIES(UdpTable)
        buf += "UDP: "+ UDP_LOCALIP(UdpTable, cnt)   +":"+ UDP_LOCALPORT(UdpTable, cnt)
;       buf += "UDP: "+ UDP_LOCALHOST(UdpTable, cnt) +":"+ UDP_LOCALPORT(UdpTable, cnt)
        buf += " ["   + UDP_STATE(UdpTable, cnt)     + "]\\n"
    loop

    mesbox buf,640,480
#endif
/* mod_dnsptr.hsp */
// DNS逆引きモジュール
#ifndef GetHostByAddr
#module mod_dns_ptr
  #uselib "wsock32.dll"
    #cfunc inet_addr "inet_addr" sptr
  #uselib "dnsapi.dll"
    #func DnsQuery_A "DnsQuery_A" sptr,int,nullptr,nullptr,int,nullptr

  #defcfunc GetHostByAddr str _ip, local ip
    ip = inet_addr(_ip)
    ip = ""+(ip>>24&$FF)+"."+(ip>>16&$FF)+"."+(ip>>8&$FF)+"."+(ip>>0&$FF)
    DnsQuery_A ip+".IN-ADDR.ARPA",12, varptr(pQueryResultsSet)
    if stat : return _ip
    dupptr p,pQueryResultsSet+24,4 : return strf("%s",p)
#global
#endif