summaryrefslogtreecommitdiff
path: root/ACE/ace/SOCK_CODgram.cpp
blob: 8cf14f0ebe2eaec0de0711339ce36e80d4517ebd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "ace/SOCK_CODgram.h"
#include "ace/Log_Category.h"
#include "ace/OS_NS_sys_socket.h"
#if defined (ACE_HAS_ALLOC_HOOKS)
# include "ace/Malloc_Base.h"
#endif /* ACE_HAS_ALLOC_HOOKS */

#if !defined (__ACE_INLINE__)
#include "ace/SOCK_CODgram.inl"
#endif /* __ACE_INLINE__ */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

ACE_ALLOC_HOOK_DEFINE(ACE_SOCK_CODgram)

void
ACE_SOCK_CODgram::dump () const
{
#if defined (ACE_HAS_DUMP)
  ACE_TRACE ("ACE_SOCK_CODgram::dump");
#endif /* ACE_HAS_DUMP */
}

// Here's the general-purpose constructor.

ACE_SOCK_CODgram::ACE_SOCK_CODgram (const ACE_Addr &remote,
                                    const ACE_Addr &local,
                                    int protocol_family,
                                    int protocol,
                                    int reuse_addr)
{
  ACE_TRACE ("ACE_SOCK_CODgram::ACE_SOCK_CODgram");
  if (this->open (remote,
                  local,
                  protocol_family,
                  protocol,
                  reuse_addr) == -1)
    ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_SOCK_CODgram")));
}

/* This is the general-purpose open routine.  Note that it performs
   a different set of functions depending on the LOCAL and REMOTE
   addresses passed to it.  Here's the basic logic:

   1. remote == ACE_Addr::sap_any && local == ACE_Addr::sap_any
         if protocol_family == PF_INET || PF_INET6 then
             bind the local address to a randomly generated port number...

   2. remote == ACE_Addr::sap_any && local != ACE_Addr::sap_any
         we are just binding the local address
         (used primarily by servers)

   3. remote != ACE_Addr::sap_any && local == ACE_Addr::sap_any
         we are connecting to the remote address
         (used primarily by clients)

   4. remote != ACE_Addr::sap_any && local != ACE_Addr::sap_any
         we are binding to the local address
         and connecting to the remote address
*/

int
ACE_SOCK_CODgram::open (const ACE_Addr &remote, const ACE_Addr &local,
                        int protocol_family, int protocol,
                        int reuse_addr)
{
  ACE_TRACE ("ACE_SOCK_CODgram::open");
  // Depending on the addresses passed as described above, figure out what
  // address family to specify for the new socket. If either address is
  // !ACE_Addr::sap_any, use that family. If they don't match, it's an error.
  if (remote != ACE_Addr::sap_any)
    {
      if (local == ACE_Addr::sap_any)
        protocol_family = remote.get_type ();
      else
        { // Both specified; family must match
          if (local.get_type () != remote.get_type ())
            {
              errno = EAFNOSUPPORT;
              return -1;
            }
          protocol_family = remote.get_type ();
        }
    }
  else
    {
      if (local != ACE_Addr::sap_any)
        {
          protocol_family = local.get_type ();
        }
    }
  if (ACE_SOCK::open (SOCK_DGRAM,
                      protocol_family,
                      protocol,
                      reuse_addr) == -1)
    {
      return -1;
    }
  else
    {
      bool error = false;

      if (local == ACE_Addr::sap_any && remote == ACE_Addr::sap_any)
        {
          // Assign an arbitrary port number from the transient range!!
          if ((protocol_family == PF_INET
#if defined (ACE_HAS_IPV6)
               || protocol_family == PF_INET6
#endif /* ACE_HAS_IPV6 */
              ) && ACE::bind_port (this->get_handle ()) == -1)
            error = true;
        }
      // We are binding just the local address.
      else if (local != ACE_Addr::sap_any && remote == ACE_Addr::sap_any)
        {
          if (ACE_OS::bind (this->get_handle (),
                            (sockaddr *) local.get_addr (),
                            local.get_size ()) == -1)
            error = true;
        }
      // We are connecting to the remote address.
      else if (local == ACE_Addr::sap_any && remote != ACE_Addr::sap_any)
        {
          if (ACE_OS::connect (this->get_handle (),
                               (sockaddr *) remote.get_addr (),
                               remote.get_size ()) == -1)
            error = true;
        }
      // We are binding to the local address and connecting to the
      // remote addresses.
      else
        {
          if (ACE_OS::bind (this->get_handle (),
                            (sockaddr *) local.get_addr (),
                            local.get_size ()) == -1
              || ACE_OS::connect (this->get_handle (),
                                  (sockaddr *) remote.get_addr (),
                                  remote.get_size ()) == -1)
            error = true;
        }
      if (error)
        {
          this->close ();
          this->set_handle (ACE_INVALID_HANDLE);
        }
      return error ? -1 : 0;
    }
}

ACE_END_VERSIONED_NAMESPACE_DECL