comm.cc File Reference
#include "squid.h"
#include "base/AsyncFunCalls.h"
#include "ClientInfo.h"
#include "comm/AcceptLimiter.h"
#include "comm/comm_internal.h"
#include "comm/Connection.h"
#include "comm/IoCallback.h"
#include "comm/Loops.h"
#include "comm/Read.h"
#include "comm/TcpAcceptor.h"
#include "comm/Write.h"
#include "compat/cmsg.h"
#include "DescriptorSet.h"
#include "event.h"
#include "fd.h"
#include "fde.h"
#include "globals.h"
#include "icmp/net_db.h"
#include "ip/Intercept.h"
#include "ip/QosConfig.h"
#include "ip/tools.h"
#include "pconn.h"
#include "sbuf/SBuf.h"
#include "sbuf/Stream.h"
#include "SquidConfig.h"
#include "StatCounters.h"
#include "StoreIOBuffer.h"
#include "tools.h"
#include "ssl/support.h"
#include <cerrno>
#include <cmath>
Include dependency graph for comm.cc:

Go to the source code of this file.

Functions

static int comm_openex (int sock_type, int proto, Ip::Address &, int flags, const char *note)
 
static void comm_init_opened (const Comm::ConnectionPointer &conn, const char *note, struct addrinfo *AI)
 update FD tables after a local or remote (IPC) comm_openex(); More...
 
static int comm_apply_flags (int new_socket, Ip::Address &addr, int flags, struct addrinfo *AI)
 
static void commHandleWriteHelper (void *data)
 
static void commPlanHalfClosedCheck ()
 
static Comm::Flag commBind (int s, struct addrinfo &)
 
static void commSetBindAddressNoPort (int)
 
static void commSetReuseAddr (int)
 
static void commSetNoLinger (int)
 
static void commSetTcpRcvbuf (int, int)
 
bool isOpen (const int fd)
 
static void comm_empty_os_read_buffers (int fd)
 
int comm_udp_recvfrom (int fd, void *buf, size_t len, int flags, Ip::Address &from)
 
int comm_udp_recv (int fd, void *buf, size_t len, int flags)
 
ssize_t comm_udp_send (int s, const void *buf, size_t len, int flags)
 
bool comm_has_incomplete_write (int fd)
 
unsigned short comm_local_port (int fd)
 
int comm_open (int sock_type, int proto, Ip::Address &addr, int flags, const char *note)
 
void comm_open_listener (int sock_type, int proto, Comm::ConnectionPointer &conn, const char *note)
 
int comm_open_listener (int sock_type, int proto, Ip::Address &addr, int flags, const char *note)
 
static bool limitError (int const anErrno)
 
static void comm_set_v6only (int fd, int tos)
 
static void comm_set_transparent (int fd)
 
void comm_import_opened (const Comm::ConnectionPointer &conn, const char *note, struct addrinfo *AI)
 update Comm state after getting a comm_open() FD from another process More...
 
void commUnsetFdTimeout (int fd)
 clear a timeout handler by FD number More...
 
int commSetConnTimeout (const Comm::ConnectionPointer &conn, time_t timeout, AsyncCall::Pointer &callback)
 
int commUnsetConnTimeout (const Comm::ConnectionPointer &conn)
 
int comm_connect_addr (int sock, const Ip::Address &address)
 
void commCallCloseHandlers (int fd)
 
void comm_reset_close (const Comm::ConnectionPointer &conn)
 
void old_comm_reset_close (int fd)
 
static void commStartTlsClose (const int fd)
 
static void comm_close_complete (const int fd)
 
void _comm_close (int fd, char const *file, int line)
 
int comm_udp_sendto (int fd, const Ip::Address &to_addr, const void *buf, int len)
 
AsyncCall::Pointer comm_add_close_handler (int fd, CLCB *handler, void *data)
 
void comm_add_close_handler (int fd, AsyncCall::Pointer &call)
 
void comm_remove_close_handler (int fd, CLCB *handler, void *data)
 
void comm_remove_close_handler (int fd, AsyncCall::Pointer &call)
 
int commSetNonBlocking (int fd)
 
int commUnsetNonBlocking (int fd)
 
void commSetCloseOnExec (int fd)
 
void comm_init (void)
 
void comm_exit (void)
 
int ignoreErrno (int ierrno)
 
void commCloseAllSockets (void)
 
static bool AlreadyTimedOut (fde *F)
 
static bool writeTimedOut (int fd)
 
void checkTimeouts (void)
 
void commStartHalfClosedMonitor (int fd)
 Start waiting for a possibly half-closed connection to close. More...
 
static void commHalfClosedCheck (void *)
 
