/* Minetest-c55 Copyright (C) 2010 celeron55, Perttu Ahola This program 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; either version 2 of the License, or (at your option) any later version. This program 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 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "socket.h" #include "debug.h" #include #include #include #include #include "utility.h" // Debug printing options // Set to 1 for debug output #define DP 0 // This is prepended to everything printed here #define DPS "" bool g_sockets_initialized = false; void sockets_init() { #ifdef _WIN32 WSADATA WsaData; if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR) throw SocketException("WSAStartup failed"); #else #endif g_sockets_initialized = true; } void sockets_cleanup() { #ifdef _WIN32 WSACleanup(); #endif } Address::Address() { m_address = 0; m_port = 0; } Address::Address(unsigned int address, unsigned short port) { m_address = address; m_port = port; } Address::Address(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned short port) { m_address = (a<<24) | (b<<16) | ( c<<8) | d; m_port = port; } bool Address::operator==(Address &address) { return (m_address == address.m_address && m_port == address.m_port); } bool Address::operator!=(Address &address) { return !(*this == address); } void Address::Resolve(const char *name) { struct addrinfo *resolved; int e = getaddrinfo(name, NULL, NULL, &resolved); if(e != 0) throw ResolveError(""); /* FIXME: This is an ugly hack; change the whole class to store the address as sockaddr */ struct sockaddr_in *t = (struct sockaddr_in*)resolved->ai_addr; m_address = ntohl(t->sin_addr.s_addr); freeaddrinfo(resolved); } std::string Address::serializeString() const { unsigned int a, b, c, d; a = (m_address & 0xFF000000)>>24; b = (m_address & 0x00FF0000)>>16; c = (m_address & 0x0000FF00)>>8; d = (m_address & 0x000000FF); return itos(a)+"."+itos(b)+"."+itos(c)+"."+itos(d); } unsigned int Address::getAddress() const { return m_address; } unsigned short Address::getPort() const { return m_port; } void Address::setAddress(unsigned int address) { m_address = address; } void Address::setAddress(unsigned int a, unsigned int b, unsigned int c, unsigned int d) { m_address = (a<<24) | (b<<16) | ( c<<8) | d; } void Address::setPort(unsigned short port) { m_port = port; } void Address::print(std::ostream *s) const { (*s)<<((m_address>>24)&0xff)<<"." <<((m_address>>16)&0xff)<<"." <<((m_address>>8)&0xff)<<"." <<((m_address>>0)&0xff)<<":" < "; destination.print(); dstream<<", size="<20) dstream<<"..."; if(dumping_packet) dstream<<" (DUMPED BY INTERNET_SIMULATOR)"; dstream<20) dstream<<"..."; dstream<