File: extstack.c

package info (click to toggle)
regina 3.3-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,952 kB
  • ctags: 7,235
  • sloc: ansic: 50,555; sh: 2,727; lex: 2,298; yacc: 1,498; makefile: 1,017; cpp: 117
file content (836 lines) | stat: -rw-r--r-- 26,516 bytes parent folder | download | duplicates (3)
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
#ifndef lint
static char *RCSid = "$Id: extstack.c,v 1.19 2004/04/18 02:54:33 florian Exp $";
#endif

/*
 *  The Regina Rexx Interpreter
 *  Copyright (C) 1992-1994  Anders Christensen <anders@pvv.unit.no>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#if defined(__WATCOMC__) && defined(OS2)
# include <os2/types.h>
#endif
#include "rexx.h"

#ifdef EXTERNAL_TO_REGINA
# include <assert.h>
/* tmpstr_of must be defined before the inclusion of extstack.h since it
 * redefines tsd_t (grrr) */
volatile char *tmpstr_of( tsd_t *TSD, const streng *input )
{
   /* even exiterror isn't permitted at this point. */
   assert(0); /* hint while debugging */
   return((volatile char *) input->value);
}
#endif


#if defined(WIN32)
# if defined(_MSC_VER)
#  if _MSC_VER >= 1100
/* Stupid MSC can't compile own headers without warning at least in VC 5.0 */
#   pragma warning(disable: 4115 4201 4214 4514)
#  endif
#  include <windows.h>
#  if _MSC_VER >= 1100
#   pragma warning(default: 4115 4201 4214)
#  endif
# else
#  include <windows.h>
#  include <winsock.h>
# endif
# include <io.h>
#else
# ifdef HAVE_TYPES_H
#  include <types.h>
# endif
# ifdef HAVE_SYS_SOCKET_H
#  include <sys/socket.h>
# endif
# ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>
#  endif
# ifdef HAVE_SYS_SELECT_H
#  include <sys/select.h>
# endif
# ifdef HAVE_NETDB_H
#  include <netdb.h>
# endif
# ifdef HAVE_ARPA_INET_H
#  include <arpa/inet.h>
# endif
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>

#include "extstack.h"

static int debug = -1 ;
#define DEBUGDUMP(x) {                                                       \
                        if ( debug == -1 )                                   \
                        {                                                    \
                           debug = ( getenv( "RXDEBUG" ) == NULL ) ? 0 : 1 ; \
                        }                                                    \
                        if ( debug )                                         \
                        {                                                    \
                           x ;                                               \
                        }                                                    \
                     }

/* "localhost" maps to "127.0.0.1" on most systems but it may use DNS on
 * badly configured systems. Use "127.0.0.1" to bypass any errors.
 */
static const char ReginaLocalHost[] = "127.0.0.1";

void showerror( int err, int suberr, char *tmpl, ...)
{
   va_list argptr;
   if ( suberr )
      fprintf( stderr, "Error:%d.%d - ", err, suberr );
   else
      fprintf( stderr, "Error:%d - ", err );
   va_start( argptr, tmpl );
   vfprintf( stderr, tmpl, argptr );
   va_end( argptr );
   fprintf( stderr, "\n" );
}

int init_external_queue( const tsd_t *TSD )
{
   int rc=0;
#ifdef WIN32
   WORD wsver = (WORD)MAKEWORD(1,1);
   WSADATA wsaData;
   if ( WSAStartup( wsver, &wsaData ) != 0 )
   {
      /* TSD will be NULL when called from rxqueue or rxstack */
      if ( TSD == NULL )
         showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_NO_WINSOCK, ERR_RXSTACK_NO_WINSOCK_TMPL, WSAGetLastError() );
      else if ( !TSD->called_from_saa )
         exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_NO_WINSOCK, WSAGetLastError() );
      rc = 1;
   }
#else
   TSD = TSD; /* keep compiler happy */
#endif
   return rc;
}

void term_external_queue( void )
{
#ifdef WIN32
   WSACleanup();
#endif
}

