summaryrefslogtreecommitdiff
path: root/examples/QOS/server.cpp
blob: fca38fa8107126de1f9e9ce32d42f17459b02041 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
/* -*- C++ -*- */
// $Id$

// ============================================================================
//
// = LIBRARY
//    ACE_wrappers/examples/QOS
//
// = FILENAME
//    server.cpp
//
// = AUTHOR
//    Vishal Kachroo <vishal@cs.wustl.edu>
//
// ============================================================================

#define QOSEVENT_MAIN

#include "ace/SOCK_Dgram_Mcast.h"
#include "ace/OS.h"
#include "ace/QoS_Session_Factory.h"
#include "ace/QoS_Session.h"

#include "QosEvent.h"
#include "Receiver_QOS_Event_Handler.h"
    
static int ValidOptions (char *argv[],
                         int argc,
                         OPTIONS *pOptions);
static void PrintOptions (OPTIONS *pOptions);
static void Usage (char *szProgramname,
                   OPTIONS *pOptions);

static const u_short PORT = ACE_DEFAULT_SERVER_PORT;

// To open QOS sockets administrative access is required on the
// machine.  Fill in default values for QoS structure.  The default
// values were simply choosen from existing QOS templates available
// via WSAGetQosByName.  Notice that ProviderSpecific settings are
// being allowed when picking the "default" template but not for
// "well-known" QOS templates.  Also notice that since data is only
// flowing from sender to receiver, different flowspecs are filled in
// depending upon whether this application is acting as a sender or
// receiver.

static int
fill_ace_qos_flowspec_default (ACE_QoS *pQos,
                               QOS_OPTIONS *pQosOptions)
{
  ACE_Flow_Spec ace_default_g711 (9200,
                                  708,
                                  18400,
                                  0,
                                  0,
                                  ACE_SERVICETYPE_GUARANTEED, 
                                  // SERVICETYPE_CONTROLLEDLOAD  
                                  // doesnt work here for a yet unknown
                                  // reason.
                                  368,
                                  368,
                                  25, // TTL. ACE specific. Not used on NT.
                                  1	// Priority. ACE specific. Not used on NT.
                                  );
	
  ACE_Flow_Spec ace_default_notraffic (ACE_QOS_NOT_SPECIFIED,
                                       ACE_QOS_NOT_SPECIFIED,
                                       ACE_QOS_NOT_SPECIFIED,
                                       ACE_QOS_NOT_SPECIFIED,
                                       ACE_QOS_NOT_SPECIFIED,
                                       ACE_SERVICETYPE_NOTRAFFIC,
                                       ACE_QOS_NOT_SPECIFIED,
                                       ACE_QOS_NOT_SPECIFIED,
                                       25,  // TTL. ACE specific. Not used on NT.
                                       1);  // Priority. ACE specific. Not used on NT.
  if (pQosOptions->bReceiver)
    {
      pQos->receiving_flowspec (ace_default_g711);
      pQos->sending_flowspec (ace_default_notraffic);        
    }
  else
    {
      pQos->sending_flowspec (ace_default_g711);     
      pQos->receiving_flowspec (ace_default_notraffic);
    }

  const iovec iov = {0, 0};
  pQos->provider_specific (iov);

  return 0;
}

int SetQos (QOS_OPTIONS *pQosOptions,
            int bSetQos,
            ACE_QoS *pQos) 
{
  int bError = FALSE;

  // fill in default values
  if (ACE_OS::strcasecmp (pQosOptions->szTemplate,
                          QOS_DEFAULTNAME) == 0)
    fill_ace_qos_flowspec_default (pQos,
                                   pQosOptions);
  else 
    ACE_DEBUG ((LM_DEBUG,
                "Run the program with -q:default option\n"));

  return !bError;
}

// Fill up the ACE_Flow_Spec with the default_g711 values
// as defined in the QoSEvent.h

