]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/fdmover.h
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / fdmover.h
1 /*
2 * Copyright (c) 2000-2001,2003-2004,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // fdmover - send/receive file descriptors over a UNIX domain socket connection
27 //
28 // An FdMover object is a very specialized Socket:
29 // It must be bound to a UNIX domain address
30 //
31 #ifndef _H_FDMOVER
32 #define _H_FDMOVER
33
34 #include "ip++.h"
35 #include <vector>
36
37 using namespace UnixPlusPlus;
38
39
40 namespace Security {
41 namespace IPPlusPlus {
42
43
44 // an ordered list of file descriptors
45 typedef std::vector<FileDesc> FdVector;
46
47
48 //
49 // An FdMover - a specialized Socket for transferring file descriptors
50 // across UNIX domain sockets.
51 //
52 class FdMover : public Socket {
53 private:
54 class Element : public cmsghdr {
55 public:
56 void *operator new (size_t base, ssize_t more);
57 void operator delete (void *addr, ssize_t size);
58 void operator delete (void *addr, size_t size);
59
60 Element() { }
61 Element(int level, int type);
62
63 template <class T> T &payload() { return *reinterpret_cast<T *>(CMSG_DATA(this)); }
64 size_t payloadSize() const { return cmsg_len - ((caddr_t)CMSG_DATA(this) - (caddr_t)this); }
65 };
66
67 class Message : public msghdr {
68 public:
69 Message(const void *data, size_t length);
70 void set(Element *elem);
71 Element *element() const { return (Element *)msg_control; }
72 Element *next(Element *elem) const { return (Element *)CMSG_NXTHDR(this, elem); }
73
74 public:
75 IOVec iovec;
76 };
77
78 public:
79 FdMover() { }
80 FdMover(Socket s) : Socket(s) { }
81
82 size_t send(const void *data, size_t length, const FdVector &fds);
83 size_t receive(void *data, size_t length, FdVector &fds);
84
85 private:
86
87 };
88
89
90 } // end namespace IPPlusPlus
91 } // end namespace Security
92
93
94 #endif //_H_FDMOVER