summaryrefslogtreecommitdiff
path: root/TAO/utils/catior/catior.cpp
blob: 4e8e8124e7350851641bccb1f63f35cbc202b270 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/Utils/catior
//
// = FILENAME
//    catior.cpp
//
// = DESCRIPTION
//    Reads stringified IORs and outputs the encoded information
//
// = AUTHORS
//      Jeff Hopper <jhopper@nosc.mil>
//
// ============================================================================

#include "ace/Get_Opt.h"
#include <tao/corba.h>
#include <tao/debug.h>
#include <tao/Typecode.h>
#include <tao/IIOP_Object.h>

// Destringify URL style IIOP objref.

static CORBA::Object_ptr
iiop_string_to_object (CORBA::String string,
                       CORBA::Environment &env)
{
  // NIL objref encodes as just "iiop:" ... which has already been
  // removed, so we see it as an empty string.
  
  if (!string || !*string)
    return 0;
  
  // Type ID not encoded in this string ... makes narrowing rather
  // expensive, though it does ensure that type-safe narrowing code
  // gets thoroughly excercised/debugged!  Without a typeID, the
  // _narrow will be required to make an expensive remote "is_a" call.
  
  IIOP_Object *data;
  
  // null type ID.
  ACE_NEW_RETURN (data,
                  IIOP_Object ((char *) 0),
                  0);
  
  // Remove the "N.N//" prefix, and verify the version's one that we
  // accept
  
  if (isdigit (string [0]) 
      && isdigit (string [2]) 
      && string [1] == '.'
      && string [3] == '/' 
      && string [4] == '/')
    {
      data->profile.iiop_version.major = (char) (string [0] - '0');
      data->profile.iiop_version.minor = (char) (string [2] - '0');
      string += 5;
    }
  else
    {
      env.exception (new CORBA_DATA_CONVERSION (CORBA::COMPLETED_NO));
      data->_decr_refcnt ();
      return 0;
    }
  
  if (data->profile.iiop_version.major != IIOP::MY_MAJOR
      || data->profile.iiop_version.minor > IIOP::MY_MINOR)
    {
      env.exception (new CORBA_DATA_CONVERSION (CORBA::COMPLETED_NO));
      data->_decr_refcnt ();
      return 0;
    }
  
  // Pull off the "hostname:port/" part of the objref
  
  char *cp = ACE_OS::strchr (string, ':');

  if (cp == 0)
    {
      env.exception (new CORBA_DATA_CONVERSION (CORBA::COMPLETED_NO));
      data->_decr_refcnt ();
      return 0;
    }
  
  data->profile.host = CORBA::string_alloc (1 + cp - string);

  for (cp = data->profile.host;
       *string != ':';
       *cp++ = *string++)
    continue;
  
  *cp = 0;
  string++;
  
  cp = ACE_OS::strchr ((char *) string, '/');
  
  if (cp == 0)
    {
      env.exception (new CORBA_DATA_CONVERSION (CORBA::COMPLETED_NO));
      CORBA::string_free (data->profile.host);
      data->profile.host = 0;
      data->_decr_refcnt ();
      return 0;
    }
  
  data->profile.port = (short) ACE_OS::atoi ((char *) string);
  data->profile.object_addr (0);
  string = ++cp;
  
  // Parse the object key.
  TAO_POA::decode_string_to_sequence (data->profile.object_key,
                                      string);
  
  // Create the CORBA level proxy.
  TAO_ServantBase *servant =
    TAO_ORB_Core_instance ()->orb ()->_get_collocated_servant (data);
  
  // This will increase the ref_count on data by one.
  CORBA_Object *obj;
  
  ACE_NEW_RETURN (obj,
                  CORBA_Object (data, servant, servant != 0),
                  0);
  
  // Set the ref_count on data to 1, which is correct, because only
  // obj has now a reference to it.  data->_decr_refcnt ();
  return obj;
}

