10 #include <sys/utsname.h> 12 struct utsname my_utsname;
21 #include "../clib/clib.h" 22 #include "../clib/clib_endian.h" 23 #include "../clib/logfacility.h" 24 #include "../clib/strutil.h" 46 struct sockaddr_in server;
48 struct hostent* he = gethostbyname( hostname );
49 for (
int i = 0; ( he != nullptr ) && ( he->h_addr_list[i] !=
nullptr ); ++i )
51 memcpy( &server.sin_addr, he->h_addr_list[i], he->h_length );
53 const in_addr ad = server.sin_addr;
54 const char* adstr = inet_ntoa( ad );
58 const unsigned long ip = ad.S_un.S_addr;
60 const unsigned long ip = ad.s_addr;
63 if ( ( ip & 0x0000ffff ) == 0x0000a8c0 ||
64 ( ip & 0x0000f0ff ) == 0x000010ac ||
65 ( ip & 0x000000ff ) == 0x0000000a )
70 else if ( ( ip & 0x000000ff ) == 0x0000007f )
84 #define WSOCK_VERSION 0x0101 93 res = WSAStartup( WSOCK_VERSION, &wsa_data );
96 POLLOG_ERROR <<
"Error starting Winsock 1.1: " << res <<
"\n";
108 uname( &my_utsname );
124 POLLOG_ERROR <<
"Error stopping Winsock 1.1: " << res <<
"\n";
135 setsockopt( sck, IPPROTO_TCP, TCP_NODELAY, (
const char*)&tcp_nodelay,
sizeof( tcp_nodelay ) );
138 throw std::runtime_error(
"Unable to setsockopt (TCP_NODELAY) on listening socket, res=" +
146 u_long nonblocking = 1;
147 int res = ioctlsocket( sck, FIONBIO, &nonblocking );
149 int flags = fcntl( sck, F_GETFL );
151 int res = fcntl( sck, F_SETFL, flags );
155 throw std::runtime_error(
"Unable to set socket to nonblocking mode, res=" +
164 sck = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
167 throw std::runtime_error(
"Unable to create listening socket" );
174 res = setsockopt( sck, SOL_SOCKET, SO_REUSEADDR, (
const char*)&reuse_opt,
sizeof( reuse_opt ) );
177 throw std::runtime_error(
"Unable to setsockopt (SO_REUSEADDR) on listening socket, res = " +
182 #define DISABLE_NAGLE_ALGORITHM 0 183 #if DISABLE_NAGLE_ALGORITHM 187 struct sockaddr_in connection;
188 connection.sin_family = AF_INET;
189 connection.sin_addr.s_addr = INADDR_ANY;
190 connection.sin_port =
ctBEu16( port );
191 memset( connection.sin_zero, 0,
sizeof( connection.sin_zero ) );
193 res = bind( sck, (
struct sockaddr*)&connection,
sizeof connection );
198 std::string tmp_error =
"Unable to bind listening socket. Port(" +
Clib::decint( port ) +
200 throw std::runtime_error( tmp_error );
203 res = listen( sck, SOMAXCONN );
206 throw std::runtime_error(
"Listen failed, res=" +
Clib::decint( res ) );
214 #if 0 && defined( _WIN32 ) 216 static char buf[ 80 ];
217 DWORD len =
sizeof buf;
218 if (WSAAddressToString( addr,
sizeof *addr,
220 buf, &len ) != SOCKET_ERROR)
226 return "(display error)";
229 struct sockaddr_in* in_addr = (
struct sockaddr_in*)addr;
230 if ( addr->sa_family == AF_INET )
231 return inet_ntoa( in_addr->sin_addr );
233 return "(display error)";
240 listen_timeout( {0, 0} ),
void set_ip_address(const char *ip)
void apply_socket_options(SOCKET sck)
std::string decint(unsigned short v)
void set_lan_address(const char *ip)
typedef DWORD(WINAPI *__SymGetOptions)(VOID)
int init_sockets_library(void)
int deinit_sockets_library(void)
NetworkManager networkManager
const char * AddressToString(struct sockaddr *addr)
SOCKET open_listen_socket(unsigned short port)
void disable_nagle(SOCKET sck)
struct timeval select_timeout
void search_name(const char *hostname)