해결된 질문
작성
·
109
0
void packet_handler(u_char* param,
const struct pcap_pkthdr* header,
const u_char* pkt_data) // pcap_loop()가 돌면서 패킷을 감지할때 그 때 읽어들인 감청 데이터는 pkt_data 에 들어간다.
{
EtherHeader* pEther = (EtherHeader*)pkt_data; // 감청 데이터를 EtherHeader 로 형변환
printf( "SRC: %02X-%02X-%02X-%02X-%02X-%02X -> "
"DST: %02X-%02X-%02X-%02X-%02X-%02X, type:%04X\n",
pEther->srcMac[0], pEther->srcMac[1], pEther->srcMac[2],
pEther->srcMac[3], pEther->srcMac[4], pEther->srcMac[5],
pEther->dstMac[0], pEther->dstMac[1], pEther->dstMac[2],
pEther->dstMac[3], pEther->dstMac[4], pEther->dstMac[5],
//ntohs(pEther->type)
pEther->type
);
}
ntohs(pEther->type) 과 htons(pEther->type)이 기능은 똑같다고 하는데, ntohs() 는 네트워크 오더 -> 호스트 오더 이고, htons() 는 호스트 오더->네트워크 오더 아닌가요?
제가 Intel 프로세스 탑재한 PC에서 예제 실행중인데, 위 코드와 같이 실행하면 Ipv4 인 type 값이 0008로 나옵니다. 그래서 htons() 로 감싸서 출력하면 0800 으로 잘되는데 ntohs() 로 감쌌을떄는 왜 0800 이 나오는지 모르겠습니다.
ntohs() 면 호스트 오더를 사용하는데 제 pc에서는 그대로 호스트 오더 방식으로 되야 하는거 아닌가요?
답변 1
0
ntohs()나 htons()나 이름만 다를 뿐 기능이 같습니다. 그건 어디까지나 개발자 본인이 알고 제대로 값을 넣어야 합니다. 그리고 PC가 인텔이라 하더라도 패킷 데이터 값은 네트워크 오더에 맞춰 보내줍니다. 참고하시기 바랍니다. 😄