diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /support/dktools/dk4sock32.ctr |
Initial commit
Diffstat (limited to 'support/dktools/dk4sock32.ctr')
-rw-r--r-- | support/dktools/dk4sock32.ctr | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/support/dktools/dk4sock32.ctr b/support/dktools/dk4sock32.ctr new file mode 100644 index 0000000000..686db27b73 --- /dev/null +++ b/support/dktools/dk4sock32.ctr @@ -0,0 +1,117 @@ +%% options + +copyright owner = Dirk Krause +copyright year = 2016-xxxx +license = bsd + + + +%% module + +#include "dk4conf.h" +#include "dk4types.h" +#include "dk4mem.h" +#include "dk4sock.h" +#include "dk4mai8dus.h" +#include "dk4mao8d.h" + + + +$!trace-include + + + +#if DK4_HAVE_STRUCT_SOCKADDR_IN6 +/** used to initialize IPv6 address for binding local ports. +*/ +static const IN6_ADDR dk4socket_any6 = IN6ADDR_ANY_INIT; +#endif + + + +int +dk4socket_address_family(dk4_sockaddr_storage_t *aptr) +{ + struct sockaddr *sa = NULL; + int back = AF_UNSPEC; + if (NULL != aptr) { + sa = (struct sockaddr *)aptr; + switch (sa->sa_family) { + case AF_INET : +#if DK4_HAVE_STRUCT_SOCKADDR_IN6 + case AF_INET6 : +#endif + { + back = sa->sa_family; + } break; + } + } + return back; +} + + +size_t +dk4socket_address_size(dk4_sockaddr_storage_t *aptr) +{ + struct sockaddr *sa; + size_t back = 0; + + if (NULL != aptr) { + sa = (struct sockaddr *)aptr; + switch (sa->sa_family) { + case AF_INET : { + back = sizeof(struct sockaddr_in); + } break; +#if DK4_HAVE_STRUCT_SOCKADDR_IN6 + case AF_INET6 : { + back = sizeof(struct sockaddr_in6); + } break; +#endif + } + } + return back; +} + + + +int +dk4socket_complete_addresses( + dk4_sockaddr_storage_t *array, + unsigned short remport, + unsigned short locport +) +{ +#if DK4_HAVE_STRUCT_SOCKADDR_IN6 + struct sockaddr_in6 *sa6; +#endif + struct sockaddr_in *sa4; + struct sockaddr *sa; + int back = DK4_SOCKET_RESULT_FAILED; + $? "+ dk4socket_complete_addresses" + sa = (struct sockaddr *)array; + switch (sa->sa_family) { + case AF_INET : { $? ". IPv4" + sa4 = (struct sockaddr_in *)array; + sa4->sin_port = dk4socket_htons(remport); + sa4 = (struct sockaddr_in *)(&(array[1])); + sa4->sin_family = AF_INET; + sa4->sin_addr.s_addr = dk4socket_htonl(INADDR_ANY); + sa4->sin_port = dk4socket_htons(locport); + back = DK4_SOCKET_RESULT_SUCCESS; + } break; +#if DK4_HAVE_STRUCT_SOCKADDR_IN6 + case AF_INET6 : { $? ". IPv6" + sa6 = (struct sockaddr_in6 *)array; + sa6->sin6_port = dk4socket_htons(remport); + sa6 = (struct sockaddr_in6 *)(&(array[1])); + sa6->sin6_family = AF_INET6; + DK4_MEMCPY(&(sa6->sin6_addr), &dk4socket_any6, sizeof(IN6_ADDR)); + sa6->sin6_port = dk4socket_htons(locport); + back = DK4_SOCKET_RESULT_SUCCESS; + } break; +#endif + } + $? "- dk4socket_complete_addresses %d", back + return back; +} + |