CCNA

핑ㅅ스

컴공 2013. 6. 14. 13:31
반응형
지식iN
전체서비스
검색창 펼치기
지식Q&A
이전질문하기
컴퓨터통신 > C, C++ 관심분야 추가
[질문]
C++로 패킷정보 알아내는 프로그램 소스(내공100)
비공개|마감률55%|2007.10.03
답변1 | 6 본문 작게 보기가 본문 크게 보기가
안녕하세요 프로그래밍을 공부하는데 정말로 모르겠어서 이렇게 질문을 올립니다
 
ping을 치고 해당 아이피 주소를 치면 그 아이피 주소에 관련하여
 
그 패킷에 대한 정보가 나오는 프로그램을 만들려고 하는데 어떻게 해야 하나요??
 
ping 을 쳤을때  버전,헤더길이,서비스타입,길이 식별자 플래그 옵셋 등등 나오게 하는
 
프로그램 어떻게 만들어야 하나요?
 
소스가 있으시면 가르쳐 주시거나 소스가 있는곳 아시면 가르쳐 주시기 바랍니다.
 
소스가 있으신 분은 메일로 보내주시면 감사하겠습니다.
의견쓰기 신고
[답변]
아루스(tinydew4)님의 답변
영웅|채택률100%|2007.10.05
질문자인사감사합니다
winsock2 와 iphlpapi 라이브러리를 이용하시면 됩니다.
 
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#pragma warning(push)
#pragma warning(disable: 4127)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
#include <stdio.h>
#include <stdlib.h>
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
/* Note: could also use malloc() and free() */
// Global variables
ULONG  ulOutBufLen;
DWORD  dwRetVal;
int main() {
 
/* variables used for GetNetworkParams */
  FIXED_INFO  *pFixedInfo;
  IP_ADDR_STRING  *pIPAddr;
/* variables used for GetAdapterInfo */
  IP_ADAPTER_INFO  *pAdapterInfo;
  IP_ADAPTER_INFO  *pAdapter;
  UINT  dwAddrLen;
/* variables used for GetInterfaceInfo */
  IP_INTERFACE_INFO*  pInterfaceInfo;
/* variables used for GetIpAddrTable */
  MIB_IPADDRTABLE  *pIPAddrTable;
  DWORD  dwSize;
  struct in_addr  IPAddr;
  char  *strIPAddr;
/* variables used for AddIpAddress */
  UINT  iaIPAddress;
  UINT  imIPMask;
  ULONG  NTEContext;
  ULONG  NTEInstance;
/* variables used for GetIpStatistics */
  MIB_IPSTATS  *pStats;
/* variables used for GetTcpStatistics */
  MIB_TCPSTATS  *pTCPStats;
  printf("------------------------\n");
  printf("This is GetNetworkParams\n");
  printf("------------------------\n");
  pFixedInfo = (FIXED_INFO *) MALLOC( sizeof( FIXED_INFO ) );
  if (pFixedInfo == NULL) {
    printf("Error allocating memory needed to call GetNetworkParams\n");
    return 1;
  }
  ulOutBufLen = sizeof( FIXED_INFO );
  if( GetNetworkParams( pFixedInfo, &ulOutBufLen ) == ERROR_BUFFER_OVERFLOW ) {
    FREE( pFixedInfo );
    pFixedInfo = (FIXED_INFO * ) MALLOC ( ulOutBufLen );
    if (pFixedInfo == NULL) {
      printf("Error allocating memory needed to call GetNetworkParams\n");
      return 1;
    }
  }
    dwRetVal = GetNetworkParams( pFixedInfo, &ulOutBufLen );
  if ( dwRetVal != NO_ERROR ) {
    printf("GetNetworkParams call failed with %d\n", dwRetVal);
    return 1;
  }
 
  else {
    printf("\tHost Name: %s\n", pFixedInfo -> HostName);
    printf("\tDomain Name: %s\n", pFixedInfo -> DomainName);
    printf("\tDNS Servers:\n");
    printf("\t\t%s\n", pFixedInfo -> DnsServerList.IpAddress.String);
    pIPAddr = pFixedInfo -> DnsServerList.Next;
    while ( pIPAddr ) {
      printf("\t\t%s\n", pIPAddr -> IpAddress.String);
      pIPAddr = pIPAddr -> Next;
    }
    if (pFixedInfo -> EnableRouting)
      printf("\tEnable Routing: Yes\n");
    else
      printf("\tEnable Routing: No\n");
    if (pFixedInfo -> EnableProxy)
      printf("\tEnable Proxy: Yes\n");
    else
      printf("\tEnable Proxy: No\n");
    if (pFixedInfo -> EnableDns)
      printf("\tEnable DNS: Yes\n");
    else
      printf("\tEnable DNS: No\n");
  }
  printf("------------------------\n");
  printf("This is GetAdaptersInfo\n");
  printf("------------------------\n");

  pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC( sizeof(IP_ADAPTER_INFO) );
  if (pAdapterInfo == NULL) {
    printf("Error allocating memory needed to call GetAdapterInfo\n");
    return 1;
  }
  ulOutBufLen = sizeof(IP_ADAPTER_INFO);
  if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
    FREE (pAdapterInfo);
    pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC (ulOutBufLen);
    if (pAdapterInfo == NULL) {
      printf("Error allocating memory needed to call GetAdapterInfo\n");
      return 1;
    }
  }
  if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) != NO_ERROR) {
    printf("GetAdaptersInfo call failed with %d\n", dwRetVal);
    return 1;
  }
  pAdapter = pAdapterInfo;
  while (pAdapter) {
    printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
    printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
    dwAddrLen = pAdapter->AddressLength;
    for (int i=0; i++, dwAddrLen--; ) {
  BYTE Data = pAdapter->Address[i];
      if (i ==0)
         printf("\tAdapter Addr: \t%d-", Data);
      else
         printf("%d-", Data);
    }
    // printf("\tAdapter Addr: \t%ld\n", pAdapter->Address);
    printf("\tIP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
    printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
    printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
    printf("\t***\n");
    if (pAdapter->DhcpEnabled) {
      printf("\tDHCP Enabled: Yes\n");
      printf("\t\tDHCP Server: \t%s\n", pAdapter->DhcpServer.IpAddress.String);
      printf("\tLease Obtained: %ld\n", pAdapter->LeaseObtained);
    }
    else
      printf("\tDHCP Enabled: No\n");
    if (pAdapter->HaveWins) {
      printf("\tHave Wins: Yes\n");
      printf("\t\tPrimary Wins Server: \t%s\n", pAdapter->PrimaryWinsServer.IpAddress.String);
      printf("\t\tSecondary Wins Server: \t%s\n", pAdapter->SecondaryWinsServer.IpAddress.String);
    }
    else
      printf("\tHave Wins: No\n");
    pAdapter = pAdapter->Next;
  }
 
  printf("------------------------\n");
  printf("This is GetInterfaceInfo\n");
  printf("------------------------\n");
  pInterfaceInfo = (IP_INTERFACE_INFO *) MALLOC( sizeof(IP_INTERFACE_INFO) );
  if (pInterfaceInfo == NULL) {
    printf("Error allocating memory needed to call GetInterfaceInfo\n");
    return 1;
  }
  if ( GetInterfaceInfo(pInterfaceInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER) {
    FREE (pInterfaceInfo);
    pInterfaceInfo = (IP_INTERFACE_INFO *) MALLOC (ulOutBufLen);
    if (pInterfaceInfo == NULL) {
      printf("Error allocating memory needed to call GetInterfaceInfo\n");
      return 1;
    }
    printf("ulLen = %ld\n", ulOutBufLen);
  }
  if ((dwRetVal = GetInterfaceInfo(pInterfaceInfo, &ulOutBufLen)) == NO_ERROR ) {
    printf("Adapter Name: %ws\n", pInterfaceInfo->Adapter[0].Name);
    printf("Adapter Index: %ld\n", pInterfaceInfo->Adapter[0].Index);
    printf("Num Adapters: %ld\n", pInterfaceInfo->NumAdapters);
    printf("this succeeded.\n");
  }
  else {
    LPVOID lpMsgBuf = NULL;
  
    if (FormatMessage(
      FORMAT_MESSAGE_ALLOCATE_BUFFER |
      FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_IGNORE_INSERTS,
      NULL,
      dwRetVal,
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
      (LPTSTR) &lpMsgBuf,
      0,
      NULL ))
    {
      printf("\tError: %s", lpMsgBuf);
    }
    LocalFree( lpMsgBuf );
  }
  /* If DHCP enabled, release and renew the IP address */
  /* THIS WORKS BUT IT TAKES A LONG TIME AND INTERRUPTS NET CONNECTIONS  */
  if (pAdapter->DhcpEnabled) {
    if ((dwRetVal = IpReleaseAddress(&pInterfaceInfo->Adapter[0])) == NO_ERROR) {
      printf("Ip Release succeeded.\n");
    }
    if ((dwRetVal = IpRenewAddress(&pInterfaceInfo->Adapter[0])) == NO_ERROR) {
      printf("Ip Renew succeeded.\n");
    }
  }
 
  printf("----------------------\n");
  printf("This is GetIpAddrTable\n");
  printf("----------------------\n");
 
  pIPAddrTable = (MIB_IPADDRTABLE*) MALLOC( sizeof( MIB_IPADDRTABLE) );
  if (pIPAddrTable == NULL) {
    printf("Error allocating memory needed to call GetIpAddrTable\n");
    return 1;
  }
  dwSize = 0;
  IPAddr.S_un.S_addr = ntohl(pIPAddrTable->table[1].dwAddr);
  strIPAddr = inet_ntoa(IPAddr);
  if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
    FREE ( pIPAddrTable );
    printf("GetIpAddrTable failed the first time.\n");
    pIPAddrTable = (MIB_IPADDRTABLE *) MALLOC ( dwSize );
    if (pIPAddrTable == NULL) {
      printf("Error allocating memory needed to call GetIpAddrTable\n");
      return 1;
    }
  }
  if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR ) {
    printf("GetIpAddrTable call failed with %d\n", dwRetVal);
    return 1;
  }
  printf("Address: %ld\n", pIPAddrTable->table[0].dwAddr);
  printf("Mask:    %ld\n", pIPAddrTable->table[0].dwMask);
  printf("Index:   %ld\n", pIPAddrTable->table[0].dwIndex);
  printf("BCast:   %ld\n", pIPAddrTable->table[0].dwBCastAddr);
  printf("Reasm:   %ld\n", pIPAddrTable->table[0].dwReasmSize);

  iaIPAddress = inet_addr("192.168.0.27");
  imIPMask = inet_addr("255.255.255.0");
  NTEContext = 0;
  NTEInstance = 0;
  if ( (dwRetVal = AddIPAddress(
    iaIPAddress,
    imIPMask,
    pIPAddrTable->table[0].
    dwIndex,
    &NTEContext,
    &NTEInstance) ) != NO_ERROR)
  {
    LPVOID lpMsgBuf;
    printf("\tError adding IP address.\n");
    if (FormatMessage(
      FORMAT_MESSAGE_ALLOCATE_BUFFER |
      FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_IGNORE_INSERTS,
      NULL,
      dwRetVal,
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
      (LPTSTR) &lpMsgBuf,
      0,
      NULL ))
    {
      printf("\tError: %s", lpMsgBuf);
    }
    LocalFree( lpMsgBuf );
  }
 
  if ((dwRetVal = DeleteIPAddress(NTEContext)) != NO_ERROR) {
    printf("DeleteIPAddress call failed with %d\n", dwRetVal);
  }
  printf("-------------------------\n");
  printf("This is GetIPStatistics()\n");
  printf("-------------------------\n");
  pStats = (MIB_IPSTATS*) MALLOC(sizeof(MIB_IPSTATS));
  if (pStats == NULL) {
    printf("Error allocating memory needed to call GetIpStatistics\n");
    return 1;
  }
  if ((dwRetVal = GetIpStatistics(pStats)) != NO_ERROR) {
    printf("GetIPStatistics call failed with %d\n", dwRetVal);
  }
  printf("\tNumber of IP addresses: %ld\n", pStats->dwNumAddr);
  printf("\tNumber of Interfaces: %ld\n", pStats->dwNumIf);
  printf("\tReceives: %ld\n", pStats->dwInReceives);
  printf("\tOut Requests: %ld\n", pStats->dwOutRequests);
  printf("\tRoutes: %ld\n", pStats->dwNumRoutes);
  printf("\tTimeout Time: %ld\n", pStats->dwReasmTimeout);
  printf("\tIn Delivers: %ld\n", pStats->dwInDelivers);
  printf("\tIn Discards: %ld\n", pStats->dwInDiscards);
  printf("\tTotal In: %ld\n", pStats->dwInDelivers + pStats->dwInDiscards);
  printf("\tIn Header Errors: %ld\n", pStats->dwInHdrErrors);
  printf("-------------------------\n");
  printf("This is GetTCPStatistics()\n");
  printf("-------------------------\n");
  pTCPStats = (MIB_TCPSTATS*) MALLOC (sizeof(MIB_TCPSTATS));
  if (pTCPStats == NULL) {
    printf("Error allocating memory needed to call GetTcpStatistics\n");
    return 1;
  }
  if ((dwRetVal = GetTcpStatistics(pTCPStats)) != NO_ERROR) {
    printf("GetTcpStatistics call failed with %d\n", dwRetVal);
    return 1;
  }
  printf("\tActive Opens: %ld\n", pTCPStats->dwActiveOpens);
  printf("\tPassive Opens: %ld\n", pTCPStats->dwPassiveOpens);
  printf("\tSegments Recv: %ld\n", pTCPStats->dwInSegs);
  printf("\tSegments Xmit: %ld\n", pTCPStats->dwOutSegs);
  printf("\tTotal # Conxs: %ld\n", pTCPStats->dwNumConns);
  return 0;
#pragma warning(pop)
}
반응형

'CCNA' 카테고리의 다른 글

해킹기법 분석사이트  (0) 2013.07.04
프레임릴레이와 AD(autonomous Distance)  (0) 2013.05.22
몰라몰라 3-1일차 OSPF설정 및 재분배.  (0) 2013.05.10
몰라몰라 3일차. EIGRP  (0) 2013.05.10
몰라 2일차 RIP -- 간단 설정  (0) 2013.05.08