| 1 |
/* Sclose.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 |
* Sclose.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 "sockets.h" |
| 17 |
|
| 18 |
#ifdef unix |
| 19 |
# include <unistd.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
/* ------------------------------------------------------------------------- |
| 23 |
* Local Definitions |
| 24 |
*/ |
| 25 |
#define TIMEOUT 20L |
| 26 |
|
| 27 |
/* ------------------------------------------------------------------------- */ |
| 28 |
|
| 29 |
/* Sclose: this function closes a socket. Closing a socket is easy, but |
| 30 |
* the PortMaster also needs to be informed |
| 31 |
*/ |
| 32 |
#ifdef __PROTOTYPE__ |
| 33 |
void Sclose(Socket *skt) |
| 34 |
#else |
| 35 |
void Sclose(skt) |
| 36 |
Socket *skt; |
| 37 |
#endif |
| 38 |
{ |
| 39 |
int trycnt; |
| 40 |
Socket *pmskt = NULL; |
| 41 |
SKTEVENT mesg; |
| 42 |
SKTEVENT port; |
| 43 |
|
| 44 |
|
| 45 |
if(!skt) { /* return immediately */ |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
/* --- PortMaster Interface --- */ |
| 50 |
if(skt->type == PM_SERVER && skt->sktname && skt->sktname[0]) { /* Servers Only */ |
| 51 |
pmskt = Sopen_clientport(skt->hostname,"SktClose",PORTMASTER); |
| 52 |
if(!pmskt) { |
| 53 |
} |
| 54 |
|
| 55 |
trycnt= 0; |
| 56 |
do { |
| 57 |
mesg= htons(PM_CLOSE); |
| 58 |
Swrite(pmskt,(char *) &mesg,sizeof(mesg)); |
| 59 |
if(Stimeoutwait(pmskt,TIMEOUT,0L) < 0) goto ShutDown; |
| 60 |
if(Sreadbytes(pmskt,(char *) &mesg,sizeof(mesg)) <= 0) { |
| 61 |
shutdown(skt->skt,2); |
| 62 |
close(skt->skt); |
| 63 |
return; |
| 64 |
} |
| 65 |
mesg= ntohs(mesg); |
| 66 |
|
| 67 |
/* only allow PM_MAXTRY attempts to communicate with the PortMaster */ |
| 68 |
if(++trycnt >= PM_MAXTRY) { |
| 69 |
shutdown(pmskt->skt,2); |
| 70 |
close(pmskt->skt); |
| 71 |
shutdown(skt->skt,2); |
| 72 |
close(skt->skt); |
| 73 |
return; |
| 74 |
} |
| 75 |
} while(mesg != PM_OK); |
| 76 |
|
| 77 |
port= htons(skt->port); |
| 78 |
Swrite(pmskt,(char *) &port,sizeof(port)); |
| 79 |
|
| 80 |
/* read PortMaster's response (should be PM_OK) */ |
| 81 |
if(Stimeoutwait(pmskt,TIMEOUT,0L) < 0) goto ShutDown; |
| 82 |
Sreadbytes(pmskt,(char *) &mesg,sizeof(mesg)); |
| 83 |
mesg= ntohs(mesg); |
| 84 |
shutdown(pmskt->skt,2); |
| 85 |
close(pmskt->skt); |
| 86 |
} /* end of PortMaster Interface */ |
| 87 |
|
| 88 |
else { /* Handle non-server sockets (clients, accepts) */ |
| 89 |
mesg= PM_OK; |
| 90 |
} |
| 91 |
|
| 92 |
/* --- Close the Socket --- */ |
| 93 |
ShutDown: |
| 94 |
shutdown(skt->skt,2); |
| 95 |
close(skt->skt); |
| 96 |
freeSocket(skt); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
/* --------------------------------------------------------------------- |
| 101 |
* vim: ts=4 |
| 102 |
*/ |