int get_length_from_header( const tsd_t *TSD, const streng *header )
{
   int length=0,error=1;
   streng *result=NULL;

   result = MAKESTRENG( RXSTACK_HEADER_SIZE - 1 );
   if ( result )
   {
      result->len = RXSTACK_HEADER_SIZE - 1;
      memcpy( result->value, header->value+1, RXSTACK_HEADER_SIZE-1 );
      DEBUGDUMP(printf("Hex value: %.*s\n", PSTRENGLEN(result),PSTRENGVAL(result)););
      length = REXX_X2D( result, &error );
      DROPSTRENG( result );
   }
   return ( error ) ? 0 : length;
}

#if !defined(NO_EXTERNAL_QUEUES)
int default_port_number( void )
{
   int portno;
   char dummy;
   char *port = getenv("RXSTACK"); /* FIXME: May be overwritten by VALUE-BIF */

   if ( port != NULL )
   {
      if ( sscanf( port, "%d %c", &portno, &dummy ) == 1 )
         if ( (portno > 1) && (portno < 0xFFFF) )
            return portno;
   }
   return RXSOCKET;
}

int default_external_address( void )
{
   return inet_addr( ReginaLocalHost );
}

streng *default_external_name( const tsd_t *TSD )
{
   int len;
   streng *result;

   len = sizeof(ReginaLocalHost); /* includes the term. \0 */
   result = MAKESTRENG( len );
   result->len = len - 1;
   memcpy( result->value, ReginaLocalHost, len ) ;
   return result ;
}

int connect_to_rxstack( tsd_t *TSD, Queue *q )
{
   struct sockaddr_in server;
   int eno;

   /*
    * Connect to external rxstack process/daemon/service
    */
   assert( q->type == QisExternal ) ;
   DEBUGDUMP(printf("In connect_to_rxstack: q = {name=%.*s, address=%08X, portno=%d}\n", q->u.e.name->len, q->u.e.name->value, q->u.e.address, q->u.e.portno););
   memset( &server, 0, sizeof(server) );
   server.sin_family = AF_INET;
   server.sin_addr.s_addr = q->u.e.address;
   server.sin_port = htons((unsigned short) q->u.e.portno);

   q->u.e.socket = socket( AF_INET, SOCK_STREAM, 0 );
   if ( q->u.e.socket >= 0 )
   {

      if ( connect( q->u.e.socket, (struct sockaddr *)&server, sizeof(server) ) >= 0 )
      {
         DEBUGDUMP(printf("In connect_to_rxstack: socket=%d\n", q->u.e.socket););
         return(q->u.e.socket);
      }
      eno = errno;
      close(q->u.e.socket);
      q->u.e.socket = -1;
      errno = eno;
   }
   /* TSD will be NULL when called from rxqueue or rxstack */
   if ( TSD == NULL )
      showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_CANT_CONNECT, ERR_RXSTACK_CANT_CONNECT_TMPL, q->u.e.name, q->u.e.portno, strerror ( errno ) );
   else if ( !TSD->called_from_saa )
      exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_CANT_CONNECT, tmpstr_of( (void *) TSD, q->u.e.name ), q->u.e.portno, strerror ( errno ) );

   return -1;
}

int disconnect_from_rxstack( const tsd_t *TSD, Queue *q )
{
   int rc;

   assert( q->type == QisExternal ) ;
   if (q->u.e.socket != -1)
   {
      DEBUGDUMP(printf("Diconnecting from socket %d\n", q->u.e.socket ););
      rc = send_command_to_rxstack( TSD, q->u.e.socket, RXSTACK_EXIT_STR, NULL, 0 );
      close( q->u.e.socket );
   }
   else
      rc = 0; /* success: 0 bytes transfered */
   if (q->u.e.name != NULL)
      DROPSTRENG( q->u.e.name );
   memset( q, 0, sizeof(Queue) ) ;
   q->type = QisUnused ;
   return rc;
}

