TypedMsgHdr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
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.
7  */
8 
9 /* DEBUG: section 54 Interprocess Communication */
10 
11 #ifndef SQUID_SRC_IPC_TYPEDMSGHDR_H
12 #define SQUID_SRC_IPC_TYPEDMSGHDR_H
13 
14 #include "compat/cmsg.h"
15 #include "ipc/Messages.h"
16 #if HAVE_SYS_SOCKET_H
17 #include <sys/socket.h>
18 #endif
19 #if HAVE_SYS_UIO_H
20 #include <sys/uio.h>
21 #endif
22 #if HAVE_SYS_UN_H
23 #include <sys/un.h>
24 #endif
25 
26 #include <type_traits>
27 
28 class String;
29 
30 namespace Ipc
31 {
32 
34 class TypedMsgHdr: public msghdr
35 {
36 public:
37  enum {maxSize = 4096};
38 
39 public:
40  TypedMsgHdr();
41  TypedMsgHdr(const TypedMsgHdr &tmh);
42  TypedMsgHdr &operator =(const TypedMsgHdr &tmh);
43 
44  void address(const struct sockaddr_un &addr);
45 
46  /* message type manipulation; these must be called before put/get*() */
47  void setType(int aType);
48  void checkType(int aType) const;
49  int rawType() const { return msg_iov ? data.type_ : 0; }
52 
53  /* access for TriviallyCopyable (a.k.a. Plain Old Data or POD) message parts */
54  template <class Pod> void getPod(Pod &pod) const;
55  template <class Pod> void putPod(const Pod &pod);
56 
57  /* access to message parts for selected commonly-used part types */
58  void getString(String &s) const;
59  void putString(const String &s);
60  int getInt() const;
61  void putInt(int n);
62  void getFixed(void *raw, size_t size) const;
63  void putFixed(const void *raw, size_t size);
64 
66  bool hasMoreData() const { return offset < data.size; }
67 
68  /* access to a "file" descriptor that can be passed between processes */
69  void putFd(int aFd);
70  int getFd() const;
71  bool hasFd() const;
72 
73  /* raw, type-independent access for I/O */
74  void prepForReading();
75  char *raw() { return reinterpret_cast<char*>(this); }
76  const char *raw() const { return reinterpret_cast<const char*>(this); }
77  size_t size() const { return sizeof(*this); }
78 
79 private:
80  void clear();
81  void sync();
82  void allocData();
83  void allocName();
84  void allocControl();
85 
86  /* raw, type-independent manipulation used by type-specific methods */
87  void getRaw(void *raw, size_t size) const;
88  void putRaw(const void *raw, size_t size);
89 
90 private:
91  struct sockaddr_un name;
92 
93  struct iovec ios[1];
94 
95  struct DataBuffer {
96  DataBuffer() { memset(raw, 0, sizeof(raw)); }
97 
98  int type_ = 0;
99  size_t size = 0;
100  char raw[maxSize];
101  } data;
102 
103  struct CtrlBuffer {
104  CtrlBuffer() { memset(raw, 0, sizeof(raw)); }
105 
107  char raw[SQUID_CMSG_SPACE(sizeof(int))];
108  } ctrl;
109 
111  mutable unsigned int offset = 0;
112 };
113 
114 } // namespace Ipc
115 
116 template <class Pod>
117 void
119 {
120  static_assert(std::is_trivially_copyable<Pod>::value, "getPod() used for a POD");
121  getFixed(&pod, sizeof(pod));
122 }
123 
124 template <class Pod>
125 void
127 {
128  static_assert(std::is_trivially_copyable<Pod>::value, "putPod() used for a POD");
129  putFixed(&pod, sizeof(pod));
130 }
131 
132 #endif /* SQUID_SRC_IPC_TYPEDMSGHDR_H */
133 
unsigned int offset
data offset for the next get/put*() to start with
Definition: TypedMsgHdr.h:111
bool hasMoreData() const
returns true if there is data to extract; handy for optional parts
Definition: TypedMsgHdr.h:66
void setType(int aType)
sets message type; use MessageType enum
Definition: TypedMsgHdr.cc:100
void putFixed(const void *raw, size_t size)
always store size bytes
Definition: TypedMsgHdr.cc:158
void checkType(int aType) const
Definition: TypedMsgHdr.cc:94
struct Ipc::TypedMsgHdr::DataBuffer data
same as .msg_iov[0].iov_base
bool hasFd() const
whether the message has a descriptor stored
Definition: TypedMsgHdr.cc:187
int rawType() const
Definition: TypedMsgHdr.h:51
#define SQUID_CMSG_SPACE
Definition: cmsg.h:136
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:126
void prepForReading()
reset and provide all buffers
Definition: TypedMsgHdr.cc:234
int size
Definition: ModDevPoll.cc:69
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:119
char raw[maxSize]
buffer with type-specific data
Definition: TypedMsgHdr.h:100
int getInt() const
load an integer
Definition: TypedMsgHdr.cc:111
int getFd() const
returns stored descriptor
Definition: TypedMsgHdr.cc:217
struct sockaddr_un name
same as .msg_name
Definition: TypedMsgHdr.h:91
struct iovec ios[1]
same as .msg_iov[]
Definition: TypedMsgHdr.h:93
void getString(String &s) const
load variable-length string
Definition: TypedMsgHdr.cc:125
void getRaw(void *raw, size_t size) const
low-level loading of exactly size bytes of raw data
Definition: TypedMsgHdr.cc:166
Definition: cmsg.h:81
const char * raw() const
Definition: TypedMsgHdr.h:76
Definition: cmsg.h:88
int type_
Message kind, uses MessageType values.
Definition: TypedMsgHdr.h:98
size_t size
actual raw data size (for sanity checks)
Definition: TypedMsgHdr.h:99
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:34
void putFd(int aFd)
stores descriptor
Definition: TypedMsgHdr.cc:196
void allocData()
initialize io vector with one io record
Definition: TypedMsgHdr.cc:246
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:118
char raw[SQUID_CMSG_SPACE(sizeof(int))]
control buffer space for one fd
Definition: TypedMsgHdr.h:107
void address(const struct sockaddr_un &addr)
sets [dest.] address
Definition: TypedMsgHdr.cc:85
void putString(const String &s)
store variable-length string
Definition: TypedMsgHdr.cc:143
void getFixed(void *raw, size_t size) const
always load size bytes
Definition: TypedMsgHdr.cc:151
TypedMsgHdr & operator=(const TypedMsgHdr &tmh)
Definition: TypedMsgHdr.cc:31
struct iovec * msg_iov
Definition: cmsg.h:92
Definition: IpcIoFile.h:23
void putRaw(const void *raw, size_t size)
low-level storage of exactly size bytes of raw data
Definition: TypedMsgHdr.cc:177
struct Ipc::TypedMsgHdr::CtrlBuffer ctrl
same as .msg_control

 

Introduction

Documentation

Support

Miscellaneous