/* 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: dk4sock23.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. */ /** @file dk4sock23.c The dk4sock23 module. */ #line 8 "dk4sock23.ctr" #include "dk4sock.h" #include "dk4mem.h" #line 16 "dk4sock23.ctr" dk4_socket_set_t * dk4socket_set_server_directly( unsigned short portno, int backlog, int local, dk4_er_t *erp ) { #if DK4_HAVE_STRUCT_SOCKADDR_IN6 struct sockaddr_in6 soin6; #endif struct sockaddr_in soin4; dk4_socket_set_t *back = NULL; size_t max; int res; #if DK4_HAVE_SETSOCKOPT && (defined(SO_REUSEPORT) || defined(SO_REUSEADDR)) int opt; #endif if (0 < portno) { max = 1; #if DK4_HAVE_STRUCT_SOCKADDR_IN6 max++; #endif back = dk4socket_set_new(max, erp); if (NULL != back) { /* IPv4 address */ DK4_MEMRES(&soin4, sizeof(soin4)); soin4.sin_family = AF_INET; soin4.sin_port = dk4socket_htons(portno); if (0 != local) { soin4.sin_addr.s_addr = dk4socket_htonl(INADDR_LOOPBACK); } else { soin4.sin_addr.s_addr = dk4socket_htonl(INADDR_ANY); } (back->pSockets)[back->szUsed] = dk4socket_open( AF_INET, SOCK_STREAM, 0, erp ); if (INVALID_SOCKET != (back->pSockets)[back->szUsed]) { #if DK4_HAVE_SETSOCKOPT && defined(SO_REUSEPORT) dk4socket_set_one_bytes(&opt, sizeof(opt)); res = setsockopt( (back->pSockets)[back->szUsed], SOL_SOCKET, SO_REUSEPORT, (void *)(&opt), sizeof(opt) ); if (0 != res) { #line 66 "dk4sock23.ctr" } #else #if DK4_HAVE_SETSOCKOPT && defined(SO_REUSEADDR) #line 70 "dk4sock23.ctr" dk4socket_set_one_bytes(&opt, sizeof(opt)); res = setsockopt( (back->pSockets)[back->szUsed], SOL_SOCKET, SO_REUSEADDR, (void *)(&opt), sizeof(opt) ); if (0 != res) { #line 76 "dk4sock23.ctr" } #else #line 79 "dk4sock23.ctr" #endif #endif res = dk4socket_bind( (back->pSockets)[back->szUsed], (struct sockaddr *)(&soin4), sizeof(soin4), erp ); if (DK4_SOCKET_RESULT_SUCCESS == res) { res = dk4socket_listen( (back->pSockets)[back->szUsed], backlog, erp ); if (DK4_SOCKET_RESULT_SUCCESS == res) { back->szUsed += 1; } else { (void)dk4socket_close((back->pSockets)[back->szUsed], NULL); (back->pSockets)[back->szUsed] = INVALID_SOCKET; } } else { (void)dk4socket_close((back->pSockets)[back->szUsed], NULL); (back->pSockets)[back->szUsed] = INVALID_SOCKET; } } /* IPv6 address if available */ #if DK4_HAVE_STRUCT_SOCKADDR_IN6 DK4_MEMRES(&soin6, sizeof(soin6)); soin6.sin6_family = AF_INET6; if (0 != local) { soin6.sin6_addr.s6_addr[15] = 0x01; } soin6.sin6_port = dk4socket_htons(portno); (back->pSockets)[back->szUsed] = dk4socket_open( AF_INET6, SOCK_STREAM, 0, erp ); if (INVALID_SOCKET != (back->pSockets)[back->szUsed]) { #if DK4_HAVE_SETSOCKOPT && defined(SO_REUSEPORT) dk4socket_set_one_bytes(&opt, sizeof(opt)); res = setsockopt( (back->pSockets)[back->szUsed], SOL_SOCKET, SO_REUSEPORT, (void *)(&opt), sizeof(opt) ); if (0 != res) { #line 123 "dk4sock23.ctr" } #else #if DK4_HAVE_SETSOCKOPT && defined(SO_REUSEADDR) #line 127 "dk4sock23.ctr" dk4socket_set_one_bytes(&opt, sizeof(opt)); res = setsockopt( (back->pSockets)[back->szUsed], SOL_SOCKET, SO_REUSEADDR, (void *)(&opt), sizeof(opt) ); if (0 != res) { #line 133 "dk4sock23.ctr" } #else #line 136 "dk4sock23.ctr" #endif #endif res = dk4socket_bind( (back->pSockets)[back->szUsed], (struct sockaddr *)(&soin6), sizeof(soin6), erp ); if (DK4_SOCKET_RESULT_SUCCESS == res) { res = dk4socket_listen( (back->pSockets)[back->szUsed], backlog, erp ); if (DK4_SOCKET_RESULT_SUCCESS == res) { back->szUsed += 1; } else { (void)dk4socket_close((back->pSockets)[back->szUsed], NULL); (back->pSockets)[back->szUsed] = INVALID_SOCKET; } } else { (void)dk4socket_close((back->pSockets)[back->szUsed], NULL); (back->pSockets)[back->szUsed] = INVALID_SOCKET; } } #endif if (0 == back->szUsed) { dk4socket_set_delete(back, NULL); back = NULL; } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; }