Main Page | Class List | Directories | File List | Class Members | File Members

utils.c File Reference

#include <unistd.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include "utils.h"

Go to the source code of this file.

Typedefs

typedef void sigfunc (int)

Functions

sigfuncsig_catch (int sig, void(*f)(int))
static void catch_alrm (int x)
int timeout_connect (int sockfd, const struct sockaddr *serv_addr, size_t addrlen)
int fd_timeout_read (int fd, char fdflag, void *buf, size_t nbytes)
int ssl_timeout_read (SSL *ssl, void *buf, int nbytes)
int full_read (int fd, char fdflag, void *vbuf, int min, int len)
int full_read_ssl (SSL *ssl, unsigned char *buf, int min, int len)
int full_write (int fd, char fdflag, const void *vbuf, int len)


Typedef Documentation

typedef void sigfunc(int)
 

Definition at line 53 of file utils.c.


Function Documentation

static void catch_alrm int  x  )  [static]
 

Definition at line 65 of file utils.c.

References UNUSED_VARIABLE.

Referenced by fd_timeout_read(), ssl_timeout_read(), and timeout_connect().

00066 {
00067     UNUSED_VARIABLE(x);
00068 }

int fd_timeout_read int  fd,
char  fdflag,
void *  buf,
size_t  nbytes
 

Definition at line 98 of file utils.c.

References catch_alrm(), libspamc_timeout, and sig_catch().

Referenced by _spamc_read_full_line(), and full_read().

00099 {
00100     int nred;
00101     int origerr;
00102 #ifndef _WIN32
00103     sigfunc *sig;
00104 
00105     sig = sig_catch(SIGALRM, catch_alrm);
00106     if (libspamc_timeout > 0) {
00107         alarm(libspamc_timeout);
00108     }
00109 #endif
00110 
00111     do {
00112         if (fdflag) {
00113             nred = (int)read(fd, buf, nbytes);
00114             origerr = errno;
00115         }
00116         else {
00117             nred = (int)recv(fd, buf, nbytes, 0);
00118 #ifndef _WIN32
00119             origerr = errno;
00120 #else
00121             origerr = WSAGetLastError();
00122 #endif
00123         }
00124     } while (nred < 0 && origerr == EWOULDBLOCK);
00125 
00126 #ifndef _WIN32
00127     if (nred < 0 && origerr == EINTR)
00128         errno = ETIMEDOUT;
00129 
00130     if (libspamc_timeout > 0) {
00131         alarm(0);
00132     }
00133 
00134     /* restore old signal handler */
00135     sig_catch(SIGALRM, sig);
00136 #endif
00137 
00138     return nred;
00139 }

int full_read int  fd,
char  fdflag,
void *  vbuf,
int  min,
int  len
 

Definition at line 184 of file utils.c.

References fd_timeout_read().

Referenced by _message_read_bsmtp(), _message_read_raw(), message_dump(), and message_filter().

00185 {
00186     unsigned char *buf = (unsigned char *) vbuf;
00187     int total;
00188     int thistime;
00189 
00190     for (total = 0; total < min;) {
00191         thistime = fd_timeout_read(fd, fdflag, buf + total, len - total);
00192 
00193         if (thistime < 0) {
00194             if (total >= min) {
00195                 /* error, but we read *some*.  return what we've read
00196                  * so far and next read (if there is one) will return -1. */
00197                 return total;
00198             } else {
00199                 return -1;
00200             }
00201         }
00202         else if (thistime == 0) {
00203             /* EOF, but we didn't read the minimum.  return what we've read
00204              * so far and next read (if there is one) will return 0. */
00205             return total;
00206         }
00207 
00208         total += thistime;
00209     }
00210     return total;
00211 }

int full_read_ssl SSL ssl,
unsigned char *  buf,
int  min,
int  len
 

Definition at line 213 of file utils.c.

References ssl_timeout_read().

Referenced by message_filter().

00214 {
00215     int total;
00216     int thistime;
00217 
00218     for (total = 0; total < min;) {
00219         thistime = ssl_timeout_read(ssl, buf + total, len - total);
00220 
00221         if (thistime < 0) {
00222             if (total >= min) {
00223                 /* error, but we read *some*.  return what we've read
00224                  * so far and next read (if there is one) will return -1. */
00225                 return total;
00226             } else {
00227                 return -1;
00228             }
00229         }
00230         else if (thistime == 0) {
00231             /* EOF, but we didn't read the minimum.  return what we've read
00232              * so far and next read (if there is one) will return 0. */
00233             return total;
00234         }
00235 
00236         total += thistime;
00237     }
00238     return total;
00239 }

