00001
00002
00003 #include "ace/LSOCK.h"
00004
00005 ACE_RCSID(ace, LSOCK, "LSOCK.cpp,v 4.24 2005/10/28 16:14:52 ossama Exp")
00006
00007 #if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)
00008
00009 #include "ace/Log_Msg.h"
00010 #include "ace/OS_NS_sys_socket.h"
00011
00012 #if !defined (__ACE_INLINE__)
00013 #include "ace/LSOCK.inl"
00014 #endif
00015
00016 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00017
00018 ACE_ALLOC_HOOK_DEFINE(ACE_LSOCK)
00019
00020 void
00021 ACE_LSOCK::dump (void) const
00022 {
00023 #if defined (ACE_HAS_DUMP)
00024 ACE_TRACE ("ACE_LSOCK::dump");
00025
00026 ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00027 ACE_DEBUG ((LM_DEBUG, ACE_LIB_TEXT ("aux_handle_ = %d"), this->aux_handle_));
00028 ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00029 #endif
00030 }
00031
00032 #if defined (ACE_HAS_MSG)
00033
00034
00035 ssize_t
00036 ACE_LSOCK::send_handle (const ACE_HANDLE handle) const
00037 {
00038 ACE_TRACE ("ACE_LSOCK::send_handle");
00039 u_char a[2];
00040 iovec iov;
00041 msghdr send_msg;
00042 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00043 char cmsgbuf[ACE_BSD_CONTROL_MSG_LEN];
00044 cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
00045 #endif
00046
00047 a[0] = 0xab;
00048 a[1] = 0xcd;
00049 iov.iov_base = (char *) a;
00050 iov.iov_len = sizeof a;
00051 send_msg.msg_iov = &iov;
00052 send_msg.msg_iovlen = 1;
00053 send_msg.msg_name = 0;
00054 send_msg.msg_namelen = 0;
00055
00056 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00057 cmsgptr->cmsg_level = SOL_SOCKET;
00058 cmsgptr->cmsg_type = SCM_RIGHTS;
00059 cmsgptr->cmsg_len = sizeof cmsgbuf;
00060 send_msg.msg_control = cmsgbuf;
00061 send_msg.msg_controllen = sizeof cmsgbuf;
00062 *(ACE_HANDLE *) CMSG_DATA (cmsgptr) = handle;
00063 send_msg.msg_flags = 0;
00064 #else
00065 send_msg.msg_accrights = (char *) &handle;
00066 send_msg.msg_accrightslen = sizeof handle;
00067 #endif
00068
00069 return ACE_OS::sendmsg (this->get_handle (), &send_msg, 0);
00070 }
00071
00072
00073
00074
00075
00076 ssize_t
00077 ACE_LSOCK::recv_handle (ACE_HANDLE &handle, char *pbuf, ssize_t *len) const
00078 {
00079 ACE_TRACE ("ACE_LSOCK::recv_handle");
00080 u_char a[2];
00081 iovec iov;
00082 msghdr recv_msg;
00083
00084 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00085 char cmsgbuf[ACE_BSD_CONTROL_MSG_LEN];
00086 #endif
00087
00088 if (pbuf != 0 && len != 0)
00089 {
00090 iov.iov_base = pbuf;
00091 iov.iov_len = *len;
00092 }
00093 else
00094 {
00095 iov.iov_base = (char *) a;
00096 iov.iov_len = sizeof a;
00097 }
00098
00099 recv_msg.msg_iov = &iov;
00100 recv_msg.msg_iovlen = 1;
00101 recv_msg.msg_name = 0;
00102 recv_msg.msg_namelen = 0;
00103 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00104 recv_msg.msg_control = cmsgbuf;
00105 recv_msg.msg_controllen = sizeof cmsgbuf;
00106 #else
00107 recv_msg.msg_accrights = (char *) &handle;
00108 recv_msg.msg_accrightslen = sizeof handle;
00109 #endif
00110
00111 #if defined (ACE_HAS_STREAMS)
00112
00113 ssize_t nbytes = ACE_OS::recvmsg (this->get_handle (), &recv_msg, 0);
00114
00115 if (nbytes != ACE_INVALID_HANDLE)
00116 {
00117 if (len != 0)
00118 *len = nbytes;
00119
00120 if (nbytes == sizeof a
00121 && ((u_char *) iov.iov_base)[0] == 0xab
00122 && ((u_char *) iov.iov_base)[1] == 0xcd)
00123 {
00124 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00125 cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
00126 handle = *(ACE_HANDLE *) CMSG_DATA (cmsgptr);
00127 #endif
00128 return 1;
00129 }
00130 else
00131 return 0;
00132 }
00133 #else
00134 ssize_t nbytes = ACE_OS::recvmsg (this->get_handle (),
00135 &recv_msg,
00136 MSG_PEEK);
00137
00138 if (nbytes != ACE_INVALID_HANDLE)
00139 {
00140 if (nbytes == sizeof a
00141 && ((u_char *) iov.iov_base)[0] == 0xab
00142 && ((u_char *) iov.iov_base)[1] == 0xcd)
00143 {
00144 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00145
00146 ACE_OS::closesocket (*(ACE_HANDLE *) CMSG_DATA ((cmsghdr *) cmsgbuf));
00147 recv_msg.msg_control = cmsgbuf;
00148 recv_msg.msg_controllen = sizeof cmsgbuf;
00149 #else
00150 recv_msg.msg_accrights = (char *) &handle;
00151 recv_msg.msg_accrightslen = sizeof handle;
00152 #endif
00153
00154 if (ACE_OS::recvmsg (this->get_handle (),
00155 &recv_msg, 0) == ACE_INVALID_HANDLE)
00156 return ACE_INVALID_HANDLE;
00157 else
00158 {
00159 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00160 cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
00161 handle = *(ACE_HANDLE *) CMSG_DATA (cmsgptr);
00162 #endif
00163 return 1;
00164 }
00165 }
00166 else
00167 {
00168 if (len != 0)
00169 *len = nbytes;
00170 return 0;
00171 }
00172 }
00173 #endif
00174 else
00175 return ACE_INVALID_HANDLE;
00176 }
00177 #endif
00178
00179 ACE_END_VERSIONED_NAMESPACE_DECL
00180
00181 #endif