| 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 |
* This process runs as a daemon (or NT service). It maintains multiple, |
| 21 |
* named Rexx queues. |
| 22 |
* All communication is done via TCP/IP sockets. |
| 23 |
* This process waits on a known port; 5656 by default for connections |
| 24 |
* from clients. A client is any process that respects the Interface |
| 25 |
* defined below. The "normal" clients are regina and rxqueue. |
| 26 |
* Details about each client is kept, like current queue name. |
| 27 |
* |
| 28 |
* Structure |
| 29 |
* --------- |
| 30 |
* startup |
| 31 |
* - set signal handler for SIGTERM |
| 32 |
* initialise socket interface |
| 33 |
* - socket() |
| 34 |
* - bind() |
| 35 |
* - listen() |
| 36 |
* loop until killed |
| 37 |
* - setup read FDs |
| 38 |
* - select() |
| 39 |
* - if listen socket, add new client |
| 40 |
* - otherwise read command |
| 41 |
* cleanup |
| 42 |
* - disconnect all clients |
| 43 |
* - free up resources |
| 44 |
* |
| 45 |
* Interface. |
| 46 |
* --------- |
| 47 |
* Once a client connects, it sends commands |
| 48 |
* Commands are single character, followed by optional 6 hex character |
| 49 |
* length and optional data. |
| 50 |
* F - queue data onto client's current queue (FIFO) |
| 51 |
* in -> FFFFFFFxxx--data--xxx |
| 52 |
* out-> 0000000 (if successful) |
| 53 |
* out-> 2xxxxxx (if error, eg queue deleted) |
| 54 |
* out-> 3000000 (memory allocation error) |
| 55 |
* regina QUEUE, rxqueue /fifo, websocket /fifo |
| 56 |
* L - push data onto client's current queue (LIFO) |
| 57 |
* in-> LFFFFFFxxx--data--xxx |
| 58 |
* out-> 0000000 (if successful) |
| 59 |
* out-> 2xxxxxx (if error, eg queue deleted) |
| 60 |
* out-> 3000000 (memory allocation error) |
| 61 |
* regina PUSH, rxqueue /lifo, websocket /lifo |
| 62 |
* C - create queue |
| 63 |
* in-> CFFFFFFxxx--queue name--xxx (if length 0, create name) |
| 64 |
* out-> 0FFFFFFxxx--queue name--xxx (if queue name created) |
| 65 |
* out-> 1FFFFFFxxx--queue name--xxx (if queue name existed) |
| 66 |
* out-> 2xxxxxx (if error) |
| 67 |
* out-> 6000000 (queue name not passed) |
| 68 |
* regina RXQUEUE('C'), rxqueue N/A, websocket /create |
| 69 |
* D - delete queue |
| 70 |
* in-> DFFFFFFxxx--queue name--xxx |
| 71 |
* out-> 0000000 (if queue name deleted) |
| 72 |
* out-> 5xxxxxx (trying to delete 'SESSION' queue) |
| 73 |
* out-> 6000000 (queue name not passed) |
| 74 |
* out-> 9xxxxxx (if error, eg queue already deleted) |
| 75 |
* regina RXQUEUE('D'), rxqueue N/A, websocket /delete |
| 76 |
* E - empty data from specified queue |
| 77 |
* in-> EFFFFFFxxx--queue name--xxx |
| 78 |
* out-> 0000000 (if queue emptied) |
| 79 |
* out-> 2xxxxxx (if error, eg queue deleted) |
| 80 |
* out-> 3000000 (memory allocation error) |
| 81 |
* regina N/A, rxqueue /clear, websocket /clear |
| 82 |
* P - pop item off client's default queue |
| 83 |
* in-> P000000 |
| 84 |
* out-> 0FFFFFFxxx--data--xxx (if queue name existed) |
| 85 |
* out-> 1000000 (if queue empty) |
| 86 |
* out-> 2xxxxxx (if queue name deleted - length ignored) |
| 87 |
* out-> 4xxxxxx (if timeout on queue exceeded - length ignored) |
| 88 |
* regina PULL, rxqueue /pull, websocket /pull |
| 89 |
* p - fetch item off client's default queue |
| 90 |
* in-> p000000 |
| 91 |
* out-> 0FFFFFFxxx--data--xxx (if queue name existed) |
| 92 |
* out-> 1000000 (if queue empty) |
| 93 |
* out-> 2xxxxxx (if queue name deleted - length ignored) |
| 94 |
* regina PULL without timeout, rxqueue N/A, websocket N/A |
| 95 |
* S - set default queue name (allow false queues) |
| 96 |
* in-> SFFFFFFxxx--queue name--xxx |
| 97 |
* out-> 0000000 (if successful) |
| 98 |
* out-> 3000000 (memory allocation error) |
| 99 |
* out-> 6000000 (queue name not passed) |
| 100 |
* regina RXQUEUE('S'), rxqueue N/A, websocket N/A |
| 101 |
* G - get default queue name |
| 102 |
* in-> G000000 |
| 103 |
* out-> 0FFFFFFxxx--queue name--xxx |
| 104 |
* regina RXQUEUE('G'), rxqueue N/A, websocket N/A |
| 105 |
* N - return number of lines on stack |
| 106 |
* in-> N000000 |
| 107 |
* out-> 0FFFFFF (if queue exists) |
| 108 |
* out-> 2xxxxxx (if error or queue doesn't exist - length ignored) |
| 109 |
* regina QUEUED(), rxqueue /queued, websocket /queued |
| 110 |
* Q - return names of stacks |
| 111 |
* in-> Q000000 |
| 112 |
* out-> 0FFFFFFxxx--queue name--xxxDDxxx--queue name---xxxDD... |
| 113 |
* where FFFFFF is length of all names |
| 114 |
* and DD is xFF delimiting names of queues and ... is more queues |
| 115 |
* out-> 2xxxxxx (if error) |
| 116 |
* regina RXQUEUE(), rxqueue /queues, websocket /queues |
| 117 |
* T - set timeout on queue pull |
| 118 |
* in-> TFFFFFFTTTTTT |
| 119 |
* out-> 0000000 (if queue timeout set) |
| 120 |
* out-> 2xxxxxx (if error, eg invalid argument) |
| 121 |
* out-> 6000000 (queue name not passed) |
| 122 |
* regina RXQUEUE('T'), rxqueue N/A, websocket N/A |
| 123 |
* X - client disconnect |
| 124 |
* in-> X000000 |
| 125 |
* out-> |
| 126 |
* Z - client requests shutdown - should only be called by ourselves!! |
| 127 |
* in-> Z000000 |
| 128 |
* out-> |
| 129 |
*/ |
| 130 |
|
| 131 |
#define NO_CTYPE_REPLACEMENT |
| 132 |
#include "rexx.h" |
| 133 |
#if defined(WIN32) || defined(__LCC__) |
| 134 |
# if defined(_MSC_VER) |
| 135 |
# if _MSC_VER >= 1100 |
| 136 |
/* Stupid MSC can't compile own headers without warning at least in VC 5.0 */ |
| 137 |
# pragma warning(disable: 4115 4201 4214 4514) |
| 138 |
# endif |
| 139 |
# include <windows.h> |
| 140 |
# if _MSC_VER >= 1100 |
| 141 |
# pragma warning(default: 4115 4201 4214) |
| 142 |
# endif |
| 143 |
# elif defined(__LCC__) |
| 144 |
# include <windows.h> |
| 145 |
# include <winsvc.h> |
| 146 |
# include <winsock.h> |
| 147 |
# else |
| 148 |
# include <windows.h> |
| 149 |
# endif |
| 150 |
# include <io.h> |
| 151 |
#else |
| 152 |
# ifdef HAVE_SYS_SOCKET_H |
| 153 |
# include <sys/socket.h> |
| 154 |
# endif |
| 155 |
# ifdef HAVE_NETINET_IN_H |
| 156 |
# include <netinet/in.h> |
| 157 |
# endif |
| 158 |
# if defined(HAVE_POLL_H) && defined(HAVE_POLL) |
| 159 |
# include <poll.h> |
| 160 |
# elif defined(HAVE_SYS_POLL_H) && defined(HAVE_POLL) |
| 161 |
# include <sys/poll.h> |
| 162 |
# elif defined(HAVE_SYS_SELECT_H) |
| 163 |
# include <sys/select.h> |
| 164 |
# endif |
| 165 |
# ifdef HAVE_NETDB_H |
| 166 |
# include <netdb.h> |
| 167 |
# endif |
| 168 |
# ifdef HAVE_ARPA_INET_H |
| 169 |
# include <arpa/inet.h> |
| 170 |
# endif |
| 171 |
# define closesocket(x) close(x) |
| 172 |
#endif |
| 173 |
#include <string.h> |
| 174 |
|
| 175 |
#ifdef HAVE_UNISTD_H |
| 176 |
#include <unistd.h> |
| 177 |
#endif |
| 178 |
|
| 179 |
#ifdef HAVE_ERRNO_H |
| 180 |
#include <errno.h> |
| 181 |
#endif |
| 182 |
|
| 183 |
#ifdef HAVE_SIGNAL_H |
| 184 |
#include <signal.h> |
| 185 |
#endif |
| 186 |
|
| 187 |
#ifdef HAVE_CTYPE_H |
| 188 |
#include <ctype.h> |
| 189 |
#endif |
| 190 |
|
| 191 |
#ifdef HAVE_PROCESS_H |
| 192 |
# include <process.h> |
| 193 |
#endif |
| 194 |
|
| 195 |
#if defined(TIME_WITH_SYS_TIME) |
| 196 |
# include <sys/time.h> |
| 197 |
# include <time.h> |
| 198 |
#else |
| 199 |
# if defined(HAVE_SYS_TIME_H) |
| 200 |
# include <sys/time.h> |
| 201 |
# else |
| 202 |
# include <time.h> |
| 203 |
# endif |
| 204 |
#endif |
| 205 |
|
| 206 |
#include <assert.h> |
| 207 |
|
| 208 |
#define HAVE_FORK |
| 209 |
#if defined(__WATCOMC__) || defined(_MSC_VER) || (defined(__IBMC__) && defined(WIN32)) || defined(__SASC) || defined(__MINGW32__) || defined(__BORLANDC__) || defined(DOS) || defined(__LCC__) |
| 210 |
# undef HAVE_FORK |
| 211 |
#endif |
| 212 |
#if defined(__WATCOMC__) && defined(__QNX__) |
| 213 |
# define HAVE_FORK |
| 214 |
#endif |
| 215 |
|
| 216 |
#include "extstack.h" |
| 217 |
#ifndef HAVE_GETOPT_LONG |
| 218 |
# include "mygetopt.h" |
| 219 |
#endif |
| 220 |
#include "contrib/LibSha1.h" |
| 221 |
|
| 222 |
#ifdef BUILD_NT_SERVICE |
| 223 |
# include "service.h" |
| 224 |
/* |
| 225 |
* this event is signalled when the |
| 226 |
* service should end |
| 227 |
*/ |
| 228 |
HANDLE hServerStopEvent = NULL; |
| 229 |
#endif |
| 230 |
|
| 231 |
#ifdef WIN32 |
| 232 |
# define os_errno ((int)WSAGetLastError()) |
| 233 |
# define errno_str(code) Win32ErrorString(code) |
| 234 |
# undef EINTR |
| 235 |
# define EINTR WSAEINTR |
| 236 |
# undef ECONNRESET |
| 237 |
# define ECONNRESET WSAECONNRESET |
| 238 |
#else |
| 239 |
# define os_errno errno |
| 240 |
# define errno_str(code) strerror(code) |
| 241 |
#endif |
| 242 |
|
| 243 |
#define WS_RESPONSE_HEADER "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %.*s\r\n\r\n" |
| 244 |
#define WS_RESPONSE_MAGIC "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" |
| 245 |
/* |
| 246 |
* Translation Table for Base64 encoding as described in RFC1113 |
| 247 |
*/ |
| 248 |
static const char cb64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 249 |
#ifdef REQUIRE_BASE64_DECODE |
| 250 |
static const char cd64[]="|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"; |
| 251 |
#endif |
| 252 |
/* |
| 253 |
* debugging is turned off. You can turn it on by the command line option "-D". |
| 254 |
*/ |
| 255 |
static int debug = 0 ; |
| 256 |
#define DEBUGDUMP(x) { if ( debug ) \ |
| 257 |
{x;} \ |
| 258 |
} |
| 259 |
/* |
| 260 |
* Length of action string from websocket command |
| 261 |
*/ |
| 262 |
#define ACTION_FIFO_LEN 6 |
| 263 |
#define ACTION_LIFO_LEN 6 |
| 264 |
#define ACTION_QUEUED_LEN 8 |
| 265 |
#define ACTION_CLEAR_LEN 7 |
| 266 |
#define ACTION_DELETE_LEN 8 |
| 267 |
#define ACTION_CREATE_LEN 8 |
| 268 |
#define ACTION_PULL_LEN 6 |
| 269 |
|
| 270 |
/* |
| 271 |
* DEFAULT_WAKEUP is the time in ms after which the process shall wakeup. |
| 272 |
* The time has a maximum of 49 days and shall be around one day. It may |
| 273 |
* be set much shorter for debugging purpose. |
| 274 |
*/ |
| 275 |
#define DEFAULT_WAKEUP 86400000 |
| 276 |
|
| 277 |
/* |
| 278 |
* QUEUE_TIMEOUT is the time in ms after which an unused queue will be |
| 279 |
* removed. A queue is unused if no client is connected to it. |
| 280 |
* be set much shorter for debugging purpose. |
| 281 |
* The time has a maximum of 49 days and may be set to one week. It may |
| 282 |
* be set much shorter for debugging purpose. |
| 283 |
*/ |
| 284 |
#define QUEUE_TIMEOUT (86400000*7) |
| 285 |
|
| 286 |
/* |
| 287 |
* RxTime defines a structure holding the time in milliseconds resolution. |
| 288 |
* I know, most systems have at least one sort of high precision |
| 289 |
* time structure, but the ugly "#ifdef" are unreadable if we use |
| 290 |
* them all over here. |
| 291 |
* Defining a structure on our own is much more helpful. |
| 292 |
* Of course, we can't use a single 32 bit value for milliseconds. This |
| 293 |
* will break the server after 49 days. unix will live a little bit longer |
| 294 |
* and there shall Windows machines exist which doesn't have reboot since |
| 295 |
* longer periods ;-) |
| 296 |
*/ |
| 297 |
typedef struct { |
| 298 |
/* seconds are typical time_t values. */ |
| 299 |
time_t seconds ; |
| 300 |
|
| 301 |
/* |
| 302 |
* milli's values are between 0 and 999. The special value -1 indicates |
| 303 |
* a not-used condition. |
| 304 |
*/ |
| 305 |
int milli ; |
| 306 |
} RxTime ; |
| 307 |
|
| 308 |
/* |
| 309 |
* now holds the current time. It isn't updated after every operation and |
| 310 |
* may be out of date by some milliseconds some times. |
| 311 |
*/ |
| 312 |
RxTime now ; |
| 313 |
|
| 314 |
/* |
| 315 |
* This value is now plus 7 days. |
| 316 |
*/ |
| 317 |
RxTime queue_deadline ; |
| 318 |
|
| 319 |
struct _Client ; |
| 320 |
typedef struct _RxQueue { |
| 321 |
/* |
| 322 |
* linked list maintainance elements |
| 323 |
*/ |
| 324 |
struct _RxQueue *prev, *next ; |
| 325 |
/* name is the uppercased name of the queue. |
| 326 |
*/ |
| 327 |
streng *name ; |
| 328 |
/* |
| 329 |
* Indicates if the queue is a "real" queue |
| 330 |
* or a false queue as a result of a rxqueue('set', 'qname') |
| 331 |
* on a queue that doesn't exist. This is crap behaviour! |
| 332 |
* but that's how Object Rexx works :-( |
| 333 |
*/ |
| 334 |
int isReal ; |
| 335 |
/* |
| 336 |
* Content: single buffered stack in opposite to the multi buffered |
| 337 |
* internal stacks of Regina. |
| 338 |
*/ |
| 339 |
Buffer buf ; |
| 340 |
/* |
| 341 |
* deadline is the time the queue was last used plus a timeout. |
| 342 |
* The queue is removed if the queue isn't used for one week. |
| 343 |
* Thus, the value is the time the queue was used last plus one week. |
| 344 |
*/ |
| 345 |
RxTime deadline ; |
| 346 |
/* |
| 347 |
* Several clients may want to wait for incoming data. They are queued |
| 348 |
* in the following structure and automatically reponsed by the |
| 349 |
* data acceptor of the queue in a FIFO manner, which is a fair-queue |
| 350 |
* algorithm. |
| 351 |
* The clients will get a notice of am error if the queue is destroyed |
| 352 |
* or emptied by an explicite call. |
| 353 |
* structure (n = newer, o = older) |
| 354 |
* oldest newest |
| 355 |
* || || |
| 356 |
* NULL<-o--client<-o--client<-o--client--n->NULL |
| 357 |
* | ^ | ^ |
| 358 |
* | | | | |
| 359 |
* +--n--+ +--n--+ |
| 360 |
*/ |
| 361 |
struct _Client *oldest, *newest; |
| 362 |
} RxQueue; |
| 363 |
|
| 364 |
/* |
| 365 |
* queues we work on. |
| 366 |
* Format, p=prev, n=next: |
| 367 |
* queues |
| 368 |
* || |
| 369 |
* NULL<--p-queue-n---->queue-n---->queue-n-->NULL |
| 370 |
* ^ v ^ v |
| 371 |
* | | | | |
| 372 |
* +---p---+ +---p---+ |
| 373 |
*/ |
| 374 |
RxQueue *queues ; |
| 375 |
|
| 376 |
/* SESSION is the special queue which can't be deleted and to which clients |
| 377 |
* drop when their current queue is deleted. |
| 378 |
*/ |
| 379 |
RxQueue *SESSION ; |
| 380 |
|
| 381 |
/* |
| 382 |
* Structure for multiple clients |
| 383 |
*/ |
| 384 |
typedef struct _Client |
| 385 |
{ |
| 386 |
/* |
| 387 |
* linked list maintainance elements |
| 388 |
*/ |
| 389 |
struct _Client *prev, *next ; |
| 390 |
|
| 391 |
/* |
| 392 |
* socket contains the socket's handle |
| 393 |
*/ |
| 394 |
int socket; |
| 395 |
|
| 396 |
/* |
| 397 |
* Indicates if this client is a Websocket client, 0 no, 1 yes |
| 398 |
*/ |
| 399 |
int isWebsocket; |
| 400 |
|
| 401 |
/* |
| 402 |
* each client has a default queue associated. It must be valid all |
| 403 |
* the times after initialization. |
| 404 |
*/ |
| 405 |
RxQueue *default_queue; |
| 406 |
|
| 407 |
/* |
| 408 |
* if queue_timeout is set, the client expects an error code after |
| 409 |
* this time instead of waiting until world's end. |
| 410 |
* The value is in milliseconds. |
| 411 |
* These values have internal meaning; in the call to rxqueue('T',0) the zero is converted to -1 |
| 412 |
* A value of zero means no timeout; return immediately if no data |
| 413 |
* A value of -1 means wait forever |
| 414 |
*/ |
| 415 |
long queue_timeout; |
| 416 |
|
| 417 |
/* |
| 418 |
* We manage a deadline. A PULL operation is in an error state after |
| 419 |
* this timestamp. The value is set at a PULL operation to |
| 420 |
* now+queue_timeout. |
| 421 |
*/ |
| 422 |
RxTime deadline ; |
| 423 |
|
| 424 |
/* |
| 425 |
* linked list maintainance elements for waiters. |
| 426 |
*/ |
| 427 |
struct _Client *older, *newer ; |
| 428 |
} Client; |
| 429 |
|
| 430 |
/* |
| 431 |
* clients we work on. |
| 432 |
* Format, p=prev, n=next: |
| 433 |
* clients |
| 434 |
* || |
| 435 |
* NULL<--p-client-n--->client-n--->client-n-->NULL |
| 436 |
* ^ v ^ v |
| 437 |
* | | | | |
| 438 |
* +---p---+ +---p---+ |
| 439 |
*/ |
| 440 |
Client *clients; |
| 441 |
|
| 442 |
int running = 1; |
| 443 |
int allclean = 0; |
| 444 |
time_t base_secs; /* the time the process started */ |
| 445 |
|
| 446 |
static long portno = 0; |
| 447 |
static int world = 0 ; |
| 448 |
static int isdaemon = 0; |
| 449 |
static int tosuicide = 0; |
| 450 |
|
| 451 |
void empty_queue( RxQueue *q ) ; |
| 452 |
|
| 453 |
#define MAX_LEN 128 |
| 454 |
|
| 455 |
struct param |
| 456 |
{ |
| 457 |
char name[MAX_LEN]; |
| 458 |
char value[MAX_LEN]; |
| 459 |
}; |
| 460 |
|
| 461 |
static void strtrim(char *s) |
| 462 |
{ |
| 463 |
char *p = s + strlen(s); |
| 464 |
|
| 465 |
while (--p >= s && isspace(*p)) |
| 466 |
*p = '\0'; |
| 467 |
} |
| 468 |
|
| 469 |
static void read_params(FILE *in) |
| 470 |
{ |
| 471 |
long pport; |
| 472 |
char s[MAX_LEN]; |
| 473 |
struct param p, *pp=&p; |
| 474 |
|
| 475 |
|
| 476 |
while ( fgets(s, MAX_LEN, in) ) |
| 477 |
{ |
| 478 |
if (sscanf(s, " %[#\n\r]", pp->name)) /* emty line or comment */ |
| 479 |
continue; |
| 480 |
if (sscanf(s, " %[a-z_A-Z0-9] = %[^#\n\r]", pp->name, pp->value) < 2) |
| 481 |
{ |
| 482 |
fprintf(stderr, "error at line: %s\n", s); |
| 483 |
return; |
| 484 |
} |
| 485 |
strtrim(pp->value); |
| 486 |
if ( strcmp( pp->name, "port" ) == 0 ) |
| 487 |
{ |
| 488 |
pport = atol( pp->value ); |
| 489 |
if ( pport == 0 |
| 490 |
|| pport > 0xFFFF ) |
| 491 |
{ |
| 492 |
} |
| 493 |
else |
| 494 |
{ |
| 495 |
portno = pport; |
| 496 |
DEBUGDUMP(printf("port from config: %ld\n",portno);) |
| 497 |
} |
| 498 |
} |
| 499 |
else if ( strcmp( pp->name, "world" ) == 0 ) |
| 500 |
{ |
| 501 |
DEBUGDUMP(printf("world from config: %s\n",pp->value);) |
| 502 |
if ( strcasecmp( pp->value, "yes" ) == 0 ) world = 1; |
| 503 |
if ( strcasecmp( pp->value, "no" ) == 0 ) world = 0; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
return; |
| 508 |
} |
| 509 |
|
| 510 |
#if !defined(HAVE_STRERROR) |
| 511 |
/* |
| 512 |
* Sigh! This must probably be done this way, although it's incredibly |
| 513 |
* backwards. Some versions of gcc comes with a complete set of ANSI C |
| 514 |
* include files, which contains the definition of strerror(). However, |
| 515 |
* that function does not exist in the default libraries of SunOS. |
| 516 |
* To circumvent that problem, strerror() is #define'd to get_sys_errlist() |
| 517 |
* in config.h, and here follows the definition of that function. |
| 518 |
* Originally, strerror() was #defined to sys_errlist[x], but that does |
| 519 |
* not work if string.h contains a declaration of the (non-existing) |
| 520 |
* function strerror(). |
| 521 |
* |
| 522 |
* So, this is a mismatch between the include files and the library, and |
| 523 |
* it should not create problems for Regina. However, the _user_ will not |
| 524 |
* encounter any problems until he compiles Regina, so we'll have to |
| 525 |
* clean up after a buggy installation of the C compiler! |
| 526 |
*/ |
| 527 |
const char *get_sys_errlist( int num ) |
| 528 |
{ |
| 529 |
extern char *sys_errlist[] ; |
| 530 |
return sys_errlist[num] ; |
| 531 |
} |
| 532 |
#endif |
| 533 |
|
| 534 |
#ifdef WIN32 |
| 535 |
const volatile char *Win32ErrorString(int code) |
| 536 |
{ |
| 537 |
static char buffer[512]; |
| 538 |
size_t len; |
| 539 |
const CHAR *array[10]; |
| 540 |
static HINSTANCE tcpip = NULL; |
| 541 |
DWORD rc; |
| 542 |
|
| 543 |
for (rc = 0;rc < sizeof(array) / sizeof(array[0]);rc++) |
| 544 |
array[rc] = "?"; |
| 545 |
|
| 546 |
sprintf(buffer,"code %d: ",code); |
| 547 |
len = strlen(buffer); |
| 548 |
|
| 549 |
if (tcpip == NULL) |
| 550 |
tcpip = GetModuleHandle("wsock32"); |
| 551 |
rc = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | |
| 552 |
FORMAT_MESSAGE_FROM_HMODULE | |
| 553 |
FORMAT_MESSAGE_ARGUMENT_ARRAY | |
| 554 |
FORMAT_MESSAGE_MAX_WIDTH_MASK, |
| 555 |
tcpip, /* lpSource */ |
| 556 |
code, |
| 557 |
GetUserDefaultLangID(), |
| 558 |
buffer + len, |
| 559 |
sizeof(buffer) - len - 1, /* w/o term. 0 */ |
| 560 |
(va_list *) &array); |
| 561 |
if ((rc == 0) && (len >= 2)) |
| 562 |
buffer[len - 2] = '\0'; /* cut off ": " at the end*/ |
| 563 |
return(buffer); |
| 564 |
} |
| 565 |
#endif |
| 566 |
|
| 567 |
/* |
| 568 |
* get_now returns the current time. |
| 569 |
*/ |
| 570 |
RxTime get_now( void ) |
| 571 |
{ |
| 572 |
RxTime retval ; |
| 573 |
#if defined(HAVE_GETTIMEOFDAY) |
| 574 |
struct timeval times ; |
| 575 |
|
| 576 |
gettimeofday(×, NULL) ; |
| 577 |
retval.seconds = times.tv_sec ; |
| 578 |
retval.milli = times.tv_usec / 1000 ; |
| 579 |
|
| 580 |
#elif defined(MAXLONGLONG) |
| 581 |
static enum { T_first, T_OK, T_illegal } state = T_first ; |
| 582 |
static LARGE_INTEGER freq; |
| 583 |
LARGE_INTEGER curr; |
| 584 |
|
| 585 |
retval.seconds = 0; /* Keep compiler happy */ |
| 586 |
if ( state == T_first ) |
| 587 |
{ |
| 588 |
if ( !QueryPerformanceFrequency( &freq ) ) |
| 589 |
state = T_illegal ; |
| 590 |
else |
| 591 |
state = T_OK ; |
| 592 |
} |
| 593 |
if ( state == T_OK ) |
| 594 |
{ |
| 595 |
if ( !QueryPerformanceCounter( &curr ) ) |
| 596 |
state = T_illegal ; |
| 597 |
else |
| 598 |
{ |
| 599 |
ULONGLONG h ; |
| 600 |
/* |
| 601 |
* if we don't native support for the 64 bit arithmetic, use |
| 602 |
* doubles. Everything else is a pain. |
| 603 |
* I hope we never get a compiler for Windows which can't compile |
| 604 |
* this directly. |
| 605 |
*/ |
| 606 |
retval.seconds = (time_t) ( curr.QuadPart / freq.QuadPart ) ; |
| 607 |
h = curr.QuadPart % freq.QuadPart ; |
| 608 |
h *= 1000 ; |
| 609 |
retval.milli = (int) ( h / freq.QuadPart ) ; |
| 610 |
} |
| 611 |
} |
| 612 |
if ( state == T_illegal ) |
| 613 |
{ |
| 614 |
/* Windows systems have ftime in the C library */ |
| 615 |
struct timeb timebuffer; |
| 616 |
|
| 617 |
ftime(&timebuffer); |
| 618 |
retval.seconds = timebuffer.time ; |
| 619 |
retval.milli = timebuffer.millitm ; |
| 620 |
} |
| 621 |
|
| 622 |
#elif defined(HAVE_FTIME) |
| 623 |
struct timeb timebuffer; |
| 624 |
|
| 625 |
ftime(&timebuffer); |
| 626 |
retval.seconds = timebuffer.time ; |
| 627 |
retval.milli = timebuffer.millitm ; |
| 628 |
|
| 629 |
#else |
| 630 |
clock_t c ; |
| 631 |
|
| 632 |
if ( ( c = clock() ) == (clock_t) -1 ) |
| 633 |
{ |
| 634 |
/* |
| 635 |
* clock() values are not adjusted to 1.1.1970 and CLOCKS_PER_SEC |
| 636 |
* may be a float or double |
| 637 |
*/ |
| 638 |
retval.seconds = (time_t) (c / CLOCKS_PER_SEC) ; |
| 639 |
retval.milli = (int) ( ( c * 1000.0 ) / CLOCKS_PER_SEC) % 1000 ; |
| 640 |
} |
| 641 |
else |
| 642 |
{ |
| 643 |
retval.seconds = time( NULL ) ; |
| 644 |
retval.milli = 0 ; |
| 645 |
} |
| 646 |
#endif |
| 647 |
return retval ; |
| 648 |
} |
| 649 |
|
| 650 |
/* |
| 651 |
* time_add increments an amount of milliseconds to time. the amount is |
| 652 |
* usually a Client->queue_timeout. |
| 653 |
*/ |
| 654 |
static void time_add( RxTime *t, long incr ) |
| 655 |
{ |
| 656 |
time_t s = (time_t) ( incr / 1000 ) ; |
| 657 |
int m = ( incr % 1000 ) ; |
| 658 |
|
| 659 |
assert( t->milli != -1 ) ; |
| 660 |
/* wrapping may occur, be careful */ |
| 661 |
s += t->seconds ; |
| 662 |
if ( ( t->milli += m ) >= 1000 ) |
| 663 |
{ |
| 664 |
s++ ; |
| 665 |
t->milli -= 1000 ; |
| 666 |
} |
| 667 |
if ( t->seconds > s ) |
| 668 |
{ |
| 669 |
/* wrapping! */ |
| 670 |
s = (time_t) -1 ; /* maximum 1 */ |
| 671 |
if ( t->seconds > s ) |
| 672 |
{ |
| 673 |
/* time_t is a signed value, most representations have |
| 674 |
* MinX = -MaxX - 1 |
| 675 |
*/ |
| 676 |
s++; |
| 677 |
s = -s; |
| 678 |
} |
| 679 |
assert( t->seconds <= s ) ; |
| 680 |
} |
| 681 |
t->seconds = s ; |
| 682 |
} |
| 683 |
|
| 684 |
/* |
| 685 |
* time_diff returns t1 - t2 in milliseconds. -1 is returned on overflow; |
| 686 |
* -1 is never returned else. -2 is used is the returned time will be |
| 687 |
* negative. |
| 688 |
*/ |
| 689 |
static int time_diff( RxTime t1, RxTime t2 ) |
| 690 |
{ |
| 691 |
int retval, h ; |
| 692 |
|
| 693 |
assert( t1.milli != -1 ) ; |
| 694 |
assert( t2.milli != -1 ) ; |
| 695 |
if ( t1.seconds < t2.seconds ) |
| 696 |
retval = -2 ; |
| 697 |
else |
| 698 |
{ |
| 699 |
retval = (int) ( ( t1.seconds - t2.seconds ) * 1000 ) ; |
| 700 |
|
| 701 |
if ( ( t1.milli < t2.milli ) && ( retval == 0 ) ) |
| 702 |
retval = -2 ; |
| 703 |
else |
| 704 |
{ |
| 705 |
retval += t1.milli - t2.milli ; |
| 706 |
|
| 707 |
/* final check for overflow */ |
| 708 |
h = (int) ( t1.seconds - t2.seconds ) ; |
| 709 |
if ( ( ( retval / 1000 ) < h - 1 ) |
| 710 |
|| ( ( retval / 1000 ) > h + 1 ) |
| 711 |
|| ( retval < 0 ) ) |
| 712 |
return -1 ; |
| 713 |
} |
| 714 |
} |
| 715 |
return retval ; |
| 716 |
} |
| 717 |
|
| 718 |
/* compares 2 strengs and returns 0 if they are equal, 1 if not. |
| 719 |
* The second one is converted to uppercase while comparing, the first |
| 720 |
* one must be uppercase. |
| 721 |
*/ |
| 722 |
int Str_ccmp( const streng *first, const streng *second ) |
| 723 |
{ |
| 724 |
int tmp ; |
| 725 |
|
| 726 |
if ( PSTRENGLEN( first ) != PSTRENGLEN( second ) ) |
| 727 |
return 1 ; |
| 728 |
|
| 729 |
for (tmp=0; tmp < PSTRENGLEN( first ); tmp++ ) |
| 730 |
if ( first->value[tmp] != toupper( second->value[tmp] ) ) |
| 731 |
return 1 ; |
| 732 |
|
| 733 |
return 0 ; |
| 734 |
} |
| 735 |
|
| 736 |
/* Str_cre_or_exit create a streng or exits after a message about |
| 737 |
* missing memory. |
| 738 |
*/ |
| 739 |
streng *Str_cre_or_exit( const char *str, unsigned length ) |
| 740 |
{ |
| 741 |
streng *retval ; |
| 742 |
|
| 743 |
if ( ( retval = MAKESTRENG( length ) ) == NULL ) |
| 744 |
{ |
| 745 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ); |
| 746 |
exit( ERR_STORAGE_EXHAUSTED ); |
| 747 |
} |
| 748 |
|
| 749 |
memcpy( PSTRENGVAL( retval ), str, length ) ; |
| 750 |
retval->len = length ; |
| 751 |
return retval ; |
| 752 |
} |
| 753 |
|
| 754 |
/* |
| 755 |
* Str_buf create a streng from a buffer. It may return NULL on error. |
| 756 |
*/ |
| 757 |
streng *Str_buf( const char *str, unsigned length ) |
| 758 |
{ |
| 759 |
streng *retval ; |
| 760 |
|
| 761 |
if ( ( retval = MAKESTRENG( length ) ) == NULL ) |
| 762 |
{ |
| 763 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ); |
| 764 |
return NULL; |
| 765 |
} |
| 766 |
|
| 767 |
memcpy( PSTRENGVAL( retval ), str, length ); |
| 768 |
retval->len = length; |
| 769 |
return retval; |
| 770 |
} |
| 771 |
|
| 772 |
/* |
| 773 |
* Str_dup duplicates a streng. It may return NULL on error. |
| 774 |
*/ |
| 775 |
streng *Str_dup( const streng *str ) |
| 776 |
{ |
| 777 |
return Str_buf( PSTRENGVAL( str ), PSTRENGLEN( str ) ) ; |
| 778 |
} |
| 779 |
|
| 780 |
streng *Str_cat( streng *first, const streng *second ) |
| 781 |
{ |
| 782 |
streng *ptr; |
| 783 |
int tmp; |
| 784 |
|
| 785 |
tmp = Str_len( first ) + Str_len( second ); |
| 786 |
|
| 787 |
if ( ( ptr = MAKESTRENG( tmp ) ) != NULL ) |
| 788 |
{ |
| 789 |
memcpy( ptr->value, first->value, Str_len( first ) ); |
| 790 |
memcpy( ptr->value + Str_len( first ), second->value, Str_len( second ) ); |
| 791 |
ptr->len = tmp; |
| 792 |
} |
| 793 |
return ptr; |
| 794 |
} |
| 795 |
|
| 796 |
/* |
| 797 |
* delete_a_queue deletes the queue's content and unlinks it from the list |
| 798 |
* of existing queues if q isn't SESSION. |
| 799 |
*/ |
| 800 |
void delete_a_queue( RxQueue *q ) |
| 801 |
{ |
| 802 |
Client *c ; |
| 803 |
|
| 804 |
if ( q->name ) |
| 805 |
{ |
| 806 |
DEBUGDUMP(printf("Deleting queue <%.*s>\n", PSTRENGLEN( q->name), PSTRENGVAL( q->name));); |
| 807 |
} |
| 808 |
else |
| 809 |
{ |
| 810 |
DEBUGDUMP(printf("Deleting natal queue\n");); |
| 811 |
} |
| 812 |
empty_queue( q ) ; |
| 813 |
|
| 814 |
if ( q == SESSION ) |
| 815 |
return ; |
| 816 |
|
| 817 |
if ( q->name != NULL ) |
| 818 |
DROPSTRENG( q->name ); |
| 819 |
|
| 820 |
/* |
| 821 |
* dequeue from the linked list and free space |
| 822 |
*/ |
| 823 |
if (q->prev != NULL) |
| 824 |
q->prev->next = q->next ; |
| 825 |
else |
| 826 |
queues = q->next ; |
| 827 |
if (q->next != NULL) |
| 828 |
q->next->prev = q->prev ; |
| 829 |
free( q ) ; |
| 830 |
|
| 831 |
/* |
| 832 |
* Let all clients connected to this queue fall back to "SESSION" as |
| 833 |
* the default queue. |
| 834 |
* FIXME: It may be better to leave a queue to a non-isReal state. |
| 835 |
* This kind of work destroys the data integrity on SESSION. |
| 836 |
* We get many more connections working on SESSION than expected. |
| 837 |
*/ |
| 838 |
for( c = clients; c != NULL; c = c->next ) |
| 839 |
{ |
| 840 |
if ( c->default_queue == q ) |
| 841 |
c->default_queue = SESSION ; |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
/* |
| 846 |
* delete_a_client deletes the clients' content and unlinks it from the list |
| 847 |
* of existing clients. |
| 848 |
*/ |
| 849 |
void delete_a_client( Client *c ) |
| 850 |
{ |
| 851 |
closesocket( c->socket ) ; |
| 852 |
|
| 853 |
/* |
| 854 |
* dequeue from the linked list and free space |
| 855 |
*/ |
| 856 |
if (c->prev != NULL) |
| 857 |
c->prev->next = c->next ; |
| 858 |
else |
| 859 |
clients = c->next ; |
| 860 |
if (c->next != NULL) |
| 861 |
c->next->prev = c->prev ; |
| 862 |
free( c ) ; |
| 863 |
} |
| 864 |
|
| 865 |
void delete_all_queues( void ) |
| 866 |
{ |
| 867 |
RxQueue *q, *h ; |
| 868 |
|
| 869 |
/* |
| 870 |
* SESSION won't be deleted. Be careful to initiate one delete per |
| 871 |
* queue. |
| 872 |
*/ |
| 873 |
for ( q = queues; q != NULL; ) |
| 874 |
{ |
| 875 |
h = q ; |
| 876 |
q = q->next ; |
| 877 |
delete_a_queue( h ); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
char *get_unspecified_queue( void ) |
| 882 |
{ |
| 883 |
char *rxq = getenv( "RXQUEUE" ); |
| 884 |
|
| 885 |
if ( rxq == NULL ) |
| 886 |
rxq = "SESSION"; |
| 887 |
|
| 888 |
if ( strchr(rxq, '@' ) == NULL ) |
| 889 |
{ |
| 890 |
char *h ; |
| 891 |
|
| 892 |
if ( ( h = (char *)malloc( strlen( rxq ) + 2 ) ) != NULL ) |
| 893 |
{ |
| 894 |
strcpy( h, rxq ) ; |
| 895 |
strcat( h, "@" ) ; |
| 896 |
rxq = h ; |
| 897 |
} |
| 898 |
} |
| 899 |
return rxq; |
| 900 |
} |
| 901 |
|
| 902 |
int suicide( void ) |
| 903 |
{ |
| 904 |
int sock; |
| 905 |
int myport; |
| 906 |
streng *queue; |
| 907 |
char *in_queue=get_unspecified_queue(); |
| 908 |
Queue q; |
| 909 |
|
| 910 |
if ( init_external_queue( NULL ) ) |
| 911 |
return 1; |
| 912 |
|
| 913 |
|
| 914 |
queue = Str_cre_or_exit( in_queue, strlen( in_queue ) ) ; |
| 915 |
|
| 916 |
if ( parse_queue( NULL, queue, &q ) == 1 ) |
| 917 |
{ |
| 918 |
if ( portno == 0 ) |
| 919 |
myport = default_port_number(); |
| 920 |
else |
| 921 |
myport = portno; |
| 922 |
q.u.e.portno = myport; |
| 923 |
sock = connect_to_rxstack( NULL, &q ); |
| 924 |
if ( sock < 0 ) |
| 925 |
{ |
| 926 |
/* error already shown by the function */ |
| 927 |
return(ERR_RXSTACK_CANT_CONNECT); |
| 928 |
} |
| 929 |
send_command_to_rxstack( NULL, sock, RXSTACK_KILL_STR, NULL, 0 ); |
| 930 |
read_result_from_rxstack( NULL, sock, RXSTACK_HEADER_SIZE ); |
| 931 |
closesocket(sock); |
| 932 |
} |
| 933 |
term_external_queue( ) ; |
| 934 |
return 0; |
| 935 |
} |
| 936 |
|
| 937 |
int rxstack_cleanup( void ) |
| 938 |
{ |
| 939 |
if ( !allclean ) |
| 940 |
{ |
| 941 |
DEBUGDUMP(printf("Cleaning up\n");); |
| 942 |
/* |
| 943 |
* Disconnect all clients |
| 944 |
* Delete all clients |
| 945 |
*/ |
| 946 |
delete_all_queues(); |
| 947 |
DEBUGDUMP(printf("Finished Cleaning up\n");); |
| 948 |
term_external_queue( ) ; |
| 949 |
allclean = 1; |
| 950 |
} |
| 951 |
return 0; |
| 952 |
} |
| 953 |
|
| 954 |
#ifdef BUILD_NT_SERVICE |
| 955 |
BOOL report_service_start( void ) |
| 956 |
{ |
| 957 |
/* |
| 958 |
* report the status to the service control manager. |
| 959 |
*/ |
| 960 |
return (ReportStatusToSCMgr( |
| 961 |
SERVICE_RUNNING, /* service state */ |
| 962 |
NO_ERROR, /* exit code */ |
| 963 |
0)); /* wait hint */ |
| 964 |
} |
| 965 |
|
| 966 |
BOOL report_service_pending_start( void ) |
| 967 |
{ |
| 968 |
/* |
| 969 |
* report the status to the service control manager. |
| 970 |
*/ |
| 971 |
return (ReportStatusToSCMgr( |
| 972 |
SERVICE_START_PENDING, /* service state */ |
| 973 |
NO_ERROR, /* exit code */ |
| 974 |
3000)); /* wait hint */ |
| 975 |
} |
| 976 |
|
| 977 |
int nt_service_start( void ) |
| 978 |
{ |
| 979 |
/* |
| 980 |
* code copied from sample NT Service code. The goto's are |
| 981 |
* not mine!! |
| 982 |
* report the status to the service control manager. |
| 983 |
*/ |
| 984 |
if ( !report_service_pending_start() ) |
| 985 |
goto cleanupper; |
| 986 |
|
| 987 |
/* |
| 988 |
* create the event object. The control handler function signals |
| 989 |
* this event when it receives the "stop" control code. |
| 990 |
*/ |
| 991 |
hServerStopEvent = CreateEvent( |
| 992 |
NULL, /* no security attributes */ |
| 993 |
TRUE, /* manual reset event */ |
| 994 |
FALSE, /* not-signalled */ |
| 995 |
NULL); /* no name */ |
| 996 |
|
| 997 |
if ( hServerStopEvent == NULL) |
| 998 |
goto cleanupper; |
| 999 |
|
| 1000 |
/* |
| 1001 |
* report the status to the service control manager. |
| 1002 |
*/ |
| 1003 |
if ( !report_service_pending_start() ) |
| 1004 |
goto cleanupper; |
| 1005 |
|
| 1006 |
return 0; |
| 1007 |
cleanupper: |
| 1008 |
return 1; |
| 1009 |
} |
| 1010 |
|
| 1011 |
VOID ServiceStop() |
| 1012 |
{ |
| 1013 |
DEBUGDUMP(printf("In ServiceStop()\n");); |
| 1014 |
suicide(); |
| 1015 |
/* |
| 1016 |
running = 0; |
| 1017 |
*/ |
| 1018 |
} |
| 1019 |
#endif |
| 1020 |
|
| 1021 |
void rxstack_signal_handler( int sig ) |
| 1022 |
{ |
| 1023 |
running = 0; |
| 1024 |
} |
| 1025 |
|
| 1026 |
/* Creates a new client and appends it in front of the current clients. |
| 1027 |
* Don't forget to set a default_queue and the socket at once. |
| 1028 |
*/ |
| 1029 |
Client *get_new_client( ) |
| 1030 |
{ |
| 1031 |
Client *retval = (Client *)malloc( sizeof( Client ) ) ; |
| 1032 |
|
| 1033 |
if ( retval == NULL ) |
| 1034 |
return NULL ; |
| 1035 |
memset( retval, 0, sizeof( Client ) ) ; |
| 1036 |
retval->socket = -1 ; |
| 1037 |
retval->deadline.milli = -1 ; /* deadline not used --> infinite timeout */ |
| 1038 |
|
| 1039 |
retval->next = clients ; |
| 1040 |
if ( clients != NULL ) |
| 1041 |
clients->prev = retval ; |
| 1042 |
clients = retval ; |
| 1043 |
return retval ; |
| 1044 |
} |
| 1045 |
|
| 1046 |
/* |
| 1047 |
* Find the named queue - case insensitive |
| 1048 |
* returns the queue or NULL if no queue with this name exists. |
| 1049 |
*/ |
| 1050 |
RxQueue *find_queue( const streng *queue_name ) |
| 1051 |
{ |
| 1052 |
RxQueue *q ; |
| 1053 |
|
| 1054 |
for ( q = queues; q != NULL; q = q->next ) |
| 1055 |
{ |
| 1056 |
/* This is inefficient, FIXME: Introduce a hash value */ |
| 1057 |
if ( Str_ccmp( q->name, queue_name ) == 0 ) |
| 1058 |
return q; |
| 1059 |
} |
| 1060 |
return NULL ; |
| 1061 |
} |
| 1062 |
|
| 1063 |
/* Creates a new queue and appends it in front of the current queues. |
| 1064 |
* Don't forget to set a name at once. |
| 1065 |
*/ |
| 1066 |
RxQueue *get_new_queue( void ) |
| 1067 |
{ |
| 1068 |
RxQueue *retval = (RxQueue *)malloc( sizeof( RxQueue ) ) ; |
| 1069 |
|
| 1070 |
if ( retval == NULL ) |
| 1071 |
return NULL ; |
| 1072 |
memset( retval, 0, sizeof( RxQueue ) ) ; |
| 1073 |
retval->deadline = queue_deadline ; |
| 1074 |
|
| 1075 |
retval->next = queues ; |
| 1076 |
if ( queues != NULL ) |
| 1077 |
queues->prev = retval ; |
| 1078 |
queues = retval ; |
| 1079 |
return retval ; |
| 1080 |
} |
| 1081 |
|
| 1082 |
int rxstack_delete_queue( Client *client, streng *queue_name ) |
| 1083 |
{ |
| 1084 |
RxQueue *q ; |
| 1085 |
int rc ; |
| 1086 |
|
| 1087 |
if ( ( q = find_queue( queue_name ) ) == NULL ) |
| 1088 |
{ |
| 1089 |
rc = 9; |
| 1090 |
} |
| 1091 |
else |
| 1092 |
{ |
| 1093 |
if ( q == SESSION ) |
| 1094 |
rc = 5; |
| 1095 |
else |
| 1096 |
{ |
| 1097 |
if ( !q->isReal ) |
| 1098 |
{ |
| 1099 |
/* |
| 1100 |
* If we found a false queue, return 9 |
| 1101 |
* but delete it. |
| 1102 |
*/ |
| 1103 |
delete_a_queue( q ); |
| 1104 |
rc = 9; |
| 1105 |
} |
| 1106 |
else |
| 1107 |
{ |
| 1108 |
/* |
| 1109 |
* Delete the contents of the queue |
| 1110 |
* and mark it as gone. |
| 1111 |
*/ |
| 1112 |
delete_a_queue( q ); |
| 1113 |
rc = 0; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
return rc ; |
| 1118 |
} |
| 1119 |
|
| 1120 |
int rxstack_create_client( int socket ) |
| 1121 |
{ |
| 1122 |
Client *c ; |
| 1123 |
|
| 1124 |
if ( ( c = get_new_client( ) ) == NULL ) |
| 1125 |
{ |
| 1126 |
closesocket( socket ) ; |
| 1127 |
/* This may have been the connection telling us to go down ;-) */ |
| 1128 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ); |
| 1129 |
exit( ERR_STORAGE_EXHAUSTED ); |
| 1130 |
} |
| 1131 |
|
| 1132 |
c->socket = socket; |
| 1133 |
c->default_queue = SESSION ; |
| 1134 |
return 0; |
| 1135 |
} |
| 1136 |
|
| 1137 |
int encode_ws_payload(u_char const *src, size_t srclength, char *target, size_t targsize, unsigned int opcode) |
| 1138 |
{ |
| 1139 |
unsigned long long b64_sz, payload_offset = 2, len = 0; |
| 1140 |
|
| 1141 |
if ((int)srclength <= 0) |
| 1142 |
{ |
| 1143 |
return 0; |
| 1144 |
} |
| 1145 |
|
| 1146 |
DEBUGDUMP(printf("Encode new frame\n");); |
| 1147 |
b64_sz = srclength; |
| 1148 |
|
| 1149 |
target[0] = (char)((opcode & 0x0F) | 0x80); |
| 1150 |
|
| 1151 |
if (b64_sz <= 125) |
| 1152 |
{ |
| 1153 |
target[1] = (char) b64_sz; |
| 1154 |
payload_offset = 2; |
| 1155 |
} |
| 1156 |
else if ((b64_sz > 125) && (b64_sz < 65536)) |
| 1157 |
{ |
| 1158 |
target[1] = (char) 126; |
| 1159 |
*(u_short*)&(target[2]) = htons(b64_sz); |
| 1160 |
payload_offset = 4; |
| 1161 |
} |
| 1162 |
else |
| 1163 |
{ |
| 1164 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, "rxstack websocket interface error: %s", "Sending frames larger than 65535 bytes not supported" ); |
| 1165 |
return -1; |
| 1166 |
} |
| 1167 |
|
| 1168 |
memcpy( target+payload_offset, src, srclength ); |
| 1169 |
len = srclength; |
| 1170 |
return len + payload_offset; |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* rxstack_send_return writes back to the client the action return code |
| 1174 |
* and optionally a string of length len. |
| 1175 |
* The functions returns 0 on success, -1 on error |
| 1176 |
*/ |
| 1177 |
int rxstack_send_return( Client *client, char *action, char *str, int len ) |
| 1178 |
{ |
| 1179 |
streng *qlen, *header; |
| 1180 |
int rc, retval = 0, rcode ; |
| 1181 |
int sock = client->socket; |
| 1182 |
char ws_response[4096]; |
| 1183 |
|
| 1184 |
if ( client->isWebsocket ) |
| 1185 |
{ |
| 1186 |
DEBUGDUMP(printf("Sending to websocket client %d Result: %c <%.*s>\n", sock, *action, len, (str) ? str : "");); |
| 1187 |
rcode = encode_ws_payload( (const unsigned char *)str, len, ws_response, sizeof(ws_response), 1); |
| 1188 |
rc = send( client->socket, ws_response, rcode, 0 ); |
| 1189 |
if ( rc != rcode ) |
| 1190 |
{ |
| 1191 |
DEBUGDUMP(printf("Send failed: rc> %d != len> %d errno = %d\n", rc, len, os_errno );); |
| 1192 |
retval = -1; |
| 1193 |
} |
| 1194 |
} |
| 1195 |
else |
| 1196 |
{ |
| 1197 |
DEBUGDUMP(printf("Sending to %d Result: %c <%.*s>\n", sock, *action, len, (str) ? str : "");); |
| 1198 |
qlen = REXX_D2X( len ); |
| 1199 |
if ( qlen ) |
| 1200 |
{ |
| 1201 |
header = REXX_RIGHT( qlen, RXSTACK_HEADER_SIZE, '0'); |
| 1202 |
DROPSTRENG( qlen ); |
| 1203 |
if ( header ) |
| 1204 |
{ |
| 1205 |
header->value[0] = action[0]; |
| 1206 |
DEBUGDUMP(printf("Sending Header: %.*s\n", PSTRENGLEN(header), PSTRENGVAL(header));); |
| 1207 |
rc = send( sock, PSTRENGVAL(header), PSTRENGLEN(header), 0 ); |
| 1208 |
if ( rc != PSTRENGLEN(header) ) |
| 1209 |
{ |
| 1210 |
DEBUGDUMP(printf("Send failed: rc> %d != PSTRENGLEN(header)> %d errno = %d\n", rc, PSTRENGLEN(header),os_errno );); |
| 1211 |
retval = -1 ; |
| 1212 |
} |
| 1213 |
else if ( str ) |
| 1214 |
{ |
| 1215 |
rc = send( sock, str, len, 0 ); |
| 1216 |
if ( rc != len ) |
| 1217 |
{ |
| 1218 |
DEBUGDUMP(printf("Send failed: errno = %d\n", os_errno );); |
| 1219 |
retval = -1 ; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
DROPSTRENG( header ); |
| 1223 |
} |
| 1224 |
} |
| 1225 |
} |
| 1226 |
return retval ; |
| 1227 |
} |
| 1228 |
|
| 1229 |
int rxstack_delete_client( Client *client ) |
| 1230 |
{ |
| 1231 |
DEBUGDUMP(printf("Deleting client: Socket: %d\n", client->socket );); |
| 1232 |
delete_a_client( client ) ; |
| 1233 |
return 0; |
| 1234 |
} |
| 1235 |
|
| 1236 |
/* rxstack_set_default_queue sets the client's (new) queue name. |
| 1237 |
* A false queue is created if the queue isn't found. |
| 1238 |
* The new queue is returned or NULL if we are out of memory. |
| 1239 |
*/ |
| 1240 |
RxQueue *rxstack_set_default_queue( Client *client, streng *data ) |
| 1241 |
{ |
| 1242 |
RxQueue *q, *prev; |
| 1243 |
streng *newq; |
| 1244 |
|
| 1245 |
prev = client->default_queue; |
| 1246 |
if ( ( q = find_queue( data ) ) == NULL ) |
| 1247 |
{ |
| 1248 |
/* |
| 1249 |
* We didn't find a real or a false queue, so create |
| 1250 |
* a false queue |
| 1251 |
*/ |
| 1252 |
q = get_new_queue( ); |
| 1253 |
if ( q != NULL ) |
| 1254 |
{ |
| 1255 |
newq = Str_dup( data ); |
| 1256 |
if ( newq == NULL ) |
| 1257 |
{ |
| 1258 |
delete_a_queue( q ); |
| 1259 |
return NULL; |
| 1260 |
} |
| 1261 |
q->name = REXX_UPPER( newq ) ; |
| 1262 |
DEBUGDUMP(printf("Creating the false queue <%.*s>", PSTRENGLEN( q->name ), PSTRENGVAL( q->name ) );); |
| 1263 |
/* q->isReal set to 0 by get_new_queue --> false queue */ |
| 1264 |
client->default_queue = q; |
| 1265 |
} |
| 1266 |
} |
| 1267 |
else |
| 1268 |
{ |
| 1269 |
client->default_queue = q; |
| 1270 |
} |
| 1271 |
|
| 1272 |
if ( q == NULL ) |
| 1273 |
{ |
| 1274 |
DEBUGDUMP(printf("No FREE MEMORY when setting default queue for client: <%.*s>\n", PSTRENGLEN(data), PSTRENGVAL(data) );); |
| 1275 |
} |
| 1276 |
else |
| 1277 |
{ |
| 1278 |
DEBUGDUMP(printf("Setting default queue for client: <%.*s> Prev: %p <%.*s>\n", PSTRENGLEN(q->name), PSTRENGVAL(q->name), prev, PSTRENGLEN(prev->name), PSTRENGVAL(prev->name) );); |
| 1279 |
/* SET or CREATE resets a timeout to 0; effectively turns off any timeout */ |
| 1280 |
client->queue_timeout = 0 ; |
| 1281 |
} |
| 1282 |
return q; |
| 1283 |
} |
| 1284 |
|
| 1285 |
int rxstack_timeout_queue( Client *client, const streng *data ) |
| 1286 |
{ |
| 1287 |
int val,error; |
| 1288 |
|
| 1289 |
/* |
| 1290 |
* Convert the timeout |
| 1291 |
* If the supplied timeout is 0 (infinite wait), set the client->queue_timeout |
| 1292 |
* to -1. |
| 1293 |
*/ |
| 1294 |
val = REXX_X2D( data, &error ); |
| 1295 |
if ( error ) |
| 1296 |
return 2; |
| 1297 |
if ( val == 0 ) |
| 1298 |
val = -1; |
| 1299 |
client->queue_timeout = val; |
| 1300 |
DEBUGDUMP(printf("Timeout on queue: %ld\n", client->queue_timeout );); |
| 1301 |
|
| 1302 |
return 0; |
| 1303 |
} |
| 1304 |
|
| 1305 |
/* unique_name creates a unique name for a queue. |
| 1306 |
* The function may exit after a message about missing memory. |
| 1307 |
*/ |
| 1308 |
static streng *unique_name( void ) |
| 1309 |
{ |
| 1310 |
static int first = 1 ; |
| 1311 |
static char buf[ 80 ] ; |
| 1312 |
static char *ptr ; |
| 1313 |
static unsigned runner = 0; |
| 1314 |
|
| 1315 |
if ( first ) |
| 1316 |
{ |
| 1317 |
first = 0 ; |
| 1318 |
sprintf( buf, "S%d%ld", (int) getpid(), (long) time( NULL ) ) ; |
| 1319 |
ptr = buf + strlen( buf ) ; |
| 1320 |
} |
| 1321 |
sprintf( ptr, "%u", runner++ ) ; |
| 1322 |
return Str_buf( buf, strlen( buf ) ) ; |
| 1323 |
} |
| 1324 |
|
| 1325 |
int rxstack_create_queue( Client *client, streng *data, streng **result ) |
| 1326 |
{ |
| 1327 |
RxQueue *q ; |
| 1328 |
streng *new_queue = NULL; |
| 1329 |
int rc = 0; |
| 1330 |
|
| 1331 |
if ( data ) |
| 1332 |
{ |
| 1333 |
DEBUGDUMP(printf("Creating new user-specified queue: <%.*s>\n", PSTRENGLEN(data), PSTRENGVAL(data) );); |
| 1334 |
if ( ( q = find_queue( data ) ) == NULL ) |
| 1335 |
{ |
| 1336 |
/* |
| 1337 |
* No queue of that name, so use a duplicate of it. |
| 1338 |
*/ |
| 1339 |
DEBUGDUMP(printf("Couldn't find <%.*s>; so creating it\n", PSTRENGLEN(data), PSTRENGVAL(data) );); |
| 1340 |
new_queue = data; |
| 1341 |
} |
| 1342 |
else |
| 1343 |
{ |
| 1344 |
/* |
| 1345 |
* If the queue we found is a false queue, we can still |
| 1346 |
* use the supplied name and the slot |
| 1347 |
*/ |
| 1348 |
DROPSTRENG( data ); |
| 1349 |
if ( !q->isReal ) |
| 1350 |
{ |
| 1351 |
DEBUGDUMP(printf("Found false queue\n" );); |
| 1352 |
q->isReal = 1; |
| 1353 |
/* SET or CREATE resets a timeout to 0 */ |
| 1354 |
client->queue_timeout = 0; |
| 1355 |
*result = q->name; |
| 1356 |
return 0; /* Pass back the name. May be different due to |
| 1357 |
* different locales or codepages, but it IS the selected |
| 1358 |
* name. |
| 1359 |
*/ |
| 1360 |
} |
| 1361 |
new_queue = unique_name( ); |
| 1362 |
if ( new_queue == NULL ) |
| 1363 |
return 3; |
| 1364 |
DEBUGDUMP(printf("Having to create unique queue <%.*s>\n", PSTRENGLEN( new_queue ), PSTRENGVAL( new_queue ) );); |
| 1365 |
rc = 1; |
| 1366 |
} |
| 1367 |
} |
| 1368 |
else |
| 1369 |
{ |
| 1370 |
DEBUGDUMP(printf("Creating system generated queue.\n");); |
| 1371 |
new_queue = unique_name( ); |
| 1372 |
if ( new_queue == NULL ) |
| 1373 |
return 3; |
| 1374 |
} |
| 1375 |
|
| 1376 |
if ( ( q = get_new_queue( ) ) == NULL ) |
| 1377 |
{ |
| 1378 |
DROPSTRENG( new_queue ); |
| 1379 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ); |
| 1380 |
return 3; |
| 1381 |
} |
| 1382 |
|
| 1383 |
/* |
| 1384 |
* Uppercase the queue name |
| 1385 |
*/ |
| 1386 |
q->name = REXX_UPPER( new_queue ); |
| 1387 |
q->isReal = 1; |
| 1388 |
/* SET or CREATE resets a timeout to 0 */ |
| 1389 |
client->queue_timeout = 0 ; |
| 1390 |
*result = q->name; |
| 1391 |
return rc; /* Both code 0 and code 1 return the name to the caller. |
| 1392 |
* May be different due to codepages, etc. |
| 1393 |
*/ |
| 1394 |
} |
| 1395 |
|
| 1396 |
/* |
| 1397 |
* Pushes 'line' onto the REXX stack in LIFO manner. |
| 1398 |
* point to the new line. The line is put on top of the current |
| 1399 |
* buffer. |
| 1400 |
*/ |
| 1401 |
StackLine *rxstack_stack_lifo( RxQueue *current_queue, streng *line ) |
| 1402 |
{ |
| 1403 |
StackLine *newbox ; |
| 1404 |
|
| 1405 |
if ( ( newbox = (StackLine *) malloc( sizeof(StackLine) ) ) != NULL ) |
| 1406 |
{ |
| 1407 |
newbox->contents = line ; |
| 1408 |
LIFO_LINE( ¤t_queue->buf, newbox ) ; |
| 1409 |
} |
| 1410 |
|
| 1411 |
return newbox; |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
/* |
| 1416 |
* Pushes 'line' onto the REXX stack in FIFO manner. |
| 1417 |
* point to the new line. The line is put on top of the current |
| 1418 |
* buffer. |
| 1419 |
*/ |
| 1420 |
StackLine *rxstack_stack_fifo( RxQueue *current_queue, streng *line ) |
| 1421 |
{ |
| 1422 |
StackLine *newbox ; |
| 1423 |
|
| 1424 |
if ( ( newbox = (StackLine *) malloc( sizeof(StackLine) ) ) != NULL ) |
| 1425 |
{ |
| 1426 |
newbox->contents = line ; |
| 1427 |
FIFO_LINE( ¤t_queue->buf, newbox ) ; |
| 1428 |
} |
| 1429 |
|
| 1430 |
return newbox; |
| 1431 |
} |
| 1432 |
|
| 1433 |
/* |
| 1434 |
* dequeue_waiter dequeues the Client c from the waiter's list of the queue q. |
| 1435 |
* The client must be linked in the waiter's list of the queue. |
| 1436 |
* The client's variables for this purpose are cleaned, too. |
| 1437 |
*/ |
| 1438 |
static void dequeue_waiter( RxQueue *q, Client *c ) |
| 1439 |
{ |
| 1440 |
#if defined(DEBUG) |
| 1441 |
Client *run ; |
| 1442 |
for ( run = q->oldest; run != NULL; run = run->newer ) |
| 1443 |
if ( run == c ) |
| 1444 |
break ; |
| 1445 |
assert( run != NULL ) ; /* This is the test if c is a waiter of q */ |
| 1446 |
#endif |
| 1447 |
|
| 1448 |
if ( c->older != NULL ) |
| 1449 |
{ |
| 1450 |
c->older->newer = c->newer ; |
| 1451 |
} |
| 1452 |
else |
| 1453 |
{ |
| 1454 |
q->oldest = c->newer ; |
| 1455 |
if ( q->oldest != NULL ) |
| 1456 |
q->oldest->older = NULL ; |
| 1457 |
} |
| 1458 |
|
| 1459 |
if ( c->newer != NULL ) |
| 1460 |
{ |
| 1461 |
c->newer->older = c->older ; |
| 1462 |
} |
| 1463 |
else |
| 1464 |
{ |
| 1465 |
q->newest = c->older ; |
| 1466 |
if ( q->newest != NULL ) |
| 1467 |
q->newest->newer = NULL ; |
| 1468 |
} |
| 1469 |
assert( ( ( q->newest != NULL ) && ( q->oldest != NULL ) ) || |
| 1470 |
( ( q->newest == NULL ) && ( q->oldest == NULL ) ) ) ; |
| 1471 |
c->older = c->newer = NULL ; |
| 1472 |
c->deadline.milli = -1 ; |
| 1473 |
} |
| 1474 |
|
| 1475 |
/* redir sends data as the answer of a pull operation back to |
| 1476 |
* the oldest waiting client and dequeues this client. |
| 1477 |
* data is dropped after the operation. |
| 1478 |
*/ |
| 1479 |
void redir( RxQueue *q, streng *data ) |
| 1480 |
{ |
| 1481 |
Client *c ; |
| 1482 |
|
| 1483 |
c = q->oldest ; |
| 1484 |
DEBUGDUMP(printf("Redirecting <%.*s> to waiting client %d\n", PSTRENGLEN(data), (PSTRENGVAL(data)) ? PSTRENGVAL(data) : "", c->socket );); |
| 1485 |
|
| 1486 |
dequeue_waiter( q, c ) ; |
| 1487 |
|
| 1488 |
rxstack_send_return( c, "0", PSTRENGVAL( data ), PSTRENGLEN( data ) ) ; |
| 1489 |
DROPSTRENG( data ) ; |
| 1490 |
} |
| 1491 |
|
| 1492 |
/* bad_news_for_waiter informs a waiter about an error while waiting for |
| 1493 |
* data for a pull request. The client is dequeued from its queue. |
| 1494 |
*/ |
| 1495 |
void bad_news_for_waiter( RxQueue *q, Client *c ) |
| 1496 |
{ |
| 1497 |
dequeue_waiter( q, c ) ; |
| 1498 |
DEBUGDUMP(printf("Sending negative response to waiting client %d\n", c->socket );); |
| 1499 |
|
| 1500 |
rxstack_send_return( c, "4", NULL, 0 ) ; |
| 1501 |
} |
| 1502 |
|
| 1503 |
int rxstack_queue_data( Client *client, streng *data, char order ) |
| 1504 |
{ |
| 1505 |
int rc = 0; |
| 1506 |
|
| 1507 |
if ( client->default_queue->oldest != NULL ) |
| 1508 |
{ |
| 1509 |
redir( client->default_queue, data ) ; |
| 1510 |
return 0 ; |
| 1511 |
} |
| 1512 |
DEBUGDUMP(printf("Queueing: <%.*s> Order: %c\n", PSTRENGLEN(data), (PSTRENGVAL(data)) ? PSTRENGVAL(data) : "", order );); |
| 1513 |
if ( order == RXSTACK_QUEUE_FIFO ) |
| 1514 |
{ |
| 1515 |
if ( rxstack_stack_fifo( client->default_queue, data ) == NULL ) |
| 1516 |
{ |
| 1517 |
DROPSTRENG( data ); |
| 1518 |
rc = 3; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
else |
| 1522 |
{ |
| 1523 |
if ( rxstack_stack_lifo( client->default_queue, data ) == NULL ) |
| 1524 |
{ |
| 1525 |
DROPSTRENG( data ); |
| 1526 |
rc = 3; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
return rc; |
| 1530 |
} |
| 1531 |
|
| 1532 |
/* Clears the content of the queue. All waiters are informed by code |
| 1533 |
* 2 of a cleaned queue and removed the the waiter's list. |
| 1534 |
*/ |
| 1535 |
void empty_queue( RxQueue *q ) |
| 1536 |
{ |
| 1537 |
StackLine *tmp, *line; |
| 1538 |
streng *contents; |
| 1539 |
Buffer *b ; |
| 1540 |
|
| 1541 |
b = &q->buf ; |
| 1542 |
for ( line = b->top; line != NULL; ) |
| 1543 |
{ |
| 1544 |
contents = line->contents; |
| 1545 |
DROPSTRENG( contents ); |
| 1546 |
tmp = line; |
| 1547 |
line = line->lower; |
| 1548 |
free( tmp ); |
| 1549 |
} |
| 1550 |
memset( &q->buf, 0, sizeof( Buffer ) ) ; |
| 1551 |
|
| 1552 |
/* acknowledge waiters for data not ready and dequeue them */ |
| 1553 |
|
| 1554 |
while ( q->oldest != NULL ) { |
| 1555 |
bad_news_for_waiter( q, q->oldest ) ; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
/* Clears the content of the queue named data. In opposite to the previous |
| 1560 |
* version, the client's current queue isn't set to the named queue any |
| 1561 |
* longer. The is the default behaviour in Regina. |
| 1562 |
* returns 0 on success, 2 if the queue doesn't exist. |
| 1563 |
*/ |
| 1564 |
int rxstack_empty_queue( Client *client, streng *data ) |
| 1565 |
{ |
| 1566 |
RxQueue *q ; |
| 1567 |
|
| 1568 |
DEBUGDUMP(printf("Emptying queue: <%.*s>\n", PSTRENGLEN(data), (PSTRENGVAL(data)) ? PSTRENGVAL(data) : "" );); |
| 1569 |
if ( ( q = find_queue( data ) ) == NULL ) |
| 1570 |
return 2; |
| 1571 |
|
| 1572 |
empty_queue( q ) ; |
| 1573 |
|
| 1574 |
return 0; |
| 1575 |
} |
| 1576 |
|
| 1577 |
int rxstack_number_in_queue( Client *client ) |
| 1578 |
{ |
| 1579 |
int lines = (int) client->default_queue->buf.elements; |
| 1580 |
|
| 1581 |
DEBUGDUMP(printf("Querying number in queue: %d\n", lines );); |
| 1582 |
return lines ; |
| 1583 |
} |
| 1584 |
|
| 1585 |
/* |
| 1586 |
* Pulls a line off the queue and dequeues it. |
| 1587 |
* |
| 1588 |
* If nowait isn't set and no data is available and the client's queue_timeout |
| 1589 |
* is set, the client is set to the newest end of the client's default_queue. |
| 1590 |
* |
| 1591 |
* It will be awaken by either ariving data on this pipe, or deleting/emptying |
| 1592 |
* the pipe, or by a timeout. |
| 1593 |
* Returns: |
| 1594 |
* 0 if line OK |
| 1595 |
* 1 if queue empty |
| 1596 |
* 3 if waiting |
| 1597 |
*/ |
| 1598 |
int rxstack_pull_line_off_queue( Client *client, streng **result, int nowait ) |
| 1599 |
{ |
| 1600 |
int rc; |
| 1601 |
Buffer *b; |
| 1602 |
StackLine *line; |
| 1603 |
RxQueue *q; |
| 1604 |
|
| 1605 |
b = &client->default_queue->buf; |
| 1606 |
POP_LINE( b, line ); |
| 1607 |
if ( line != NULL ) |
| 1608 |
{ |
| 1609 |
*result = line->contents; |
| 1610 |
free( line ); |
| 1611 |
rc = 0; |
| 1612 |
} |
| 1613 |
else |
| 1614 |
{ |
| 1615 |
*result = NULL; |
| 1616 |
if ( nowait ) |
| 1617 |
{ |
| 1618 |
rc = RXSTACK_EMPTY; /* queue empty */ |
| 1619 |
DEBUGDUMP(printf("nowait set to 1\n" );); |
| 1620 |
} |
| 1621 |
else |
| 1622 |
{ |
| 1623 |
if ( client->queue_timeout == 0 ) |
| 1624 |
{ |
| 1625 |
rc = RXSTACK_EMPTY; /* queue empty */ |
| 1626 |
DEBUGDUMP(printf("client timeout = 0\n" );); |
| 1627 |
} |
| 1628 |
else |
| 1629 |
{ |
| 1630 |
assert( client->deadline.milli == -1 ); |
| 1631 |
assert ( client->newer == NULL ); |
| 1632 |
if ( client->queue_timeout != -1 ) |
| 1633 |
{ |
| 1634 |
now = get_now( ); |
| 1635 |
client->deadline = now; |
| 1636 |
time_add( &client->deadline, client->queue_timeout ); |
| 1637 |
} |
| 1638 |
q = client->default_queue; |
| 1639 |
client->newer = NULL; |
| 1640 |
client->older = q->newest; |
| 1641 |
if ( client->older != NULL ) |
| 1642 |
client->older->newer = client; |
| 1643 |
q->newest = client; |
| 1644 |
if ( q->oldest == NULL ) |
| 1645 |
q->oldest = client; |
| 1646 |
rc = RXSTACK_WAITING; /* waiting */ |
| 1647 |
DEBUGDUMP(printf("waiting until %ld.%d\n", client->deadline.seconds,client->deadline.milli );); |
| 1648 |
} |
| 1649 |
} |
| 1650 |
} |
| 1651 |
DEBUGDUMP(printf("Pulling line off queue; rc %d\n", rc );); |
| 1652 |
return rc; |
| 1653 |
} |
| 1654 |
|
| 1655 |
/* |
| 1656 |
* Gets all queues for all clients |
| 1657 |
*/ |
| 1658 |
int rxstack_get_queues( Client *client, streng **result ) |
| 1659 |
{ |
| 1660 |
int rc,i=0; |
| 1661 |
int len=0; |
| 1662 |
RxQueue *q; |
| 1663 |
char eol[3]; |
| 1664 |
streng *seol,*tmp1=NULL,*tmp2=NULL; |
| 1665 |
|
| 1666 |
DEBUGDUMP(printf("Getting queues...\n" );); |
| 1667 |
/* get the length of all queues and the nul terminator and EOL delimiter */ |
| 1668 |
for ( q = queues; q != NULL; ) |
| 1669 |
{ |
| 1670 |
len += PSTRENGLEN( q->name)+1; |
| 1671 |
#if !defined(UNIX) && !defined(MAC) |
| 1672 |
len++; |
| 1673 |
#endif |
| 1674 |
DEBUGDUMP(printf("Queue name <%.*s>\n", PSTRENGLEN( q->name), PSTRENGVAL( q->name));); |
| 1675 |
q = q->next ; |
| 1676 |
} |
| 1677 |
/* determine eol for appending */ |
| 1678 |
#if !defined(UNIX) |
| 1679 |
eol[i++] = REGINA_CR; |
| 1680 |
#endif |
| 1681 |
#if !defined(MAC) |
| 1682 |
eol[i++] = REGINA_EOL; |
| 1683 |
#endif |
| 1684 |
eol[i] = '\0'; |
| 1685 |
seol = Str_cre_or_exit( eol, i ); |
| 1686 |
for ( q = queues; q != NULL; ) |
| 1687 |
{ |
| 1688 |
if ( tmp1 ) |
| 1689 |
tmp1 = Str_cat( tmp2, q->name ); |
| 1690 |
else |
| 1691 |
tmp1 = Str_dup( q->name ) ; |
| 1692 |
/* free tmp2 ? if null */ |
| 1693 |
DROPSTRENG( tmp2 ); |
| 1694 |
tmp2 = Str_cat( tmp1, seol ); |
| 1695 |
/* free tmp1 ? */ |
| 1696 |
DROPSTRENG( tmp1 ); |
| 1697 |
q = q->next ; |
| 1698 |
} |
| 1699 |
*result = tmp2; |
| 1700 |
rc = 0; |
| 1701 |
|
| 1702 |
return rc; |
| 1703 |
} |
| 1704 |
|
| 1705 |
static int eec_base64_encode( unsigned char *rawstr, int strlen, unsigned char **encstr, int *encstrlen ) |
| 1706 |
{ |
| 1707 |
unsigned char in[3], out[4]; |
| 1708 |
int len,i,j,k,encoded_length; |
| 1709 |
unsigned char *encoded_code; |
| 1710 |
|
| 1711 |
/* |
| 1712 |
* Allocate 4/3 times the rawstring length |
| 1713 |
*/ |
| 1714 |
encoded_length = 3 + ((strlen * 4) / 3); |
| 1715 |
encoded_code = (unsigned char *)malloc( encoded_length + sizeof(long) ); |
| 1716 |
if ( encoded_code == NULL ) |
| 1717 |
{ |
| 1718 |
return 1; |
| 1719 |
} |
| 1720 |
|
| 1721 |
for ( i = 0, j = 0; i < strlen; i +=3 ) |
| 1722 |
{ |
| 1723 |
len = 1; |
| 1724 |
in[0] = rawstr[i]; |
| 1725 |
if (i+1 < strlen) |
| 1726 |
{ |
| 1727 |
in[1] = rawstr[i+1]; |
| 1728 |
len++; |
| 1729 |
} |
| 1730 |
else |
| 1731 |
in[1] = 0; |
| 1732 |
if (i+2 < strlen) |
| 1733 |
{ |
| 1734 |
in[2] = rawstr[i+2]; |
| 1735 |
len++; |
| 1736 |
} |
| 1737 |
else |
| 1738 |
in[2] = 0; |
| 1739 |
/* encode */ |
| 1740 |
out[0] = cb64[ in[0] >> 2 ]; |
| 1741 |
out[1] = cb64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ]; |
| 1742 |
out[2] = (unsigned char) (len > 1 ? cb64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '='); |
| 1743 |
out[3] = (unsigned char) (len > 2 ? cb64[ in[2] & 0x3f ] : '='); |
| 1744 |
for ( k = 0; k < 4; k++ ) |
| 1745 |
{ |
| 1746 |
encoded_code[j++] = out[k]; |
| 1747 |
} |
| 1748 |
} |
| 1749 |
*encstrlen = j; |
| 1750 |
*encstr = encoded_code; |
| 1751 |
return 0; |
| 1752 |
} |
| 1753 |
|
| 1754 |
#if REQUIRE_BASE64_DECODE |
| 1755 |
static int eec_base64_decode( unsigned char *encstr, long encstrlen, unsigned char **rawstr, long *rawstrlen ) |
| 1756 |
{ |
| 1757 |
unsigned char *raw_code,v; |
| 1758 |
unsigned char *encoded_code; |
| 1759 |
unsigned char in[4], out[3]; |
| 1760 |
int i,j,k,len; |
| 1761 |
|
| 1762 |
encoded_code = encstr; |
| 1763 |
if ( (encstrlen % 4 ) != 0 ) |
| 1764 |
{ |
| 1765 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, "rxstack websocket interface error: %s", "Length of encoded value must be a multiple of 4." ); |
| 1766 |
return 1; |
| 1767 |
} |
| 1768 |
/* |
| 1769 |
* Decode encoded_code to produce raw |
| 1770 |
* Result will always be smaller so allocate memory for encoded length |
| 1771 |
*/ |
| 1772 |
raw_code = (unsigned char *)malloc( encstrlen ); |
| 1773 |
if ( raw_code == NULL ) |
| 1774 |
{ |
| 1775 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ); |
| 1776 |
return 1; |
| 1777 |
} |
| 1778 |
/* |
| 1779 |
* Now decode encoded_code |
| 1780 |
*/ |
| 1781 |
for ( i = 0, j = 0; i < encstrlen; ) |
| 1782 |
{ |
| 1783 |
for ( k = 0, len = 0; k < 4; ) |
| 1784 |
{ |
| 1785 |
v = (unsigned char) encoded_code[i++]; |
| 1786 |
v = (unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v - 43 ]); |
| 1787 |
if( v ) |
| 1788 |
{ |
| 1789 |
v = (unsigned char) ((v == '$') ? 0 : v - 61); |
| 1790 |
if ( v ) |
| 1791 |
{ |
| 1792 |
in[k++] = (unsigned char) (v - 1); |
| 1793 |
len++; |
| 1794 |
} |
| 1795 |
else |
| 1796 |
break; |
| 1797 |
} |
| 1798 |
} |
| 1799 |
/* decode */ |
| 1800 |
out[ 0 ] = (unsigned char ) (in[0] << 2 | in[1] >> 4); |
| 1801 |
out[ 1 ] = (unsigned char ) (in[1] << 4 | in[2] >> 2); |
| 1802 |
out[ 2 ] = (unsigned char ) (((in[2] << 6) & 0xc0) | in[3]); |
| 1803 |
for ( k = 0; k < len - 1; k++ ) |
| 1804 |
{ |
| 1805 |
raw_code[j++] = out[k]; |
| 1806 |
} |
| 1807 |
} |
| 1808 |
raw_code[j] = '\0'; |
| 1809 |
*rawstrlen = j; |
| 1810 |
*rawstr = raw_code; |
| 1811 |
return 0; |
| 1812 |
} |
| 1813 |
#endif |
| 1814 |
|
| 1815 |
int memcmpi( char *buf1, char *buf2, int len ) |
| 1816 |
{ |
| 1817 |
register short i=0; |
| 1818 |
char c1=0,c2=0; |
| 1819 |
for(i=0;i<len;i++) |
| 1820 |
{ |
| 1821 |
if (isupper(*buf1)) |
| 1822 |
c1 = tolower(*buf1); |
| 1823 |
else |
| 1824 |
c1 = *buf1; |
| 1825 |
if (isupper(*buf2)) |
| 1826 |
c2 = tolower(*buf2); |
| 1827 |
else |
| 1828 |
c2 = *buf2; |
| 1829 |
if (c1 != c2) |
| 1830 |
return(c1-c2); |
| 1831 |
++buf1; |
| 1832 |
++buf2; |
| 1833 |
} |
| 1834 |
return(0); |
| 1835 |
} |
| 1836 |
|
| 1837 |
int countstr( char *str, char ch ) |
| 1838 |
{ |
| 1839 |
int i,count=0; |
| 1840 |
int len = strlen( str ); |
| 1841 |
|
| 1842 |
for ( i = 0; i < len; i++ ) |
| 1843 |
{ |
| 1844 |
if ( str[i] == ch ) |
| 1845 |
count++; |
| 1846 |
} |
| 1847 |
return count; |
| 1848 |
} |
| 1849 |
|
| 1850 |
char determine_rxstack_command( char *str ) |
| 1851 |
{ |
| 1852 |
if ( memcmpi( "/fifo:", str, ACTION_FIFO_LEN ) == 0 |
| 1853 |
&& countstr( str, ':' ) >= 2 ) |
| 1854 |
return RXSTACK_QUEUE_FIFO; |
| 1855 |
else if ( memcmpi( "/lifo:", str, ACTION_FIFO_LEN ) == 0 |
| 1856 |
&& countstr( str, ':' ) >= 2 ) |
| 1857 |
return RXSTACK_QUEUE_LIFO; |
| 1858 |
else if ( memcmpi( "/queued:", str, ACTION_QUEUED_LEN ) == 0 |
| 1859 |
&& countstr( str, ':' ) == 1 ) |
| 1860 |
return RXSTACK_NUMBER_IN_QUEUE; |
| 1861 |
else if ( memcmpi( "/clear:", str, ACTION_CLEAR_LEN ) == 0 |
| 1862 |
&& countstr( str, ':' ) == 1 ) |
| 1863 |
return RXSTACK_EMPTY_QUEUE; |
| 1864 |
else if ( memcmpi( "/pull:", str, ACTION_PULL_LEN ) == 0 |
| 1865 |
&& countstr( str, ':' ) == 1 ) |
| 1866 |
return RXSTACK_PULL; |
| 1867 |
else if ( memcmpi( "/create:", str, ACTION_CREATE_LEN ) == 0 |
| 1868 |
&& countstr( str, ':' ) == 1 ) |
| 1869 |
return RXSTACK_CREATE_QUEUE; |
| 1870 |
else if ( memcmpi( "/delete:", str, ACTION_DELETE_LEN ) == 0 |
| 1871 |
&& countstr( str, ':' ) == 1 ) |
| 1872 |
return RXSTACK_DELETE_QUEUE; |
| 1873 |
/* wrong action or incorrect number of colons - error */ |
| 1874 |
return 0; |
| 1875 |
} |
| 1876 |
|
| 1877 |
#define BAD_REQUEST "HTTP/1.1 400 Bad Request\r\n\r\n" |
| 1878 |
int bad_request_from_websocket( Client *client ) |
| 1879 |
{ |
| 1880 |
int rc; |
| 1881 |
int len = strlen( BAD_REQUEST ); |
| 1882 |
|
| 1883 |
rc = send( client->socket, BAD_REQUEST, len, 0 ); |
| 1884 |
if ( rc != len ) |
| 1885 |
{ |
| 1886 |
DEBUGDUMP(printf("Send failed: rc> %d != len> %d errno = %d\n", rc, len, os_errno );); |
| 1887 |
return 0; |
| 1888 |
} |
| 1889 |
return 1; |
| 1890 |
} |
| 1891 |
|
| 1892 |
int rxstack_process_websockets_headers( Client *client ) |
| 1893 |
{ |
| 1894 |
int rc = 1; /* by default successful return */ |
| 1895 |
char ws_headers[4096]; |
| 1896 |
char key[1024]; |
| 1897 |
char protocol[1024]; |
| 1898 |
char response[4096]; |
| 1899 |
char *start, *end; |
| 1900 |
SHA1_HASH hash[SHA1_HASH_SIZE]; |
| 1901 |
Sha1Context context; |
| 1902 |
unsigned char *strptr; |
| 1903 |
int strlength; |
| 1904 |
int len; |
| 1905 |
|
| 1906 |
memset( ws_headers, 0, sizeof(ws_headers) ); |
| 1907 |
rc = recv( client->socket, ws_headers, 4096, 0 ); |
| 1908 |
if ( rc < 0 ) |
| 1909 |
{ |
| 1910 |
if ( os_errno != ECONNRESET ) |
| 1911 |
{ |
| 1912 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_READING_SOCKET, ERR_RXSTACK_READING_SOCKET_TMPL, errno_str( os_errno ) ); |
| 1913 |
} |
| 1914 |
/* |
| 1915 |
* Assume client has been lost |
| 1916 |
*/ |
| 1917 |
rxstack_delete_client( client ); |
| 1918 |
return 0 ; |
| 1919 |
} |
| 1920 |
if ( rc == 0 ) |
| 1921 |
{ |
| 1922 |
DEBUGDUMP(printf("read empty WS header\n");); |
| 1923 |
/* |
| 1924 |
* Assume client has been lost |
| 1925 |
*/ |
| 1926 |
rxstack_delete_client( client ); |
| 1927 |
return 0 ; |
| 1928 |
} |
| 1929 |
DEBUGDUMP(printf("header: [%s]\nLength: %d\n",ws_headers,rc);); |
| 1930 |
/* |
| 1931 |
* Parse the headers |
| 1932 |
*/ |
| 1933 |
start = ws_headers+4; |
| 1934 |
/* |
| 1935 |
* must specify "root" as location: ws://localhost:8888 or ws://localhost:8888/ only. |
| 1936 |
* no path allowed after optional port |
| 1937 |
*/ |
| 1938 |
end = strstr( start, "/ HTTP/1.1" ); |
| 1939 |
if ( !end ) |
| 1940 |
{ |
| 1941 |
bad_request_from_websocket( client ); |
| 1942 |
return 0; |
| 1943 |
} |
| 1944 |
/* validate and get protocol header */ |
| 1945 |
start = strstr( ws_headers, "\r\nSec-WebSocket-Protocol: "); |
| 1946 |
if (!start) |
| 1947 |
{ |
| 1948 |
DEBUGDUMP(printf("No header: \"Sec-WebSocket-Protocol:\"\n");); |
| 1949 |
bad_request_from_websocket( client ); |
| 1950 |
return 0; |
| 1951 |
} |
| 1952 |
/* protocol must be "rxstack" */ |
| 1953 |
start += 26; |
| 1954 |
end = strstr( start, "\r\n"); |
| 1955 |
strncpy( protocol, start, end-start ); |
| 1956 |
protocol[end-start] = '\0'; |
| 1957 |
if ( strcmp( protocol, "rxstack" ) != 0 ) |
| 1958 |
{ |
| 1959 |
DEBUGDUMP(printf("Invalid Protocol: %s; should be \"rxstack\"\n",protocol);); |
| 1960 |
bad_request_from_websocket( client ); |
| 1961 |
return 0; |
| 1962 |
} |
| 1963 |
/* validate and get key header */ |
| 1964 |
start = strstr(ws_headers, "\r\nSec-WebSocket-Key: "); |
| 1965 |
if (!start) |
| 1966 |
{ |
| 1967 |
bad_request_from_websocket( client ); |
| 1968 |
return 0; |
| 1969 |
} |
| 1970 |
start += 21; |
| 1971 |
end = strstr(start, "\r\n"); |
| 1972 |
strncpy( key, start, end-start ); |
| 1973 |
key[end-start] = '\0'; |
| 1974 |
|
| 1975 |
start = strstr(ws_headers, "\r\n\r\n"); |
| 1976 |
DEBUGDUMP(printf("end of headers at %x: [%s]\n",start,start );); |
| 1977 |
|
| 1978 |
/* |
| 1979 |
* Generate our response key |
| 1980 |
*/ |
| 1981 |
Sha1Initialise( &context ); |
| 1982 |
Sha1Update( &context, key, strlen(key) ); |
| 1983 |
Sha1Update( &context, WS_RESPONSE_MAGIC, 36 ); |
| 1984 |
Sha1Finalise( &context, hash ); |
| 1985 |
if ( eec_base64_encode( (unsigned char *)hash, SHA1_HASH_SIZE, (unsigned char **)&strptr, &strlength ) != 0 ) |
| 1986 |
{ |
| 1987 |
DEBUGDUMP(printf("base64 encoding failed:\n" );); |
| 1988 |
return 0; |
| 1989 |
} |
| 1990 |
DEBUGDUMP(printf("Base64 key: <%.*s>\n", strlength, strptr);); |
| 1991 |
/* |
| 1992 |
* Respond with our headers |
| 1993 |
*/ |
| 1994 |
len = sprintf( response, WS_RESPONSE_HEADER, strlength, strptr ); |
| 1995 |
DEBUGDUMP(printf("Sending to %d Result:\n[%.*s]\n", client->socket, len, response);); |
| 1996 |
rc = send( client->socket, response, len, 0 ); |
| 1997 |
if ( rc != len ) |
| 1998 |
{ |
| 1999 |
DEBUGDUMP(printf("Send failed: rc> %d != len> %d errno = %d\n", rc, len, os_errno );); |
| 2000 |
return 0; |
| 2001 |
} |
| 2002 |
free( strptr ); |
| 2003 |
/* |
| 2004 |
* Indicate we are a Websocket client |
| 2005 |
*/ |
| 2006 |
client->isWebsocket = 1; |
| 2007 |
return rc; |
| 2008 |
} |
| 2009 |
|
| 2010 |
int decode_ws_payload( unsigned char *src, size_t srclength, unsigned char *target, size_t targsize, unsigned int *opcode, unsigned int *left ) |
| 2011 |
{ |
| 2012 |
unsigned char *frame, *mask, *payload, save_char; |
| 2013 |
int masked = 0; |
| 2014 |
int i = 0, framecount = 0; |
| 2015 |
size_t remaining = 0; |
| 2016 |
unsigned int target_offset = 0, hdr_length = 0, payload_length = 0, decoded_length; |
| 2017 |
|
| 2018 |
*left = srclength; |
| 2019 |
frame = src; |
| 2020 |
|
| 2021 |
DEBUGDUMP(printf("Decode new frame\n");); |
| 2022 |
while (1) |
| 2023 |
{ |
| 2024 |
// Need at least two bytes of the header |
| 2025 |
// Find beginning of next frame. First time hdr_length, masked and |
| 2026 |
// payload_length are zero |
| 2027 |
frame += hdr_length + 4*masked + payload_length; |
| 2028 |
DEBUGDUMP(printf("frame[0..3]: 0x%x 0x%x 0x%x 0x%x (tot: %d)\n",(unsigned char) frame[0],(unsigned char) frame[1],(unsigned char) frame[2],(unsigned char) frame[3], srclength);); |
| 2029 |
|
| 2030 |
if (frame > src + srclength) |
| 2031 |
{ |
| 2032 |
DEBUGDUMP(printf("Truncated frame from client, need %d more bytes\n",frame - (src + srclength));); |
| 2033 |
break; |
| 2034 |
} |
| 2035 |
remaining = (src + srclength) - frame; |
| 2036 |
if (remaining < 2) |
| 2037 |
{ |
| 2038 |
DEBUGDUMP(printf("Truncated frame header from client\n");); |
| 2039 |
break; |
| 2040 |
} |
| 2041 |
framecount ++; |
| 2042 |
|
| 2043 |
*opcode = frame[0] & 0x0f; |
| 2044 |
masked = (frame[1] & 0x80) >> 7; |
| 2045 |
|
| 2046 |
if (*opcode == 0x8) |
| 2047 |
{ |
| 2048 |
// client sent orderly close frame |
| 2049 |
break; |
| 2050 |
} |
| 2051 |
|
| 2052 |
payload_length = frame[1] & 0x7f; |
| 2053 |
if (payload_length < 126) |
| 2054 |
{ |
| 2055 |
hdr_length = 2; |
| 2056 |
//frame += 2 * sizeof(char); |
| 2057 |
} |
| 2058 |
else if (payload_length == 126) |
| 2059 |
{ |
| 2060 |
payload_length = (frame[2] << 8) + frame[3]; |
| 2061 |
hdr_length = 4; |
| 2062 |
} |
| 2063 |
else |
| 2064 |
{ |
| 2065 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, "rxstack websocket interface error: %s", "Receiving frames larger than 65535 bytes not supported" ); |
| 2066 |
return -1; |
| 2067 |
} |
| 2068 |
if ((hdr_length + 4*masked + payload_length) > remaining) |
| 2069 |
{ |
| 2070 |
continue; |
| 2071 |
} |
| 2072 |
DEBUGDUMP(printf(" payload_length: %u, raw remaining: %u, , hdr_length: %d, opcode %d\n",payload_length, remaining,hdr_length, *opcode);); |
| 2073 |
payload = frame + hdr_length + 4*masked; |
| 2074 |
|
| 2075 |
if (*opcode != 1 && *opcode != 2) |
| 2076 |
{ |
| 2077 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, "rxstack websocket interface error: Ignoring non-data frame, opcode 0x%x", *opcode ); |
| 2078 |
continue; |
| 2079 |
} |
| 2080 |
|
| 2081 |
if (payload_length == 0) |
| 2082 |
{ |
| 2083 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, "rxstack websocket interface error: %s", "Ignoring empty frame" ); |
| 2084 |
continue; |
| 2085 |
} |
| 2086 |
|
| 2087 |
if ((payload_length > 0) && (!masked)) |
| 2088 |
{ |
| 2089 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, "rxstack websocket interface error: %s", "Received unmasked payload from client" ); |
| 2090 |
return -1; |
| 2091 |
} |
| 2092 |
|
| 2093 |
// Terminate with a null for base64 decode |
| 2094 |
save_char = payload[payload_length]; |
| 2095 |
payload[payload_length] = '\0'; |
| 2096 |
if ( debug ) |
| 2097 |
{ |
| 2098 |
DEBUGDUMP(printf("payload before masking: [%s]\nLength: %d\n",payload,payload_length);); |
| 2099 |
for ( i = 0; i < payload_length; i++ ) |
| 2100 |
DEBUGDUMP(printf("%x ",payload[i]);); |
| 2101 |
DEBUGDUMP(printf("\n");); |
| 2102 |
} |
| 2103 |
// unmask the data |
| 2104 |
mask = payload - 4; |
| 2105 |
for (i = 0; i < payload_length; i++) |
| 2106 |
{ |
| 2107 |
payload[i] ^= mask[i%4]; |
| 2108 |
} |
| 2109 |
if ( debug ) |
| 2110 |
{ |
| 2111 |
DEBUGDUMP(printf("payload after masking: [%s]\nLength: %d\n",payload,payload_length);); |
| 2112 |
for ( i = 0; i < payload_length; i++ ) |
| 2113 |
DEBUGDUMP(printf("%x ",payload[i]);); |
| 2114 |
DEBUGDUMP(printf("\n");); |
| 2115 |
} |
| 2116 |
#ifdef REQUIRE_BASE64_DECODE |
| 2117 |
// base64 decode the data - not needed for text-only data ? |
| 2118 |
if ( eec_base64_decode( (unsigned char *)payload, payload_length, &decoded_chunk, &decoded_length ) != 0 ) |
| 2119 |
{ |
| 2120 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_INTERNAL, "rxstack websocket interface error: Base64 decode error code %d", len ); |
| 2121 |
return -1; |
| 2122 |
} |
| 2123 |
memcpy( target+target_offset, decoded_chunk, decoded_length ); |
| 2124 |
free( decoded_chunk ); |
| 2125 |
#else |
| 2126 |
memcpy( target+target_offset, payload, payload_length+1 ); /* +1 ensures nul terminated string */ |
| 2127 |
decoded_length = payload_length; |
| 2128 |
#endif |
| 2129 |
|
| 2130 |
// Restore the first character of the next frame |
| 2131 |
payload[payload_length] = save_char; |
| 2132 |
target_offset += decoded_length; |
| 2133 |
} |
| 2134 |
|
| 2135 |
if (framecount > 1) |
| 2136 |
{ |
| 2137 |
DEBUGDUMP(printf("framecount: %d\n",framecount);); |
| 2138 |
} |
| 2139 |
|
| 2140 |
*left = remaining; |
| 2141 |
return target_offset; |
| 2142 |
} |
| 2143 |
|
| 2144 |
int split_ws_payload( char *ws_payload, int offset, streng **queue, streng **buffer ) |
| 2145 |
{ |
| 2146 |
int i,buffer_start=0; |
| 2147 |
int len = strlen( ws_payload ); |
| 2148 |
char *q = ws_payload+offset; |
| 2149 |
|
| 2150 |
if ( ws_payload[offset] == '\0' |
| 2151 |
|| ( ws_payload[offset] == ':' && len == offset + 1 ) ) |
| 2152 |
{ |
| 2153 |
/* queue and buffer both empty */ |
| 2154 |
/* caters for: |
| 2155 |
* /clear: |
| 2156 |
* /fifo:: |
| 2157 |
*/ |
| 2158 |
*queue = *buffer = NULL; |
| 2159 |
return 0; |
| 2160 |
} |
| 2161 |
for ( i = offset; i < len; i++ ) |
| 2162 |
{ |
| 2163 |
if ( ws_payload[i] == ':' ) |
| 2164 |
{ |
| 2165 |
ws_payload[i] = '\0'; |
| 2166 |
*queue = Str_buf( q, i - offset ); |
| 2167 |
buffer_start = i+1; |
| 2168 |
break; |
| 2169 |
} |
| 2170 |
} |
| 2171 |
/* if no ':' found, buffer is empty but we have a queue */ |
| 2172 |
if ( buffer_start == 0 ) |
| 2173 |
{ |
| 2174 |
*buffer = NULL; |
| 2175 |
*queue = Str_buf( q, len - offset ); |
| 2176 |
} |
| 2177 |
else |
| 2178 |
{ |
| 2179 |
*buffer = Str_buf( ws_payload+buffer_start, len - buffer_start + 1 ); |
| 2180 |
} |
| 2181 |
return 0; |
| 2182 |
} |
| 2183 |
|
| 2184 |
int send_response_to_client( Client *client, char action, streng *buffer ) |
| 2185 |
{ |
| 2186 |
int rc,length; |
| 2187 |
char rcode[2]; |
| 2188 |
RxQueue *q ; |
| 2189 |
streng *result=NULL; |
| 2190 |
|
| 2191 |
switch( action ) |
| 2192 |
{ |
| 2193 |
case RXSTACK_QUEUE_FIFO: |
| 2194 |
case RXSTACK_QUEUE_LIFO: |
| 2195 |
DEBUGDUMP(printf("--- Queue %s ---\n", action == RXSTACK_QUEUE_FIFO ? "FIFO" : "LIFO");); |
| 2196 |
/* |
| 2197 |
* fixes bug 700539 |
| 2198 |
*/ |
| 2199 |
if ( buffer == NULL ) |
| 2200 |
buffer = Str_buf( "", 0 ); |
| 2201 |
if ( buffer == NULL ) |
| 2202 |
rc = 3; |
| 2203 |
else |
| 2204 |
rc = rxstack_queue_data( client, buffer, action ); |
| 2205 |
rcode[0] = (char)(rc+'0'); |
| 2206 |
rxstack_send_return( client, rcode, NULL, 0 ); |
| 2207 |
buffer = NULL ; /* consumed by rxstack_queue_data */ |
| 2208 |
break; |
| 2209 |
case RXSTACK_EXIT: |
| 2210 |
DEBUGDUMP(printf("--- Exit ---\n");); |
| 2211 |
/* |
| 2212 |
* Client has requested disconnect, so remove all |
| 2213 |
* references to the client |
| 2214 |
*/ |
| 2215 |
rxstack_send_return( client, "0", NULL, 0 ); |
| 2216 |
rxstack_delete_client( client ); |
| 2217 |
if ( buffer != NULL ) |
| 2218 |
{ |
| 2219 |
DROPSTRENG( buffer ); |
| 2220 |
buffer = NULL; |
| 2221 |
} |
| 2222 |
break; |
| 2223 |
case RXSTACK_KILL: |
| 2224 |
DEBUGDUMP(printf("--- Kill ---\n");); |
| 2225 |
/* |
| 2226 |
* Client has requested server to stop |
| 2227 |
*/ |
| 2228 |
rxstack_send_return( client, "0", NULL, 0 ); |
| 2229 |
rxstack_delete_client( client ); |
| 2230 |
running = 0; |
| 2231 |
return 0; |
| 2232 |
case RXSTACK_SET_QUEUE: |
| 2233 |
DEBUGDUMP(printf("--- Set Queue ---\n");); |
| 2234 |
/* |
| 2235 |
* Set the default queue for the client |
| 2236 |
*/ |
| 2237 |
if ( buffer == NULL ) |
| 2238 |
rxstack_send_return( client, "6", NULL, 0 ); |
| 2239 |
else |
| 2240 |
{ |
| 2241 |
q = rxstack_set_default_queue( client, buffer ); |
| 2242 |
if ( q == NULL ) |
| 2243 |
rxstack_send_return( client, "3", NULL, 0 ); |
| 2244 |
else |
| 2245 |
rxstack_send_return( client, "0", q->name->value, q->name->len ); |
| 2246 |
DROPSTRENG( buffer ); |
| 2247 |
buffer = NULL; |
| 2248 |
} |
| 2249 |
break; |
| 2250 |
case RXSTACK_EMPTY_QUEUE: |
| 2251 |
DEBUGDUMP(printf("--- Empty Queue ---\n");); |
| 2252 |
/* |
| 2253 |
* Use the current queue as the default queue. |
| 2254 |
*/ |
| 2255 |
if ( buffer == NULL ) |
| 2256 |
buffer = client->default_queue->name; |
| 2257 |
rc = rxstack_empty_queue( client, buffer ); |
| 2258 |
rcode[0] = (char)(rc+'0'); |
| 2259 |
rxstack_send_return( client, rcode, NULL, 0 ); |
| 2260 |
if ( buffer != client->default_queue->name ) |
| 2261 |
DROPSTRENG( buffer ); |
| 2262 |
buffer = NULL ; |
| 2263 |
break; |
| 2264 |
case RXSTACK_NUMBER_IN_QUEUE: |
| 2265 |
DEBUGDUMP(printf("--- Number in Queue ---\n");); |
| 2266 |
length = rxstack_number_in_queue( client ); |
| 2267 |
rxstack_send_return( client, "0", NULL, length ); |
| 2268 |
if ( buffer != NULL ) |
| 2269 |
{ |
| 2270 |
DROPSTRENG( buffer ); |
| 2271 |
buffer = NULL; |
| 2272 |
} |
| 2273 |
break; |
| 2274 |
case RXSTACK_SHOW_QUEUES: |
| 2275 |
DEBUGDUMP(printf("--- Show ---\n");); |
| 2276 |
rc = rxstack_get_queues( client, &result ); |
| 2277 |
switch( rc ) |
| 2278 |
{ |
| 2279 |
case 0: /* all OK */ |
| 2280 |
rxstack_send_return( client, "0", PSTRENGVAL( result ), PSTRENGLEN( result ) ); |
| 2281 |
DROPSTRENG( result ); |
| 2282 |
break; |
| 2283 |
default: /* empty/error */ |
| 2284 |
rcode[0] = (char)(rc+'0'); |
| 2285 |
rxstack_send_return( client, rcode, NULL, 0 ); |
| 2286 |
break; |
| 2287 |
} |
| 2288 |
if ( buffer != NULL ) |
| 2289 |
{ |
| 2290 |
DROPSTRENG( buffer ); |
| 2291 |
buffer = NULL; |
| 2292 |
} |
| 2293 |
break; |
| 2294 |
case RXSTACK_PULL: |
| 2295 |
case RXSTACK_FETCH: |
| 2296 |
DEBUGDUMP(printf("--- Pull ---\n");); |
| 2297 |
rc = rxstack_pull_line_off_queue( client, &result, action == RXSTACK_FETCH ); |
| 2298 |
switch( rc ) |
| 2299 |
{ |
| 2300 |
case 0: /* all OK */ |
| 2301 |
rxstack_send_return( client, "0", PSTRENGVAL( result ), PSTRENGLEN( result ) ); |
| 2302 |
DROPSTRENG( result ); |
| 2303 |
break; |
| 2304 |
case RXSTACK_WAITING: /* still waiting; don't return */ |
| 2305 |
break; |
| 2306 |
default: /* empty/error */ |
| 2307 |
rcode[0] = (char)(rc+'0'); |
| 2308 |
rxstack_send_return( client, rcode, NULL, 0 ); |
| 2309 |
break; |
| 2310 |
} |
| 2311 |
if ( buffer != NULL ) |
| 2312 |
{ |
| 2313 |
DROPSTRENG( buffer ); |
| 2314 |
buffer = NULL; |
| 2315 |
} |
| 2316 |
break; |
| 2317 |
case RXSTACK_GET_QUEUE: |
| 2318 |
DEBUGDUMP(printf("--- Get Queue ---\n");); |
| 2319 |
rxstack_send_return( client, "0", PSTRENGVAL(client->default_queue->name), PSTRENGLEN(client->default_queue->name) ) ; |
| 2320 |
if ( buffer != NULL ) |
| 2321 |
{ |
| 2322 |
DROPSTRENG( buffer ); |
| 2323 |
buffer = NULL; |
| 2324 |
} |
| 2325 |
break; |
| 2326 |
case RXSTACK_CREATE_QUEUE: |
| 2327 |
DEBUGDUMP(printf("--- Create Queue ---\n");); |
| 2328 |
/* |
| 2329 |
* Create a new queue |
| 2330 |
*/ |
| 2331 |
rc = rxstack_create_queue( client, buffer, &result ); |
| 2332 |
rcode[0] = (char)(rc+'0'); |
| 2333 |
if ( ( rc != 1 ) && ( rc != 0 ) ) |
| 2334 |
rxstack_send_return( client, rcode, NULL, 0 ); |
| 2335 |
else |
| 2336 |
rxstack_send_return( client, rcode, PSTRENGVAL(result), PSTRENGLEN(result) ); |
| 2337 |
buffer = NULL; /* consumed by rxstack_create_queue */ |
| 2338 |
break; |
| 2339 |
case RXSTACK_DELETE_QUEUE: |
| 2340 |
DEBUGDUMP(printf("--- Delete Queue ---\n");); |
| 2341 |
/* |
| 2342 |
* Delete the queue |
| 2343 |
*/ |
| 2344 |
if ( buffer == NULL ) |
| 2345 |
rc = 6; |
| 2346 |
else |
| 2347 |
rc = rxstack_delete_queue( client, buffer ); |
| 2348 |
rcode[0] = (char)(rc+'0'); |
| 2349 |
rxstack_send_return( client, rcode, NULL, 0 ); |
| 2350 |
if ( buffer != NULL ) |
| 2351 |
{ |
| 2352 |
DROPSTRENG( buffer ); |
| 2353 |
buffer = NULL; |
| 2354 |
} |
| 2355 |
break; |
| 2356 |
case RXSTACK_TIMEOUT_QUEUE: |
| 2357 |
DEBUGDUMP(printf("--- Timeout Queue ---\n");); |
| 2358 |
/* |
| 2359 |
* Set timeout for pull from queue |
| 2360 |
*/ |
| 2361 |
if ( buffer == NULL ) |
| 2362 |
rc = 6; |
| 2363 |
else |
| 2364 |
rc = rxstack_timeout_queue( client, buffer ); |
| 2365 |
rcode[0] = (char)(rc+'0'); |
| 2366 |
rxstack_send_return( client, rcode, NULL, 0 ); |
| 2367 |
if ( buffer != NULL ) |
| 2368 |
DROPSTRENG( buffer ); |
| 2369 |
buffer = NULL; |
| 2370 |
break; |
| 2371 |
case RXSTACK_UNKNOWN: |
| 2372 |
/* do nothing */ |
| 2373 |
break; |
| 2374 |
default: |
| 2375 |
rxstack_send_return( client, "9", NULL, 0 ); |
| 2376 |
break; |
| 2377 |
} |
| 2378 |
assert( buffer == NULL ) ; |
| 2379 |
if ( buffer != NULL ) |
| 2380 |
DROPSTRENG( buffer ) ; |
| 2381 |
return 1; |
| 2382 |
} |
| 2383 |
|
| 2384 |
int rxstack_process_websockets_data( Client *client ) |
| 2385 |
{ |
| 2386 |
int rc = 1; |
| 2387 |
char ws_headers[8192]; |
| 2388 |
char ws_payload[4096]; |
| 2389 |
char ws_response[4096]; |
| 2390 |
char response[4096]; |
| 2391 |
int response_length; |
| 2392 |
int len,i,rcode; |
| 2393 |
unsigned int opcode, left; |
| 2394 |
streng *queue=NULL, *buffer=NULL, *result; |
| 2395 |
streng *resp_streng, *rcode_streng; |
| 2396 |
char action; |
| 2397 |
int length; |
| 2398 |
char srcode[3]; |
| 2399 |
RxQueue *q ; |
| 2400 |
|
| 2401 |
srcode[1] = ':'; |
| 2402 |
srcode[2] = '\0'; |
| 2403 |
/* assumes only 4096 bytes can b sent by client and in one chunk */ |
| 2404 |
memset( ws_headers, 0, sizeof(ws_headers) ); |
| 2405 |
rc = recv( client->socket, ws_headers, 4096, 0 ); |
| 2406 |
if ( rc < 0 ) |
| 2407 |
{ |
| 2408 |
if ( os_errno != ECONNRESET ) |
| 2409 |
{ |
| 2410 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_READING_SOCKET, ERR_RXSTACK_READING_SOCKET_TMPL, errno_str( os_errno ) ); |
| 2411 |
} |
| 2412 |
/* |
| 2413 |
* Assume client has been lost |
| 2414 |
*/ |
| 2415 |
rxstack_delete_client( client ); |
| 2416 |
return 0 ; |
| 2417 |
} |
| 2418 |
if ( rc == 0 ) |
| 2419 |
{ |
| 2420 |
DEBUGDUMP(printf("read empty WS data\n");); |
| 2421 |
/* |
| 2422 |
* Assume client has been lost |
| 2423 |
*/ |
| 2424 |
rxstack_delete_client( client ); |
| 2425 |
return 0 ; |
| 2426 |
} |
| 2427 |
if ( debug ) |
| 2428 |
{ |
| 2429 |
DEBUGDUMP(printf("header: [%s]\nLength: %d\n",ws_headers,rc);); |
| 2430 |
for ( i = 0; i < rc; i++ ) |
| 2431 |
DEBUGDUMP(printf("%x ",ws_headers[i]);); |
| 2432 |
DEBUGDUMP(printf("\n");); |
| 2433 |
} |
| 2434 |
/* |
| 2435 |
* Decode the websocket payload |
| 2436 |
*/ |
| 2437 |
rcode = decode_ws_payload( (unsigned char *)ws_headers, rc, (unsigned char *)ws_payload, sizeof(ws_payload), &opcode, &left ); |
| 2438 |
DEBUGDUMP(printf("after decode payload: rcode: %d remaining: %d payload:[%s]\n",rcode,left,ws_payload);); |
| 2439 |
if ( opcode == 0x8 ) |
| 2440 |
{ |
| 2441 |
/* client sent close request; send back a close opcode message with empty payload */ |
| 2442 |
len = encode_ws_payload( (unsigned char *)"", 0, ws_response, sizeof(ws_response), opcode); |
| 2443 |
rc = send( client->socket, ws_response, len, 0 ); |
| 2444 |
if ( rc != len ) |
| 2445 |
{ |
| 2446 |
DEBUGDUMP(printf("Send failed: rc> %d != len> %d errno = %d\n", rc, len, os_errno );); |
| 2447 |
return 0; |
| 2448 |
} |
| 2449 |
rxstack_delete_client( client ); |
| 2450 |
} |
| 2451 |
else |
| 2452 |
{ |
| 2453 |
DEBUGDUMP(printf("got valid command:[%s]\n",ws_payload);); |
| 2454 |
/* process command from client - TODO */ |
| 2455 |
action = determine_rxstack_command( ws_payload ); |
| 2456 |
if ( action == 0 ) |
| 2457 |
{ |
| 2458 |
DEBUGDUMP(printf("decoded invalid command: opcode %d\n",opcode);); |
| 2459 |
/* wrong action or syntax error */ |
| 2460 |
len = sprintf( ws_headers, "9:Incorrect rxstack command or invalid format of command: %s", ws_payload ); |
| 2461 |
rxstack_send_return( client, &action, ws_headers, len ); |
| 2462 |
} |
| 2463 |
else |
| 2464 |
{ |
| 2465 |
DEBUGDUMP(printf("decoded valid command:[%c]\n",action);); |
| 2466 |
/* valid action: process the request and send response */ |
| 2467 |
switch( action ) |
| 2468 |
{ |
| 2469 |
case RXSTACK_QUEUE_FIFO: |
| 2470 |
case RXSTACK_QUEUE_LIFO: |
| 2471 |
DEBUGDUMP(printf("WS: Queue %s ---\n", action == RXSTACK_QUEUE_FIFO ? "FIFO" : "LIFO");); |
| 2472 |
split_ws_payload( ws_payload, ACTION_FIFO_LEN, &queue, &buffer ); |
| 2473 |
if ( queue |
| 2474 |
&& PSTRENGLEN( queue ) ) |
| 2475 |
{ |
| 2476 |
q = rxstack_set_default_queue( client, queue ); |
| 2477 |
if ( q == NULL ) |
| 2478 |
// errorrxstack_send_return( client, "3", NULL, 0 ); |
| 2479 |
break; |
| 2480 |
} |
| 2481 |
/* |
| 2482 |
* fixes bug 700539 |
| 2483 |
*/ |
| 2484 |
if ( buffer == NULL ) |
| 2485 |
buffer = Str_buf( "", 0 ); |
| 2486 |
if ( buffer == NULL ) |
| 2487 |
rc = 3; |
| 2488 |
else |
| 2489 |
rc = rxstack_queue_data( client, buffer, action ); |
| 2490 |
|
| 2491 |
response_length = sprintf( response, "%d:", rc ); |
| 2492 |
DEBUGDUMP(printf(" Response: [%s] Len: %d\n",response,response_length);); |
| 2493 |
rxstack_send_return( client, &action, response, response_length ); |
| 2494 |
buffer = NULL ; /* consumed by rxstack_queue_data */ |
| 2495 |
break; |
| 2496 |
case RXSTACK_EMPTY_QUEUE: |
| 2497 |
DEBUGDUMP(printf("WS: Empty Queue ---\n");); |
| 2498 |
split_ws_payload( ws_payload, ACTION_CLEAR_LEN, &queue, &buffer ); |
| 2499 |
if ( !queue |
| 2500 |
|| PSTRENGLEN( queue ) == 0 ) |
| 2501 |
queue = client->default_queue->name; |
| 2502 |
rc = rxstack_empty_queue( client, queue ); |
| 2503 |
srcode[0] = (char)(rc+'0'); |
| 2504 |
rxstack_send_return( client, &action, srcode, 2 ); |
| 2505 |
break; |
| 2506 |
case RXSTACK_NUMBER_IN_QUEUE: |
| 2507 |
DEBUGDUMP(printf("WS: Number in Queue ---\n");); |
| 2508 |
split_ws_payload( ws_payload, ACTION_QUEUED_LEN, &queue, &buffer ); |
| 2509 |
if ( queue |
| 2510 |
&& PSTRENGLEN( queue ) ) |
| 2511 |
{ |
| 2512 |
q = rxstack_set_default_queue( client, queue ); |
| 2513 |
if ( q == NULL ) |
| 2514 |
// errorrxstack_send_return( client, "3", NULL, 0 ); |
| 2515 |
break; |
| 2516 |
} |
| 2517 |
length = rxstack_number_in_queue( client ); |
| 2518 |
response_length = sprintf( response, "0:%d", length ); |
| 2519 |
DEBUGDUMP(printf(" Response: [%s] Len: %d\n",response,response_length);); |
| 2520 |
rxstack_send_return( client, &action, response, response_length ); |
| 2521 |
break; |
| 2522 |
case RXSTACK_PULL: |
| 2523 |
DEBUGDUMP(printf("WS: Pull ---\n");); |
| 2524 |
split_ws_payload( ws_payload, ACTION_PULL_LEN, &queue, &buffer ); |
| 2525 |
if ( queue |
| 2526 |
&& PSTRENGLEN( queue ) ) |
| 2527 |
{ |
| 2528 |
q = rxstack_set_default_queue( client, queue ); |
| 2529 |
if ( q == NULL ) |
| 2530 |
// errorrxstack_send_return( client, "3", NULL, 0 ); |
| 2531 |
break; |
| 2532 |
} |
| 2533 |
rc = rxstack_pull_line_off_queue( client, &result, action == RXSTACK_FETCH ); |
| 2534 |
srcode[0] = (char)(rc+'0'); |
| 2535 |
switch( rc ) |
| 2536 |
{ |
| 2537 |
case 0: /* all OK */ |
| 2538 |
rcode_streng = Str_buf( srcode, 2 ); |
| 2539 |
if ( rcode_streng == NULL ) |
| 2540 |
{ |
| 2541 |
rxstack_send_return( client, &action, "9:rxstack: out of memory", 23 ); |
| 2542 |
} |
| 2543 |
else |
| 2544 |
{ |
| 2545 |
resp_streng = Str_cat( rcode_streng, result ); |
| 2546 |
if ( resp_streng == NULL ) |
| 2547 |
{ |
| 2548 |
rxstack_send_return( client, &action, "9:rxstack: out of memory", 23 ); |
| 2549 |
DROPSTRENG( rcode_streng ); |
| 2550 |
} |
| 2551 |
else |
| 2552 |
{ |
| 2553 |
rxstack_send_return( client, &action, PSTRENGVAL( resp_streng ), PSTRENGLEN( resp_streng ) ); |
| 2554 |
DROPSTRENG( result ); |
| 2555 |
DROPSTRENG( resp_streng ); |
| 2556 |
DROPSTRENG( rcode_streng ); |
| 2557 |
} |
| 2558 |
} |
| 2559 |
break; |
| 2560 |
case RXSTACK_WAITING: /* still waiting; don't return */ |
| 2561 |
break; |
| 2562 |
default: /* empty/error */ |
| 2563 |
rxstack_send_return( client, &action, srcode, 2 ); |
| 2564 |
break; |
| 2565 |
} |
| 2566 |
break; |
| 2567 |
case RXSTACK_CREATE_QUEUE: |
| 2568 |
DEBUGDUMP(printf("WS: Create Queue ---\n");); |
| 2569 |
/* |
| 2570 |
* Create a new queue |
| 2571 |
*/ |
| 2572 |
split_ws_payload( ws_payload, ACTION_CREATE_LEN, &queue, &buffer ); |
| 2573 |
rc = rxstack_create_queue( client, queue, &result ); |
| 2574 |
srcode[0] = (char)(rc+'0'); |
| 2575 |
if ( ( rc != 1 ) && ( rc != 0 ) ) |
| 2576 |
rxstack_send_return( client, &action, srcode, 2 ); |
| 2577 |
else |
| 2578 |
{ |
| 2579 |
rcode_streng = Str_buf( srcode, 2 ); |
| 2580 |
if ( rcode_streng == NULL ) |
| 2581 |
{ |
| 2582 |
rxstack_send_return( client, &action, "9:rxstack: out of memory", 23 ); |
| 2583 |
} |
| 2584 |
else |
| 2585 |
{ |
| 2586 |
resp_streng = Str_cat( rcode_streng, result ); |
| 2587 |
if ( resp_streng == NULL ) |
| 2588 |
{ |
| 2589 |
rxstack_send_return( client, &action, "9:rxstack: out of memory", 23 ); |
| 2590 |
DROPSTRENG( rcode_streng ); |
| 2591 |
} |
| 2592 |
else |
| 2593 |
{ |
| 2594 |
rxstack_send_return( client, &action, PSTRENGVAL( resp_streng ), PSTRENGLEN( resp_streng ) ); |
| 2595 |
DROPSTRENG( result ); |
| 2596 |
DROPSTRENG( resp_streng ); |
| 2597 |
DROPSTRENG( rcode_streng ); |
| 2598 |
} |
| 2599 |
} |
| 2600 |
} |
| 2601 |
buffer = NULL; /* consumed by rxstack_create_queue */ |
| 2602 |
break; |
| 2603 |
case RXSTACK_DELETE_QUEUE: |
| 2604 |
DEBUGDUMP(printf("WS: Delete Queue ---\n");); |
| 2605 |
/* |
| 2606 |
* Delete the queue |
| 2607 |
*/ |
| 2608 |
split_ws_payload( ws_payload, ACTION_DELETE_LEN, &queue, &buffer ); |
| 2609 |
if ( !queue |
| 2610 |
|| PSTRENGLEN( queue ) == 0 ) |
| 2611 |
rc = 6; |
| 2612 |
else |
| 2613 |
rc = rxstack_delete_queue( client, queue ); |
| 2614 |
srcode[0] = (char)(rc+'0'); |
| 2615 |
rxstack_send_return( client, &action, srcode, 2 ); |
| 2616 |
break; |
| 2617 |
default: |
| 2618 |
break; |
| 2619 |
} |
| 2620 |
if ( buffer ) |
| 2621 |
DROPSTRENG( buffer ) ; |
| 2622 |
if ( queue |
| 2623 |
&& queue != client->default_queue->name ) |
| 2624 |
DROPSTRENG( queue ) ; |
| 2625 |
} |
| 2626 |
} |
| 2627 |
|
| 2628 |
DEBUGDUMP(printf("after valid response: rcode: %d\n",rcode);); |
| 2629 |
|
| 2630 |
return rc; |
| 2631 |
} |
| 2632 |
|
| 2633 |
int rxstack_process_traditional_command( Client * client ) |
| 2634 |
{ |
| 2635 |
char cheader[RXSTACK_HEADER_SIZE]; |
| 2636 |
streng *header; |
| 2637 |
streng *buffer = NULL ; |
| 2638 |
int rc,length; |
| 2639 |
now = get_now(); |
| 2640 |
memset( cheader, 0, sizeof(cheader) ); |
| 2641 |
DEBUGDUMP(printf("\nreading from socket %d at %ld,%03d\n", client->socket, now.seconds, now.milli);); |
| 2642 |
rc = recv( client->socket, cheader, RXSTACK_HEADER_SIZE, 0 ); |
| 2643 |
if ( rc < 0 ) |
| 2644 |
{ |
| 2645 |
if ( os_errno != ECONNRESET ) |
| 2646 |
{ |
| 2647 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_READING_SOCKET, ERR_RXSTACK_READING_SOCKET_TMPL, errno_str( os_errno ) ); |
| 2648 |
} |
| 2649 |
/* |
| 2650 |
* Assume client has been lost |
| 2651 |
*/ |
| 2652 |
rxstack_delete_client( client ); |
| 2653 |
return 0 ; |
| 2654 |
} |
| 2655 |
if ( rc == 0 ) |
| 2656 |
{ |
| 2657 |
DEBUGDUMP(printf("read empty header\n");); |
| 2658 |
/* |
| 2659 |
* Assume client has been lost |
| 2660 |
*/ |
| 2661 |
rxstack_delete_client( client ); |
| 2662 |
return 0 ; |
| 2663 |
} |
| 2664 |
else if ( rc != RXSTACK_HEADER_SIZE ) |
| 2665 |
{ |
| 2666 |
DEBUGDUMP(printf("read corrupted header\n");); |
| 2667 |
/* |
| 2668 |
* Assume client has been lost |
| 2669 |
*/ |
| 2670 |
rxstack_delete_client( client ); |
| 2671 |
return 0 ; |
| 2672 |
} |
| 2673 |
DEBUGDUMP(printf("header: %.*s\n",RXSTACK_HEADER_SIZE,cheader);); |
| 2674 |
header = MakeStreng( RXSTACK_HEADER_SIZE - 1 ); |
| 2675 |
if ( header == NULL ) |
| 2676 |
{ |
| 2677 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ); |
| 2678 |
exit( ERR_STORAGE_EXHAUSTED ); |
| 2679 |
} |
| 2680 |
memcpy( PSTRENGVAL(header), cheader+1, RXSTACK_HEADER_SIZE-1 ); |
| 2681 |
header->len = RXSTACK_HEADER_SIZE-1 ; |
| 2682 |
buffer = NULL; |
| 2683 |
/* |
| 2684 |
* Convert the data length |
| 2685 |
*/ |
| 2686 |
length = REXX_X2D( header, &rc ); |
| 2687 |
if ( rc ) |
| 2688 |
{ |
| 2689 |
/* |
| 2690 |
* Errorneous number. Kill the client. |
| 2691 |
*/ |
| 2692 |
DEBUGDUMP(printf("Invalid header: <%.*s>, client killed\n", header->len, header->value);); |
| 2693 |
rxstack_send_return( client, "9", NULL, 0 ); |
| 2694 |
rxstack_delete_client( client ); |
| 2695 |
return 1; |
| 2696 |
} |
| 2697 |
|
| 2698 |
DEBUGDUMP(printf("Header: <%.*s> length: %d\n", header->len, header->value, length);); |
| 2699 |
DROPSTRENG( header ); |
| 2700 |
if ( length > 0 ) |
| 2701 |
{ |
| 2702 |
/* |
| 2703 |
* Allocate a streng big enough for the expected data |
| 2704 |
* string, based on the length just read; even if the length |
| 2705 |
* is zero |
| 2706 |
*/ |
| 2707 |
buffer = MAKESTRENG ( length ); |
| 2708 |
if ( buffer == NULL ) |
| 2709 |
{ |
| 2710 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ); |
| 2711 |
DEBUGDUMP(printf("can't buffer input of client\n");); |
| 2712 |
rxstack_delete_client( client ); |
| 2713 |
return 0; |
| 2714 |
} |
| 2715 |
rc = recv( client->socket, PSTRENGVAL(buffer), length, 0 ); |
| 2716 |
if ( rc < 0 ) |
| 2717 |
{ |
| 2718 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_READING_SOCKET, ERR_RXSTACK_READING_SOCKET_TMPL, errno_str( os_errno ) ); |
| 2719 |
} |
| 2720 |
else if ( rc == 0 ) |
| 2721 |
{ |
| 2722 |
/* |
| 2723 |
* All we can assume here is that the client has been lost |
| 2724 |
*/ |
| 2725 |
DEBUGDUMP(printf("read empty header\n");); |
| 2726 |
rxstack_delete_client( client ); |
| 2727 |
DROPSTRENG( buffer ) ; |
| 2728 |
return 0 ; |
| 2729 |
} |
| 2730 |
else |
| 2731 |
buffer->len = length ; |
| 2732 |
} |
| 2733 |
now = get_now(); |
| 2734 |
DEBUGDUMP(printf("\nbefore send_response_to_client from socket %d at %ld,%03d\n", client->socket, now.seconds, now.milli);); |
| 2735 |
send_response_to_client( client, cheader[0], buffer ); |
| 2736 |
now = get_now(); |
| 2737 |
DEBUGDUMP(printf("\nafter send_response_to_client from socket %d at %ld,%03d\n", client->socket, now.seconds, now.milli);); |
| 2738 |
return 1; |
| 2739 |
} |
| 2740 |
|
| 2741 |
/* rxstack_process_command reads a new command from the client and processes |
| 2742 |
* it. |
| 2743 |
* returns 0 if the client has been terminated, 1 if the client persists. |
| 2744 |
*/ |
| 2745 |
int rxstack_process_command( Client *client ) |
| 2746 |
{ |
| 2747 |
char pheader[RXSTACK_PEEK_HEADER_SIZE]; |
| 2748 |
int rc; |
| 2749 |
|
| 2750 |
if ( client->deadline.milli != -1 ) |
| 2751 |
{ |
| 2752 |
/* |
| 2753 |
* interrupted wait, assume the client don't want to wait for data any |
| 2754 |
* longer |
| 2755 |
*/ |
| 2756 |
bad_news_for_waiter( client->default_queue, client ) ; |
| 2757 |
} |
| 2758 |
memset( pheader, 0, sizeof(pheader) ); |
| 2759 |
now = get_now(); |
| 2760 |
DEBUGDUMP(printf("\npeeking from socket %d at %ld,%03d\n", client->socket, now.seconds, now.milli);); |
| 2761 |
rc = recv( client->socket, pheader, RXSTACK_PEEK_HEADER_SIZE, MSG_PEEK ); |
| 2762 |
if ( rc < 0 ) |
| 2763 |
{ |
| 2764 |
if ( os_errno != ECONNRESET ) |
| 2765 |
{ |
| 2766 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_READING_SOCKET, ERR_RXSTACK_READING_SOCKET_TMPL, errno_str( os_errno ) ); |
| 2767 |
} |
| 2768 |
/* |
| 2769 |
* Assume client has been lost |
| 2770 |
*/ |
| 2771 |
rxstack_delete_client( client ); |
| 2772 |
return 0 ; |
| 2773 |
} |
| 2774 |
if ( rc == 0 ) |
| 2775 |
{ |
| 2776 |
DEBUGDUMP(printf("read empty header\n");); |
| 2777 |
/* |
| 2778 |
* Assume client has been lost |
| 2779 |
*/ |
| 2780 |
rxstack_delete_client( client ); |
| 2781 |
return 0 ; |
| 2782 |
} |
| 2783 |
else if ( rc != RXSTACK_PEEK_HEADER_SIZE ) |
| 2784 |
{ |
| 2785 |
DEBUGDUMP(printf("read corrupted header\n");); |
| 2786 |
/* |
| 2787 |
* Assume client has been lost |
| 2788 |
*/ |
| 2789 |
rxstack_delete_client( client ); |
| 2790 |
return 0 ; |
| 2791 |
} |
| 2792 |
DEBUGDUMP(printf("peek at 1st two bytes of header: 0x%x%x\n", pheader[0],pheader[1]);); |
| 2793 |
/* |
| 2794 |
* We have peeked at the first 2 bytes of the message. determine what type of connection it is: |
| 2795 |
* GE - header for Websocket interface (start of GET) |
| 2796 |
* first byte < x20 - Websocket data |
| 2797 |
* anything else - assume traditional interface |
| 2798 |
*/ |
| 2799 |
if ( memcmp( pheader, "GE", 2 ) == 0 ) |
| 2800 |
{ |
| 2801 |
DEBUGDUMP(printf("have a WS header...\n");); |
| 2802 |
rc = rxstack_process_websockets_headers( client ); |
| 2803 |
} |
| 2804 |
else if ( client->isWebsocket ) |
| 2805 |
{ |
| 2806 |
DEBUGDUMP(printf("have WS data...\n");); |
| 2807 |
rc = rxstack_process_websockets_data( client ); |
| 2808 |
} |
| 2809 |
else |
| 2810 |
{ |
| 2811 |
DEBUGDUMP(printf("have normal data...\n");); |
| 2812 |
rc = rxstack_process_traditional_command( client ); |
| 2813 |
} |
| 2814 |
return rc; |
| 2815 |
} |
| 2816 |
|
| 2817 |
/* |
| 2818 |
* earlier returns the time stamp which is more early. |
| 2819 |
*/ |
| 2820 |
static RxTime earlier( RxTime t1, RxTime t2 ) |
| 2821 |
{ |
| 2822 |
if ( time_diff( t1, t2 ) == -2 ) |
| 2823 |
return t1 ; |
| 2824 |
return t2 ; |
| 2825 |
} |
| 2826 |
|
| 2827 |
/* check_for_waiting checks a client for a pending IO request. |
| 2828 |
* We currently only support PULL requests. |
| 2829 |
* The function returns immediately if the client doesn't wait. |
| 2830 |
* In the other case it checks whether the maximum wait time has been |
| 2831 |
* expired. |
| 2832 |
* If the client is still waiting and the deadline isn't reached, it |
| 2833 |
* checks if the deadline is more early then next_timeout and sets this |
| 2834 |
* value is necessary. |
| 2835 |
* |
| 2836 |
* The client's queue's deadline is set to the default deadline of queues in |
| 2837 |
* all cases. |
| 2838 |
*/ |
| 2839 |
void check_for_waiting( Client *client, RxTime *next_timeout ) |
| 2840 |
{ |
| 2841 |
int diff ; |
| 2842 |
|
| 2843 |
client->default_queue->deadline = queue_deadline ; |
| 2844 |
/* Do we are a waiter? */ |
| 2845 |
if ( client->deadline.milli == -1 ) |
| 2846 |
return ; |
| 2847 |
|
| 2848 |
/* |
| 2849 |
* Check if there is anything in the queue... |
| 2850 |
*/ |
| 2851 |
diff = time_diff( client->deadline, now ) ; |
| 2852 |
if ( ( diff != -2 ) && ( diff != 0 ) ) |
| 2853 |
{ |
| 2854 |
DEBUGDUMP( |
| 2855 |
if ( diff == -1 ) |
| 2856 |
printf("Still waiting infinitely for %d\n", client->socket ); |
| 2857 |
else |
| 2858 |
printf("Still waiting %d ms at max for %d\n", diff, client->socket ); |
| 2859 |
); |
| 2860 |
*next_timeout = earlier( *next_timeout, client->deadline ) ; |
| 2861 |
} |
| 2862 |
else |
| 2863 |
{ |
| 2864 |
bad_news_for_waiter( client->default_queue, client ) ; |
| 2865 |
} |
| 2866 |
} |
| 2867 |
|
| 2868 |
/* check_queue checks a queue has reached its deadline and if no clients |
| 2869 |
* have this queue as the default queue. The queue is deleted in this |
| 2870 |
* case. |
| 2871 |
* If the queuet is still valid and the deadline isn't reached, it |
| 2872 |
* checks if the deadline is more early then next_timeout and sets this |
| 2873 |
* value is necessary. |
| 2874 |
*/ |
| 2875 |
void check_queue( RxQueue *q, RxTime *next_timeout ) |
| 2876 |
{ |
| 2877 |
int diff ; |
| 2878 |
Client *c ; |
| 2879 |
|
| 2880 |
diff = time_diff( q->deadline, now ) ; |
| 2881 |
if ( ( diff != -2 ) && ( diff != 0 ) ) |
| 2882 |
{ |
| 2883 |
*next_timeout = earlier( *next_timeout, q->deadline ) ; |
| 2884 |
return ; |
| 2885 |
} |
| 2886 |
|
| 2887 |
for ( c = clients; c != NULL; c = c->next ) |
| 2888 |
{ |
| 2889 |
if ( c->default_queue == q ) |
| 2890 |
{ |
| 2891 |
q->deadline = queue_deadline ; |
| 2892 |
*next_timeout = earlier( *next_timeout, q->deadline ) ; |
| 2893 |
return ; |
| 2894 |
} |
| 2895 |
} |
| 2896 |
|
| 2897 |
DEBUGDUMP( printf( "Purging unused queue <%.*s>\n", PSTRENGLEN( q->name ), PSTRENGVAL( q->name ) ) ); |
| 2898 |
q->deadline = queue_deadline ; /* needed at least for SESSION */ |
| 2899 |
*next_timeout = earlier( *next_timeout, q->deadline ) ; |
| 2900 |
delete_a_queue( q ) ; |
| 2901 |
} |
| 2902 |
|
| 2903 |
int rxstack_doit( ) |
| 2904 |
{ |
| 2905 |
RxTime timeout ; |
| 2906 |
int listen_sock,msgsock; |
| 2907 |
struct sockaddr_in server,client; |
| 2908 |
#ifdef HAVE_SOCKLEN_T |
| 2909 |
socklen_t client_size ; |
| 2910 |
#else |
| 2911 |
int client_size ; |
| 2912 |
#endif |
| 2913 |
int rc; |
| 2914 |
Client *c, *ch ; |
| 2915 |
RxQueue *q, *qh ; |
| 2916 |
#if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)) |
| 2917 |
# define POLL_INCR 16 |
| 2918 |
struct pollfd *pd = NULL ; |
| 2919 |
unsigned poll_max = 0 ; |
| 2920 |
unsigned poll_cnt = 0 ; |
| 2921 |
#else |
| 2922 |
int max_sock ; |
| 2923 |
fd_set ready ; |
| 2924 |
struct timeval to ; |
| 2925 |
#endif |
| 2926 |
#ifdef BUILD_NT_SERVICE |
| 2927 |
char buf[30]; |
| 2928 |
#endif |
| 2929 |
#if defined(SO_REUSEADDR) && defined(SOL_SOCKET) |
| 2930 |
int on = 1; |
| 2931 |
#endif |
| 2932 |
|
| 2933 |
client_size = sizeof( struct sockaddr ); |
| 2934 |
#ifdef WIN32 |
| 2935 |
if ( init_external_queue( NULL ) ) |
| 2936 |
return 1; |
| 2937 |
#endif |
| 2938 |
|
| 2939 |
#ifdef BUILD_NT_SERVICE |
| 2940 |
if ( IsItNT() |
| 2941 |
&& !report_service_pending_start() ) |
| 2942 |
goto notrunning; |
| 2943 |
#endif |
| 2944 |
/* |
| 2945 |
* Set up signal handler |
| 2946 |
*/ |
| 2947 |
#ifdef SIGTERM |
| 2948 |
signal( SIGTERM, rxstack_signal_handler ); |
| 2949 |
#endif |
| 2950 |
#ifdef SIGINT |
| 2951 |
signal( SIGINT, rxstack_signal_handler ); |
| 2952 |
#endif |
| 2953 |
#ifdef SIGBREAK |
| 2954 |
signal( SIGBREAK, rxstack_signal_handler ); |
| 2955 |
#endif |
| 2956 |
#ifdef SIGPIPE |
| 2957 |
signal( SIGPIPE, SIG_IGN ); |
| 2958 |
#endif |
| 2959 |
clients = NULL ; |
| 2960 |
queues = NULL ; |
| 2961 |
|
| 2962 |
/* |
| 2963 |
* Initialise default "SESSION" queue |
| 2964 |
*/ |
| 2965 |
if ( ( SESSION = get_new_queue( ) ) == NULL ) |
| 2966 |
{ |
| 2967 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ) ; |
| 2968 |
return ERR_STORAGE_EXHAUSTED ; |
| 2969 |
} |
| 2970 |
SESSION->name = Str_cre_or_exit( "SESSION", 7 ) ; |
| 2971 |
SESSION->isReal = 1; |
| 2972 |
|
| 2973 |
#if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)) |
| 2974 |
pd = (struct pollfd *)malloc( ( poll_max = POLL_INCR ) * sizeof( struct pollfd ) ); |
| 2975 |
if ( pd == NULL ) |
| 2976 |
{ |
| 2977 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ); |
| 2978 |
exit( ERR_STORAGE_EXHAUSTED ); |
| 2979 |
} |
| 2980 |
#endif |
| 2981 |
|
| 2982 |
#ifdef BUILD_NT_SERVICE |
| 2983 |
if ( IsItNT() |
| 2984 |
&& !report_service_pending_start() ) |
| 2985 |
goto notrunning; |
| 2986 |
#endif |
| 2987 |
/* |
| 2988 |
* Create listener socket |
| 2989 |
*/ |
| 2990 |
listen_sock = socket(AF_INET, SOCK_STREAM, 0); |
| 2991 |
if (listen_sock < 0) |
| 2992 |
{ |
| 2993 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_GENERAL, ERR_RXSTACK_GENERAL_TMPL, "Listening on socket", errno_str( os_errno ) ); |
| 2994 |
rxstack_cleanup(); |
| 2995 |
exit(ERR_RXSTACK_GENERAL); |
| 2996 |
} |
| 2997 |
memset( &server, 0, sizeof(server) ); |
| 2998 |
server.sin_family = AF_INET; |
| 2999 |
if ( world ) |
| 3000 |
server.sin_addr.s_addr = htonl(INADDR_ANY); |
| 3001 |
else |
| 3002 |
server.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 3003 |
if ( portno == 0 ) |
| 3004 |
portno = default_port_number(); |
| 3005 |
server.sin_port = htons((unsigned short) portno); |
| 3006 |
|
| 3007 |
#ifdef BUILD_NT_SERVICE |
| 3008 |
if ( IsItNT() |
| 3009 |
&& !report_service_pending_start() ) |
| 3010 |
goto notrunning; |
| 3011 |
#endif |
| 3012 |
|
| 3013 |
#if defined(SO_REUSEADDR) && defined(SOL_SOCKET) |
| 3014 |
setsockopt( listen_sock, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof( on ) ); |
| 3015 |
#endif |
| 3016 |
if ( bind(listen_sock, (struct sockaddr *)&server, sizeof(server)) < 0) |
| 3017 |
{ |
| 3018 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_GENERAL, ERR_RXSTACK_GENERAL_TMPL, "Error binding socket", errno_str( os_errno ) ); |
| 3019 |
rxstack_cleanup(); |
| 3020 |
exit( ERR_RXSTACK_GENERAL ); |
| 3021 |
} |
| 3022 |
#ifdef BUILD_NT_SERVICE |
| 3023 |
sprintf(buf, "Listening on port: %ld", portno ); |
| 3024 |
if ( IsItNT() ) |
| 3025 |
{ |
| 3026 |
if ( !report_service_start() ) |
| 3027 |
goto notrunning; |
| 3028 |
if ( debug == 1 ) |
| 3029 |
{ |
| 3030 |
printf( "%s\n", buf ); |
| 3031 |
fflush(stdout); |
| 3032 |
} |
| 3033 |
else |
| 3034 |
{ |
| 3035 |
AddToMessageLog(TEXT(buf)); |
| 3036 |
} |
| 3037 |
} |
| 3038 |
else |
| 3039 |
{ |
| 3040 |
printf( "%s\n", buf ); |
| 3041 |
fflush(stdout); |
| 3042 |
} |
| 3043 |
#else |
| 3044 |
printf( "rxstack listening on port: %ld\n", portno ); |
| 3045 |
printf( "rxstack world access: %s\n", (world) ? "yes" : "no" ); |
| 3046 |
fflush(stdout); |
| 3047 |
#endif |
| 3048 |
/* |
| 3049 |
* Start accepting connections |
| 3050 |
*/ |
| 3051 |
listen(listen_sock, 5); |
| 3052 |
timeout = get_now( ) ; |
| 3053 |
time_add( &timeout, DEFAULT_WAKEUP ) ; |
| 3054 |
queue_deadline = now ; |
| 3055 |
time_add( &queue_deadline, QUEUE_TIMEOUT ) ; |
| 3056 |
while ( running ) |
| 3057 |
{ |
| 3058 |
#if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)) |
| 3059 |
poll_cnt = 0 ; |
| 3060 |
pd[ poll_cnt ].events = POLLIN ; |
| 3061 |
pd[ poll_cnt++ ].fd = listen_sock ; |
| 3062 |
DEBUGDUMP(printf("****** poll((%d", listen_sock);); |
| 3063 |
for ( c = clients; c != NULL; c = c->next ) |
| 3064 |
{ |
| 3065 |
if ( c->socket == -1 ) |
| 3066 |
{ |
| 3067 |
continue ; |
| 3068 |
} |
| 3069 |
|
| 3070 |
if ( poll_cnt == poll_max ) |
| 3071 |
{ |
| 3072 |
pd = (struct pollfd *)realloc( pd, ( poll_max += POLL_INCR ) * sizeof( struct pollfd ) ); |
| 3073 |
if ( pd == NULL ) |
| 3074 |
{ |
| 3075 |
showerror( ERR_STORAGE_EXHAUSTED, 0, ERR_STORAGE_EXHAUSTED_TMPL ); |
| 3076 |
exit( ERR_STORAGE_EXHAUSTED ); |
| 3077 |
} |
| 3078 |
} |
| 3079 |
|
| 3080 |
pd[ poll_cnt ].events = POLLIN ; |
| 3081 |
pd[ poll_cnt++ ].fd = c->socket ; |
| 3082 |
DEBUGDUMP(printf(", %d", c->socket);); |
| 3083 |
} |
| 3084 |
#else |
| 3085 |
FD_ZERO(&ready); |
| 3086 |
FD_SET(listen_sock, &ready); |
| 3087 |
DEBUGDUMP(printf("****** select((%d", listen_sock);); |
| 3088 |
max_sock = listen_sock; |
| 3089 |
/* |
| 3090 |
* For each connected client, allow its socket |
| 3091 |
* to be triggered |
| 3092 |
*/ |
| 3093 |
for ( c = clients; c != NULL; c = c->next ) |
| 3094 |
{ |
| 3095 |
if ( c->socket != -1 ) |
| 3096 |
{ |
| 3097 |
DEBUGDUMP(printf(", %d", c->socket);); |
| 3098 |
FD_SET( c->socket, &ready ); |
| 3099 |
if ( c->socket > max_sock ) |
| 3100 |
max_sock = c->socket; |
| 3101 |
} |
| 3102 |
} |
| 3103 |
#endif |
| 3104 |
now = get_now( ) ; |
| 3105 |
rc = time_diff( timeout, now ) ; |
| 3106 |
if ( rc == -2 ) |
| 3107 |
rc = 0 ; /* already timed out */ |
| 3108 |
if ( ( rc == -1 ) || ( rc > DEFAULT_WAKEUP ) ) |
| 3109 |
rc = DEFAULT_WAKEUP ; |
| 3110 |
DEBUGDUMP(printf("), to=%d) ms at %ld,%03d\n", rc, now.seconds, now.milli );); |
| 3111 |
#if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)) |
| 3112 |
rc = poll( pd, poll_cnt, rc ) ; |
| 3113 |
#else |
| 3114 |
to.tv_usec = ( rc % 1000 ) * 1000 ; /* microseconds fraction */ |
| 3115 |
to.tv_sec = rc / 1000; |
| 3116 |
rc = select( max_sock + 1, &ready, (fd_set *)0, (fd_set *)0, &to ) ; |
| 3117 |
#endif |
| 3118 |
now = get_now( ) ; |
| 3119 |
DEBUGDUMP(printf("****** after waiting(), rc=%d at %ld,%03d\n", rc, now.seconds, now.milli );); |
| 3120 |
if ( rc < 0 ) |
| 3121 |
{ |
| 3122 |
if ( os_errno != EINTR ) /* Win32 doesn't know about it ? */ |
| 3123 |
{ |
| 3124 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_GENERAL, ERR_RXSTACK_GENERAL_TMPL, "Calling select", errno_str( os_errno ) ); |
| 3125 |
exit( ERR_RXSTACK_GENERAL ); |
| 3126 |
} |
| 3127 |
continue ; |
| 3128 |
} |
| 3129 |
if ( rc ) |
| 3130 |
{ |
| 3131 |
#if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)) |
| 3132 |
if ( pd[ 0 ].revents ) |
| 3133 |
#else |
| 3134 |
if ( FD_ISSET(listen_sock, &ready ) ) |
| 3135 |
#endif |
| 3136 |
{ |
| 3137 |
msgsock = accept(listen_sock, (struct sockaddr *)&client, &client_size); |
| 3138 |
if (msgsock == -1) |
| 3139 |
{ |
| 3140 |
showerror( ERR_EXTERNAL_QUEUE, ERR_RXSTACK_GENERAL, ERR_RXSTACK_GENERAL_TMPL, "Calling listen", errno_str( os_errno ) ); |
| 3141 |
rxstack_cleanup(); |
| 3142 |
exit( ERR_RXSTACK_GENERAL ); |
| 3143 |
} |
| 3144 |
else |
| 3145 |
{ |
| 3146 |
/* |
| 3147 |
* A client has connected, create a client entry |
| 3148 |
* and set their default queue to SESSION |
| 3149 |
*/ |
| 3150 |
/* |
| 3151 |
* Validate the client here...TBD |
| 3152 |
* use details in client sockaddr struct |
| 3153 |
*/ |
| 3154 |
DEBUGDUMP(printf("Client connecting from %s has socket %d\n", inet_ntoa( client.sin_addr ), msgsock );); |
| 3155 |
rxstack_create_client( msgsock ); |
| 3156 |
} |
| 3157 |
} |
| 3158 |
else |
| 3159 |
{ |
| 3160 |
#if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)) |
| 3161 |
for ( c = clients, poll_cnt = 1; c != NULL; poll_cnt++ ) |
| 3162 |
#else |
| 3163 |
for ( c = clients; c != NULL; ) |
| 3164 |
#endif |
| 3165 |
{ |
| 3166 |
/* c might be deleted by the following calls. |
| 3167 |
* Assure we have everything perfect to use the |
| 3168 |
* next element. An access to c after rxstack_process_command |
| 3169 |
* is forbidden. |
| 3170 |
*/ |
| 3171 |
ch = c ; |
| 3172 |
c = c->next ; |
| 3173 |
#if defined(HAVE_POLL) && (defined(HAVE_POLL_H) || defined(HAVE_SYS_POLL_H)) |
| 3174 |
if ( pd[ poll_cnt ].revents ) |
| 3175 |
#else |
| 3176 |
if ( FD_ISSET( ch->socket, &ready ) ) |
| 3177 |
#endif |
| 3178 |
{ |
| 3179 |
DEBUGDUMP(printf("Client with socket %d ready for reading\n", ch->socket );); |
| 3180 |
/* |
| 3181 |
* Process the client's command... |
| 3182 |
*/ |
| 3183 |
rxstack_process_command( ch ) ; |
| 3184 |
} |
| 3185 |
} |
| 3186 |
} |
| 3187 |
} |
| 3188 |
/* |
| 3189 |
* If select() timed out or received input, check all connected clients who |
| 3190 |
* may be waiting for input on one of the queues. |
| 3191 |
* |
| 3192 |
* now contains the time between the start of select() call and now |
| 3193 |
* in milliseconds |
| 3194 |
*/ |
| 3195 |
now = get_now(); |
| 3196 |
timeout = get_now( ) ; |
| 3197 |
time_add( &timeout, DEFAULT_WAKEUP ) ; |
| 3198 |
queue_deadline = now ; |
| 3199 |
time_add( &queue_deadline, QUEUE_TIMEOUT ) ; |
| 3200 |
for ( c = clients; c != NULL; c = c->next ) |
| 3201 |
{ |
| 3202 |
check_for_waiting( c, &timeout ); |
| 3203 |
} |
| 3204 |
for ( q = queues; q != NULL; ) |
| 3205 |
{ |
| 3206 |
qh = q ; |
| 3207 |
q = q->next ; |
| 3208 |
check_queue( qh, &timeout ); |
| 3209 |
} |
| 3210 |
} |
| 3211 |
#ifdef BUILD_NT_SERVICE |
| 3212 |
notrunning: |
| 3213 |
#endif |
| 3214 |
return 0; |
| 3215 |
} |
| 3216 |
|
| 3217 |
/* |
| 3218 |
* Gives a short usage description on stderr and returns 1 |
| 3219 |
*/ |
| 3220 |
int usage( const char *argv0 ) |
| 3221 |
{ |
| 3222 |
fprintf( stdout, "\n%s: %s (%d bit). All rights reserved.\n", argv0, PARSE_VERSION_STRING, REGINA_BITS ); |
| 3223 |
fprintf( stdout,"Regina is distributed under the terms of the GNU General Library Public License \n" ); |
| 3224 |
fprintf( stdout,"and comes with NO WARRANTY. See the file COPYING-LIB for details.\n" ); |
| 3225 |
fprintf( stdout,"\n%s [switches]\n", argv0 ); |
| 3226 |
fprintf( stdout,"where switches are:\n\n" ); |
| 3227 |
fprintf( stdout," --help, -h show this message\n" ); |
| 3228 |
fprintf( stdout," --version, -v display Regina version and exit\n" ); |
| 3229 |
fprintf( stdout," --debug, -D turn on debugging\n" ); |
| 3230 |
#if defined(HAVE_FORK) |
| 3231 |
fprintf( stdout," --daemon, -d run %s as a daemon process\n", argv0 ); |
| 3232 |
#endif |
| 3233 |
fprintf( stdout," --kill, -k kill the running %s process\n", argv0 ); |
| 3234 |
fprintf( stdout," --port=portno, -pportno listen on TCP port portno\n"); |
| 3235 |
fprintf( stdout," --world, -w allow connections from anywhere. By default only connections from localhost allowed\n"); |
| 3236 |
fflush( stdout ); |
| 3237 |
return 1 ; |
| 3238 |
} |
| 3239 |
|
| 3240 |
static void checkDebug(void) |
| 3241 |
{ |
| 3242 |
if ( getenv( "RXDEBUG" ) != NULL ) |
| 3243 |
debug = 1 ; |
| 3244 |
} |
| 3245 |
|
| 3246 |
static void getDefaultsFromConfig(void) |
| 3247 |
{ |
| 3248 |
#if defined(UNIX) |
| 3249 |
/* look in /etc/rxstack.conf */ |
| 3250 |
FILE *fp; |
| 3251 |
|
| 3252 |
fp = fopen( "/etc/rxstack.conf", "r"); |
| 3253 |
if (fp == NULL) return; |
| 3254 |
read_params(fp); |
| 3255 |
#endif |
| 3256 |
} |
| 3257 |
|
| 3258 |
int runNormal( int argc, char **argv ) |
| 3259 |
{ |
| 3260 |
int rc = 0 ; |
| 3261 |
int c; |
| 3262 |
const char *argv0 = argv[ 0 ] ; |
| 3263 |
static struct my_getopt_option long_options[] = |
| 3264 |
{ |
| 3265 |
{"help", no_argument, 0, 'h' }, |
| 3266 |
{"version", no_argument, 0, 'v' }, |
| 3267 |
{"debug", no_argument, 0, 'D' }, |
| 3268 |
{"kill", no_argument, 0, 'k' }, |
| 3269 |
{"world", no_argument, 0, 'w' }, |
| 3270 |
{"port", required_argument, 0, 'p' }, |
| 3271 |
{"daemon", no_argument, 0, 'd' }, |
| 3272 |
{0, 0, 0, 0 } |
| 3273 |
}; |
| 3274 |
getDefaultsFromConfig(); |
| 3275 |
checkDebug(); |
| 3276 |
while (1) |
| 3277 |
{ |
| 3278 |
int option_index = 0; |
| 3279 |
|
| 3280 |
c = my_getopt_long( argc, argv, "+hvDdkwp:", long_options, &option_index ); |
| 3281 |
if ( c == -1 ) |
| 3282 |
break; |
| 3283 |
|
| 3284 |
switch(c) |
| 3285 |
{ |
| 3286 |
case 'h': |
| 3287 |
return usage( argv0 ); |
| 3288 |
break; |
| 3289 |
case 'D': |
| 3290 |
debug = 1; |
| 3291 |
putenv( "RXDEBUG=1" ) ; |
| 3292 |
break; |
| 3293 |
case 'd': |
| 3294 |
#ifndef BUILD_NT_SERVICE |
| 3295 |
isdaemon = 1; |
| 3296 |
#endif |
| 3297 |
break; |
| 3298 |
case 'k': |
| 3299 |
tosuicide = 1; |
| 3300 |
break; |
| 3301 |
case 'w': |
| 3302 |
world = 1; |
| 3303 |
break; |
| 3304 |
case 'p': |
| 3305 |
portno = atol( optarg ); |
| 3306 |
if ( portno == 0 |
| 3307 |
|| portno > 0xFFFF ) |
| 3308 |
{ |
| 3309 |
fprintf( stderr, "Option \"-p\" or \"--port\" requires integer between 1 and 65535.\n" ) ; |
| 3310 |
return usage( argv0 ); |
| 3311 |
} |
| 3312 |
break; |
| 3313 |
case 'v': |
| 3314 |
fprintf( stderr, "%s: %s (%d bit)\n", argv0, PARSE_VERSION_STRING, REGINA_BITS ); |
| 3315 |
return 0; |
| 3316 |
default: /* unknown switch */ |
| 3317 |
return usage( argv0 ); |
| 3318 |
break; |
| 3319 |
} |
| 3320 |
} |
| 3321 |
if ( argc > optind ) |
| 3322 |
{ |
| 3323 |
fprintf( stderr, "Extra, unknown command line argument(s).\n" ) ; |
| 3324 |
return usage( argv0 ); |
| 3325 |
} |
| 3326 |
if ( tosuicide ) |
| 3327 |
return suicide(); |
| 3328 |
#ifndef BUILD_NT_SERVICE |
| 3329 |
if ( isdaemon ) |
| 3330 |
{ |
| 3331 |
#if defined(HAVE_FORK) |
| 3332 |
if ( ( rc = fork() ) != 0 ) |
| 3333 |
exit(rc < 0); |
| 3334 |
rc = rxstack_doit(); |
| 3335 |
#else |
| 3336 |
fprintf( stderr, "Option \"-d\" or \"--daemon\" is invalid on this platform.\n" ) ; |
| 3337 |
return usage( argv0 ); |
| 3338 |
#endif |
| 3339 |
} |
| 3340 |
else |
| 3341 |
#endif |
| 3342 |
{ |
| 3343 |
rc = rxstack_doit(); |
| 3344 |
} |
| 3345 |
rxstack_cleanup(); |
| 3346 |
printf( "%s terminated.\n", argv0 ); |
| 3347 |
fflush(stdout); |
| 3348 |
return rc; |
| 3349 |
} |
| 3350 |
|
| 3351 |
#ifdef BUILD_NT_SERVICE |
| 3352 |
VOID ServiceStart(DWORD argc, LPTSTR *argv) |
| 3353 |
#else |
| 3354 |
int main(int argc, char *argv[]) |
| 3355 |
#endif |
| 3356 |
{ |
| 3357 |
#ifdef BUILD_NT_SERVICE |
| 3358 |
if ( IsItNT() ) |
| 3359 |
{ |
| 3360 |
if ( !nt_service_start() ) |
| 3361 |
{ |
| 3362 |
/* |
| 3363 |
checkDebug(); |
| 3364 |
rxstack_doit(); |
| 3365 |
*/ |
| 3366 |
runNormal(argc, argv); |
| 3367 |
} |
| 3368 |
rxstack_cleanup(); |
| 3369 |
return; |
| 3370 |
} |
| 3371 |
else |
| 3372 |
{ |
| 3373 |
runNormal(argc, argv); |
| 3374 |
} |
| 3375 |
#else |
| 3376 |
return runNormal( argc, argv ); |
| 3377 |
#endif |
| 3378 |
} |