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
|
/* Srmsrvr.c: this file contains Socket library support */
#include <stdio.h>
#include "xtdio.h"
#include "sockets.h"
/* -------------------------------------------------------------------------
* Local Definitions:
*/
#define BUFSIZE 128
#define TIMEOUT 20L
/* ------------------------------------------------------------------------- */
/* Srmsrvr: this function removes a server from the PortMaster's table
* It only needs a server name.
*
* Returns: PM_OK if it worked
* PM_SORRY otherwise
*/
#ifdef __PROTOTYPE__
SKTEVENT Srmsrvr(char *skthost)
#else
SKTEVENT Srmsrvr(skthost)
char *skthost;
#endif
{
char *at = NULL;
char *hostname = NULL;
int trycnt;
Socket *pmskt = NULL;
static char localhost[BUFSIZE] = "";
SKTEVENT mesg;
/* parse skthost into sktname and hostname strings */
at= strchr(skthost,'@');
if(!at) { /* fill in missing hostname with current host */
char *pmshare= NULL;
/* if sharing a PortMaster and no host was specified, use the PMSHARE-specified
* host (if PMSHARE exists), otherwise use localhost
*/
pmshare= getenv("PMSHARE");
if(pmshare) hostname= pmshare;
else {
if(!localhost[0]) gethostname(localhost,BUFSIZE);
hostname= localhost;
}
pmskt= Sopen_clientport(hostname,"SktRmSrvr",PORTMASTER);
}
else { /* use hostname specified in skthost */
skthost[(int) (at - skthost)]= '\0';
hostname= skthost + ((int) (at - skthost)) + 1;
pmskt = Sopen_clientport(hostname,"SktRmSrvr",PORTMASTER);
}
/* --- PortMaster Interface --- */
if(!pmskt) {
if(at) skthost[(int) (at - skthost)]= '@';
return PM_SORRY;
}
trycnt= 0;
do {
mesg= htons(PM_RMSERVER);
Swrite(pmskt,(char *) &mesg,sizeof(mesg));
/* don't wait forever... */
if(Stimeoutwait(pmskt,TIMEOUT,0L) < 0) {
shutdown(pmskt->skt,2);
close(pmskt->skt);
freeSocket(pmskt);
return PM_SORRY;
}
/* read PortMaster's response */
if(Sreadbytes(pmskt,(char *) &mesg,sizeof(mesg)) <= 0) {
if(at) skthost[(int) (at - skthost)]= '@';
shutdown(pmskt->skt,2);
close(pmskt->skt);
return PM_SORRY;
}
mesg= ntohs(mesg);
/* only allow PM_MAXTRY attempts to communicate with the PortMaster */
if(++trycnt >= PM_MAXTRY) {
if(at) skthost[(int) (at - skthost)]= '@';
shutdown(pmskt->skt,2);
close(pmskt->skt);
return PM_SORRY;
}
} while(mesg != PM_OK);
Sputs(skthost,pmskt);
/* read PortMaster's response (should be PM_OK) */
Sreadbytes(pmskt,(char *) &mesg,sizeof(mesg));
shutdown(pmskt->skt,2);
close(pmskt->skt);
mesg= ntohs(mesg);
/* restore skthost string */
if(at) skthost[(int) (at - skthost)]= '@';
return mesg;
}
/* ---------------------------------------------------------------------
* vim: ts=4
*/
|