CORBA::Boolean
catior (CORBA::String str,
       CORBA::Environment &env)
{
  // Unhex the bytes, and make a CDR deencapsulation stream from the
  // resulting data.
  
  ACE_Message_Block mb (ACE_OS::strlen ((char *) str)  / 2 + 1
                        + CDR::MAX_ALIGNMENT);
  
  CDR::mb_align (&mb);
  
  char *buffer = mb.rd_ptr ();
  char *tmp = (char *) str;
  size_t len = 0;
  
  CORBA::Boolean continue_decoding;

  // The prefix of the IOR must be removed, and the string must start
  // with the encapsulation byte

  while (tmp [0] && tmp [1])
    {
      u_char byte;
    
      if (! (isxdigit (tmp [0]) && isxdigit (tmp [1])))
        break;
    
      byte = (u_char) (ACE::hex2byte (tmp [0]) << 4);
      byte |= ACE::hex2byte (tmp [1]);
    
      buffer [len++] = byte;
      tmp += 2;
    }
  
  if (tmp [0] && !isspace (tmp [0]))
    {
      env.exception (new CORBA::BAD_PARAM (CORBA::COMPLETED_NO));
      return  CORBA::B_FALSE;
    }
  
  // Create deencapsulation stream ... then unmarshal objref from that
  // stream.
  
  CORBA::Boolean byteOrder;
  
  {
    TAO_InputCDR encapStream (&mb);
    continue_decoding = encapStream.read_boolean (byteOrder);
  }
  
  mb.rd_ptr (1);
  mb.wr_ptr (2*len-1);
  TAO_InputCDR stream (&mb, byteOrder);
  
  if (byteOrder == CORBA::B_TRUE)
    ACE_DEBUG ((LM_DEBUG,
                "The Byte Order:\tLittle Endian\n"));
  else
    ACE_DEBUG ((LM_DEBUG,
                "The Byte Order:\tBig Endian\n"));
  
  // First, read the type hint. This will be the type_id encoded in an
  // object reference.
  CORBA::String type_hint;

  if ((continue_decoding = stream.decode (CORBA::_tc_string, &type_hint, 0, env)) 
      == CORBA::B_FALSE)
    {
      ACE_DEBUG ((LM_DEBUG, 
                  "cannot read type id\n"));
      return CORBA::TypeCode::TRAVERSE_STOP;
    }

  ACE_DEBUG ((LM_DEBUG,
              "The Type Id:\t\"%s\"\n",
              type_hint));
  
  // Release any memory associated with the type_hint
  CORBA::string_free (type_hint);
  
  // Read the profiles, discarding all until an IIOP profile comes by.
  // Once we see an IIOP profile, ignore any further ones.
  //
  // XXX this will need to change someday to let different protocol
  // code be accessed, not just IIOP.  Protocol modules will be
  // dynamically loaded from shared libraries via ORB_init (), and we
  // just need to be able to access such preloaded libraries here as
  // we unmarshal objrefs.
  
  CORBA::ULong profiles = 0;
  //  IIOP_Object *objdata = 0;
  
  continue_decoding = stream.read_ulong (profiles);

  // Get the count of profiles that follow.
  if (continue_decoding == CORBA::B_FALSE)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "cannot read the profile count\n"));
      return CORBA::TypeCode::TRAVERSE_STOP;
    }

  CORBA::ULong profile_counter = 0;
  
  // No profiles means a NIL objref.
  if (profiles == 0)
    return CORBA_TypeCode::TRAVERSE_CONTINUE;
  else
    while (profiles-- != 0)
      {
        ACE_DEBUG ((LM_DEBUG,
                    "Profile Count:\t%d\n",
                    ++profile_counter));
      
        // We keep decoding until we find a valid IIOP profile.
        CORBA::ULong tag;

        continue_decoding = stream.read_ulong (tag);
      
        // Get the profile ID tag.
        if (continue_decoding == CORBA::B_FALSE)
          {
            ACE_DEBUG ((LM_DEBUG,
                        "cannot read profile tag\n"));
            continue;
          }
      
        if (tag != TAO_IOP_TAG_INTERNET_IOP)
          {
            continue_decoding = stream.skip_string ();
            ACE_DEBUG ((LM_DEBUG,
                        "unknown tag %d skipping\n", tag));
            continue;
          }
      
        // OK, we've got an IIOP profile.  It's going to be
        // encapsulated ProfileData.  Create a new decoding stream and
        // context for it, and tell the "parent" stream that this data
        // isn't part of it any more.
      
        CORBA::ULong encap_len;
        continue_decoding = stream.read_ulong (encap_len);

        // ProfileData is encoded as a sequence of octet. So first get
        // the length of the sequence.
        if (continue_decoding == CORBA::B_FALSE)
          {
            ACE_DEBUG ((LM_DEBUG,
                        "cannot read encap length\n"));
            continue;
          }
      
        // Create the decoding stream from the encapsulation in the
        // buffer, and skip the encapsulation.
        TAO_InputCDR str (stream, encap_len);
      
        continue_decoding = str.good_bit ()
          && stream.skip_bytes (encap_len);
      
        if (!continue_decoding)
          {
            ACE_DEBUG ((LM_DEBUG,
                        "problem decoding encapsulated stream, "
                        "len = %d\n",
                        encap_len));
            continue;
          }
      
        // Read and verify major, minor versions, ignoring IIOP
        // profiles whose versions we don't understand.
        //
        // XXX this doesn't actually go back and skip the whole
        // encapsulation...
        CORBA::Octet iiop_version_major, iiop_version_minor;
        if (! (str.read_octet (iiop_version_major)
              && iiop_version_major == IIOP::MY_MAJOR
              && str.read_octet (iiop_version_minor)
              && iiop_version_minor <= IIOP::MY_MINOR))
          {
            ACE_DEBUG ((LM_DEBUG,
                        "detected new v%d.%d IIOP profile",
                        iiop_version_major,
                        iiop_version_minor));
            continue;
          }
      
        ACE_DEBUG ((LM_DEBUG,
                    "IIOP Version:\t%d.%d\n",
                    iiop_version_major,
                    iiop_version_minor));
      
        // Get host and port.
        CORBA::UShort port_number;
        CORBA::String hostname;
        if (str.decode (CORBA::_tc_string,
                        &hostname,
                        0,
                        env) != CORBA::TypeCode::TRAVERSE_CONTINUE
            || !str.read_ushort (port_number))
          {
            ACE_DEBUG ((LM_DEBUG,
                        "error decoding IIOP host/port"));
            return CORBA::TypeCode::TRAVERSE_STOP;
          }
      
        ACE_DEBUG ((LM_DEBUG,
                    "Host Name:\t%s\n",
                    hostname));
        ACE_DEBUG ((LM_DEBUG,
                    "Port Number:\t%d\n",
                    port_number));
        CORBA::string_free (hostname);
      
        // ... and object key.

        CORBA::ULong objKeyLength = 0;
        continue_decoding = str.read_ulong (objKeyLength);

        ACE_DEBUG ((LM_DEBUG,
                    "Object Key len:\t%d\n",
                    objKeyLength));
      
        ACE_DEBUG ((LM_DEBUG,
                    "Object Key as hex:\n"));
        
        CORBA::Octet anOctet;
        CORBA::String objKey = CORBA::string_alloc (objKeyLength + 1);
      
        short counter = -1;

        for (u_int i = 0; i < objKeyLength; i++)
          {
            if (++counter == 8) 
              { 
                ACE_DEBUG ((LM_DEBUG, 
                            "\n"));
                counter = 0; 
              }
            str.read_octet (anOctet);

            ACE_DEBUG ((LM_DEBUG,
                        "%x ",
                        anOctet));
            objKey[i] = (char) anOctet;
          }

        objKey[i] = '\0';
      
        ACE_DEBUG ((LM_DEBUG,
                    "\nThe Object Key as string:\n%s\n",
                    objKey));
        CORBA::string_free (objKey);
      }
  return CORBA::B_TRUE;
}

