]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/fdmover.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // ip++ - C++ layer for IP socket and address management
22 // [Also see comments in header file.]
26 #include <Security/debugging.h>
30 namespace IPPlusPlus
{
33 void *FdMover::Element::operator new (size_t base
, size_t more
)
35 Element
*element
= (Element
*)::malloc(CMSG_SPACE(more
));
36 element
->cmsg_len
= CMSG_LEN(more
);
40 void FdMover::Element::operator delete (void *data
, size_t base
)
45 FdMover::Element::Element(int level
, int type
)
52 FdMover::Message::Message(const void *data
, size_t length
)
64 void FdMover::Message::set(Element
*elem
)
66 msg_control
= (caddr_t
)elem
;
67 msg_controllen
= elem
->cmsg_len
;
71 size_t FdMover::send(const void *data
, size_t length
, const FdVector
&fds
)
73 auto_ptr
<Element
> elem(new (fds
.size() * sizeof(int)) Element (SOL_SOCKET
, SCM_RIGHTS
));
74 copy(fds
.begin(), fds
.end(), &elem
.get()->payload
<int>());
75 Message
msg(data
, length
);
77 ssize_t rc
= ::sendmsg(fd(), &msg
, 0);
83 size_t FdMover::receive(void *data
, size_t length
, FdVector
&fds
)
85 static const int maxFds
= 20; // arbitrary limit
86 Message
msg(data
, length
);
87 auto_ptr
<Element
> elem(new (maxFds
* sizeof(int)) Element
);
89 ssize_t rc
= ::recvmsg(fd(), &msg
, 0);
91 unsigned count
= elem
.get()->payloadSize() / sizeof(int);
93 copy(&elem
.get()->payload
<int>(), &elem
.get()->payload
<int>() + count
, back_inserter(result
));
99 } // end namespace IPPlusPlus
100 } // end namespace Security