TCP & UDP connection table

Share your advanced PureBasic knowledge/code with the community.
peterb
User
User
Posts: 60
Joined: Sun Oct 02, 2005 8:55 am
Location: Czech Republic
Contact:

TCP & UDP connection table

Post by peterb »

Code updated For 5.20+

progress for display of all open TCP and UDP endpoints.

Code: Select all

Dim TCPstate.s(15)

For k = 1 To 15
  TCPstate(k) = StringField("Closed|Listening|SYN Sent|SYN Received|Established|Waiting For FIN|Waiting For FIN|Waiting For Close|Closing|Last ACK|Time Wait|TCB deleted", k, "|")
Next

Structure MIB_TCPROW
  dwStats.l
  dwLocalAddr.l
  dwLocalPort.l
  dwRemoteAddr.l
  dwRemotePort.l
EndStructure

Structure MIB_TCPTABLE
  dwNumEntries.l
  table.MIB_TCPROW[2048]
EndStructure


Structure MIB_UDPROW
  dwLocalAddr.l
  dwLocalPort.l
EndStructure

Structure MIB_UDPTABLE
  dwNumEntries.l
  table.MIB_UDPROW[2048]
EndStructure


Procedure.s long2ip(longIP.l)
  
  fourth.l  = longIP / 16777216
  longIP    = longIP % 16777216
  third.l   = longIP / 65536
  longIP    = longIP % 65536
  second.l  = longIP / 256
  first.l   = longIP % 256
  
  ProcedureReturn(Str(first) + "." + Str(second) + "." + Str(third) + "." + Str(fourth))

EndProcedure


dwSize = $0
If GetTcpTable_(@tcpTable.MIB_TCPTABLE, @dwSize, #True)
  If GetTcpTable_(@tcpTable.MIB_TCPTABLE, @dwSize, #True) = #NO_ERROR
        
    For cnt = 0 To tcpTable\dwNumEntries - 1
      Debug("type: TCP")
      Debug("state: " + TCPstate (tcpTable\table[cnt]\dwStats))
      Debug("local IP: "    + long2ip(tcpTable\table[cnt]\dwLocalAddr))
      Debug("local port: "  + Str(htons_(tcpTable\table[cnt]\dwLocalPort)))
      Debug("remote IP: "   + long2ip(tcpTable\table[cnt]\dwRemoteAddr))
      Debug("remote port: " + Str(htons_(tcpTable\table[cnt]\dwRemotePort)))
      Debug("")
    Next
      
  EndIf
EndIf
      
dwSize = $0
If GetUdpTable_(@udpTable.MIB_UDPTABLE, @dwSize, #True)
  If GetUdpTable_(@udpTable.MIB_UDPTABLE, @dwSize, #True) = #NO_ERROR
        
    For cnt = 0 To udpTable\dwNumEntries - 1
      Debug("type UDP")
      Debug("local IP: " + long2ip (udpTable\table[cnt]\dwLocalAddr))
      Debug("local port: " + Str(htons_  (udpTable\table[cnt]\dwLocalPort)))
      Debug("")
    Next

  EndIf
EndIf


End
John Bedlam
User
User
Posts: 11
Joined: Thu Aug 31, 2006 2:29 pm

Post by John Bedlam »

Good code.

long2ip(longIP.l) does the same thing as IPString(IPAdress) in the network library. ;)
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

Do you know how to force disconnect a connection and/or block ports?
I love Purebasic.
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

Heathen wrote:Do you know how to force disconnect a connection and/or block ports?
Use something like this to kill an established connection :

Code: Select all

TcpEntry.MIB_TCPROW
TcpEntry\dwState 		= #MIB_TCP_STATE_DELETE_TCB
TcpEntry\dwLocalAddr 	= inet_addr_(YOURADDR)
TcpEntry\dwLocalPort 	= htons_(YOURPORT)
TcpEntry\dwRemoteAddr 	= inet_addr_(OTHERADDR)
TcpEntry\dwRemotePort 	= htons_(OTHERPORT)

SetTcpEntry_(@TcpEntry)   
Of course you have to replace the variables with the addresses and ports of the connection you want to kill.
- Registered PB user -

Using PB 4.00
Post Reply