TypedMsgHdr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 #if HAVE_SYS_SOCKET_H
16 #include <sys/socket.h>
17 #endif
18 #if HAVE_SYS_UIO_H
19 #include <sys/uio.h>
20 #endif
21 #if HAVE_SYS_UN_H
22 #include <sys/un.h>
23 #endif
24 
25 class String;
26 
27 namespace Ipc
28 {
29 
31 class TypedMsgHdr: public msghdr
32 {
33 public:
34  enum {maxSize = 4096};
35 
36 public:
37  TypedMsgHdr();
38  TypedMsgHdr(const TypedMsgHdr &tmh);
39  TypedMsgHdr &operator =(const TypedMsgHdr &tmh);
40 
41  void address(const struct sockaddr_un &addr);
42 
43  /* message type manipulation; these must be called before put/get*() */
44  void setType(int aType);
45  void checkType(int aType) const;
46  int type() const;
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)); }
51  template <class Pod>
52  void putPod(const Pod &pod) { putFixed(&pod, sizeof(pod)); }
53 
54  /* access to message parts for selected commonly-used part types */
55  void getString(String &s) const;
56  void putString(const String &s);
57  int getInt() const;
58  void putInt(int n);
59  void getFixed(void *raw, size_t size) const;
60  void putFixed(const void *raw, size_t size);
61 
63  bool hasMoreData() const { return offset < data.size; }
64 
65  /* access to a "file" descriptor that can be passed between processes */
66  void putFd(int aFd);
67  int getFd() const;
68  bool hasFd() const;
69 
70  /* raw, type-independent access for I/O */
71  void prepForReading();
72  char *raw() { return reinterpret_cast<char*>(this); }
73  const char *raw() const { return reinterpret_cast<const char*>(this); }
74  size_t size() const { return sizeof(*this); }
75 
76 private:
77  void sync();
78  void allocData();
79  void allocName();
80  void allocControl();
81 
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 
86 private:
87  struct sockaddr_un name;
88 
89  struct iovec ios[1];
90 
91  struct DataBuffer {
92  int type_;
93  size_t size;
94  char raw[maxSize];
95  } data;
96 
97  struct CtrlBuffer {
99  char raw[SQUID_CMSG_SPACE(sizeof(int))];
100  } ctrl;
101 
103  mutable unsigned int offset;
104 };
105 
106 } // namespace Ipc
107 
108 #endif /* SQUID_IPC_TYPED_MSG_HDR_H */
109 
char raw[maxSize]
buffer with type-specific data
Definition: TypedMsgHdr.h:94
void prepForReading()
reset and provide all buffers
Definition: TypedMsgHdr.cc:224
void putInt(int n)
store an integer
Definition: TypedMsgHdr.cc:109
void getPod(Pod &pod) const
load POD
Definition: TypedMsgHdr.h:50
void putPod(const Pod &pod)
store POD
Definition: TypedMsgHdr.h:52
#define SQUID_CMSG_SPACE
Definition: cmsg.h:136
unsigned int offset
data offset for the next get/put*() to start with
Definition: TypedMsgHdr.h:103
void getFixed(void *raw, size_t size) const
always load size bytes
Definition: TypedMsgHdr.cc:141
struct sockaddr_un name
same as .msg_name
Definition: TypedMsgHdr.h:87
bool hasFd() const
whether the message has a descriptor stored
Definition: TypedMsgHdr.cc:177
struct iovec ios[1]
same as .msg_iov[]
Definition: TypedMsgHdr.h:89
void * addr
Definition: membanger.c:46
struct Ipc::TypedMsgHdr::CtrlBuffer ctrl
same as .msg_control
int getInt() const
load an integer
Definition: TypedMsgHdr.cc:101
size_t size
actual raw data size (for sanity checks)
Definition: TypedMsgHdr.h:93
size_t size() const
not true message size
Definition: TypedMsgHdr.h:74
void address(const struct sockaddr_un &addr)
sets [dest.] address
Definition: TypedMsgHdr.cc:75
void putFd(int aFd)
stores descriptor
Definition: TypedMsgHdr.cc:186
void putString(const String &s)
store variable-length string
Definition: TypedMsgHdr.cc:133
void allocData()
initialize io vector with one io record
Definition: TypedMsgHdr.cc:234
struct msghdr with a known type, fixed-size I/O and control buffers
Definition: TypedMsgHdr.h:31
void checkType(int aType) const
throws if stored type is not aType
Definition: TypedMsgHdr.cc:84
Definition: cmsg.h:88
int type() const
returns stored type or zero if none
Definition: TypedMsgHdr.cc:68
TypedMsgHdr & operator=(const TypedMsgHdr &tmh)
Definition: TypedMsgHdr.cc:31
void getString(String &s) const
load variable-length string
Definition: TypedMsgHdr.cc:115
void getRaw(void *raw, size_t size) const
low-level loading of exactly size bytes of raw data
Definition: TypedMsgHdr.cc:156
int getFd() const
returns stored descriptor
Definition: TypedMsgHdr.cc:207
void putRaw(const void *raw, size_t size)
low-level storage of exactly size bytes of raw data
Definition: TypedMsgHdr.cc:167
char raw[SQUID_CMSG_SPACE(sizeof(int))]
control buffer space for one fd
Definition: TypedMsgHdr.h:99
void setType(int aType)
sets message type; use MessageType enum
Definition: TypedMsgHdr.cc:90
int type_
Message kind, uses MessageType values.
Definition: TypedMsgHdr.h:92
const char * raw() const
Definition: TypedMsgHdr.h:73
void putFixed(const void *raw, size_t size)
always store size bytes
Definition: TypedMsgHdr.cc:148
struct Ipc::TypedMsgHdr::DataBuffer data
same as .msg_iov[0].iov_base
bool hasMoreData() const
returns true if there is data to extract; handy for optional parts
Definition: TypedMsgHdr.h:63
Definition: cmsg.h:81

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors