summaryrefslogtreecommitdiffstats
path: root/xpa/xpamb.c
blob: 4e58fb562ec59366e62c555237d2bd6cac8c197e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
 *	Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
 */

#include <time.h>
#include <xpap.h>

#define XPAMB_CLASS "XPAMB"
#define XPAMB_NAME "xpamb"
#define XPAMB_MODE NULL
#define XPAMB_HELP "xpa message bus:\n"

/* message bus structure */
typedef struct mbrec{
  struct mbrec *next;
  char *name;
  char *info;
  char *buf;
  size_t len;
  time_t t;
} *MB, MBRec;

/* this is the head of the message bus list -- too lazy to do anything more */
static MB mbhead=NULL;

/* global error message buffer */
static char errbuf[SZ_LINE];
static int maxhosts=XPA_MAXHOSTS;
static XPA xpa=NULL;

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPAReceiveExit
 *
 * Purpose:	exit program
 *
 * Returns:	NONE
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
XPAReceiveExit (void *client_data, void *call_data, char *paramlist,
		char *buf, size_t len)
#else
static int XPAReceiveExit(client_data, call_data, paramlist, buf, len)
     void *client_data;
     void *call_data;
     char *paramlist;
     char *buf;
     size_t len;
#endif
{
  /* freeing the xpa handle will cause the xpa loop to terminate */
  XPAFree(xpa);
  return(0);
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	XPASendExit
 *
 * Purpose:	exit program
 *
 * Returns:	NONE
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
XPASendExit (void *client_data, void *call_data, char *paramlist,
	    char **buf, size_t *len)
#else
static int XPASendExit(client_data, call_data, paramlist, buf, len)
     void *client_data;
     void *call_data;
     char *paramlist;
     char **buf;
     size_t *len;
#endif
{
  /* freeing the xpa handle will cause the xpa loop to terminate */
  XPAFree(xpa);
  return(0);
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	MBLookup
 *
 * Purpose:	lookup anentry by name
 *
 * Returns: 	mb struct on success, NULL on failure
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static MB 
MBLookup (char *name)
#else
static MB MBLookup(name)
     char *name;
#endif
{
  MB cur;

  /*make sure we have something to work with */
  if( (name == NULL) || (*name == '\0') )
    return(NULL);

  /*  look for exact match */
  for(cur=mbhead; cur!=NULL; cur=cur->next){
    if( !strcmp(name, cur->name) ){
      return(cur);
    }
  }
  return(NULL);
}

/*
 *---------------------------------------------------------------------------
 *
 * Routine: 	MBDel
 *
 * Purpose: 	free up and remove an MB
 *
 * Results:	0 on success, -1 for failure
 *
 *---------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
MBDel (MB mb)
#else
static int MBDel(mb)
     MB mb;
#endif
{
  MB cur;

  if( mb == NULL ){
    snprintf(errbuf, SZ_LINE, "can't delete invalid xpamb entry");
    return(-1);
  }

  /* remove from list of MB's */
  if( mbhead ){
    if( mbhead == mb ){
      mbhead = mbhead->next;
    }
    else{
      for(cur=mbhead; cur!=NULL; cur=cur->next){
	if( cur->next == mb ){
	  cur->next = (cur->next)->next;
	  break;
	}
      }
    }
  }

  /* free up alloc'ed space */
  if( mb->name ) xfree(mb->name);
  if( mb->info ) xfree(mb->info);
  if( mb->buf )  xfree(mb->buf);

  /* free up record struct */
  xfree((char *)mb);
  return(0);
}

/*
 *---------------------------------------------------------------------------
 *
 * Routine: 	MBRemove
 *
 * Purpose: 	free up and remove an MB by name
 *
 * Results:	0 on success, -1 for failure
 *
 *---------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
MBRemove (char *name)
#else
static int MBRemove(name)
     char *name;
#endif
{
  MB mb;

  if( (mb=MBLookup(name)) != NULL )
    return(MBDel(mb));
  else{
    snprintf(errbuf, SZ_LINE, "can't delete unknown xpamb entry: %s", name);
    return(-1);
  }
}

/*
 *----------------------------------------------------------------------------
 *
 * Routine:	MBAdd
 *
 * Purpose:	add one MB to the xpa MB list
 *
 * Returns: 	0 on success, -1 on failure
 *
 *----------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
MBAdd (char *name, char *info, char *buf, size_t len, int replace)
#else
static int MBAdd(name, info, buf, len, replace)
     char *name;
     char *info;
     char *buf;
     size_t len;
     int replace;
#endif
{
  MB xnew=NULL;
  MB cur;

  /* see if this entry already exists */
  if( (cur = MBLookup(name)) != NULL ){
    if( !replace ){
      snprintf(errbuf, SZ_LINE, "xpamb entry already exists: %s", name);
      goto error;
    }
    else
      MBDel(cur);
  }

  /* allocate MB struct */
  if( (xnew = (MB)xcalloc(1, sizeof(MBRec))) == NULL ){
    snprintf(errbuf, SZ_LINE, "can't allocate memory for xpamb entry: %s",
	     name);
    goto error;
  }

  /* fill in the blanks */
  xnew->name = xstrdup(name);
  xnew->info = xstrdup(info);
  if( !(xnew->buf = (char *)xmalloc(len)) ){
    snprintf(errbuf, SZ_LINE, "can't allocate memory for xpamb buffer: %s",
	     name);
    goto error;
  }
  memcpy(xnew->buf, buf, len);
  xnew->len = len;
  xnew->t = time(NULL);

  /* add this MB to end of list of MB's */
  if( mbhead == NULL ){
    mbhead = xnew;
  }
  else{
    for(cur=mbhead; cur->next!=NULL; cur=cur->next)
      ;
    cur->next = xnew;
  }
  return(0);

error:
  if( xnew )
    xfree(xnew);
  return(-1);
}

/*
 *---------------------------------------------------------------------------
 *
 * Routine: 	MBInfo
 *
 * Purpose: 	send info string to specified fd
 *
 * Results:	0 on success, -1 for failure
 *
 *---------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
MBInfo (int fd, MB mb)
#else
static int MBInfo(fd, mb)
     int fd;
     MB mb;
#endif
{
  MB cur;
  char *tbuf;

  if( mb != NULL ){
    tbuf = (char *)xmalloc(strlen(mb->name) + SZ_LINE);
    snprintf(tbuf, SZ_LINE, "%s\tsize:\t\t%d\n\tcreated:\t%s",
	     mb->name, (int)mb->len, ctime(&(mb->t)));
    send(fd, tbuf, strlen(tbuf), 0);
    xfree(tbuf);
    if( mb->info && *(mb->info) ){
      tbuf = (char *)xmalloc(strlen(mb->info) + SZ_LINE);
      snprintf(tbuf, SZ_LINE, "\tinfo:\t\t%s\n", mb->info);
      send(fd, tbuf, strlen(tbuf), 0);
      xfree(tbuf);
    }
  }
  else{
    /*  send info for all entries */
    for(cur=mbhead; cur!=NULL; cur=cur->next){
      MBInfo(fd, cur);
    }
  }
  return(0);
}

/*
 *---------------------------------------------------------------------------
 *
 * Routine: 	MBData
 *
 * Purpose: 	send data to specified fd
 *
 * Results:	0 on success, -1 for failure
 *
 *---------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
MBData (int fd, MB mb)
#else
static int MBData(fd, mb)
     int fd;
     MB mb;
#endif
{
  if( mb != NULL ){
    if( mb->buf && mb->len ){
      if( send(fd, mb->buf, mb->len, 0) == mb->len ){
	return(0);
      }
      else{
	snprintf(errbuf, SZ_LINE, "writing data for xpamb entry: %s",
		 mb->name);
	return(-1);
      }
    }
    else{
      return(0);
    }
  }
  return(0);
}

/*
 *---------------------------------------------------------------------------
 *
 * Routine: 	MBSendCB
 *
 * Purpose: 	callback when we need to send data to a client
 *
 * Results:	0 on success, -1 for failure
 *
 *---------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
MBSendCB (void *client_data, void *call_data, char *paramlist,
	 char **buf, size_t *len)
#else
static int MBSendCB(client_data, call_data, paramlist, buf, len)
     void *client_data;
     void *call_data;
     char *paramlist;
     char **buf;
     size_t *len;
#endif
{
  XPA xpa = (XPA)call_data;
  MB mb;
  int i;
  int got=0;
  int tp=0;
  int doinfo=0;
  int dodata=0;
  int status=0;
  int fds[1];
  char tbuf[SZ_LINE];
  char *errs[XPA_MAXHOSTS];
  char *names[XPA_MAXHOSTS];

  /* reset error buffer */
  *errbuf = '\0';

  /* no paramlist means return info on all stored entries */
  if( !paramlist || !*paramlist ){
    return(MBInfo(xpa_datafd(xpa), NULL));
  }

  /* process switches */
  while( word(paramlist, tbuf, &tp) ){
    if( *tbuf != '-' )
      break;
    else if( !strcmp(tbuf, "-data") ){
      dodata++;
    }
    else if( !strcmp(tbuf, "-info") ){
      doinfo++;
    }
    else{
      break;
    }
  }

  if( doinfo || dodata ){
    if( !*tbuf ){
      snprintf(errbuf, SZ_LINE, "missing xpamb entry name");
      status = -1;
    }
    if( (mb=MBLookup(tbuf)) != NULL ){
      if( doinfo ){
	status = MBInfo(xpa_datafd(xpa), mb);
      }
      if( dodata ){
	status = MBData(xpa_datafd(xpa), mb);
      }
    }
    else{
      snprintf(errbuf, SZ_LINE, "unknown xpamb entry: %s", tbuf);
      status = -1;
    }
  }
  else{
    fds[0] = xpa_datafd(xpa);
    got = XPAGetFd(NULL, tbuf, &paramlist[tp], NULL, fds, names, errs,
		   -maxhosts);
    for(i=0; i<got; i++){
      if( errs[i] ){
	if( !*errbuf )
	  strcpy(errbuf, errs[i]);
	status = -1;
	xfree(errs[i]);
      }
      if( names[i] ) xfree(names[i]);
    }
  }

  /* send error message and return status */
  if( *errbuf )
    XPAError(xpa, errbuf);
  return(status);
}

/*
 *---------------------------------------------------------------------------
 *
 * Routine: 	MBRecCB
 *
 * Purpose: 	callback when we receive data/command from a client
 *
 * Results:	0 on success, -1 for failure
 *
 *---------------------------------------------------------------------------
 */
#ifdef ANSI_FUNC
static int 
MBRecCB (void *client_data, void *call_data, char *paramlist,
	    char *buf, size_t len)
#else
static int MBRecCB(client_data, call_data, paramlist, buf, len)
     void *client_data;
     void *call_data;
     char *paramlist;
     char *buf;
     size_t len;
#endif
{
  XPA xpa = (XPA)call_data;
  MB mb;
  int i;
  int got=0;
  int tp=0;
  int rflag=0;
  int status=0;
  char tbuf[SZ_LINE];
  char xsend[SZ_LINE];
  char xadd[SZ_LINE];
  char xinfo[SZ_LINE];
  char xremove[SZ_LINE];
  char xtemplate[SZ_LINE];
  char *errs[XPA_MAXHOSTS];
  char *names[XPA_MAXHOSTS];

  /* init flags */
  *xsend = '\0';
  *xadd = '\0';
  *xinfo = '\0';
  *xremove = '\0';
  /* reset error buffer */
  *errbuf = '\0';

  /* process switches */
  while( word(paramlist, tbuf, &tp) ){
    if( *tbuf != '-' )
      break;
    else if( !strcmp(tbuf, "-data") ){
      word(paramlist, xadd, &tp);
    }
    else if( !strcmp(tbuf, "-add") ){
      word(paramlist, xadd, &tp);
    }
    else if( !strcmp(tbuf, "-replace") ){
      word(paramlist, xadd, &tp);
      rflag = 1;
    }
    else if( !strcmp(tbuf, "-info") ){
      word(paramlist, xinfo, &tp);
    }
    else if( !strncmp(tbuf, "-del", 4) ){
      word(paramlist, xremove, &tp);
    }
    else if( !strcmp(tbuf, "-send") ){
      word(paramlist, xsend, &tp);
    }
    else
      break;
  }

  /* broadcast data, if we have a target
     the first non-switch word we found previously is the target */
  if( *tbuf ){
    strcpy(xtemplate, tbuf);
    /* the rest of the input string is the paramlist */
    paramlist = &(paramlist[tp]);
    /* send named data, if necessary */
    if( *xsend != '\0' ){
      if( (mb=MBLookup(xsend)) != NULL ){
	got = XPASet(NULL, xtemplate, paramlist, NULL, mb->buf, mb->len,
		     names, errs, maxhosts);
      }
      if( (buf != NULL) && (len>0) ){
	got = XPASet(NULL, xtemplate, paramlist, NULL, buf, len,
		     names, errs, maxhosts);
      }
    }
    else{
      got = XPASet(NULL, xtemplate, paramlist, NULL, buf, len,
		   names, errs, maxhosts);
    }
    for(i=0; i<got; i++){
      if( errs[i] ){
	if( !*errbuf )
	  strcpy(errbuf, errs[i]);
	status = -1;
	xfree(errs[i]);
      }
      if( names[i] ) xfree(names[i]);
    }
  }

  /* save named data, if necessary */
  if( *xadd !='\0' ){
    status = MBAdd(xadd, xinfo, buf, len, rflag);
  }

  /* remove named data, if necessary */
  if( *xremove !='\0' ){
    status = MBRemove(xremove);
  }

  /* send error message and return status */
  if( *errbuf )
    XPAError(xpa, errbuf);
  return(status);
}

#ifdef ANSI_FUNC
int 
main (int argc, char **argv)
#else
int main(argc, argv)
     int argc;
     char **argv;
#endif
{
  char *s;

  /* display version and exit, if necessary */
  if( (argc == 2) && !strcmp(argv[1], "--version") ){
    fprintf(stderr, "%s\n", XPA_VERSION);
    exit(0);
  }

  /* set global value for max hosts */
  maxhosts=XPA_MAXHOSTS;
  if( (s=(char *)getenv("XPA_MAXHOSTS")) != NULL )
    maxhosts = atoi(s);

  /* start up the xpa access point */
  if( !(xpa=XPANew(XPAMB_CLASS, XPAMB_NAME, XPAMB_HELP,
		   MBSendCB, NULL, XPAMB_MODE, MBRecCB, NULL, XPAMB_MODE)) ){
    fprintf(stderr, "XPA$ERROR: could not create xpamb access point\n");
    exit(1);
  }
  /* allow for a graceful exit */
  XPACmdAdd(XPAGetReserved (), "-exit", "\texit program",
	  XPASendExit, NULL, NULL, XPAReceiveExit, NULL, "fillbuf=false");

  /* process events */
  XPAMainLoop();
  return(0);
}