int 
FillQoSTraffic (ACE_Flow_Spec &afc)
{
  afc = default_g711;
  return 0;
}

// Fill up the ACE_Flow_Spec with the default_notraffic values
// as defined in the QoSEvent.h

int
FillQoSNoTraffic (ACE_Flow_Spec &afc)
{
  afc = default_notraffic;
  return 0;
}

// This function fills up the ACE_QoS_Params with the supplied iovec and ACE_QoS.

int
FillQoSParams (ACE_QoS_Params &qos_params,
               iovec* iov, 
               ACE_QoS* qos)
{
  qos_params.callee_data (iov);
  qos_params.caller_data (0);
  qos_params.socket_qos (qos);
  qos_params.group_socket_qos (0);
  qos_params.flags (ACE_JL_BOTH);

  return 0;
}

int
main (int argc, char * argv[])
{  
  ACE_Protocol_Info protocol_info, *pinfo=0;

  OPTIONS options;

  if (!ValidOptions (argv,
                     argc,
                     &options))
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error in ValidOptions\n"),
                      -1);
 
  // XX pinfo = &protocol_info;
  // XX FindServiceProvider is not needed by RSVP
  // XX if (FindServiceProvider (options.spOptions.iProtocol,
  // XX                          options.spOptions.bQos,
  // XX                          options.spOptions.bMulticast,
  // XX                          pinfo) == FALSE)
  // XX   ACE_ERROR_RETURN ((LM_ERROR,
  // XX                      "Error in FindServiceProvider\n"),
  // XX                     -1);
	
  ACE_QoS qos;
         
  // By default the QOS is set as QOS_IOCTL_SET_BEFORE. 
  if (options.spOptions.bQos)
    {
      if (QOS_IOCTL_SET_BEFORE == options.qosOptions.qosIoctlSet)
        {
          if (SetQos (&options.qosOptions,
                      TRUE,
                      &qos))
            ACE_DEBUG ((LM_DEBUG,
                        "  QOS set before accept\n"));
        }
      else if (QOS_IOCTL_SET_QOS == options.qosOptions.qosIoctlSet)
        {
          options.qosOptions.bDisableSignalling = TRUE;
          if (SetQos (&options.qosOptions,
                      TRUE,
                      &qos))
            ACE_DEBUG ((LM_DEBUG,
                        "  QOS set qos before accept - will be "
                        "set again in FD_QOS\n"));
        }
    }

  else
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Use the -q:default option to enable the QOS\n"),
                      -1);

  // Opening a new Multicast Datagram.
  ACE_SOCK_Dgram_Mcast dgram_mcast;

  // The windows example code uses PF_INET for the address family.
  // Winsock.h defines PF_INET to be AF_INET. The following
  // code internally uses AF_INET as a default for the underlying socket.

  ACE_INET_Addr mult_addr (options.port,
                           options.szHostname);

  // Fill the ACE_QoS_Params to be passed to the <ACE_OS::join_leaf>
  // through subscribe.

  ACE_QoS_Params qos_params;
  FillQoSParams (qos_params, 0, &qos);

  // Create a QoS Session Factory.
  ACE_QoS_Session_Factory session_factory;

  // Ask the factory to create a QoS session. This could be RAPI or
  // GQoS based on the parameter passed.
  ACE_QoS_Session *qos_session = 
    session_factory.create_session (ACE_QoS_Session_Factory::ACE_RAPI_SESSION);
