diff --git a/c2s/c2s.c b/c2s/c2s.c index 991af050..677a0e11 100644 --- a/c2s/c2s.c +++ b/c2s/c2s.c @@ -74,7 +74,7 @@ static int _c2s_client_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) len = recv(sess->fd->fd, buf->data, buf->len, 0); if(len < 0) { - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) { + if(MIO_WOULDBLOCK) { buf->len = 0; return 0; } @@ -142,7 +142,7 @@ static int _c2s_client_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) return len; } - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) + if(MIO_WOULDBLOCK) return 0; if(s->state >= state_OPEN && sess->resources != NULL) @@ -672,7 +672,7 @@ int c2s_router_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { len = recv(c2s->fd->fd, buf->data, buf->len, 0); if(len < 0) { - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) { + if(MIO_WOULDBLOCK) { buf->len = 0; return 0; } @@ -706,7 +706,7 @@ int c2s_router_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { return len; } - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) + if(MIO_WOULDBLOCK) return 0; log_write(c2s->log, LOG_NOTICE, "[%d] [router] write error: %s (%d)", c2s->fd->fd, MIO_STRERROR(MIO_ERROR), MIO_ERROR); diff --git a/mio/mio.h b/mio/mio.h index 72a65dc7..fea4aca6 100644 --- a/mio/mio.h +++ b/mio/mio.h @@ -1,173 +1,175 @@ -/* - * jabberd - Jabber Open Source Server - * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney, - * Ryan Eatmon, Robert Norris - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA - */ - -#ifndef INCL_MIO_H -#define INCL_MIO_H - -#ifdef HAVE_CONFIG_H -# include -#endif - -#include "ac-stdint.h" - -/* jabberd2 Windows DLL */ -#ifndef JABBERD2_API -# ifdef _WIN32 -# ifdef JABBERD2_EXPORTS -# define JABBERD2_API __declspec(dllexport) -# else /* JABBERD2_EXPORTS */ -# define JABBERD2_API __declspec(dllimport) -# endif /* JABBERD2_EXPORTS */ -# else /* _WIN32 */ -# define JABBERD2_API extern -# endif /* _WIN32 */ -#endif /* JABBERD2_API */ - -#ifdef _WIN32 -# define MIO_MAXFD FD_SETSIZE -#else -# define MIO_MAXFD 1024 -#endif - -#include -#include -#include -#include - -#ifdef HAVE_UNISTD_H -# include -#endif - -#ifdef HAVE_SYS_SOCKET_H -# include -#endif - -#ifdef HAVE_FCNTL_H -# include -#endif - -#ifdef HAVE_SYS_IOCTL_H -# include -#endif - -#ifdef HAVE_SYS_FILIO_H -# include -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @file mio/mio.h - * @brief mio - manage i/o - * - * This used to be something large and all inclusive for 1.2/1.4, - * but for 1.5 and beyond it is the most simple fd wrapper possible. - * It is also customized per-app and may be limited/extended depending on needs. - * - * Usage is pretty simple: - * - create a manager - * - add fds or tell it to listen - * - assign an action handler - * - tell mio to read or write with a fd - * - process accept, read, write, and close requests - * - * Note: normal fd's don't get events unless the app calls mio_read/write() first! - */ - -/** the master mio mama */ -struct mio_st; - -typedef struct mio_fd_st -{ - int fd; -} *mio_fd_t; - -/** these are the actions and a handler type assigned by the applicaiton using mio */ -typedef enum { action_ACCEPT, action_READ, action_WRITE, action_CLOSE } mio_action_t; -typedef int (*mio_handler_t) (struct mio_st **m, mio_action_t a, struct mio_fd_st *fd, void* data, void *arg); - -typedef struct mio_st -{ - void (*mio_free)(struct mio_st **m); - - struct mio_fd_st *(*mio_listen)(struct mio_st **m, int port, char *sourceip, - mio_handler_t app, void *arg); - - struct mio_fd_st *(*mio_connect)(struct mio_st **m, int port, char *hostip, - mio_handler_t app, void *arg); - - void (*mio_app)(struct mio_st **m, struct mio_fd_st *fd, - mio_handler_t app, void *arg); - - void (*mio_close)(struct mio_st **m, struct mio_fd_st *fd); - - void (*mio_write)(struct mio_st **m, struct mio_fd_st *fd); - - void (*mio_read)(struct mio_st **m, struct mio_fd_st *fd); - - void (*mio_run)(struct mio_st **m, int timeout); -} **mio_t; - -/** create/free the mio subsytem */ -JABBERD2_API mio_t mio_new(int maxfd); /* returns NULL if failed */ - -#define mio_free(m) (*m)->mio_free(m) - -/** for creating a new listen socket in this mio (returns new fd or <0) */ -#define mio_listen(m, port, sourceip, app, arg) \ - (*m)->mio_listen(m, port, sourceip, app, arg) - -/** for creating a new socket connected to this ip:port (returns new fd or <0, use mio_read/write first) */ -#define mio_connect(m, port, hostip, app, arg) \ - (*m)->mio_connect(m, port, hostip, app, arg) - -/** re-set the app handler */ -#define mio_app(m, fd, app, arg) (*m)->mio_app(m, fd, app, arg) - -/** request that mio close this fd */ -#define mio_close(m, fd) (*m)->mio_close(m, fd) - -/** mio should try the write action on this fd now */ -#define mio_write(m, fd) (*m)->mio_write(m, fd) - -/** process read events for this fd */ -#define mio_read(m, fd) (*m)->mio_read(m, fd) - -/** give some cpu time to mio to check it's sockets, 0 is non-blocking */ -#define mio_run(m, timeout) (*m)->mio_run(m, timeout) - -/** all MIO related routines should use those for error reporting */ -#ifndef _WIN32 -# define MIO_ERROR errno -# define MIO_STRERROR(errno) strerror(errno) -#else /* _WIN32 */ -JABBERD2_API char *mio_strerror(int code); -# define MIO_ERROR WSAGetLastError() +/* + * jabberd - Jabber Open Source Server + * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney, + * Ryan Eatmon, Robert Norris + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA + */ + +#ifndef INCL_MIO_H +#define INCL_MIO_H + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "ac-stdint.h" + +/* jabberd2 Windows DLL */ +#ifndef JABBERD2_API +# ifdef _WIN32 +# ifdef JABBERD2_EXPORTS +# define JABBERD2_API __declspec(dllexport) +# else /* JABBERD2_EXPORTS */ +# define JABBERD2_API __declspec(dllimport) +# endif /* JABBERD2_EXPORTS */ +# else /* _WIN32 */ +# define JABBERD2_API extern +# endif /* _WIN32 */ +#endif /* JABBERD2_API */ + +#ifdef _WIN32 +# define MIO_MAXFD FD_SETSIZE +#else +# define MIO_MAXFD 1024 +#endif + +#include +#include +#include +#include + +#ifdef HAVE_UNISTD_H +# include +#endif + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif + +#ifdef HAVE_FCNTL_H +# include +#endif + +#ifdef HAVE_SYS_IOCTL_H +# include +#endif + +#ifdef HAVE_SYS_FILIO_H +# include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file mio/mio.h + * @brief mio - manage i/o + * + * This used to be something large and all inclusive for 1.2/1.4, + * but for 1.5 and beyond it is the most simple fd wrapper possible. + * It is also customized per-app and may be limited/extended depending on needs. + * + * Usage is pretty simple: + * - create a manager + * - add fds or tell it to listen + * - assign an action handler + * - tell mio to read or write with a fd + * - process accept, read, write, and close requests + * + * Note: normal fd's don't get events unless the app calls mio_read/write() first! + */ + +/** the master mio mama */ +struct mio_st; + +typedef struct mio_fd_st +{ + int fd; +} *mio_fd_t; + +/** these are the actions and a handler type assigned by the applicaiton using mio */ +typedef enum { action_ACCEPT, action_READ, action_WRITE, action_CLOSE } mio_action_t; +typedef int (*mio_handler_t) (struct mio_st **m, mio_action_t a, struct mio_fd_st *fd, void* data, void *arg); + +typedef struct mio_st +{ + void (*mio_free)(struct mio_st **m); + + struct mio_fd_st *(*mio_listen)(struct mio_st **m, int port, char *sourceip, + mio_handler_t app, void *arg); + + struct mio_fd_st *(*mio_connect)(struct mio_st **m, int port, char *hostip, + mio_handler_t app, void *arg); + + void (*mio_app)(struct mio_st **m, struct mio_fd_st *fd, + mio_handler_t app, void *arg); + + void (*mio_close)(struct mio_st **m, struct mio_fd_st *fd); + + void (*mio_write)(struct mio_st **m, struct mio_fd_st *fd); + + void (*mio_read)(struct mio_st **m, struct mio_fd_st *fd); + + void (*mio_run)(struct mio_st **m, int timeout); +} **mio_t; + +/** create/free the mio subsytem */ +JABBERD2_API mio_t mio_new(int maxfd); /* returns NULL if failed */ + +#define mio_free(m) (*m)->mio_free(m) + +/** for creating a new listen socket in this mio (returns new fd or <0) */ +#define mio_listen(m, port, sourceip, app, arg) \ + (*m)->mio_listen(m, port, sourceip, app, arg) + +/** for creating a new socket connected to this ip:port (returns new fd or <0, use mio_read/write first) */ +#define mio_connect(m, port, hostip, app, arg) \ + (*m)->mio_connect(m, port, hostip, app, arg) + +/** re-set the app handler */ +#define mio_app(m, fd, app, arg) (*m)->mio_app(m, fd, app, arg) + +/** request that mio close this fd */ +#define mio_close(m, fd) (*m)->mio_close(m, fd) + +/** mio should try the write action on this fd now */ +#define mio_write(m, fd) (*m)->mio_write(m, fd) + +/** process read events for this fd */ +#define mio_read(m, fd) (*m)->mio_read(m, fd) + +/** give some cpu time to mio to check it's sockets, 0 is non-blocking */ +#define mio_run(m, timeout) (*m)->mio_run(m, timeout) + +/** all MIO related routines should use those for error reporting */ +#ifndef _WIN32 +# define MIO_ERROR errno +# define MIO_STRERROR(errno) strerror(errno) +# define MIO_WOULDBLOCK (errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) +#else /* _WIN32 */ +JABBERD2_API char *mio_strerror(int code); +# define MIO_ERROR WSAGetLastError() # define MIO_STRERROR(errno) mio_strerror(errno) -#endif /* _WIN32 */ - -#ifdef __cplusplus -} -#endif - -#endif /* INCL_MIO_H */ - +# define MIO_WOULDBLOCK (WSAGetLastError() == WSAEWOULDBLOCK) +#endif /* _WIN32 */ + +#ifdef __cplusplus +} +#endif + +#endif /* INCL_MIO_H */ + diff --git a/resolver/resolver.c b/resolver/resolver.c index aec4f2dd..d65b78a4 100644 --- a/resolver/resolver.c +++ b/resolver/resolver.c @@ -146,7 +146,7 @@ static int _resolver_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { len = recv(r->fd->fd, buf->data, buf->len, 0); if(len < 0) { - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) { + if(MIO_WOULDBLOCK) { buf->len = 0; return 0; } @@ -180,7 +180,7 @@ static int _resolver_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { return len; } - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) + if(MIO_WOULDBLOCK) return 0; log_write(r->log, LOG_NOTICE, "[%d] [router] write error: %s (%d)", r->fd->fd, MIO_STRERROR(MIO_ERROR), MIO_ERROR); @@ -540,8 +540,7 @@ JABBER_MAIN("jabberd2resolver", "Jabber 2 Resolver", "Jabber Open Source Server: jabber_signal(SIGPIPE, SIG_IGN); #endif - r = (resolver_t) malloc(sizeof(struct resolver_st)); - memset(r, 0, sizeof(struct resolver_st)); + r = (resolver_t) calloc(1, sizeof(struct resolver_st)); /* load our config */ r->config = config_new(); diff --git a/router/router.c b/router/router.c index 256a416e..cce0e74d 100644 --- a/router/router.c +++ b/router/router.c @@ -562,7 +562,7 @@ static int _router_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { } if(len < 0) { - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) { + if(MIO_WOULDBLOCK) { buf->len = 0; return 0; } @@ -596,7 +596,7 @@ static int _router_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { return len; } - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) + if(MIO_WOULDBLOCK) return 0; log_debug(ZONE, "write failed: %s", strerror(errno)); @@ -914,8 +914,7 @@ int router_mio_callback(mio_t m, mio_action_t a, mio_fd_t fd, void *data, void * if(_router_accept_check(r, fd, (char *) data) != 0) return 1; - comp = (component_t) malloc(sizeof(struct component_st)); - memset(comp, 0, sizeof(struct component_st)); + comp = (component_t) calloc(1, sizeof(struct component_st)); comp->r = r; diff --git a/s2s/in.c b/s2s/in.c index d93ea99e..ec251b8a 100644 --- a/s2s/in.c +++ b/s2s/in.c @@ -114,8 +114,7 @@ int in_mio_callback(mio_t m, mio_action_t a, mio_fd_t fd, void *data, void *arg) log_write(s2s->log, LOG_NOTICE, "[%d] [%s, port=%d] incoming connection", fd->fd, (char *) data, port); /* new conn */ - in = (conn_t) malloc(sizeof(struct conn_st)); - memset(in, 0, sizeof(struct conn_st)); + in = (conn_t) calloc(1, sizeof(struct conn_st)); in->s2s = s2s; @@ -132,6 +131,9 @@ int in_mio_callback(mio_t m, mio_action_t a, mio_fd_t fd, void *data, void *arg) in->s = sx_new(s2s->sx_env, in->fd->fd, _in_sx_callback, (void *) in); mio_app(m, in->fd, in_mio_callback, (void *) in); + if(s2s->stanza_size_limit != 0) + in->s->rbytesmax = s2s->stanza_size_limit; + /* add to incoming connections hash */ snprintf(ipport, INET6_ADDRSTRLEN + 16, "%s/%d", in->ip, in->port); xhash_put(s2s->in_accept, pstrdup(xhash_pool(s2s->in_accept),ipport), (void *) in); @@ -173,7 +175,7 @@ static int _in_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { len = recv(in->fd->fd, buf->data, buf->len, 0); if(len < 0) { - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) { + if(MIO_WOULDBLOCK) { buf->len = 0; return 0; } @@ -207,7 +209,7 @@ static int _in_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { return len; } - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) + if(MIO_WOULDBLOCK) return 0; log_write(in->s2s->log, LOG_NOTICE, "[%d] [%s, port=%d] write error: %s (%d)", in->fd->fd, in->ip, in->port, MIO_STRERROR(MIO_ERROR), MIO_ERROR); @@ -404,8 +406,7 @@ static void _in_result(conn_t in, nad_t nad) { nad_append_cdata(verify, NAD_CDATA(nad, 0), NAD_CDATA_L(nad, 0), 1); /* new packet */ - pkt = (pkt_t) malloc(sizeof(struct pkt_st)); - memset(pkt, 0, sizeof(struct pkt_st)); + pkt = (pkt_t) calloc(1, sizeof(struct pkt_st)); pkt->nad = verify; diff --git a/s2s/out.c b/s2s/out.c index ae691620..86c3b501 100644 --- a/s2s/out.c +++ b/s2s/out.c @@ -166,8 +166,7 @@ void out_packet(s2s_t s2s, pkt_t pkt) { /* new resolution */ log_debug(ZONE, "no dns for %s, preparing for resolution", pkt->to->domain); - dns = (dnscache_t) malloc(sizeof(struct dnscache_st)); - memset(dns, 0, sizeof(struct dnscache_st)); + dns = (dnscache_t) calloc(1, sizeof(struct dnscache_st)); strcpy(dns->name, pkt->to->domain); @@ -237,8 +236,7 @@ void out_packet(s2s_t s2s, pkt_t pkt) { _out_packet_queue(s2s, pkt); /* no conn, create one */ - out = (conn_t) malloc(sizeof(struct conn_st)); - memset(out, 0, sizeof(struct conn_st)); + out = (conn_t) calloc(1, sizeof(struct conn_st)); out->s2s = s2s; @@ -556,7 +554,7 @@ static int _out_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { len = recv(out->fd->fd, buf->data, buf->len, 0); if(len < 0) { - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) { + if(MIO_WOULDBLOCK) { buf->len = 0; return 0; } @@ -590,7 +588,7 @@ static int _out_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { return len; } - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) + if(MIO_WOULDBLOCK) return 0; log_write(out->s2s->log, LOG_NOTICE, "[%d] [%s, port=%d] write error: %s (%d)", out->fd->fd, out->ip, out->port, MIO_STRERROR(MIO_ERROR), MIO_ERROR); diff --git a/s2s/router.c b/s2s/router.c index 7c00879b..6b53d1df 100644 --- a/s2s/router.c +++ b/s2s/router.c @@ -47,7 +47,7 @@ int s2s_router_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { len = recv(s2s->fd->fd, buf->data, buf->len, 0); if(len < 0) { - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) { + if(MIO_WOULDBLOCK) { buf->len = 0; return 0; } @@ -81,7 +81,7 @@ int s2s_router_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { return len; } - if(errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) + if(MIO_WOULDBLOCK) return 0; log_write(s2s->log, LOG_NOTICE, "[%d] [router] write error: %s (%d)", s2s->fd->fd, MIO_STRERROR(MIO_ERROR), MIO_ERROR); @@ -250,8 +250,7 @@ int s2s_router_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { } /* new packet */ - pkt = (pkt_t) malloc(sizeof(struct pkt_st)); - memset(pkt, 0, sizeof(struct pkt_st)); + pkt = (pkt_t) calloc(1, sizeof(struct pkt_st)); pkt->nad = nad; diff --git a/sm/sm.c b/sm/sm.c index 598f82bb..373c52ca 100644 --- a/sm/sm.c +++ b/sm/sm.c @@ -56,7 +56,7 @@ int sm_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { len = recv(sm->fd->fd, buf->data, buf->len, 0); if (len < 0) { - if (errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) { + if (MIO_WOULDBLOCK) { buf->len = 0; return 0; } @@ -90,7 +90,7 @@ int sm_sx_callback(sx_t s, sx_event_t e, void *data, void *arg) { return len; } - if (errno == EWOULDBLOCK || errno == EINTR || errno == EAGAIN) + if (MIO_WOULDBLOCK) return 0; log_write(sm->log, LOG_NOTICE, "[%d] [router] write error: %s (%d)", sm->fd->fd, MIO_STRERROR(MIO_ERROR), MIO_ERROR);