summaryrefslogtreecommitdiff
path: root/ACE/tests/SOCK_Dgram_Test.cpp
blob: 9b959a51f35c804f7add8e92722dd3c4ebac750c (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//=============================================================================
/**
 *  @file    SOCK_Dgram_Test.cpp
 *
 *   Tests that a call to open with an any address binds to the any address
 *   for the protocol passed in.
 *
 *   This test uses the same test setup as SOCK_Test.
 *
 *
 *  @author Brian Buesker (bbuesker@qualcomm.com)
 */
//=============================================================================


#include "test_config.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_sys_wait.h"
#include "ace/Thread.h"
#include "ace/Thread_Manager.h"
#include "ace/SOCK_Dgram.h"
#include "ace/Log_Msg.h"
#include "ace/Time_Value.h"
#include "ace/OS_NS_unistd.h"

#define SERVER_PORT 20000
#define TEST_DATA ACE_TEXT ("UDP Open Test")

static void *
client (void *arg)
{
  ACE_INET_Addr *remote_addr = (ACE_INET_Addr *) arg;
  ACE_INET_Addr server_addr, peer_addr;
  ACE_SOCK_Dgram cli_dgram;
  ACE_Time_Value timeout (1);

  ACE_TCHAR buf[20];
  ACE_TCHAR hostname_string[100];

  if (remote_addr->get_type () == AF_INET)
    {
      server_addr.set (remote_addr->get_port_number (),
                       ACE_LOCALHOST,
                       1,
                       remote_addr->get_type ());
    }
#if defined (ACE_HAS_IPV6)
  else
    {
      server_addr.set (remote_addr->get_port_number(),
                       ACE_IPV6_LOCALHOST,
                       1,
                       remote_addr->get_type ());
    }
#endif /* ACE_HAS_IPV6 */

  server_addr.addr_to_string (hostname_string, 100);
  ACE_DEBUG ((LM_DEBUG,
              ACE_TEXT ("(%P|%t) starting client UDP send to %s\n"),
              hostname_string));

  if (cli_dgram.open (ACE_Addr::sap_any, server_addr.get_type ()) == -1)
    {
      ACE_ERROR((LM_ERROR,
                 ACE_TEXT("(%P|%t) protocol %d, %p\n"),
                 server_addr.get_type (),
                 ACE_TEXT ("SOCK_Dgram open")));
    }
  else if (cli_dgram.send (TEST_DATA, sizeof (TEST_DATA), server_addr) == -1)
    {
      ACE_ERROR((LM_ERROR,
                 ACE_TEXT("(%P|%t) UDP send to %s %p\n"),
                 hostname_string,
                 ACE_TEXT ("failed")));
    }
  else
    {
      ssize_t rcv_cnt = cli_dgram.recv (buf,
                                        sizeof (buf),
                                        peer_addr,
                                        0,
                                        &timeout);
      if (rcv_cnt == -1)
        {
          if (errno == ETIME)
          {
            ACE_ERROR((LM_ERROR,
                       ACE_TEXT("(%P|%t) UDP recv on proto %d timed out\n"),
                       server_addr.get_type ()));
          }
          else
          {
            ACE_ERROR((LM_ERROR,
                       ACE_TEXT("(%P|%t) UDP recv on proto %d %p\n"),
                       server_addr.get_type (),
                       ACE_TEXT("failed")));
          }
        }
      else
        {
          // recv() ok, check data and 'from' address
          size_t rcv_siz = static_cast<size_t> (rcv_cnt);
          if (rcv_siz != sizeof (TEST_DATA))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("(%P|%t) recv cnt %B; should be %B\n"),
                          rcv_siz,
                          sizeof (TEST_DATA)));
            }
          else
            {
              buf[rcv_siz/sizeof (ACE_TCHAR)] = '\0';
              if (ACE_OS::strncmp (buf, TEST_DATA, ACE_OS::strlen (TEST_DATA)))
                ACE_ERROR ((LM_ERROR,
                            ACE_TEXT ("(%P|%t) recv string should be %s; ")
                            ACE_TEXT (" but is %s\n"),
                            TEST_DATA, buf));
            }
          if (peer_addr.get_type () != server_addr.get_type ())
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("(%P|%t) recv addr type %d; should be %d\n"),
                        peer_addr.get_type (),
                        server_addr.get_type ()));
          if (peer_addr.get_size () != server_addr.get_size ())
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("(%P|%t) recv addr size %d; should be %d\n"),
                        peer_addr.get_size (),
                        server_addr.get_size ()));
        }
    }

  cli_dgram.close();

  return 0;
}

