본문 바로가기

C++/Pcap

pcap_findalldevs 함수

[1. 원형]

int pcap_findalldevs(pcap_if_t ** alldevsp, char* errbuf)


[2. 설명]

pcap_open_live() 함수로 개방 될 수 있는 네트워크 디바이스 목록을 생성 함

 

권한이 없는 경우 모든 장치를 확인 할 수 없을 수 있다.

 

실패 시 -1 을 반환하고, errbuf 에 적절한 에러 메시지가 설정되며,

성공 시 0 을 반환함.


[3. winpcap]

int pcap_findalldevs_ex(char* source, pcap_rmtauth* auth, pcap_if_t ** alldevs, char* errbuf)

 

기존 pcap_findalldevs() 가 로컬 머신에 있는 네트워크 장치에 대해서만 확인 할 수 있었다면,

pcap_findalldevs_ex() 는 추가로 원격 머신에 있는 장치 및 주어진 폴더 내 pcap 파일까지 확인 할 수 있다.

 

원격 머신에 대해서 작업을 수행 할 때, 

해당 머신으로 control connection 을 열고 interface 를 확인 한 뒤 연결을 종료하는데,

해당 머신이 active 한 경우 연결을 해당 연결을 종료되지 않고 존재하는 소켓이 사용 된다.(?)

 

파라미터 설명

source: pcap_open() 과 같은 syntax 같은 형태이며, lookup 할 것을 명시한다.

=> local [rpcap://]

=> remote [rpcap://host:port]

 

auth: source가 remote 인 경우 RPCAP 연결을 위한 인증 정보 구조체에 대한 포인터, 로컬인 경우 NULL

 

에러 메시지 종류

 

  • libpcap/WinPcap was not installed on the local/remote host
  • the user does not have enough privileges to list the devices / files
  • a network problem
  • the RPCAP version negotiation failed
  • other errors (not enough memory and others)

[4. pcap_freealldevs]

alldevsp 에 할당된 메모리 해제를 진행하는 함수


[5. 예제]

#include "pcap.h"
int main()
{
char* dev, errbuf[PCAP_ERRBUF_SIZE];
pcap_if_t* alldevs;
pcap_if_t* d;
// alldevs: array of struct pcap_if
const int ret = pcap_findalldevs(&alldevs, errbuf);
if (ret != 0) {
// error
printf("%s\n", errbuf);
}
/* Print the list */
for (d = alldevs; d != NULL; d = d->next)
{
if (d->description) {
printf("desc: %s\n", d->description);
}
if (d->name) {
printf("name: %s\n", d->name);
}
struct pcap_addr* cur_addr = NULL;
if (d->addresses) {
cur_addr = d->addresses;
while (cur_addr != NULL)
{
const auto z = ((struct sockaddr_in*)cur_addr->addr)->sin_addr;
printf("address %s\n", inet_ntoa(z));
cur_addr = cur_addr->next;
}
}
printf("flags: %d\n\n", d->flags);
}
// free resource
pcap_freealldevs(alldevs);
return 0;
}
view raw gistfile1.cpp hosted with ❤ by GitHub

'C++ > Pcap' 카테고리의 다른 글

pcap_dispatch 와 기타 함수 들  (0) 2022.03.09
pcap_open_live 함수  (0) 2022.03.06
pcap_lookupnet 함수  (0) 2022.03.06
pcap 개발 환경 설정  (0) 2022.03.06