00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <Socket.h>
00020 #include <SocketHandler.h>
00021 #include <StringLib.h>
00022
00023 using namespace std;
00024 using namespace StringLib;
00025
00026
00027 Socket::Socket(){
00028
00029 #ifdef HAVE_SSL
00030 OpenSSL_add_ssl_algorithms();
00031 SSL_load_error_strings();
00032
00033 connected = false;
00034 #endif
00035
00036 is_SSL = false;
00037 sock = -1;
00038 #ifdef _WIN32
00039 WSADATA WinsockData;
00040 if (WSAStartup(MAKEWORD(2, 2), &WinsockData) != 0) {
00041
00042
00043 throw SocketException(WSA_FAILED);
00044
00045 }
00046
00047 #endif
00048 }
00049
00050
00051 Socket::Socket(socket_t _sock){
00052
00053 sock = _sock;
00054 if(sock > -1) connected = true;
00055 else connected = false;
00056 is_SSL = false;
00057
00058 #ifdef _WIN32
00059 WSADATA WinsockData;
00060 if (WSAStartup(MAKEWORD(2, 2), &WinsockData) != 0) {
00061
00062
00063 throw SocketException(WSA_FAILED);
00064
00065 }
00066
00067 #endif
00068
00069 }
00070
00071
00072 Socket::~Socket(){
00073 if (sock != -1){
00074 Close();
00075 }
00076 }
00077
00078 void Socket::MakeSock(){
00079
00080 if(sock!=-1) throw SocketException(ALREADY_HAVE_SOCKET);
00081
00082 int sockettype;
00083
00084 switch(type){
00085
00086 case TYPE_TCP: sockettype = SOCK_STREAM; break;
00087 case TYPE_UDP: sockettype = SOCK_DGRAM; break;
00088
00089 default: throw SocketException(WRONG_SOCKET_TYPE);
00090
00091 }
00092
00093 sock = socket(AF_INET, sockettype, 0);
00094 if(sock < 0) throw SocketException(SOCKET_FAILED);
00095
00096 #ifdef HAVE_SSL
00097
00098 if(is_SSL){
00099
00100 SSL_library_init();
00101 if(!(ctx = SSL_CTX_new(SSLv23_method())) or !(ssl = SSL_new (ctx)) or
00102 SSL_set_fd(ssl, GetSock()) == 0) throw SocketException(SSL_FAILED);
00103 debug("SSL Created");
00104
00105 }
00106
00107 #endif
00108
00109 }
00110
00111 void Socket::Close(){
00112
00113 #ifdef _WIN32
00114 closesocket(sock);
00115 #else
00116 close(sock);
00117 #endif
00118
00119 connected = false;
00120 sock = -1;
00121
00122 __OnClose();
00123
00124 }
00125
00126
00127 socket_t Socket::GetSock(){
00128
00129 return sock;
00130
00131 }
00132
00133
00134 int Socket::GetPort(){
00135
00136 return (int) ntohs(saddr.sin_port);
00137
00138 }
00139
00140
00141
00142 string Socket::GetAddr(){
00143
00144 return (string) inet_ntoa(saddr.sin_addr);
00145
00146 }
00147
00148
00149 string Socket::GetLastError(){
00150
00151 #ifdef _WIN32
00152
00153 return itos(WSAGetLastError());
00154
00155 #else
00156
00157 if(errno > 0) {
00158
00159 char* err = strerror(errno);
00160 return (string) err + " (" + itos(errno) + ")";
00161
00162 }
00163 #endif
00164 return "";
00165
00166 }
00167
00168
00169 #ifdef HAVE_SSL
00170 void Socket::SetSSL(bool _is_SSL){
00171
00172 if(connected) return;
00173
00174 is_SSL = _is_SSL;
00175
00176 }
00177 #endif
00178
00179 void Socket::OnConnected(){
00180
00181 connected = true;
00182 __OnConnected();
00183
00184 }
00185
00186
00187 void Socket::OnReadError(){
00188
00189 __OnReadError();
00190 Close();
00191
00192 }
00193
00194
00195 void Socket::OnConnClosed(){
00196
00197 SocketHandler* handler = SocketHandler::getInstance();
00198 if(!handler) throw SocketException(SOCKETHANDLING_FAILED);
00199
00200 handler -> Remove(this);
00201 __OnConnClosed();
00202
00203 }
00204
00205
00206 string Resolv(string _addr){
00207
00208 struct hostent *host;
00209
00210 struct in_addr destip;
00211 #ifdef _WIN32 // sad :(
00212 if ((host = gethostbyname(_addr.c_str())) != NULL){
00213
00214 memcpy(&(destip.s_addr),host->h_addr_list[0],sizeof(u_long));
00215
00216 return (string) inet_ntoa(destip);
00217
00218 }else{
00219
00220 return "";
00221
00222 }
00223
00224 #else
00225
00226 if(inet_aton(_addr.c_str(), &destip)){
00227
00228 return _addr;
00229
00230 } else {
00231
00232 if ((host = gethostbyname(_addr.c_str())) != NULL){
00233
00234 memcpy(&(destip.s_addr),host->h_addr_list[0],sizeof(u_long));
00235
00236 return (string) inet_ntoa(destip);
00237
00238 }else{
00239
00240 return "";
00241
00242 }
00243
00244 }
00245
00246 #endif
00247 }