1 #include "ace/SOCK_CODgram.h"
2 #include "ace/Log_Category.h"
3 #include "ace/OS_NS_sys_socket.h"
4 #if defined (ACE_HAS_ALLOC_HOOKS)
5 # include "ace/Malloc_Base.h"
6 #endif /* ACE_HAS_ALLOC_HOOKS */
8 #if !defined (__ACE_INLINE__)
9 #include "ace/SOCK_CODgram.inl"
10 #endif /* __ACE_INLINE__ */
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
14 ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_CODgram
)
17 ACE_SOCK_CODgram::dump () const
19 #if defined (ACE_HAS_DUMP)
20 ACE_TRACE ("ACE_SOCK_CODgram::dump");
21 #endif /* ACE_HAS_DUMP */
24 // Here's the general-purpose constructor.
26 ACE_SOCK_CODgram::ACE_SOCK_CODgram (const ACE_Addr
&remote
,
27 const ACE_Addr
&local
,
32 ACE_TRACE ("ACE_SOCK_CODgram::ACE_SOCK_CODgram");
33 if (this->open (remote
,
38 ACELIB_ERROR ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_SOCK_CODgram")));
41 /* This is the general-purpose open routine. Note that it performs
42 a different set of functions depending on the LOCAL and REMOTE
43 addresses passed to it. Here's the basic logic:
45 1. remote == ACE_Addr::sap_any && local == ACE_Addr::sap_any
46 if protocol_family == PF_INET || PF_INET6 then
47 bind the local address to a randomly generated port number...
49 2. remote == ACE_Addr::sap_any && local != ACE_Addr::sap_any
50 we are just binding the local address
51 (used primarily by servers)
53 3. remote != ACE_Addr::sap_any && local == ACE_Addr::sap_any
54 we are connecting to the remote address
55 (used primarily by clients)
57 4. remote != ACE_Addr::sap_any && local != ACE_Addr::sap_any
58 we are binding to the local address
59 and connecting to the remote address
63 ACE_SOCK_CODgram::open (const ACE_Addr
&remote
, const ACE_Addr
&local
,
64 int protocol_family
, int protocol
,
67 ACE_TRACE ("ACE_SOCK_CODgram::open");
68 // Depending on the addresses passed as described above, figure out what
69 // address family to specify for the new socket. If either address is
70 // !ACE_Addr::sap_any, use that family. If they don't match, it's an error.
71 if (remote
!= ACE_Addr::sap_any
)
73 if (local
== ACE_Addr::sap_any
)
74 protocol_family
= remote
.get_type ();
76 { // Both specified; family must match
77 if (local
.get_type () != remote
.get_type ())
82 protocol_family
= remote
.get_type ();
87 if (local
!= ACE_Addr::sap_any
)
89 protocol_family
= local
.get_type ();
92 if (ACE_SOCK::open (SOCK_DGRAM
,
103 if (local
== ACE_Addr::sap_any
&& remote
== ACE_Addr::sap_any
)
105 // Assign an arbitrary port number from the transient range!!
106 if ((protocol_family
== PF_INET
107 #if defined (ACE_HAS_IPV6)
108 || protocol_family
== PF_INET6
109 #endif /* ACE_HAS_IPV6 */
110 ) && ACE::bind_port (this->get_handle ()) == -1)
113 // We are binding just the local address.
114 else if (local
!= ACE_Addr::sap_any
&& remote
== ACE_Addr::sap_any
)
116 if (ACE_OS::bind (this->get_handle (),
117 (sockaddr
*) local
.get_addr (),
118 local
.get_size ()) == -1)
121 // We are connecting to the remote address.
122 else if (local
== ACE_Addr::sap_any
&& remote
!= ACE_Addr::sap_any
)
124 if (ACE_OS::connect (this->get_handle (),
125 (sockaddr
*) remote
.get_addr (),
126 remote
.get_size ()) == -1)
129 // We are binding to the local address and connecting to the
133 if (ACE_OS::bind (this->get_handle (),
134 (sockaddr
*) local
.get_addr (),
135 local
.get_size ()) == -1
136 || ACE_OS::connect (this->get_handle (),
137 (sockaddr
*) remote
.get_addr (),
138 remote
.get_size ()) == -1)
144 this->set_handle (ACE_INVALID_HANDLE
);
146 return error
? -1 : 0;
150 ACE_END_VERSIONED_NAMESPACE_DECL