@@ -210,11 +210,7 @@ static void
210210commSetBindAddressNoPort (const int fd)
211211{
212212#if defined(IP_BIND_ADDRESS_NO_PORT)
213- int flag = 1 ;
214- if (setsockopt (fd, IPPROTO_IP , IP_BIND_ADDRESS_NO_PORT , reinterpret_cast <char *>(&flag), sizeof (flag)) < 0 ) {
215- const auto savedErrno = errno;
216- debugs (50 , DBG_IMPORTANT , " ERROR: setsockopt(IP_BIND_ADDRESS_NO_PORT) failure: " << xstrerr (savedErrno));
217- }
213+ Comm::SetBooleanSocketOption (fd, IPPROTO_IP , IP_BIND_ADDRESS_NO_PORT , true , SBuf (" IP_BIND_ADDRESS_NO_PORT" ));
218214#else
219215 (void )fd;
220216#endif
@@ -291,16 +287,13 @@ limitError(int const anErrno)
291287}
292288
293289static void
294- comm_set_v6only (int fd, int tos )
290+ comm_set_v6only (int fd, bool enabled )
295291{
296- #ifdef IPV6_V6ONLY
297- if (setsockopt (fd, IPPROTO_IPV6 , IPV6_V6ONLY , (char *) &tos, sizeof (int )) < 0 ) {
298- int xerrno = errno;
299- debugs (50 , DBG_IMPORTANT , MYNAME << " setsockopt(IPV6_V6ONLY) " << (tos?" ON" :" OFF" ) << " for FD " << fd << " : " << xstrerr (xerrno));
300- }
292+ #if defined(IPV6_V6ONLY)
293+ Comm::SetBooleanSocketOption (fd, IPPROTO_IPV6 , IPV6_V6ONLY , enabled, SBuf (" IPV6_V6ONLY" ));
301294#else
302- debugs (50 , DBG_CRITICAL , MYNAME << " WARNING: setsockopt(IPV6_V6ONLY) not supported on this platform" );
303- #endif /* sockopt */
295+ debugs (50 , DBG_CRITICAL , " WARNING: setsockopt(IPV6_V6ONLY) not supported on this platform" );
296+ #endif
304297}
305298
306299/* *
@@ -315,17 +308,20 @@ comm_set_transparent(int fd)
315308#if _SQUID_LINUX_ && defined(IP_TRANSPARENT) // Linux
316309# define soLevel SOL_IP
317310# define soFlag IP_TRANSPARENT
311+ const SBuf name (" IP_TRANSPARENT" );
318312 bool doneSuid = false ;
319313
320314#elif defined(SO_BINDANY) // OpenBSD 4.7+ and NetBSD with PF
321315# define soLevel SOL_SOCKET
322316# define soFlag SO_BINDANY
317+ const SBuf name (" SO_BINDANY" );
323318 enter_suid ();
324319 bool doneSuid = true ;
325320
326321#elif defined(IP_BINDANY) // FreeBSD with IPFW
327322# define soLevel IPPROTO_IP
328323# define soFlag IP_BINDANY
324+ const SBuf name (" IP_BINDANY" );
329325 enter_suid ();
330326 bool doneSuid = true ;
331327
@@ -335,11 +331,7 @@ comm_set_transparent(int fd)
335331#endif /* sockopt */
336332
337333#if defined(soLevel) && defined(soFlag)
338- int tos = 1 ;
339- if (setsockopt (fd, soLevel, soFlag, (char *) &tos, sizeof (int )) < 0 ) {
340- int xerrno = errno;
341- debugs (50 , DBG_IMPORTANT , MYNAME << " setsockopt(TPROXY) on FD " << fd << " : " << xstrerr (xerrno));
342- } else {
334+ if (Comm::SetBooleanSocketOption (fd, soLevel, soFlag, true , name)) {
343335 /* mark the socket as having transparent options */
344336 fd_table[fd].flags .transparent = true ;
345337 }
@@ -423,12 +415,12 @@ comm_openex(int sock_type,
423415 debugs (50 , 3 , " comm_openex: Opened socket " << conn << " : family=" << AI ->ai_family << " , type=" << AI ->ai_socktype << " , protocol=" << AI ->ai_protocol );
424416
425417 if ( Ip::EnableIpv6&IPV6_SPECIAL_SPLITSTACK && addr.isIPv6 () )
426- comm_set_v6only (conn->fd , 1 );
418+ comm_set_v6only (conn->fd , true );
427419
428420 /* Windows Vista supports Dual-Sockets. BUT defaults them to V6ONLY. Turn it OFF. */
429421 /* Other OS may have this administratively disabled for general use. Same deal. */
430422 if ( Ip::EnableIpv6&IPV6_SPECIAL_V4MAPPING && addr.isIPv6 () )
431- comm_set_v6only (conn->fd , 0 );
423+ comm_set_v6only (conn->fd , false );
432424
433425 comm_init_opened (conn, note, AI );
434426 new_socket = comm_apply_flags (conn->fd , addr, flags, AI );
@@ -505,8 +497,7 @@ comm_apply_flags(int new_socket,
505497
506498#if defined(SO_REUSEPORT)
507499 if (flags & COMM_REUSEPORT ) {
508- int on = 1 ;
509- if (setsockopt (new_socket, SOL_SOCKET , SO_REUSEPORT , reinterpret_cast <char *>(&on), sizeof (on)) < 0 ) {
500+ if (!Comm::SetBooleanSocketOption (new_socket, SOL_SOCKET , SO_REUSEPORT , true , ToSBuf (" SO_REUSEPORT on " , addr))) {
510501 const auto savedErrno = errno;
511502 const auto errorMessage = ToSBuf (" cannot enable SO_REUSEPORT socket option when binding to " ,
512503 addr, " : " , xstrerr (savedErrno));
@@ -770,10 +761,7 @@ commConfigureLinger(const int fd, const OnOff enabled)
770761
771762 fd_table[fd].flags .harshClosureRequested = (l.l_onoff && !l.l_linger ); // close(2) sends TCP RST if true
772763
773- if (setsockopt (fd, SOL_SOCKET , SO_LINGER , reinterpret_cast <char *>(&l), sizeof (l)) < 0 ) {
774- const auto xerrno = errno;
775- debugs (50 , DBG_CRITICAL , " ERROR: Failed to set closure behavior (SO_LINGER) for FD " << fd << " : " << xstrerr (xerrno));
776- }
764+ Comm::SetSocketOption (fd, SOL_SOCKET , SO_LINGER , l, ToSBuf (" SO_LINGER (0 seconds) " , (l.l_onoff ?" enabled" :" disabled" )));
777765}
778766
779767/* *
@@ -1011,29 +999,16 @@ comm_remove_close_handler(int fd, AsyncCall::Pointer &call)
1011999static void
10121000commSetReuseAddr (int fd)
10131001{
1014- int on = 1 ;
1015- if (setsockopt (fd, SOL_SOCKET , SO_REUSEADDR , (char *) &on, sizeof (on)) < 0 ) {
1016- int xerrno = errno;
1017- debugs (50 , DBG_IMPORTANT , MYNAME << " FD " << fd << " : " << xstrerr (xerrno));
1018- }
1002+ Comm::SetBooleanSocketOption (fd, SOL_SOCKET , SO_REUSEADDR , true , SBuf (" SO_REUSEADDR" ));
10191003}
10201004
10211005static void
10221006commSetTcpRcvbuf (int fd, int size)
10231007{
1024- if (setsockopt (fd, SOL_SOCKET , SO_RCVBUF , (char *) &size, sizeof (size)) < 0 ) {
1025- int xerrno = errno;
1026- debugs (50 , DBG_IMPORTANT , MYNAME << " FD " << fd << " , SIZE " << size << " : " << xstrerr (xerrno));
1027- }
1028- if (setsockopt (fd, SOL_SOCKET , SO_SNDBUF , (char *) &size, sizeof (size)) < 0 ) {
1029- int xerrno = errno;
1030- debugs (50 , DBG_IMPORTANT , MYNAME << " FD " << fd << " , SIZE " << size << " : " << xstrerr (xerrno));
1031- }
1032- #ifdef TCP_WINDOW_CLAMP
1033- if (setsockopt (fd, SOL_TCP , TCP_WINDOW_CLAMP , (char *) &size, sizeof (size)) < 0 ) {
1034- int xerrno = errno;
1035- debugs (50 , DBG_IMPORTANT , MYNAME << " FD " << fd << " , SIZE " << size << " : " << xstrerr (xerrno));
1036- }
1008+ Comm::SetSocketOption (fd, SOL_SOCKET , SO_RCVBUF , size, ToSBuf (" SO_RCVBUF to " , size, " bytes" ));
1009+ Comm::SetSocketOption (fd, SOL_SOCKET , SO_SNDBUF , size, ToSBuf (" SO_SNDBUF to " , size, " bytes" ));
1010+ #if defined(TCP_WINDOW_CLAMP)
1011+ Comm::SetSocketOption (fd, SOL_TCP , TCP_WINDOW_CLAMP , size, ToSBuf (" TCP_WINDOW_CLAMP to " , size, " bytes" ));
10371012#endif
10381013}
10391014
@@ -1118,20 +1093,13 @@ commSetCloseOnExec(int fd)
11181093#endif
11191094}
11201095
1121- #ifdef TCP_NODELAY
1096+ #if defined( TCP_NODELAY)
11221097static void
11231098commSetTcpNoDelay (int fd)
11241099{
1125- int on = 1 ;
1126-
1127- if (setsockopt (fd, IPPROTO_TCP , TCP_NODELAY , (char *) &on, sizeof (on)) < 0 ) {
1128- int xerrno = errno;
1129- debugs (50 , DBG_IMPORTANT , MYNAME << " FD " << fd << " : " << xstrerr (xerrno));
1130- }
1131-
1100+ Comm::SetBooleanSocketOption (fd, IPPROTO_TCP , TCP_NODELAY , true , SBuf (" TCP_NODELAY" ));
11321101 fd_table[fd].flags .nodelay = true ;
11331102}
1134-
11351103#endif
11361104
11371105void
0 commit comments