@@ -270,6 +270,31 @@ std::string Socket::resolveHostToBestExternalAddrGuess(const std::string &host,
270
270
return newaddr;
271
271
}
272
272
273
+ // / Gets bound host and port for a socket and returns them by reference.
274
+ // / Returns true on success and false on failure.
275
+ bool Socket::getSocketName (int fd, std::string & host, uint32_t & port){
276
+ struct sockaddr_in6 tmpaddr;
277
+ socklen_t len = sizeof (tmpaddr);
278
+ if (getsockname (fd, (sockaddr *)&tmpaddr, &len)){
279
+ return false ;
280
+ }
281
+ static char addrconv[INET6_ADDRSTRLEN];
282
+ if (tmpaddr.sin6_family == AF_INET6){
283
+ host = inet_ntop (AF_INET6, &(tmpaddr.sin6_addr ), addrconv, INET6_ADDRSTRLEN);
284
+ if (host.substr (0 , 7 ) == " ::ffff:" ){host = host.substr (7 );}
285
+ port = ntohs (tmpaddr.sin6_port );
286
+ HIGH_MSG (" Local IPv6 addr [%s:%" PRIu32 " ]" , host.c_str (), port);
287
+ return true ;
288
+ }
289
+ if (tmpaddr.sin6_family == AF_INET){
290
+ host = inet_ntop (AF_INET, &(((sockaddr_in *)&tmpaddr)->sin_addr ), addrconv, INET6_ADDRSTRLEN);
291
+ port = ntohs (((sockaddr_in *)&tmpaddr)->sin_port );
292
+ HIGH_MSG (" Local IPv4 addr [%s:%" PRIu32 " ]" , host.c_str (), port);
293
+ return true ;
294
+ }
295
+ return false ;
296
+ }
297
+
273
298
std::string uint2string (unsigned int i){
274
299
std::stringstream st;
275
300
st << i;
@@ -457,20 +482,8 @@ void Socket::Connection::setBoundAddr(){
457
482
return ;
458
483
}
459
484
// Otherwise, read from socket pointer. Works for both SSL and non-SSL sockets, and real sockets passed as fd's, but not for non-sockets (duh)
460
- struct sockaddr_in6 tmpaddr;
461
- socklen_t len = sizeof (tmpaddr);
462
- if (!getsockname (getSocket (), (sockaddr *)&tmpaddr, &len)){
463
- static char addrconv[INET6_ADDRSTRLEN];
464
- if (tmpaddr.sin6_family == AF_INET6){
465
- boundaddr = inet_ntop (AF_INET6, &(tmpaddr.sin6_addr ), addrconv, INET6_ADDRSTRLEN);
466
- if (boundaddr.substr (0 , 7 ) == " ::ffff:" ){boundaddr = boundaddr.substr (7 );}
467
- HIGH_MSG (" Local IPv6 addr [%s]" , boundaddr.c_str ());
468
- }
469
- if (tmpaddr.sin6_family == AF_INET){
470
- boundaddr = inet_ntop (AF_INET, &(((sockaddr_in *)&tmpaddr)->sin_addr ), addrconv, INET6_ADDRSTRLEN);
471
- HIGH_MSG (" Local IPv4 addr [%s]" , boundaddr.c_str ());
472
- }
473
- }
485
+ uint32_t boundport = 0 ;
486
+ getSocketName (getSocket (), boundaddr, boundport);
474
487
}
475
488
476
489
// Cleans up the socket by dropping the connection.
@@ -1741,21 +1754,9 @@ void Socket::UDPConnection::SendNow(const char *sdata, size_t len){
1741
1754
}
1742
1755
1743
1756
std::string Socket::UDPConnection::getBoundAddress (){
1744
- struct sockaddr_in6 tmpaddr;
1745
- socklen_t len = sizeof (tmpaddr);
1746
1757
std::string boundaddr;
1747
- if (!getsockname (sock, (sockaddr *)&tmpaddr, &len)){
1748
- static char addrconv[INET6_ADDRSTRLEN];
1749
- if (tmpaddr.sin6_family == AF_INET6){
1750
- boundaddr = inet_ntop (AF_INET6, &(tmpaddr.sin6_addr ), addrconv, INET6_ADDRSTRLEN);
1751
- if (boundaddr.substr (0 , 7 ) == " ::ffff:" ){boundaddr = boundaddr.substr (7 );}
1752
- HIGH_MSG (" Local IPv6 addr [%s]" , boundaddr.c_str ());
1753
- }
1754
- if (tmpaddr.sin6_family == AF_INET){
1755
- boundaddr = inet_ntop (AF_INET, &(((sockaddr_in *)&tmpaddr)->sin_addr ), addrconv, INET6_ADDRSTRLEN);
1756
- HIGH_MSG (" Local IPv4 addr [%s]" , boundaddr.c_str ());
1757
- }
1758
- }
1758
+ uint32_t boundport;
1759
+ Socket::getSocketName (sock, boundaddr, boundport);
1759
1760
return boundaddr;
1760
1761
}
1761
1762
0 commit comments