| 1 |
/* Stimeoutwait.c: this file contains code which allows one to wait until data |
| 2 |
* is present at the Socket (this will block) |
| 3 |
* |
| 4 |
* Returns: number of bytes of data awaiting perusal |
| 5 |
* or -1 if unable to "select" the socket |
| 6 |
* or -2 if timeout occurs |
| 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 |
* Stimeoutwait.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 Stimeoutwait(Socket *skt,long seconds,long useconds) |
| 31 |
#else |
| 32 |
int Stimeoutwait(skt,seconds,useconds) |
| 33 |
Socket *skt; |
| 34 |
long seconds; |
| 35 |
long useconds; |
| 36 |
#endif |
| 37 |
{ |
| 38 |
short result; |
| 39 |
int ret; |
| 40 |
fd_set emask; |
| 41 |
fd_set rmask; |
| 42 |
fd_set wmask; |
| 43 |
struct timeval timeout; |
| 44 |
static char buf[PM_BIGBUF]; |
| 45 |
|
| 46 |
|
| 47 |
/* sanity check */ |
| 48 |
if(!skt) { |
| 49 |
return -1; |
| 50 |
} |
| 51 |
|
| 52 |
FD_ZERO(&rmask); |
| 53 |
FD_SET(skt->skt,&rmask); |
| 54 |
FD_ZERO(&wmask); |
| 55 |
FD_ZERO(&emask); |
| 56 |
|
| 57 |
/* test if something is available for reading on the socket. This form |
| 58 |
* will block (sleep) until something arrives |
| 59 |
*/ |
| 60 |
timeout.tv_sec = seconds; |
| 61 |
timeout.tv_usec= useconds; |
| 62 |
|
| 63 |
#ifdef SSLNOPEEK |
| 64 |
|
| 65 |
|
| 66 |
result= select(skt->skt+1, |
| 67 |
rmask.fds_bits,wmask.fds_bits,emask.fds_bits, &timeout); |
| 68 |
|
| 69 |
#else /* #ifdef SSLNOPEEK ... #else ... #endif */ |
| 70 |
|
| 71 |
result= select(skt->skt+1, &rmask,&wmask,&emask, &timeout); |
| 72 |
#endif /* #ifdef SSLNOPEEK ... #else ... #endif */ |
| 73 |
|
| 74 |
/* socket error */ |
| 75 |
if(result < 0) { |
| 76 |
return -1; |
| 77 |
} |
| 78 |
|
| 79 |
/* timed out */ |
| 80 |
else if(result == 0) { |
| 81 |
return -2; |
| 82 |
} |
| 83 |
|
| 84 |
/* server sockets return the select result */ |
| 85 |
if(skt->type == PM_SERVER) { |
| 86 |
return result; |
| 87 |
} |
| 88 |
|
| 89 |
#ifdef SSLNOPEEK |
| 90 |
return 1; |
| 91 |
|
| 92 |
#else /* #ifdef SSLNOPEEK ... #else ... #endif */ |
| 93 |
|
| 94 |
if(FD_ISSET(skt->skt,&rmask)) { |
| 95 |
ret= recv(skt->skt,buf,PM_BIGBUF-1,MSG_PEEK); |
| 96 |
if(result == 1 && ret == 0) ret= EOF; |
| 97 |
return ret; |
| 98 |
} |
| 99 |
|
| 100 |
/* socket is empty */ |
| 101 |
return 0; |
| 102 |
#endif /* #ifdef SSLNOPEEK ... #else ... #endif */ |
| 103 |
} |
| 104 |
|
| 105 |
/* --------------------------------------------------------------------- |
| 106 |
* vim: ts=4 |
| 107 |
*/ |