]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/TypedMsgHdr.h
Merged from trunk (r13356).
[thirdparty/squid.git] / src / ipc / TypedMsgHdr.h
1 /*
2 * DEBUG: section 54 Interprocess Communication
3 *
4 */
5
6 #ifndef SQUID_IPC_TYPED_MSG_HDR_H
7 #define SQUID_IPC_TYPED_MSG_HDR_H
8
9 #include "compat/cmsg.h"
10 #if HAVE_SYS_SOCKET_H
11 #include <sys/socket.h>
12 #endif
13 #if HAVE_SYS_UIO_H
14 #include <sys/uio.h>
15 #endif
16 #if HAVE_SYS_UN_H
17 #include <sys/un.h>
18 #endif
19
20 class String;
21
22 namespace Ipc
23 {
24
25 /// struct msghdr with a known type, fixed-size I/O and control buffers
26 class TypedMsgHdr: public msghdr
27 {
28 public:
29 enum {maxSize = 4096};
30
31 public:
32 TypedMsgHdr();
33 TypedMsgHdr(const TypedMsgHdr &tmh);
34 TypedMsgHdr &operator =(const TypedMsgHdr &tmh);
35
36 void address(const struct sockaddr_un &addr); ///< sets [dest.] address
37
38 /* message type manipulation; these must be called before put/get*() */
39 void setType(int aType); ///< sets message type; use MessageType enum
40 void checkType(int aType) const; ///< throws if stored type is not aType
41 int type() const; ///< returns stored type or zero if none
42
43 /* access for Plain Old Data (POD)-based message parts */
44 template <class Pod>
45 void getPod(Pod &pod) const { getFixed(&pod, sizeof(pod)); } ///< load POD
46 template <class Pod>
47 void putPod(const Pod &pod) { putFixed(&pod, sizeof(pod)); } ///< store POD
48
49 /* access to message parts for selected commonly-used part types */
50 void getString(String &s) const; ///< load variable-length string
51 void putString(const String &s); ///< store variable-length string
52 int getInt() const; ///< load an integer
53 void putInt(int n); ///< store an integer
54 void getFixed(void *raw, size_t size) const; ///< always load size bytes
55 void putFixed(const void *raw, size_t size); ///< always store size bytes
56
57 /// returns true if there is data to extract; handy for optional parts
58 bool hasMoreData() const { return offset < data.size; }
59
60 /* access to a "file" descriptor that can be passed between processes */
61 void putFd(int aFd); ///< stores descriptor
62 int getFd() const; ///< returns stored descriptor
63 bool hasFd() const; ///< whether the message has a descriptor stored
64
65 /* raw, type-independent access for I/O */
66 void prepForReading(); ///< reset and provide all buffers
67 char *raw() { return reinterpret_cast<char*>(this); }
68 const char *raw() const { return reinterpret_cast<const char*>(this); }
69 size_t size() const { return sizeof(*this); } ///< not true message size
70
71 private:
72 void sync();
73 void allocData();
74 void allocName();
75 void allocControl();
76
77 /* raw, type-independent manipulation used by type-specific methods */
78 void getRaw(void *raw, size_t size) const;
79 void putRaw(const void *raw, size_t size);
80
81 private:
82 struct sockaddr_un name; ///< same as .msg_name
83
84 struct iovec ios[1]; ///< same as .msg_iov[]
85
86 struct DataBuffer {
87 int type_; ///< Message kind, uses MessageType values
88 size_t size; ///< actual raw data size (for sanity checks)
89 char raw[maxSize]; ///< buffer with type-specific data
90 } data; ///< same as .msg_iov[0].iov_base
91
92 struct CtrlBuffer {
93 /// control buffer space for one fd
94 char raw[SQUID_CMSG_SPACE(sizeof(int))];
95 } ctrl; ///< same as .msg_control
96
97 /// data offset for the next get/put*() to start with
98 mutable unsigned int offset;
99 };
100
101 } // namespace Ipc
102
103 #endif /* SQUID_IPC_TYPED_MSG_HDR_H */