00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00041 String *IPSocketAddress::className(void) const
00042 {
00043 return new String("IPSocketAddress");
00044 }
00045
00046
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
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
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
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
00139 ByteArray *IPSocketAddress::thisHost(void)
00140 {
00141 return new ByteArray("\x00\x00\x00\x00", 4);
00142 }
00143
00144
00145 unsigned short IPSocketAddress::anyPort(void)
00146 {
00147 return 0;
00148 }
00149
00150
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 }