Files
@ r19774:47e5514dfea2
Branch filter:
Location: cpp/openttd-patchpack/source/src/network/core/tcp_listen.h - annotation
r19774:47e5514dfea2
4.9 KiB
text/x-c
(svn r24729) -Codechange: Unify the handling of HEBR_EDITING.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | r16345:eb75d2d8867a r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16344:08e6688931ee r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r17127:fe5262df76e1 r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16618:d89f0c3f42e7 r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d r16227:792ebc5cdc7d | /* $Id$ */
/*
* This file is part of OpenTTD.
* OpenTTD 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, version 2.
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file tcp_listen.h Basic functions to listen for TCP connections.
*/
#ifndef NETWORK_CORE_TCP_LISTEN_H
#define NETWORK_CORE_TCP_LISTEN_H
#include "tcp.h"
#include "../network.h"
#include "../../core/pool_type.hpp"
#include "../../debug.h"
#include "table/strings.h"
#ifdef ENABLE_NETWORK
/**
* Template for TCP listeners.
* @param Tsocket The class we create sockets for.
* @param Tfull_packet The packet type to return when we don't allow more sockets.
* @param Tban_packet The packet type to return when the client is banned.
*/
template <class Tsocket, PacketType Tfull_packet, PacketType Tban_packet>
class TCPListenHandler {
/** List of sockets we listen on. */
static SocketList sockets;
public:
/**
* Accepts clients from the sockets.
* @param ls Socket to accept clients from.
*/
static void AcceptClient(SOCKET ls)
{
for (;;) {
struct sockaddr_storage sin;
memset(&sin, 0, sizeof(sin));
socklen_t sin_len = sizeof(sin);
SOCKET s = accept(ls, (struct sockaddr*)&sin, &sin_len);
if (s == INVALID_SOCKET) return;
SetNonBlocking(s); // XXX error handling?
NetworkAddress address(sin, sin_len);
DEBUG(net, 1, "[%s] Client connected from %s on frame %d", Tsocket::GetName(), address.GetHostname(), _frame_counter);
SetNoDelay(s); // XXX error handling?
/* Check if the client is banned */
bool banned = false;
for (char **iter = _network_ban_list.Begin(); iter != _network_ban_list.End(); iter++) {
banned = address.IsInNetmask(*iter);
if (banned) {
Packet p(Tban_packet);
p.PrepareToSend();
DEBUG(net, 1, "[%s] Banned ip tried to join (%s), refused", Tsocket::GetName(), *iter);
send(s, (const char*)p.buffer, p.size, 0);
closesocket(s);
break;
}
}
/* If this client is banned, continue with next client */
if (banned) continue;
/* Can we handle a new client? */
if (!Tsocket::AllowConnection()) {
/* no more clients allowed?
* Send to the client that we are full! */
Packet p(Tfull_packet);
p.PrepareToSend();
send(s, (const char*)p.buffer, p.size, 0);
closesocket(s);
continue;
}
Tsocket::AcceptConnection(s, address);
}
}
/**
* Handle the receiving of packets.
* @return true if everything went okay.
*/
static bool Receive()
{
fd_set read_fd, write_fd;
struct timeval tv;
FD_ZERO(&read_fd);
FD_ZERO(&write_fd);
Tsocket *cs;
FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) {
FD_SET(cs->sock, &read_fd);
FD_SET(cs->sock, &write_fd);
}
/* take care of listener port */
for (SocketList::iterator s = sockets.Begin(); s != sockets.End(); s++) {
FD_SET(s->second, &read_fd);
}
tv.tv_sec = tv.tv_usec = 0; // don't block at all.
#if !defined(__MORPHOS__) && !defined(__AMIGA__)
select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv);
#else
WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL);
#endif
/* accept clients.. */
for (SocketList::iterator s = sockets.Begin(); s != sockets.End(); s++) {
if (FD_ISSET(s->second, &read_fd)) AcceptClient(s->second);
}
/* read stuff from clients */
FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) {
cs->writable = !!FD_ISSET(cs->sock, &write_fd);
if (FD_ISSET(cs->sock, &read_fd)) {
cs->ReceivePackets();
}
}
return _networking;
}
/**
* Listen on a particular port.
* @param port The port to listen on.
* @return true if listening succeeded.
*/
static bool Listen(uint16 port)
{
assert(sockets.Length() == 0);
NetworkAddressList addresses;
GetBindAddresses(&addresses, port);
for (NetworkAddress *address = addresses.Begin(); address != addresses.End(); address++) {
address->Listen(SOCK_STREAM, &sockets);
}
if (sockets.Length() == 0) {
DEBUG(net, 0, "[server] could not start network: could not create listening socket");
NetworkError(STR_NETWORK_ERROR_SERVER_START);
return false;
}
return true;
}
/** Close the sockets we're listening on. */
static void CloseListeners()
{
for (SocketList::iterator s = sockets.Begin(); s != sockets.End(); s++) {
closesocket(s->second);
}
sockets.Clear();
DEBUG(net, 1, "[%s] closed listeners", Tsocket::GetName());
}
};
template <class Tsocket, PacketType Tfull_packet, PacketType Tban_packet> SocketList TCPListenHandler<Tsocket, Tfull_packet, Tban_packet>::sockets;
#endif /* ENABLE_NETWORK */
#endif /* NETWORK_CORE_TCP_LISTEN_H */
|