int send_command_to_rxstack( const tsd_t *TSD, int sock, const char *action, const char *str, int len )
{
   streng *qlen, *header;
   int rc=-1;

   DEBUGDUMP(printf("Sending to %d Action: %s <%.*s> Len:%d\n", sock, action, len, (str) ? str : "", len););
   qlen = REXX_D2X( len );
   if ( qlen )
   {
      header = REXX_RIGHT( qlen, RXSTACK_HEADER_SIZE, '0');
      DROPSTRENG( qlen );
      if ( header )
      {
         header->value[0] = action[0];
         rc = send( sock, PSTRENGVAL(header), PSTRENGLEN(header), 0 );
         DEBUGDUMP(printf("Send length: %.*s(%d) rc %d\n", PSTRENGLEN(header),PSTRENGVAL(header),PSTRENGLEN(header),rc););
         if ( str && rc != -1 )
         {
            rc = send( sock, str, len, 0 );
            DEBUGDUMP(printf("Send str length: %d\n", rc););
         }
         DROPSTRENG( header );
      }
   }
   return rc;
}

static int inject_result_from_rxstack( int sock, streng *str, int size )
{
  /*
   * Reads size bytes from sock and adds them to str. No overflow checking
   * is performed.
   * returns the result from recv.
   */
   int rc;

   rc = recv( sock, PSTRENGVAL(str) + PSTRENGLEN(str), size, 0 );
   DEBUGDUMP(printf("Recv result: %.*s(%d) rc %d\n", size, PSTRENGVAL(str) + PSTRENGLEN(str), PSTRENGLEN(str), rc ); );
   str->len += size;
   return rc;
}

streng *read_result_from_rxstack( const tsd_t *TSD, int sock, int result_size )
{
  /*
   * Creates a streng of length 'result_size' and if non-zero,
   * reads this many characters from the socket.
   * The caller MUST DROPSTRENG this streng.
   */
   streng *result;
   int rc;

   result = MAKESTRENG( result_size );
   if ( result )
   {
      if ( result_size )
      {
         result->len = 0 ; /* MAKESTRENG sets ->len to nonzero sometimes */
         rc = inject_result_from_rxstack( sock, result, result_size );
      }
   }
   return result;
}

/* parse_queue validates the queue name. The format is [queue][@host[:port]]
 * *eq is filled and may contain default values.
 * queue may be empty to designate the current/default queue.
 * host may be empty to designate the local host. It has to be either a
 * IPv4 hostname or a IPv4 IP address.
 * The [@host[:port]] part is chopped off and the pure queue name remains.
 * return values:
 * -code: error detected (exiterror() is thrown normally!), code is one
 *        or RXQUEUE_...
 *     0: queue is NULL
 *     1: queue is parsed successfully, either with or without host part.
 */
