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