]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm.h
Applied source formatting rules in preparation for trunk merge.
[thirdparty/squid.git] / src / comm.h
1 #ifndef __COMM_H__
2 #define __COMM_H__
3
4 #include "squid.h"
5 #include "AsyncEngine.h"
6 #include "base/AsyncCall.h"
7 #include "StoreIOBuffer.h"
8 #include "Array.h"
9 #include "ip/IpAddress.h"
10
11 #define COMMIO_FD_READCB(fd) (&commfd_table[(fd)].readcb)
12 #define COMMIO_FD_WRITECB(fd) (&commfd_table[(fd)].writecb)
13
14 typedef enum {
15 COMM_OK = 0,
16 COMM_ERROR = -1,
17 COMM_NOMESSAGE = -3,
18 COMM_TIMEOUT = -4,
19 COMM_SHUTDOWN = -5,
20 COMM_IDLE = -6, /* there are no active fds and no pending callbacks. */
21 COMM_INPROGRESS = -7,
22 COMM_ERR_CONNECT = -8,
23 COMM_ERR_DNS = -9,
24 COMM_ERR_CLOSING = -10,
25 #if USE_IPV6
26 COMM_ERR_PROTOCOL = -11, /* IPv4 or IPv6 cannot be used on the fd socket */
27 #endif
28 COMM_ERR__END__ = -999999 /* Dummy entry to make syntax valid (comma on line above), do not use. New entries added above */
29 } comm_err_t;
30
31 class DnsLookupDetails;
32 typedef void CNCB(int fd, const DnsLookupDetails &dns, comm_err_t status, int xerrno, void *data);
33
34 typedef void IOCB(int fd, char *, size_t size, comm_err_t flag, int xerrno, void *data);
35
36
37 /* comm.c */
38 extern bool comm_iocallbackpending(void); /* inline candidate */
39
40 SQUIDCEXTERN int commSetNonBlocking(int fd);
41 SQUIDCEXTERN int commUnsetNonBlocking(int fd);
42 SQUIDCEXTERN void commSetCloseOnExec(int fd);
43 SQUIDCEXTERN void commSetTcpKeepalive(int fd, int idle, int interval, int timeout);
44 extern void _comm_close(int fd, char const *file, int line);
45 #define comm_close(fd) (_comm_close((fd), __FILE__, __LINE__))
46 SQUIDCEXTERN void comm_reset_close(int fd);
47 #if LINGERING_CLOSE
48 SQUIDCEXTERN void comm_lingering_close(int fd);
49 #endif
50 SQUIDCEXTERN void commConnectStart(int fd, const char *, u_short, CNCB *, void *);
51 void commConnectStart(int fd, const char *, u_short, AsyncCall::Pointer &cb);
52
53 SQUIDCEXTERN int comm_connect_addr(int sock, const IpAddress &addr);
54 SQUIDCEXTERN void comm_init(void);
55 SQUIDCEXTERN void comm_exit(void);
56
57 SQUIDCEXTERN int comm_open(int, int, IpAddress &, int, const char *note);
58 SQUIDCEXTERN int comm_open_uds(int sock_type, int proto, struct sockaddr_un* addr, int flags);
59 /// update Comm state after getting a comm_open() FD from another process
60 SQUIDCEXTERN void comm_import_opened(int fd, IpAddress &addr, int flags, const char *note, struct addrinfo *AI);
61
62 /**
63 * Open a port specially bound for listening or sending through a specific port.
64 * This is a wrapper providing IPv4/IPv6 failover around comm_openex().
65 * Please use for all listening sockets and bind() outbound sockets.
66 *
67 * It will open a socket bound for:
68 * - IPv4 if IPv6 is disabled or address is IPv4-native.
69 * - IPv6 if address is IPv6-native
70 * - IPv6 dual-stack mode if able to open [::]
71 *
72 * When an open performs failover it update the given address to feedback
73 * the new IPv4-only status of the socket. Further displays of the IP
74 * (in debugs or cachemgr) will occur in Native IPv4 format.
75 * A reconfigure is needed to reset the stored IP in most cases and attempt a port re-open.
76 */
77 SQUIDCEXTERN int comm_open_listener(int sock_type, int proto, IpAddress &addr, int flags, const char *note);
78
79 SQUIDCEXTERN int comm_openex(int, int, IpAddress &, int, unsigned char TOS, const char *);
80 SQUIDCEXTERN u_short comm_local_port(int fd);
81 SQUIDCEXTERN int comm_set_tos(int fd, int tos);
82
83 SQUIDCEXTERN void commSetSelect(int, unsigned int, PF *, void *, time_t);
84 SQUIDCEXTERN void commResetSelect(int);
85
86 SQUIDCEXTERN int comm_udp_sendto(int sock, const IpAddress &to, const void *buf, int buflen);
87 extern void comm_write(int fd, const char *buf, int len, IOCB *callback, void *callback_data, FREE *func);
88 extern void comm_write(int fd, const char *buf, int size, AsyncCall::Pointer &callback, FREE * free_func = NULL);
89 SQUIDCEXTERN void comm_write_mbuf(int fd, MemBuf *mb, IOCB * handler, void *handler_data);
90 extern void comm_write_mbuf(int fd, MemBuf *mb, AsyncCall::Pointer &callback);
91 SQUIDCEXTERN void commCallCloseHandlers(int fd);
92 SQUIDCEXTERN int commSetTimeout(int fd, int, PF *, void *);
93 extern int commSetTimeout(int fd, int, AsyncCall::Pointer &calback);
94 SQUIDCEXTERN int ignoreErrno(int);
95 SQUIDCEXTERN void commCloseAllSockets(void);
96 SQUIDCEXTERN void checkTimeouts(void);
97
98
99 /*
100 * comm_select.c
101 */
102 SQUIDCEXTERN void comm_select_init(void);
103 SQUIDCEXTERN comm_err_t comm_select(int);
104 SQUIDCEXTERN void comm_quick_poll_required(void);
105
106 class ConnectionDetail;
107 typedef void IOACB(int fd, int nfd, ConnectionDetail *details, comm_err_t flag, int xerrno, void *data);
108 extern void comm_add_close_handler(int fd, PF *, void *);
109 extern void comm_add_close_handler(int fd, AsyncCall::Pointer &);
110 extern void comm_remove_close_handler(int fd, PF *, void *);
111 extern void comm_remove_close_handler(int fd, AsyncCall::Pointer &);
112
113
114 extern int comm_has_pending_read_callback(int fd);
115 extern bool comm_monitors_read(int fd);
116 extern void comm_read(int fd, char *buf, int len, IOCB *handler, void *data);
117 extern void comm_read(int fd, char *buf, int len, AsyncCall::Pointer &callback);
118 extern void comm_read_cancel(int fd, IOCB *callback, void *data);
119 extern void comm_read_cancel(int fd, AsyncCall::Pointer &callback);
120 extern int comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, IpAddress &from);
121 extern int comm_udp_recv(int fd, void *buf, size_t len, int flags);
122 extern ssize_t comm_udp_send(int s, const void *buf, size_t len, int flags);
123 extern bool comm_has_incomplete_write(int);
124
125 /** The read channel has closed and the caller does not expect more data
126 * but needs to detect connection aborts. The current detection method uses
127 * 0-length reads: We read until the error occurs or the writer closes
128 * the connection. If there is a read error, we close the connection.
129 */
130 extern void commStartHalfClosedMonitor(int fd);
131 extern bool commHasHalfClosedMonitor(int fd);
132 // XXX: remove these wrappers which minimize client_side.cc changes in a commit
133 inline void commMarkHalfClosed(int fd) { commStartHalfClosedMonitor(fd); }
134 inline bool commIsHalfClosed(int fd) { return commHasHalfClosedMonitor(fd); }
135
136 /* A comm engine that calls comm_select */
137
138 class CommSelectEngine : public AsyncEngine
139 {
140
141 public:
142 virtual int checkEvents(int timeout);
143 };
144
145 #endif