54#include <netinet/in.h>
59#define SA struct sockaddr
70 if ( a.s_addr == ntohl(INADDR_ANY) ) {
76 unsigned char s[
sizeof(a.s_addr)];
77 memcpy( s, &a.s_addr,
sizeof(a.s_addr) );
79 snprintf(output,
sizeof(HostAddress),
"%d.%d.%d.%d", s[0], s[1], s[2], s[3] );
92 snprintf( buffer,
sizeof(SockAddress),
"%s:%u",
108 struct sockaddr_in socket;
111 if ( (peer ? getpeername( fd, (
SA*) &socket, &len ) :
112 getsockname( fd, (
SA*) &socket, &len )) == -1 )
125 if ( host ==
nullptr )
return -1;
126 unsigned long int h = inet_addr(host);
127 if ( h == 0xFFFFFFFF && strncmp(host,
"255.255.255.255",15) != 0 ) {
129 struct hostent* dns = gethostbyname(host);
130 if ( dns ==
nullptr )
return -1;
131 else memcpy( &dst.s_addr, dns->h_addr, dns->h_length );
146 int p = strtoul(
port,
nullptr,0);
150 struct servent* proto = getservbyname(
port,
"tcp" );
151 if ( proto ==
nullptr )
return -1;
152 else dst = proto->s_port;
const char * my_inet_ntoa(const struct in_addr &a, HostAddress output)
int convertHostname(const char *host, in_addr &dst)
int convertPortname(const char *port, unsigned short &dst)
const char * my_sock_fd2a(int fd, SockAddress buffer, bool peer)
const char * my_sock_ntoa(const struct sockaddr_in &a, SockAddress buffer)