bool commHasHalfClosedMonitor (int fd)
 checks whether we are waiting for possibly half-closed connection to close More...
 
void commStopHalfClosedMonitor (int const fd)
 stop waiting for possibly half-closed connection to close More...
 
static void commHalfClosedReader (const Comm::ConnectionPointer &conn, char *, size_t size, Comm::Flag flag, int, void *)
 I/O handler for the possibly half-closed connection monitoring code. More...
 
int comm_open_uds (int sock_type, int proto, struct sockaddr_un *addr, int flags)
 Create a unix-domain socket (UDS) that only supports FD_MSGHDR I/O. More...
 

Variables

static IOCB commHalfClosedReader
 
static DescriptorSetTheHalfClosed = nullptr
 
static bool WillCheckHalfClosed = false
 the set of half-closed FDs More...
 
static EVH commHalfClosedCheck
 true if check is scheduled More...
 

Function Documentation

◆ _comm_close()

◆ AlreadyTimedOut()

static bool AlreadyTimedOut ( fde F)
static

Definition at line 1500 of file comm.cc.

References F(), and squid_curtime.

Referenced by checkTimeouts().

◆ checkTimeouts()

◆ comm_add_close_handler() [1/2]

void comm_add_close_handler ( int  fd,
AsyncCall::Pointer call 
)

Definition at line 961 of file comm.cc.

References debugs, fd_table, and AsyncCall::setNext().

◆ comm_add_close_handler() [2/2]

◆ comm_apply_flags()

◆ comm_close_complete()

static void comm_close_complete ( const int  fd)
static

◆ comm_connect_addr()

int comm_connect_addr ( int  sock,
const Ip::Address address 
)

Connect socket FD to given remote address. If return value is an error flag (COMM_ERROR, ERR_CONNECT, ERR_PROTOCOL, etc.), then error code will also be returned in errno.

Definition at line 634 of file comm.cc.

References assert, Comm::COMM_ERROR, StatCounters::connects, DBG_DATA, debugs, Comm::ERR_PROTOCOL, F(), fd_table, Ip::Address::FreeAddr(), Ip::Address::getAddrInfo(), ignoreErrno(), Comm::INPROGRESS, Ip::Address::isIPv4(), MAX_IPSTRLEN, Comm::OK, Ip::Address::port(), StatCounters::sock, statCounter, StatCounters::syscalls, Ip::Address::toStr(), and xstrerr().

Referenced by Comm::ConnOpener::doConnect(), ipc_thread_1(), ipcCreate(), and logfile_mod_udp_open().

◆ comm_empty_os_read_buffers()

static void comm_empty_os_read_buffers ( int  fd)
static

Empty the read buffers

This is a magical routine that empties the read buffers. Under some platforms (Linux) if a buffer has data in it before you call close(), the socket will hang and take quite a while to timeout.

Definition at line 102 of file comm.cc.

References FD_MSGHDR, FD_READ_METHOD(), and fd_table.

Referenced by _comm_close().

◆ comm_exit()

void comm_exit ( void  )

Definition at line 1189 of file comm.cc.

References Comm::CallbackTableDestruct(), and TheHalfClosed.

Referenced by SquidShutdown().

◆ comm_has_incomplete_write()

bool comm_has_incomplete_write ( int  fd)

Definition at line 151 of file comm.cc.

References assert, COMMIO_FD_WRITECB, and isOpen().

Referenced by PconnPool::push().

◆ comm_import_opened()

void comm_import_opened ( const Comm::ConnectionPointer conn,
const char *  note,
struct addrinfo *  AI 
)

◆ comm_init()

◆ comm_init_opened()

void comm_init_opened ( const Comm::ConnectionPointer conn,
const char *  note,
struct addrinfo *  AI 
)
static

Definition at line 441 of file comm.cc.

References assert, conn, debugs, F(), fd_open(), FD_SOCKET, fd_table, Comm::IsConnOpen(), and isOpen().

Referenced by comm_import_opened(), and comm_openex().

◆ comm_local_port()

unsigned short comm_local_port ( int  fd)

Queue a write. handler/handler_data are called when the write fully completes, on error, or on file descriptor close.

Definition at line 164 of file comm.cc.

References DBG_IMPORTANT, debugs, F(), fd_table, Ip::Address::FreeAddr(), Ip::Address::InitAddr(), MYNAME, Ip::Address::port(), Ip::Address::setIPv4(), and xstrerr().

Referenced by comm_udp_sendto(), Dns::Init(), Ftp::Server::listenForDataConnection(), and Ftp::Client::sendEprt().

◆ comm_open()

int comm_open ( int  sock_type,
int  proto,
Ip::Address addr,
int  flags,
const char *  note 
)

