NetStat

Share your advanced PureBasic knowledge/code with the community.
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

NetStat

Post by fweil »

Code updated for 5.20+

First as a reply to GeoTrail I made this translation from a VB code found through Google.

It seems that no former post correspond to such net statistics in PureBasic.

API called functions are not necessarily well documented in WinAPI help and other material.

Windows only !

Code: Select all

Enumeration
  #Window_Main
  #Gadget_Editor
EndEnumeration

#MIB_TCP_STATE_CLOSED = 0
#MIB_TCP_STATE_LISTEN = 1
#MIB_TCP_STATE_SYN_SENT = 2
#MIB_TCP_STATE_SYN_RCVD = 3
#MIB_TCP_STATE_ESTAB = 4
#MIB_TCP_STATE_FIN_WAIT1 = 5
#MIB_TCP_STATE_FIN_WAIT2 = 6
#MIB_TCP_STATE_CLOSE_WAIT = 7
#MIB_TCP_STATE_CLOSING = 8
#MIB_TCP_STATE_LAST_ACK = 9
#MIB_TCP_STATE_TIME_WAIT = 10
#MIB_TCP_STATE_DELETE_TCB = 11

Structure MIB_IPSTATS
  dwForwarding.l ; IP forwarding enabled Or disabled
  dwDefaultTTL.l ; Default time-to-live
  dwInReceives.l ; datagrams received
  dwInHdrErrors.l ; received header errors
  dwInAddrErrors.l ; received address errors
  dwForwDatagrams.l ; datagrams forwarded
  dwInUnknownProtos.l ; datagrams with unknown protocol
  dwInDiscards.l ; received datagrams discarded
  dwInDelivers.l ; received datagrams delivered
  dwOutRequests.l
  dwRoutingDiscards.l
  dwOutDiscards.l ; sent datagrams discarded
  dwOutNoRoutes.l ; datagrams For which no route
  dwReasmTimeout.l ; datagrams For which all frags didn't arrive
  dwReasmReqds.l ; datagrams requiring reassembly
  dwReasmOks.l ; successful reassemblies
  dwReasmFails.l ; failed reassemblies
  dwFragOks.l ; successful fragmentations
  dwFragFails.l ; failed fragmentations
  dwFragCreates.l ; datagrams fragmented
  dwNumIf.l ; number of interfaces on computer
  dwNumAddr.l ; number of IP address on computer
  dwNumRoutes.l ; number of routes in routing table
EndStructure

Structure MIB_TCPSTATS
  dwRtoAlgorithm.l ; timeout algorithm
  dwRtoMin.l ; minimum timeout
  dwRtoMax.l ; maximum timeout
  dwMaxConn.l ; maximum connections
  dwActiveOpens.l ; active opens
  dwPassiveOpens.l ; passive opens
  dwAttemptFails.l ; failed attempts
  dwEstabResets.l ; establised connections reset
  dwCurrEstab.l ; established connections
  dwInSegs.l ; segments received
  dwOutSegs.l ; segment sent
  dwRetransSegs.l ; segments retransmitted
  dwInErrs.l ; incoming errors
  dwOutRsts.l ; outgoing resets
  dwNumConns.l ; cumulative connections
EndStructure

Structure MIB_UDPSTATS
  dwInDatagrams.l ; received datagrams
  dwNoPorts.l ; datagrams For which no port
  dwInErrors.l ; errors on received datagrams
  dwOutDatagrams.l ; sent datagrams
  dwNumAddrs.l ; number of entries in UDP listener table
EndStructure

Structure MIBICMPSTATS
  dwMsgs.l ; number of messages
  dwErrors.l ; number of errors
  dwDestUnreachs.l ; destination unreachable messages
  dwTimeExcds.l ; time-to-live exceeded messages
  dwParmProbs.l ; parameter problem messages
  dwSrcQuenchs.l ; source quench messages
  dwRedirects.l ; redirection messages
  dwEchos.l ; echo requests
  dwEchoReps.l ; echo replies
  dwTimestamps.l ; timestamp requests
  dwTimestampReps.l ; timestamp replies
  dwAddrMasks.l ; address mask requests
  dwAddrMaskReps.l ; address mask replies
EndStructure

Structure MIBICMPINFO
  icmpInStats.MIBICMPSTATS ; stats For incoming messages
  icmpOutStats.MIBICMPSTATS ; stats For outgoing messages
EndStructure

Global ip.MIB_IPSTATS
Global tcp.MIB_TCPSTATS
Global udp.MIB_UDPSTATS
Global icmp.MIBICMPINFO

Global EOL.s, TAB.s
EOL = Chr(13) + Chr(10)
TAB = Chr(9)

