/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4sock34.ctr */ /* Copyright (C) 2016-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /** @file dk4sock34.c The dk4sock34 module. */ #line 10 "dk4sock34.ctr" #include "dk4conf.h" #include "dk4types.h" #include "dk4mem.h" #include "dk4sock.h" #include "dk4mai8dus.h" #include "dk4maowd.h" #include "dk4mao8d.h" #include "dk4rec24.h" #line 23 "dk4sock34.ctr" #if DK4_ON_WINDOWS /** Attempt to convert an IPv4 or IPv6 address from text to address. @param dptr Destination variable. @param str Source text. @return 1 on success, 0 on error. */ static int dk4socket_wc_find_address(dk4_sockaddr_storage_t *dptr, const wchar_t *str) { #if DK4_HAVE_STRUCT_SOCKADDR_IN6 struct sockaddr_in6 *in6; #endif struct sockaddr_in *in4; #if DK4_HAVE_STRUCT_SOCKADDR_IN6 IN6_ADDR addr6; #endif IN_ADDR addr4; int back = 0; int res; #line 48 "dk4sock34.ctr" res = dk4socket_wc_inet_pton(&addr4, sizeof(addr4), AF_INET, str, NULL); if (DK4_SOCKET_RESULT_SUCCESS == res) { in4 = (struct sockaddr_in *)dptr; in4->sin_family = AF_INET; DK4_MEMCPY(&(in4->sin_addr), &addr4, sizeof(addr4)); back = 1; } else { #if DK4_HAVE_STRUCT_SOCKADDR_IN6 res = dk4socket_wc_inet_pton(&addr6, sizeof(addr6), AF_INET6, str, NULL); if (DK4_SOCKET_RESULT_SUCCESS == res) { in6 = (struct sockaddr_in6 *)dptr; in6->sin6_family = AF_INET6; DK4_MEMCPY(&(in6->sin6_addr), &addr6, sizeof(addr6)); back = 1; } #endif } #line 66 "dk4sock34.ctr" return back; } /** Find address for one host name. @param dptr Address of destination data structure. @param remhost Remote host name. @param remport Remote port number in host representation. @param ip4 Flag: Prefer IPv4 over IPv6 addresses. @param erp Error report, may be NULL. @return 1 on success, 0 on error. */ static int dk4socket_wc_find_host( dk4_sockaddr_storage_t *dptr, const wchar_t *remhost, unsigned short remport, int ip4, dk4_er_t *erp ) { #if DK4_HAVE_GETADDRINFO /* +++ getaddrinfo +++ */ wchar_t buf[16*sizeof(unsigned short)]; ADDRINFOW hints; ADDRINFOW *results = NULL; ADDRINFOW *rptr = NULL; int back = 0; int res = 0; #line 96 "dk4sock34.ctr" res = dk4ma_write_wc_decimal_unsigned( buf, DK4_SIZEOF(buf,wchar_t), (dk4_um_t)remport, 0, NULL ); if (0 != res) { DK4_MEMRES(&hints, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = 0; hints.ai_flags = AI_NUMERICSERV; res = GetAddrInfoW(remhost, buf, &hints, &results); if ((0 == res) && (NULL != results)) { #line 107 "dk4sock34.ctr" if (0 != ip4) { rptr = results; while ((NULL != rptr) && (0 == back)) { if ((0 < rptr->ai_addrlen) && (NULL != rptr->ai_addr)) { if (AF_INET == rptr->ai_addr->sa_family) { if (sizeof(dk4_sockaddr_storage_t) >= rptr->ai_addrlen) { DK4_MEMCPY(dptr, rptr->ai_addr, rptr->ai_addrlen); back = 1; #line 115 "dk4sock34.ctr" } #if TRACE_DEBUG else { #line 118 "dk4sock34.ctr" } #endif } #if TRACE_DEBUG else { #line 123 "dk4sock34.ctr" } #endif } #if TRACE_DEBUG else { #line 128 "dk4sock34.ctr" } #endif rptr = rptr->ai_next; } if (0 == back) { rptr = results; while ((NULL != rptr) && (0 == back)) { if ((0 < rptr->ai_addrlen) && (NULL != rptr->ai_addr)) { if (sizeof(dk4_sockaddr_storage_t) >= rptr->ai_addrlen) { DK4_MEMCPY(dptr, rptr->ai_addr, rptr->ai_addrlen); back = 1; #line 139 "dk4sock34.ctr" } #if TRACE_DEBUG else { #line 142 "dk4sock34.ctr" } #endif } #if TRACE_DEBUG else { #line 147 "dk4sock34.ctr" } #endif rptr = rptr->ai_next; } } } else { #line 153 "dk4sock34.ctr" rptr = results; while ((NULL != rptr) && (0 == back)) { if ((0 < rptr->ai_addrlen) && (NULL != rptr->ai_addr)) { if (sizeof(dk4_sockaddr_storage_t) >= rptr->ai_addrlen) { DK4_MEMCPY(dptr, rptr->ai_addr, rptr->ai_addrlen); back = 1; #line 159 "dk4sock34.ctr" } #if TRACE_DEBUG else { #line 162 "dk4sock34.ctr" } #endif } #if TRACE_DEBUG else { #line 167 "dk4sock34.ctr" } #endif rptr = rptr->ai_next; } } } else { #line 173 "dk4sock34.ctr" } if (NULL != results) { FreeAddrInfoW(results); } } #if TRACE_DEBUG else { #line 178 "dk4sock34.ctr" } #endif #line 181 "dk4sock34.ctr" return back; /* --- getaddrinfo --- */ #else /* +++ ! getaddrinfo +++ */ #if DK4_HAVE_GETHOSTBYNAME /* +++ gethostbyname +++ */ char hona[1040]; #if DK4_HAVE_STRUCT_SOCKADDR_IN6 struct sockaddr_in6 *sa6; #endif struct sockaddr_in *sa4; struct hostent *hep = NULL; int back = 0; #line 195 "dk4sock34.ctr" if (0 != dk4recode_wchar_t_to_char(hona, sizeof(hona), remhost, erp)) { hep = gethostbyname(hona); if (NULL != hep) { #line 198 "dk4sock34.ctr" if (NULL != hep->h_addr_list) { #line 199 "dk4sock34.ctr" if (NULL != *(hep->h_addr_list)) { #line 200 "dk4sock34.ctr" switch (hep->h_addrtype) { case AF_INET : { #line 202 "dk4sock34.ctr" sa4 = (struct sockaddr_in *)dptr; DK4_MEMRES(sa4, sizeof(struct sockaddr_in)); sa4->sin_family = AF_INET; sa4->sin_port = dk4socket_htons(remport); DK4_MEMCPY(&(sa4->sin_addr),*(hep->h_addr_list),sizeof(IN_ADDR)); back = 1; #line 208 "dk4sock34.ctr" } break; #if DK4_HAVE_STRUCT_SOCKADDR_IN6 case AF_INET6 : { #line 211 "dk4sock34.ctr" sa6 = (struct sockaddr_in6 *)dptr; DK4_MEMRES(sa6, sizeof(struct sockaddr_in6)); sa6->sin6_family = AF_INET6; sa6->sin_port = dk4socket_htons(remport); DK4_MEMCPY(&(sa6->sin6_addr),*(hep->h_addr_list),sizeof(IN6_ADDR)); back = 1; #line 217 "dk4sock34.ctr" } break; #endif #if TRACE_DEBUG default : { #line 221 "dk4sock34.ctr" } break; #endif } } #if TRACE_DEBUG else { #line 227 "dk4sock34.ctr" } #endif } #if TRACE_DEBUG else { #line 232 "dk4sock34.ctr" } #endif } #if TRACE_DEBUG else { #line 237 "dk4sock34.ctr" } #endif } #if TRACE_DEBUG else { #line 242 "dk4sock34.ctr" } #endif #line 245 "dk4sock34.ctr" return back; /* --- gethostbyname --- */ #else /* +++ ! gethostbyname +++ */ dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); return 0; #line 251 "dk4sock34.ctr" /* --- ! gethostbyname --- */ #endif /* --- ! getaddrinfo --- */ #endif } #endif int dk4socket_wc_udp_addresses_numeric( dk4_sockaddr_storage_t *array, const wchar_t *remhost, unsigned short remport, unsigned short locport, int ip4, dk4_er_t *erp ) { #if DK4_ON_WINDOWS int back = DK4_SOCKET_RESULT_FAILED; #line 272 "dk4sock34.ctr" if ((NULL != array) && (NULL != remhost) && (0 < remport) && (0 < locport)) { DK4_MEMRES(array, (2*sizeof(dk4_sockaddr_storage_t))); if (0 != dk4socket_wc_find_address(array, remhost)) { #line 276 "dk4sock34.ctr" back = dk4socket_complete_addresses(array, remport, locport); #line 278 "dk4sock34.ctr" } if (DK4_SOCKET_RESULT_FAILED == back) { #line 280 "dk4sock34.ctr" if (0 != dk4socket_wc_find_host(array, remhost, remport, ip4, erp)) { back = dk4socket_complete_addresses(array, remport, locport); if (DK4_SOCKET_RESULT_FAILED == back) { #line 283 "dk4sock34.ctr" dk4error_set_simple_error_code(erp, DK4_E_BUG); } } else { #line 286 "dk4sock34.ctr" dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND); } } } else { #line 290 "dk4sock34.ctr" dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 293 "dk4sock34.ctr" return back; #else dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); return 0; #endif } int dk4socket_udp_addresses_numeric( dk4_sockaddr_storage_t *array, const dkChar *remhost, unsigned short remport, unsigned short locport, int ip4, dk4_er_t *erp ) { #if DK4_CHAR_SIZE > 1 return ( dk4socket_wc_udp_addresses_numeric( array, remhost, remport, locport, ip4, erp ) ); #else return ( dk4socket_c8_udp_addresses_numeric( array, remhost, remport, locport, ip4, erp ) ); #endif }