int parse_queue( tsd_t *TSD, streng *queue, Queue *q )
{
   int len, AtPos;
   char *h, dummy;
   struct hostent *he;

   q->type = QisExternal ;
   q->u.e.portno = 0;  /* Good practise for initialisation */
   q->u.e.socket = -1;
   q->u.e.address = 0;
   q->u.e.name = NULL;

   if ( queue == NULL )
      return 0 ;

   len = PSTRENGLEN( queue );
   if (( h = memchr( PSTRENGVAL( queue ), '@', len ) ) == NULL )
      return 1 ;

   AtPos = (int) ( h - PSTRENGVAL( queue ) ) ;
   h++ ;
   len -= AtPos + 1 ;

   /* h is at host, len contains its length. We now do a trick. We copy the
    * complete host part into q->u.e.name, but a '\0' appended. We can simply
    * check out the portno part and we can apply sscanf() on it. Working with
    * a temporary buffer for the portno has no benefit, we can save 5 byte
    * per connection with a maximum of 1 permanent connection ==> 5 byte
    */
   q->u.e.name = MAKESTRENG( len + 1 ) ;
   if (q->u.e.name == NULL )
   {
      /* TSD will be NULL when called from rxqueue or rxstack */
      if ( TSD == NULL )
         showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL );
      else if ( !TSD->called_from_saa )
         exiterror( ERR_STORAGE_EXHAUSTED, 0 );
      return -4;  /* RXQUEUE_NOEMEM */
   }
   memcpy( PSTRENGVAL( q->u.e.name ), h, len ) ;
   PSTRENGVAL( q->u.e.name )[len] = '\0' ;
   q->u.e.name->len = len ;

   if (( h = memchr( PSTRENGVAL( q->u.e.name ), ':', len ) ) == NULL )
      q->u.e.portno = default_port_number();
   else
   {
      q->u.e.name->len = (int) ( h - PSTRENGVAL( q->u.e.name ) ) ;
      *h++ = '\0' ; /* chop off the portno part and jump over ':' */
      if ( sscanf( h, "%d %c", &q->u.e.portno, &dummy ) != 1 )
         q->u.e.portno = 0;
      if ( ( q->u.e.portno < 1 ) || ( q->u.e.portno > 0xFFFF ) )
      {
         if ( TSD == NULL )
            showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INVALID_QUEUE, ERR_RXSTACK_INVALID_QUEUE_TMPL, PSTRENGVAL( queue ) );
         else if ( !TSD->called_from_saa )
            exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INVALID_QUEUE, tmpstr_of( (void *) TSD, queue ) );
         DROPSTRENG( q->u.e.name ) ;
         q->u.e.name = NULL ;
         return -5; /* RXQUEUE_BADQNAME */
      }
   }

   h = PSTRENGVAL( q->u.e.name ) ;
   if ( *h == '\0' )
   {
      q->u.e.address = default_external_address();
      DROPSTRENG( q->u.e.name );
      q->u.e.name = default_external_name( TSD );
   }
   else
   {
      /* h is either a dotted name or a host name, always try the dotted one
       * first. It's MUCH faster.
       */
      q->u.e.address = inet_addr( h );
      if ( ( q->u.e.address == 0 ) || ( q->u.e.address == -1 ) ) /* various errors */
      {
         he = gethostbyname( h ) ;
         if ( ( he != NULL )
           && ( he->h_addr != NULL )
           && ( he->h_addrtype == AF_INET ) )
            q->u.e.address = ( ( struct in_addr * ) he->h_addr )->s_addr;
         if ( ( q->u.e.address == 0 ) || ( q->u.e.address == -1 ) )
         {
            /* TSD will be NULL when called from rxqueue or rxstack */
            if ( TSD == NULL )
               showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_NO_IP, ERR_RXSTACK_NO_IP_TMPL, PSTRENGVAL( q->u.e.name ) );
            else if ( !TSD->called_from_saa )
               exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_NO_IP, tmpstr_of( (void *) TSD, q->u.e.name ) );
            DROPSTRENG( q->u.e.name ) ;
            q->u.e.name = NULL ;
            return -5; /* RXQUEUE_BADQNAME */
         }
      }
   }

   queue->len = AtPos ; /* chop off the host part */
   return 1 ;
}

int delete_queue_from_rxstack( const tsd_t *TSD, int sock, const streng *queue_name )
{
   int rc;
   streng *result;

   rc = send_command_to_rxstack( TSD, sock, RXSTACK_DELETE_QUEUE_STR, PSTRENGVAL( queue_name ), PSTRENGLEN( queue_name ) );
   if ( rc != -1 )
   {
      result = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
      if ( result )
      {
         rc = result->value[0]-'0';
         DROPSTRENG( result );
      }
   }
   return rc;
}