// XX Shouldn't have to specify GQOS or RAPI?!?
// XX it is not clear that we need to pass in a key indicating the type
// XX of object to create.  Since we use RAPI flag at compile time can
// XX we assume rapi here also?  Or could we have RAPI and GQoS?

  // Create a destination address for the QoS session. The same
  // address should be used for the subscribe call later. A copy is
  // made below only to distinguish the two usages of the dest
  // address.

  ACE_INET_Addr dest_addr (mult_addr);

  // A QoS session is defined by the 3-tuple [DestAddr, DestPort,
  // Protocol]. Initialize the QoS session.
  if (qos_session->open (mult_addr,
                         IPPROTO_UDP) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error in opening the QoS session\n"),
                      -1);

  // The following call opens the Dgram_Mcast and calls the
  // <ACE_OS::join_leaf> with the qos_params supplied here. Note the
  // QoS session object is passed into this call. This subscribes the
  // underlying socket to the passed in QoS session. For joining
  // multiple multicast sessions, the following subscribe call should
  // be made with different multicast addresses and a new QoS session
  // object should be passed in for each such call. The QoS session
  // objects can be created only through the session factory. Care
  // should be taken that the mult_addr for the subscribe() call
  // matches the dest_addr of the QoS session object. If this is not
  // done, the subscribe call will fail. A more abstract version of
  // subscribe will be added that constrains the various features of
  // GQoS like different flags etc.

  if (dgram_mcast.subscribe (mult_addr,
                             qos_params,
                             1,
                             0,
                             AF_INET,
                             // ACE_FROM_PROTOCOL_INFO,
                             0,
                             pinfo,
                             0,
                             ACE_OVERLAPPED_SOCKET_FLAG 
                             | ACE_FLAG_MULTIPOINT_C_LEAF 
                             | ACE_FLAG_MULTIPOINT_D_LEAF,
                             qos_session) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error in subscribe\n"),
                      -1);
  else
    ACE_DEBUG ((LM_DEBUG,
                "Dgram_Mcast subscribe succeeds \n"));
			
  int nIP_TTL = 25;
  char achInBuf [BUFSIZ];
  u_long dwBytes;

  // Should this be abstracted into QoS objects ?? Doesnt seem to have
  // to do anything directly with QoS.
  if (ACE_OS::ioctl (dgram_mcast.get_handle (), // Socket.
                     ACE_SIO_MULTICAST_SCOPE, // IO control code.
                     &nIP_TTL, // In buffer.
                     sizeof (nIP_TTL), // Length of in buffer.
                     achInBuf, // Out buffer.
                     BUFSIZ, // Length of Out buffer.
                     &dwBytes, // bytes returned.
                     0, // Overlapped.
                     0) == -1) // Func.
    ACE_ERROR ((LM_ERROR,
                "Error in Multicast scope ACE_OS::ioctl() \n"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "Setting TTL with Multicast scope ACE_OS::ioctl call succeeds \n"));

  int bFlag = FALSE;

  // Should this be abstracted into QoS objects ?? Doesnt seem to have
  // to do anything directly with QoS.
  if (ACE_OS::ioctl (dgram_mcast.get_handle (), // Socket.
                     ACE_SIO_MULTIPOINT_LOOPBACK, // IO control code.
                     &bFlag, // In buffer.
                     sizeof (bFlag), // Length of in buffer.
                     achInBuf, // Out buffer.
                     BUFSIZ, // Length of Out buffer.
                     &dwBytes, // bytes returned.
                     0, // Overlapped.
                     0) == -1) // Func.
    ACE_ERROR ((LM_ERROR,
                "Error in Loopback ACE_OS::ioctl() \n"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "Disable Loopback with ACE_OS::ioctl call succeeds \n"));

  // Fill up an ACE_QoS and pass it to the overloaded ACE_OS::ioctl ()
  // that uses the I/O control code as SIO_SET_QOS.
  ACE_QoS ace_qos;

  // Make sure the flowspec is set in the correct direction for the
  // sender/client.
  ACE_Flow_Spec sending_flowspec;
  ACE_Flow_Spec receiving_flowspec;
  const iovec iov = {0, 0};

  FillQoSTraffic (receiving_flowspec);
  FillQoSNoTraffic (sending_flowspec);

  ace_qos.sending_flowspec (sending_flowspec);     
  ace_qos.receiving_flowspec (receiving_flowspec);
  ace_qos.provider_specific (iov); 
  
  // Set the QoS for the session. Replaces the ioctl () call that was
  // being made previously.
  if (qos_session->qos (&dgram_mcast,
                        ace_qos) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Unable to set QoS\n"),
                      -1);
  else
    ACE_DEBUG ((LM_DEBUG,
                "Setting QOS succeeds.\n"));

  // Instantiate a QOS Event Handler and pass the Dgram_Mcast and QoS
  // session object into it.
  ACE_QOS_Event_Handler qos_event_handler (dgram_mcast,
                                           qos_session);
	
  // Register the QOS Handler with the Reactor.
  if (ACE_Reactor::instance ()->register_handler 
      (&qos_event_handler,
       ACE_Event_Handler::QOS_MASK | ACE_Event_Handler::READ_MASK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error in registering QOS Handler\n"),
                      -1);

  // Start the event loop.
  ACE_DEBUG ((LM_DEBUG,
              "Running the Event Loop ... \n"));

  ACE_Reactor::instance ()->run_event_loop ();

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) shutting down server logging daemon\n"));
  return 0;
}

