TLI.cpp

Go to the documentation of this file.
00001 // TLI.cpp,v 4.26 2005/10/28 16:14:56 ossama Exp
00002 
00003 // Defines the member functions for the base class of the ACE_TLI
00004 // abstraction.
00005 
00006 #include "ace/TLI.h"
00007 #include "ace/Log_Msg.h"
00008 #include "ace/OS_Memory.h"
00009 #include "ace/OS_TLI.h"
00010 #include "ace/OS_NS_string.h"
00011 #include "ace/OS_NS_sys_socket.h"
00012 #include "ace/Auto_Ptr.h"
00013 
00014 ACE_RCSID(ace, TLI, "TLI.cpp,v 4.26 2005/10/28 16:14:56 ossama Exp")
00015 
00016 #if defined (ACE_HAS_TLI)
00017 
00018 #if !defined (__ACE_INLINE__)
00019 #include "ace/TLI.inl"
00020 #endif /* __ACE_INLINE__ */
00021 
00022 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00023 
00024 ACE_ALLOC_HOOK_DEFINE(ACE_TLI)
00025 
00026 void
00027 ACE_TLI::dump (void) const
00028 {
00029 #if defined (ACE_HAS_DUMP)
00030   ACE_TRACE ("ACE_TLI::dump");
00031 #endif /* ACE_HAS_DUMP */
00032 }
00033 
00034 ACE_TLI::ACE_TLI (void)
00035 {
00036   ACE_TRACE ("ACE_TLI::ACE_TLI");
00037 #if defined (ACE_HAS_SVR4_TLI)
00038 // Solaris 2.4 ACE_TLI option handling is broken.  Thus, we must do
00039 // the memory allocation ourselves...  Thanks to John P. Hearn
00040 // (jph@ccrl.nj.nec.com) for the help.
00041 
00042   this->so_opt_req.opt.maxlen = sizeof (opthdr) + sizeof (long);
00043   ACE_NEW (this->so_opt_req.opt.buf,
00044            char[this->so_opt_req.opt.maxlen]);
00045 
00046   this->so_opt_ret.opt.maxlen = sizeof (opthdr) + sizeof (long);
00047   ACE_NEW (this->so_opt_ret.opt.buf,
00048            char[this->so_opt_ret.opt.maxlen]);
00049 
00050   if (this->so_opt_ret.opt.buf == 0)
00051     {
00052       delete [] this->so_opt_req.opt.buf;
00053       this->so_opt_req.opt.buf = 0;
00054       return;
00055     }
00056 #endif /* ACE_HAS_SVR4_TLI */
00057 }
00058 
00059 ACE_HANDLE
00060 ACE_TLI::open (const char device[], int oflag, struct t_info *info)
00061 {
00062   ACE_TRACE ("ACE_TLI::open");
00063   if (oflag == 0)
00064     oflag = O_RDWR;
00065   this->set_handle (ACE_OS::t_open ((char *) device, oflag, info));
00066 
00067   return this->get_handle ();
00068 }
00069 
00070 ACE_TLI::~ACE_TLI (void)
00071 {
00072   ACE_TRACE ("ACE_TLI::~ACE_TLI");
00073 #if defined (ACE_HAS_SVR4_TLI)
00074   if (this->so_opt_req.opt.buf)
00075     {
00076       delete [] this->so_opt_req.opt.buf;
00077       delete [] this->so_opt_ret.opt.buf;
00078       this->so_opt_req.opt.buf = 0;
00079       this->so_opt_ret.opt.buf = 0;
00080     }
00081 #endif /* ACE_HAS_SVR4_TLI */
00082 }
00083 
00084 ACE_TLI::ACE_TLI (const char device[], int oflag, struct t_info *info)
00085 {
00086   ACE_TRACE ("ACE_TLI::ACE_TLI");
00087   if (this->open (device, oflag, info) == ACE_INVALID_HANDLE)
00088     ACE_ERROR ((LM_ERROR,
00089                 ACE_LIB_TEXT ("%p\n"),
00090                 ACE_LIB_TEXT ("ACE_TLI::ACE_TLI")));
00091 }
00092 
00093 int
00094 ACE_TLI::get_local_addr (ACE_Addr &sa) const
00095 {
00096   ACE_TRACE ("ACE_TLI::get_local_addr");
00097   struct netbuf name;
00098 
00099   name.maxlen = sa.get_size ();
00100   name.buf    = (char *) sa.get_addr ();
00101 
00102   if (ACE_OS::t_getname (this->get_handle (), &name, LOCALNAME) == -1)
00103     return -1;
00104   else
00105     return 0;
00106 }
00107 
00108 int
00109 ACE_TLI::close (void)
00110 {
00111   ACE_TRACE ("ACE_TLI::close");
00112   int result = 0; // Geisler: result must be int
00113 
00114   if (this->get_handle () != ACE_INVALID_HANDLE)
00115     {
00116       result = ACE_OS::t_close (this->get_handle ());
00117       this->set_handle (ACE_INVALID_HANDLE);
00118     }
00119   return result;
00120 }
00121 
00122 int
00123 ACE_TLI::set_option (int level, int option, void *optval, int optlen)
00124 {
00125   /* Set up options for ACE_TLI */
00126   ACE_TRACE ("ACE_TLI::set_option");
00127 
00128 #if defined (ACE_HAS_XTI)
00129   // ret will get the negotiated option back after attempting to set it.
00130   // Assume this will fit in the requested size.
00131   struct t_optmgmt req, ret;
00132   ACE_NEW_RETURN (req.opt.buf, char[sizeof (struct t_opthdr) + optlen], -1);
00133 #  if (_XOPEN_SOURCE - 0 >= 500)
00134   auto_ptr<char> req_opt_buf_p (reinterpret_cast<char*> (req.opt.buf));
00135 #  else
00136   ACE_Auto_Array_Ptr<char> req_opt_buf_p (req.opt.buf);
00137 #  endif /* XPG5 vs XPG4 */
00138   struct t_opthdr *opthdr =
00139     reinterpret_cast<struct t_opthdr *> (req.opt.buf);
00140   ACE_NEW_RETURN (ret.opt.buf, char[sizeof (struct t_opthdr) + optlen], -1);
00141 #  if (_XOPEN_SOURCE - 0 >= 500)
00142   auto_ptr<char> ret_opt_buf_p (reinterpret_cast<char*> (ret.opt.buf));
00143 #  else
00144   ACE_Auto_Array_Ptr<char> ret_opt_buf_p (ret.opt.buf);
00145 #  endif /* XPG5 vs XPG4 */
00146 
00147   req.flags = T_NEGOTIATE;
00148   req.opt.len = sizeof *opthdr + optlen;
00149   ret.opt.maxlen = req.opt.len;
00150   opthdr->level = level;
00151   opthdr->name  = option;
00152   opthdr->len   = req.opt.len;   // We only request one option at a time.
00153   ACE_OS::memcpy (&opthdr[1], optval, optlen);
00154   return ACE_OS::t_optmgmt (this->get_handle (), &req, &ret);
00155 
00156 #elif defined (ACE_HAS_SVR4_TLI)
00157   struct opthdr *opthdr = 0; /* See <sys/socket.h> for info on this format */
00158 
00159   this->so_opt_req.flags = T_NEGOTIATE;
00160   this->so_opt_req.opt.len = sizeof *opthdr + OPTLEN (optlen);
00161 
00162   if (this->so_opt_req.opt.len > this->so_opt_req.opt.maxlen)
00163     {
00164 #  if !defined (ACE_HAS_SET_T_ERRNO)
00165       t_errno = TBUFOVFLW;
00166 #  else
00167       set_t_errno (TBUFOVFLW);
00168 #  endif /* ACE_HAS_SET_T_ERRNO */
00169       return -1;
00170     }
00171 
00172   opthdr = reinterpret_cast<struct opthdr *> (this->so_opt_req.opt.buf);
00173   opthdr->level = level;
00174   opthdr->name  = option;
00175   opthdr->len   = OPTLEN (optlen);
00176   ACE_OS::memcpy (OPTVAL (opthdr), optval, optlen);
00177 
00178   return ACE_OS::t_optmgmt (this->get_handle (),
00179                             &this->so_opt_req,
00180                             &this->so_opt_ret);
00181 #else
00182   ACE_UNUSED_ARG (level);
00183   ACE_UNUSED_ARG (option);
00184   ACE_UNUSED_ARG (optval);
00185   ACE_UNUSED_ARG (optlen);
00186   return -1;
00187 #endif /* ACE_HAS_XTI, else ACE_HAS_SVR4_TLI */
00188 }
00189 
00190 int
00191 ACE_TLI::get_option (int level, int option, void *optval, int &optlen)
00192 {
00193   ACE_TRACE ("ACE_TLI::get_option");
00194 #if defined (ACE_HAS_XTI)
00195   // ret will get the option requested in req.
00196   struct t_optmgmt req, ret;
00197   ACE_NEW_RETURN (req.opt.buf, char[sizeof (struct t_opthdr)], -1);
00198 #  if (_XOPEN_SOURCE - 0 >= 500)
00199   auto_ptr<char> req_opt_buf_p (reinterpret_cast<char*> (req.opt.buf));
00200 #  else
00201   ACE_Auto_Array_Ptr<char> req_opt_buf_p (req.opt.buf);
00202 #  endif /* XPG5 vs XPG4 */
00203   struct t_opthdr *opthdr =
00204     reinterpret_cast<struct t_opthdr *> (req.opt.buf);
00205   ACE_NEW_RETURN (ret.opt.buf, char[sizeof (struct t_opthdr) + optlen], -1);
00206 #  if (_XOPEN_SOURCE - 0 >= 500)
00207   auto_ptr<char> ret_opt_buf_p (reinterpret_cast<char*> (ret.opt.buf));
00208 #  else
00209   ACE_Auto_Array_Ptr<char> ret_opt_buf_p (ret.opt.buf);
00210 #  endif /* XPG5 vs XPG4 */
00211 
00212   req.flags = T_CURRENT;
00213   req.opt.len = sizeof *opthdr;
00214   ret.opt.maxlen = sizeof (struct t_opthdr) + optlen;
00215   opthdr->level = level;
00216   opthdr->name  = option;
00217   opthdr->len   = sizeof (*opthdr);    // Just the header on the request
00218   if (ACE_OS::t_optmgmt (this->get_handle (), &req, &ret) == -1)
00219     return -1;
00220   else
00221     {
00222       opthdr = reinterpret_cast<struct t_opthdr *> (ret.opt.buf);
00223       if (opthdr->status == T_NOTSUPPORT)
00224         {
00225           errno = ENOTSUP;
00226           return -1;
00227         }
00228       else
00229         {
00230           ACE_OS::memcpy (optval, &opthdr[1], optlen);
00231           return 0;
00232         }
00233     }
00234 
00235 #elif defined (ACE_HAS_SVR4_TLI)
00236   struct opthdr *opthdr = 0; /* See <sys/socket.h> for details on this format */
00237 
00238   this->so_opt_req.flags = T_CHECK;
00239   this->so_opt_ret.opt.len = sizeof *opthdr + OPTLEN (optlen);
00240 
00241   if (this->so_opt_ret.opt.len > this->so_opt_ret.opt.maxlen)
00242     {
00243 #if !defined (ACE_HAS_SET_T_ERRNO)
00244       t_errno = TBUFOVFLW;
00245 #else
00246       set_t_errno (TBUFOVFLW);
00247 #endif /* ACE_HAS_SET_T_ERRNO */
00248       return -1;
00249     }
00250 
00251   opthdr        = (struct opthdr *) this->so_opt_req.opt.buf;
00252   opthdr->level = level;
00253   opthdr->name  = option;
00254   opthdr->len   = OPTLEN (optlen);
00255   if (ACE_OS::t_optmgmt (this->get_handle (), &this->so_opt_req, &this->so_opt_ret) == -1)
00256     return -1;
00257   else
00258     {
00259       ACE_OS::memcpy (optval, OPTVAL (opthdr), optlen);
00260       return 0;
00261     }
00262 #else
00263   ACE_UNUSED_ARG (level);
00264   ACE_UNUSED_ARG (option);
00265   ACE_UNUSED_ARG (optval);
00266   ACE_UNUSED_ARG (optlen);
00267   return -1;
00268 #endif /* ACE_HAS_SVR4_TLI */
00269 }
00270 
00271 ACE_END_VERSIONED_NAMESPACE_DECL
00272 
00273 #endif /* ACE_HAS_TLI */

Generated on Thu Nov 9 09:42:07 2006 for ACE by doxygen 1.3.6