Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / SOCK_CODgram.cpp
blob8cf14f0ebe2eaec0de0711339ce36e80d4517ebd
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)
16 void
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,
28 int protocol_family,
29 int protocol,
30 int reuse_addr)
32 ACE_TRACE ("ACE_SOCK_CODgram::ACE_SOCK_CODgram");
33 if (this->open (remote,
34 local,
35 protocol_family,
36 protocol,
37 reuse_addr) == -1)
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
62 int
63 ACE_SOCK_CODgram::open (const ACE_Addr &remote, const ACE_Addr &local,
64 int protocol_family, int protocol,
65 int reuse_addr)
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 ();
75 else
76 { // Both specified; family must match
77 if (local.get_type () != remote.get_type ())
79 errno = EAFNOSUPPORT;
80 return -1;
82 protocol_family = remote.get_type ();
85 else
87 if (local != ACE_Addr::sap_any)
89 protocol_family = local.get_type ();
92 if (ACE_SOCK::open (SOCK_DGRAM,
93 protocol_family,
94 protocol,
95 reuse_addr) == -1)
97 return -1;
99 else
101 bool error = false;
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)
111 error = true;
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)
119 error = true;
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)
127 error = true;
129 // We are binding to the local address and connecting to the
130 // remote addresses.
131 else
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)
139 error = true;
141 if (error)
143 this->close ();
144 this->set_handle (ACE_INVALID_HANDLE);
146 return error ? -1 : 0;
150 ACE_END_VERSIONED_NAMESPACE_DECL