int 
FindServiceProvider(int iProtocol,
                    int bQos,
                    int bMulticast,
                    ACE_Protocol_Info   *pProtocolInfo)
{
  ACE_Protocol_Info *protocol_buffer = 0;
  u_long buffer_length = 0;
  u_long dwErr;
  int bProtocolFound = FALSE;

  // first have enum_protocols () tell us how big a buffer is needed.
  int ret_val = ACE_OS::enum_protocols (0,
                                        protocol_buffer, 
                                        &buffer_length);
  if (ret_val != -1)
    ACE_DEBUG ((LM_DEBUG,
                "enum_protocols () : should not have suceeded\n"));

  else if (ACE_ENOBUFS != (dwErr = ACE_OS::set_errno_to_wsa_last_error ()))
    // enum_protocols () failed for some reason not relating to buffer size 
    ACE_DEBUG ((LM_DEBUG,
                "enum_protocols () : failed for a reason other than "
                "inadequate buffer size : %d\n",
                ACE_OS::set_errno_to_wsa_last_error ()));
  else
    {
      if (buffer_length > 0)
        {
          void *ptr = 0;
          ACE_NEW_RETURN (ptr,
                          char [buffer_length],
                          0);
          protocol_buffer = (ACE_Protocol_Info *) ptr;
        }

      else
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Buffer length returned by enum_protocols () is"
                           "less than or equal to zero\n"),
                          -1);

      if (protocol_buffer)
        {
          // now we can call enum_protocols () again with the
          // expectation it will succeed because we have allocated a
          // big enough buffer.
          ret_val = ACE_OS::enum_protocols (0, 
                                            protocol_buffer, 
                                            &buffer_length);
          if (ret_val == -1)
            ACE_DEBUG ((LM_DEBUG,
                        "enum_protocols () : failed even after allocating"
                        " a big enough buffer : %d\n",
                        ACE_OS::set_errno_to_wsa_last_error ()));
          else
            {
              ACE_DEBUG ((LM_DEBUG,
                          "Size allocated for the Protocol Buffer = %d\n"
                          "\nList of transport protocols "
                          "returned by enum_protocols () : \n",
                          buffer_length));
			  
              // Loop thru protocols, looking for a matching service
              // provider.
              for (int i = 0; i < ret_val; i++)
                {
                  ACE_DEBUG ((LM_DEBUG,
                              "  sp <%s>\n",
                              protocol_buffer[i].szProtocol));

                  if (AF_INET == protocol_buffer[i].iAddressFamily 
                      && iProtocol == protocol_buffer[i].iProtocol)
                    {
                      // look for 
                      if (bQos && bMulticast)
                        {
                          if ((ACE_XP1_QOS_SUPPORTED == ((ACE_XP1_QOS_SUPPORTED
                                                          & protocol_buffer[i].dwServiceFlags1)))
                              && (ACE_XP1_SUPPORT_MULTIPOINT == (ACE_XP1_SUPPORT_MULTIPOINT &
                                                                 protocol_buffer[i].dwServiceFlags1)))
                            {
                              *pProtocolInfo = protocol_buffer[i];
                              bProtocolFound = TRUE;
                              break;
                            }
                        }
                      else if (bQos)
                        {
                          if (ACE_XP1_QOS_SUPPORTED == (ACE_XP1_QOS_SUPPORTED
                                                        & protocol_buffer[i].dwServiceFlags1))
                            {
                              *pProtocolInfo = protocol_buffer[i];
                              bProtocolFound = TRUE;
                              break;
                            } 
                        }
                      else if (bMulticast)
                        {
                          if ((ACE_XP1_SUPPORT_MULTIPOINT == (ACE_XP1_SUPPORT_MULTIPOINT &   
                                                              protocol_buffer[i].dwServiceFlags1)) 
                              && (ACE_XP1_QOS_SUPPORTED != (ACE_XP1_QOS_SUPPORTED & 
                                                            protocol_buffer[i].dwServiceFlags1)))
                            {
                              *pProtocolInfo = protocol_buffer[i];
                              bProtocolFound = TRUE;
                              break;
                            }
                        }
                      else if ((ACE_XP1_QOS_SUPPORTED != (ACE_XP1_QOS_SUPPORTED 
                                                          & protocol_buffer[i].dwServiceFlags1)))
                        {
                          *pProtocolInfo = protocol_buffer[i];
                          bProtocolFound = TRUE;
                          break;
                        }
                    } 

                } // for loop.
          
            } // ACE_OS::enum_protocols ().
      
          ACE_OS::free (protocol_buffer);
      
        } // protocol_buffer

    } // ACE_OS::enum_protocols ().

  if (bProtocolFound)
    ACE_DEBUG ((LM_DEBUG,
                "\n  Using service provider <%s>\n\n", 
                pProtocolInfo->szProtocol));

  return bProtocolFound;

}

