]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/TypedMsgHdr.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 54 Interprocess Communication */
12 #include "base/TextException.h"
13 #include "ipc/TypedMsgHdr.h"
18 Ipc::TypedMsgHdr::TypedMsgHdr()
20 memset(this, 0, sizeof(*this));
24 Ipc::TypedMsgHdr::TypedMsgHdr(const TypedMsgHdr
&tmh
)
26 memcpy(this, &tmh
, sizeof(*this));
30 Ipc::TypedMsgHdr
&Ipc::TypedMsgHdr::operator =(const TypedMsgHdr
&tmh
)
32 if (this != &tmh
) { // skip assignment to self
33 memcpy(this, &tmh
, sizeof(*this));
39 // update msghdr and ios pointers based on msghdr counters
40 void Ipc::TypedMsgHdr::sync()
42 if (msg_name
) { // we have a name
45 Must(!msg_namelen
&& !msg_name
);
48 if (msg_iov
) { // we have a data component
49 Must(msg_iovlen
== 1);
51 ios
[0].iov_base
= &data
;
52 Must(ios
[0].iov_len
== sizeof(data
));
54 Must(!msg_iovlen
&& !msg_iov
);
57 if (msg_control
) { // we have a control component
58 Must(msg_controllen
> 0);
61 Must(!msg_controllen
&& !msg_control
);
67 Ipc::TypedMsgHdr::type() const
69 Must(msg_iovlen
== 1);
74 Ipc::TypedMsgHdr::address(const struct sockaddr_un
& addr
)
79 msg_namelen
= SUN_LEN(&name
);
83 Ipc::TypedMsgHdr::checkType(int destType
) const
85 Must(type() == destType
);
89 Ipc::TypedMsgHdr::setType(int aType
)
92 Must(data
.type_
== aType
);
100 Ipc::TypedMsgHdr::getInt() const
108 Ipc::TypedMsgHdr::putInt(const int n
)
114 Ipc::TypedMsgHdr::getString(String
&s
) const
116 const int length
= getInt();
118 // String uses memcpy uncoditionally; TODO: SBuf eliminates this check
124 Must(length
<= maxSize
);
125 // TODO: use SBuf.reserve() instead of a temporary buffer
127 getRaw(&buf
, length
);
128 s
.limitInit(buf
, length
);
132 Ipc::TypedMsgHdr::putString(const String
&s
)
134 Must(s
.psize() <= maxSize
);
136 putRaw(s
.rawBuf(), s
.psize());
140 Ipc::TypedMsgHdr::getFixed(void *rawBuf
, size_t rawSize
) const
142 // no need to load size because it is constant
143 getRaw(rawBuf
, rawSize
);
147 Ipc::TypedMsgHdr::putFixed(const void *rawBuf
, size_t rawSize
)
149 // no need to store size because it is constant
150 putRaw(rawBuf
, rawSize
);
153 /// low-level loading of exactly size bytes of raw data
155 Ipc::TypedMsgHdr::getRaw(void *rawBuf
, size_t rawSize
) const
158 Must(rawSize
<= data
.size
- offset
);
159 memcpy(rawBuf
, data
.raw
+ offset
, rawSize
);
164 /// low-level storage of exactly size bytes of raw data
166 Ipc::TypedMsgHdr::putRaw(const void *rawBuf
, size_t rawSize
)
169 Must(rawSize
<= sizeof(data
.raw
) - data
.size
);
170 memcpy(data
.raw
+ data
.size
, rawBuf
, rawSize
);
171 data
.size
+= rawSize
;
176 Ipc::TypedMsgHdr::hasFd() const
178 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(this);
180 cmsg
->cmsg_level
== SOL_SOCKET
&&
181 cmsg
->cmsg_type
== SCM_RIGHTS
;
185 Ipc::TypedMsgHdr::putFd(int fd
)
191 const int fdCount
= 1;
193 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(this);
194 cmsg
->cmsg_level
= SOL_SOCKET
;
195 cmsg
->cmsg_type
= SCM_RIGHTS
;
196 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int) * fdCount
);
198 int *fdStore
= reinterpret_cast<int*>(CMSG_DATA(cmsg
));
199 memcpy(fdStore
, &fd
, fdCount
* sizeof(int));
200 msg_controllen
= cmsg
->cmsg_len
;
206 Ipc::TypedMsgHdr::getFd() const
208 Must(msg_control
&& msg_controllen
);
211 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR(this);
212 Must(cmsg
->cmsg_level
== SOL_SOCKET
);
213 Must(cmsg
->cmsg_type
== SCM_RIGHTS
);
215 const int fdCount
= 1;
216 const int *fdStore
= reinterpret_cast<const int*>(CMSG_DATA(cmsg
));
218 memcpy(&fd
, fdStore
, fdCount
* sizeof(int));
223 Ipc::TypedMsgHdr::prepForReading()
225 memset(this, 0, sizeof(*this));
231 /// initialize io vector with one io record
233 Ipc::TypedMsgHdr::allocData()
235 Must(!msg_iovlen
&& !msg_iov
);
238 ios
[0].iov_base
= &data
;
239 ios
[0].iov_len
= sizeof(data
);
245 Ipc::TypedMsgHdr::allocName()
247 Must(!msg_name
&& !msg_namelen
);
249 msg_namelen
= sizeof(name
); // is that the right size?
253 Ipc::TypedMsgHdr::allocControl()
255 Must(!msg_control
&& !msg_controllen
);
257 msg_controllen
= sizeof(ctrl
);