#include "hostmask.h"
#include "sslproc.h"
#include "hash.h"
+#include "s_assert.h"
+#include "logger.h"
#ifndef INADDR_NONE
#define INADDR_NONE ((unsigned int) 0xffffffff)
{ { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } };
#endif
-static listener_t *ListenerPollList = NULL;
+static struct Listener *ListenerPollList = NULL;
static int accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
static void accept_callback(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t addrlen, void *data);
-static listener_t *
+static struct Listener *
make_listener(struct rb_sockaddr_storage *addr)
{
- listener_t *listener = (listener_t *) rb_malloc(sizeof(listener_t));
+ struct Listener *listener = (struct Listener *) rb_malloc(sizeof(struct Listener));
s_assert(0 != listener);
listener->name = me.name;
listener->F = NULL;
}
void
-free_listener(listener_t *listener)
+free_listener(struct Listener *listener)
{
s_assert(NULL != listener);
if(listener == NULL)
ListenerPollList = listener->next;
else
{
- listener_t *prev = ListenerPollList;
+ struct Listener *prev = ListenerPollList;
for (; prev; prev = prev->next)
{
if(listener == prev->next)
* returns "host.foo.org:6667" for a given listener
*/
const char *
-get_listener_name(const listener_t *listener)
+get_listener_name(const struct Listener *listener)
{
static char buf[HOSTLEN + HOSTLEN + PORTNAMELEN + 4];
int port = 0;
void
show_ports(struct Client *source_p)
{
- listener_t *listener = 0;
+ struct Listener *listener = 0;
for (listener = ListenerPollList; listener; listener = listener->next)
{
#endif
static int
-inetport(listener_t *listener)
+inetport(struct Listener *listener)
{
rb_fde_t *F;
- int ret;
int opt = 1;
/*
return 0;
}
- if((ret = rb_listen(F, RATBOX_SOMAXCONN)))
+ if(rb_listen(F, RATBOX_SOMAXCONN, listener->defer_accept))
{
ilog_error("listen()");
rb_close(F);
return 1;
}
-static listener_t *
+static struct Listener *
find_listener(struct rb_sockaddr_storage *addr)
{
- listener_t *listener = NULL;
- listener_t *last_closed = NULL;
+ struct Listener *listener = NULL;
+ struct Listener *last_closed = NULL;
for (listener = ListenerPollList; listener; listener = listener->next)
{
* the format "255.255.255.255"
*/
void
-add_listener(int port, const char *vhost_ip, int family, int ssl)
+add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_accept)
{
- listener_t *listener;
+ struct Listener *listener;
struct rb_sockaddr_storage vaddr;
/*
listener->F = NULL;
listener->ssl = ssl;
+ listener->defer_accept = defer_accept;
if(inetport(listener))
listener->active = 1;
* close_listener - close a single listener
*/
void
-close_listener(listener_t *listener)
+close_listener(struct Listener *listener)
{
s_assert(listener != NULL);
if(listener == NULL)
void
close_listeners()
{
- listener_t *listener;
- listener_t *listener_next = 0;
+ struct Listener *listener;
+ struct Listener *listener_next = 0;
/*
* close all 'extra' listening ports we have
*/
* any client list yet.
*/
static void
-add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, void *ssl_ctl, int exempt)
+add_connection(struct Listener *listener, rb_fde_t *F, struct sockaddr *sai, struct sockaddr *lai, void *ssl_ctl)
{
struct Client *new_client;
s_assert(NULL != listener);
new_client = make_client(NULL);
memcpy(&new_client->localClient->ip, sai, sizeof(struct rb_sockaddr_storage));
+ memcpy(&new_client->preClient->lip, lai, sizeof(struct rb_sockaddr_storage));
/*
* copy address to 'sockhost' as a string, copy it to host too
++listener->ref_count;
- if(!exempt)
- {
- if(check_reject(new_client))
- return;
- if(add_unknown_ip(new_client))
- return;
- }
-
start_auth(new_client);
}
+static const char *toofast = "ERROR :Reconnecting too fast, throttled.\r\n";
+
static int
accept_precallback(rb_fde_t *F, struct sockaddr *addr, rb_socklen_t addrlen, void *data)
{
char buf[BUFSIZE];
struct ConfItem *aconf;
static time_t last_oper_notice = 0;
+ int len;
if(listener->ssl && (!ssl_ok || !get_ssld_count()))
{
rb_write(F, "ERROR :All connections in use\r\n", 32);
rb_close(F);
- /* Re-register a new IO request for the next accept .. */
return 0;
}
if(ConfigFileEntry.dline_with_reason)
{
- if (rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", aconf->passwd) >= (int)(sizeof(buf)-1))
+ len = rb_snprintf(buf, sizeof(buf), "ERROR :*** Banned: %s\r\n", get_user_ban_reason(aconf));
+ if (len >= (int)(sizeof(buf)-1))
{
buf[sizeof(buf) - 3] = '\r';
buf[sizeof(buf) - 2] = '\n';
return 0;
}
+ if(check_reject(F, addr))
+ return 0;
+
+ if(throttle_add(addr))
+ {
+ rb_write(F, toofast, strlen(toofast));
+ rb_close(F);
+ return 0;
+ }
+
return 1;
}
{
ssl_ctl_t *ctl;
rb_fde_t *xF[2];
- rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection");
+ if(rb_socketpair(AF_UNIX, SOCK_STREAM, 0, &xF[0], &xF[1], "Incoming ssld Connection") == -1)
+ {
+ ilog_error("creating SSL/TLS socket pairs");
+ rb_close(F);
+ return;
+ }
ctl = start_ssld_accept(F, xF[1], rb_get_fd(xF[0])); /* this will close F for us */
- add_connection(listener, xF[0], addr, ctl, 1);
+ add_connection(listener, xF[0], addr, laddr, ctl);
}
static void
if(getsockname(rb_get_fd(F), (struct sockaddr *) &lip, &locallen) < 0)
{
- /* this shouldn't fail so... */
- /* XXX add logging of this */
+ /* this can fail if the connection disappeared in the meantime */
rb_close(F);
+ return;
}
if(listener->ssl)
accept_ssld(F, addr, (struct sockaddr *)&lip, listener);
else
- add_connection(listener, F, addr, NULL, 1);
+ add_connection(listener, F, addr, (struct sockaddr *)&lip, NULL);
}