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