int timeout_queue_on_rxstack( const tsd_t *TSD, int sock, long timeout )
{
   int rc=0;
   streng *result,*qtimeout, *hex_timeout;

   qtimeout = REXX_D2X( timeout );
   if ( qtimeout )
   {
      hex_timeout = REXX_RIGHT( qtimeout, RXSTACK_TIMEOUT_SIZE, '0');
      DROPSTRENG( qtimeout );
      if ( hex_timeout )
      {
         DEBUGDUMP(printf("Send timeout: %.*s(%d) rc %d\n", PSTRENGLEN(hex_timeout),PSTRENGVAL(hex_timeout),PSTRENGLEN(hex_timeout),rc););
         rc = send_command_to_rxstack( TSD, sock, RXSTACK_TIMEOUT_QUEUE_STR, PSTRENGVAL(hex_timeout), PSTRENGLEN(hex_timeout) );
         DROPSTRENG( hex_timeout );
         if ( rc != -1 )
         {
            result = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
            if ( result )
            {
               rc = result->value[0]-'0';
               DROPSTRENG( result );
            }
         }
      }
   }
   return rc;
}

int get_number_in_queue_from_rxstack( const tsd_t *TSD, int sock, int *errcode )
{
   int rc,length=0;
   streng *header;

   rc = send_command_to_rxstack( TSD, sock, RXSTACK_NUMBER_IN_QUEUE_STR, NULL, 0 );
   if ( rc != -1 )
   {
      header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
      if ( header )
      {
         rc = header->value[0]-'0';
         if ( rc == 0 )
         {
            /*
             * now get the length from the header
             */
            DEBUGDUMP(printf("before get_length_from_header: %.*s\n", header->len, header->value););
            length = get_length_from_header( TSD, header );
         }
         else
         {
            /* TSD will be NULL when called from rxqueue or rxstack */
            if ( TSD == NULL )
               showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Getting number in queue" );
            else if ( !TSD->called_from_saa )
               exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting number in queue"  );
            rc = 9; /* RXQUEUE_NOTREG */
         }
         DROPSTRENG( header );
      }
   }
   if ( errcode )
      *errcode = rc;
   return length;
}

int clear_queue_on_rxstack( const tsd_t *TSD, int sock )
{
   int rc ;
   streng *result ;

   rc = send_command_to_rxstack( TSD, sock, RXSTACK_EMPTY_QUEUE_STR, NULL, 0 );
   if ( rc != -1 )
   {
      result = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
      if ( result )
      {
         rc = result->value[0]-'0';
         DROPSTRENG( result );
      }
   }
   return rc ;
}

static streng *init_connect_string( const tsd_t *TSD, const Queue *q, int addlength )
{
   /* Determines the length of the connect string of eq and sums up
    * addlength. A streng of that size is created and returned.
    */
   streng *retval ;
   int len = 0 ;

   assert( q->type == QisExternal ) ;
   if ( q->u.e.name != 0 )
      len = PSTRENGLEN( q->u.e.name ) ;
   if ( len == 0 )
      len = 15 ; /* enough for "xxx.xxx.xxx.xxx" */
   len += 8 ;    /* enough for "@" and ":65535" and a terminating 0 */

   retval = MAKESTRENG( len + addlength ) ;
   if ( retval == NULL )
   {
      /* TSD will be NULL when called from rxqueue or rxstack */
      if ( TSD == NULL )
         showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL );
      else if ( !TSD->called_from_saa )
         exiterror( ERR_STORAGE_EXHAUSTED, 0 );
   }
   else
      retval->len = 0 ; /* MAKESTRENG sets ->len to nonzero sometimes */
   return retval ;
}

static void add_connect_string( const Queue *q, streng *str )
{
   /* Adds the connect string of eq to str.
    * This may be "@localhost:5757"
    * We always add the port. It may be redefined in between by a call to
    * the VALUE BIF.
    */
   int sum = 0, len ;
   char *ptr = PSTRENGVAL( str ) ;
   char *addr ;
   struct in_addr ia ;

   assert( q->type == QisExternal ) ;
   ptr += PSTRENGLEN( str ) ;
   *ptr++ = '@' ;
   sum++ ;
   if ( q->u.e.name != 0 )
   {
      len = PSTRENGLEN( q->u.e.name ) ;
      memcpy( ptr, PSTRENGVAL( q->u.e.name ), len ) ;
      ptr += len ;
      sum += len ;
   }
   if ( sum == 1 ) /* no q->u.e.name? use the dotted IP address */
   {
      ia.s_addr = q->u.e.address ;
      addr = inet_ntoa( ia ) ;
      len = strlen( addr ) ;
      memcpy( ptr, addr, len ) ;
      ptr += len ;
      sum += len ;
   }
   /* finally add the port */
   sum += sprintf( ptr, ":%u", (unsigned) q->u.e.portno ) ;
   str->len += sum ;
}