int 
main (int argc, char *argv[])
{
  ACE_Get_Opt get_opt (argc, argv, "f:n:");
  
  CORBA::ORB_ptr orb_ptr = CORBA::ORB_init (argc, argv);
  
  CORBA::Environment env;
  
  char opt;
  while ((opt = get_opt ()) != EOF)
    {
      switch (opt)
        {
        case 'n':
            //  read the CosName from the NamingService convert the
            //  object_ptr to a CORBA::String_var via the call to
            //  object_to_string.
            ACE_DEBUG ((LM_DEBUG,
                        "opening a connection to the NamingService\n"
                        "resolving the CosName %s\n",
                        get_opt.optarg));
            break;
        case 'f':
          {
            //  read the file into a CORBA::String_var
            ACE_DEBUG ((LM_DEBUG, 
                        "reading the file %s\n",
                        get_opt.optarg));
        
            ifstream ifstr (get_opt.optarg);
        
            if (!ifstr.good ())
              {
                ifstr.close ();
                return -1;
              }
        
            char ch;
            ACE_CString aString;

            while (!ifstr.eof ())
              {
                ifstr.get (ch); 
                aString += ch;
              }
        
            ACE_DEBUG ((LM_DEBUG,
                        "\nhere is the IOR\n%s\n\n",
                        aString.fast_rep ()));
        
            CORBA::String str;
            if (aString.find ("IOR:") == 0)
              {
                ACE_DEBUG ((LM_DEBUG,
                            "decoding an IOR:\n"));
          
                // Strip the IOR: off the string.
                ACE_CString prefix = "IOR:";
                short prefixLength = prefix.length ();
          
                ACE_CString subString =
                  aString.substring (prefixLength,
                                     aString.length () - prefixLength);
                subString[subString.length () - 1] = '\0';
                str = subString.rep ();
              }
            else if (aString.find ("iiop:") == 0)
              {
                ACE_DEBUG ((LM_DEBUG,
                            "decoding an IIOP IOR\n"));
          
                ACE_CString prefix = "IIOP:";
                short prefixLength = prefix.length () + 1;

                ACE_DEBUG ((LM_DEBUG,
                            "prefix length = %d\n%s\n",
                            prefixLength,
                            aString.substring (prefixLength,
                                               aString.length () - prefixLength).fast_rep ()));
                str = aString.rep ();
              }
            else 
              ACE_ERROR_RETURN ((LM_DEBUG,
                                 "Don't know how to decode this IOR\n"),
                                -1);
        
            CORBA::Boolean  b = catior (str, env);
        
            if (b == CORBA::B_TRUE)
              ACE_DEBUG ((LM_DEBUG,
                          "catior returned true\n"));
            else
              ACE_DEBUG ((LM_DEBUG,
                          "catior returned false\n"));
            ifstr.close ();
          }
        break;
        case '?':
        default:
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Usage: %s "
                             "-f filename "
                             "-n CosName "
                             "\n"
                             "Reads an IOR "
                             "and dumps the contents to stdout "
                             "\n",
                             argv[0]),
                             1);
        }
    }
  return 0;
}