| 1 |
/* Smaskwait.c: this file contains code which allows one to wait until data |
| 2 |
* is present given a mask |
| 3 |
* Authors: Charles E. Campbell, Jr. |
| 4 |
* Terry McRoberts |
| 5 |
* Copyright: Copyright (C) 1999-2005 Charles E. Campbell, Jr. {{{1 |
| 6 |
* Permission is hereby granted to use and distribute this code, |
| 7 |
* with or without modifications, provided that this copyright |
| 8 |
* notice is copied with it. Like anything else that's free, |
| 9 |
* Smaskwait.c is provided *as is* and comes with no warranty |
| 10 |
* of any kind, either expressed or implied. By using this |
| 11 |
* software, you agree that in no event will the copyright |
| 12 |
* holder be liable for any damages resulting from the use |
| 13 |
* of this software. |
| 14 |
* Date: Aug 22, 2005 |
| 15 |
*/ |
| 16 |
#include <stdio.h> |
| 17 |
#include "xtdio.h" |
| 18 |
#define SSLNEEDTIME |
| 19 |
#include "sockets.h" |
| 20 |
|
| 21 |
/* --------------------------------------------------------------------- |
| 22 |
* Local Variables: |
| 23 |
*/ |
| 24 |
static int waitall = 0; |
| 25 |
static fd_set mask; /* mask last-used by Smaskwait, etc */ |
| 26 |
static struct timeval *timeptr = NULL; |
| 27 |
static struct timeval timewait; |
| 28 |
static fd_set rmask; /* set by Smaskwait */ |
| 29 |
static int first = 1; |
| 30 |
|
| 31 |
/* --------------------------------------------------------------------- |
| 32 |
* Local Prototypes: |
| 33 |
*/ |
| 34 |
|
| 35 |
/* --------------------------------------------------------------------- |
| 36 |
* Source Code: |
| 37 |
*/ |
| 38 |
|
| 39 |
/* Smaskwait: this function waits until some Socket specified to the |
| 40 |
* Smaskset() function receives something. |
| 41 |
* |
| 42 |
* Returns: < 0: error |
| 43 |
* ==0: timeout |
| 44 |
* > 0: communication data awaiting |
| 45 |
* |
| 46 |
* NOTE: THE MASK IS NOT RESET TO ZERO - THE MASK IS RETAINED!!! |
| 47 |
*/ |
| 48 |
#ifdef __PROTOTYPE__ |
| 49 |
int Smaskwait() |
| 50 |
#else |
| 51 |
int Smaskwait() |
| 52 |
#endif |
| 53 |
{ |
| 54 |
fd_set wmask; /* write mask */ |
| 55 |
fd_set emask; /* error mask */ |
| 56 |
short result; |
| 57 |
|
| 58 |
|
| 59 |
FD_ZERO(&wmask); |
| 60 |
FD_ZERO(&emask); |
| 61 |
|
| 62 |
/* test if something is available for reading on the socket. This form |
| 63 |
* will block (sleep) until something arrives |
| 64 |
*/ |
| 65 |
rmask = mask; |
| 66 |
result = select(waitall, &rmask,&wmask,&emask,timeptr); |
| 67 |
|
| 68 |
if(result < 0) { /* error */ |
| 69 |
return result; |
| 70 |
} |
| 71 |
|
| 72 |
/* result > 0: socket has something waiting |
| 73 |
* result == 0: timeout |
| 74 |
*/ |
| 75 |
return result; |
| 76 |
} |
| 77 |
|
| 78 |
/* --------------------------------------------------------------------- */ |
| 79 |
|
| 80 |
/* Smaskset: this function sets up the mask |
| 81 |
* A null skt clears the mask |
| 82 |
*/ |
| 83 |
#ifdef __PROTOTYPE__ |
| 84 |
void Smaskset(Socket *skt) |
| 85 |
#else |
| 86 |
void Smaskset(skt) |
| 87 |
Socket *skt; |
| 88 |
#endif |
| 89 |
{ |
| 90 |
|
| 91 |
if(first) { |
| 92 |
FD_ZERO(&mask); |
| 93 |
first= 0; |
| 94 |
} |
| 95 |
|
| 96 |
if(skt) { |
| 97 |
FD_SET(skt->skt,&mask); |
| 98 |
if(skt->skt >= waitall) waitall= skt->skt + 1; |
| 99 |
} |
| 100 |
else { |
| 101 |
waitall= 0; |
| 102 |
FD_ZERO(&mask); |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/* --------------------------------------------------------------------- */ |
| 108 |
|
| 109 |
/* Smaskfdset: this function sets up the mask with a "file descriptor" |
| 110 |
* which may be associated with devices other than Sockets |
| 111 |
*/ |
| 112 |
#ifdef __PROTOTYPE__ |
| 113 |
void Smaskfdset(int fd) |
| 114 |
#else |
| 115 |
void Smaskfdset(fd) |
| 116 |
int fd; |
| 117 |
#endif |
| 118 |
{ |
| 119 |
|
| 120 |
if(first) { |
| 121 |
FD_ZERO(&mask); |
| 122 |
first= 0; |
| 123 |
} |
| 124 |
|
| 125 |
if(0 <= fd && fd < FD_SETSIZE) { /* fd must be a legal file descriptor */ |
| 126 |
FD_SET(fd,&mask); |
| 127 |
if(fd >= waitall) waitall= fd + 1; |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
/* --------------------------------------------------------------------- */ |
| 133 |
|
| 134 |
/* Smasktime: this function sets up a timeval structure - specifies a |
| 135 |
* time limit for waiting. Note: if seconds < 0 or useconds < 0, |
| 136 |
* or seconds == useconds == 0, then there will be no timeout waiting. |
| 137 |
*/ |
| 138 |
#ifdef __PROTOTYPE__ |
| 139 |
void Smasktime(long seconds,long useconds) |
| 140 |
#else |
| 141 |
void Smasktime(seconds,useconds) |
| 142 |
long seconds; /* time in seconds */ |
| 143 |
long useconds; /* time in micro-seconds */ |
| 144 |
#endif |
| 145 |
{ |
| 146 |
|
| 147 |
if(seconds < 0 || useconds < 0 || (seconds == 0 && useconds == 0)) { |
| 148 |
/* no timeout (ie. infinite wait) */ |
| 149 |
timewait.tv_sec = 0L; |
| 150 |
timewait.tv_usec= 0L; |
| 151 |
timeptr = (struct timeval *) NULL; |
| 152 |
} |
| 153 |
|
| 154 |
else { /* set up specified timeout */ |
| 155 |
timewait.tv_sec = seconds; |
| 156 |
timewait.tv_usec= useconds; |
| 157 |
timeptr = &timewait; |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
/* --------------------------------------------------------------------- */ |
| 163 |
|
| 164 |
/* Smasktest: this function allows one to test if any Socket in the |
| 165 |
* mask has something on it |
| 166 |
* Returns: < 0: error |
| 167 |
* ==0: timeout |
| 168 |
* > 0: communication data awaiting |
| 169 |
*/ |
| 170 |
int Smasktest() |
| 171 |
{ |
| 172 |
int ret; |
| 173 |
struct timeval timewait_hold; |
| 174 |
struct timeval *timeptr_hold=NULL; |
| 175 |
|
| 176 |
|
| 177 |
/* save current time info */ |
| 178 |
timewait_hold = timewait; |
| 179 |
timeptr_hold = timeptr; |
| 180 |
|
| 181 |
/* set up for new time info */ |
| 182 |
timewait.tv_sec = 0L; |
| 183 |
timewait.tv_usec= 0L; |
| 184 |
timeptr = &timewait; |
| 185 |
|
| 186 |
ret= Smaskwait(); |
| 187 |
|
| 188 |
/* restore time info */ |
| 189 |
timewait= timewait_hold; |
| 190 |
timeptr = timeptr_hold; |
| 191 |
|
| 192 |
return ret; |
| 193 |
} |
| 194 |
|
| 195 |
/* --------------------------------------------------------------------- */ |
| 196 |
|
| 197 |
/* Smaskunset: this function sets up the mask |
| 198 |
* A null skt clears the mask |
| 199 |
*/ |
| 200 |
#ifdef __PROTOTYPE__ |
| 201 |
void Smaskunset(Socket *skt) |
| 202 |
#else |
| 203 |
void Smaskunset(skt) |
| 204 |
Socket *skt; |
| 205 |
#endif |
| 206 |
{ |
| 207 |
|
| 208 |
if(skt) { |
| 209 |
FD_CLR(skt->skt,&mask); |
| 210 |
} |
| 211 |
else { |
| 212 |
waitall= 0; |
| 213 |
FD_ZERO(&mask); |
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
/* --------------------------------------------------------------------- */ |
| 219 |
|
| 220 |
/* Smaskunfdset: this function sets up the mask |
| 221 |
* A null skt clears the mask |
| 222 |
*/ |
| 223 |
#ifdef __PROTOTYPE__ |
| 224 |
void Smaskunfdset(int fd) |
| 225 |
#else |
| 226 |
void Smaskunfdset(fd) |
| 227 |
int fd; |
| 228 |
#endif |
| 229 |
{ |
| 230 |
|
| 231 |
if(fd) FD_CLR(fd,&mask); |
| 232 |
else { |
| 233 |
waitall= 0; |
| 234 |
FD_ZERO(&mask); |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
/* --------------------------------------------------------------------- */ |
| 240 |
|
| 241 |
/* Smaskget: this function returns the current mask */ |
| 242 |
#ifdef __PROTOTYPE__ |
| 243 |
Smask Smaskget() |
| 244 |
#else |
| 245 |
Smask Smaskget() |
| 246 |
#endif |
| 247 |
{ |
| 248 |
Smask smask; |
| 249 |
|
| 250 |
smask.mask = mask; |
| 251 |
smask.waitall= waitall; |
| 252 |
|
| 253 |
return smask; |
| 254 |
} |
| 255 |
|
| 256 |
/* --------------------------------------------------------------------- */ |
| 257 |
|
| 258 |
/* Smaskuse: this function sets up the mask with the provided mask */ |
| 259 |
#ifdef __PROTOTYPE__ |
| 260 |
void Smaskuse(Smask usermask) |
| 261 |
#else |
| 262 |
void Smaskuse(usermask) |
| 263 |
Smask usermask; |
| 264 |
#endif |
| 265 |
{ |
| 266 |
|
| 267 |
mask = usermask.mask; |
| 268 |
waitall= usermask.waitall; |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
/* --------------------------------------------------------------------- */ |
| 273 |
|
| 274 |
typedef struct MaskStack_str MaskStack; |
| 275 |
struct MaskStack_str { |
| 276 |
Smask mask; |
| 277 |
MaskStack *nxt,*prv; |
| 278 |
}; |
| 279 |
static MaskStack *mshd=NULL,*mstl=NULL; |
| 280 |
|
| 281 |
/* Smaskpush: this function pushes the current mask down on a stack |
| 282 |
* ALSO: the current mask is *cleared*!!! |
| 283 |
*/ |
| 284 |
void Smaskpush() |
| 285 |
{ |
| 286 |
MaskStack *m; |
| 287 |
|
| 288 |
|
| 289 |
/* allocate */ |
| 290 |
m= (MaskStack *) malloc(sizeof(MaskStack)); |
| 291 |
|
| 292 |
/* link */ |
| 293 |
if(mstl) mstl->nxt= m; |
| 294 |
else mshd = m; |
| 295 |
m->prv= mstl; |
| 296 |
mstl = m; |
| 297 |
|
| 298 |
/* initialize */ |
| 299 |
mstl->mask.mask = mask; |
| 300 |
mstl->mask.waitall= waitall; |
| 301 |
|
| 302 |
Smaskset((Socket *) NULL); |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
/* --------------------------------------------------------------------- */ |
| 307 |
|
| 308 |
/* Smaskpop: this function pops the stack (or, if the stack is empty, |
| 309 |
* it will set the current mask to null) |
| 310 |
*/ |
| 311 |
void Smaskpop() |
| 312 |
{ |
| 313 |
|
| 314 |
if(mstl) { |
| 315 |
MaskStack *old=mstl; |
| 316 |
|
| 317 |
Smaskuse(mstl->mask); |
| 318 |
|
| 319 |
/* delink current tail */ |
| 320 |
if(mstl->prv) mstl->prv->nxt= NULL; |
| 321 |
else mshd = NULL; |
| 322 |
mstl= mstl->prv; |
| 323 |
|
| 324 |
free((char *) old); |
| 325 |
} |
| 326 |
else Smaskset((Socket *) NULL); |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
/* --------------------------------------------------------------------- */ |
| 331 |
|
| 332 |
/* Smaskisset: this function tests if a specific Socket is read-ready. |
| 333 |
* Assumption: function is called after a Smaskwait() so that the |
| 334 |
* rmask has been set up. |
| 335 |
*/ |
| 336 |
#ifdef __PROTOTYPE__ |
| 337 |
int Smaskisset(Socket *skt) |
| 338 |
#else |
| 339 |
int Smaskisset(skt) |
| 340 |
Socket *skt; |
| 341 |
#endif |
| 342 |
{ |
| 343 |
return FD_ISSET(skt->skt,&rmask); |
| 344 |
} |
| 345 |
|
| 346 |
/* --------------------------------------------------------------------- */ |
| 347 |
|
| 348 |
/* Smaskfdisset: this function tests if a specific file descriptor is |
| 349 |
* read-ready |
| 350 |
*/ |
| 351 |
#ifdef __PROTOTYPE__ |
| 352 |
int Smaskfdisset(int fd) |
| 353 |
#else |
| 354 |
int Smaskfdisset(fd) |
| 355 |
int fd; |
| 356 |
#endif |
| 357 |
{ |
| 358 |
return FD_ISSET(fd,&rmask); |
| 359 |
} |
| 360 |
|
| 361 |
/* --------------------------------------------------------------------- */ |
| 362 |
|
| 363 |
|
| 364 |
/* --------------------------------------------------------------------- |
| 365 |
* vim: ts=4 |
| 366 |
*/ |