/* * Paglo Crawler * Copyright (C) 2006-2008 Paglo Labs Inc. All rights reserved. * www.paglo.com * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ //--------------------------------------------------------------------------- #include "ArpTable.h" #include "Debug.h" //--------------------------------------------------------------------------- TArpTable::TArpTable(void) { pthread_mutex_init(&TableMutex, NULL); } //--------------------------------------------------------------------------- TArpTable::~TArpTable(void) { pthread_mutex_destroy(&TableMutex); } //--------------------------------------------------------------------------- void TArpTable::AddEntry(TIPAddress IPAddr, TMacAddress MacAddr) { TArpEntry *EntryPtr; if (IPAddr.Valid() && MacAddr.Valid()) { DEBUG_MESSAGE(DEBUG_ARPTABLE, ("Inserting ARP entry of %s for %s.\n", MacAddr.Print().c_str(), IPAddr.Print().c_str())); EntryPtr = (TArpEntry *)malloc(sizeof(TArpEntry)); EntryPtr->IPAddr = IPAddr; EntryPtr->MacAddr = MacAddr; pthread_mutex_lock(&TableMutex); ArpEntries.push_front(EntryPtr); pthread_mutex_unlock(&TableMutex); } } //--------------------------------------------------------------------------- TMacAddress TArpTable::LookupEntry(TIPAddress TargetIP) { list::iterator CurrEntry; TMacAddress MacAddr; DEBUG_MESSAGE(DEBUG_ARPTABLE, ("Looking up MAC address for %s.\n", TargetIP.Print().c_str())); pthread_mutex_lock(&TableMutex); for (CurrEntry = ArpEntries.begin(); CurrEntry != ArpEntries.end(); CurrEntry++) { if ((*CurrEntry)->IPAddr.IPv4Value() == TargetIP.IPv4Value()) { MacAddr = (*CurrEntry)->MacAddr; DEBUG_MESSAGE(DEBUG_ARPTABLE, ("Found MAC address for %s: %s.\n", TargetIP.Print().c_str(), MacAddr.Print().c_str())); break; } } pthread_mutex_unlock(&TableMutex); return MacAddr; } //---------------------------------------------------------------------------