/* Generated automatically by jlpp - do not edit. */ #include static jakelib::lang::String* jakelib2_strings[] = {null, null, null, null}; // "Unknown host " static jchar chars_jakelib2_str_0[] = {85,110,107,110,111,119,110,32,104,111,115,116,32}; // " (error code " static jchar chars_jakelib2_str_1[] = {32,40,101,114,114,111,114,32,99,111,100,101,32}; // ")" static jchar chars_jakelib2_str_2[] = {41}; // "Cannot determine host name for " static jchar chars_jakelib2_str_3[] = {67,97,110,110,111,116,32,100,101,116,101,114,109,105,110,101,32,104,111,115,116,32,110,97,109,101,32,102,111,114,32}; #line 1 "net/InetAddress.jlc" // -*- 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(JAKELIB_ONDEMAND(jakelib2_strings[0], new jakelib::lang::String(chars_jakelib2_str_0, 0, 13)) ->plus( name )->plus( JAKELIB_ONDEMAND(jakelib2_strings[1], new jakelib::lang::String(chars_jakelib2_str_1, 0, 13)) )->plus( (jlong) Socket::getLastError() )->plus( JAKELIB_ONDEMAND(jakelib2_strings[2], new jakelib::lang::String(chars_jakelib2_str_2, 0, 1)) )->plus( 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(JAKELIB_ONDEMAND(jakelib2_strings[3], new jakelib::lang::String(chars_jakelib2_str_3, 0, 31)) ->plus(getHostAddress() )->plus( 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(); }