int get_queue_from_rxstack( const tsd_t *TSD, const Queue *q, streng **result )
{
   int rc,length;
   streng *header;

   assert( q->type == QisExternal ) ;
   rc = send_command_to_rxstack( TSD, q->u.e.socket, RXSTACK_GET_QUEUE_STR, NULL, 0 );
   if ( rc != -1 )
   {
      header = read_result_from_rxstack( TSD, q->u.e.socket, RXSTACK_HEADER_SIZE );
      if ( header )
      {
         rc = header->value[0]-'0';
         if ( rc == 0 )
         {
            /*
             * now get the length from the header and get that many characters...
             */
            length = get_length_from_header( TSD, header );
            if ( ( *result = init_connect_string( TSD, q, length ) ) != NULL )
            {
               inject_result_from_rxstack( q->u.e.socket, *result, length ) ;
               add_connect_string( q, *result );
            }
            else
               rc = 4; /* RXQUEUE_NOEMEM, not really used */
         }
         else
         {
            /* TSD will be NULL when called from rxqueue or rxstack */
            if ( TSD == NULL )
               showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Getting queue from stack" );
            else if ( !TSD->called_from_saa )
               exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting queue from stack" );
         }
         DROPSTRENG( header );
      }
   }
   return rc;
}

int get_line_from_rxstack( const tsd_t *TSD, int sock, streng **result, int nowait )
{
   int rc,length;
   streng *header;

   rc = send_command_to_rxstack( TSD, sock, (nowait) ? RXSTACK_FETCH_STR : RXSTACK_PULL_STR, NULL, 0 );
   if ( rc != -1 )
   {
      header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
      if ( header )
      {
         rc = header->value[0]-'0';
         if ( rc == 0 )
         {
            /*
             * now get the length from the header and get that many characters...
             */
            length = get_length_from_header( TSD, header );
            *result = read_result_from_rxstack( TSD, sock, length );
         }
         else if ( rc == 1 || rc == 4 )
         {
            /*
             * queue is empty - return a NULL
             */
            *result = NULL;
         }
         else
         {
            /* TSD will be NULL when called from rxqueue or rxstack */
            if ( TSD == NULL )
               showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Getting line from queue" );
            else if ( !TSD->called_from_saa )
               exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting line from queue" );
         }
         DROPSTRENG( header );
      }
   }
   return rc;
}

int create_queue_on_rxstack( const tsd_t *TSD, const Queue *q, const streng *queue, streng **result )
{
   int rc,length;
   streng *header;

   assert( q->type == QisExternal ) ;
   rc = send_command_to_rxstack( TSD, q->u.e.socket, RXSTACK_CREATE_QUEUE_STR, (queue) ? PSTRENGVAL( queue ) : NULL, (queue) ? PSTRENGLEN( queue ) : 0 );
   if ( rc != -1 )
   {
      header = read_result_from_rxstack( TSD, q->u.e.socket, RXSTACK_HEADER_SIZE );
      if ( header )
      {
         rc = header->value[0]-'0';
         if ( ( rc == 0 ) || ( rc == 1 ) )
         {
            /*
             * now get the length from the header and get that many characters...
             */
            length = get_length_from_header( TSD, header );
            if ( ( *result = init_connect_string( TSD, q, length ) ) != NULL )
            {
               inject_result_from_rxstack( q->u.e.socket, *result, length ) ;
               add_connect_string( q, *result );
            }
            else
               rc = 4; /* RXQUEUE_NOEMEM */
         }
         else
         {
            /* TSD will be NULL when called from rxqueue or rxstack */
            if ( TSD == NULL )
               showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Creating queue" );
            else if ( !TSD->called_from_saa )
               exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Creating queue" );
            switch ( rc )
            {
               case 2: rc = 5; break; /* RXQUEUE_BADQNAME */
               case 3: rc = 4; break; /* RXQUEUE_NOEMEM */
               case 6: rc = 1; break; /* RXQUEUE_STORAGE */
               default:;
            }
         }
         DROPSTRENG( header );
      }
   }
   return rc;
}

