How can I close an internet connection if I only know the IP?

Hi, Im writing an application on Delphi 7 and I want to close and internet connection knowing only the IP, In other words I need a procedure like this:

CloseConnection('xxx.xxx.xxx.xxx');

Thanks
manganzonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dagan HooverDeveloperCommented:
This was ziolko's so if he see's and posts give him the points(I didn't make this):
unit uIPHelperAPI;
 
interface
 
uses Windows;
 
 
resourcestring
  ResTCPStateClosed = 'Closed';
  ResTCPStateListen = 'Listen';
  ResTCPStateSynSent = 'SynSent';
  ResTCPStateSynRcvd = 'SynRcvd';
  ResTCPStateEst = 'Established';
  ResTCPStateFW1 = 'Fin Wait1';
  ResTCPStateFW2 = 'Fin Wait2';
  ResTCPStateCloseWait = 'Close Wait';
  ResTCPStateClosing = 'Closing';
  ResTCPStateLastAck = 'Last ACK';
  ResTCPStateTimeWait = 'Time Wait';
  ResTCPStateDeleteTCB = 'Delete TCB';
 
const
 
     TCP_STATES: array[1..12] of string = (ResTCPStateClosed,
                                           ResTCPStateListen,
                                           ResTCPStateSynSent,
                                           ResTCPStateSynRcvd,
                                           ResTCPStateEst,
                                           ResTCPStateFW1,
                                           ResTCPStateFW2,
                                           ResTCPStateCloseWait,
                                           ResTCPStateClosing,
                                           ResTCPStateLastAck,
                                           ResTCPStateTimeWait,
                                           ResTCPStateDeleteTCB);
 
type
 
    TCP_TABLE_CLASS = (TCP_TABLE_BASIC_LISTENER, TCP_TABLE_BASIC_CONNECTIONS,
                       TCP_TABLE_BASIC_ALL, TCP_TABLE_OWNER_PID_LISTENER,
                       TCP_TABLE_OWNER_PID_CONNECTIONS, TCP_TABLE_OWNER_PID_ALL,
                       TCP_TABLE_OWNER_MODULE_LISTENER, TCP_TABLE_OWNER_MODULE_CONNECTIONS,
                       TCP_TABLE_OWNER_MODULE_ALL);
 
    UDP_TABLE_CLASS = (UDP_TABLE_BASIC, UDP_TABLE_OWNER_PID, UDP_TABLE_OWNER_MODULE);
 
    PMIB_TCPROW = ^MIB_TCPROW;
    MIB_TCPROW = packed record
      dwState: DWORD;
      dwLocalAddr: DWORD;
      dwLocalPort: DWORD;
      dwRemoteAddr: DWORD;
      dwRemotePort: DWORD;
    end;
 
    PMIB_TCPROW_OWNER_PID = ^MIB_TCPROW_OWNER_PID;
    MIB_TCPROW_OWNER_PID = packed record
      dwState: DWORD;
      dwLocalAddr: DWORD;
      dwLocalPort: DWORD;
      dwRemoteAddr: DWORD;
      dwRemotePort: DWORD;
      dwOwnerPID: DWORD;
    end;
 
    PMIB_UDPROW_OWNER_PID = ^MIB_UDPROW_OWNER_PID;
    MIB_UDPROW_OWNER_PID = packed record
      dwLocalAddr: DWORD;
      dwLocalPort: DWORD;
      dwOwnerPID: DWORD;
    end;
 
    PMIB_TCPTABLE_OWNER_PID = ^MIB_TCPTABLE_OWNER_PID;
    MIB_TCPTABLE_OWNER_PID = packed record
      dwNumEntries: DWORD;
      table: array [0..0] of MIB_TCPROW_OWNER_PID;
    end;
 
    PMIB_UDPTABLE_OWNER_PID = ^MIB_UDPTABLE_OWNER_PID;
    MIB_UDPTABLE_OWNER_PID = packed record
      dwNumEntries: DWORD;
      table: array [0..0] of MIB_UDPROW_OWNER_PID;
    end;
 
    TAllocateAndGetTcpExTableFromStack = function (pTcpTable: PMIB_TCPTABLE_OWNER_PID;bOrder: BOOL;heap: THandle;
                                                   zero: DWORD;flags: DWORD):DWORD;stdcall;
    TAllocateAndGetUdpExTableFromStack = function (pTcpTable: PMIB_TCPTABLE_OWNER_PID;bOrder: BOOL;heap: THandle;
                                                   zero: DWORD;flags: DWORD):DWORD;stdcall;
    TSendTcpEntry = function (pTCPRow: PMIB_TCPROW):DWORD;stdcall;
    TGetExtendedTcpTable = function (pTcpTable: PMIB_TCPTABLE_OWNER_PID;pdwSize: PDWORD;bOrder: BOOL;ulAf: ULONG;
                                     TableClass: TCP_TABLE_CLASS;Reserved: ULONG):DWORD;stdcall;
    TGetExtendedUdpTable = function (pTcpTable: PMIB_UDPTABLE_OWNER_PID;pdwSize: PDWORD;bOrder: BOOL;ulAf: ULONG;
                                     TableClass: UDP_TABLE_CLASS;Reserved: ULONG):DWORD;stdcall;
 
const
  MIB_TCP_STATE_CLOSED = 1;
  MIB_TCP_STATE_LISTEN = 2;
  MIB_TCP_STATE_SYN_SENT = 3;
  MIB_TCP_STATE_SYN_RCVD = 4;
  MIB_TCP_STATE_ESTAB = 5;
  MIB_TCP_STATE_FIN_WAIT1 = 6;
  MIB_TCP_STATE_FIN_WAIT2 = 7;
  MIB_TCP_STATE_CLOSE_WAIT = 8;
  MIB_TCP_STATE_CLOSING = 9;
  MIB_TCP_STATE_LAST_ACK = 10;
  MIB_TCP_STATE_TIME_WAIT = 11;
  MIB_TCP_STATE_DELETE_TCB = 12;
 
var
   IPHelperLoaded: Boolean;
   IPHelperXPLoaded: Boolean;
   IPHelperVistaLoaded: Boolean;
   AllocateAndGetTcpExTableFromStack: TAllocateAndGetTcpExTableFromStack;
   AllocateAndGetUdpExTableFromStack: TAllocateAndGetUdpExTableFromStack;
   SetTcpEntry: TSendTcpEntry;
   GetExtendedTcpTable: TGetExtendedTcpTable;
   GetExtendedUdpTable: TGetExtendedUdpTable;
 
implementation
 
const
     iphelper = 'iphlpapi.dll';
var
   LibHandle: THandle;
   
procedure LoadIPHelper;
begin
  LibHandle := LoadLibrary(iphelper);
  if LibHandle <> INVALID_HANDLE_VALUE then begin
    @AllocateAndGetTcpExTableFromStack := GetProcAddress(LibHandle, 'AllocateAndGetTcpExTableFromStack');
    @AllocateAndGetUdpExTableFromStack := GetProcAddress(LibHandle, 'AllocateAndGetUdpExTableFromStack');
    @SetTcpEntry := GetProcAddress(LibHandle, 'SetTcpEntry');
    @GetExtendedTcpTable := GetProcAddress(LibHandle, 'GetExtendedTcpTable');
    @GetExtendedUdpTable := GetProcAddress(LibHandle, 'GetExtendedUdpTable');
  end;
  IPHelperLoaded := (LibHandle <> INVALID_HANDLE_VALUE) and Assigned(SetTcpEntry);
  IPHelperXPLoaded := IPHelperLoaded and
                      (Assigned(AllocateAndGetTcpExTableFromStack) and Assigned(AllocateAndGetUdpExTableFromStack));
  IPHelperVistaLoaded := IPHelperLoaded and
                         (Assigned(GetExtendedTcpTable) and Assigned(GetExtendedUdpTable));
end;
 
procedure ReleaseIPHelper;
begin
  if LibHandle <> INVALID_HANDLE_VALUE then
    FreeLibrary(LibHandle);
end;
 
initialization
 
  LoadIPHelper;
 
finalization
 
  ReleaseIPHelper;
 
end.

Open in new window

0
Dagan HooverDeveloperCommented:
The above as in a separate unit:
In your form is below:

Usage KillConnection(IPAddress: String);

uses winsock;
 
var
  FBufferTCP: PMIB_TCPTABLE_OWNER_PID;
 
 
function GetIP(AIP: DWORD): WideString;
var bytes: array[0..3] of Byte;
begin
  Move(AIP, bytes[0], SizeOf(AIP));
  Result := IntToStr(bytes[0]) + '.' +
            IntToStr(bytes[1]) + '.' +
            IntToStr(bytes[2]) + '.' +
            IntToStr(bytes[3]);
end;
 
function GetPort(APort: DWORD): DWORD;
begin
  Result := (APort div 256) + (APort mod 256) * 256;
end;
 
procedure ReadTCPTable;
var wsadata: TWSAData;
    ret: DWORD;
    dwSize: DWORD;
begin
  if not IPHelperLoaded then
    Exit;
  WSAStartup(2, wsadata);
  try
    if IPHelperVistaLoaded then begin
      dwSize := 0;
      ret := GetExtendedTcpTable(FBufferTCP, @dwSize, True, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
      if ret = ERROR_INSUFFICIENT_BUFFER then begin
        GetMem(FBufferTCP, dwSize);
        GetExtendedTcpTable(FBufferTCP, @dwSize, True, AF_INET, TCP_TABLE_OWNER_PID_ALL, 0);
      end;
    end else if IPHelperXPLoaded then
      AllocateAndGetTcpExTableFromStack(@FBufferTCP, True, GetProcessHeap, 2, 2);
  finally
    WSACleanup;
  end;
end;
 
procedure KillConnection(IPAddress: String);
var
  item: MIB_TCPROW_OWNER_PID;
  item2: MIB_TCPROW 
  I : Integer;
begin
  ReadTCPTable;
  For I := 0 To FBufferTCP^.dwNumEntries Do
    begin
    item := FBufferTCP^.table[I];
    If GetIP(Item.dwRemoteAddr) = IpAddress
      begin
      item2.dwState := MIB_TCP_STATE_DELETE_TCB;
      item2.dwLocalAddr := item.dwLocalAddr;
      item2.dwLocalPort := item.dwLocalPort;
      item2.dwRemoteAddr := item.dwRemoteAddr;
      item2.dwRemotePort := item.dwRemotePort;
      SetTCPEntry(@item);
      end;
    end;
  end;
end;

Open in new window

0
ziolkoCommented:
heh ThievingSix i was just about post pointer to your Q with this code:)

ziolko.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

ziolkoCommented:
jeeez just realized that I forgot to mention that I use INVALID_HANDLE_VALUE in this code which is not same as the one defined in Windows.pas.
Use 0 instead.


procedure LoadIPHelper;
begin
  LibHandle := LoadLibrary(iphelper);
  if LibHandle <> 0 then begin
    @AllocateAndGetTcpExTableFromStack := GetProcAddress(LibHandle, 'AllocateAndGetTcpExTableFromStack');
    @AllocateAndGetUdpExTableFromStack := GetProcAddress(LibHandle, 'AllocateAndGetUdpExTableFromStack');
    @SetTcpEntry := GetProcAddress(LibHandle, 'SetTcpEntry');
    @GetExtendedTcpTable := GetProcAddress(LibHandle, 'GetExtendedTcpTable');
    @GetExtendedUdpTable := GetProcAddress(LibHandle, 'GetExtendedUdpTable');
  end;
  IPHelperLoaded := (LibHandle <> 0) and Assigned(SetTcpEntry);
  IPHelperXPLoaded := IPHelperLoaded and
                      (Assigned(AllocateAndGetTcpExTableFromStack) and Assigned(AllocateAndGetUdpExTableFromStack));
  IPHelperVistaLoaded := IPHelperLoaded and
                         (Assigned(GetExtendedTcpTable) and Assigned(GetExtendedUdpTable));
end;

procedure ReleaseIPHelper;
begin
  if LibHandle <> 0 then
    FreeLibrary(LibHandle);
end;


ziolko.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
manganzonAuthor Commented:
Hi, thanks for ur answers but I got a problem, when I try to kill the connection, this is still working, I tried killing eMule connection (77.247.178.244) but nothing happen :(
0
ziolkoCommented:
two possible causes:
1. SetTCPEntry() failed, you can get error code like this:
   ret := SetTCPEntry();
   if ret <> NO_ERROR then
     ShowMEssage(SysErrorMessage(ret))

2. eMule reestablished connection once it was closed in this case... well get good firewall:)

ziolko.
0
manganzonAuthor Commented:
Hi agian, I found the problem, the procedure is:

procedure KillConnection(IPAddress: String);
var
  item: MIB_TCPROW_OWNER_PID;
  item2: MIB_TCPROW
  I : Integer;
begin
  ReadTCPTable;
  For I := 0 To FBufferTCP^.dwNumEntries Do
    begin
    item := FBufferTCP^.table[I];
    If GetIP(Item.dwRemoteAddr) = IpAddress
      begin
      item2.dwState := MIB_TCP_STATE_DELETE_TCB;
      item2.dwLocalAddr := item.dwLocalAddr;
      item2.dwLocalPort := item.dwLocalPort;
      item2.dwRemoteAddr := item.dwRemoteAddr;
      item2.dwRemotePort := item.dwRemotePort;
      SetTCPEntry(@item);
      end;
    end;
  end;

but I have changed SetTCPEntry(@item) for SetTCPEntry(@item2) and now is working :)) so as ThievingSix told me, I will give points to ziolko.

Thank you very much =D
0
ziolkoCommented:
ur welcome:)

ziolko.
0
Dagan HooverDeveloperCommented:
Whooops, my bad on the item, item2. Had to switch the two from my source too yours. =P
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.