// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * 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 * * $Id: InetAddress.jlc,v 1.6 2003/10/03 20:45:36 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/net/InetAddress.h" #include "jakelib2/net/Socket.h" #ifdef WIN32 # include #else # include # include # include # include #endif using namespace jakelib::net; using namespace jakelib::io; using namespace jakelib::lang; JAKELIB_IMPLEMENT_CLASS("jakelib.net.InetAddress", InetAddress, Object); /*****************************************************************************\ * InetAddress | *****************************************************************************/ InetAddress::InetAddress(char* addr) { Socket::initWinsock(); for (int idx = 0; idx < 4; idx++) { this->addr[idx] = addr[idx]; } } /*****************************************************************************\ * ~InetAddress | *****************************************************************************/ InetAddress::~InetAddress() { } /*****************************************************************************\ * getAddress | *****************************************************************************/ char* InetAddress::getAddress() { return addr; } /*****************************************************************************\ * getByName | *****************************************************************************/ InetAddress *InetAddress::getByName(char* name) { Socket::initWinsock(); struct hostent* hostEntry; hostEntry = gethostbyname(name); if (hostEntry == null) { throw new IOException(`"Unknown host "` .. name .. `" (error code "` .. (jlong) Socket::getLastError() .. `")"` .. JAKELIB_AT2("jakelib.net.InetAddress.getByName")); } else { return new InetAddress(hostEntry->h_addr_list[0]); } } /*****************************************************************************\ * getHostAddress | *****************************************************************************/ String *InetAddress::getHostAddress() { StringBuffer buf; // FIXME: Probably singedness problem! buf.append((unsigned char) addr[0])->append('.') ->append((unsigned char) addr[1])->append('.') ->append((unsigned char) addr[2])->append('.') ->append((unsigned char) addr[3]); return buf.toString(); } /*****************************************************************************\ * getHostName | *****************************************************************************/ String *InetAddress::getHostName() { Socket::initWinsock(); struct hostent* hostEntry; hostEntry = gethostbyaddr(addr, 4, AF_INET); if (hostEntry == null) { throw new IOException(`"Cannot determine host name for "` ..getHostAddress() .. JAKELIB_AT2("jakelib.net.InetAddress.getHostName")); } else { return new String(hostEntry->h_name); } } /*****************************************************************************\ * getLocalHost | *****************************************************************************/ InetAddress *InetAddress::getLocalHost() { char a[4]; a[0] = 127; a[1] = 0; a[2] = 0; a[3] = 1; return new InetAddress(a); } /*****************************************************************************\ * toString | *****************************************************************************/ String *InetAddress::toString() { return getHostAddress(); }