#ifndef __pxx_sock_addr_ih__ #define __pxx_sock_addr_ih__ #include "pxx_sock_addr.hh" #include "pxx_default_allocator.ih" #include #include namespace pxx { inline SockAddr::SockAddr (char const* _host, uint16_t _port) { struct hostent* he; he = gethostbyname(_host); if (he == null) { switch (h_errno) { case HOST_NOT_FOUND: throw HostNotFound(); case NO_ADDRESS: throw NoAddress(); case NO_RECOVERY: throw ServerError(); case TRY_AGAIN: throw TryAgain(); default: throw UnknownError(); } } switch (he->h_addrtype) { case AF_INET: { family = af_inet; struct sockaddr_in* p = (struct sockaddr_in*)default_allocator.allocate(sizeof(sockaddr_in)); p->sin_family = AF_INET; p->sin_port = htons(_port); p->sin_addr = *((struct in_addr*)(he->h_addr)); addr = (struct sockaddr*)p; addrlen = sizeof(struct sockaddr_in); } break; case AF_INET6: family = af_inet6; default: throw AF_NotSupported(); } } inline SockAddr::SockAddr (address_family_t _af, uint16_t _port /* = 0 */) : family (_af) { switch (_af) { case AF_INET: { struct sockaddr_in* p = (struct sockaddr_in*)default_allocator.allocate(sizeof(sockaddr_in)); p->sin_family = AF_INET; p->sin_port = htons(_port); p->sin_addr.s_addr = htonl(INADDR_ANY); addr = (struct sockaddr*)p; addrlen = sizeof(struct sockaddr_in); } break; case AF_INET6: default: throw AF_NotSupported(); } } inline SockAddr::~SockAddr () { // PMETHOD; default_allocator.deallocate(addr); } } #endif // __pxx_sock_addr_ih__