// m_bWithCallback = TRUE : Listening network with callback function,no support now // m_bWithCallback = FALSE : Listening network without callback function,support it now BOOL CNetListener::Create(int m_nProtocol, bool m_bWithCallback) { char m_strErrorMsg[PCAP_ERRBUF_SIZE],m_strFilter[256]; u_int m_nNetMask; struct bpf_program m_structCode;
if ((m_nProtocol != LISTEN_PROTOCOL_TCP && m_nProtocol != LISTEN_PROTOCOL_UDP && m_nProtocol != LISTEN_PROTOCOL_BOTH) || m_bWithCallback == TRUE) return FALSE; // Init vatiable m_nProtocolType = m_nProtocol; m_bCallbackFlag = m_bWithCallback; // strcpy(m_strFilter,"ip and udp"); // Set filting condition strcpy(m_strFilter,"ip and tcp"); // Set filting condition // Retrieve the device list if (pcap_findalldevs(&m_pAllNetcard, m_strErrorMsg) == -1) return FALSE; m_pCurrNetcard = m_pAllNetcard->next; // To listen first netcard m_structHandle = pcap_open_live(m_pCurrNetcard->name,65536,1,1000,m_strErrorMsg); if (m_structHandle == NULL) { pcap_freealldevs(m_pAllNetcard); return FALSE; } // Check the link layer. We support only Ethernet for simplicity. if (pcap_datalink(m_structHandle) != DLT_EN10MB) { pcap_freealldevs(m_pAllNetcard); return FALSE; }
if (m_pCurrNetcard->addresses != NULL) // Retrieve the mask of the first address of the interface m_nNetMask = ((struct sockaddr_in *)(m_pCurrNetcard->addresses->netmask))->sin_addr.S_un.S_addr; else // If the interface is without addresses we suppose to be in a C class network m_nNetMask = 0xffffff; // compile the filter if (pcap_compile(m_structHandle,&m_structCode,m_strFilter,1,m_nNetMask) < 0 ) { pcap_freealldevs(m_pAllNetcard); return FALSE; }
// set the filter if (pcap_setfilter(m_structHandle,&m_structCode) < 0) { pcap_freealldevs(m_pAllNetcard); return FALSE; } pcap_freealldevs(m_pAllNetcard); return TRUE; }
// m_bWithCallback = FALSE : Listening network without callback function,support it now
BOOL CNetListener::Create(int m_nProtocol, bool m_bWithCallback)
{
char m_strErrorMsg[PCAP_ERRBUF_SIZE],m_strFilter[256];
u_int m_nNetMask;
struct bpf_program m_structCode;
if ((m_nProtocol != LISTEN_PROTOCOL_TCP &&
m_nProtocol != LISTEN_PROTOCOL_UDP &&
m_nProtocol != LISTEN_PROTOCOL_BOTH) || m_bWithCallback == TRUE)
return FALSE; // Init vatiable
m_nProtocolType = m_nProtocol;
m_bCallbackFlag = m_bWithCallback;
// strcpy(m_strFilter,"ip and udp"); // Set filting condition
strcpy(m_strFilter,"ip and tcp"); // Set filting condition // Retrieve the device list
if (pcap_findalldevs(&m_pAllNetcard, m_strErrorMsg) == -1)
return FALSE; m_pCurrNetcard = m_pAllNetcard->next; // To listen first netcard
m_structHandle = pcap_open_live(m_pCurrNetcard->name,65536,1,1000,m_strErrorMsg);
if (m_structHandle == NULL)
{
pcap_freealldevs(m_pAllNetcard); return FALSE;
} // Check the link layer. We support only Ethernet for simplicity.
if (pcap_datalink(m_structHandle) != DLT_EN10MB)
{
pcap_freealldevs(m_pAllNetcard); return FALSE;
}
if (m_pCurrNetcard->addresses != NULL)
// Retrieve the mask of the first address of the interface
m_nNetMask = ((struct sockaddr_in *)(m_pCurrNetcard->addresses->netmask))->sin_addr.S_un.S_addr;
else
// If the interface is without addresses we suppose to be in a C class network
m_nNetMask = 0xffffff;
// compile the filter
if (pcap_compile(m_structHandle,&m_structCode,m_strFilter,1,m_nNetMask) < 0 )
{
pcap_freealldevs(m_pAllNetcard); return FALSE;
}
// set the filter
if (pcap_setfilter(m_structHandle,&m_structCode) < 0)
{
pcap_freealldevs(m_pAllNetcard); return FALSE;
} pcap_freealldevs(m_pAllNetcard);
return TRUE;
}