Procedure.s Stats()
  txtOutput.s = ""
  If GetIpStatistics_(ip)
    txtOutput + "Unable to retrieve IP Statistics"
  Else
    txtOutput + "IP Statistics" + EOL + "==============================" + EOL
    txtOutput + "IP forwarding enabled or disabled:" + TAB + Str(ip\dwForwarding) + EOL
    txtOutput + "default time-to-live:" + TAB + Str(ip\dwDefaultTTL) + EOL
    txtOutput + "datagrams received:" + TAB + Str(ip\dwInReceives) + EOL
    txtOutput + "received header errors:" + TAB + Str(ip\dwInHdrErrors) + EOL
    txtOutput + "received address errors:" + TAB + Str(ip\dwInAddrErrors) + EOL
    txtOutput + "datagrams forwarded:" + TAB + Str(ip\dwForwDatagrams) + EOL
    txtOutput + "datagrams with unknown protocol:" + TAB + Str(ip\dwInUnknownProtos) + EOL
    txtOutput + "received datagrams discarded:" + TAB + Str(ip\dwInDiscards) + EOL
    txtOutput + "received datagrams delivered:" + TAB + Str(ip\dwInDelivers) + EOL
    txtOutput + "outgoing datagrams requested:" + TAB + Str(ip\dwOutRequests) + EOL
    txtOutput + "outgoing datagrams discarded:" + TAB + Str(ip\dwRoutingDiscards) + EOL
    txtOutput + "sent datagrams discarded:" + TAB + Str(ip\dwOutDiscards) + EOL
    txtOutput + "datagrams for which no route:" + TAB + Str(ip\dwOutNoRoutes) + EOL
    txtOutput + "datagrams for which all frags didn't arrive:" + TAB + Str(ip\dwReasmTimeout) + EOL
    txtOutput + "datagrams requiring reassembly:" + TAB + Str(ip\dwReasmReqds) + EOL
    txtOutput + "successful reassemblies:" + TAB + Str(ip\dwReasmOks) + EOL
    txtOutput + "failed reassemblies:" + TAB + Str(ip\dwReasmFails) + EOL
    txtOutput + "successful fragmentations:" + TAB + Str(ip\dwFragOks) + EOL
    txtOutput + "failed fragmentations:" + TAB + Str(ip\dwFragFails) + EOL
    txtOutput + "datagrams fragmented:" + TAB + Str(ip\dwFragCreates) + EOL
    txtOutput + "number of interfaces on computer:" + TAB + Str(ip\dwNumIf) + EOL
    txtOutput + "number of IP address on computer:" + TAB + Str(ip\dwNumAddr) + EOL
    txtOutput + "number of routes in routing table:" + TAB + Str(ip\dwNumRoutes) + EOL
    txtOutput + EOL
  EndIf
  
  If GetTcpStatistics_(tcp)
    txtOutput + "Unable to retrieve TCP Statistics"
  Else
    txtOutput + "TCP Statistics" + EOL + "==============================" + EOL
    txtOutput + "timeout algorithm:" + TAB + Str(tcp\dwRtoAlgorithm) + EOL
    txtOutput + "minimum timeout:" + TAB + Str(tcp\dwRtoMin) + EOL
    txtOutput + "maximum timeout:" + TAB + Str(tcp\dwRtoMax) + EOL
    txtOutput + "maximum connections:" + TAB + Str(tcp\dwMaxConn) + EOL
    txtOutput + "active opens:" + TAB + Str(tcp\dwActiveOpens) + EOL
    txtOutput + "passive opens:" + TAB + Str(tcp\dwPassiveOpens) + EOL
    txtOutput + "failed attempts:" + TAB + Str(tcp\dwAttemptFails) + EOL
    txtOutput + "establised connections reset:" + TAB + Str(tcp\dwEstabResets) + EOL
    txtOutput + "established connections:" + TAB + Str(tcp\dwCurrEstab) + EOL
    txtOutput + "segments received:" + TAB + Str(tcp\dwInSegs) + EOL
    txtOutput + "segment sent:" + TAB + Str(tcp\dwOutSegs) + EOL
    txtOutput + "segments retransmitted:" + TAB + Str(tcp\dwRetransSegs) + EOL
    txtOutput + "incoming errors:" + TAB + Str(tcp\dwInErrs) + EOL
    txtOutput + "outgoing resets:" + TAB + Str(tcp\dwOutRsts) + EOL
    txtOutput + "cumulative connections:" + TAB + Str(tcp\dwNumConns) + EOL
    txtOutput + EOL
  EndIf
  
  If GetUdpStatistics_(udp)
    txtOutput + "Unable to retrieve UDP Statistics"
  Else
    txtOutput + "UDP Statistics" + EOL + "==============================" + EOL
    txtOutput + "received datagrams:" + TAB + Str(udp\dwInDatagrams) + EOL
    txtOutput + "datagrams for which no port:" + TAB + Str(udp\dwNoPorts) + EOL
    txtOutput + "errors on received datagrams:" + TAB + Str(udp\dwInErrors) + EOL
    txtOutput + "sent datagrams:" + TAB + Str(udp\dwOutDatagrams) + EOL
    txtOutput + "number of entries in UDP listener table:" + TAB + Str(udp\dwNumAddrs) + EOL
    txtOutput + EOL
  EndIf
  
  If GetIcmpStatistics_(icmp)
    txtOutput + "Unable to retrieve ICMP Statistics"
  Else
    txtOutput + "ICMP Statistics" + EOL + "==============================" + EOL
    txtOutput + "*****  In  *****" + EOL
    txtOutput + "number of messages:" + TAB + Str(icmp\icmpInStats\dwMsgs) + EOL
    txtOutput + "number of errors:" + TAB + Str(icmp\icmpInStats\dwErrors) + EOL
    txtOutput + "destination unreachable messages:" + TAB + Str(icmp\icmpInStats\dwDestUnreachs) + EOL
    txtOutput + "time-to-live exceeded messages:" + TAB + Str(icmp\icmpInStats\dwTimeExcds) + EOL
    txtOutput + "parameter problem messages:" + TAB + Str(icmp\icmpInStats\dwParmProbs) + EOL
    txtOutput + "source quench messages:" + TAB + Str(icmp\icmpInStats\dwSrcQuenchs) + EOL
    txtOutput + "redirection messages:" + TAB + Str(icmp\icmpInStats\dwRedirects) + EOL
    txtOutput + "echo requests:" + TAB + Str(icmp\icmpInStats\dwEchos) + EOL
    txtOutput + "echo replies:" + TAB + Str(icmp\icmpInStats\dwEchoReps) + EOL
    txtOutput + "timestamp requests:" + TAB + Str(icmp\icmpInStats\dwTimestamps) + EOL
    txtOutput + "timestamp replies:" + TAB + Str(icmp\icmpInStats\dwTimestampReps) + EOL
    txtOutput + "address mask requests:" + TAB + Str(icmp\icmpInStats\dwAddrMasks) + EOL
    txtOutput + "address mask replies:" + TAB + Str(icmp\icmpInStats\dwAddrMaskReps) + EOL
    txtOutput + EOL
    txtOutput + "*****  Out  *****" + EOL
    txtOutput + "number of messages:" + TAB + Str(icmp\icmpOutStats\dwMsgs) + EOL
    txtOutput + "number of errors:" + TAB + Str(icmp\icmpOutStats\dwErrors) + EOL
    txtOutput + "destination unreachable messages:" + TAB + Str(icmp\icmpOutStats\dwDestUnreachs) + EOL
    txtOutput + "time-to-live exceeded messages:" + TAB + Str(icmp\icmpOutStats\dwTimeExcds) + EOL
    txtOutput + "parameter problem messages:" + TAB + Str(icmp\icmpOutStats\dwParmProbs) + EOL
    txtOutput + "source quench messages:" + TAB + Str(icmp\icmpOutStats\dwSrcQuenchs) + EOL
    txtOutput + "redirection messages:" + TAB + Str(icmp\icmpOutStats\dwRedirects) + EOL
    txtOutput + "echo requests:" + TAB + Str(icmp\icmpOutStats\dwEchos) + EOL
    txtOutput + "echo replies:" + TAB + Str(icmp\icmpOutStats\dwEchoReps) + EOL
    txtOutput + "timestamp requests:" + TAB + Str(icmp\icmpOutStats\dwTimestamps) + EOL
    txtOutput + "timestamp replies:" + TAB + Str(icmp\icmpOutStats\dwTimestampReps) + EOL
    txtOutput + "address mask requests:" + TAB + Str(icmp\icmpOutStats\dwAddrMasks) + EOL
    txtOutput + "address mask replies:" + TAB + Str(icmp\icmpOutStats\dwAddrMaskReps) + EOL
    txtOutput + EOL
  EndIf
  ProcedureReturn txtOutput