static void *
server (void *arg)
{
  ACE_SOCK_Dgram *server_dgram = (ACE_SOCK_Dgram *) arg;
  ACE_INET_Addr peer_addr;

  ACE_TCHAR buf[20];
  ACE_TCHAR hostname_string[100];
  ssize_t rcv_cnt;

  if ((rcv_cnt = server_dgram->recv (buf, sizeof (buf), peer_addr, 0)) == -1)
    {
      ACE_ERROR((LM_ERROR, ACE_TEXT("(%P|%t) %p\n"), ACE_TEXT("server recv")));
    }
  else
    {
      // recv() ok, check data and 'from' address
      size_t rcv_siz = static_cast<size_t> (rcv_cnt);
      if (rcv_siz != sizeof (TEST_DATA))
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("(%P|%t) recv cnt %B; should be %B\n"),
                      rcv_siz,
                      sizeof (TEST_DATA)));
        }
      else
        {
          buf[rcv_siz / sizeof (ACE_TCHAR)] = '\0';
          if (ACE_OS::strncmp (buf, TEST_DATA, ACE_OS::strlen (TEST_DATA)))
            ACE_ERROR ((LM_ERROR,
                        ACE_TEXT ("(%P|%t) recv string should be %s; ")
                        ACE_TEXT (" but is %s\n"),
                        TEST_DATA, buf));
        }

      if (0 == peer_addr.addr_to_string (hostname_string,
          sizeof (hostname_string)))
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%P|%t) Echoing data to %s\n"),
                    hostname_string));
      else
        ACE_ERROR ((LM_ERROR,
                    ACE_TEXT ("(%P|%t) %p\n"),
                    ACE_TEXT ("addr_to_string")));
      if (server_dgram->send (TEST_DATA, sizeof (TEST_DATA),
                              peer_addr, 0) == -1)
        {
          ACE_ERROR((LM_ERROR,
                     ACE_TEXT("(%P|%t) %p\n"),
                     ACE_TEXT("Server UDP send failed")));
        }
    }
  server_dgram->close ();

  return 0;
}

static int
spawn (int proto)
{
  ACE_SOCK_Dgram server_dgram;
  ACE_INET_Addr server_addr;

  if (proto == AF_INET)
    {
      server_addr.set (SERVER_PORT, ACE_LOCALHOST, 1, proto);
    }
#if defined (ACE_HAS_IPV6)
  else
    {
      server_addr.set (SERVER_PORT, ACE_IPV6_LOCALHOST, 1, proto);
    }
#endif /* ACE_HAS_IPV6 */

  // Bind UDP server to the appropriate port
  if (server_dgram.open (server_addr, proto) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("(%P|%t) %p\n"),
                         ACE_TEXT ("server dgram open")),
                         1);
    }
  else
    {
      if (server_addr.get_port_number() != SERVER_PORT)
        {
          ACE_TCHAR hostname_string[100];
          server_addr.addr_to_string (hostname_string, 100);
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT("(%P|%t) Portnumber has unexpected value of %d on host %s\n"),
                             server_addr.get_port_number(), hostname_string), 1);
        }
      else
        {
          ACE_DEBUG ((LM_DEBUG,
                      ACE_TEXT ("(%P|%t) started server at proto %d, port %d\n"),
                      proto,
                      server_addr.get_port_number ()));
#if !defined (ACE_LACKS_FORK)
          switch (ACE_OS::fork (ACE_TEXT ("child")))
            {
            case -1:
              ACE_ERROR_BREAK ((LM_ERROR,
                                ACE_TEXT ("(%P|%t) %p\n"),
                                ACE_TEXT ("fork failed")));
              /* NOTREACHED */
            case 0:
              client (&server_addr);
              ACE_OS::exit (0);
              /* NOTREACHED */
            default:
              server ((void *) &server_dgram);
              ACE_OS::wait ();
            }
#elif defined (ACE_HAS_THREADS)
          if (ACE_Thread_Manager::instance ()->spawn
              (ACE_THR_FUNC (server),
               (void *) &server_dgram,
               THR_NEW_LWP | THR_DETACHED) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,
                               ACE_TEXT ("(%P|%t) %p\n"),
                               ACE_TEXT ("thread create failed")),
                               1);

          if (ACE_Thread_Manager::instance ()->spawn
              (ACE_THR_FUNC (client),
               (void *) &server_addr,
               THR_NEW_LWP | THR_DETACHED) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,
                        ACE_TEXT ("(%P|%t) %p\n"),
                        ACE_TEXT ("thread create failed")),
                        1);

          // Wait for the threads to exit.
          ACE_Thread_Manager::instance ()->wait ();
#else
          ACE_ERROR ((LM_INFO,
                      ACE_TEXT ("(%P|%t) ")
                      ACE_TEXT ("only one thread may be run ")
                      ACE_TEXT ("in a process on this platform\n")));
#endif /* ACE_HAS_THREADS */
      }

      server_dgram.close ();
    }

  return 0;
}

int run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("SOCK_Dgram_Test"));

  int retval = spawn (AF_INET);

#if defined (ACE_HAS_IPV6)

  if (retval == 0)
    {
      retval = spawn (AF_INET6);
    }

#endif /* ACE_HAS_IPV6 */

  ACE_END_TEST;
  return retval;
}