TypedMsgHdr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 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_IPC_TYPED_MSG_HDR_H
12 #define SQUID_IPC_TYPED_MSG_HDR_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 class String;
27 
28 namespace Ipc
29 {
30 
32 class TypedMsgHdr: public msghdr
33 {
34 public:
35  enum {maxSize = 4096};
36 
37 public:
38  TypedMsgHdr();
39  TypedMsgHdr(const TypedMsgHdr &tmh);
40  TypedMsgHdr &operator =(const TypedMsgHdr &tmh);
41 
42  void address(const struct sockaddr_un &addr);
43 
44  /* message type manipulation; these must be called before put/get*() */
45  void setType(int aType);
46  void checkType(int aType) const;
47  int rawType() const { return msg_iov ? data.type_ : 0; }
50 
51  /* access for TriviallyCopyable (a.k.a. Plain Old Data or POD) message parts */
52  template <class Pod> void getPod(Pod &pod) const;
53  template <class Pod> void putPod(const Pod &pod);
54 
55  /* access to message parts for selected commonly-used part types */
56  void getString(String &s) const;
57  void putString(const String &s);
58  int getInt() const;
59  void putInt(int n);
60  void getFixed(void *raw, size_t size) const;
61  void putFixed(const void *raw, size_t size);
62 
64  bool hasMoreData() const { return offset < data.size; }
65 
66  /* access to a "file" descriptor that can be passed between processes */
67  void putFd(int aFd);
68  int getFd() const;
69  bool hasFd() const;
70 
71  /* raw, type-independent access for I/O */
72  void prepForReading();
73  char *raw() { return reinterpret_cast<char*>(this); }
74  const char *raw() const { return reinterpret_cast<const char*>(this); }
75  size_t size() const { return sizeof(*this); }
76 
77 private:
78  void clear();
79  void sync();
80  void allocData();
81  void allocName();
82  void allocControl();
83 
84  /* raw, type-independent manipulation used by type-specific methods */
85  void getRaw(void *raw, size_t size) const;
86  void putRaw(const void *raw, size_t size);
87 
88 private:
89  struct sockaddr_un name;
90 
91  struct iovec ios[1];
92 
93  struct DataBuffer {
94  DataBuffer() { memset(raw, 0, sizeof(raw)); }
95 
96  int type_ = 0;
97  size_t size = 0;
98  char raw[maxSize];
99  } data;
100 
101  struct CtrlBuffer {
102  CtrlBuffer() { memset(raw, 0, sizeof(raw)); }
103 
105  char raw[SQUID_CMSG_SPACE(sizeof(int))];
106  } ctrl;
107 
109  mutable unsigned int offset = 0;
110 };
111 
112 } // namespace Ipc
113 
114 template <class Pod>
115 void
117 {
118 #if HAVE_STD_IS_TRIVIALLY_COPYABLE
119  // TODO: Enable after fixing Ipc::SharedListenRequest::SharedListenRequest()
120  //static_assert(std::is_trivially_copyable<Pod>::value, "getPod() used for a POD");
121 #endif
122  getFixed(&pod, sizeof(pod));
123 }
124 
125 template <class Pod>
126 void
128 {
129 #if HAVE_STD_IS_TRIVIALLY_COPYABLE
130  // TODO: Enable after fixing Ipc::SharedListenRequest::pack()
131  //static_assert(std::is_trivially_copyable<Pod>::value, "putPod() used for a POD");
132 #endif
133  putFixed(&pod, sizeof(pod));
134 }
135 
136 #endif /* SQUID_IPC_TYPED_MSG_HDR_H */
137 
unsigned int offset
data offset for the next get/put*() to start with
Definition: TypedMsgHdr.h:109
bool hasMoreData() const
returns true if there is data to extract; handy for optional parts
Definition: TypedMsgHdr.h:64
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:49
#define SQUID_CMSG_SPACE
Definition: cmsg.h:136
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:127
void prepForReading()
reset and provide all buffers
Definition: TypedMsgHdr.cc:234
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:119
char raw[maxSize]
buffer with type-specific data
Definition: TypedMsgHdr.h:98
size_t size() const
not true message size
Definition: TypedMsgHdr.h:75
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:89
struct iovec ios[1]
same as .msg_iov[]
Definition: TypedMsgHdr.h:91
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:74
Definition: cmsg.h:88
int type_
Message kind, uses MessageType values.
Definition: TypedMsgHdr.h:96
size_t size
actual raw data size (for sanity checks)
Definition: TypedMsgHdr.h:97
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:33
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:116
char raw[SQUID_CMSG_SPACE(sizeof(int))]
control buffer space for one fd
Definition: TypedMsgHdr.h:105
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:24
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

Web Site Translations

Mirrors