Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello everyone!

My purpose is to kill a listening local port in window. I've succeeded to kill an established local port but haven't succeeded in killing a listening local port like port 80. I wanna that during my program still working the port 80 is killed so that we can't connect to the internet.
To do this first I use function GetTcpTable to retrieve the information about all connections. Then the user input the port (variable named port) that he wants to kill. We loop through the table of connections again using GetTcpTable then for each loop we compare the feild dwLocalPort to port. If they're the same we do like this (using function setTcpEntry)
C++
MIB_TCPROW sKillConn;
				
sKillConn.dwLocalAddr = pTcpTable2->table[j].dwLocalAddr;	
sKillConn.dwLocalPort = pTcpTable2->table[j].dwLocalPort; 
				
sKillConn.dwRemoteAddr = pTcpTable2->table[j].dwRemoteAddr;
sKillConn.dwRemotePort = pTcpTable2->table[j].dwRemotePort;
sKillConn.dwState = MIB_TCP_STATE_DELETE_TCB;
DWORD dwRez = SetTcpEntry(&sKillConn);
if(dwRez != NO_ERROR)
{
	dwRez = ::GetLastError();
	printf("\nError closing connection;\n");
}
else
	printf("\nConnection closed succesfully!\n");



Here is my attempt:

C++
// Need to link with Iphlpapi.lib and Ws2_32.lib
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

/* Note: could also use malloc() and free() */

