| 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: ipxserver.cpp,v 1.10 2009-05-27 09:15:41 qbix79 Exp $ */ |
| 20 |
|
| 21 |
#include "dosbox.h" |
| 22 |
|
| 23 |
#if C_IPX |
| 24 |
|
| 25 |
#include "dosbox.h" |
| 26 |
#include "ipxserver.h" |
| 27 |
#include "timer.h" |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
#include "ipx.h" |
| 31 |
|
| 32 |
IPaddress ipxServerIp; // IPAddress for server's listening port |
| 33 |
UDPsocket ipxServerSocket; // Listening server socket |
| 34 |
|
| 35 |
packetBuffer connBuffer[SOCKETTABLESIZE]; |
| 36 |
|
| 37 |
Bit8u inBuffer[IPXBUFFERSIZE]; |
| 38 |
IPaddress ipconn[SOCKETTABLESIZE]; // Active TCP/IP connection |
| 39 |
UDPsocket tcpconn[SOCKETTABLESIZE]; // Active TCP/IP connections |
| 40 |
SDLNet_SocketSet serverSocketSet; |
| 41 |
TIMER_TickHandler* serverTimer; |
| 42 |
|
| 43 |
Bit8u packetCRC(Bit8u *buffer, Bit16u bufSize) { |
| 44 |
Bit8u tmpCRC = 0; |
| 45 |
Bit16u i; |
| 46 |
for(i=0;i<bufSize;i++) { |
| 47 |
tmpCRC ^= *buffer; |
| 48 |
buffer++; |
| 49 |
} |
| 50 |
return tmpCRC; |
| 51 |
} |
| 52 |
|
| 53 |
/* |
| 54 |
static void closeSocket(Bit16u sockidx) { |
| 55 |
Bit32u host; |
| 56 |
|
| 57 |
host = ipconn[sockidx].host; |
| 58 |
LOG_MSG("IPXSERVER: %d.%d.%d.%d disconnected", CONVIP(host)); |
| 59 |
|
| 60 |
SDLNet_TCP_DelSocket(serverSocketSet,tcpconn[sockidx]); |
| 61 |
SDLNet_TCP_Close(tcpconn[sockidx]); |
| 62 |
connBuffer[sockidx].connected = false; |
| 63 |
connBuffer[sockidx].waitsize = false; |
| 64 |
} |
| 65 |
*/ |
| 66 |
|
| 67 |
static void sendIPXPacket(Bit8u *buffer, Bit16s bufSize) { |
| 68 |
Bit16u srcport, destport; |
| 69 |
Bit32u srchost, desthost; |
| 70 |
Bit16u i; |
| 71 |
Bits result; |
| 72 |
UDPpacket outPacket; |
| 73 |
outPacket.channel = -1; |
| 74 |
outPacket.data = buffer; |
| 75 |
outPacket.len = bufSize; |
| 76 |
outPacket.maxlen = bufSize; |
| 77 |
IPXHeader *tmpHeader; |
| 78 |
tmpHeader = (IPXHeader *)buffer; |
| 79 |
|
| 80 |
srchost = tmpHeader->src.addr.byIP.host; |
| 81 |
desthost = tmpHeader->dest.addr.byIP.host; |
| 82 |
|
| 83 |
srcport = tmpHeader->src.addr.byIP.port; |
| 84 |
destport = tmpHeader->dest.addr.byIP.port; |
| 85 |
|
| 86 |
|
| 87 |
if(desthost == 0xffffffff) { |
| 88 |
// Broadcast |
| 89 |
for(i=0;i<SOCKETTABLESIZE;i++) { |
| 90 |
if(connBuffer[i].connected && ((ipconn[i].host != srchost)||(ipconn[i].port!=srcport))) { |
| 91 |
outPacket.address = ipconn[i]; |
| 92 |
result = SDLNet_UDP_Send(ipxServerSocket,-1,&outPacket); |
| 93 |
if(result == 0) { |
| 94 |
LOG_MSG("IPXSERVER: %s", SDLNet_GetError()); |
| 95 |
continue; |
| 96 |
} |
| 97 |
//LOG_MSG("IPXSERVER: Packet of %d bytes sent from %d.%d.%d.%d to %d.%d.%d.%d (BROADCAST) (%x CRC)", bufSize, CONVIP(srchost), CONVIP(ipconn[i].host), packetCRC(&buffer[30], bufSize-30)); |
| 98 |
} |
| 99 |
} |
| 100 |
} else { |
| 101 |
// Specific address |
| 102 |
for(i=0;i<SOCKETTABLESIZE;i++) { |
| 103 |
if((connBuffer[i].connected) && (ipconn[i].host == desthost) && (ipconn[i].port == destport)) { |
| 104 |
outPacket.address = ipconn[i]; |
| 105 |
result = SDLNet_UDP_Send(ipxServerSocket,-1,&outPacket); |
| 106 |
if(result == 0) { |
| 107 |
LOG_MSG("IPXSERVER: %s", SDLNet_GetError()); |
| 108 |
continue; |
| 109 |
} |
| 110 |
//LOG_MSG("IPXSERVER: Packet sent from %d.%d.%d.%d to %d.%d.%d.%d", CONVIP(srchost), CONVIP(desthost)); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
bool IPX_isConnectedToServer(Bits tableNum, IPaddress ** ptrAddr) { |
| 121 |
if(tableNum >= SOCKETTABLESIZE) return false; |
| 122 |
*ptrAddr = &ipconn[tableNum]; |
| 123 |
return connBuffer[tableNum].connected; |
| 124 |
} |
| 125 |
|
| 126 |
static void ackClient(IPaddress clientAddr) { |
| 127 |
IPXHeader regHeader; |
| 128 |
UDPpacket regPacket; |
| 129 |
Bits result; |
| 130 |
|
| 131 |
SDLNet_Write16(0xffff, regHeader.checkSum); |
| 132 |
SDLNet_Write16(sizeof(regHeader), regHeader.length); |
| 133 |
|
| 134 |
SDLNet_Write32(0, regHeader.dest.network); |
| 135 |
PackIP(clientAddr, ®Header.dest.addr.byIP); |
| 136 |
SDLNet_Write16(0x2, regHeader.dest.socket); |
| 137 |
|
| 138 |
SDLNet_Write32(1, regHeader.src.network); |
| 139 |
PackIP(ipxServerIp, ®Header.src.addr.byIP); |
| 140 |
SDLNet_Write16(0x2, regHeader.src.socket); |
| 141 |
regHeader.transControl = 0; |
| 142 |
|
| 143 |
regPacket.data = (Uint8 *)®Header; |
| 144 |
regPacket.len = sizeof(regHeader); |
| 145 |
regPacket.maxlen = sizeof(regHeader); |
| 146 |
regPacket.address = clientAddr; |
| 147 |
// Send registration string to client. If client doesn't get this, client will not be registered |
| 148 |
result = SDLNet_UDP_Send(ipxServerSocket,-1,®Packet); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
static void IPX_ServerLoop() { |
| 153 |
UDPpacket inPacket; |
| 154 |
IPaddress tmpAddr; |
| 155 |
|
| 156 |
//char regString[] = "IPX Register\0"; |
| 157 |
|
| 158 |
Bit16u i; |
| 159 |
Bit32u host; |
| 160 |
Bits result; |
| 161 |
|
| 162 |
inPacket.channel = -1; |
| 163 |
inPacket.data = &inBuffer[0]; |
| 164 |
inPacket.maxlen = IPXBUFFERSIZE; |
| 165 |
|
| 166 |
|
| 167 |
result = SDLNet_UDP_Recv(ipxServerSocket, &inPacket); |
| 168 |
if (result != 0) { |
| 169 |
// Check to see if incoming packet is a registration packet |
| 170 |
// For this, I just spoofed the echo protocol packet designation 0x02 |
| 171 |
IPXHeader *tmpHeader; |
| 172 |
tmpHeader = (IPXHeader *)&inBuffer[0]; |
| 173 |
|
| 174 |
// Check to see if echo packet |
| 175 |
if(SDLNet_Read16(tmpHeader->dest.socket) == 0x2) { |
| 176 |
// Null destination node means its a server registration packet |
| 177 |
if(tmpHeader->dest.addr.byIP.host == 0x0) { |
| 178 |
UnpackIP(tmpHeader->src.addr.byIP, &tmpAddr); |
| 179 |
for(i=0;i<SOCKETTABLESIZE;i++) { |
| 180 |
if(!connBuffer[i].connected) { |
| 181 |
// Use prefered host IP rather than the reported source IP |
| 182 |
// It may be better to use the reported source |
| 183 |
ipconn[i] = inPacket.address; |
| 184 |
|
| 185 |
connBuffer[i].connected = true; |
| 186 |
host = ipconn[i].host; |
| 187 |
LOG_MSG("IPXSERVER: Connect from %d.%d.%d.%d", CONVIP(host)); |
| 188 |
ackClient(inPacket.address); |
| 189 |
return; |
| 190 |
} else { |
| 191 |
if((ipconn[i].host == tmpAddr.host) && (ipconn[i].port == tmpAddr.port)) { |
| 192 |
|
| 193 |
LOG_MSG("IPXSERVER: Reconnect from %d.%d.%d.%d", CONVIP(tmpAddr.host)); |
| 194 |
// Update anonymous port number if changed |
| 195 |
ipconn[i].port = inPacket.address.port; |
| 196 |
ackClient(inPacket.address); |
| 197 |
return; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// IPX packet is complete. Now interpret IPX header and send to respective IP address |
| 206 |
sendIPXPacket((Bit8u *)inPacket.data, inPacket.len); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
void IPX_StopServer() { |
| 211 |
TIMER_DelTickHandler(&IPX_ServerLoop); |
| 212 |
SDLNet_UDP_Close(ipxServerSocket); |
| 213 |
} |
| 214 |
|
| 215 |
bool IPX_StartServer(Bit16u portnum) { |
| 216 |
Bit16u i; |
| 217 |
|
| 218 |
if(!SDLNet_ResolveHost(&ipxServerIp, NULL, portnum)) { |
| 219 |
|
| 220 |
//serverSocketSet = SDLNet_AllocSocketSet(SOCKETTABLESIZE); |
| 221 |
ipxServerSocket = SDLNet_UDP_Open(portnum); |
| 222 |
if(!ipxServerSocket) return false; |
| 223 |
|
| 224 |
for(i=0;i<SOCKETTABLESIZE;i++) connBuffer[i].connected = false; |
| 225 |
|
| 226 |
TIMER_AddTickHandler(&IPX_ServerLoop); |
| 227 |
return true; |
| 228 |
} |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
#endif |