int full_write int  fd,
char  fdflag,
const void *  vbuf,
int  len
 

Definition at line 241 of file utils.c.

Referenced by main(), message_dump(), message_filter(), message_process(), message_tell(), and message_write().

00242 {
00243     const char *buf = (const char *) vbuf;
00244     int total;
00245     int thistime;
00246     int origerr;
00247 
00248     for (total = 0; total < len;) {
00249         if (fdflag) {
00250             thistime = write(fd, buf + total, len - total);
00251             origerr = errno;
00252         }
00253         else {
00254             thistime = send(fd, buf + total, len - total, 0);
00255 #ifndef _WIN32
00256             origerr = errno;
00257 #else
00258             origerr = WSAGetLastError();
00259 #endif
00260         }
00261         if (thistime < 0) {
00262             if (EINTR == origerr || EWOULDBLOCK == origerr)
00263                 continue;
00264             return thistime;    /* always an error for writes */
00265         }
00266         total += thistime;
00267     }
00268     return total;
00269 }

sigfunc* sig_catch int  sig,
void(*)(int)  f
 

Definition at line 55 of file utils.c.

Referenced by fd_timeout_read(), ssl_timeout_read(), and timeout_connect().

00056 {
00057     struct sigaction act, oact;
00058     act.sa_handler = f;
00059     act.sa_flags = 0;
00060     sigemptyset(&act.sa_mask);
00061     sigaction(sig, &act, &oact);
00062     return oact.sa_handler;
00063 }

int ssl_timeout_read SSL ssl,
void *  buf,
int  nbytes
 

Definition at line 141 of file utils.c.

References catch_alrm(), libspamc_timeout, sig_catch(), and UNUSED_VARIABLE.

Referenced by _spamc_read_full_line(), and full_read_ssl().

00142 {
00143     int nred;
00144 
00145 #ifndef _WIN32
00146     sigfunc *sig;
00147 
00148     sig = sig_catch(SIGALRM, catch_alrm);
00149     if (libspamc_timeout > 0) {
00150         alarm(libspamc_timeout);
00151     }
00152 #endif
00153 
00154     do {
00155 
00156 #ifdef SPAMC_SSL
00157         nred = SSL_read(ssl, buf, nbytes);
00158 #else
00159         UNUSED_VARIABLE(ssl);
00160         UNUSED_VARIABLE(buf);
00161         UNUSED_VARIABLE(nbytes);
00162         nred = 0;               /* never used */
00163 #endif
00164 
00165     } while (nred < 0 && errno == EWOULDBLOCK);
00166 
00167 #ifndef _WIN32
00168     if (nred < 0 && errno == EINTR)
00169         errno = ETIMEDOUT;
00170 
00171     if (libspamc_timeout > 0) {
00172         alarm(0);
00173     }
00174 
00175     /* restore old signal handler */
00176     sig_catch(SIGALRM, sig);
00177 #endif
00178 
00179     return nred;
00180 }

int timeout_connect int  sockfd,
const struct sockaddr *  serv_addr,
size_t  addrlen
 

Definition at line 71 of file utils.c.

References catch_alrm(), libspamc_timeout, and sig_catch().

Referenced by _try_to_connect_tcp(), and _try_to_connect_unix().

00072 {
00073     int ret;
00074 
00075 #ifndef _WIN32
00076     sigfunc* sig;
00077 
00078     sig = sig_catch(SIGALRM, catch_alrm);
00079     if (libspamc_timeout > 0) {
00080       alarm(libspamc_timeout);
00081     }
00082 #endif
00083 
00084     ret = connect(sockfd, serv_addr, addrlen);
00085 
00086 #ifndef _WIN32
00087     if (libspamc_timeout > 0) {
00088       alarm(0);
00089     }
00090   
00091     /* restore old signal handler */
00092     sig_catch(SIGALRM, sig);
00093 #endif
00094   
00095     return ret;
00096 }


© sourcejam.com 2005-2008