Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

IPSocketAddress.cc

Go to the documentation of this file.
00001 /*
00002  * IPSocketAddress.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Remote station IP address.
00006  *
00007  * Copyright (c) 2004 Milan Cermak
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #include <stlib/net/IPSocketAddress.h>
00025 #include <stlib/net/UnknownHostError.h>
00026 
00027 #include <stlib/Array.h>
00028 #include <stlib/ByteArray.h>
00029 #include <stlib/Character.h>
00030 #include <stlib/Integer.h>
00031 #include <stlib/String.h>
00032 #include <stlib/Stream.h>
00033 #include <stlib/WriteStream.h>
00034 
00035 #include <netdb.h>
00036 #include <sys/socket.h>
00037 
00038 using namespace Net;
00039 
00040 /* Class-accessing protocol */
00041 String *IPSocketAddress::className(void) const
00042 {
00043     return new String("IPSocketAddress");
00044 }
00045 
00046 /* Instance creation protocol */
00047 IPSocketAddress *IPSocketAddress::hostAddress(ByteArray *addr, unsigned short int port)
00048 {
00049     IPSocketAddress *instance = new IPSocketAddress;
00050     instance->hostAddress(addr);
00051     instance->port(port);
00052     return instance;
00053 }
00054 
00055 IPSocketAddress *IPSocketAddress::hostName(String *name, unsigned short int port)
00056 {
00057     IPSocketAddress *instance = new IPSocketAddress;
00058     instance->hostName(name);
00059     instance->port(port);
00060     return instance;
00061 }
00062 
00063 IPSocketAddress *IPSocketAddress::thisHostAnyPort(void)
00064 {
00065     return hostAddress(thisHost(), anyPort());
00066 }
00067 
00068 /* Class-naming utilities protocol */
00069 ByteArray *IPSocketAddress::hostAddressByName(String *name)
00070 {
00071     struct hostent *ph = NULL;
00072 
00073     ph = gethostbyname(name->asCString());
00074     if (ph != NULL) return new ByteArray(ph->h_addr, 4);
00075 
00076     ByteArray *address = stringToBytes(name);
00077     if (address != nil) return address;
00078     (new UnknownHostError(name, __PRETTY_FUNCTION__))->raise();
00079 }
00080 
00081 String *IPSocketAddress::hostNameByAddress(ByteArray *address)
00082 {
00083     struct hostent *ph = NULL;
00084 
00085     ph = gethostbyaddr(address->rawBytesReadOnly(), address->size(), AF_INET);
00086     if (ph != NULL) return new String(ph->h_name);
00087     return bytesToName(address);
00088 }
00089 
00090 /* Class-private protocol */
00091 String *IPSocketAddress::bytesToName(ByteArray *address)
00092 {
00093     return address->printSeparatedBy(".");
00094 }
00095 
00096 ByteArray *IPSocketAddress::stringToBytes(String *name)
00097 {
00098     Array *parts = name->asArrayOfSubstringsSeparatedBy(Character::value('.'));
00099     if (parts->size() != 4) return nil;
00100     ByteArray *address = new ByteArray(4);
00101     for (int i = 0; i < 4; i++) {
00102         address->put(i, dynamic_cast<String *>(parts->at(i))->asNumber());
00103     }
00104     return address;
00105 }
00106 
00107 /* Accessing protocol */
00108 ByteArray *IPSocketAddress::hostAddress(void) const
00109 {
00110     return _address;
00111 }
00112 
00113 void IPSocketAddress::hostAddress(ByteArray *addr)
00114 {
00115     _address = addr;
00116 }
00117 
00118 String *IPSocketAddress::hostName(void) const
00119 {
00120     return hostNameByAddress(hostAddress());
00121 }
00122 
00123 void IPSocketAddress::hostName(String *name)
00124 {
00125     hostAddress(hostAddressByName(name));
00126 }
00127 
00128 unsigned short int IPSocketAddress::port(void)
00129 {
00130     return _port;
00131 }
00132 
00133 void IPSocketAddress::port(unsigned short int p)
00134 {
00135     _port = p;
00136 }
00137 
00138 /* Addressing protocol */
00139 ByteArray *IPSocketAddress::thisHost(void)
00140 {
00141     return new ByteArray("\x00\x00\x00\x00", 4);
00142 }
00143 
00144 /* Constants protocol */
00145 unsigned short IPSocketAddress::anyPort(void)
00146 {
00147     return 0;
00148 }
00149 
00150 /* Printing protocol */
00151 void IPSocketAddress::printOn(Stream *stream) const
00152 {
00153     Object::printOn(stream);
00154     stream->nextPut('(');
00155     stream->nextPutAll(hostName());
00156     stream->nextPutAll(", ");
00157     stream->print((long) _port);
00158     stream->nextPut(')');
00159 }

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2