/*----------------------------------------------------------------------------- * hostaddress.cpp * Copyright (C) 2003, 2004, 2005 Akito Nozaki * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ // Need this class to be thread safe :( most c network stuff is not thread // safe :( #include #include #include #include #include #include NAMESPACE_TANOSHI { HostAddress::HostAddress() { _ipv4 = 0; empty = true; } HostAddress::HostAddress(unsigned long ip4addr) { _ipv4 = ip4addr; empty = false; } HostAddress::HostAddress(const std::string &address) { setAddress(address); } HostAddress::~HostAddress() { } bool HostAddress::setAddress(unsigned long ip4addr) { _ipv4 = ip4addr; empty = false; } bool HostAddress::setAddress(const std::string &address) { // is this thing thread safe? if(inet_aton(address.c_str(), (in_addr*)&_ipv4)) { _ipv4 = htonl(_ipv4); empty = false; } else empty = true; return !empty; } unsigned long HostAddress::toIPv4Address() { return _ipv4; } std::string HostAddress::toString() { // can't use net_ntoa because its not safe... int a1 = (_ipv4 >> 24) & 0xFF; int a2 = (_ipv4 >> 16) & 0xFF; int a3 = (_ipv4 >> 8) & 0xFF; int a4 = (_ipv4) & 0xFF; std::string temp; temp += _toString(a1) + "." + _toString(a2) + "." + _toString(a3) + "." + _toString(a4); return temp; } bool HostAddress::operator ==(const HostAddress &address) { if(address.empty != empty) return false; if(address.empty == true) return true; return (address._ipv4 == _ipv4); } bool HostAddress::isEmpty() { return empty; } void HostAddress::clear() { empty = true; } std::string HostAddress::_toString(int num) { // very lazy way of doing this :P char temp[10]; sprintf(temp, "%d", num); std::string data = temp; return data; } }