From 34bf407d7722634d6363c46dd4d0df00f53732ef Mon Sep 17 00:00:00 2001 From: jongfoster Date: Sat, 9 Mar 2002 20:03:52 +0000 Subject: [PATCH 1/1] - Making various functions return int rather than size_t. (Undoing a recent change). Since size_t is unsigned on Windows, functions like read_socket that return -1 on error cannot return a size_t. THIS WAS A MAJOR BUG - it caused frequent, unpredictable crashes, and also frequently caused JB to jump to 100% CPU and stay there. (Because it thought it had just read ((unsigned)-1) == 4Gb of data...) - The signature of write_socket has changed, it now simply returns success=0/failure=nonzero. - Trying to get rid of a few warnings --with-debug on Windows, I've introduced a new type "jb_socket". This is used for the socket file descriptors. On Windows, this is SOCKET (a typedef for unsigned). Everywhere else, it's an int. The error value can't be -1 any more, so it's now JB_INVALID_SOCKET (which is -1 on UNIX, and in Windows it maps to the #define INVALID_SOCKET.) - The signature of bind_port has changed. --- amiga.c | 7 +++- gateway.c | 51 ++++++++++++----------- gateway.h | 12 ++++-- jbsockets.c | 97 ++++++++++++++++++++++++++++---------------- jbsockets.h | 20 +++++---- jcc.c | 114 +++++++++++++++++++++++++++------------------------- parsers.c | 14 +++++-- parsers.h | 7 +++- project.h | 30 ++++++++++++-- 9 files changed, 218 insertions(+), 134 deletions(-) diff --git a/amiga.c b/amiga.c index df1ad8ca..8da8d567 100644 --- a/amiga.c +++ b/amiga.c @@ -1,4 +1,4 @@ -const char amiga_rcs[] = "$Id: amiga.c,v 1.4 2001/10/07 15:35:13 oes Exp $"; +const char amiga_rcs[] = "$Id: amiga.c,v 1.5 2002/03/03 09:18:03 joergs Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/amiga.c,v $ @@ -28,6 +28,9 @@ const char amiga_rcs[] = "$Id: amiga.c,v 1.4 2001/10/07 15:35:13 oes Exp $"; * * Revisions : * $Log: amiga.c,v $ + * Revision 1.5 2002/03/03 09:18:03 joergs + * Made jumbjuster work on AmigaOS again. + * * Revision 1.4 2001/10/07 15:35:13 oes * Replaced 6 boolean members of csp with one bitmap (csp->flags) * @@ -76,7 +79,7 @@ __saveds ULONG server_thread(void) { SetErrnoPtr(&(UserData.eno),sizeof(int)); local_csp->cfd=ObtainSocket(local_csp->cfd, AF_INET, SOCK_STREAM, 0); - if(-1!=local_csp->cfd) + if(JB_INVALID_SOCKET!=local_csp->cfd) { Signal(main_task,SIGF_SINGLE); serve((struct client_state *) local_csp); diff --git a/gateway.c b/gateway.c index 2e4cbe27..7d67ff0d 100644 --- a/gateway.c +++ b/gateway.c @@ -1,4 +1,4 @@ -const char gateway_rcs[] = "$Id: gateway.c,v 1.10 2002/03/07 03:50:19 oes Exp $"; +const char gateway_rcs[] = "$Id: gateway.c,v 1.11 2002/03/08 17:46:04 jongfoster Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/gateway.c,v $ @@ -34,6 +34,9 @@ const char gateway_rcs[] = "$Id: gateway.c,v 1.10 2002/03/07 03:50:19 oes Exp $" * * Revisions : * $Log: gateway.c,v $ + * Revision 1.11 2002/03/08 17:46:04 jongfoster + * Fixing int/size_t warnings + * * Revision 1.10 2002/03/07 03:50:19 oes * - Improved handling of failed DNS lookups * - Fixed compiler warnings @@ -113,10 +116,10 @@ const char gateway_rcs[] = "$Id: gateway.c,v 1.10 2002/03/07 03:50:19 oes Exp $" const char gateway_h_rcs[] = GATEWAY_H_VERSION; -static int socks4_connect(const struct forward_spec * fwd, - const char * target_host, - int target_port, - struct client_state *csp); +static jb_socket socks4_connect(const struct forward_spec * fwd, + const char * target_host, + int target_port, + struct client_state *csp); #define SOCKS_REQUEST_GRANTED 90 @@ -160,9 +163,9 @@ static const char socks_userid[] = "anonymous"; * Returns : -1 => failure, else it is the socket file descriptor. * *********************************************************************/ -int forwarded_connect(const struct forward_spec * fwd, - struct http_request *http, - struct client_state *csp) +jb_socket forwarded_connect(const struct forward_spec * fwd, + struct http_request *http, + struct client_state *csp) { const char * dest_host; int dest_port; @@ -195,7 +198,7 @@ int forwarded_connect(const struct forward_spec * fwd, /* Should never get here */ log_error(LOG_LEVEL_FATAL, "SOCKS4 impossible internal error - bad SOCKS type."); errno = EINVAL; - return(-1); + return(JB_INVALID_SOCKET); } } @@ -218,10 +221,10 @@ int forwarded_connect(const struct forward_spec * fwd, * Returns : -1 => failure, else a socket file descriptor. * *********************************************************************/ -static int socks4_connect(const struct forward_spec * fwd, - const char * target_host, - int target_port, - struct client_state *csp) +static jb_socket socks4_connect(const struct forward_spec * fwd, + const char * target_host, + int target_port, + struct client_state *csp) { int web_server_addr; char cbuf[BUFFER_SIZE]; @@ -230,7 +233,7 @@ static int socks4_connect(const struct forward_spec * fwd, struct socks_reply *s = (struct socks_reply *)sbuf; size_t n; size_t csiz; - int sfd; + jb_socket sfd; int err = 0; char *errstr; @@ -249,7 +252,7 @@ static int socks4_connect(const struct forward_spec * fwd, if (err) { errno = EINVAL; - return(-1); + return(JB_INVALID_SOCKET); } /* build a socks request for connection to the web server */ @@ -265,7 +268,7 @@ static int socks4_connect(const struct forward_spec * fwd, if (web_server_addr == INADDR_NONE) { log_error(LOG_LEVEL_CONNECT, "socks4_connect: could not resolve target host %s", target_host); - return(-1); + return(JB_INVALID_SOCKET); } break; case SOCKS_4A: @@ -274,7 +277,7 @@ static int socks4_connect(const struct forward_spec * fwd, if (n > sizeof(cbuf)) { errno = EINVAL; - return(-1); + return(JB_INVALID_SOCKET); } strcpy(cbuf + csiz, target_host); csiz = n; @@ -283,7 +286,7 @@ static int socks4_connect(const struct forward_spec * fwd, /* Should never get here */ log_error(LOG_LEVEL_FATAL, "SOCKS4 impossible internal error - bad SOCKS type."); errno = EINVAL; - return(-1); + return(JB_INVALID_SOCKET); } c->vn = 4; @@ -298,23 +301,23 @@ static int socks4_connect(const struct forward_spec * fwd, /* pass the request to the socks server */ sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp); - if (sfd < 0) + if (sfd == JB_INVALID_SOCKET) { - return(-1); + return(JB_INVALID_SOCKET); } - if (write_socket(sfd, (char *)c, csiz) != csiz) + if (write_socket(sfd, (char *)c, (int)csiz)) { log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation write failed..."); close_socket(sfd); - return(-1); + return(JB_INVALID_SOCKET); } if (read_socket(sfd, sbuf, sizeof(sbuf)) != sizeof(*s)) { log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation read failed..."); close_socket(sfd); - return(-1); + return(JB_INVALID_SOCKET); } switch (s->cd) @@ -347,7 +350,7 @@ static int socks4_connect(const struct forward_spec * fwd, log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s ...", errstr); close_socket(sfd); - return(-1); + return(JB_INVALID_SOCKET); } diff --git a/gateway.h b/gateway.h index 278f70e0..98970443 100644 --- a/gateway.h +++ b/gateway.h @@ -1,6 +1,6 @@ #ifndef GATEWAY_H_INCLUDED #define GATEWAY_H_INCLUDED -#define GATEWAY_H_VERSION "$Id: gateway.h,v 1.2 2001/06/07 23:12:14 jongfoster Exp $" +#define GATEWAY_H_VERSION "$Id: gateway.h,v 1.3 2001/07/29 18:58:15 jongfoster Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/gateway.h,v $ @@ -36,6 +36,10 @@ * * Revisions : * $Log: gateway.h,v $ + * Revision 1.3 2001/07/29 18:58:15 jongfoster + * Removing nested #includes, adding forward declarations for needed + * structures, and changing the #define _FILENAME_H to FILENAME_H_INCLUDED. + * * Revision 1.2 2001/06/07 23:12:14 jongfoster * Removing gateways[] list - no longer used. * Replacing function pointer in struct gateway with a directly @@ -59,9 +63,9 @@ struct forward_spec; struct http_request; struct client_state; -extern int forwarded_connect(const struct forward_spec * fwd, - struct http_request *http, - struct client_state *csp); +extern jb_socket forwarded_connect(const struct forward_spec * fwd, + struct http_request *http, + struct client_state *csp); /* Revision control strings from this header and associated .c file */ extern const char gateway_rcs[]; diff --git a/jbsockets.c b/jbsockets.c index 63548620..7648c2ba 100644 --- a/jbsockets.c +++ b/jbsockets.c @@ -1,4 +1,4 @@ -const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.23 2002/03/05 00:36:01 jongfoster Exp $"; +const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.24 2002/03/07 03:51:36 oes Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jbsockets.c,v $ @@ -35,6 +35,10 @@ const char jbsockets_rcs[] = "$Id: jbsockets.c,v 1.23 2002/03/05 00:36:01 jongfo * * Revisions : * $Log: jbsockets.c,v $ + * Revision 1.24 2002/03/07 03:51:36 oes + * - Improved handling of failed DNS lookups + * - Fixed compiler warnings etc + * * Revision 1.23 2002/03/05 00:36:01 jongfoster * Fixing bug 514988 - unable to restart JunkBuster * @@ -182,13 +186,15 @@ const char jbsockets_h_rcs[] = JBSOCKETS_H_VERSION; * 3 : csp = Current client state (buffers, headers, etc...) * Not modified, only used for source IP and ACL. * - * Returns : -1 => failure, else it is the socket file descriptor. + * Returns : JB_INVALID_SOCKET => failure, else it is the socket + * file descriptor. * *********************************************************************/ -int connect_to(const char *host, int portnum, struct client_state *csp) +jb_socket connect_to(const char *host, int portnum, struct client_state *csp) { struct sockaddr_in inaddr; - int fd, addr; + jb_socket fd; + int addr; fd_set wfds; struct timeval tv[1]; #if !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) @@ -204,7 +210,7 @@ int connect_to(const char *host, int portnum, struct client_state *csp) if ((addr = resolve_hostname_to_ip(host)) == INADDR_NONE) { csp->http->host_ip_addr_str = strdup("unknown"); - return(-1); + return(JB_INVALID_SOCKET); } #ifdef FEATURE_ACL @@ -218,7 +224,7 @@ int connect_to(const char *host, int portnum, struct client_state *csp) #else errno = EPERM; #endif - return(-1); + return(JB_INVALID_SOCKET); } #endif /* def FEATURE_ACL */ @@ -239,9 +245,13 @@ int connect_to(const char *host, int portnum, struct client_state *csp) } #endif /* ndef _WIN32 */ +#ifdef _WIN32 + if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) == JB_INVALID_SOCKET) +#else if ((fd = socket(inaddr.sin_family, SOCK_STREAM, 0)) < 0) +#endif { - return(-1); + return(JB_INVALID_SOCKET); } #ifdef TCP_NODELAY @@ -259,7 +269,7 @@ int connect_to(const char *host, int portnum, struct client_state *csp) } #endif /* !defined(_WIN32) && !defined(__BEOS__) && !defined(AMIGA) && !defined(__OS2__) */ - while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == -1) + while (connect(fd, (struct sockaddr *) & inaddr, sizeof inaddr) == JB_INVALID_SOCKET) { #ifdef _WIN32 if (errno == WSAEINPROGRESS) @@ -279,7 +289,7 @@ int connect_to(const char *host, int portnum, struct client_state *csp) #endif /* __OS2__ */ { close_socket(fd); - return(-1); + return(JB_INVALID_SOCKET); } } @@ -298,10 +308,11 @@ int connect_to(const char *host, int portnum, struct client_state *csp) tv->tv_sec = 30; tv->tv_usec = 0; - if (select(fd + 1, NULL, &wfds, NULL, tv) <= 0) + /* MS Windows uses int, not SOCKET, for the 1st arg of select(). Wierd! */ + if (select((int)fd + 1, NULL, &wfds, NULL, tv) <= 0) { close_socket(fd); - return(-1); + return(JB_INVALID_SOCKET); } return(fd); @@ -319,22 +330,26 @@ int connect_to(const char *host, int portnum, struct client_state *csp) * 2 : buf = pointer to data to be written. * 3 : len = length of data to be written to the socket "fd". * - * Returns : Win32 & Unix: If no error occurs, returns the total number of - * bytes sent, which can be less than the number - * indicated by len. Otherwise, returns (-1). + * Returns : 0 on success (entire buffer sent). + * nonzero on error. * *********************************************************************/ -size_t write_socket(int fd, const char *buf, size_t len) +int write_socket(jb_socket fd, const char *buf, int len) { - if (len <= 0) + if (len == 0) { - return(0); + return 0; + } + + if (len < 0) + { + return 1; } log_error(LOG_LEVEL_LOG, "%N", len, buf); #if defined(_WIN32) || defined(__BEOS__) || defined(AMIGA) - return(send(fd, buf, len, 0)); + return (send(fd, buf, len, 0) != len); #elif defined(__OS2__) /* * Break the data up into SOCKET_SEND_MAX chunks for sending... @@ -351,13 +366,13 @@ size_t write_socket(int fd, const char *buf, size_t len) send_len = SOCKET_SEND_MAX; send_rc = send(fd,(char*)buf + i, send_len, 0); if (send_rc == -1) - return(0); + return 1; i = i + send_len; - } - return len; + } + return 0; } #else - return(write(fd, buf, len)); + return (write(fd, buf, len) != len); #endif } @@ -381,14 +396,14 @@ size_t write_socket(int fd, const char *buf, size_t len) * smaller than the number of bytes requested; this may hap- * pen for example because fewer bytes are actually available * right now (maybe because we were close to end-of-file, or - * because we are reading from a pipe, or from a terminal), - * or because read() was interrupted by a signal. On error, + * because we are reading from a pipe, or from a terminal, + * or because read() was interrupted by a signal). On error, * -1 is returned, and errno is set appropriately. In this * case it is left unspecified whether the file position (if * any) changes. * *********************************************************************/ -size_t read_socket(int fd, char *buf, size_t len) +int read_socket(jb_socket fd, char *buf, int len) { if (len <= 0) { @@ -415,7 +430,7 @@ size_t read_socket(int fd, char *buf, size_t len) * Returns : void * *********************************************************************/ -void close_socket(int fd) +void close_socket(jb_socket fd) { #if defined(_WIN32) || defined(__BEOS__) closesocket(fd); @@ -440,21 +455,23 @@ void close_socket(int fd) * Parameters : * 1 : hostnam = TCP/IP address to bind/listen to * 2 : portnum = port to listen on + * 3 : pfd = pointer used to return file descriptor. * - * Returns : if success, return file descriptor + * Returns : if success, returns 0 and sets *pfd. * if failure, returns -3 if address is in use, * -2 if address unresolvable, * -1 otherwise - * *********************************************************************/ -int bind_port(const char *hostnam, int portnum) +int bind_port(const char *hostnam, int portnum, jb_socket *pfd) { struct sockaddr_in inaddr; - int fd; + jb_socket fd; #ifndef _WIN32 int one = 1; #endif /* ndef _WIN32 */ + *pfd = JB_INVALID_SOCKET; + memset((char *)&inaddr, '\0', sizeof inaddr); inaddr.sin_family = AF_INET; @@ -480,7 +497,11 @@ int bind_port(const char *hostnam, int portnum) fd = socket(AF_INET, SOCK_STREAM, 0); +#ifdef _WIN32 + if (fd == JB_INVALID_SOCKET) +#else if (fd < 0) +#endif { return(-1); } @@ -526,7 +547,8 @@ int bind_port(const char *hostnam, int portnum) } } - return fd; + *pfd = fd; + return 0; } @@ -547,11 +569,11 @@ int bind_port(const char *hostnam, int portnum) * On an error it returns 0 (FALSE). * *********************************************************************/ -int accept_connection(struct client_state * csp, int fd) +int accept_connection(struct client_state * csp, jb_socket fd) { struct sockaddr_in client, server; struct hostent *host = NULL; - int afd; + jb_socket afd; size_t c_length, s_length; #if defined(HAVE_GETHOSTBYADDR_R_8_ARGS) || defined(HAVE_GETHOSTBYADDR_R_7_ARGS) || defined(HAVE_GETHOSTBYADDR_R_5_ARGS) struct hostent result; @@ -565,15 +587,22 @@ int accept_connection(struct client_state * csp, int fd) c_length = s_length = sizeof(client); +#ifdef _WIN32 + afd = accept (fd, (struct sockaddr *) &client, &c_length); + if (afd == JB_INVALID_SOCKET) + { + return 0; + } +#else do { afd = accept (fd, (struct sockaddr *) &client, &c_length); } while (afd < 1 && errno == EINTR); - if (afd < 0) { return 0; } +#endif /* * Determine the IP-Adress that the client used to reach us diff --git a/jbsockets.h b/jbsockets.h index 4975a057..08f0ddec 100644 --- a/jbsockets.h +++ b/jbsockets.h @@ -1,6 +1,6 @@ #ifndef JBSOCKETS_H_INCLUDED #define JBSOCKETS_H_INCLUDED -#define JBSOCKETS_H_VERSION "$Id: jbsockets.h,v 1.3 2001/07/29 19:01:11 jongfoster Exp $" +#define JBSOCKETS_H_VERSION "$Id: jbsockets.h,v 1.4 2002/03/07 03:51:36 oes Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jbsockets.h,v $ @@ -37,6 +37,10 @@ * * Revisions : * $Log: jbsockets.h,v $ + * Revision 1.4 2002/03/07 03:51:36 oes + * - Improved handling of failed DNS lookups + * - Fixed compiler warnings etc + * * Revision 1.3 2001/07/29 19:01:11 jongfoster * Changed _FILENAME_H to FILENAME_H_INCLUDED. * Added forward declarations for needed structures. @@ -51,19 +55,21 @@ *********************************************************************/ +#include "project.h" + #ifdef __cplusplus extern "C" { #endif struct client_state; -extern int connect_to(const char *host, int portnum, struct client_state *csp); -extern size_t write_socket(int fd, const char *buf, size_t n); -extern size_t read_socket(int fd, char *buf, size_t n); -extern void close_socket(int fd); +extern jb_socket connect_to(const char *host, int portnum, struct client_state *csp); +extern int write_socket(jb_socket fd, const char *buf, int n); +extern int read_socket(jb_socket fd, char *buf, int n); +extern void close_socket(jb_socket fd); -extern int bind_port(const char *hostnam, int portnum); -extern int accept_connection(struct client_state * csp, int fd); +extern int bind_port(const char *hostnam, int portnum, jb_socket *pfd); +extern int accept_connection(struct client_state * csp, jb_socket fd); extern unsigned long resolve_hostname_to_ip(const char *host); diff --git a/jcc.c b/jcc.c index 47337bd5..edb15588 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.77 2002/03/07 03:52:06 oes Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.78 2002/03/08 21:35:04 oes Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -33,6 +33,9 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.77 2002/03/07 03:52:06 oes Exp $"; * * Revisions : * $Log: jcc.c,v $ + * Revision 1.78 2002/03/08 21:35:04 oes + * Added optional group supplement to --user option. Will now use default group of user if no group given + * * Revision 1.77 2002/03/07 03:52:06 oes * - Fixed compiler warnings etc * - Improved handling of failed DNS lookups @@ -675,14 +678,18 @@ static void chat(struct client_state *csp) #define IS_ENABLED_AND IS_TOGGLED_ON_AND IS_NOT_FORCED_AND char buf[BUFFER_SIZE]; - char *hdr, *p, *req; + char *hdr; + char *p; + char *req; fd_set rfds; - int n, maxfd, server_body; + int n; + jb_socket maxfd; + int server_body; int ms_iis5_hack = 0; int byte_count = 0; const struct forward_spec * fwd; struct http_request *http; - size_t len; /* for buffer sizes */ + int len; /* for buffer sizes */ #ifdef FEATURE_KILL_POPUPS int block_popups; /* bool, 1==will block popups */ int block_popups_now = 0; /* bool, 1==currently blocking popups */ @@ -967,8 +974,8 @@ static void chat(struct client_state *csp) ) { /* Write the answer to the client */ - if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length) - || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length)) + if (write_socket(csp->cfd, rsp->head, rsp->head_length) + || write_socket(csp->cfd, rsp->body, rsp->content_length)) { log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host); } @@ -1003,7 +1010,7 @@ static void chat(struct client_state *csp) csp->sfd = forwarded_connect(fwd, http, csp); - if (csp->sfd < 0) + if (csp->sfd == JB_INVALID_SOCKET) { log_error(LOG_LEVEL_CONNECT, "connect to: %s failed: %E", http->hostport); @@ -1027,8 +1034,8 @@ static void chat(struct client_state *csp) /* Write the answer to the client */ if(rsp) { - if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length) - || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length)) + if (write_socket(csp->cfd, rsp->head, rsp->head_length) + || write_socket(csp->cfd, rsp->body, rsp->content_length)) { log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host); } @@ -1055,10 +1062,8 @@ static void chat(struct client_state *csp) * (along with anything else that may be in the buffer) */ - len = strlen(hdr); - - if ((write_socket(csp->sfd, hdr, len) != len) - || (flush_socket(csp->sfd, csp ) < 0)) + if (write_socket(csp->sfd, hdr, strlen(hdr)) + || (flush_socket(csp->sfd, csp) < 0)) { log_error(LOG_LEVEL_CONNECT, "write header to: %s failed: %E", http->hostport); @@ -1070,8 +1075,8 @@ static void chat(struct client_state *csp) if(rsp) { - if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length) - || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length)) + if (write_socket(csp->cfd, rsp->head, rsp->head_length) + || write_socket(csp->cfd, rsp->body, rsp->content_length)) { log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host); } @@ -1092,7 +1097,7 @@ static void chat(struct client_state *csp) log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 2\n", csp->ip_addr_str, http->ocmd); - if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1) < 0) + if (write_socket(csp->cfd, CSUCCEED, sizeof(CSUCCEED)-1)) { freez(hdr); return; @@ -1139,7 +1144,7 @@ static void chat(struct client_state *csp) break; /* "game over, man" */ } - if (write_socket(csp->sfd, buf, len) != len) + if (write_socket(csp->sfd, buf, len)) { log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host); return; @@ -1170,8 +1175,8 @@ static void chat(struct client_state *csp) if(rsp) { - if ((write_socket(csp->cfd, rsp->head, rsp->head_length) != rsp->head_length) - || (write_socket(csp->cfd, rsp->body, rsp->content_length) != rsp->content_length)) + if (write_socket(csp->cfd, rsp->head, rsp->head_length) + || write_socket(csp->cfd, rsp->body, rsp->content_length)) { log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host); } @@ -1241,10 +1246,8 @@ static void chat(struct client_state *csp) log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header"); } - len = strlen(hdr); - - if ((write_socket(csp->cfd, hdr, len) != len) - || (write_socket(csp->cfd, p != NULL ? p : csp->iob->cur, csp->content_length) != (int)csp->content_length)) + if (write_socket(csp->cfd, hdr, strlen(hdr)) + || write_socket(csp->cfd, p != NULL ? p : csp->iob->cur, csp->content_length)) { log_error(LOG_LEVEL_ERROR, "write modified content to client failed: %E"); return; @@ -1304,8 +1307,8 @@ static void chat(struct client_state *csp) len = strlen(hdr); byte_count += len; - if (((write_socket(csp->cfd, hdr, len) != len) - || (len = flush_socket(csp->cfd, csp) < 0))) + if (write_socket(csp->cfd, hdr, len) + || (len = flush_socket(csp->cfd, csp) < 0)) { log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E"); @@ -1323,7 +1326,7 @@ static void chat(struct client_state *csp) } else { - if (write_socket(csp->cfd, buf, len) != len) + if (write_socket(csp->cfd, buf, len)) { log_error(LOG_LEVEL_ERROR, "write to client failed: %E"); return; @@ -1395,13 +1398,6 @@ static void chat(struct client_state *csp) log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header"); } - len = strlen(hdr); - - /* write the server's (modified) header to - * the client (along with anything else that - * may be in the buffer) - */ - #ifdef FEATURE_KILL_POPUPS /* Start blocking popups if appropriate. */ @@ -1437,24 +1433,33 @@ static void chat(struct client_state *csp) content_filter = gif_deanimate_response; } - /* * Only write if we're not buffering for content modification */ - if (!content_filter && ((write_socket(csp->cfd, hdr, len) != len) - || (len = flush_socket(csp->cfd, csp) < 0))) + if (!content_filter) { - log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E"); - - /* the write failed, so don't bother - * mentioning it to the client... - * it probably can't hear us anyway. + /* write the server's (modified) header to + * the client (along with anything else that + * may be in the buffer) */ - freez(hdr); - return; - } - if(!content_filter) byte_count += len; + len = strlen(hdr); + + if (write_socket(csp->cfd, hdr, len) + || (len = flush_socket(csp->cfd, csp) < 0)) + { + log_error(LOG_LEVEL_CONNECT, "write header to client failed: %E"); + + /* the write failed, so don't bother + * mentioning it to the client... + * it probably can't hear us anyway. + */ + freez(hdr); + return; + } + + byte_count += len; + } /* we're finished with the server's header */ @@ -1504,7 +1509,7 @@ static void serve(struct client_state *csp) chat(csp); close_socket(csp->cfd); - if (csp->sfd >= 0) + if (csp->sfd != JB_INVALID_SOCKET) { close_socket(csp->sfd); } @@ -1850,9 +1855,10 @@ int main(int argc, const char *argv[]) * Returns : Port that was opened. * *********************************************************************/ -static int bind_port_helper(struct configuration_spec * config) +static jb_socket bind_port_helper(struct configuration_spec * config) { - int bfd; + int result; + jb_socket bfd; if ( (config->haddr != NULL) && (config->haddr[0] == '1') @@ -1874,11 +1880,11 @@ static int bind_port_helper(struct configuration_spec * config) config->hport, config->haddr); } - bfd = bind_port(config->haddr, config->hport); + result = bind_port(config->haddr, config->hport, &bfd); - if (bfd < 0) + if (result < 0) { - switch(bfd) + switch(result) { case -3 : log_error(LOG_LEVEL_FATAL, "can't bind to %s:%d: " @@ -1898,7 +1904,7 @@ static int bind_port_helper(struct configuration_spec * config) } /* shouldn't get here */ - return -1; + return JB_INVALID_SOCKET; } config->need_bind = 0; @@ -1921,7 +1927,7 @@ static int bind_port_helper(struct configuration_spec * config) static void listen_loop(void) { struct client_state *csp = NULL; - int bfd; + jb_socket bfd; struct configuration_spec * config; config = load_config(); @@ -1960,7 +1966,7 @@ static void listen_loop(void) } csp->flags |= CSP_FLAG_ACTIVE; - csp->sfd = -1; + csp->sfd = JB_INVALID_SOCKET; csp->config = config = load_config(); diff --git a/parsers.c b/parsers.c index bb5b31d6..a0da912a 100644 --- a/parsers.c +++ b/parsers.c @@ -1,4 +1,4 @@ -const char parsers_rcs[] = "$Id: parsers.c,v 1.47 2002/02/20 23:15:13 jongfoster Exp $"; +const char parsers_rcs[] = "$Id: parsers.c,v 1.48 2002/03/07 03:46:53 oes Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/parsers.c,v $ @@ -40,6 +40,9 @@ const char parsers_rcs[] = "$Id: parsers.c,v 1.47 2002/02/20 23:15:13 jongfoster * * Revisions : * $Log: parsers.c,v $ + * Revision 1.48 2002/03/07 03:46:53 oes + * Fixed compiler warnings etc + * * Revision 1.47 2002/02/20 23:15:13 jongfoster * Parsing functions now handle out-of-memory gracefully by returning * an error code. @@ -442,17 +445,20 @@ const add_header_func_ptr add_server_headers[] = { * file, the results are not portable. * *********************************************************************/ -size_t flush_socket(int fd, struct client_state *csp) +int flush_socket(jb_socket fd, struct client_state *csp) { struct iob *iob = csp->iob; - size_t len = iob->eod - iob->cur; + int len = iob->eod - iob->cur; if (len <= 0) { return(0); } - len = write_socket(fd, iob->cur, len); + if (write_socket(fd, iob->cur, len)) + { + return(-1); + } iob->eod = iob->cur = iob->buf; return(len); diff --git a/parsers.h b/parsers.h index b3a7d5c3..1526cb1f 100644 --- a/parsers.h +++ b/parsers.h @@ -1,6 +1,6 @@ #ifndef PARSERS_H_INCLUDED #define PARSERS_H_INCLUDED -#define PARSERS_H_VERSION "$Id: parsers.h,v 1.20 2002/02/20 23:15:13 jongfoster Exp $" +#define PARSERS_H_VERSION "$Id: parsers.h,v 1.21 2002/03/07 03:46:17 oes Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/parsers.h,v $ @@ -43,6 +43,9 @@ * * Revisions : * $Log: parsers.h,v $ + * Revision 1.21 2002/03/07 03:46:17 oes + * Fixed compiler warnings + * * Revision 1.20 2002/02/20 23:15:13 jongfoster * Parsing functions now handle out-of-memory gracefully by returning * an error code. @@ -142,7 +145,7 @@ extern const struct parsers server_patterns[]; extern const add_header_func_ptr add_client_headers[]; extern const add_header_func_ptr add_server_headers[]; -extern size_t flush_socket(int fd, struct client_state *csp); +extern int flush_socket(jb_socket fd, struct client_state *csp); extern size_t add_to_iob(struct client_state *csp, char *buf, size_t n); extern char *get_header(struct client_state *csp); extern char *get_header_value(const struct list *header_list, const char *header_name); diff --git a/project.h b/project.h index e332fe48..aed21f05 100644 --- a/project.h +++ b/project.h @@ -1,6 +1,6 @@ #ifndef PROJECT_H_INCLUDED #define PROJECT_H_INCLUDED -#define PROJECT_H_VERSION "$Id: project.h,v 1.52 2002/03/07 03:46:17 oes Exp $" +#define PROJECT_H_VERSION "$Id: project.h,v 1.53 2002/03/08 16:48:55 oes Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/project.h,v $ @@ -36,6 +36,9 @@ * * Revisions : * $Log: project.h,v $ + * Revision 1.53 2002/03/08 16:48:55 oes + * Added FEATURE_NO_GIFS and BUILTIN_IMAGE_MIMETYPE + * * Revision 1.52 2002/03/07 03:46:17 oes * Fixed compiler warnings * @@ -401,10 +404,31 @@ #include "amiga.h" #endif /* def AMIGA */ +#ifdef _WIN32 +/* + * I don't want to have to #include all this just for the declaration + * of SOCKET. However, it looks like we have to... + */ +#include +#endif + + #ifdef __cplusplus extern "C" { #endif +/* + * The type used by sockets. On UNIX it's an int. Microsoft decided to + * make it an unsigned. + */ +#ifdef _WIN32 +typedef SOCKET jb_socket; +#define JB_INVALID_SOCKET INVALID_SOCKET +#else /* ndef _WIN32 */ +typedef int jb_socket; +#define JB_INVALID_SOCKET (-1) +#endif /* ndef _WIN32 */ + /* * Error codes. Functions returning these should return a jb_err @@ -712,10 +736,10 @@ struct client_state struct current_action_spec action[1]; /* socket to talk to client (web browser) */ - int cfd; + jb_socket cfd; /* socket to talk to server (web server or proxy) */ - int sfd; + jb_socket sfd; /* Multi-purpose flag container, see CSP_FLAG_* above */ unsigned short int flags; -- 2.39.2