EndProcedure

WindowXSize = 640
WindowYSize = 480
Quit = #False
If OpenWindow(#Window_Main, 0, 0, WindowXSize, WindowYSize, "NetStats", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
  AddKeyboardShortcut(#Window_Main, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
  EditorGadget (#Gadget_Editor, 10, 10, WindowXSize - 20, WindowYSize - 20, #PB_Container_Raised)
  SetGadgetText(#Gadget_Editor, Stats())
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = #True
      Case #PB_Event_Menu
        Select EventMenu()
          Case #PB_Shortcut_Escape
            Quit = #True
        EndSelect
    EndSelect
  Until Quit
  CloseWindow(#Window_Main)
EndIf
End
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Wow, thanks fweil.
Looks very impressive, and it runs without any errors too.

Just got to get some time now so I can study it more closely :)

Thanks again.

BTW, have been barbequing all day today, and will tomorrow aswell. And alittle beer too offcourse ;)

Cheers
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

a pitty that I decided to live west-southern France, cause a BBQ in Bergen would have been a nice trick ...
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
GeoTrail
Addict
Addict
Posts: 2794
Joined: Fri Feb 13, 2004 12:45 am
Location: Bergen, Norway
Contact:

Post by GeoTrail »

Well, southern france is nice too :)
I Stepped On A Cornflake!!! Now I'm A Cereal Killer!
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Yes it is, indeed. That's why I live there ...

After having travelled so much I decided to install myself in Bayonne some years ago, and this will be my home base forever.
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post by TerryHough »

@fweil

Thanks, this should be very useful

Keep these good ideas coming. :)

Terry
Post Reply