int 
ValidOptions (char *argv[],
              int argc,
              OPTIONS *pOptions)
{
  int bValidOptions = TRUE;
  *pOptions = default_options;

  for (int i = 1; i < argc; i++)
    {
      if ((argv[i][0] == '-') || (argv[i][0] == '/') ) 
        {
          switch (ACE_OS::to_lower (argv[i][1])) 
            {
            case 'b' :
              if (ACE_OS::strlen (argv[i]) > 3)
                pOptions->nBufSize = 1024 * ACE_OS::atoi (&argv[i][3]);
              break;

            case 'c' :
              pOptions->qosOptions.bAlternateQos = TRUE;
              break;

            case 'd' :
              if (ACE_OS::strlen (argv[i]) > 3)
                pOptions->fillchar = argv[i][3];
              break;

            case 'e' :
              if (ACE_OS::strlen (argv[i]) > 3)
                pOptions->port = ACE_OS::atoi (&argv[i][3]);
              break;

            case 'i' :
              if (ACE_OS::strlen (argv[i]) > 3)
                {
                  if ('a' == argv[i][3] || 'A' == argv[i][3])
                    pOptions->qosOptions.qosIoctlSet = QOS_IOCTL_SET_AFTER;
                  else if ('b' == argv[i][3] || 'B' == argv[i][3])
                    pOptions->qosOptions.qosIoctlSet = QOS_IOCTL_SET_BEFORE;
                  else if ('d' == argv[i][3] || 'D' == argv[i][3])
                    pOptions->qosOptions.qosIoctlSet = QOS_IOCTL_SET_DURING;
                  else if ('q' == argv[i][3] || 'Q' == argv[i][3])
                    pOptions->qosOptions.qosIoctlSet = QOS_IOCTL_SET_QOS;
                  else
                    pOptions->qosOptions.qosIoctlSet = QOS_IOCTL_SET_BEFORE;
                }
              break;

            case 'l' :
              if (ACE_OS::strlen (argv[i]) > 3)
                pOptions->nRepeat = ACE_OS::atoi (&argv[i][3]);
              break;

            case 'm' :
              pOptions->spOptions.bMulticast = TRUE;
              if (ACE_OS::strlen (argv[i]) > 3)
                ACE_OS::strcpy (pOptions->szHostname,
                                &argv[i][3]);
              else
                ACE_OS::strcpy (pOptions->szHostname,
                                DEFAULT_MULTICASTGROUP);
              break;

            case 'n' : 
              pOptions->qosOptions.bReceiver = FALSE; 
              // multicast group overrides hostname on -n
              if (!pOptions->spOptions.bMulticast)
                {
                  if (ACE_OS::strlen (argv[i]) > 3)
                    ACE_OS::strcpy (pOptions->szHostname,
                                    &argv[i][3]);
                }
              break;

            case 'p' :
              if (ACE_OS::strlen (argv[i]) > 3)
                {
                  if ('u' == argv[i][3] || 'U' == argv[i][3])
                    pOptions->spOptions.iProtocol = IPPROTO_UDP;
                  else if ('t' == argv[i][3] || 'T' == argv[i][3])
                    pOptions->spOptions.iProtocol = IPPROTO_TCP;
                  else
                    pOptions->spOptions.iProtocol = IPPROTO_TCP;
                }
              break;

            case 'q' : 
              pOptions->spOptions.bQos = TRUE; 
              if (ACE_OS::strlen (argv[i]) > 3)
                ACE_OS::strcpy (pOptions->qosOptions.szTemplate,
                                &argv[i][3]);
              break;

            case 'r' :
              if (ACE_OS::strlen (argv[i]) > 3)
                {
                  if (ACE_OS::strcasecmp (argv[i],
                                          "-rsvp-confirm") == 0
                      || ACE_OS::strcasecmp (argv[i],
                                             "-rsvp_confirm") == 0)
                    {
                      pOptions->qosOptions.bConfirmResv = TRUE;
                      pOptions->qosOptions.bProviderSpecific = TRUE;
                    }
                  else if (ACE_OS::strcasecmp (argv[i],
                                               "-rsvp-wait") == 0
                           || ACE_OS::strcasecmp (argv[i],
                                                  "-rsvp_wait") == 0)
                    {
                      pOptions->qosOptions.bWaitToSend = TRUE;
                      pOptions->qosOptions.bProviderSpecific = TRUE;
                    }
                  else
                    ACE_DEBUG ((LM_DEBUG,
                                "Ignoring option <%s>\n", 
                                argv[i]));
                }
              break;

            case 's' :
              if (ACE_OS::strlen (argv[i]) > 3)
                pOptions->dwSleep =
                  ACE_OS::atoi (&argv[i][3]);
              break;

            case '?' :
              Usage (argv[0],
                     &default_options);
              return FALSE;
              break;

            default:
              ACE_DEBUG ((LM_DEBUG,
                          "  unknown options flag %s\n", 
                          argv[i]));
              Usage (argv[0],
                     &default_options);
              return FALSE;
              break;
            }
        }
      else
        {
          ACE_DEBUG ((LM_DEBUG,
                      "  unknown option %s\n", 
                      argv[i]));
          Usage (argv[0],
                 &default_options);
          return FALSE;
        }
    }


  if (pOptions->qosOptions.bReceiver)
    pOptions->fillchar = 0;

  if (pOptions->spOptions.bMulticast)
    {
      pOptions->spOptions.iProtocol = IPPROTO_UDP;
      pOptions->qosOptions.bSetDestaddr = FALSE;
    }

  if (IPPROTO_TCP == pOptions->spOptions.iProtocol)
    {
      pOptions->spOptions.bMulticast = FALSE;
      pOptions->qosOptions.bSetDestaddr = FALSE;
    }
  else if (!pOptions->spOptions.bMulticast)
    pOptions->qosOptions.bSetDestaddr = TRUE;

  if (pOptions->spOptions.bQos)
    {
      if (pOptions->qosOptions.bReceiver 
          && pOptions->qosOptions.bWaitToSend)
        pOptions->qosOptions.bWaitToSend = FALSE;  
      // not a valid receiver option

      if (!pOptions->qosOptions.bReceiver 
          && pOptions->qosOptions.bConfirmResv)
        pOptions->qosOptions.bWaitToSend = FALSE;  
      // not a valid sender option

      if (pOptions->qosOptions.bAlternateQos)
        // override repeat count to continuous mode
        pOptions->nRepeat = 0;					   

      if (IPPROTO_UDP == pOptions->spOptions.iProtocol
          && !pOptions->spOptions.bMulticast)
        // Using UDP, there WSAAccept will not be called, therefore
        // do not wait to set qos.
        pOptions->qosOptions.qosIoctlSet =
          QOS_IOCTL_SET_BEFORE;

      pOptions->qosOptions.bFineGrainErrorAvail = TRUE;
      pOptions->qosOptions.bQosabilityIoctls = TRUE;
      ACE_DEBUG ((LM_DEBUG,
                  "running on XX\n"));
    }

  if (pOptions->nBufSize > 0)
    ACE_NEW_RETURN (pOptions->buf,
                    char[pOptions->nBufSize],
                    0);

  else 
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Buffer size to be allocated is less than or"
                       "equal to zero\n"),
                      -1);

  if (pOptions->buf == 0)
    return FALSE;
  else
    {      
      ACE_OS::memset (pOptions->buf,
                      pOptions->fillchar,
                      pOptions->nBufSize);
      return TRUE;
    }
}

