From 53e8a8b05b7f0696ce72ccfd347a35bb80f19570 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 21 Apr 2014 15:29:05 +0100 Subject: [PATCH] Finish conversion to new inet addr structure. --- src/buffer.c | 2 +- src/controller.c | 6 ++--- src/logger.c | 13 +++++++---- src/logger.h | 10 +++------ src/lua/lua_common.c | 4 ++-- src/lua_worker.c | 46 +++++++------------------------------- src/plugins/fuzzy_check.c | 12 +--------- src/plugins/spf.c | 47 +++++++++------------------------------ src/protocol.c | 34 +++------------------------- src/smtp_proxy.c | 27 ++++++++++------------ src/webui.c | 32 ++++++-------------------- src/worker.c | 5 +++-- 12 files changed, 62 insertions(+), 176 deletions(-) diff --git a/src/buffer.c b/src/buffer.c index 95104d2a5..864f2fad6 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -30,7 +30,7 @@ #endif #define G_DISPATCHER_ERROR dispatcher_error_quark() -#define debug_ip(...) rspamd_conditional_debug(rspamd_main->logger, d->peer_addr, __FUNCTION__, __VA_ARGS__) +#define debug_ip(...) rspamd_conditional_debug(rspamd_main->logger, NULL, __FUNCTION__, __VA_ARGS__) static void dispatcher_cb (gint fd, short what, void *arg); diff --git a/src/controller.c b/src/controller.c index 3b48bf5cd..f8b49d236 100644 --- a/src/controller.c +++ b/src/controller.c @@ -1488,7 +1488,6 @@ controller_read_socket (f_str_t * in, void *arg) } /* Set up async session */ task->s = new_async_session (task->task_pool, fin_learn_task, restore_learn_task, free_task_hard, task); - task->dispatcher = session->dispatcher; session->learn_task = task; session->state = STATE_LEARN_SPAM; rspamd_dispatcher_pause (session->dispatcher); @@ -1692,7 +1691,6 @@ controller_write_socket (void *arg) } } } - session->learn_task->dispatcher = NULL; destroy_session (session->learn_task->s); session->state = STATE_REPLY; if (! rspamd_dispatcher_write (session->dispatcher, out_buf, i, FALSE, FALSE)) { @@ -1738,13 +1736,13 @@ accept_socket (gint fd, short what, void *arg) union sa_union su; struct controller_session *new_session; struct timeval *io_tv; - socklen_t addrlen = sizeof (su.ss); gint nfd; struct rspamd_controller_ctx *ctx; + rspamd_inet_addr_t addr; ctx = worker->ctx; - if ((nfd = accept_from_socket (fd, (struct sockaddr *)&su.ss, &addrlen)) == -1) { + if ((nfd = rspamd_accept_from_socket (fd, &addr)) == -1) { return; } diff --git a/src/logger.c b/src/logger.c index 285e8decb..01814d24d 100644 --- a/src/logger.c +++ b/src/logger.c @@ -711,15 +711,20 @@ file_log_function (const gchar * log_domain, const gchar *function, GLogLevelFla * Write log line depending on ip */ void -rspamd_conditional_debug (rspamd_logger_t *rspamd_log, guint32 addr, const gchar *function, const gchar *fmt, ...) +rspamd_conditional_debug (rspamd_logger_t *rspamd_log, + rspamd_inet_addr_t *addr, const gchar *function, const gchar *fmt, ...) { static gchar logbuf[BUFSIZ]; va_list vp; u_char *end; - if (rspamd_log->cfg->log_level >= G_LOG_LEVEL_DEBUG || rspamd_log->is_debug || - (rspamd_log->debug_ip != NULL && radix32tree_find (rspamd_log->debug_ip, ntohl (addr)) != RADIX_NO_VALUE)) { - + if (rspamd_log->cfg->log_level >= G_LOG_LEVEL_DEBUG || rspamd_log->is_debug) { + if (rspamd_log->debug_ip && addr != NULL) { + if (addr->af == AF_INET && radix32tree_find (rspamd_log->debug_ip, + ntohl (addr->addr.s4.sin_addr.s_addr)) == RADIX_NO_VALUE) { + return; + } + } g_mutex_lock (rspamd_log->mtx); va_start (vp, fmt); end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp); diff --git a/src/logger.h b/src/logger.h index 6f46080de..b0766b938 100644 --- a/src/logger.h +++ b/src/logger.h @@ -70,7 +70,7 @@ void rspamd_common_logv (rspamd_logger_t *logger, * Conditional debug function */ void rspamd_conditional_debug (rspamd_logger_t *logger, - guint32 addr, const gchar *function, const gchar *fmt, ...) ; + rspamd_inet_addr_t *addr, const gchar *function, const gchar *fmt, ...) ; /** * Function with variable number of arguments support that uses static default logger @@ -104,12 +104,8 @@ void rspamd_log_nodebug (rspamd_logger_t *logger); #define msg_err(...) rspamd_common_log_function(rspamd_main->logger, G_LOG_LEVEL_CRITICAL, __FUNCTION__, __VA_ARGS__) #define msg_warn(...) rspamd_common_log_function(rspamd_main->logger, G_LOG_LEVEL_WARNING, __FUNCTION__, __VA_ARGS__) #define msg_info(...) rspamd_common_log_function(rspamd_main->logger, G_LOG_LEVEL_INFO, __FUNCTION__, __VA_ARGS__) -#define msg_debug(...) rspamd_conditional_debug(rspamd_main->logger, -1, __FUNCTION__, __VA_ARGS__) -#ifdef HAVE_INET_PTON -# define debug_task(...) rspamd_conditional_debug(rspamd_main->logger, task->from_addr.d.in4.s_addr, __FUNCTION__, __VA_ARGS__) -#else -# define debug_task(...) rspamd_conditional_debug(rspamd_main->logger, task->from_addr.s_addr, __FUNCTION__, __VA_ARGS__) -#endif +#define msg_debug(...) rspamd_conditional_debug(rspamd_main->logger, NULL, __FUNCTION__, __VA_ARGS__) +#define debug_task(...) rspamd_conditional_debug(rspamd_main->logger, &task->from_addr, __FUNCTION__, __VA_ARGS__) #else #define msg_err(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, __FUNCTION__, __VA_ARGS__) #define msg_warn(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, __FUNCTION__, __VA_ARGS__) diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index ee624db3d..f7912a722 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -170,7 +170,7 @@ lua_common_log (GLogLevelFlags level, lua_State *L, const gchar *msg) } rspamd_snprintf (func_buf, sizeof (func_buf), "%s:%d", p, d.currentline); if (level == G_LOG_LEVEL_DEBUG) { - rspamd_conditional_debug (rspamd_main->logger, -1, func_buf, "%s", msg); + rspamd_conditional_debug (rspamd_main->logger, NULL, func_buf, "%s", msg); } else { rspamd_common_log_function (rspamd_main->logger, level, func_buf, "%s", msg); @@ -178,7 +178,7 @@ lua_common_log (GLogLevelFlags level, lua_State *L, const gchar *msg) } else { if (level == G_LOG_LEVEL_DEBUG) { - rspamd_conditional_debug (rspamd_main->logger, -1, __FUNCTION__, "%s", msg); + rspamd_conditional_debug (rspamd_main->logger, NULL, __FUNCTION__, "%s", msg); } else { rspamd_common_log_function (rspamd_main->logger, level, __FUNCTION__, "%s", msg); diff --git a/src/lua_worker.c b/src/lua_worker.c index 319242a3a..c6482bcfd 100644 --- a/src/lua_worker.c +++ b/src/lua_worker.c @@ -325,52 +325,26 @@ lua_accept_socket (gint fd, short what, void *arg) { struct rspamd_worker *worker = (struct rspamd_worker *) arg; struct rspamd_lua_worker_ctx *ctx, **pctx; - union sa_union su; - socklen_t addrlen = sizeof (su.ss); gint nfd; - struct in_addr addr; - gchar *addr_str = NULL; lua_State *L; + rspamd_inet_addr_t addr; ctx = worker->ctx; L = ctx->L; if ((nfd = - accept_from_socket (fd, (struct sockaddr *) &su.ss, &addrlen)) == -1) { + rspamd_accept_from_socket (fd, &addr)) == -1) { msg_warn ("accept failed: %s", strerror (errno)); return; } /* Check for EAGAIN */ - if (nfd == 0){ + if (nfd == 0) { return; } - if (su.ss.ss_family == AF_UNIX) { - msg_info ("accepted connection from unix socket"); - addr.s_addr = INADDR_NONE; - addr_str = "127.0.0.1"; - } - else if (su.ss.ss_family == AF_INET) { - msg_info ("accepted connection from %s port %d", - inet_ntoa (su.s4.sin_addr), ntohs (su.s4.sin_port)); - memcpy (&addr, &su.s4.sin_addr, - sizeof (struct in_addr)); - addr_str = g_strdup (inet_ntoa (su.s4.sin_addr)); - } - else if (su.ss.ss_family == AF_INET6) { - addr_str = g_malloc0 (INET6_ADDRSTRLEN + 1); - /* XXX: support ipv6 addresses here */ - addr.s_addr = INADDR_NONE; - inet_ntop (AF_INET6, &su.s6.sin6_addr, addr_str, INET6_ADDRSTRLEN); - msg_info ("accepted connection from [%s] port %d", - addr_str, ntohs (su.s6.sin6_port)); - } - else { - addr.s_addr = INADDR_NONE; - msg_err ("accepted connection from unsupported address family: %d", su.ss.ss_family); - close (nfd); - return; - } + msg_info ("accepted connection from %s port %d", + rspamd_inet_address_to_string (&addr), + rspamd_inet_address_get_port (&addr)); /* Call finalizer function */ lua_rawgeti (L, LUA_REGISTRYINDEX, ctx->cbref_accept); @@ -378,17 +352,13 @@ lua_accept_socket (gint fd, short what, void *arg) lua_setclass (L, "rspamd{worker}", -1); *pctx = ctx; lua_pushinteger (L, nfd); - lua_pushstring (L, addr_str); - lua_pushinteger (L, addr.s_addr); + lua_pushstring (L, rspamd_inet_address_to_string (&addr)); + lua_pushinteger (L, 0); if (lua_pcall (L, 4, 0, 0) != 0) { msg_info ("call to worker accept failed: %s", lua_tostring (L, -1)); } - - if (addr_str) { - g_free (addr_str); - } } static gboolean diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index 195ee19da..a6c1ed0f2 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -821,24 +821,14 @@ fuzzy_symbol_callback (struct rspamd_task *task, void *unused) GList *cur; /* Check whitelist */ -#ifdef HAVE_INET_PTON if (fuzzy_module_ctx->whitelist && task->from_addr.af == AF_INET) { if (radix32tree_find (fuzzy_module_ctx->whitelist, ntohl (task->from_addr.addr.s4.sin_addr.s_addr)) != RADIX_NO_VALUE) { msg_info ("<%s>, address %s is whitelisted, skip fuzzy check", - task->message_id, inet_ntoa (task->from_addr.d.in4)); + task->message_id, rspamd_inet_address_to_string (&task->from_addr)); return; } } -#else - if (fuzzy_module_ctx->whitelist && task->from_addr.s_addr != 0) { - if (radix32tree_find (fuzzy_module_ctx->whitelist, ntohl ((guint32) task->from_addr.s_addr)) != RADIX_NO_VALUE) { - msg_info ("<%s>, address %s is whitelisted, skip fuzzy check", - task->message_id, inet_ntoa (task->from_addr)); - return; - } - } -#endif cur = fuzzy_module_ctx->fuzzy_rules; while (cur) { diff --git a/src/plugins/spf.c b/src/plugins/spf.c index 94245ea7e..19aaa44e5 100644 --- a/src/plugins/spf.c +++ b/src/plugins/spf.c @@ -161,25 +161,24 @@ static gboolean spf_check_element (struct spf_addr *addr, struct rspamd_task *task) { gboolean res = FALSE; -#ifdef HAVE_INET_PTON guint8 *s, *d, t; guint nbits, addrlen; struct in_addr in4s, in4d; struct in6_addr in6s, in6d; /* Basic comparing algorithm */ - if (addr->data.normal.ipv6 == task->from_addr.ipv6) { + if (addr->data.normal.ipv6 && task->from_addr.af == AF_INET6) { if (addr->data.normal.ipv6) { addrlen = sizeof (struct in6_addr); memcpy (&in6s, &addr->data.normal.d.in6, sizeof (struct in6_addr)); - memcpy (&in6d, &task->from_addr.d.in6, sizeof (struct in6_addr)); + memcpy (&in6d, &task->from_addr.addr.s6.sin6_addr, sizeof (struct in6_addr)); s = (guint8 *)&in6s; d = (guint8 *)&in6d; } else { addrlen = sizeof (struct in_addr); memcpy (&in4s, &addr->data.normal.d.in4, sizeof (struct in_addr)); - memcpy (&in4d, &task->from_addr.d.in4, sizeof (struct in_addr)); + memcpy (&in4d, &task->from_addr.addr.s4.sin_addr, sizeof (struct in_addr)); s = (guint8 *)&in4s; d = (guint8 *)&in4d; } @@ -215,18 +214,6 @@ spf_check_element (struct spf_addr *addr, struct rspamd_task *task) res = FALSE; } } -#else - guint32 s, m; - - if (addr->data.normal.mask == 0) { - m = 0; - } - else { - m = htonl (G_MAXUINT32 << (32 - addr->data.normal.mask)); - } - s = task->from_addr.s_addr; - res = (s & m) == (addr->data.normal.d.in4.s_addr & m); -#endif if (res) { switch (addr->mech) { @@ -300,28 +287,16 @@ spf_symbol_callback (struct rspamd_task *task, void *unused) gchar *domain; GList *l; -#ifdef HAVE_INET_PTON - if (task->from_addr.has_addr) { - if (TRUE) { -#else - if (task->from_addr.s_addr != INADDR_NONE && task->from_addr.s_addr != INADDR_ANY) { - if (radix32tree_find (spf_module_ctx->whitelist_ip, ntohl (task->from_addr.s_addr)) == RADIX_NO_VALUE) { -#endif - domain = get_spf_domain (task); - if (domain) { - if ((l = rspamd_lru_hash_lookup (spf_module_ctx->spf_hash, domain, task->tv.tv_sec)) != NULL) { - spf_check_list (l, task); - } - else if (!resolve_spf (task, spf_plugin_callback)) { - msg_info ("cannot make spf request for [%s]", task->message_id); - } + if (task->from_addr.af != AF_UNIX) { + domain = get_spf_domain (task); + if (domain) { + if ((l = rspamd_lru_hash_lookup (spf_module_ctx->spf_hash, domain, task->tv.tv_sec)) != NULL) { + spf_check_list (l, task); + } + else if (!resolve_spf (task, spf_plugin_callback)) { + msg_info ("cannot make spf request for [%s]", task->message_id); } } -#ifndef HAVE_INET_PTON - else { - msg_info ("ip %s is whitelisted for spf checks", inet_ntoa (task->from_addr)); - } -#endif } } diff --git a/src/protocol.c b/src/protocol.c index 9da4f445d..8a5c3f0df 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -312,39 +312,10 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, struct rspamd_http_mes case 'I': if (g_ascii_strcasecmp (headern, IP_ADDR_HEADER) == 0) { tmp = h->value->str; -#ifdef HAVE_INET_PTON - if (g_ascii_strncasecmp (tmp, "IPv6:", 5) == 0) { - if (inet_pton (AF_INET6, tmp + 6, &task->from_addr.d.in6) == 1) { - task->from_addr.ipv6 = TRUE; - } - else { - msg_err ("bad ip header: '%s'", tmp); - return FALSE; - } - task->from_addr.has_addr = TRUE; - } - else { - if (inet_pton (AF_INET, tmp, &task->from_addr.d.in4) != 1) { - /* Try ipv6 */ - if (inet_pton (AF_INET6, tmp, &task->from_addr.d.in6) == 1) { - task->from_addr.ipv6 = TRUE; - } - else { - msg_err ("bad ip header: '%s'", tmp); - return FALSE; - } - } - else { - task->from_addr.ipv6 = FALSE; - } - task->from_addr.has_addr = TRUE; - } -#else - if (!inet_aton (tmp, &task->from_addr)) { + if (!rspamd_parse_inet_address (&task->from_addr, tmp)) { msg_err ("bad ip header: '%s'", tmp); return FALSE; } -#endif debug_task ("read IP header, value: %s", tmp); } else { @@ -482,7 +453,8 @@ urls_protocol_cb (gpointer key, gpointer value, gpointer ud) if (cb->task->cfg->log_urls) { msg_info ("<%s> URL: %s - %s: %s", cb->task->message_id, cb->task->user ? cb->task->user : (cb->task->from ? cb->task->from : "unknown"), - inet_ntoa (cb->task->client_addr), struri (url)); + rspamd_inet_address_to_string (&cb->task->from_addr), + struri (url)); } return FALSE; diff --git a/src/smtp_proxy.c b/src/smtp_proxy.c index bffad8e48..c4fa23605 100644 --- a/src/smtp_proxy.c +++ b/src/smtp_proxy.c @@ -667,7 +667,7 @@ smtp_dns_cb (struct rdns_reply *reply, void *arg) /* Parse reverse reply and start resolve of this ip */ if (reply->code != RDNS_RC_NOERROR) { rspamd_conditional_debug (rspamd_main->logger, - session->client_addr.s_addr, __FUNCTION__, "DNS error: %s", + NULL, __FUNCTION__, "DNS error: %s", rdns_strerror (reply->code)); if (reply->code == RDNS_RC_NXDOMAIN) { @@ -696,7 +696,7 @@ smtp_dns_cb (struct rdns_reply *reply, void *arg) case SMTP_PROXY_STATE_RESOLVE_NORMAL: if (reply->code != RDNS_RC_NOERROR) { rspamd_conditional_debug (rspamd_main->logger, - session->client_addr.s_addr, __FUNCTION__, "DNS error: %s", + NULL, __FUNCTION__, "DNS error: %s", rdns_strerror (reply->code)); if (reply->code == RDNS_RC_NXDOMAIN) { @@ -901,14 +901,15 @@ static void accept_socket (gint fd, short what, void *arg) { struct rspamd_worker *worker = (struct rspamd_worker *)arg; - union sa_union su; struct smtp_proxy_session *session; struct smtp_proxy_ctx *ctx; + rspamd_inet_addr_t addr; + gint nfd; - socklen_t addrlen = sizeof (su.ss); - gint nfd; + ctx = worker->ctx; - if ((nfd = accept_from_socket (fd, (struct sockaddr *)&su.ss, &addrlen)) == -1) { + if ((nfd = + rspamd_accept_from_socket (fd, &addr)) == -1) { msg_warn ("accept failed: %s", strerror (errno)); return; } @@ -917,18 +918,14 @@ accept_socket (gint fd, short what, void *arg) return; } + msg_info ("accepted connection from %s port %d", + rspamd_inet_address_to_string (&addr), + rspamd_inet_address_get_port (&addr)); + ctx = worker->ctx; session = g_slice_alloc0 (sizeof (struct smtp_proxy_session)); session->pool = rspamd_mempool_new (rspamd_mempool_suggest_size ()); - if (su.ss.ss_family == AF_UNIX) { - msg_info ("accepted connection from unix socket"); - session->client_addr.s_addr = INADDR_NONE; - } - else if (su.ss.ss_family == AF_INET) { - msg_info ("accepted connection from %s port %d", inet_ntoa (su.s4.sin_addr), ntohs (su.s4.sin_port)); - memcpy (&session->client_addr, &su.s4.sin_addr, sizeof (struct in_addr)); - } session->sock = nfd; session->worker = worker; @@ -936,7 +933,7 @@ accept_socket (gint fd, short what, void *arg) session->resolver = ctx->resolver; session->ev_base = ctx->ev_base; session->upstream_sock = -1; - session->ptr_str = rdns_generate_ptr_from_str (inet_ntoa (su.s4.sin_addr)); + session->ptr_str = rdns_generate_ptr_from_str (rspamd_inet_address_to_string (&addr)); worker->srv->stat->connections_count++; /* Resolve client's addr */ diff --git a/src/webui.c b/src/webui.c index 5e8c33a35..6c438135f 100644 --- a/src/webui.c +++ b/src/webui.c @@ -1691,47 +1691,29 @@ rspamd_webui_accept_socket (gint fd, short what, void *arg) struct rspamd_worker *worker = (struct rspamd_worker *) arg; struct rspamd_webui_worker_ctx *ctx; struct rspamd_webui_session *nsession; + rspamd_inet_addr_t addr; gint nfd; - union sa_union su; - socklen_t addrlen = sizeof (su); - char ip_str[INET6_ADDRSTRLEN + 1]; ctx = worker->ctx; if ((nfd = - accept_from_socket (fd, &su.sa, &addrlen)) == -1) { + rspamd_accept_from_socket (fd, &addr)) == -1) { msg_warn ("accept failed: %s", strerror (errno)); return; } /* Check for EAGAIN */ - if (nfd == 0){ + if (nfd == 0) { return; } + msg_info ("accepted connection from %s port %d", + rspamd_inet_address_to_string (&addr), + rspamd_inet_address_get_port (&addr)); + nsession = g_slice_alloc0 (sizeof (struct rspamd_webui_session)); nsession->pool = rspamd_mempool_new (rspamd_mempool_suggest_size ()); nsession->ctx = ctx; - if (su.sa.sa_family == AF_UNIX) { - msg_info ("accepted connection from unix socket"); - nsession->from_addr.has_addr = FALSE; - } - else if (su.sa.sa_family == AF_INET) { - msg_info ("accepted connection from %s port %d", - inet_ntoa (su.s4.sin_addr), ntohs (su.s4.sin_port)); - nsession->from_addr.has_addr = TRUE; - nsession->from_addr.d.in4.s_addr = su.s4.sin_addr.s_addr; - } - else if (su.sa.sa_family == AF_INET6) { - msg_info ("accepted connection from %s port %d", - inet_ntop (su.sa.sa_family, &su.s6.sin6_addr, ip_str, sizeof (ip_str)), - ntohs (su.s6.sin6_port)); - memcpy (&nsession->from_addr.d.in6, &su.s6.sin6_addr, - sizeof (struct in6_addr)); - nsession->from_addr.has_addr = TRUE; - nsession->from_addr.ipv6 = TRUE; - } - rspamd_http_router_handle_socket (ctx->http, nfd, nsession); } diff --git a/src/worker.c b/src/worker.c index 978ff72ec..8efb5a19e 100644 --- a/src/worker.c +++ b/src/worker.c @@ -254,7 +254,7 @@ rspamd_worker_error_handler (struct rspamd_http_connection *conn, GError *err) struct rspamd_task *task = (struct rspamd_task *) conn->ud; msg_info ("abnormally closing connection from: %s, error: %s", - inet_ntoa (task->client_addr), err->message); + rspamd_inet_address_to_string (&task->client_addr), err->message); if (task->state != CLOSING_CONNECTION) { /* We still need to write a reply */ task->error_code = err->code; @@ -275,7 +275,8 @@ rspamd_worker_finish_handler (struct rspamd_http_connection *conn, struct rspamd_task *task = (struct rspamd_task *) conn->ud; if (task->state == CLOSING_CONNECTION) { - msg_debug ("normally closing connection from: %s", inet_ntoa (task->client_addr)); + msg_debug ("normally closing connection from: %s", + rspamd_inet_address_to_string (&task->client_addr)); destroy_session (task->s); } else { -- 2.39.5