143 lines
3.5 KiB
C
143 lines
3.5 KiB
C
/*
|
|
* PVVMUD a 3D MUD
|
|
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program 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 General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
*
|
|
*/
|
|
#include "pvvmud.H"
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
#include <stdio.h>
|
|
#include <iostream.h>
|
|
#include "inetaddress.H"
|
|
|
|
#ifndef INADDR_NONE
|
|
#define INADDR_NONE -1
|
|
#endif
|
|
|
|
CUnknownHostException::CUnknownHostException()
|
|
:CException("Unknown host exception"){
|
|
|
|
}
|
|
|
|
CInetAddress::CInetAddress(){
|
|
setAddress(INADDR_NONE,0);
|
|
}
|
|
|
|
CInetAddress::CInetAddress(DWORD address){
|
|
setAddress(address,0);
|
|
}
|
|
|
|
CInetAddress::CInetAddress(DWORD address,WORD port){
|
|
setAddress(address,port);
|
|
}
|
|
|
|
CInetAddress::CInetAddress(char * hostName, WORD port){
|
|
struct hostent * hent;
|
|
char *portString;
|
|
|
|
portString=strchr(hostName,':');
|
|
if (portString) {
|
|
port=atoi(portString+1);
|
|
portString[0]=0;
|
|
// printf("%s , %s\n",hostName,portString+1);
|
|
cdebug << hostName << " , " << (portString+1) << "\n";
|
|
};
|
|
|
|
if (hostName) {
|
|
if (!strcmp("localhost",hostName)) {
|
|
// printf("Connecting to localhost, port : %i",port);
|
|
cdebug << "Connecting to localhost, port : " << port << "\n";
|
|
if (portString) portString[0]=':';
|
|
setAddress(INADDR_LOOPBACK,port);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if ((hent = gethostbyname(hostName)) == NULL){
|
|
throw new CUnknownHostException();
|
|
}
|
|
|
|
if (portString) {
|
|
portString[0]=':';
|
|
}
|
|
|
|
//printf("Connecting to another server, port : %i",port);
|
|
cdebug << "Connecting to another server, port : " << port << "\n";
|
|
|
|
setAddress(htonl(*((DWORD*)hent->h_addr)),port);
|
|
}
|
|
|
|
CInetAddress::CInetAddress(char * hostName){
|
|
struct hostent * hent;
|
|
char *portString;
|
|
WORD port=0;
|
|
|
|
portString=strchr(hostName,':');
|
|
if (portString) {
|
|
port=atoi(portString+1);
|
|
};
|
|
|
|
if ((hent = gethostbyname(hostName)) == NULL){
|
|
throw new CUnknownHostException();
|
|
}
|
|
|
|
setAddress(htonl(*((DWORD*)hent->h_addr)),port);
|
|
}
|
|
|
|
void CInetAddress::setAddress(DWORD address,WORD port){
|
|
m_address = address;
|
|
m_port = port;
|
|
}
|
|
|
|
char * CInetAddress::getHostName(){
|
|
struct hostent * hent;
|
|
DWORD address;
|
|
|
|
address=htonl(m_address);
|
|
|
|
if ((hent = gethostbyaddr((char*)&address, sizeof m_address, AF_INET)) == NULL){
|
|
throw new CUnknownHostException();
|
|
}
|
|
|
|
return hent->h_name;
|
|
}
|
|
|
|
char * CInetAddress::getAddressString(){
|
|
static char addr[1024];
|
|
try {
|
|
sprintf(addr,"%s:%d",getHostName(),getPort());
|
|
} catch (CException * e){
|
|
cdebug << *e << "\n";
|
|
delete e;
|
|
DWORD naddr = htonl(getAddress());
|
|
struct in_addr * inaddr = (struct in_addr*)&naddr;
|
|
sprintf(addr,"%s:%d",inet_ntoa(*inaddr),getPort());
|
|
}
|
|
return addr;
|
|
}
|
|
|
|
|
|
ostream& operator<<(ostream&s,const CInetAddress& address){
|
|
return s << ((CInetAddress)address).getAddressString();
|
|
}
|
|
|