static 
void PrintOptions (OPTIONS *pOptions)
{
  ACE_DEBUG ((LM_DEBUG,
              "Options\n"
              "  Protocol %d\n"
              "  Port %d\n",
              pOptions->spOptions.iProtocol,
              pOptions->port));

  if (pOptions->qosOptions.bReceiver)
    ACE_DEBUG ((LM_DEBUG,
                "  Act as Receiver\n"));
  else
    {
      ACE_DEBUG ((LM_DEBUG,
                  "  Act as sender and send to %s\n"
                  "  Sleep %d milliseconds between sends\n"
                  "  Fill buffer with <%c>\n", 
                  pOptions->szHostname,
                  pOptions->dwSleep,
                  pOptions->fillchar));

      if (pOptions->nRepeat)
        ACE_DEBUG ((LM_DEBUG,
                    "  Repeat sending the buffer %d times\n", 
                    pOptions->nRepeat));
      else
        ACE_DEBUG ((LM_DEBUG,
                    "  Repeat sending the buffer continually\n"));

    }
  ACE_DEBUG ((LM_DEBUG,
              "  Bufsize %d (1K increments)\n"
              "  Multicast is %s\n", 
              pOptions->nBufSize,
              (pOptions->spOptions.bMulticast ? "Enabled" : "Disabled")));

  if (pOptions->spOptions.bQos)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "  Qos template %s\n"
                  "  Qos to be set ", 
                  pOptions->qosOptions.szTemplate));
      
      switch (pOptions->qosOptions.qosIoctlSet)
        {
        case QOS_IOCTL_SET_BEFORE: 
          ACE_DEBUG ((LM_DEBUG,
                      "Before accept/connect/joinleaf\n"));
          break;
        case QOS_IOCTL_SET_AFTER:  
          ACE_DEBUG ((LM_DEBUG,
                      "After accept/connect/joinleaf\n")); 
          break;
        case QOS_IOCTL_SET_DURING: 
          ACE_DEBUG ((LM_DEBUG,
                      "During accept/connect/joinleaf\n"));
          break;
        case QOS_IOCTL_SET_QOS: 
          if (pOptions->qosOptions.bReceiver)
            ACE_DEBUG ((LM_DEBUG,
                        "During FD_QOS, implies QOS with no "
                        "signaling set before accept/connect "
                        "(eventually)\n"));
          else
            {
              pOptions->qosOptions.qosIoctlSet = QOS_IOCTL_SET_BEFORE;
              ACE_DEBUG ((LM_DEBUG,
                          "Before connect\n"));
            }
          break;
        }

      if (pOptions->qosOptions.bConfirmResv )
        ACE_DEBUG ((LM_DEBUG,
                    "  RESV confirmation to be requested\n"));

      if (INVALID_SEND_PRIORITY != pOptions->qosOptions.SendPriority)
        ACE_DEBUG ((LM_DEBUG,
                    "  Qos TC SendPriority to be set to %d\n", 
                    pOptions->qosOptions.SendPriority));
      if (!pOptions->qosOptions.bReceiver)
        {
          if (pOptions->qosOptions.bWaitToSend)
            ACE_DEBUG ((LM_DEBUG,
                        "  Wait for RESV before sending data\n"));
          else
            ACE_DEBUG ((LM_DEBUG,
                        "  Do not wait for RESV to start sending data\n"));
        }
      else
        ACE_DEBUG ((LM_DEBUG,
                    "  Qos must be set during Accept = %s\n", 
                    (pOptions->qosOptions.bMustSetQosInAccept?"TRUE":"FALSE")));
           
      ACE_DEBUG ((LM_DEBUG,
                  "  Query for QOS buffer size: %s\n\n",
                  (pOptions->qosOptions.bQueryBufferSize?"TRUE":"FALSE")));
    }
  return;
}

