00001 #include "ace_pch.h"
00002
00003
00004
00005 #include "ace/SOCK.h"
00006 #include "ace/Log_Msg.h"
00007
00008 #if defined (ACE_LACKS_INLINE_FUNCTIONS)
00009 #include "ace/SOCK.i"
00010 #endif
00011
00012 ACE_RCSID(ace, SOCK, "$Id: SOCK.cpp,v 1.1.1.4.2.1 2003/03/13 19:44:22 chad Exp $")
00013
00014 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK)
00015
00016 void
00017 ACE_SOCK::dump (void) const
00018 {
00019 ACE_TRACE ("ACE_SOCK::dump");
00020 }
00021
00022 ACE_SOCK::ACE_SOCK (void)
00023 {
00024
00025 }
00026
00027
00028
00029
00030 int
00031 ACE_SOCK::get_remote_addr (ACE_Addr &sa) const
00032 {
00033 ACE_TRACE ("ACE_SOCK::get_remote_addr");
00034
00035 int len = sa.get_size ();
00036 sockaddr *addr = ACE_reinterpret_cast (sockaddr *,
00037 sa.get_addr ());
00038
00039 if (ACE_OS::getpeername (this->get_handle (),
00040 addr,
00041 &len) == -1)
00042 return -1;
00043
00044 sa.set_size (len);
00045 sa.set_type (addr->sa_family);
00046 return 0;
00047 }
00048
00049 int
00050 ACE_SOCK::get_local_addr (ACE_Addr &sa) const
00051 {
00052 ACE_TRACE ("ACE_SOCK::get_local_addr");
00053
00054 int len = sa.get_size ();
00055 sockaddr *addr = ACE_reinterpret_cast (sockaddr *,
00056 sa.get_addr ());
00057
00058 if (ACE_OS::getsockname (this->get_handle (),
00059 addr,
00060 &len) == -1)
00061 return -1;
00062
00063 sa.set_type (addr->sa_family);
00064 sa.set_size (len);
00065 return 0;
00066 }
00067
00068
00069
00070 int
00071 ACE_SOCK::close (void)
00072 {
00073 ACE_TRACE ("ACE_SOCK::close");
00074 int result = 0;
00075
00076 if (this->get_handle () != ACE_INVALID_HANDLE)
00077 {
00078 result = ACE_OS::closesocket (this->get_handle ());
00079 this->set_handle (ACE_INVALID_HANDLE);
00080 }
00081 return result;
00082 }
00083
00084 int
00085 ACE_SOCK::open (int type,
00086 int protocol_family,
00087 int protocol,
00088 int reuse_addr)
00089 {
00090 ACE_TRACE ("ACE_SOCK::open");
00091 int one = 1;
00092
00093 this->set_handle (ACE_OS::socket (protocol_family,
00094 type,
00095 protocol));
00096
00097 if (this->get_handle () == ACE_INVALID_HANDLE)
00098 return -1;
00099 else if (protocol_family != PF_UNIX
00100 && reuse_addr
00101 && this->set_option (SOL_SOCKET,
00102 SO_REUSEADDR,
00103 &one,
00104 sizeof one) == -1)
00105 {
00106 this->close ();
00107 return -1;
00108 }
00109 return 0;
00110 }
00111
00112
00113
00114
00115 ACE_SOCK::ACE_SOCK (int type,
00116 int protocol_family,
00117 int protocol,
00118 int reuse_addr)
00119 {
00120
00121 if (this->open (type,
00122 protocol_family,
00123 protocol,
00124 reuse_addr) == -1)
00125 ACE_ERROR ((LM_ERROR,
00126 ACE_LIB_TEXT ("%p\n"),
00127 ACE_LIB_TEXT ("ACE_SOCK::ACE_SOCK")));
00128 }
00129
00130 int
00131 ACE_SOCK::open (int type,
00132 int protocol_family,
00133 int protocol,
00134 ACE_Protocol_Info *protocolinfo,
00135 ACE_SOCK_GROUP g,
00136 u_long flags,
00137 int reuse_addr)
00138 {
00139 ACE_TRACE ("ACE_SOCK::open");
00140
00141 this->set_handle (ACE_OS::socket (protocol_family,
00142 type,
00143 protocol,
00144 protocolinfo,
00145 g,
00146 flags));
00147 int one = 1;
00148
00149 if (this->get_handle () == ACE_INVALID_HANDLE)
00150 return -1;
00151 else if (reuse_addr
00152 && this->set_option (SOL_SOCKET,
00153 SO_REUSEADDR,
00154 &one,
00155 sizeof one) == -1)
00156 {
00157 this->close ();
00158 return -1;
00159 }
00160 else
00161 return 0;
00162 }
00163
00164 ACE_SOCK::ACE_SOCK (int type,
00165 int protocol_family,
00166 int protocol,
00167 ACE_Protocol_Info *protocolinfo,
00168 ACE_SOCK_GROUP g,
00169 u_long flags,
00170 int reuse_addr)
00171 {
00172
00173 if (this->open (type,
00174 protocol_family,
00175 protocol,
00176 protocolinfo,
00177 g,
00178 flags,
00179 reuse_addr) == -1)
00180 ACE_ERROR ((LM_ERROR,
00181 ACE_LIB_TEXT ("%p\n"),
00182 ACE_LIB_TEXT ("ACE_SOCK::ACE_SOCK")));
00183 }