]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/TypedMsgHdr.cc
4 * DEBUG: section 54 Interprocess Communication
12 #include "base/TextException.h"
13 #include "ipc/TypedMsgHdr.h"
15 Ipc::TypedMsgHdr::TypedMsgHdr()
17 xmemset(this, 0, sizeof(*this));
21 Ipc::TypedMsgHdr::TypedMsgHdr(const TypedMsgHdr
&tmh
)
23 memcpy(this, &tmh
, sizeof(*this));
27 Ipc::TypedMsgHdr
&Ipc::TypedMsgHdr::operator =(const TypedMsgHdr
&tmh
)
29 if (this != &tmh
) { // skip assignment to self
30 memcpy(this, &tmh
, sizeof(*this));
36 // update msghdr and ios pointers based on msghdr counters
37 void Ipc::TypedMsgHdr::sync()
39 if (msg_name
) { // we have a name
42 Must(!msg_namelen
&& !msg_name
);
45 if (msg_iov
) { // we have a data component
46 Must(msg_iovlen
== 1);
48 ios
[0].iov_base
= &data
;
49 Must(ios
[0].iov_len
== sizeof(data
));
51 Must(!msg_iovlen
&& !msg_iov
);
54 if (msg_control
) { // we have a control component
55 Must(msg_controllen
> 0);
58 Must(!msg_controllen
&& !msg_control
);
66 Ipc::TypedMsgHdr::type() const
68 Must(msg_iovlen
== 1);
73 Ipc::TypedMsgHdr::address(const struct sockaddr_un
& addr
)
78 msg_namelen
= SUN_LEN(&name
);
82 Ipc::TypedMsgHdr::checkType(int destType
) const
84 Must(type() == destType
);
88 Ipc::TypedMsgHdr::setType(int aType
)
91 Must(data
.type_
== aType
);
99 Ipc::TypedMsgHdr::getInt() const
107 Ipc::TypedMsgHdr::putInt(const int n
)
113 Ipc::TypedMsgHdr::getString(String
&s
) const
115 const int length
= getInt();
117 // String uses memcpy uncoditionally; TODO: SBuf eliminates this check
123 Must(length
<= maxSize
);
124 // TODO: use SBuf.reserve() instead of a temporary buffer
126 getRaw(&buf
, length
);
127 s
.limitInit(buf
, length
);
131 Ipc::TypedMsgHdr::putString(const String
&s
)
133 Must(s
.psize() <= maxSize
);
135 putRaw(s
.rawBuf(), s
.psize());
139 Ipc::TypedMsgHdr::getFixed(void *raw
, size_t size
) const
141 // no need to load size because it is constant
146 Ipc::TypedMsgHdr::putFixed(const void *raw
, size_t size
)
148 // no need to store size because it is constant
152 /// low-level loading of exactly size bytes of raw data
154 Ipc::TypedMsgHdr::getRaw(void *raw
, size_t size
) const
158 Must(size
<= data
.size
- offset
);
159 memcpy(raw
, data
.raw
+ offset
, size
);
164 /// low-level storage of exactly size bytes of raw data
166 Ipc::TypedMsgHdr::putRaw(const void *raw
, size_t size
)
170 Must(size
<= sizeof(data
.raw
) - data
.size
);
171 memcpy(data
.raw
+ data
.size
, raw
, size
);
177 Ipc::TypedMsgHdr::putFd(int fd
)
182 const int fdCount
= 1;
184 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(this);
185 cmsg
->cmsg_level
= SOL_SOCKET
;
186 cmsg
->cmsg_type
= SCM_RIGHTS
;
187 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * fdCount
);
189 int *fdStore
= reinterpret_cast<int*>(CMSG_DATA(cmsg
));
190 memcpy(fdStore
, &fd
, fdCount
* sizeof(int));
191 msg_controllen
= cmsg
->cmsg_len
;
195 Ipc::TypedMsgHdr::getFd() const
197 Must(msg_control
&& msg_controllen
);
199 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(this);
200 Must(cmsg
->cmsg_level
== SOL_SOCKET
);
201 Must(cmsg
->cmsg_type
== SCM_RIGHTS
);
203 const int fdCount
= 1;
204 const int *fdStore
= reinterpret_cast<const int*>(CMSG_DATA(cmsg
));
206 memcpy(&fd
, fdStore
, fdCount
* sizeof(int));
211 Ipc::TypedMsgHdr::prepForReading()
213 xmemset(this, 0, sizeof(*this));
219 /// initialize io vector with one io record
221 Ipc::TypedMsgHdr::allocData()
223 Must(!msg_iovlen
&& !msg_iov
);
226 ios
[0].iov_base
= &data
;
227 ios
[0].iov_len
= sizeof(data
);
233 Ipc::TypedMsgHdr::allocName()
235 Must(!msg_name
&& !msg_namelen
);
237 msg_namelen
= sizeof(name
); // is that the right size?
241 Ipc::TypedMsgHdr::allocControl()
243 Must(!msg_control
&& !msg_controllen
);
245 msg_controllen
= sizeof(ctrl
);