|
Mixe for Privacy and Anonymity in the Internet
|
00001 /* 00002 Copyright (c) 2000, The JAP-Team 00003 All rights reserved. 00004 Redistribution and use in source and binary forms, with or without modification, 00005 are permitted provided that the following conditions are met: 00006 00007 - Redistributions of source code must retain the above copyright notice, 00008 this list of conditions and the following disclaimer. 00009 00010 - Redistributions in binary form must reproduce the above copyright notice, 00011 this list of conditions and the following disclaimer in the documentation and/or 00012 other materials provided with the distribution. 00013 00014 - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors 00015 may be used to endorse or promote products derived from this software without specific 00016 prior written permission. 00017 00018 00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS 00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE 00027 */ 00028 #include "StdAfx.h" 00029 #include "CASocketAddrINet.hpp" 00030 #include "CAMsg.hpp" 00031 00032 CAMutex* CASocketAddrINet::m_pcsGet=NULL; 00033 00035 /*SINT32 CASocketAddrINet::init() 00036 { 00037 if(!m_bIsCsInitialized) 00038 { 00039 m_bIsCsInitialized=true; 00040 } 00041 return E_SUCCESS; 00042 } 00043 */ 00046 /*SINT32 CASocketAddrINet::destroy() 00047 { 00048 if(m_bIsCsInitialized) 00049 { 00050 DeleteCriticalSection(&m_csGet); 00051 } 00052 m_bIsCsInitialized=false; 00053 return E_SUCCESS; 00054 } 00055 */ 00056 00058 CASocketAddrINet::CASocketAddrINet() 00059 { 00060 memset((SOCKADDR*)LPSOCKADDR(),0,getSize()); 00061 sin_family=AF_INET; 00062 sin_addr.s_addr=INADDR_ANY; 00063 sin_port=0; 00064 } 00065 00067 CASocketAddrINet::CASocketAddrINet(UINT16 port) 00068 { 00069 memset((SOCKADDR*)LPSOCKADDR(),0,getSize()); 00070 sin_family=AF_INET; 00071 sin_port=htons(port); 00072 sin_addr.s_addr=INADDR_ANY; 00073 } 00074 00076 CASocketAddrINet::CASocketAddrINet(const CASocketAddrINet& addr) 00077 { 00078 memset((SOCKADDR*)LPSOCKADDR(),0,getSize()); 00079 sin_family=AF_INET; 00080 sin_port=addr.sin_port; 00081 sin_addr.s_addr=addr.sin_addr.s_addr; 00082 } 00083 00093 SINT32 CASocketAddrINet::setAddr(const UINT8* szIP,UINT16 port) 00094 { 00095 UINT32 newAddr=INADDR_ANY; 00096 if(szIP!=NULL) 00097 { 00098 newAddr=inet_addr((const char*)szIP); //is it a doted string (a.b.c.d) ? 00099 if(newAddr==INADDR_NONE) //if not try to find the hostname 00100 { 00101 m_pcsGet->lock(); 00102 HOSTENT* hostent=gethostbyname((const char*)szIP); //lookup 00103 if(hostent!=NULL) //get it! 00104 memcpy(&newAddr,hostent->h_addr_list[0],hostent->h_length); 00105 else 00106 { 00107 m_pcsGet->unlock(); 00108 return E_UNKNOWN_HOST; //not found! 00109 } 00110 m_pcsGet->unlock(); 00111 } 00112 } 00113 sin_addr.s_addr=newAddr; 00114 sin_port=htons(port); 00115 return E_SUCCESS; 00116 } 00117 00123 SINT32 CASocketAddrINet::setPort(UINT16 port) 00124 { 00125 sin_port=htons(port); 00126 return E_SUCCESS; 00127 } 00128 00132 UINT16 CASocketAddrINet::getPort() const 00133 { 00134 return ntohs(sin_port); 00135 } 00136 00145 SINT32 CASocketAddrINet::getHostName(UINT8* buff,UINT32 len) const 00146 { 00147 if(buff==NULL) 00148 return E_UNSPECIFIED; 00149 SINT32 ret; 00150 m_pcsGet->lock(); 00151 HOSTENT* hosten=gethostbyaddr((const char*)&sin_addr,4,AF_INET); 00152 if(hosten==NULL||hosten->h_name==NULL) 00153 ret=E_UNKNOWN_HOST; 00154 else if(strlen(hosten->h_name)>=len) 00155 ret=E_SPACE; 00156 else 00157 { 00158 strcpy((char*)buff,hosten->h_name); 00159 ret=E_SUCCESS; 00160 } 00161 m_pcsGet->unlock(); 00162 return ret; 00163 } 00164 00169 SINT32 CASocketAddrINet::getIP(UINT8 buff[4]) const 00170 { 00171 memcpy(buff,&sin_addr.s_addr,4); 00172 return E_SUCCESS; 00173 } 00174 00179 SINT32 CASocketAddrINet::setIP(UINT8 ip[4]) 00180 { 00181 memcpy(&sin_addr.s_addr,ip,4); 00182 return E_SUCCESS; 00183 } 00184 00190 SINT32 CASocketAddrINet::getIPAsStr(UINT8* buff,UINT32 len) const 00191 { 00192 if(buff==NULL) 00193 return E_UNKNOWN; 00194 UINT8 ip[4]; 00195 if(getIP(ip)!=E_SUCCESS) 00196 return E_UNKNOWN; 00197 char* strAddr=inet_ntoa(sin_addr); 00198 if(strAddr==NULL) 00199 return E_UNKNOWN; 00200 if(strlen(strAddr)>=len) 00201 return E_UNKNOWN; 00202 strcpy((char*)buff,strAddr); 00203 return E_SUCCESS; 00204 } 00205 00214 SINT32 CASocketAddrINet::getLocalHostName(UINT8* buff,UINT32 len) 00215 { 00216 if(buff==NULL) 00217 return E_UNSPECIFIED; 00218 SINT32 ret; 00219 m_pcsGet->lock(); 00220 if(gethostname((char*)buff,len)==-1) 00221 ret=E_SPACE; 00222 else 00223 { 00224 HOSTENT* hosten=gethostbyname((char*)buff); 00225 if(hosten==NULL||hosten->h_name==NULL) 00226 ret=E_UNKNOWN_HOST; 00227 else if(strlen(hosten->h_name)>=len) 00228 ret=E_SPACE; 00229 else 00230 { 00231 strcpy((char*)buff,hosten->h_name); 00232 ret=E_SUCCESS; 00233 } 00234 } 00235 m_pcsGet->unlock(); 00236 return ret; 00237 } 00238 00244 SINT32 CASocketAddrINet::getLocalHostIP(UINT8 ip[4]) 00245 { 00246 SINT32 ret; 00247 char buff[256]; 00248 m_pcsGet->lock(); 00249 if(gethostname(buff,256)==-1) 00250 ret=E_UNKNOWN; 00251 else 00252 { 00253 HOSTENT* hosten=gethostbyname((char*)buff); 00254 if(hosten==NULL) 00255 ret=E_UNKNOWN; 00256 else 00257 { 00258 memcpy((char*)ip,hosten->h_addr_list[0],4); 00259 ret=E_SUCCESS; 00260 } 00261 } 00262 m_pcsGet->unlock(); 00263 return ret; 00264 } 00265
1.7.6.1