int set_queue_in_rxstack( const tsd_t *TSD, int sock, const streng *queue_name )
{
   int rc,length;
   streng *header, *dummy;

   rc = send_command_to_rxstack( TSD, sock, RXSTACK_SET_QUEUE_STR, PSTRENGVAL( queue_name ), PSTRENGLEN( queue_name ) );
   if ( rc != -1 )
   {
      header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
      if ( header )
      {
         rc = header->value[0]-'0';
         if ( rc == 0 )
         {
            /*
             * now get the length from the header and get that many characters...
             */
            length = get_length_from_header( TSD, header );
            dummy = read_result_from_rxstack( TSD, sock, length );
            /* dummy is no longer used */
            DROPSTRENG( dummy ) ;
         }
         else
         {
            /* TSD will be NULL when called from rxqueue or rxstack */
            if ( TSD == NULL )
               showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Setting queue" );
            else if ( !TSD->called_from_saa )
               exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Setting queue" );
            switch ( rc )
            {
               case 3: rc = 4; break; /* RXQUEUE_NOEMEM */
               case 6: rc = 1; break; /* RXQUEUE_STORAGE */
               default:;
            }
         }
         DROPSTRENG( header );
      }
   }
   else
      rc = 100; /* RXQUEUE_NETERROR */
   return rc;
}

int queue_line_fifo_to_rxstack( const tsd_t *TSD, int sock, const streng *line )
{
   int rc;
   streng *header;

   rc = send_command_to_rxstack( TSD, sock, RXSTACK_QUEUE_FIFO_STR, PSTRENGVAL( line ), PSTRENGLEN( line ) );
   if ( rc != -1 )
   {
      header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
      if ( header )
      {
         rc = header->value[0]-'0';
         if ( rc != 0 )
         {
            /* TSD will be NULL when called from rxqueue or rxstack */
            if ( TSD == NULL )
               showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Queueing line" );
            else if ( !TSD->called_from_saa )
               exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Queueing line" );
            switch ( rc )
            {
               case 2: rc = 5; break; /* RXQUEUE_BADQNAME */
               case 3: rc = 4; break; /* RXQUEUE_NOEMEM */
               case 6: rc = 1; break; /* RXQUEUE_STORAGE */
               default:;
            }
         }
         DROPSTRENG( header );
      }
   }
   return rc;
}

int queue_line_lifo_to_rxstack( const tsd_t *TSD, int sock, const streng *line )
{
   int rc;
   streng *header;

   rc = send_command_to_rxstack( TSD, sock, RXSTACK_QUEUE_LIFO_STR, PSTRENGVAL( line ), PSTRENGLEN( line ) );
   if ( rc != -1 )
   {
      header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
      if ( header )
      {
         rc = header->value[0]-'0';
         if ( rc != 0 )
         {
            /* TSD will be NULL when called from rxqueue or rxstack */
            if ( TSD == NULL )
               showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Queueing line" );
            else if ( !TSD->called_from_saa )
               exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Queueing line" );
            switch ( rc )
            {
               case 2: rc = 5; break; /* RXQUEUE_BADQNAME */
               case 3: rc = 4; break; /* RXQUEUE_NOEMEM */
               case 6: rc = 1; break; /* RXQUEUE_STORAGE */
               default:;
            }
         }
         DROPSTRENG( header );
      }
   }
   return rc;
}
#endif