Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ace / LSOCK.cpp
blobf09bb8a3d1af391d7d23d876724a3c9720e42d00
1 #include "ace/LSOCK.h"
3 #if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)
5 #include "ace/Log_Category.h"
6 #include "ace/OS_NS_sys_socket.h"
7 #if defined (ACE_HAS_ALLOC_HOOKS)
8 # include "ace/Malloc_Base.h"
9 #endif /* ACE_HAS_ALLOC_HOOKS */
11 #if !defined (__ACE_INLINE__)
12 #include "ace/LSOCK.inl"
13 #endif /* __ACE_INLINE__ */
15 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
17 ACE_ALLOC_HOOK_DEFINE(ACE_LSOCK)
19 void
20 ACE_LSOCK::dump () const
22 #if defined (ACE_HAS_DUMP)
23 ACE_TRACE ("ACE_LSOCK::dump");
25 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
26 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("aux_handle_ = %d"), this->aux_handle_));
27 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
28 #endif /* ACE_HAS_DUMP */
31 #if defined (ACE_HAS_MSG)
32 // This routine sends an open file descriptor to <this->handle_>.
34 ssize_t
35 ACE_LSOCK::send_handle (const ACE_HANDLE handle) const
37 ACE_TRACE ("ACE_LSOCK::send_handle");
38 u_char a[2];
39 iovec iov;
40 msghdr send_msg;
41 #if !defined (ACE_LACKS_SENDMSG) && defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
42 char cmsgbuf[ACE_BSD_CONTROL_MSG_LEN];
43 cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
44 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
46 a[0] = 0xab;
47 a[1] = 0xcd;
48 iov.iov_base = (char *) a;
49 iov.iov_len = sizeof a;
50 send_msg.msg_iov = &iov;
51 send_msg.msg_iovlen = 1;
52 send_msg.msg_name = 0;
53 send_msg.msg_namelen = 0;
55 #if defined (ACE_LACKS_SENDMSG)
56 ACE_UNUSED_ARG(handle);
57 #else
58 # if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
59 cmsgptr->cmsg_level = SOL_SOCKET;
60 cmsgptr->cmsg_type = SCM_RIGHTS;
61 cmsgptr->cmsg_len = sizeof cmsgbuf;
62 send_msg.msg_control = cmsgbuf;
63 send_msg.msg_controllen = sizeof cmsgbuf;
64 ACE_HANDLE *ph = (ACE_HANDLE *) CMSG_DATA (cmsgptr);
65 *ph = handle;
66 send_msg.msg_flags = 0;
67 # else
68 send_msg.msg_accrights = (char *) &handle;
69 send_msg.msg_accrightslen = sizeof handle;
70 # endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
71 #endif /* ACE_LACKS_SENDMSG */
73 return ACE_OS::sendmsg (this->get_handle (), &send_msg, 0);
76 // This file receives an open file descriptor from <this->handle_>.
77 // Note, this routine returns -1 if problems occur, 0 if we recv a
78 // message that does not have file descriptor in it, and 1 otherwise.
80 ssize_t
81 ACE_LSOCK::recv_handle (ACE_HANDLE &handle, char *pbuf, ssize_t *len) const
83 ACE_TRACE ("ACE_LSOCK::recv_handle");
84 u_char a[2];
85 iovec iov;
86 msghdr recv_msg;
88 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
89 char cmsgbuf[ACE_BSD_CONTROL_MSG_LEN];
90 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
92 if (pbuf != 0 && len != 0)
94 iov.iov_base = pbuf;
95 iov.iov_len = *len;
97 else
99 iov.iov_base = (char *) a;
100 iov.iov_len = sizeof a;
103 recv_msg.msg_iov = &iov;
104 recv_msg.msg_iovlen = 1;
105 recv_msg.msg_name = 0;
106 recv_msg.msg_namelen = 0;
108 #ifndef ACE_LACKS_SENDMSG
109 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
110 recv_msg.msg_control = cmsgbuf;
111 recv_msg.msg_controllen = sizeof cmsgbuf;
112 #else
113 recv_msg.msg_accrights = (char *) &handle;
114 recv_msg.msg_accrightslen = sizeof handle;
115 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
116 #endif /* ACE_LACKS_SENDMSG */
118 #if defined (ACE_HAS_STREAMS)
120 ssize_t nbytes = ACE_OS::recvmsg (this->get_handle (), &recv_msg, 0);
122 if (nbytes != ACE_INVALID_HANDLE)
124 if (len != 0)
125 *len = nbytes;
127 if (nbytes == sizeof a
128 && ((u_char *) iov.iov_base)[0] == 0xab
129 && ((u_char *) iov.iov_base)[1] == 0xcd)
131 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
132 cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
133 handle = *(ACE_HANDLE *) CMSG_DATA (cmsgptr);
134 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
135 return 1;
137 else
138 return 0;
140 #else
141 ssize_t nbytes = ACE_OS::recvmsg (this->get_handle (),
142 &recv_msg,
143 MSG_PEEK);
145 if (nbytes != ACE_INVALID_HANDLE)
147 if (nbytes == sizeof a
148 && ((u_char *) iov.iov_base)[0] == 0xab
149 && ((u_char *) iov.iov_base)[1] == 0xcd)
151 #ifndef ACE_LACKS_SENDMSG
152 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
153 // Close down the socket that was returned by the MSG_PEEK.
154 cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
155 ACE_HANDLE * ph = (ACE_HANDLE *) CMSG_DATA (cmsgptr);
156 ACE_OS::closesocket (*ph);
157 recv_msg.msg_control = cmsgbuf;
158 recv_msg.msg_controllen = sizeof cmsgbuf;
159 #else
160 recv_msg.msg_accrights = (char *) &handle;
161 recv_msg.msg_accrightslen = sizeof handle;
162 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
163 #endif /* ACE_LACKS_SENDMSG */
164 if (ACE_OS::recvmsg (this->get_handle (),
165 &recv_msg, 0) == ACE_INVALID_HANDLE)
166 return ACE_INVALID_HANDLE;
167 else
169 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
170 cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
171 ACE_HANDLE * ph = (ACE_HANDLE *) CMSG_DATA (cmsgptr);
172 handle = *ph;
173 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
174 return 1;
177 else
179 if (len != 0)
180 *len = nbytes;
181 return 0;
184 #endif /* ACE_HAS_STREAMS */
185 else
186 return ACE_INVALID_HANDLE;
188 #endif /* ACE_HAS_MSG */
190 ACE_END_VERSIONED_NAMESPACE_DECL
192 #endif /* ACE_LACKS_UNIX_DOMAIN_SOCKETS */