Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

LSOCK.cpp

Go to the documentation of this file.
00001 #include "ace_pch.h"
00002 // $Id: LSOCK.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:21 chad Exp $
00003 
00004 #include "ace/LSOCK.h"
00005 
00006 ACE_RCSID(ace, LSOCK, "$Id: LSOCK.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:21 chad Exp $")
00007 
00008 #if !defined (ACE_LACKS_UNIX_DOMAIN_SOCKETS)
00009 
00010 #include "ace/Log_Msg.h"
00011 
00012 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00013 #include "ace/LSOCK.i"
00014 #endif
00015 
00016 ACE_ALLOC_HOOK_DEFINE(ACE_LSOCK)
00017 
00018 void
00019 ACE_LSOCK::dump (void) const
00020 {
00021   ACE_TRACE ("ACE_LSOCK::dump");
00022 
00023   ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
00024   ACE_DEBUG ((LM_DEBUG,  ACE_LIB_TEXT ("aux_handle_ = %d"), this->aux_handle_));
00025   ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
00026 }
00027 
00028 #if defined (ACE_HAS_MSG)
00029 // This routine sends an open file descriptor to <this->handle_>.
00030 
00031 int
00032 ACE_LSOCK::send_handle (const ACE_HANDLE handle) const
00033 {
00034   ACE_TRACE ("ACE_LSOCK::send_handle");
00035   u_char a[2];
00036   iovec iov;
00037   msghdr send_msg;
00038 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00039   char cmsgbuf[ACE_BSD_CONTROL_MSG_LEN];
00040   cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
00041 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
00042 
00043   a[0] = 0xab;
00044   a[1] = 0xcd;
00045   iov.iov_base = (char *) a;
00046   iov.iov_len = sizeof a;
00047   send_msg.msg_iov = &iov;
00048   send_msg.msg_iovlen = 1;
00049   send_msg.msg_name = 0;
00050   send_msg.msg_namelen = 0;
00051 
00052 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00053   cmsgptr->cmsg_level = SOL_SOCKET;
00054   cmsgptr->cmsg_type = SCM_RIGHTS;
00055   cmsgptr->cmsg_len = sizeof cmsgbuf;
00056   send_msg.msg_control = cmsgbuf;
00057   send_msg.msg_controllen = sizeof cmsgbuf;
00058   *(ACE_HANDLE *) CMSG_DATA (cmsgptr) = handle;
00059   send_msg.msg_flags = 0;
00060 #else
00061   send_msg.msg_accrights = (char *) &handle;
00062   send_msg.msg_accrightslen = sizeof handle;
00063 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
00064 
00065   return ACE_OS::sendmsg (this->get_handle (), &send_msg, 0);
00066 }
00067 
00068 // This file receives an open file descriptor from <this->handle_>.
00069 // Note, this routine returns -1 if problems occur, 0 if we recv a
00070 // message that does not have file descriptor in it, and 1 otherwise.
00071 
00072 int
00073 ACE_LSOCK::recv_handle (ACE_HANDLE &handle, char *pbuf, int *len) const
00074 {
00075   ACE_TRACE ("ACE_LSOCK::recv_handle");
00076   u_char a[2];
00077   iovec iov;
00078   msghdr recv_msg;
00079 
00080 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00081   char cmsgbuf[ACE_BSD_CONTROL_MSG_LEN];
00082 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
00083 
00084   if (pbuf != 0 && len != 0)
00085     {
00086       iov.iov_base = pbuf;
00087       iov.iov_len  = *len;
00088     }
00089   else
00090     {
00091       iov.iov_base = (char *) a;
00092       iov.iov_len  = sizeof a;
00093     }
00094 
00095   recv_msg.msg_iov = &iov;
00096   recv_msg.msg_iovlen = 1;
00097   recv_msg.msg_name = 0;
00098   recv_msg.msg_namelen = 0;
00099 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00100   recv_msg.msg_control = cmsgbuf;
00101   recv_msg.msg_controllen = sizeof cmsgbuf;
00102 #else
00103   recv_msg.msg_accrights = (char *) &handle;
00104   recv_msg.msg_accrightslen = sizeof handle;
00105 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
00106 
00107 #if defined (ACE_HAS_STREAMS)
00108 
00109   ssize_t nbytes = ACE_OS::recvmsg (this->get_handle (), &recv_msg, 0);
00110 
00111   if (nbytes != ACE_INVALID_HANDLE)
00112     {
00113       if (len != 0)
00114         *len = nbytes;
00115 
00116       if (nbytes == sizeof a
00117           && ((u_char *) iov.iov_base)[0] == 0xab
00118           && ((u_char *) iov.iov_base)[1] == 0xcd)
00119         {
00120 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00121           cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
00122           handle = *(ACE_HANDLE *) CMSG_DATA (cmsgptr);
00123 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
00124           return 1;
00125         }
00126       else
00127         return 0;
00128     }
00129 #else
00130   ssize_t nbytes = ACE_OS::recvmsg (this->get_handle (),
00131                                     &recv_msg,
00132                                     MSG_PEEK);
00133 
00134   if (nbytes != ACE_INVALID_HANDLE)
00135     {
00136       if (nbytes == sizeof a
00137           && ((u_char *) iov.iov_base)[0] == 0xab
00138           && ((u_char *) iov.iov_base)[1] == 0xcd)
00139         {
00140 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00141           // Close down the socket that was returned by the MSG_PEEK.
00142           ACE_OS::closesocket (*(ACE_HANDLE *) CMSG_DATA ((cmsghdr *) cmsgbuf)); 
00143           recv_msg.msg_control = cmsgbuf;
00144           recv_msg.msg_controllen = sizeof cmsgbuf;
00145 #else
00146           recv_msg.msg_accrights = (char *) &handle;
00147           recv_msg.msg_accrightslen = sizeof handle;
00148 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
00149 
00150           if (ACE_OS::recvmsg (this->get_handle (),
00151                                &recv_msg, 0) == ACE_INVALID_HANDLE)
00152             return ACE_INVALID_HANDLE;
00153           else
00154             {
00155 #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG)
00156               cmsghdr *cmsgptr = (cmsghdr *) cmsgbuf;
00157               handle = *(ACE_HANDLE *) CMSG_DATA (cmsgptr);
00158 #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */
00159               return 1;
00160             }
00161         }
00162       else
00163         {
00164           if (len != 0)
00165             *len = nbytes;
00166           return 0;
00167         }
00168     }
00169 #endif /* ACE_HAS_STREAMS */
00170   else
00171     return ACE_INVALID_HANDLE;
00172 }
00173 #endif /* ACE_HAS_MSG */
00174 #endif /* ACE_LACKS_UNIX_DOMAIN_SOCKETS */

Generated on Mon Jun 16 11:20:01 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002