/* 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: dk4sock02.ctr */ /* Copyright (C) 2015-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. */ #line 8 "dk4sock02.ctr" /** @file dk4sock01.c Conversion between host and net representation. */ #include "dk4types.h" #include "dk4error.h" #include "dk4sock.h" #if DK4_HAVE_STRINGS_H #ifndef STRINGS_H_INCLUDED #include #define STRINGS_H_INCLUDED 1 #endif #endif #if (!DK4_ON_WINDOWS) \ && (DK4_HAVE_HTONS || DK4_HAVE_NTOHS || DK4_HAVE_HTONL || DK4_HAVE_NTOHL) #if DK4_HAVE_ARPA_INET_H #ifndef ARPA_INET_H_INCLUDED #include #define ARPA_INET_H_INCLUDED 1 #endif #endif #if DK4_HAVE_NETINET_IN_H #ifndef NETINET_IN_H_INCLUDED #include #define NETINET_IN_H_INCLUDED 1 #endif #endif #endif #if DK4_ON_WINDOWS || (!DK4_HAVE_HTONS) || (!DK4_HAVE_NTOHS) #if !DK4_WORDS_BIGENDIAN static uint16_t dk4socket_short_swap(uint16_t u) { return ( ((u >> 8) & 0x00FFU) | ((u << 8) & 0xFF00U) ); } #endif #endif #if DK4_ON_WINDOWS || (!DK4_HAVE_HTONL) || (!DK4_HAVE_NTOHL) #if !DK4_WORDS_BIGENDIAN static uint32_t dk4socket_long_swap(uint32_t u) { return ( ( (u >> 24) & 0x000000FFUL) | ((u >> 8) & 0x0000FF00UL) | ((u << 8) & 0x00FF0000UL) | ((u << 24) & 0xFF000000UL) ); } #endif #endif uint16_t dk4socket_htons(uint16_t u) { #if DK4_HAVE_HTONS && (!DK4_ON_WINDOWS) return (htons(u)); #else #if DK4_WORDS_BIGENDIAN return u; #else return (dk4socket_short_swap(u)); #endif #endif } uint16_t dk4socket_ntohs(uint16_t u) { #if DK4_HAVE_NTOHS && (!DK4_ON_WINDOWS) return (ntohs(u)); #else #if DK4_WORDS_BIGENDIAN return u; #else return (dk4socket_short_swap(u)); #endif #endif } uint32_t dk4socket_htonl(uint32_t u) { #if DK4_HAVE_HTONL && (!DK4_ON_WINDOWS) return (htonl(u)); #else #if DK4_WORDS_BIGENDIAN return u; #else return (dk4socket_long_swap(u)); #endif #endif } uint32_t dk4socket_ntohl(uint32_t u) { #if DK4_HAVE_NTOHL && (!DK4_ON_WINDOWS) return (ntohl(u)); #else #if DK4_WORDS_BIGENDIAN return u; #else return (dk4socket_long_swap(u)); #endif #endif } void dk4socket_swap_bytes_if_not_bigendian(void *buf, size_t sz) { #if !DK4_WORDS_BIGENDIAN unsigned char *ucp; /* Buffer address converted ty bytes pointer */ size_t szh; /* Half of the size sz */ size_t i; /* Current byte index, 0 to szh-1 */ unsigned char u; /* Byte variable used to swap bytes */ if ((NULL != buf) && (0 < sz)) { ucp = (unsigned char *)buf; szh = sz / 2; for (i = 0; i < szh; i++) { u = ucp[i]; ucp[i] = ucp[sz - 1 - i]; ucp[sz - 1 - i] = u; } } #endif } int dk4socket_mask_bits(unsigned long *dptr, size_t sz, dk4_er_t *erp) { unsigned long res = 0UL; unsigned long val = 0x80000000UL; int back = DK4_SOCKET_RESULT_FAILED; if (NULL != dptr) { if ((0 < sz) && (33 > sz)) { while (0 < sz--) { res |= val; val = val / 2UL; } *dptr = res; back = DK4_SOCKET_RESULT_SUCCESS; } else { dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; } #if DK4_HAVE_STRUCT_SOCKADDR_IN6 int dk4socket_mask6_bits(struct in6_addr *inp, size_t sz, dk4_er_t *erp) { int back = DK4_SOCKET_RESULT_FAILED; size_t i = 0; size_t j = 0; unsigned char rchr = 0x00; unsigned char vchr = 0x80; if (NULL != inp) { for (i = 0; i < 16; i++) { inp->s6_addr[i] = 0xFF; } if ((0 < sz) && (129 > sz)) { for (i = 0; i < 16; i++) { rchr = 0x00; vchr = 0x80; for (j = 0; j < 8; j++) { if (0 < sz) { rchr |= vchr; sz--; } vchr = vchr / 2; } inp->s6_addr[i] = rchr; } back = DK4_SOCKET_RESULT_SUCCESS; } else { dk4error_set_simple_error_code(erp, DK4_E_SYNTAX); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; } #endif void dk4socket_set_one_bytes(void *ptr, size_t nbytes) { unsigned char *cptr; cptr = ptr; while (0 < nbytes--) { *(cptr++) = 0x01; } } #if DK4_HAVE_STRUCT_SOCKADDR_UN size_t dk4socket_unix_length(const struct sockaddr_un *sock) { size_t back = 0; if (NULL != sock) { #ifdef SUN_LEN back = SUN_LEN(sock); #else back = sizeof(struct sockaddr_un) - sizeof(sock->sun_path) + strlen(sock->sun_path); #endif } return back; } #endif int dk4socket_pf_for_af(int af) { int back = 0; switch (af) { #if defined(AF_UNSPEC) && defined(PF_UNSPEC) case AF_UNSPEC : { back = PF_UNSPEC ; } break; #endif #if defined(AF_UNIX) && defined(PF_UNIX) case AF_UNIX : { back = PF_UNIX ; } break; #endif #if defined(AF_LOCAL) && defined(PF_LOCAL) && \ (!(defined(AF_UNIX) && defined(PF_UNIX))) case AF_LOCAL : { back = PF_LOCAL ; } break; #endif #if defined(AF_FILE) && defined(PF_FILE) && \ (!(defined(AF_UNIX) && defined(PF_UNIX))) && \ (!(defined(AF_LOCAL) && defined(PF_LOCAL))) case AF_FILE : { back = PF_FILE ; } break; #endif #if defined(AF_INET) && defined(PF_INET) case AF_INET : { back = PF_INET ; } break; #endif #if defined(AF_AX25) && defined(PF_AX25) case AF_AX25 : { back = PF_AX25 ; } break; #endif #if defined(AF_IPX) && defined(PF_IPX) case AF_IPX : { back = PF_IPX ; } break; #endif #if defined(AF_APPLETALK) && defined(PF_APPLETALK) case AF_APPLETALK : { back = PF_APPLETALK ; } break; #endif #if defined(AF_NETROM) && defined(PF_NETROM) case AF_NETROM : { back = PF_NETROM ; } break; #endif #if defined(AF_BRIDGE) && defined(PF_BRIDGE) case AF_BRIDGE : { back = PF_BRIDGE ; } break; #endif #if defined(AF_ATMPVC) && defined(PF_ATMPVC) case AF_ATMPVC : { back = PF_ATMPVC ; } break; #endif #if defined(AF_X25) && defined(PF_X25) case AF_X25 : { back = PF_X25 ; } break; #endif #if defined(AF_INET6) && defined(PF_INET6) case AF_INET6 : { back = PF_INET6 ; } break; #endif #if defined(AF_ROSE) && defined(PF_ROSE) case AF_ROSE : { back = PF_ROSE ; } break; #endif #if defined(AF_DECnet) && defined(PF_DECnet) case AF_DECnet : { back = PF_DECnet ; } break; #endif #if defined(AF_NETBEUI) && defined(PF_NETBEUI) case AF_NETBEUI : { back = PF_NETBEUI ; } break; #endif #if defined(AF_SECURITY) && defined(PF_SECURITY) case AF_SECURITY : { back = PF_SECURITY ; } break; #endif #if defined(AF_KEY) && defined(PF_KEY) case AF_KEY : { back = PF_KEY ; } break; #endif #if defined(AF_NETLINK) && defined(PF_NETLINK) case AF_NETLINK : { back = PF_NETLINK ; } break; #endif #if defined(AF_ROUTE) && defined(PF_ROUTE) && \ (!(defined(AF_NETLINK) && defined(PF_NETLINK))) case AF_ROUTE : { back = PF_ROUTE ; } break; #endif #if defined(AF_PACKET) && defined(PF_PACKET) case AF_PACKET : { back = PF_PACKET ; } break; #endif #if defined(AF_ASH) && defined(PF_ASH) case AF_ASH : { back = PF_ASH ; } break; #endif #if defined(AF_ECONET) && defined(PF_ECONET) case AF_ECONET : { back = PF_ECONET ; } break; #endif #if defined(AF_ATMSVC) && defined(PF_ATMSVC) case AF_ATMSVC : { back = PF_ATMSVC ; } break; #endif #if defined(AF_RDS) && defined(PF_RDS) case AF_RDS : { back = PF_RDS ; } break; #endif #if defined(AF_SNA) && defined(PF_SNA) case AF_SNA : { back = PF_SNA ; } break; #endif #if defined(AF_IRDA) && defined(PF_IRDA) case AF_IRDA : { back = PF_IRDA ; } break; #endif #if defined(AF_PPPOX) && defined(PF_PPPOX) case AF_PPPOX : { back = PF_PPPOX ; } break; #endif #if defined(AF_WANPIPE) && defined(PF_WANPIPE) case AF_WANPIPE : { back = PF_WANPIPE ; } break; #endif #if defined(AF_LLC) && defined(PF_LLC) case AF_LLC : { back = PF_LLC ; } break; #endif #if defined(AF_CAN) && defined(PF_CAN) case AF_CAN : { back = PF_CAN ; } break; #endif #if defined(AF_TIPC) && defined(PF_TIPC) case AF_TIPC : { back = PF_TIPC ; } break; #endif #if defined(AF_BLUETOOTH) && defined(PF_BLUETOOTH) case AF_BLUETOOTH : { back = PF_BLUETOOTH ; } break; #endif #if defined(AF_IUCV) && defined(PF_IUCV) case AF_IUCV : { back = PF_IUCV ; } break; #endif #if defined(AF_RXRPC) && defined(PF_RXRPC) case AF_RXRPC : { back = PF_RXRPC ; } break; #endif #if defined(AF_ISDN) && defined(PF_ISDN) case AF_ISDN : { back = PF_ISDN ; } break; #endif #if defined(AF_PHONET) && defined(PF_PHONET) case AF_PHONET : { back = PF_PHONET ; } break; #endif #if defined(AF_IEEE802154) && defined(PF_IEEE802154) case AF_IEEE802154 : { back = PF_IEEE802154 ; } break; #endif #if defined(AF_CAIF) && defined(PF_CAIF) case AF_CAIF : { back = PF_CAIF ; } break; #endif #if defined(AF_ALG) && defined(PF_ALG) case AF_ALG : { back = PF_ALG ; } break; #endif #if defined(AF_NFC) && defined(PF_NFC) case AF_NFC : { back = PF_NFC ; } break; #endif #if defined(AF_VSOCK) && defined(PF_VSOCK) case AF_VSOCK : { back = PF_VSOCK ; } break; #endif } return back; } int dk4socket_correct_addr_lgt(const struct sockaddr *soa, size_t *plgt) { int back = 0; if ((NULL != soa) && (NULL != plgt)) { back = 1; switch (soa->sa_family) { case AF_INET : { if (sizeof(struct sockaddr_in) <= *plgt) { *plgt = sizeof(struct sockaddr_in); } else { back = 0; } } break; #if DK4_HAVE_STRUCT_SOCKADDR_IN6 case AF_INET6 : { if (sizeof(struct sockaddr_in6) <= *plgt) { *plgt = sizeof(struct sockaddr_in6); } else { back = 0; } } break; #endif } } return back; }