Create a socket. Default is blocking, stream (TCP) socket. IO_TYPE is OR of flags specified in comm.h. Defaults TOS

Definition at line 242 of file comm.cc.

References COMM_DOBIND, COMM_DOBIND_PORT_LATER, comm_openex(), Ip::Address::isAnyAddr(), and Ip::Address::port().

Referenced by Comm::ConnOpener::createFd(), ipc_thread_1(), ipcCreate(), and logfile_mod_udp_open().

◆ comm_open_listener() [1/2]

◆ comm_open_listener() [2/2]

int comm_open_listener ( int  sock_type,
int  proto,
Ip::Address addr,
int  flags,
const char *  note 
)

Open a port specially bound for listening or sending through a specific port. Please use for all listening sockets and bind() outbound sockets.

It will open a socket bound for:

  • IPv4 if IPv6 is disabled or address is IPv4-native.
  • IPv6 if address is IPv6-native
  • IPv6 dual-stack mode if able to open [::]

When an open performs failover it update the given address to feedback the new IPv4-only status of the socket. Further displays of the IP (in debugs or cachemgr) will occur in Native IPv4 format. A reconfigure is needed to reset the stored IP in most cases and attempt a port re-open.

Definition at line 269 of file comm.cc.

References COMM_DOBIND, and comm_openex().

◆ comm_open_uds()

◆ comm_openex()

static int comm_openex ( int  sock_type,
int  proto,
Ip::Address addr,
int  flags,
const char *  note 
)
static

◆ comm_remove_close_handler() [1/2]

void comm_remove_close_handler ( int  fd,
AsyncCall::Pointer call 
)

◆ comm_remove_close_handler() [2/2]

◆ comm_reset_close()

void comm_reset_close ( const Comm::ConnectionPointer conn)

enable linger with time of 0 so that when the socket is closed, TCP generates a RESET

Definition at line 785 of file comm.cc.

References conn, DBG_CRITICAL, debugs, and xstrerr().

Referenced by ConnStateData::abortChunkedRequestBody(), clientProcessRequestFinished(), Adaptation::Icap::ServiceRep::putConnection(), and ConnStateData::setAuth().

◆ comm_set_transparent()

static void comm_set_transparent ( int  fd)
static

Set the socket option required for TPROXY spoofing for:

  • Linux TPROXY v4 support,
  • OpenBSD divert-to support,
  • FreeBSD IPFW TPROXY v4 support.

Definition at line 312 of file comm.cc.

References DBG_CRITICAL, DBG_IMPORTANT, debugs, enter_suid(), fd_table, leave_suid(), MYNAME, and xstrerr().

Referenced by comm_apply_flags().

◆ comm_set_v6only()

static void comm_set_v6only ( int  fd,
int  tos 
)
static

Definition at line 293 of file comm.cc.

References DBG_CRITICAL, DBG_IMPORTANT, debugs, MYNAME, and xstrerr().

Referenced by comm_openex().

◆ comm_udp_recv()

int comm_udp_recv ( int  fd,
void *  buf,
size_t  len,
int  flags 
)

Definition at line 138 of file comm.cc.

References comm_udp_recvfrom().

Referenced by ipcCreate(), and IcmpSquid::Recv().

◆ comm_udp_recvfrom()

int comm_udp_recvfrom ( int  fd,
void *  buf,
size_t  len,
int  flags,
Ip::Address from 
)

◆ comm_udp_send()

ssize_t comm_udp_send ( int  s,
const void *  buf,
size_t  len,
int  flags 
)

Definition at line 145 of file comm.cc.

Referenced by ipcCreate(), IcmpSquid::SendEcho(), wccpAssignBuckets(), and wccpHereIam().

◆ comm_udp_sendto()

◆ commBind()

static Comm::Flag commBind ( int  s,
struct addrinfo &  inaddr 
)
static

◆ commCallCloseHandlers()

void commCallCloseHandlers ( int  fd)

◆ commCloseAllSockets()

void commCloseAllSockets ( void  )

Definition at line 1470 of file comm.cc.

References Biggest_FD, debugs, F(), FD_SOCKET, fd_table, old_comm_reset_close(), and ScheduleCallHere.

Referenced by SquidShutdown().

◆ commHalfClosedCheck()

static void commHalfClosedCheck ( void *  )
static

iterates over all descriptors that may need half-closed tests and calls comm_read for those that do; re-schedules the check if needed

Definition at line 1603 of file comm.cc.

References DescriptorSet::begin(), CallBack(), commPlanHalfClosedCheck(), debugs, DescriptorSet::end(), Comm::Connection::fd, fd_table, TheHalfClosed, and WillCheckHalfClosed.

◆ commHalfClosedReader()

