11/*
2- * Authored by Alex Hultman, 2018-2019 .
2+ * Authored by Alex Hultman, 2018-2021 .
33 * Intellectual property of third-party.
44
55 * Licensed under the Apache License, Version 2.0 (the "License");
@@ -107,15 +107,27 @@ void internal_finalize_bsd_addr(struct bsd_addr_t *addr) {
107107 if (addr -> mem .ss_family == AF_INET6 ) {
108108 addr -> ip = (char * ) & ((struct sockaddr_in6 * ) addr )-> sin6_addr ;
109109 addr -> ip_length = sizeof (struct in6_addr );
110+ addr -> port = ntohs (((struct sockaddr_in6 * ) addr )-> sin6_port );
110111 } else if (addr -> mem .ss_family == AF_INET ) {
111112 addr -> ip = (char * ) & ((struct sockaddr_in * ) addr )-> sin_addr ;
112113 addr -> ip_length = sizeof (struct in_addr );
114+ addr -> port = ntohs (((struct sockaddr_in * ) addr )-> sin_port );
113115 } else {
114116 addr -> ip_length = 0 ;
117+ addr -> port = -1 ;
115118 }
116119}
117120
118- int bsd_socket_addr (LIBUS_SOCKET_DESCRIPTOR fd , struct bsd_addr_t * addr ) {
121+ int bsd_local_addr (LIBUS_SOCKET_DESCRIPTOR fd , struct bsd_addr_t * addr ) {
122+ addr -> len = sizeof (addr -> mem );
123+ if (getsockname (fd , (struct sockaddr * ) & addr -> mem , & addr -> len )) {
124+ return -1 ;
125+ }
126+ internal_finalize_bsd_addr (addr );
127+ return 0 ;
128+ }
129+
130+ int bsd_remote_addr (LIBUS_SOCKET_DESCRIPTOR fd , struct bsd_addr_t * addr ) {
119131 addr -> len = sizeof (addr -> mem );
120132 if (getpeername (fd , (struct sockaddr * ) & addr -> mem , & addr -> len )) {
121133 return -1 ;
@@ -132,6 +144,10 @@ int bsd_addr_get_ip_length(struct bsd_addr_t *addr) {
132144 return addr -> ip_length ;
133145}
134146
147+ int bsd_addr_get_port (struct bsd_addr_t * addr ) {
148+ return addr -> port ;
149+ }
150+
135151// called by dispatch_ready_poll
136152LIBUS_SOCKET_DESCRIPTOR bsd_accept_socket (LIBUS_SOCKET_DESCRIPTOR fd , struct bsd_addr_t * addr ) {
137153 LIBUS_SOCKET_DESCRIPTOR accepted_fd ;
@@ -228,17 +244,18 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int
228244 return LIBUS_SOCKET_ERROR ;
229245 }
230246
231- /* Always enable SO_REUSEPORT and SO_REUSEADDR _unless_ options specify otherwise */
247+ if (port != 0 ) {
248+ /* Otherwise, always enable SO_REUSEPORT and SO_REUSEADDR _unless_ options specify otherwise */
232249#if /*defined(__linux) &&*/ defined(SO_REUSEPORT )
233- if (!(options & LIBUS_LISTEN_EXCLUSIVE_PORT )) {
234- int optval = 1 ;
235- setsockopt (listenFd , SOL_SOCKET , SO_REUSEPORT , & optval , sizeof (optval ));
236- }
250+ if (!(options & LIBUS_LISTEN_EXCLUSIVE_PORT )) {
251+ int optval = 1 ;
252+ setsockopt (listenFd , SOL_SOCKET , SO_REUSEPORT , & optval , sizeof (optval ));
253+ }
237254#endif
238-
239- int enabled = 1 ;
240- setsockopt ( listenFd , SOL_SOCKET , SO_REUSEADDR , ( SETSOCKOPT_PTR_TYPE ) & enabled , sizeof ( enabled ));
241-
255+ int enabled = 1 ;
256+ setsockopt ( listenFd , SOL_SOCKET , SO_REUSEADDR , ( SETSOCKOPT_PTR_TYPE ) & enabled , sizeof ( enabled )) ;
257+ }
258+
242259#ifdef IPV6_V6ONLY
243260 int disabled = 0 ;
244261 setsockopt (listenFd , IPPROTO_IPV6 , IPV6_V6ONLY , (SETSOCKOPT_PTR_TYPE ) & disabled , sizeof (disabled ));
0 commit comments