int main()
{

    // Declare and initialize variables
    PMIB_TCPTABLE pTcpTable;
    DWORD dwSize = 0;
    DWORD dwRetVal = 0;

    char szLocalAddr[128];
    char szRemoteAddr[128];

    struct in_addr IpAddr;

    int i;

    pTcpTable = (MIB_TCPTABLE *) MALLOC(sizeof (MIB_TCPTABLE));
    if (pTcpTable == NULL) {
        printf("Error allocating memory\n");
        return 1;
    }

    dwSize = sizeof (MIB_TCPTABLE);
	// Make an initial call to GetTcpTable to
	// get the necessary size into the dwSize variable
    if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) ==
        ERROR_INSUFFICIENT_BUFFER) {
        FREE(pTcpTable);
        pTcpTable = (MIB_TCPTABLE *) MALLOC(dwSize);
        if (pTcpTable == NULL) {
            printf("Error allocating memory\n");
            return 1;
        }
    }
		
	// Make a second call to GetTcpTable to get
	// the actual data we require
    if ((dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE)) == NO_ERROR) {
        printf("Number of entries: %d\n", (int) pTcpTable->dwNumEntries);
		printf("\nState \t\tLocal Addr \tLocal Port \tRemote Addr \tRemote Port\n");
        for (i = 0; i < (int) pTcpTable->dwNumEntries; i++) {
            IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwLocalAddr;
            strcpy_s(szLocalAddr, sizeof (szLocalAddr), inet_ntoa(IpAddr));
            IpAddr.S_un.S_addr = (u_long) pTcpTable->table[i].dwRemoteAddr;
            strcpy_s(szRemoteAddr, sizeof (szRemoteAddr), inet_ntoa(IpAddr));

           
            switch (pTcpTable->table[i].dwState) {
            case MIB_TCP_STATE_CLOSED:
                printf("CLOSED");
                break;
            case MIB_TCP_STATE_LISTEN:
                printf("LISTEN");
                break;
            case MIB_TCP_STATE_SYN_SENT:
                printf("SYN-SEND");
                break;
            case MIB_TCP_STATE_SYN_RCVD:
                printf("SYN-RECEIVE");
                break;
            case MIB_TCP_STATE_ESTAB:
                printf("ESTAB");
                break;
            case MIB_TCP_STATE_FIN_WAIT1:
                printf("FIN-WAIT-1");
                break;
            case MIB_TCP_STATE_FIN_WAIT2:
                printf("FIN-WAIT-2");
                break;
            case MIB_TCP_STATE_CLOSE_WAIT:
                printf("CLOSE-WAIT");
                break;
            case MIB_TCP_STATE_CLOSING:
                printf("CLOSING");
                break;
            case MIB_TCP_STATE_LAST_ACK:
                printf("LAST-ACK");
                break;
            case MIB_TCP_STATE_TIME_WAIT:
                printf("WAIT");
                break;
            case MIB_TCP_STATE_DELETE_TCB:
                printf("DELETE-TCB");
                break;
            default:
                printf("UNKNOWN dwState value");
                break;
            }
            
			printf("\t\t%s\t",  szLocalAddr);
            printf("\t%d\t\t", 
                   ntohs((u_short)pTcpTable->table[i].dwLocalPort));
            printf("%s\t\t",  szRemoteAddr);
            printf("\t%d\n", 
                   ntohs((u_short)pTcpTable->table[i].dwRemotePort));
        }
    } else {
        printf("\tGetTcpTable failed with %d\n", dwRetVal);
        FREE(pTcpTable);
        return 1;
    }
	
	 if (pTcpTable != NULL) {
        FREE(pTcpTable);
        pTcpTable = NULL;
    } 
	// Declare and initialize variables
    PMIB_TCPTABLE pTcpTable2;
    DWORD dwSize2 = 0;
    DWORD dwRetVal2 = 0;

    char szLocalAddr2[128];
    char szRemoteAddr2[128];

    struct in_addr IpAddr2;

    int j;

    pTcpTable2 = (MIB_TCPTABLE *) MALLOC(sizeof (MIB_TCPTABLE));
    if (pTcpTable2 == NULL) {
        printf("Error allocating memory\n");
        return 1;
    }

    dwSize2 = sizeof (MIB_TCPTABLE);
	// Make an initial call to GetTcpTable to
	// get the necessary size into the dwSize variable
    if ((dwRetVal = GetTcpTable(pTcpTable2, &dwSize2, TRUE)) ==
        ERROR_INSUFFICIENT_BUFFER) {
        FREE(pTcpTable2);
        pTcpTable2 = (MIB_TCPTABLE *) MALLOC(dwSize2);
        if (pTcpTable2 == NULL) {
            printf("Error allocating memory\n");
            return 1;
        }
    }

    
	cout << endl;
	u_short port;
	cout << "Enter port number you wanna stop: ";
	cin >> port;
	cin.get();
	if ((dwRetVal2 = GetTcpTable(pTcpTable2, &dwSize2, TRUE)) == NO_ERROR) {        
			u_long ulLocalAddress;	
			u_short usLocalPort;	
			u_long ulRemoteAddress;	
			u_short usRemotePort;	

        for (j = 0; j < (int) pTcpTable2->dwNumEntries; j++) {
            
            if(port == ntohs((u_short)pTcpTable2->table[j].dwLocalPort))
			{
								
				//cout << "\nfound port!" << endl;
								
				printf("\nState \t\tLocal Addr \t\tLocal Port \tRemote Addr \tRemote Port\n");        
				IpAddr2.S_un.S_addr = (u_long) pTcpTable2->table[j].dwLocalAddr;
				strcpy_s(szLocalAddr2, sizeof (szLocalAddr2), inet_ntoa(IpAddr2));
				IpAddr2.S_un.S_addr = (u_long) pTcpTable2->table[j].dwRemoteAddr;
				strcpy_s(szRemoteAddr2, sizeof (szRemoteAddr2), inet_ntoa(IpAddr2));

                switch (pTcpTable2->table[j].dwState) {
				case MIB_TCP_STATE_CLOSED:
					printf("CLOSED");
					break;
				case MIB_TCP_STATE_LISTEN:
					printf("LISTEN");
					break;
				case MIB_TCP_STATE_SYN_SENT:
					printf("SYN-SEND");
					break;
				case MIB_TCP_STATE_SYN_RCVD:
					printf("SYN-RECEIVE");
					break;
				case MIB_TCP_STATE_ESTAB:
					printf("ESTAB");
					break;
				case MIB_TCP_STATE_FIN_WAIT1:
					printf("FIN-WAIT-1");
					break;
				case MIB_TCP_STATE_FIN_WAIT2:
					printf("FIN-WAIT-2");
					break;
				case MIB_TCP_STATE_CLOSE_WAIT:
					printf("CLOSE-WAIT");
					break;
				case MIB_TCP_STATE_CLOSING:
					printf("CLOSING");
					break;
				case MIB_TCP_STATE_LAST_ACK:
					printf("LAST-ACK");
					break;
				case MIB_TCP_STATE_TIME_WAIT:
					printf("WAIT");
					break;
				case MIB_TCP_STATE_DELETE_TCB:
					printf("DELETED");
					break;
				default:
					printf("UNKNOWN dwState value");
					break;
				}
            
				printf("\t\t%s\t",  szLocalAddr2);
				printf("\t\t%d\t\t", 
					   ntohs((u_short)pTcpTable2->table[j].dwLocalPort));
				printf("%s\t\t",  szRemoteAddr2);
				printf("\t%d\n", 
					   ntohs((u_short)pTcpTable2->table[j].dwRemotePort));
							
				
				MIB_TCPROW sKillConn;
				
				sKillConn.dwLocalAddr = pTcpTable2->table[j].dwLocalAddr;	
				sKillConn.dwLocalPort = pTcpTable2->table[j].dwLocalPort; 
				
				sKillConn.dwRemoteAddr = pTcpTable2->table[j].dwRemoteAddr;
				sKillConn.dwRemotePort = pTcpTable2->table[j].dwRemotePort;
				sKillConn.dwState = MIB_TCP_STATE_DELETE_TCB;

				DWORD dwRez = SetTcpEntry(&sKillConn);

				if(dwRez != NO_ERROR)
				{
					dwRez = ::GetLastError();
					printf("\nError closing connection;\n");
				}
				else
					printf("\nConnection closed succesfully!\n");
			}

		}            
    } else {
        printf("\tGetTcpTable failed with %d\n", dwRetVal2);
        FREE(pTcpTable2);
        return 1;
    }	

    if (pTcpTable2 != NULL) {
        FREE(pTcpTable2);
        pTcpTable2 = NULL;
    }        
	cin.get();
    return 0;    
}

Any suggestion would be appreciated!

Thanks in advance!
Posted
Updated 21-May-12 20:32pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900