Page 1 of 1

TCP & UDP connection table

Posted: Sat Sep 02, 2006 12:24 pm
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

Posted: Sat Sep 02, 2006 2:19 pm
by John Bedlam
Good code.

long2ip(longIP.l) does the same thing as IPString(IPAdress) in the network library. ;)

Posted: Sun Sep 03, 2006 3:49 am
by Heathen
Do you know how to force disconnect a connection and/or block ports?

Posted: Sat Sep 09, 2006 7:07 pm
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.