static void commHalfClosedReader ( const Comm::ConnectionPointer conn,
char *  ,
size_t  size,
Comm::Flag  flag,
int  ,
void *   
)
static

◆ commHandleWriteHelper()

void commHandleWriteHelper ( void *  data)
static

◆ commHasHalfClosedMonitor()

bool commHasHalfClosedMonitor ( int  fd)

◆ commPlanHalfClosedCheck()

static void commPlanHalfClosedCheck ( )
static

◆ commSetBindAddressNoPort()

static void commSetBindAddressNoPort ( const int  fd)
static

sets the IP_BIND_ADDRESS_NO_PORT socket option to optimize ephemeral port reuse by outgoing TCP connections that must bind(2) to a source IP address

Definition at line 209 of file comm.cc.

References DBG_IMPORTANT, debugs, and xstrerr().

Referenced by comm_apply_flags().

◆ commSetCloseOnExec()

void commSetCloseOnExec ( int  fd)

◆ commSetConnTimeout()

◆ commSetNoLinger()

static void commSetNoLinger ( int  fd)
static

Definition at line 1022 of file comm.cc.

References DBG_CRITICAL, debugs, fd_table, MYNAME, and xstrerr().

Referenced by comm_apply_flags().

◆ commSetNonBlocking()

◆ commSetReuseAddr()

static void commSetReuseAddr ( int  fd)
static

Definition at line 1037 of file comm.cc.

References DBG_IMPORTANT, debugs, MYNAME, and xstrerr().

Referenced by comm_apply_flags(), and comm_open_uds().

◆ commSetTcpRcvbuf()

static void commSetTcpRcvbuf ( int  fd,
int  size 
)
static

Definition at line 1047 of file comm.cc.

References DBG_IMPORTANT, debugs, MYNAME, size, and xstrerr().

Referenced by comm_apply_flags(), and comm_open_uds().

◆ commStartHalfClosedMonitor()

void commStartHalfClosedMonitor ( int  fd)

The read channel has closed and the caller does not expect more data but needs to detect connection aborts. The current detection method uses 0-length reads: We read until the error occurs or the writer closes the connection. If there is a read error, we close the connection.

Definition at line 1580 of file comm.cc.

References DescriptorSet::add(), assert, commHasHalfClosedMonitor(), commPlanHalfClosedCheck(), CodeContext::Current(), debugs, fd_table, isOpen(), and TheHalfClosed.

Referenced by commMarkHalfClosed().

◆ commStartTlsClose()

static void commStartTlsClose ( const int  fd)
static

Definition at line 814 of file comm.cc.

References fd_table, and Security::SessionSendGoodbye().

Referenced by _comm_close().

◆ commStopHalfClosedMonitor()

void commStopHalfClosedMonitor ( int const  fd)

Definition at line 1637 of file comm.cc.

References debugs, DescriptorSet::del(), fd_table, Comm::ReadCancel(), and TheHalfClosed.

Referenced by _comm_close(), and comm_read_base().

◆ commUnsetConnTimeout()

◆ commUnsetFdTimeout()

void commUnsetFdTimeout ( int  fd)

Definition at line 582 of file comm.cc.

References assert, debugs, F(), fd_table, and Squid_MaxFD.

Referenced by _comm_close(), DiskdIOStrategy::init(), ipcCreate(), IcmpSquid::Open(), and unlinkdInit().

◆ commUnsetNonBlocking()

int commUnsetNonBlocking ( int  fd)

◆ ignoreErrno()

◆ isOpen()

◆ limitError()

static bool limitError ( int const  anErrno)
static

Definition at line 287 of file comm.cc.

Referenced by comm_open_uds(), and comm_openex().

◆ old_comm_reset_close()

void old_comm_reset_close ( int  fd)

Definition at line 800 of file comm.cc.

References comm_close, DBG_CRITICAL, debugs, and xstrerr().

Referenced by commCloseAllSockets().

◆ writeTimedOut()

static bool writeTimedOut ( int  fd)
static

Definition at line 1515 of file comm.cc.

References COMMIO_FD_WRITECB, Config, fd_table, squid_curtime, SquidConfig::Timeout, and SquidConfig::write.

Referenced by checkTimeouts().

Variable Documentation

◆ commHalfClosedCheck

EVH commHalfClosedCheck
static

Definition at line 75 of file comm.cc.

Referenced by commPlanHalfClosedCheck().

◆ commHalfClosedReader

IOCB commHalfClosedReader
static

Definition at line 60 of file comm.cc.

◆ TheHalfClosed

◆ WillCheckHalfClosed

bool WillCheckHalfClosed = false
static

Definition at line 74 of file comm.cc.

Referenced by commHalfClosedCheck(), and commPlanHalfClosedCheck().

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors