| 1 |
/* Stest.c: this file contains code which allows one to test if data |
| 2 |
* is present via the socket and not be blocked (not held, not made to sleep) |
| 3 |
* |
| 4 |
* Returns: number of bytes of data awaiting perusal (which may be 0) |
| 5 |
* or EOF if unable to "select" the socket |
| 6 |
* or select returns 1 but nothing is on socket |
| 7 |
* |
| 8 |
* Authors: Charles E. Campbell, Jr. |
| 9 |
* Terry McRoberts |
| 10 |
* Copyright: Copyright (C) 1999-2005 Charles E. Campbell, Jr. {{{1 |
| 11 |
* Permission is hereby granted to use and distribute this code, |
| 12 |
* with or without modifications, provided that this copyright |
| 13 |
* notice is copied with it. Like anything else that's free, |
| 14 |
* Stest.c is provided *as is* and comes with no warranty |
| 15 |
* of any kind, either expressed or implied. By using this |
| 16 |
* software, you agree that in no event will the copyright |
| 17 |
* holder be liable for any damages resulting from the use |
| 18 |
* of this software. |
| 19 |
* Date: Aug 22, 2005 |
| 20 |
*/ |
| 21 |
#include <stdio.h> |
| 22 |
#define SSLNEEDTIME |
| 23 |
#include "sockets.h" |
| 24 |
|
| 25 |
/* --------------------------------------------------------------------- |
| 26 |
* Source Code: |
| 27 |
*/ |
| 28 |
|
| 29 |
#ifdef __PROTOTYPE__ |
| 30 |
int Stest(Socket *skt) |
| 31 |
#else |
| 32 |
int Stest(skt) |
| 33 |
Socket *skt; |
| 34 |
#endif |
| 35 |
{ |
| 36 |
fd_set emask; |
| 37 |
fd_set rmask; |
| 38 |
fd_set wmask; |
| 39 |
int ret; |
| 40 |
short result; |
| 41 |
static char buf[PM_BIGBUF]; |
| 42 |
struct timeval timeout; |
| 43 |
|
| 44 |
|
| 45 |
/* sanity check */ |
| 46 |
if(!skt) { |
| 47 |
return -1; |
| 48 |
} |
| 49 |
|
| 50 |
FD_ZERO(&rmask); |
| 51 |
FD_SET(skt->skt,&rmask); |
| 52 |
FD_ZERO(&wmask); |
| 53 |
FD_ZERO(&emask); |
| 54 |
|
| 55 |
timeout.tv_sec = 0; |
| 56 |
timeout.tv_usec= 0; |
| 57 |
|
| 58 |
#ifdef SSLNOPEEK |
| 59 |
result = select(skt->skt+1,rmask.fds_bits,wmask.fds_bits,emask.fds_bits,&timeout); |
| 60 |
#else |
| 61 |
result = select(skt->skt+1,&rmask,&wmask,&emask,&timeout); |
| 62 |
#endif /* SSLNOPEEK */ |
| 63 |
|
| 64 |
if(result < 0) { |
| 65 |
return EOF; |
| 66 |
} |
| 67 |
|
| 68 |
/* server sockets return the select result (client awaiting connection) */ |
| 69 |
if(skt->type == PM_SERVER) { |
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
#ifdef SSLNOPEEK |
| 74 |
|
| 75 |
if(FD_ISSET(skt->skt,&rmask)) { |
| 76 |
return 1; |
| 77 |
} |
| 78 |
|
| 79 |
#else /* #ifdef SSLNOPEEK ... #else ... #endif */ |
| 80 |
|
| 81 |
/* test if message available from socket, return qty bytes avail */ |
| 82 |
|
| 83 |
if(FD_ISSET(skt->skt,&rmask)) { |
| 84 |
ret= recv(skt->skt,buf,PM_BIGBUF-1,MSG_PEEK); |
| 85 |
|
| 86 |
/* return error indication when select returned a result of 1 |
| 87 |
* (indicating that *something* is on the socket) |
| 88 |
* and recv indicates that *nothing* is on the socket |
| 89 |
*/ |
| 90 |
if(result == 1 && ret == 0) ret= EOF; |
| 91 |
return ret; |
| 92 |
} |
| 93 |
|
| 94 |
#endif /* #ifdef SSLNOPEEK ... #else ... #endif */ |
| 95 |
|
| 96 |
/* socket is empty */ |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
/* --------------------------------------------------------------------- |
| 101 |
* vim: ts=4 |
| 102 |
*/ |