21
22
COMM_ERR_CONNECT = -8,
23
24
COMM_ERR_CLOSING = -10,
26
COMM_ERR_PROTOCOL = -11, /* IPv4 or IPv6 cannot be used on the fd socket */
25
typedef void IOFCB(int fd, StoreIOBuffer receivedData, comm_err_t flag, int xerrno, void *data);
26
typedef void IOWCB(int fd, char *buffer, size_t len, comm_err_t flag, int xerrno, void *data);
28
typedef void CWCB(int fd, char *, size_t size, comm_err_t flag, void *data);
29
typedef void CNCB(int fd, comm_err_t status, int xerrno, void *data);
30
class DnsLookupDetails;
31
typedef void CNCB(int fd, const DnsLookupDetails &dns, comm_err_t status, int xerrno, void *data);
31
33
typedef void IOCB(int fd, char *, size_t size, comm_err_t flag, int xerrno, void *data);
34
extern void comm_calliocallback(void);
35
37
extern bool comm_iocallbackpending(void); /* inline candidate */
37
39
extern int comm_listen(int fd);
38
40
SQUIDCEXTERN int commSetNonBlocking(int fd);
39
41
SQUIDCEXTERN int commUnsetNonBlocking(int fd);
40
42
SQUIDCEXTERN void commSetCloseOnExec(int fd);
43
SQUIDCEXTERN void commSetTcpKeepalive(int fd, int idle, int interval, int timeout);
41
44
extern void _comm_close(int fd, char const *file, int line);
42
45
#define comm_close(fd) (_comm_close((fd), __FILE__, __LINE__))
43
46
SQUIDCEXTERN void comm_reset_close(int fd);
45
48
SQUIDCEXTERN void comm_lingering_close(int fd);
47
50
SQUIDCEXTERN void commConnectStart(int fd, const char *, u_short, CNCB *, void *);
51
void commConnectStart(int fd, const char *, u_short, AsyncCall::Pointer &cb);
49
SQUIDCEXTERN int comm_connect_addr(int sock, const struct sockaddr_in *);
53
SQUIDCEXTERN int comm_connect_addr(int sock, const IpAddress &addr);
50
54
SQUIDCEXTERN void comm_init(void);
51
55
SQUIDCEXTERN void comm_exit(void);
53
SQUIDCEXTERN int comm_open(int, int, struct IN_ADDR, u_short port, int, const char *note);
55
SQUIDCEXTERN int comm_openex(int, int, struct IN_ADDR, u_short, int, unsigned char TOS, const char *);
57
SQUIDCEXTERN int comm_open(int, int, IpAddress &, int, const char *note);
60
* Open a port specially bound for listening or sending through a specific port.
61
* This is a wrapper providing IPv4/IPv6 failover around comm_openex().
62
* Please use for all listening sockets and bind() outbound sockets.
64
* It will open a socket bound for:
65
* - IPv4 if IPv6 is disabled or address is IPv4-native.
66
* - IPv6 if address is IPv6-native
67
* - IPv6 dual-stack mode if able to open [::]
69
* When an open performs failover it update the given address to feedback
70
* the new IPv4-only status of the socket. Further displays of the IP
71
* (in debugs or cachemgr) will occur in Native IPv4 format.
72
* A reconfigure is needed to reset the stored IP in most cases and attempt a port re-open.
74
SQUIDCEXTERN int comm_open_listener(int sock_type, int proto, IpAddress &addr, int flags, const char *note);
76
SQUIDCEXTERN int comm_openex(int, int, IpAddress &, int, unsigned char TOS, const char *);
56
77
SQUIDCEXTERN u_short comm_local_port(int fd);
57
78
SQUIDCEXTERN int comm_set_tos(int fd, int tos);
59
80
SQUIDCEXTERN void commSetSelect(int, unsigned int, PF *, void *, time_t);
60
81
SQUIDCEXTERN void commResetSelect(int);
62
SQUIDCEXTERN int comm_udp_sendto(int, const struct sockaddr_in *, int, const void *, int);
83
SQUIDCEXTERN int comm_udp_sendto(int sock, const IpAddress &to, const void *buf, int buflen);
63
84
extern void comm_write(int fd, const char *buf, int len, IOCB *callback, void *callback_data, FREE *func);
85
extern void comm_write(int fd, const char *buf, int size, AsyncCall::Pointer &callback, FREE * free_func = NULL);
64
86
SQUIDCEXTERN void comm_write_mbuf(int fd, MemBuf *mb, IOCB * handler, void *handler_data);
87
extern void comm_write_mbuf(int fd, MemBuf *mb, AsyncCall::Pointer &callback);
65
88
SQUIDCEXTERN void commCallCloseHandlers(int fd);
66
89
SQUIDCEXTERN int commSetTimeout(int fd, int, PF *, void *);
90
extern int commSetTimeout(int fd, int, AsyncCall::Pointer &calback);
67
91
SQUIDCEXTERN int ignoreErrno(int);
68
92
SQUIDCEXTERN void commCloseAllSockets(void);
69
93
SQUIDCEXTERN void checkTimeouts(void);
79
103
class ConnectionDetail;
80
104
typedef void IOACB(int fd, int nfd, ConnectionDetail *details, comm_err_t flag, int xerrno, void *data);
81
105
extern void comm_accept(int fd, IOACB *handler, void *handler_data);
106
extern void comm_accept(int fd, AsyncCall::Pointer &call);
82
107
extern void comm_add_close_handler(int fd, PF *, void *);
108
extern void comm_add_close_handler(int fd, AsyncCall::Pointer &);
83
109
extern void comm_remove_close_handler(int fd, PF *, void *);
110
extern void comm_remove_close_handler(int fd, AsyncCall::Pointer &);
85
113
extern int comm_has_pending_read_callback(int fd);
86
extern bool comm_has_pending_read(int fd);
114
extern bool comm_monitors_read(int fd);
87
115
extern void comm_read(int fd, char *buf, int len, IOCB *handler, void *data);
116
extern void comm_read(int fd, char *buf, int len, AsyncCall::Pointer &callback);
88
117
extern void comm_read_cancel(int fd, IOCB *callback, void *data);
89
extern void fdc_open(int fd, unsigned int type, char const *desc);
90
extern int comm_udp_recvfrom(int fd, void *buf, size_t len, int flags,
92
struct sockaddr *from, socklen_t *fromlen);
118
extern void comm_read_cancel(int fd, AsyncCall::Pointer &callback);
119
extern int comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, IpAddress &from);
93
120
extern int comm_udp_recv(int fd, void *buf, size_t len, int flags);
94
121
extern ssize_t comm_udp_send(int s, const void *buf, size_t len, int flags);
95
extern void commMarkHalfClosed(int);
96
extern int commIsHalfClosed(int);
97
extern void commCheckHalfClosed(void *);
98
122
extern bool comm_has_incomplete_write(int);
124
/** The read channel has closed and the caller does not expect more data
125
* but needs to detect connection aborts. The current detection method uses
126
* 0-length reads: We read until the error occurs or the writer closes
127
* the connection. If there is a read error, we close the connection.
129
extern void commStartHalfClosedMonitor(int fd);
130
extern bool commHasHalfClosedMonitor(int fd);
131
// XXX: remove these wrappers which minimize client_side.cc changes in a commit
132
inline void commMarkHalfClosed(int fd) { commStartHalfClosedMonitor(fd); }
133
inline bool commIsHalfClosed(int fd) { return commHasHalfClosedMonitor(fd); }
100
135
/* Not sure where these should live yet */
124
159
Vector<Acceptor> deferred;
127
/* App layer doesn't want any more data from the socket, as the read channel is
128
* closed, but we need to detect aborts, so this lets us do so.
135
static AbortChecker &Instance();
136
/* the current method of checking, is via a 0 length read every second.
137
* if nothing is returned by the next IO loop, we let it be.
138
* If an error occurs, we close the conn.
139
* Note that some tcp environments may allow direct polling for the socket status
140
* and this could be adjusted to use that method for the test. (in which case
141
* the singleton should be refactored to have the tcp engine register the
144
static IOCB AbortCheckReader;
146
void stopMonitoring (int);
150
static AbortChecker Instance_;
151
static void AddCheck (int const &, void *);
152
static int IntCompare (int const &, int const &);
153
static void RemoveCheck (int const &, void *);
154
AbortChecker() : fds (NULL), checking (false), lastCheck (0){}
156
mutable SplayNode<int> *fds;
159
bool contains (int const) const;
167
void addCheck (int const);
169
void removeCheck (int const);
172
/* a dispatcher for comms events */
174
class CommDispatcher : public CompletionDispatcher
178
virtual bool dispatch();
181
162
/* A comm engine that calls comm_select */
183
164
class CommSelectEngine : public AsyncEngine