1
0
forked from bip/bip

Avoid unncessary calls to getaddrinfo.

Refactor calls to getaddrinfo only on socket establishement, cache in
connection structure.
This commit is contained in:
Arnaud Cornet 2007-12-21 12:20:21 +01:00
parent 8f998c97b6
commit 27b16a86b8
5 changed files with 67 additions and 56 deletions

View File

@ -59,6 +59,10 @@ void bip_notify(struct link_client *ic, char *fmt, ...);
void adm_list_connections(struct link_client *ic, struct user *bu); void adm_list_connections(struct link_client *ic, struct user *bu);
void free_conf(list_t *l); void free_conf(list_t *l);
#ifdef HAVE_OIDENTD
#define OIDENTD_FILENAME ".oidentd.conf"
#endif
static void hash_binary(char *hex, unsigned char **password, unsigned int *seed) static void hash_binary(char *hex, unsigned char **password, unsigned int *seed)
{ {
unsigned char *md5; unsigned char *md5;
@ -1083,6 +1087,7 @@ int main(int argc, char **argv)
int r, fd; int r, fd;
char buf[30]; char buf[30];
bip_t bip; bip_t bip;
char *home;
bip_init(&bip); bip_init(&bip);
_bip = &bip; _bip = &bip;
@ -1131,16 +1136,18 @@ int main(int argc, char **argv)
check_rlimits(); check_rlimits();
home = getenv("HOME");
if (!home) {
conf_die(&bip, "no $HOME !, do you live in a trailer ?");
return 0;
}
if (confpath) { if (confpath) {
conf = fopen(confpath, "r"); conf = fopen(confpath, "r");
if (!conf) if (!conf)
fatal("config file not found"); fatal("config file not found");
} }
if (!conf) { if (!conf) {
char *home;
home = getenv("HOME");
if (!home)
fatal("no home");
confpath = malloc(strlen(home) + 1 + strlen(S_CONF) + 1); confpath = malloc(strlen(home) + 1 + strlen(S_CONF) + 1);
*confpath = 0; *confpath = 0;
strcat(confpath, home); strcat(confpath, home);
@ -1151,18 +1158,20 @@ int main(int argc, char **argv)
fatal("%s config file not found", confpath); fatal("%s config file not found", confpath);
} }
#ifdef HAVE_OIDENTD
bip.oidentdpath = malloc(strlen(home) + 1 +
strlen(OIDENTD_FILENAME) + 1);
strcpy(bip.oidentdpath, home);
strcat(bip.oidentdpath, "/");
strcat(bip.oidentdpath, OIDENTD_FILENAME);
#endif
r = fireup(&bip, conf); r = fireup(&bip, conf);
fclose(conf); fclose(conf);
if (!r) if (!r)
fatal("Not starting: error in config file."); fatal("Not starting: error in config file.");
if (!conf_biphome) { if (!conf_biphome) {
char *home = getenv("HOME");
if (!home) {
conf_die(&bip,
"no $HOME !, do you live in a trailer ?");
return 0;
}
conf_biphome = malloc(strlen(home) + strlen("/.bip") + 1); conf_biphome = malloc(strlen(home) + strlen("/.bip") + 1);
strcpy(conf_biphome, home); strcpy(conf_biphome, home);
strcat(conf_biphome, "/.bip"); strcat(conf_biphome, "/.bip");

View File

@ -96,6 +96,10 @@ void connection_free(connection_t *cn)
} }
} }
#endif #endif
if (cn->localip)
free(cn->localip);
if (cn->remoteip)
free(cn->remoteip);
free(cn); free(cn);
} }
@ -581,8 +585,8 @@ static int check_event_read(fd_set *fds, connection_t *cn)
if (ret) { if (ret) {
mylog(LOG_ERROR, "Error while reading on fd %d", mylog(LOG_ERROR, "Error while reading on fd %d",
cn->handle); cn->handle);
return 1; return 1;
} }
if (!cn->incoming_lines) if (!cn->incoming_lines)
cn->incoming_lines = list_new(NULL); cn->incoming_lines = list_new(NULL);
@ -595,6 +599,14 @@ static int check_event_read(fd_set *fds, connection_t *cn)
return 1; return 1;
} }
static void connection_connected(connection_t *c)
{
c->localip = connection_localip(c);
c->localport = connection_localport(c);
c->remoteip = connection_remoteip(c);
c->remoteport = connection_remoteport(c);
}
static int check_event_write(fd_set *fds, connection_t *cn, int *nc) static int check_event_write(fd_set *fds, connection_t *cn, int *nc)
{ {
if (cn_is_in_error(cn)) { if (cn_is_in_error(cn)) {
@ -646,6 +658,7 @@ static int check_event_write(fd_set *fds, connection_t *cn, int *nc)
} }
#endif #endif
cn->connected = CONN_OK; cn->connected = CONN_OK;
connection_connected(cn);
*nc = 1; *nc = 1;
mylog(LOG_DEBUG, "fd:%d Connection established !", mylog(LOG_DEBUG, "fd:%d Connection established !",
cn->handle); cn->handle);
@ -1254,6 +1267,7 @@ static int SSLize(connection_t *cn, int *nc)
mylog(LOG_DEBUG, "Negociated cyphers: %s", buf); mylog(LOG_DEBUG, "Negociated cyphers: %s", buf);
cn->connected = CONN_OK; cn->connected = CONN_OK;
connection_connected(cn);
*nc = 1; *nc = 1;
return 0; return 0;
} }

