네트워크 및 보안/pcap 라이브러리

[pcap 라이브러리] 10. 기타 함수들 사용 예제 코드(pcap_is_swapped, pcap_freecode ,pcap_set_datalink)

언제나휴일 2016. 4. 29. 22:10
반응형

기타 함수들


사용하는 저장 파일과 byte order가 같은지 확인

int pcap_is_swapped(pcap_t *p); //0: 같음, 1:다름

pc_stoswap.c



//pc_stoswap.c

#include <pcap.h>

 

 

int main(int argc,char **argv){

  char errbuf[PCAP_ERRBUF_SIZE];

  pcap_t *pcap_handle;

 

  pcap_handle = pcap_open_offline(argv[1],errbuf);

  if(pcap_handle == 0){ printf("fail open_offline...%s\n",errbuf); }

  if(pcap_is_swapped(pcap_handle)==0){

    printf("capture system's byte order is same with this host\n");

  }//if

  else{

    printf("capture system's byte order is not same with this host\n");

  }//else

 

  pcap_close(pcap_handle);

  return 0;

}

$./pc_stoswap a.pcap

 



필터 해제

void  pcap_freecode (struct bpf_program *fp); 

datalink 계층 타입 확인, bpf.h참고

int pcap_datalink(pcap_t *handle);

pc_datalink.c


//pc_datalink.c

#include <pcap.h>

//#include <bpf.h> maybe my system configuration error.

 

int main(int argc,char **argv){

  char errbuf[PCAP_ERRBUF_SIZE];

  pcap_t *pcap_handle;

 

  pcap_handle = pcap_open_offline(argv[1],errbuf);

  if(pcap_handle == 0){ printf("fail open_offline...%s\n",errbuf); }

 

  if(pcap_is_swapped(pcap_handle)==0){

    printf("capture system's byte order is same with this host\n");

  }//if

  else{

    printf("capture system's byte order is not same with this host\n");

  }//else

 

  int datalink = pcap_datalink(pcap_handle);

  if (datalink < 0){

    printf("fail pcap_datalink failed [%s]\n", pcap_geterr(pcap_handle));

    return 4;

 }//if

 switch(datalink){

 case 0:/*DTL_NULL:  */ printf("BSD loopback encapsulation.\n"); break;

 case 1:/*DTL_EN10MB:*/ printf("Ethernet(10MB)             \n"); break;

 case 2:/*DTL_EN3MB: */ printf("Eperimental Ethernet(3MB)  \n"); break;

 case 3:/*DTL_AX25:  */ printf("Amateur Radio AX.25        \n"); break;

 default: printf("other...\n"); break;

 

 }//switch

 

  pcap_close(pcap_handle);

  return 0;

}

 

datalink 계층 설정

int pcap_set_datalink(pcap_t *handle, int datalink);

캡쳐 설정 크기 확인

int pcap_snapshot(pcap_t *);

pc_stoswap.c

//pc_snapshot.c

#include <pcap.h>

int main(int argc, char **argv)

{

  int re;

  char ebuf[PCAP_ERRBUF_SIZE];

  pcap_t *handle;

 

  handle = pcap_open_offline(argv[1], ebuf); 

  if (handle==0){

    printf("failed open_offline %s file, error[%s]\n", argv[1], ebuf);

    return 1;

  }//if

 

  int csize;

  csize = pcap_snapshot(handle);

  if (csize <= 0){

    printf("failed snapshot [%s]\n", pcap_geterr(handle));

    return 2;

  }//if

  printf("capture size is %d\n",csize);

 

//  u_char buf[10240]= "";

  struct pcap_pkthdr header;

  const u_char *packet;// =(const u_char *)&buf ;

  int cnt =0;

  while(1){

    packet = pcap_next(handle, &header);

    if(packet ==NULL)   break;

    //to do something   

    cnt++;

  }//while

  printf("total [%d]count \n",cnt);

  pcap_close(handle);

 

  return 0;

}

 


반응형