| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* $Id $ */ |
| 20 | |
| 21 | |
| 22 | #include "config.h" |
| 23 | |
| 24 | #if C_MODEM |
| 25 | |
| 26 | /*****************************************************************************/ |
| 27 | // C++ SDLnet wrapper |
| 28 | |
| 29 | #include "misc_util.h" |
| 30 | |
| 31 | struct _TCPsocketX { |
| 32 | int ready; |
| 33 | #ifdef NATIVESOCKETS |
| 34 | SOCKET channel; |
| 35 | #endif |
| 36 | IPaddress remoteAddress; |
| 37 | IPaddress localAddress; |
| 38 | int sflag; |
| 39 | }; |
| 40 | |
| 41 | Bit32u Netwrapper_GetCapabilities() |
| 42 | { |
| 43 | Bit32u retval=0; |
| 44 | retval = CAPWORD; |
| 45 | return retval; |
| 46 | } |
| 47 | |
| 48 | #ifdef NATIVESOCKETS |
| 49 | TCPClientSocket::TCPClientSocket(int platformsocket) { |
| 50 | sendbuffer=0; |
| 51 | nativetcpstruct = new Bit8u[sizeof(struct _TCPsocketX)]; |
| 52 | |
| 53 | mysock = (TCPsocket)nativetcpstruct; |
| 54 | isopen = false; |
| 55 | if(!SDLNetInited) { |
| 56 | if(SDLNet_Init()==-1) { |
| 57 | LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError()); |
| 58 | return; |
| 59 | } |
| 60 | SDLNetInited = true; |
| 61 | } |
| 62 | // fill the SDL socket manually |
| 63 | ((struct _TCPsocketX*)nativetcpstruct)->ready=0; |
| 64 | ((struct _TCPsocketX*)nativetcpstruct)->sflag=0; |
| 65 | ((struct _TCPsocketX*)nativetcpstruct)->channel=(SOCKET) platformsocket; |
| 66 | sockaddr_in sa; |
| 67 | #ifdef OS2 |
| 68 | int sz; |
| 69 | #else |
| 70 | socklen_t sz; |
| 71 | #endif |
| 72 | sz=sizeof(sa); |
| 73 | if(getpeername(platformsocket, (sockaddr *)(&sa), &sz)==0) { |
| 74 | ((struct _TCPsocketX*)nativetcpstruct)-> |
| 75 | remoteAddress.host=/*ntohl(*/sa.sin_addr.s_addr;//); |
| 76 | ((struct _TCPsocketX*)nativetcpstruct)-> |
| 77 | remoteAddress.port=/*ntohs(*/sa.sin_port;//); |
| 78 | } |
| 79 | else { |
| 80 | mysock=0; |
| 81 | return; |
| 82 | } |
| 83 | sz=sizeof(sa); |
| 84 | if(getsockname(platformsocket, (sockaddr *)(&sa), &sz)==0) { |
| 85 | ((struct _TCPsocketX*)nativetcpstruct)-> |
| 86 | localAddress.host=/*ntohl(*/sa.sin_addr.s_addr;//); |
| 87 | ((struct _TCPsocketX*)nativetcpstruct)-> |
| 88 | localAddress.port=/*ntohs(*/sa.sin_port;//); |
| 89 | } |
| 90 | else { |
| 91 | mysock=0; |
| 92 | return; |
| 93 | } |
| 94 | if(mysock!=0) { |
| 95 | listensocketset = SDLNet_AllocSocketSet(1); |
| 96 | if(!listensocketset) return; |
| 97 | SDLNet_TCP_AddSocket(listensocketset, mysock); |
| 98 | isopen=true; |
| 99 | return; |
| 100 | } |
| 101 | mysock=0; |
| 102 | return; |
| 103 | } |
| 104 | #endif // NATIVESOCKETS |
| 105 | |
| 106 | TCPClientSocket::TCPClientSocket(TCPsocket source) { |
| 107 | #ifdef NATIVESOCKETS |
| 108 | nativetcpstruct=0; |
| 109 | #endif |
| 110 | sendbuffer=0; |
| 111 | isopen = false; |
| 112 | if(!SDLNetInited) { |
| 113 | if(SDLNet_Init()==-1) { |
| 114 | LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError()); |
| 115 | return; |
| 116 | } |
| 117 | SDLNetInited = true; |
| 118 | } |
| 119 | |
| 120 | mysock=0; |
| 121 | listensocketset=0; |
| 122 | if(source!=0) { |
| 123 | mysock = source; |
| 124 | listensocketset = SDLNet_AllocSocketSet(1); |
| 125 | if(!listensocketset) return; |
| 126 | SDLNet_TCP_AddSocket(listensocketset, source); |
| 127 | |
| 128 | isopen=true; |
| 129 | } |
| 130 | } |
| 131 | TCPClientSocket::TCPClientSocket(const char* destination, Bit16u port) { |
| 132 | #ifdef NATIVESOCKETS |
| 133 | nativetcpstruct=0; |
| 134 | #endif |
| 135 | sendbuffer=0; |
| 136 | isopen = false; |
| 137 | if(!SDLNetInited) { |
| 138 | if(SDLNet_Init()==-1) { |
| 139 | LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError()); |
| 140 | return; |
| 141 | } |
| 142 | SDLNetInited = true; |
| 143 | } |
| 144 | mysock=0; |
| 145 | listensocketset=0; |
| 146 | |
| 147 | IPaddress openip; |
| 148 | //Ancient versions of SDL_net had this as char*. People still appear to be using this one. |
| 149 | if (!SDLNet_ResolveHost(&openip,const_cast<char*>(destination),port)) { |
| 150 | listensocketset = SDLNet_AllocSocketSet(1); |
| 151 | if(!listensocketset) return; |
| 152 | mysock = SDLNet_TCP_Open(&openip); |
| 153 | if(!mysock) return; |
| 154 | SDLNet_TCP_AddSocket(listensocketset, mysock); |
| 155 | isopen=true; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | TCPClientSocket::~TCPClientSocket() { |
| 160 | |
| 161 | if(sendbuffer) delete [] sendbuffer; |
| 162 | #ifdef NATIVESOCKETS |
| 163 | if(nativetcpstruct) delete [] nativetcpstruct; |
| 164 | else |
| 165 | #endif |
| 166 | if(mysock) { |
| 167 | if(listensocketset) SDLNet_TCP_DelSocket(listensocketset,mysock); |
| 168 | SDLNet_TCP_Close(mysock); |
| 169 | } |
| 170 | |
| 171 | if(listensocketset) SDLNet_FreeSocketSet(listensocketset); |
| 172 | } |
| 173 | bool TCPClientSocket::GetRemoteAddressString(Bit8u* buffer) { |
| 174 | IPaddress* remote_ip; |
| 175 | Bit8u b1, b2, b3, b4; |
| 176 | remote_ip=SDLNet_TCP_GetPeerAddress(mysock); |
| 177 | if(!remote_ip) return false; |
| 178 | b4=remote_ip->host>>24; |
| 179 | b3=(remote_ip->host>>16)&0xff; |
| 180 | b2=(remote_ip->host>>8)&0xff; |
| 181 | b1=remote_ip->host&0xff; |
| 182 | sprintf((char*)buffer,"%u.%u.%u.%u",b1,b2,b3,b4); |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool TCPClientSocket::ReceiveArray(Bit8u* data, Bitu* size) { |
| 187 | if(SDLNet_CheckSockets(listensocketset,0)) |
| 188 | { |
| 189 | Bits retval = SDLNet_TCP_Recv(mysock, data, *size); |
| 190 | if(retval<1) { |
| 191 | isopen=false; |
| 192 | *size=0; |
| 193 | return false; |
| 194 | } else { |
| 195 | *size=retval; |
| 196 | return true; |
| 197 | } |
| 198 | } |
| 199 | else { |
| 200 | *size=0; |
| 201 | return true; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | |
| 206 | Bits TCPClientSocket::GetcharNonBlock() { |
| 207 | // return: |
| 208 | // -1: no data |
| 209 | // -2: socket closed |
| 210 | // 0..255: data |
| 211 | if(SDLNet_CheckSockets(listensocketset,0)) |
| 212 | { |
| 213 | Bitu retval =0; |
| 214 | if(SDLNet_TCP_Recv(mysock, &retval, 1)!=1) { |
| 215 | isopen=false; |
| 216 | return -2; |
| 217 | } else return retval; |
| 218 | } |
| 219 | else return -1; |
| 220 | } |
| 221 | bool TCPClientSocket::Putchar(Bit8u data) { |
| 222 | if(SDLNet_TCP_Send(mysock, &data, 1)!=1) { |
| 223 | isopen=false; |
| 224 | return false; |
| 225 | } |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | bool TCPClientSocket::SendArray(Bit8u* data, Bitu bufsize) { |
| 230 | if(SDLNet_TCP_Send(mysock, data, bufsize)!=bufsize) { |
| 231 | isopen=false; |
| 232 | return false; |
| 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | bool TCPClientSocket::SendByteBuffered(Bit8u data) { |
| 238 | |
| 239 | if(sendbufferindex==(sendbuffersize-1)) { |
| 240 | // buffer is full, get rid of it |
| 241 | sendbuffer[sendbufferindex]=data; |
| 242 | sendbufferindex=0; |
| 243 | |
| 244 | if(SDLNet_TCP_Send(mysock, sendbuffer, sendbuffersize)!=sendbuffersize) { |
| 245 | isopen=false; |
| 246 | return false; |
| 247 | } |
| 248 | } else { |
| 249 | sendbuffer[sendbufferindex]=data; |
| 250 | sendbufferindex++; |
| 251 | } |
| 252 | return true; |
| 253 | } |
| 254 | /* |
| 255 | bool TCPClientSocket::SendArrayBuffered(Bit8u* data, Bitu bufsize) { |
| 256 | |
| 257 | Bitu bytes |
| 258 | while( |
| 259 | |
| 260 | // first case, buffer already full |
| 261 | if(sendbufferindex==(sendbuffersize-1)) { |
| 262 | // buffer is full, get rid of it |
| 263 | sendbuffer[sendbufferindex]=data; |
| 264 | sendbufferindex=0; |
| 265 | |
| 266 | if(SDLNet_TCP_Send(mysock, sendbuffer, sendbuffersize)!=sendbuffersize) { |
| 267 | isopen=false; |
| 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | */ |
| 273 | void TCPClientSocket::FlushBuffer() { |
| 274 | if(sendbufferindex) { |
| 275 | if(SDLNet_TCP_Send(mysock, sendbuffer, |
| 276 | sendbufferindex)!=sendbufferindex) { |
| 277 | isopen=false; |
| 278 | return; |
| 279 | } |
| 280 | sendbufferindex=0; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void TCPClientSocket::SetSendBufferSize(Bitu bufsize) { |
| 285 | if(sendbuffer) delete [] sendbuffer; |
| 286 | sendbuffer = new Bit8u[bufsize]; |
| 287 | sendbuffersize=bufsize; |
| 288 | sendbufferindex=0; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | TCPServerSocket::TCPServerSocket(Bit16u port) |
| 293 | { |
| 294 | isopen = false; |
| 295 | mysock = 0; |
| 296 | if(!SDLNetInited) { |
| 297 | if(SDLNet_Init()==-1) { |
| 298 | LOG_MSG("SDLNet_Init failed: %s\n", SDLNet_GetError()); |
| 299 | return; |
| 300 | } |
| 301 | SDLNetInited = true; |
| 302 | } |
| 303 | if (port) { |
| 304 | IPaddress listen_ip; |
| 305 | SDLNet_ResolveHost(&listen_ip, NULL, port); |
| 306 | mysock=SDLNet_TCP_Open(&listen_ip); |
| 307 | if(!mysock) return; |
| 308 | } |
| 309 | else return; |
| 310 | isopen = true; |
| 311 | } |
| 312 | |
| 313 | TCPServerSocket::~TCPServerSocket() { |
| 314 | if(mysock) SDLNet_TCP_Close(mysock); |
| 315 | } |
| 316 | |
| 317 | TCPClientSocket* TCPServerSocket::Accept() { |
| 318 | |
| 319 | TCPsocket new_tcpsock; |
| 320 | |
| 321 | new_tcpsock=SDLNet_TCP_Accept(mysock); |
| 322 | if(!new_tcpsock) { |
| 323 | //printf("SDLNet_TCP_Accept: %s\n", SDLNet_GetError()); |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | return new TCPClientSocket(new_tcpsock); |
| 328 | } |
| 329 | #endif // #if C_MODEM |