Debian Codegraph @

extstack.c

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1992-1994 Anders Christensen <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #if defined(__WATCOMC__) && defined(OS2)
21 # include <os2/types.h>
22 #endif
23 #include "rexx.h"
24
25 #if defined(WIN32)
26 # if defined(_MSC_VER)
27 # if _MSC_VER >= 1100
28 /* Stupid MSC can't compile own headers without warning at least in VC 5.0 */
29 # pragma warning(disable: 4115 4201 4214 4514)
30 # endif
31 # include <windows.h>
32 # if _MSC_VER >= 1100
33 # pragma warning(default: 4115 4201 4214)
34 # endif
35 # else
36 # include <windows.h>
37 # include <winsock.h>
38 # endif
39 # include <io.h>
40 #else
41 # ifdef HAVE_TYPES_H
42 # include <types.h>
43 # endif
44 # ifdef HAVE_SYS_SOCKET_H
45 # include <sys/socket.h>
46 # endif
47 # ifdef HAVE_NETINET_IN_H
48 # include <netinet/in.h>
49 # endif
50 # ifdef HAVE_SYS_SELECT_H
51 # include <sys/select.h>
52 # endif
53 # ifdef HAVE_NETDB_H
54 # include <netdb.h>
55 # endif
56 # ifdef HAVE_ARPA_INET_H
57 # include <arpa/inet.h>
58 # endif
59 # define closesocket(x) close(x)
60 #endif
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <assert.h>
66 #include <stdarg.h>
67
68 #include "extstack.h"
69
70 #ifdef EXTERNAL_TO_REGINA
71 # include <assert.h>
72 /* tmpstr_of must be defined before the inclusion of extstack.h since it
73 * redefines tsd_t (grrr) */
74 volatile char *tmpstr_of( dummy_tsd_t *TSD, const streng *input )
75 {
76 /* even exiterror isn't permitted at this point. */
77 assert(0); /* hint while debugging */
78 return((volatile char *) input->value);
79 }
80 #endif
81
82 static int debug = -1 ;
83 #define DEBUGDUMP(x) { \
84 if ( debug == -1 ) \
85 { \
86 debug = ( getenv( "RXDEBUG" ) == NULL ) ? 0 : 1 ; \
87 } \
88 if ( debug ) \
89 { \
90 x ; \
91 } \
92 }
93
94 /* "localhost" maps to "127.0.0.1" on most systems but it may use DNS on
95 * badly configured systems. Use "127.0.0.1" to bypass any errors.
96 */
97 static const char ReginaLocalHost[] = "127.0.0.1";
98
99 void showerror( int err, int suberr, char *tmpl, ...)
100 {
101 va_list argptr;
102 if ( suberr )
103 fprintf( stderr, "Error:%d.%d - ", err, suberr );
104 else
105 fprintf( stderr, "Error:%d - ", err );
106 va_start( argptr, tmpl );
107 vfprintf( stderr, tmpl, argptr );
108 va_end( argptr );
109 fprintf( stderr, "\n" );
110 }
111
112 int init_external_queue( const tsd_t *TSD )
113 {
114 int rc=0;
115 #ifdef WIN32
116 WORD wsver = (WORD)MAKEWORD(1,1);
117 WSADATA wsaData;
118 if ( WSAStartup( wsver, &wsaData ) != 0 )
119 {
120 /* TSD will be NULL when called from rxqueue or rxstack */
121 if ( TSD == NULL )
122 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_NO_WINSOCK, ERR_RXSTACK_NO_WINSOCK_TMPL, WSAGetLastError() );
123 else if ( !TSD->called_from_saa )
124 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_NO_WINSOCK, WSAGetLastError() );
125 rc = 1;
126 }
127 #endif
128 return rc;
129 }
130
131 void term_external_queue( void )
132 {
133 #ifdef WIN32
134 WSACleanup();
135 #endif
136 }
137
138 int get_length_from_header( const tsd_t *TSD, const streng *header )
139 {
140 int length=0,error=1;
141 streng *result=NULL;
142
143 result = MAKESTRENG( RXSTACK_HEADER_SIZE - 1 );
144 if ( result )
145 {
146 result->len = RXSTACK_HEADER_SIZE - 1;
147 memcpy( result->value, header->value+1, RXSTACK_HEADER_SIZE-1 );
148 DEBUGDUMP(printf("Hex value: %.*s\n", PSTRENGLEN(result),PSTRENGVAL(result)););
149 length = REXX_X2D( result, &error );
150 DROPSTRENG( result );
151 }
152 return ( error ) ? 0 : length;
153 }
154
155 #if !defined(NO_EXTERNAL_QUEUES)
156 int default_port_number( void )
157 {
158 int portno;
159 char dummy;
160 char *port = getenv("RXSTACK"); /* FIXME: May be overwritten by VALUE-BIF */
161
162 if ( port != NULL )
163 {
164 if ( sscanf( port, "%d %c", &portno, &dummy ) == 1 )
165 if ( (portno > 1) && (portno < 0xFFFF) )
166 return portno;
167 }
168 return RXSOCKET;
169 }
170
171 int default_external_address( void )
172 {
173 return inet_addr( ReginaLocalHost );
174 }
175
176 streng *default_external_name( const tsd_t *TSD )
177 {
178 int len;
179 streng *result;
180
181 len = sizeof(ReginaLocalHost); /* includes the term. \0 */
182 result = MAKESTRENG( len );
183 result->len = len - 1;
184 memcpy( result->value, ReginaLocalHost, len ) ;
185 return result ;
186 }
187
188 int connect_to_rxstack( tsd_t *TSD, Queue *q )
189 {
190 struct sockaddr_in server;
191 int eno;
192
193 /*
194 * Connect to external rxstack process/daemon/service
195 */
196 assert( q->type == QisExternal ) ;
197 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););
198 memset( &server, 0, sizeof(server) );
199 server.sin_family = AF_INET;
200 server.sin_addr.s_addr = q->u.e.address;
201 server.sin_port = htons((unsigned short) q->u.e.portno);
202
203 q->u.e.socket = socket( AF_INET, SOCK_STREAM, 0 );
204 if ( q->u.e.socket >= 0 )
205 {
206
207 if ( connect( q->u.e.socket, (struct sockaddr *)&server, sizeof(server) ) >= 0 )
208 {
209 DEBUGDUMP(printf("In connect_to_rxstack: socket=%d\n", q->u.e.socket););
210 return(q->u.e.socket);
211 }
212 eno = errno;
213 closesocket(q->u.e.socket);
214 q->u.e.socket = -1;
215 errno = eno;
216 }
217 /* TSD will be NULL when called from rxqueue or rxstack */
218 if ( TSD == NULL )
219 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_CANT_CONNECT, ERR_RXSTACK_CANT_CONNECT_TMPL, q->u.e.name->value, q->u.e.portno, strerror ( errno ) );
220 else if ( !TSD->called_from_saa )
221 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_CANT_CONNECT, tmpstr_of( TSD, q->u.e.name ), q->u.e.portno, strerror ( errno ) );
222
223 return -1;
224 }
225
226 int disconnect_from_rxstack( const tsd_t *TSD, Queue *q )
227 {
228 int rc;
229
230 assert( q->type == QisExternal ) ;
231 if (q->u.e.socket != -1)
232 {
233 DEBUGDUMP(printf("Disconnecting from socket %d\n", q->u.e.socket ););
234 rc = send_command_to_rxstack( TSD, q->u.e.socket, RXSTACK_EXIT_STR, NULL, 0 );
235 closesocket( q->u.e.socket );
236 }
237 else
238 rc = 0; /* success: 0 bytes transfered */
239 if (q->u.e.name != NULL)
240 DROPSTRENG( q->u.e.name );
241 memset( q, 0, sizeof(Queue) ) ;
242 q->type = QisUnused ;
243 return rc;
244 }
245
246 int send_command_to_rxstack( const tsd_t *TSD, int sock, const char *action, const char *str, int len )
247 {
248 streng *qlen, *header;
249 int rc=-1;
250
251 DEBUGDUMP(printf("\n--> Sending to %d Action: %s <%.*s> Len:%d\n", sock, action, len, (str) ? str : "", len););
252 qlen = REXX_D2X( len );
253 if ( qlen )
254 {
255 header = REXX_RIGHT( qlen, RXSTACK_HEADER_SIZE, '0');
256 DROPSTRENG( qlen );
257 if ( header )
258 {
259 header->value[0] = action[0];
260 rc = send( sock, PSTRENGVAL(header), PSTRENGLEN(header), 0 );
261 DEBUGDUMP(printf("Send length: %.*s(%d) rc %d\n", PSTRENGLEN(header),PSTRENGVAL(header),PSTRENGLEN(header),rc););
262 if ( str && rc != -1 )
263 {
264 rc = send( sock, str, len, 0 );
265 DEBUGDUMP(printf("Send str length: %d\n", rc););
266 }
267 DROPSTRENG( header );
268 }
269 }
270 return rc;
271 }
272
273 static int inject_result_from_rxstack( int sock, streng *str, int size )
274 {
275 /*
276 * Reads size bytes from sock and adds them to str. No overflow checking
277 * is performed.
278 * returns the result from recv.
279 */
280 int rc;
281
282 rc = recv( sock, PSTRENGVAL(str) + PSTRENGLEN(str), size, 0 );
283 str->len += size;
284 DEBUGDUMP(printf("<-- Recv result: %.*s(%d) rc %d\n", size, PSTRENGVAL(str) + PSTRENGLEN(str), PSTRENGLEN(str), rc ); );
285 return rc;
286 }
287
288 streng *read_result_from_rxstack( const tsd_t *TSD, int sock, int result_size )
289 {
290 /*
291 * Creates a streng of length 'result_size' and if non-zero,
292 * reads this many characters from the socket.
293 * The caller MUST DROPSTRENG this streng.
294 */
295 streng *result;
296 int rc;
297
298 result = MAKESTRENG( result_size );
299 if ( result )
300 {
301 if ( result_size )
302 {
303 result->len = 0 ; /* MAKESTRENG sets ->len to nonzero sometimes */
304 rc = inject_result_from_rxstack( sock, result, result_size );
305 }
306 }
307 return result;
308 }
309
310 /* parse_queue validates the queue name. The format is [queue][@host[:port]]
311 * *eq is filled and may contain default values.
312 * queue may be empty to designate the current/default queue.
313 * host may be empty to designate the local host. It has to be either a
314 * IPv4 hostname or a IPv4 IP address.
315 * The [@host[:port]] part is chopped off and the pure queue name remains.
316 * return values:
317 * -code: error detected (exiterror() is thrown normally!), code is one
318 * or RXQUEUE_...
319 * 0: queue is NULL
320 * 1: queue is parsed successfully, either with or without host part.
321 */
322 int parse_queue( tsd_t *TSD, streng *queue, Queue *q )
323 {
324 int len, AtPos;
325 char *h, dummy;
326 struct hostent *he;
327
328 q->type = QisExternal ;
329 q->u.e.portno = 0; /* Good practise for initialisation */
330 q->u.e.socket = -1;
331 q->u.e.address = 0;
332 q->u.e.name = NULL;
333
334 if ( queue == NULL )
335 return 0 ;
336
337 len = PSTRENGLEN( queue );
338 if (( h = (char *)memchr( PSTRENGVAL( queue ), '@', len ) ) == NULL )
339 return 1 ;
340
341 AtPos = (int) ( h - PSTRENGVAL( queue ) ) ;
342 h++ ;
343 len -= AtPos + 1 ;
344
345 /* h is at host, len contains its length. We now do a trick. We copy the
346 * complete host part into q->u.e.name, but a '\0' appended. We can simply
347 * check out the portno part and we can apply sscanf() on it. Working with
348 * a temporary buffer for the portno has no benefit, we can save 5 byte
349 * per connection with a maximum of 1 permanent connection ==> 5 byte
350 */
351 q->u.e.name = MAKESTRENG( len + 1 ) ;
352 if (q->u.e.name == NULL )
353 {
354 /* TSD will be NULL when called from rxqueue or rxstack */
355 if ( TSD == NULL )
356 showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL );
357 else if ( !TSD->called_from_saa )
358 exiterror( ERR_STORAGE_EXHAUSTED, 0 );
359 return -4; /* RXQUEUE_NOEMEM */
360 }
361 memcpy( PSTRENGVAL( q->u.e.name ), h, len ) ;
362 PSTRENGVAL( q->u.e.name )[len] = '\0' ;
363 q->u.e.name->len = len ;
364
365 if (( h = (char *)memchr( PSTRENGVAL( q->u.e.name ), ':', len ) ) == NULL )
366 q->u.e.portno = default_port_number();
367 else
368 {
369 q->u.e.name->len = (int) ( h - PSTRENGVAL( q->u.e.name ) ) ;
370 *h++ = '\0' ; /* chop off the portno part and jump over ':' */
371 if ( sscanf( h, "%d %c", &q->u.e.portno, &dummy ) != 1 )
372 q->u.e.portno = 0;
373 if ( ( q->u.e.portno < 1 ) || ( q->u.e.portno > 0xFFFF ) )
374 {
375 if ( TSD == NULL )
376 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INVALID_QUEUE, ERR_RXSTACK_INVALID_QUEUE_TMPL, PSTRENGVAL( queue ) );
377 else if ( !TSD->called_from_saa )
378 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INVALID_QUEUE, tmpstr_of( TSD, queue ) );
379 DROPSTRENG( q->u.e.name ) ;
380 q->u.e.name = NULL ;
381 return -5; /* RXQUEUE_BADQNAME */
382 }
383 }
384
385 h = PSTRENGVAL( q->u.e.name ) ;
386 if ( *h == '\0' )
387 {
388 q->u.e.address = default_external_address();
389 DROPSTRENG( q->u.e.name );
390 q->u.e.name = default_external_name( TSD );
391 }
392 else
393 {
394 /* h is either a dotted name or a host name, always try the dotted one
395 * first. It's MUCH faster.
396 */
397 q->u.e.address = inet_addr( h );
398 if ( ( q->u.e.address == 0 ) || ( q->u.e.address == -1 ) ) /* various errors */
399 {
400 he = gethostbyname( h ) ;
401 if ( ( he != NULL )
402 && ( he->h_addr != NULL )
403 && ( he->h_addrtype == AF_INET ) )
404 q->u.e.address = ( ( struct in_addr * ) he->h_addr )->s_addr;
405 if ( ( q->u.e.address == 0 ) || ( q->u.e.address == -1 ) )
406 {
407 /* TSD will be NULL when called from rxqueue or rxstack */
408 if ( TSD == NULL )
409 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_NO_IP, ERR_RXSTACK_NO_IP_TMPL, PSTRENGVAL( q->u.e.name ) );
410 else if ( !TSD->called_from_saa )
411 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_NO_IP, tmpstr_of( TSD, q->u.e.name ) );
412 DROPSTRENG( q->u.e.name ) ;
413 q->u.e.name = NULL ;
414 return -5; /* RXQUEUE_BADQNAME */
415 }
416 }
417 }
418
419 queue->len = AtPos ; /* chop off the host part */
420 return 1 ;
421 }
422
423 int delete_queue_from_rxstack( const tsd_t *TSD, int sock, const streng *queue_name )
424 {
425 int rc;
426 streng *result;
427
428 rc = send_command_to_rxstack( TSD, sock, RXSTACK_DELETE_QUEUE_STR, PSTRENGVAL( queue_name ), PSTRENGLEN( queue_name ) );
429 if ( rc != -1 )
430 {
431 result = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
432 if ( result )
433 {
434 rc = result->value[0]-'0';
435 DROPSTRENG( result );
436 }
437 }
438 return rc;
439 }
440
441 int timeout_queue_on_rxstack( const tsd_t *TSD, int sock, long timeout )
442 {
443 int rc=0;
444 streng *result,*qtimeout, *hex_timeout;
445
446 qtimeout = REXX_D2X( timeout );
447 if ( qtimeout )
448 {
449 hex_timeout = REXX_RIGHT( qtimeout, RXSTACK_TIMEOUT_SIZE, '0');
450 DROPSTRENG( qtimeout );
451 if ( hex_timeout )
452 {
453 DEBUGDUMP(printf("Send timeout: %.*s(%d) rc %d\n", PSTRENGLEN(hex_timeout),PSTRENGVAL(hex_timeout),PSTRENGLEN(hex_timeout),rc););
454 rc = send_command_to_rxstack( TSD, sock, RXSTACK_TIMEOUT_QUEUE_STR, PSTRENGVAL(hex_timeout), PSTRENGLEN(hex_timeout) );
455 DROPSTRENG( hex_timeout );
456 if ( rc != -1 )
457 {
458 result = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
459 if ( result )
460 {
461 rc = result->value[0]-'0';
462 DROPSTRENG( result );
463 }
464 }
465 }
466 }
467 return rc;
468 }
469
470 int get_number_in_queue_from_rxstack( const tsd_t *TSD, int sock, int *errcode )
471 {
472 int rc,length=0;
473 streng *header;
474
475 rc = send_command_to_rxstack( TSD, sock, RXSTACK_NUMBER_IN_QUEUE_STR, NULL, 0 );
476 if ( rc != -1 )
477 {
478 header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
479 if ( header )
480 {
481 rc = header->value[0]-'0';
482 if ( rc == 0 )
483 {
484 /*
485 * now get the length from the header
486 */
487 DEBUGDUMP(printf("before get_length_from_header: %.*s\n", header->len, header->value););
488 length = get_length_from_header( TSD, header );
489 }
490 else
491 {
492 /* TSD will be NULL when called from rxqueue or rxstack */
493 if ( TSD == NULL )
494 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Getting number in queue" );
495 else if ( !TSD->called_from_saa )
496 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting number in queue" );
497 rc = 9; /* RXQUEUE_NOTREG */
498 }
499 DROPSTRENG( header );
500 }
501 }
502 if ( errcode )
503 *errcode = rc;
504 return length;
505 }
506
507 int get_queues_from_rxstack( const tsd_t *TSD, int sock, int *errcode , streng **result )
508 {
509 int rc,length=0;
510 streng *header;
511
512 DEBUGDUMP(printf("before send_command_to_rxstack:\n"););
513 rc = send_command_to_rxstack( TSD, sock, RXSTACK_SHOW_QUEUES_STR, NULL, 0 );
514 if ( rc != -1 )
515 {
516 header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
517 if ( header )
518 {
519 rc = header->value[0]-'0';
520 if ( rc == 0 )
521 {
522 /*
523 * now get the length from the header
524 */
525 DEBUGDUMP(printf("before get_length_from_header: %.*s\n", header->len, header->value););
526 length = get_length_from_header( TSD, header );
527 *result = read_result_from_rxstack( TSD, sock, length );
528 }
529 else
530 {
531 /* TSD will be NULL when called from rxqueue or rxstack */
532 if ( TSD == NULL )
533 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Getting queues" );
534 else if ( !TSD->called_from_saa )
535 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting queues" );
536 rc = 9; /* RXQUEUE_NOTREG */
537 }
538 DROPSTRENG( header );
539 }
540 }
541 if ( errcode )
542 *errcode = rc;
543 return length;
544 }
545
546 int clear_queue_on_rxstack( const tsd_t *TSD, int sock )
547 {
548 int rc ;
549 streng *result ;
550
551 rc = send_command_to_rxstack( TSD, sock, RXSTACK_EMPTY_QUEUE_STR, NULL, 0 );
552 if ( rc != -1 )
553 {
554 result = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
555 if ( result )
556 {
557 rc = result->value[0]-'0';
558 DROPSTRENG( result );
559 }
560 }
561 return rc ;
562 }
563
564 static streng *init_connect_string( const tsd_t *TSD, const Queue *q, int addlength )
565 {
566 /* Determines the length of the connect string of eq and sums up
567 * addlength. A streng of that size is created and returned.
568 */
569 streng *retval ;
570 int len = 0 ;
571
572 assert( q->type == QisExternal ) ;
573 if ( q->u.e.name != 0 )
574 len = PSTRENGLEN( q->u.e.name ) ;
575 if ( len == 0 )
576 len = 15 ; /* enough for "xxx.xxx.xxx.xxx" */
577 len += 8 ; /* enough for "@" and ":65535" and a terminating 0 */
578
579 retval = MAKESTRENG( len + addlength ) ;
580 if ( retval == NULL )
581 {
582 /* TSD will be NULL when called from rxqueue or rxstack */
583 if ( TSD == NULL )
584 showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL );
585 else if ( !TSD->called_from_saa )
586 exiterror( ERR_STORAGE_EXHAUSTED, 0 );
587 }
588 else
589 retval->len = 0 ; /* MAKESTRENG sets ->len to nonzero sometimes */
590 return retval ;
591 }
592
593 static void add_connect_string( const Queue *q, streng *str )
594 {
595 /* Adds the connect string of eq to str.
596 * This may be "@localhost:5757"
597 * We always add the port. It may be redefined in between by a call to
598 * the VALUE BIF.
599 */
600 int sum = 0, len ;
601 char *ptr = PSTRENGVAL( str ) ;
602 char *addr ;
603 struct in_addr ia ;
604
605 assert( q->type == QisExternal ) ;
606 ptr += PSTRENGLEN( str ) ;
607 *ptr++ = '@' ;
608 sum++ ;
609 if ( q->u.e.name != 0 )
610 {
611 len = PSTRENGLEN( q->u.e.name ) ;
612 memcpy( ptr, PSTRENGVAL( q->u.e.name ), len ) ;
613 ptr += len ;
614 sum += len ;
615 }
616 if ( sum == 1 ) /* no q->u.e.name? use the dotted IP address */
617 {
618 ia.s_addr = q->u.e.address ;
619 addr = inet_ntoa( ia ) ;
620 len = strlen( addr ) ;
621 memcpy( ptr, addr, len ) ;
622 ptr += len ;
623 sum += len ;
624 }
625 /* finally add the port */
626 sum += sprintf( ptr, ":%u", (unsigned) q->u.e.portno ) ;
627 str->len += sum ;
628 }
629
630 int get_queue_from_rxstack( const tsd_t *TSD, const Queue *q, streng **result )
631 {
632 int rc,length;
633 streng *header;
634
635 assert( q->type == QisExternal ) ;
636 rc = send_command_to_rxstack( TSD, q->u.e.socket, RXSTACK_GET_QUEUE_STR, NULL, 0 );
637 if ( rc != -1 )
638 {
639 header = read_result_from_rxstack( TSD, q->u.e.socket, RXSTACK_HEADER_SIZE );
640 if ( header )
641 {
642 rc = header->value[0]-'0';
643 if ( rc == 0 )
644 {
645 /*
646 * now get the length from the header and get that many characters...
647 */
648 length = get_length_from_header( TSD, header );
649 if ( ( *result = init_connect_string( TSD, q, length ) ) != NULL )
650 {
651 inject_result_from_rxstack( q->u.e.socket, *result, length ) ;
652 add_connect_string( q, *result );
653 }
654 else
655 rc = 4; /* RXQUEUE_NOEMEM, not really used */
656 }
657 else
658 {
659 /* TSD will be NULL when called from rxqueue or rxstack */
660 if ( TSD == NULL )
661 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Getting queue from stack" );
662 else if ( !TSD->called_from_saa )
663 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting queue from stack" );
664 }
665 DROPSTRENG( header );
666 }
667 }
668 return rc;
669 }
670
671 int get_line_from_rxstack( const tsd_t *TSD, int sock, streng **result, int nowait )
672 {
673 int rc,length;
674 streng *header;
675
676 rc = send_command_to_rxstack( TSD, sock, (nowait) ? RXSTACK_FETCH_STR : RXSTACK_PULL_STR, NULL, 0 );
677 if ( rc != -1 )
678 {
679 header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
680 if ( header )
681 {
682 rc = header->value[0]-'0';
683 DEBUGDUMP(printf("rc from read_result_from_rxstack=%d\n", rc););
684 switch( rc )
685 {
686 case RXSTACK_OK:
687 /*
688 * now get the length from the header and get that many characters...
689 */
690 length = get_length_from_header( TSD, header );
691 *result = read_result_from_rxstack( TSD, sock, length );
692 break;
693 case RXSTACK_TIMEOUT:
694 case RXSTACK_EMPTY:
695 /*
696 * queue is empty - return a NULL
697 */
698 *result = NULL;
699 break;
700 default:
701 /* TSD will be NULL when called from rxqueue or rxstack */
702 if ( TSD == NULL )
703 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Getting line from queue" );
704 else if ( !TSD->called_from_saa )
705 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Getting line from queue" );
706 break;
707 }
708 DROPSTRENG( header );
709 }
710 }
711 return rc;
712 }
713
714 int create_queue_on_rxstack( const tsd_t *TSD, const Queue *q, const streng *queue, streng **result )
715 {
716 int rc,length;
717 streng *header;
718
719 assert( q->type == QisExternal ) ;
720 rc = send_command_to_rxstack( TSD, q->u.e.socket, RXSTACK_CREATE_QUEUE_STR, (queue) ? PSTRENGVAL( queue ) : NULL, (queue) ? PSTRENGLEN( queue ) : 0 );
721 if ( rc != -1 )
722 {
723 header = read_result_from_rxstack( TSD, q->u.e.socket, RXSTACK_HEADER_SIZE );
724 if ( header )
725 {
726 rc = header->value[0]-'0';
727 if ( ( rc == 0 ) || ( rc == 1 ) )
728 {
729 /*
730 * now get the length from the header and get that many characters...
731 */
732 length = get_length_from_header( TSD, header );
733 if ( ( *result = init_connect_string( TSD, q, length ) ) != NULL )
734 {
735 inject_result_from_rxstack( q->u.e.socket, *result, length ) ;
736 add_connect_string( q, *result );
737 }
738 else
739 rc = 4; /* RXQUEUE_NOEMEM */
740 }
741 else
742 {
743 /* TSD will be NULL when called from rxqueue or rxstack */
744 if ( TSD == NULL )
745 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Creating queue" );
746 else if ( !TSD->called_from_saa )
747 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Creating queue" );
748 switch ( rc )
749 {
750 case 2: rc = 5; break; /* RXQUEUE_BADQNAME */
751 case 3: rc = 4; break; /* RXQUEUE_NOEMEM */
752 case 6: rc = 1; break; /* RXQUEUE_STORAGE */
753 default:;
754 }
755 }
756 DROPSTRENG( header );
757 }
758 }
759 return rc;
760 }
761
762 int set_queue_in_rxstack( const tsd_t *TSD, int sock, const streng *queue_name )
763 {
764 int rc,length;
765 streng *header, *dummy;
766
767 rc = send_command_to_rxstack( TSD, sock, RXSTACK_SET_QUEUE_STR, PSTRENGVAL( queue_name ), PSTRENGLEN( queue_name ) );
768 if ( rc != -1 )
769 {
770 header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
771 if ( header )
772 {
773 rc = header->value[0]-'0';
774 if ( rc == 0 )
775 {
776 /*
777 * now get the length from the header and get that many characters...
778 */
779 length = get_length_from_header( TSD, header );
780 dummy = read_result_from_rxstack( TSD, sock, length );
781 /* dummy is no longer used */
782 DROPSTRENG( dummy ) ;
783 }
784 else
785 {
786 /* TSD will be NULL when called from rxqueue or rxstack */
787 if ( TSD == NULL )
788 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Setting queue" );
789 else if ( !TSD->called_from_saa )
790 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Setting queue" );
791 switch ( rc )
792 {
793 case 3: rc = 4; break; /* RXQUEUE_NOEMEM */
794 case 6: rc = 1; break; /* RXQUEUE_STORAGE */
795 default:;
796 }
797 }
798 DROPSTRENG( header );
799 }
800 }
801 else
802 rc = 100; /* RXQUEUE_NETERROR */
803 return rc;
804 }
805
806 int queue_line_fifo_to_rxstack( const tsd_t *TSD, int sock, const streng *line )
807 {
808 int rc;
809 streng *header;
810
811 rc = send_command_to_rxstack( TSD, sock, RXSTACK_QUEUE_FIFO_STR, PSTRENGVAL( line ), PSTRENGLEN( line ) );
812 if ( rc != -1 )
813 {
814 header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
815 if ( header )
816 {
817 rc = header->value[0]-'0';
818 if ( rc != 0 )
819 {
820 /* TSD will be NULL when called from rxqueue or rxstack */
821 if ( TSD == NULL )
822 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Queueing line" );
823 else if ( !TSD->called_from_saa )
824 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Queueing line" );
825 switch ( rc )
826 {
827 case 2: rc = 5; break; /* RXQUEUE_BADQNAME */
828 case 3: rc = 4; break; /* RXQUEUE_NOEMEM */
829 case 6: rc = 1; break; /* RXQUEUE_STORAGE */
830 default:;
831 }
832 }
833 DROPSTRENG( header );
834 }
835 }
836 return rc;
837 }
838
839 int queue_line_lifo_to_rxstack( const tsd_t *TSD, int sock, const streng *line )
840 {
841 int rc;
842 streng *header;
843
844 rc = send_command_to_rxstack( TSD, sock, RXSTACK_QUEUE_LIFO_STR, PSTRENGVAL( line ), PSTRENGLEN( line ) );
845 if ( rc != -1 )
846 {
847 header = read_result_from_rxstack( TSD, sock, RXSTACK_HEADER_SIZE );
848 if ( header )
849 {
850 rc = header->value[0]-'0';
851 if ( rc != 0 )
852 {
853 /* TSD will be NULL when called from rxqueue or rxstack */
854 if ( TSD == NULL )
855 showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, ERR_RXSTACK_INTERNAL_TMPL, rc, "Queueing line" );
856 else if ( !TSD->called_from_saa )
857 exiterror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, rc, "Queueing line" );
858 switch ( rc )
859 {
860 case 2: rc = 5; break; /* RXQUEUE_BADQNAME */
861 case 3: rc = 4; break; /* RXQUEUE_NOEMEM */
862 case 6: rc = 1; break; /* RXQUEUE_STORAGE */
863 default:;
864 }
865 }
866 DROPSTRENG( header );
867 }
868 }
869 return rc;
870 }
871 #endif