-#define CHECK_ADDRESS_RETVAL(address, family, retval) \
-{ \
- if (address->m_family == wxSOCKET_NOFAMILY) \
- if (_GAddress_Init_##family(address) != wxSOCKET_NOERROR) \
- return retval; \
- if (address->m_family != wxSOCKET_##family) \
- { \
- address->m_error = wxSOCKET_INVADDR; \
- return retval; \
- } \
-}
-
-
-GAddress *GAddress_new(void)
-{
- GAddress *address;
-
- if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL)
- return NULL;
-
- address->m_family = wxSOCKET_NOFAMILY;
- address->m_addr = NULL;
- address->m_len = 0;
-
- return address;
-}
-
-GAddress *GAddress_copy(GAddress *address)
-{
- GAddress *addr2;
-
- assert(address != NULL);
-
- if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL)
- return NULL;
-
- memcpy(addr2, address, sizeof(GAddress));
-
- if (address->m_addr && address->m_len > 0)
- {
- addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
- if (addr2->m_addr == NULL)
- {
- free(addr2);
- return NULL;
- }
- memcpy(addr2->m_addr, address->m_addr, addr2->m_len);
- }
-
- return addr2;
-}
-
-void GAddress_destroy(GAddress *address)
-{
- assert(address != NULL);
-
- if (address->m_addr)
- free(address->m_addr);
-
- free(address);
-}
-
-void GAddress_SetFamily(GAddress *address, GAddressType type)
-{
- assert(address != NULL);
-
- address->m_family = type;
-}
-
-GAddressType GAddress_GetFamily(GAddress *address)
-{
- assert(address != NULL);
-
- return address->m_family;
-}
-
-wxSocketError _GAddress_translate_from(GAddress *address,
- struct sockaddr *addr, int len)
-{
- address->m_realfamily = addr->sa_family;
- switch (addr->sa_family)
- {
- case AF_INET:
- address->m_family = wxSOCKET_INET;
- break;
- case AF_UNIX:
- address->m_family = wxSOCKET_UNIX;
- break;
-#if wxUSE_IPV6
- case AF_INET6:
- address->m_family = wxSOCKET_INET6;
- break;
-#endif // wxUSE_IPV6
- default:
- {
- address->m_error = wxSOCKET_INVOP;
- return wxSOCKET_INVOP;
- }
- }
-
- if (address->m_addr)
- free(address->m_addr);
-
- address->m_len = len;
- address->m_addr = (struct sockaddr *)malloc(len);
-
- if (address->m_addr == NULL)
- {
- address->m_error = wxSOCKET_MEMERR;
- return wxSOCKET_MEMERR;
- }
-
- memcpy(address->m_addr, addr, len);
-
- return wxSOCKET_NOERROR;
-}
-
-wxSocketError _GAddress_translate_to(GAddress *address,
- struct sockaddr **addr, int *len)
-{
- if (!address->m_addr)
- {
- address->m_error = wxSOCKET_INVADDR;
- return wxSOCKET_INVADDR;
- }
-
- *len = address->m_len;
- *addr = (struct sockaddr *)malloc(address->m_len);
- if (*addr == NULL)
- {
- address->m_error = wxSOCKET_MEMERR;
- return wxSOCKET_MEMERR;
- }
-
- memcpy(*addr, address->m_addr, address->m_len);
- return wxSOCKET_NOERROR;
-}
-
-/*
- * -------------------------------------------------------------------------
- * Internet address family
- * -------------------------------------------------------------------------
- */
-
-wxSocketError _GAddress_Init_INET(GAddress *address)
-{
- address->m_len = sizeof(struct sockaddr_in);
- address->m_addr = (struct sockaddr *) malloc(address->m_len);
- if (address->m_addr == NULL)
- {
- address->m_error = wxSOCKET_MEMERR;
- return wxSOCKET_MEMERR;
- }
-
- address->m_family = wxSOCKET_INET;
- address->m_realfamily = PF_INET;
- ((struct sockaddr_in *)address->m_addr)->sin_family = AF_INET;
- ((struct sockaddr_in *)address->m_addr)->sin_addr.s_addr = INADDR_ANY;
-
- return wxSOCKET_NOERROR;
-}
-
-wxSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname)
-{
- struct hostent *he;
- struct in_addr *addr;
-
- assert(address != NULL);
-
- CHECK_ADDRESS(address, INET);
-
- addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
-
- /* If it is a numeric host name, convert it now */
-#if defined(HAVE_INET_ATON)
- if (inet_aton(hostname, addr) == 0)
- {
-#elif defined(HAVE_INET_ADDR)
- if ( (addr->s_addr = inet_addr(hostname)) == (unsigned)-1 )
- {
-#else
- /* Use gethostbyname by default */
-#ifndef __WXMAC__
- int val = 1; /* VA doesn't like constants in conditional expressions */
- if (val)
-#endif
- {
-#endif
- struct in_addr *array_addr;
-
- /* It is a real name, we solve it */
- struct hostent h;
-#if defined(HAVE_FUNC_GETHOSTBYNAME_R_3)
- struct hostent_data buffer;
-#else
- char buffer[1024];
-#endif
- int err;
- he = wxGethostbyname_r(hostname, &h, (void*)&buffer, sizeof(buffer), &err);
- if (he == NULL)
- {
- /* Reset to invalid address */
- addr->s_addr = INADDR_NONE;
- address->m_error = wxSOCKET_NOHOST;
- return wxSOCKET_NOHOST;
- }
-
- array_addr = (struct in_addr *) *(he->h_addr_list);
- addr->s_addr = array_addr[0].s_addr;
- }
-
- return wxSOCKET_NOERROR;
-}
-
-
-wxSocketError GAddress_INET_SetBroadcastAddress(GAddress *address)
-{
- return GAddress_INET_SetHostAddress(address, INADDR_BROADCAST);
-}
-
-wxSocketError GAddress_INET_SetAnyAddress(GAddress *address)
-{
- return GAddress_INET_SetHostAddress(address, INADDR_ANY);
-}
-
-wxSocketError GAddress_INET_SetHostAddress(GAddress *address,
- unsigned long hostaddr)
-{
- struct in_addr *addr;
-
- assert(address != NULL);
-
- CHECK_ADDRESS(address, INET);
-
- addr = &(((struct sockaddr_in *)address->m_addr)->sin_addr);
- addr->s_addr = htonl(hostaddr);
-
- return wxSOCKET_NOERROR;
-}
-
-wxSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
- const char *protocol)