Busan IT/공장내 Network2015. 7. 22. 17:43

================================ Outline ====================================

TCP analyzer

----------------------------------------------------------------------------




 

접속 시 SYN1이 연속으로 두 번 캡쳐 된다.

 /*** 소스 ***/

 

#include <stdio.h>
#include <netinet/in.h>
#include <pcap/pcap.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

int main(void)
{
  char*           cpNICName;
  char           caErrMSG[PCAP_ERRBUF_SIZE];
  int           iCnt;
  unsigned char const *   ucpData;
  pcap_t*         stpDS;
  struct pcap_pkthdr     stInfo;
  struct tcphdr *     stTcp;
  struct ip *        stpIp;  


  cpNICName = pcap_lookupdev(caErrMSG);

  if(0 == cpNICName)
  {
    printf("ERRMSG  : [%s]\n",caErrMSG);
    return 100;
  }

  stpDS = pcap_open_live(cpNICName, ETH_DATA_LEN, 10, caErrMSG);

  printf("Packet Monitoring Start....\n");
  getchar();

  while(1)
  {
    ucpData = pcap_next(stpDS, &stInfo);

    if(ETH_P_IP != ntohs(*(unsigned short*)(ucpData+12))) // 2byte 二쇱냼
    {
      continue;
    }

    if(IPPROTO_TCP != *(ucpData+23))
    {
      continue;
    }


    stpIp = (struct ip *) (ucpData + sizeof(struct ether_header));
    stTcp = (struct tcphdr *)(ucpData + sizeof(struct ether_header) 
        + ((*(ucpData+ sizeof(struct ether_header)) & 0x0F) * 4));

    printf("=============================================\n");
    printf("[%s:%d] ---> [%s:%d]\n"
        , inet_ntoa(stpIp -> ip_src)
        , ntohs(stTcp -> source)
        , inet_ntoa(stpIp -> ip_dst)
        , ntohs(stTcp -> dest)
        );

    printf("SYN[%d] ACK[%d] Seq[%010u] Ack[%010u]\n"
        , stTcp -> syn
        , stTcp -> ack
        , ntohl(stTcp -> seq)
        , ntohl(stTcp -> ack_seq)
        );
  }

  pcap_close(stpDS);

  return 0;
}


 

Three-way Hand Shake... yeah

 

 

 

 

반응형

'Busan IT > 공장내 Network' 카테고리의 다른 글

채팅 멀티프로세스  (0) 2015.07.28
Three-way Handshake, fork 함수, execl 함수  (0) 2015.07.24
TCP analyzer(진행 중)  (0) 2015.07.17
TCP 추출(진행중)  (0) 2015.07.16
Echo Server/Client  (0) 2015.07.15
Posted by newind2000