Assembly code snippets

Back to the snippets overview

Details

TitleKilling a TCP connection
AuthorThomas
Submitted by:Thomas
Date added:2002-02-17 19:21:58
Date modified:2002-02-17 19:22:39

Comments

This piece of code kills a TCP connection, which does not necessarily have to be owned by your process. It kills the connection by setting the TCP state of it to 'deleted'. Setup a MIB_TCPROW structure which contains the local IP & port as well as the remote IP & port of the connection.
Then with the TcpSetEntry function in the IP Helper API, set the state to MIB_TCP_STATE_DELETE_TCB.

Requirements: [link4:IPADDR and PORTNR macros]

Snippet

requires iphlpapi.lib (download from my site)
iphlpapi may be useful, but necessary includes are
shown below:
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
IF I_DONT_HAVE_IPHLPAPI_INCLUDES

    MIB_TCP_STATE_DELETE_TCB    equ   12

    MIB_TCPROW STRUCT
      dwState           DWORD   ?
      dwLocalAddr       DWORD   ?
      dwLocalPort       DWORD   ?
      dwRemoteAddr      DWORD   ?
      dwRemotePort      DWORD   ?
    MIB_TCPROW ENDS
ENDIF
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TestRow     MIB_TCPROW <MIB_TCP_STATE_DELETE_TCB,
                        IPADDR(127,0,0,1),PORTNR(1572), ;local IP & port number
                        IPADDR(127,0,0,1),PORTNR(9944)>  ;remote IP & port number

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
start:
    invoke  SetTcpEntry, addr TestRow