//      Print out usage table for the program
void 
Usage (char *szProgramname,
       OPTIONS *pOptions)
{

  ACE_DEBUG ((LM_DEBUG,
              "usage:\n  %s -?\n\n"
              "  %s [-b:#] [-d:c] [-e:#] [-l:#] [-m:group] "
              "[-n:host] [-s:#] [-u]\n\t[-q:template [-i:[a|b|d|q]]"
              "[-c] [-rsvp-confirm] [-rsvp-wait]]\n\n",
              "  -?\t\tDisplay this help\n\n"
              "  -b:bufsize\tSize of send/recv buffer; in 1K increments (Def:%d)\n"
              "  -d:c\t\tCharacter used to fill buffer (Def:%c)\n"
              "  -e:port\tEndpoint number (port) to use (Def:%d)\n"
              "  -l:loop\tLoop count for sending buffer (0==>continuous)\n"
              "  -m:group\tMulticast group (IP) to join (Def:%s)\n"
              "  -n:host\tAct as the client and connect to 'host' (Def:%s)\n"
              "  -p:protocol\tTCP or UDP (def:TCP)\n"
              "  -s:#\t\tSleep # milliseconds between sends (def: 0)\n"
              "  -q:template\tEnable QoS and use 'template' to specify the name (Def:%s)\n"
              "  -c\t\tCycle enabling/disabling QOS on sending socket (Def: no cycling)\n"
              "  -i:[a|b|d|q]\tSet Qos After, Before, During accept/connect, or during FD_QOS\n\t\t\t(def: Before)\n"
              "  -rsvp-confirm\t\tRequest RESV confirmation be sent (Def: no confirmation)\n"
              "  -rsvp-wait\t\tWait for RESV from receiver before sending data\n",
              szProgramname,
              szProgramname,
              pOptions->nBufSize,
              pOptions->fillchar,
              pOptions->port,
              pOptions->nRepeat,
              DEFAULT_MULTICASTGROUP,
              pOptions->szHostname,
              pOptions->qosOptions.szTemplate));
  return;
}