Created
          April 9, 2014 03:38 
        
      - 
      
 - 
        
Save wyyqyl/10223952 to your computer and use it in GitHub Desktop.  
    Print network connection state
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <winsock2.h> | |
| #include <ws2tcpip.h> | |
| #include <iphlpapi.h> | |
| #include <stdio.h> | |
| // Need to link with Iphlpapi.lib and Ws2_32.lib | |
| #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_TCPTABLE2 pTcpTable = NULL; | |
| ULONG ulSize = 0; | |
| DWORD dwRetVal = 0; | |
| char szLocalAddr[128]; | |
| char szRemoteAddr[128]; | |
| struct in_addr IpAddr; | |
| // Make an initial call to GetTcpTable2 to | |
| // get the necessary size into the ulSize variable | |
| dwRetVal = GetTcpTable2(pTcpTable, &ulSize, TRUE); | |
| if (dwRetVal == ERROR_INSUFFICIENT_BUFFER) { | |
| pTcpTable = (MIB_TCPTABLE2 *)MALLOC(ulSize); | |
| if (pTcpTable == NULL) { | |
| printf("Error allocating memory\n"); | |
| return 1; | |
| } | |
| } | |
| // Make a second call to GetTcpTable2 to get | |
| // the actual data we require | |
| if ((dwRetVal = GetTcpTable2(pTcpTable, &ulSize, TRUE)) == NO_ERROR) { | |
| printf("\tNumber of entries: %d\n", (int)pTcpTable->dwNumEntries); | |
| for (int idx = 0; idx < (int)pTcpTable->dwNumEntries; idx++) { | |
| printf("\n\tTCP[%d] State: %ld - ", idx, pTcpTable->table[idx].dwState); | |
| switch (pTcpTable->table[idx].dwState) { | |
| case MIB_TCP_STATE_CLOSED: | |
| printf("CLOSED\n"); | |
| break; | |
| case MIB_TCP_STATE_LISTEN: | |
| printf("LISTEN\n"); | |
| break; | |
| case MIB_TCP_STATE_SYN_SENT: | |
| printf("SYN-SENT\n"); | |
| break; | |
| case MIB_TCP_STATE_SYN_RCVD: | |
| printf("SYN-RECEIVED\n"); | |
| break; | |
| case MIB_TCP_STATE_ESTAB: | |
| printf("ESTABLISHED\n"); | |
| break; | |
| case MIB_TCP_STATE_FIN_WAIT1: | |
| printf("FIN-WAIT-1\n"); | |
| break; | |
| case MIB_TCP_STATE_FIN_WAIT2: | |
| printf("FIN-WAIT-2 \n"); | |
| break; | |
| case MIB_TCP_STATE_CLOSE_WAIT: | |
| printf("CLOSE-WAIT\n"); | |
| break; | |
| case MIB_TCP_STATE_CLOSING: | |
| printf("CLOSING\n"); | |
| break; | |
| case MIB_TCP_STATE_LAST_ACK: | |
| printf("LAST-ACK\n"); | |
| break; | |
| case MIB_TCP_STATE_TIME_WAIT: | |
| printf("TIME-WAIT\n"); | |
| break; | |
| case MIB_TCP_STATE_DELETE_TCB: | |
| printf("DELETE-TCB\n"); | |
| break; | |
| default: | |
| printf("UNKNOWN dwState value\n"); | |
| break; | |
| } | |
| IpAddr.S_un.S_addr = (u_long)pTcpTable->table[idx].dwLocalAddr; | |
| strcpy_s(szLocalAddr, sizeof(szLocalAddr), inet_ntoa(IpAddr)); | |
| printf("\tTCP[%d] Local Addr: %s\n", idx, szLocalAddr); | |
| printf("\tTCP[%d] Local Port: %d \n", idx, | |
| ntohs((u_short)pTcpTable->table[idx].dwLocalPort)); | |
| IpAddr.S_un.S_addr = (u_long)pTcpTable->table[idx].dwRemoteAddr; | |
| strcpy_s(szRemoteAddr, sizeof(szRemoteAddr), inet_ntoa(IpAddr)); | |
| printf("\tTCP[%d] Remote Addr: %s\n", idx, szRemoteAddr); | |
| printf("\tTCP[%d] Remote Port: %d\n", idx, | |
| ntohs((u_short)pTcpTable->table[idx].dwRemotePort)); | |
| printf("\tTCP[%d] Owning PID: %d\n", idx, | |
| pTcpTable->table[idx].dwOwningPid); | |
| printf("\tTCP[%d] Offload State: %ld - ", idx, | |
| pTcpTable->table[idx].dwOffloadState); | |
| switch (pTcpTable->table[idx].dwOffloadState) { | |
| case TcpConnectionOffloadStateInHost: | |
| printf("Owned by the network stack and not offloaded \n"); | |
| break; | |
| case TcpConnectionOffloadStateOffloading: | |
| printf("In the process of being offloaded\n"); | |
| break; | |
| case TcpConnectionOffloadStateOffloaded: | |
| printf("Offloaded to the network interface control\n"); | |
| break; | |
| case TcpConnectionOffloadStateUploading: | |
| printf("In the process of being uploaded back to the network stack\n"); | |
| break; | |
| default: | |
| printf("UNKNOWN Offload state value\n"); | |
| break; | |
| } | |
| } | |
| } else { | |
| printf("\tGetTcpTable2 failed with %d\n", dwRetVal); | |
| FREE(pTcpTable); | |
| return 1; | |
| } | |
| if (pTcpTable != NULL) { | |
| FREE(pTcpTable); | |
| pTcpTable = NULL; | |
| } | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment