| 1 |
/* Srmsrvr.c: this file contains Socket library support |
| 2 |
* Authors: Charles E. Campbell, Jr. |
| 3 |
* Terry McRoberts |
| 4 |
* Copyright: Copyright (C) 1999-2005 Charles E. Campbell, Jr. {{{1 |
| 5 |
* Permission is hereby granted to use and distribute this code, |
| 6 |
* with or without modifications, provided that this copyright |
| 7 |
* notice is copied with it. Like anything else that's free, |
| 8 |
* Srmsrvr.c is provided *as is* and comes with no warranty |
| 9 |
* of any kind, either expressed or implied. By using this |
| 10 |
* software, you agree that in no event will the copyright |
| 11 |
* holder be liable for any damages resulting from the use |
| 12 |
* of this software. |
| 13 |
* Date: Aug 22, 2005 |
| 14 |
*/ |
| 15 |
#include <stdio.h> |
| 16 |
#include <unistd.h> |
| 17 |
#include "xtdio.h" |
| 18 |
#include "sockets.h" |
| 19 |
|
| 20 |
/* ------------------------------------------------------------------------- |
| 21 |
* Local Definitions: |
| 22 |
*/ |
| 23 |
#define BUFSIZE 128 |
| 24 |
#define TIMEOUT 20L |
| 25 |
|
| 26 |
/* ------------------------------------------------------------------------- */ |
| 27 |
|
| 28 |
/* Srmsrvr: this function removes a server from the PortMaster's table |
| 29 |
* It only needs a server name. |
| 30 |
* |
| 31 |
* Returns: PM_OK if it worked |
| 32 |
* PM_SORRY otherwise |
| 33 |
*/ |
| 34 |
#ifdef __PROTOTYPE__ |
| 35 |
SKTEVENT Srmsrvr(char *skthost) |
| 36 |
#else |
| 37 |
SKTEVENT Srmsrvr(skthost) |
| 38 |
char *skthost; |
| 39 |
#endif |
| 40 |
{ |
| 41 |
char *at = NULL; |
| 42 |
char *hostname = NULL; |
| 43 |
int trycnt; |
| 44 |
Socket *pmskt = NULL; |
| 45 |
static char localhost[BUFSIZE] = ""; |
| 46 |
SKTEVENT mesg; |
| 47 |
|
| 48 |
|
| 49 |
/* parse skthost into sktname and hostname strings */ |
| 50 |
at= strchr(skthost,'@'); |
| 51 |
if(!at) { /* fill in missing hostname with current host */ |
| 52 |
char *pmshare= NULL; |
| 53 |
/* if sharing a PortMaster and no host was specified, use the PMSHARE-specified |
| 54 |
* host (if PMSHARE exists), otherwise use localhost |
| 55 |
*/ |
| 56 |
pmshare= getenv("PMSHARE"); |
| 57 |
if(pmshare) hostname= pmshare; |
| 58 |
else { |
| 59 |
if(!localhost[0]) gethostname(localhost,BUFSIZE); |
| 60 |
hostname= localhost; |
| 61 |
} |
| 62 |
pmskt= Sopen_clientport(hostname,"SktRmSrvr",PORTMASTER); |
| 63 |
} |
| 64 |
|
| 65 |
else { /* use hostname specified in skthost */ |
| 66 |
skthost[(int) (at - skthost)]= '\0'; |
| 67 |
hostname= skthost + ((int) (at - skthost)) + 1; |
| 68 |
pmskt = Sopen_clientport(hostname,"SktRmSrvr",PORTMASTER); |
| 69 |
} |
| 70 |
|
| 71 |
/* --- PortMaster Interface --- */ |
| 72 |
if(!pmskt) { |
| 73 |
if(at) skthost[(int) (at - skthost)]= '@'; |
| 74 |
return PM_SORRY; |
| 75 |
} |
| 76 |
|
| 77 |
trycnt= 0; |
| 78 |
do { |
| 79 |
mesg= htons(PM_RMSERVER); |
| 80 |
Swrite(pmskt,(char *) &mesg,sizeof(mesg)); |
| 81 |
|
| 82 |
/* don't wait forever... */ |
| 83 |
if(Stimeoutwait(pmskt,TIMEOUT,0L) < 0) { |
| 84 |
shutdown(pmskt->skt,2); |
| 85 |
close(pmskt->skt); |
| 86 |
freeSocket(pmskt); |
| 87 |
return PM_SORRY; |
| 88 |
} |
| 89 |
|
| 90 |
/* read PortMaster's response */ |
| 91 |
if(Sreadbytes(pmskt,(char *) &mesg,sizeof(mesg)) <= 0) { |
| 92 |
if(at) skthost[(int) (at - skthost)]= '@'; |
| 93 |
shutdown(pmskt->skt,2); |
| 94 |
close(pmskt->skt); |
| 95 |
return PM_SORRY; |
| 96 |
} |
| 97 |
mesg= ntohs(mesg); |
| 98 |
|
| 99 |
/* only allow PM_MAXTRY attempts to communicate with the PortMaster */ |
| 100 |
if(++trycnt >= PM_MAXTRY) { |
| 101 |
if(at) skthost[(int) (at - skthost)]= '@'; |
| 102 |
shutdown(pmskt->skt,2); |
| 103 |
close(pmskt->skt); |
| 104 |
return PM_SORRY; |
| 105 |
} |
| 106 |
} while(mesg != PM_OK); |
| 107 |
|
| 108 |
Sputs(skthost,pmskt); |
| 109 |
|
| 110 |
/* read PortMaster's response (should be PM_OK) */ |
| 111 |
Sreadbytes(pmskt,(char *) &mesg,sizeof(mesg)); |
| 112 |
shutdown(pmskt->skt,2); |
| 113 |
close(pmskt->skt); |
| 114 |
mesg= ntohs(mesg); |
| 115 |
|
| 116 |
/* restore skthost string */ |
| 117 |
if(at) skthost[(int) (at - skthost)]= '@'; |
| 118 |
|
| 119 |
return mesg; |
| 120 |
} |
| 121 |
|
| 122 |
/* --------------------------------------------------------------------- |
| 123 |
* vim: ts=4 |
| 124 |
*/ |