View File

@ -29,7 +29,6 @@
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
#ifdef HAVE_LIBSSL #ifdef HAVE_LIBSSL
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <openssl/x509.h> #include <openssl/x509.h>
@ -87,6 +86,8 @@ typedef struct connection {
int ssl_check_mode; int ssl_check_mode;
X509 *cert; X509 *cert;
#endif #endif
char *localip, *remoteip;
uint16_t localport, remoteport;
} connection_t; } connection_t;
connection_t *connection_new(char *dsthostname, int dstport, char *srchostname, connection_t *connection_new(char *dsthostname, int dstport, char *srchostname,

View File

@ -1,3 +1,4 @@
/* /*
* $Id: irc.c,v 1.156 2005/04/21 06:58:50 nohar Exp $ * $Id: irc.c,v 1.156 2005/04/21 06:58:50 nohar Exp $
* *
@ -46,7 +47,11 @@ static int irc_367(struct link_server *server, struct line *l);
static int irc_368(struct link_server *server, struct line *l); static int irc_368(struct link_server *server, struct line *l);
void irc_server_shutdown(struct link_server *s); void irc_server_shutdown(struct link_server *s);
static int origin_is_me(struct line *l, struct link_server *server); static int origin_is_me(struct line *l, struct link_server *server);
void oidentd_dump(list_t *connl);
#ifdef HAVE_OIDENTD
#define OIDENTD_FILENAME ".oidentd.conf"
void oidentd_dump(bip_t *bip);
#endif
void irc_client_free(struct link_client *cli); void irc_client_free(struct link_client *cli);
extern int conf_log_sync_interval; extern int conf_log_sync_interval;
@ -2071,7 +2076,7 @@ connection_t *irc_server_connect(struct link *link)
list_add_last(&_bip->conn_list, conn); list_add_last(&_bip->conn_list, conn);
#ifdef HAVE_OIDENTD #ifdef HAVE_OIDENTD
oidentd_dump(&_bip->conn_list); oidentd_dump(_bip);
#endif #endif
irc_server_startup(ls); irc_server_startup(ls);
return conn; return conn;
@ -2112,43 +2117,30 @@ void irc_server_shutdown(struct link_server *s)
#define BIP_OIDENTD_END "## END OF AUTOGENERATED STUFF ##\n" #define BIP_OIDENTD_END "## END OF AUTOGENERATED STUFF ##\n"
#define BIP_OIDENTD_END_LENGTH strlen(BIP_OIDENTD_END) #define BIP_OIDENTD_END_LENGTH strlen(BIP_OIDENTD_END)
void oidentd_dump(list_t *connl) void oidentd_dump(bip_t *bip)
{ {
list_iterator_t it; list_iterator_t it;
FILE *f; FILE *f;
char *home, *filename;
char *bipstart = NULL, *bipend = NULL; char *bipstart = NULL, *bipend = NULL;
struct stat stats; struct stat stats;
char tag_written = 0; char tag_written = 0;
home = getenv("HOME"); if (stat(bip->oidentdpath, &stats) == -1) {
if (home == NULL) {
mylog(LOG_WARN, "Can't get $HOME, not writing oidentd.conf");
return;
}
filename = (char *)malloc(strlen(home) + strlen("/.oidentd.conf") + 1);
if(filename == NULL)
fatal("Out of memory.");
sprintf(filename, "%s/.oidentd.conf", home);
if (stat(filename, &stats) == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
f = fopen(filename, "w+"); f = fopen(bip->oidentdpath, "w+");
fchmod(fileno(f), 0644); fchmod(fileno(f), 0644);
} else { } else {
mylog(LOG_WARN, "Can't open/create %s", filename); mylog(LOG_WARN, "Can't open/create %s",
free(filename); bip->oidentdpath);
return; return;
} }
} else { } else {
char *content; char *content;
f = fopen(filename, "r+"); f = fopen(bip->oidentdpath, "r+");
if (!f) { if (!f) {
mylog(LOG_WARN, "Can't open/create %s", filename); mylog(LOG_WARN, "Can't open/create %s",
free(filename); bip->oidentdpath);
return; return;
} }
@ -2161,7 +2153,8 @@ void oidentd_dump(list_t *connl)
if (fread(content, 1, stats.st_size, f) != if (fread(content, 1, stats.st_size, f) !=
(size_t)stats.st_size) { (size_t)stats.st_size) {
mylog(LOG_WARN, "Can't read %s fully", filename); mylog(LOG_WARN, "Can't read %s fully",
bip->oidentdpath);
free(content); free(content);
goto clean_oidentd; goto clean_oidentd;
} }
@ -2176,7 +2169,7 @@ void oidentd_dump(list_t *connl)
fseek(f, SEEK_SET, 0); fseek(f, SEEK_SET, 0);
if (ftruncate(fileno(f), 0) == -1) { if (ftruncate(fileno(f), 0) == -1) {
mylog(LOG_DEBUG, "Can't reset %s size", mylog(LOG_DEBUG, "Can't reset %s size",
filename); bip->oidentdpath);
free(content); free(content);
goto clean_oidentd; goto clean_oidentd;
} }
@ -2194,7 +2187,8 @@ void oidentd_dump(list_t *connl)
BIP_OIDENTD_END_LENGTH, f); BIP_OIDENTD_END_LENGTH, f);
else else
mylog(LOG_WARN, "No %s mark found in %s", mylog(LOG_WARN, "No %s mark found in %s",
BIP_OIDENTD_END, filename); BIP_OIDENTD_END,
bip->oidentdpath);
} else { } else {
/* No previous conf */ /* No previous conf */
if (stats.st_size != 0 && if (stats.st_size != 0 &&
@ -2204,19 +2198,16 @@ void oidentd_dump(list_t *connl)
free(content); free(content);
} }
for (list_it_init(connl, &it); list_it_item(&it); list_it_next(&it)) { for (list_it_init(&bip->conn_list, &it); list_it_item(&it);
list_it_next(&it)) {
connection_t *c = list_it_item(&it); connection_t *c = list_it_item(&it);
struct link_any *la = c->user_data; struct link_any *la = c->user_data;
if (la && TYPE(la) == IRC_TYPE_SERVER && ( if (la && TYPE(la) == IRC_TYPE_SERVER && (
c->connected == CONN_OK || c->connected == CONN_OK ||
c->connected == CONN_NEED_SSLIZE || c->connected == CONN_NEED_SSLIZE ||
c->connected == CONN_INPROGRESS ||
c->connected == CONN_NEW ||
c->connected == CONN_UNTRUSTED)) { c->connected == CONN_UNTRUSTED)) {
struct link_server *ls; struct link_server *ls;
struct link *l; struct link *l;
char *localip, *remoteip;
int localport, remoteport;
if (!tag_written) { if (!tag_written) {
fprintf(f, BIP_OIDENTD_START); fprintf(f, BIP_OIDENTD_START);
@ -2226,18 +2217,11 @@ void oidentd_dump(list_t *connl)
ls = (struct link_server*)la; ls = (struct link_server*)la;
l = LINK(ls); l = LINK(ls);
localip = connection_localip(CONN(ls));
localport = connection_localport(CONN(ls));
remoteip = connection_remoteip(CONN(ls));
remoteport = connection_remoteport(CONN(ls));
fprintf(f, "to %s fport %d from %s lport %d {\n", fprintf(f, "to %s fport %d from %s lport %d {\n",
remoteip, remoteport, localip, c->remoteip, c->remoteport, c->localip,
localport); c->localport);
fprintf(f, "\treply \"%s\"\n", l->username); fprintf(f, "\treply \"%s\"\n", l->username);
fprintf(f, "}\n"); fprintf(f, "}\n");
free(localip);
free(remoteip);
} }
} }
if (tag_written) if (tag_written)
@ -2245,7 +2229,6 @@ void oidentd_dump(list_t *connl)
clean_oidentd: clean_oidentd:
fclose(f); fclose(f);
free(filename);
} }
#endif #endif
@ -2455,7 +2438,7 @@ void irc_main(bip_t *bip)
list_t *ready = wait_event(&bip->conn_list, &timeleft, &nc); list_t *ready = wait_event(&bip->conn_list, &timeleft, &nc);
#ifdef HAVE_OIDENTD #ifdef HAVE_OIDENTD
if (nc) if (nc)
oidentd_dump(&bip->conn_list); oidentd_dump(bip);
#endif #endif
while ((conn = list_remove_first(ready))) while ((conn = list_remove_first(ready)))
bip_on_event(bip, conn); bip_on_event(bip, conn);

View File

@ -246,6 +246,10 @@ typedef struct bip {
hash_t users; hash_t users;
list_t errors; list_t errors;
struct link_client *reloading_client; struct link_client *reloading_client;
#ifdef HAVE_OIDENTD
char *oidentdpath;
#endif
} bip_t; } bip_t;
void bip_init(bip_t *bip); void bip_init(bip_t *bip);