From 27b16a86b8fed92e2e5bba4ed44a921a696ac532 Mon Sep 17 00:00:00 2001 From: Arnaud Cornet Date: Fri, 21 Dec 2007 12:20:21 +0100 Subject: [PATCH] Avoid unncessary calls to getaddrinfo. Refactor calls to getaddrinfo only on socket establishement, cache in connection structure. --- src/bip.c | 29 +++++++++++++-------- src/connection.c | 20 ++++++++++++--- src/connection.h | 3 ++- src/irc.c | 67 ++++++++++++++++++------------------------------ src/irc.h | 4 +++ 5 files changed, 67 insertions(+), 56 deletions(-) diff --git a/src/bip.c b/src/bip.c index 1f54e35..69b9042 100644 --- a/src/bip.c +++ b/src/bip.c @@ -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 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) { unsigned char *md5; @@ -1083,6 +1087,7 @@ int main(int argc, char **argv) int r, fd; char buf[30]; bip_t bip; + char *home; bip_init(&bip); _bip = &bip; @@ -1131,16 +1136,18 @@ int main(int argc, char **argv) check_rlimits(); + home = getenv("HOME"); + if (!home) { + conf_die(&bip, "no $HOME !, do you live in a trailer ?"); + return 0; + } + if (confpath) { conf = fopen(confpath, "r"); if (!conf) fatal("config file not found"); } if (!conf) { - char *home; - home = getenv("HOME"); - if (!home) - fatal("no home"); confpath = malloc(strlen(home) + 1 + strlen(S_CONF) + 1); *confpath = 0; strcat(confpath, home); @@ -1151,18 +1158,20 @@ int main(int argc, char **argv) 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); fclose(conf); if (!r) fatal("Not starting: error in config file."); 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); strcpy(conf_biphome, home); strcat(conf_biphome, "/.bip"); diff --git a/src/connection.c b/src/connection.c index 5b869ff..64e6d4d 100644 --- a/src/connection.c +++ b/src/connection.c @@ -96,6 +96,10 @@ void connection_free(connection_t *cn) } } #endif + if (cn->localip) + free(cn->localip); + if (cn->remoteip) + free(cn->remoteip); free(cn); } @@ -581,8 +585,8 @@ static int check_event_read(fd_set *fds, connection_t *cn) if (ret) { mylog(LOG_ERROR, "Error while reading on fd %d", cn->handle); - return 1; - } + return 1; + } if (!cn->incoming_lines) cn->incoming_lines = list_new(NULL); @@ -595,6 +599,14 @@ static int check_event_read(fd_set *fds, connection_t *cn) 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) { if (cn_is_in_error(cn)) { @@ -646,6 +658,7 @@ static int check_event_write(fd_set *fds, connection_t *cn, int *nc) } #endif cn->connected = CONN_OK; + connection_connected(cn); *nc = 1; mylog(LOG_DEBUG, "fd:%d Connection established !", cn->handle); @@ -1240,7 +1253,7 @@ static int SSLize(connection_t *cn, int *nc) err2 = SSL_get_error(cn->ssl_h, err); ERR_print_errors(errbio); - + if (err2 == SSL_ERROR_NONE) { SSL_CIPHER *cipher; char buf[128]; @@ -1254,6 +1267,7 @@ static int SSLize(connection_t *cn, int *nc) mylog(LOG_DEBUG, "Negociated cyphers: %s", buf); cn->connected = CONN_OK; + connection_connected(cn); *nc = 1; return 0; } diff --git a/src/connection.h b/src/connection.h index a39b262..77b377a 100644 --- a/src/connection.h +++ b/src/connection.h @@ -29,7 +29,6 @@ #include #include - #ifdef HAVE_LIBSSL #include #include @@ -87,6 +86,8 @@ typedef struct connection { int ssl_check_mode; X509 *cert; #endif + char *localip, *remoteip; + uint16_t localport, remoteport; } connection_t; connection_t *connection_new(char *dsthostname, int dstport, char *srchostname, diff --git a/src/irc.c b/src/irc.c index 7a474fe..3992928 100644 --- a/src/irc.c +++ b/src/irc.c @@ -1,3 +1,4 @@ + /* * $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); void irc_server_shutdown(struct link_server *s); 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); 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); #ifdef HAVE_OIDENTD - oidentd_dump(&_bip->conn_list); + oidentd_dump(_bip); #endif irc_server_startup(ls); 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_LENGTH strlen(BIP_OIDENTD_END) -void oidentd_dump(list_t *connl) +void oidentd_dump(bip_t *bip) { list_iterator_t it; FILE *f; - char *home, *filename; char *bipstart = NULL, *bipend = NULL; struct stat stats; char tag_written = 0; - home = getenv("HOME"); - 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 (stat(bip->oidentdpath, &stats) == -1) { if (errno == ENOENT) { - f = fopen(filename, "w+"); + f = fopen(bip->oidentdpath, "w+"); fchmod(fileno(f), 0644); } else { - mylog(LOG_WARN, "Can't open/create %s", filename); - free(filename); + mylog(LOG_WARN, "Can't open/create %s", + bip->oidentdpath); return; } } else { char *content; - f = fopen(filename, "r+"); + f = fopen(bip->oidentdpath, "r+"); if (!f) { - mylog(LOG_WARN, "Can't open/create %s", filename); - free(filename); + mylog(LOG_WARN, "Can't open/create %s", + bip->oidentdpath); return; } @@ -2161,7 +2153,8 @@ void oidentd_dump(list_t *connl) if (fread(content, 1, stats.st_size, f) != (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); goto clean_oidentd; } @@ -2176,7 +2169,7 @@ void oidentd_dump(list_t *connl) fseek(f, SEEK_SET, 0); if (ftruncate(fileno(f), 0) == -1) { mylog(LOG_DEBUG, "Can't reset %s size", - filename); + bip->oidentdpath); free(content); goto clean_oidentd; } @@ -2194,7 +2187,8 @@ void oidentd_dump(list_t *connl) BIP_OIDENTD_END_LENGTH, f); else mylog(LOG_WARN, "No %s mark found in %s", - BIP_OIDENTD_END, filename); + BIP_OIDENTD_END, + bip->oidentdpath); } else { /* No previous conf */ if (stats.st_size != 0 && @@ -2204,19 +2198,16 @@ void oidentd_dump(list_t *connl) 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); struct link_any *la = c->user_data; if (la && TYPE(la) == IRC_TYPE_SERVER && ( c->connected == CONN_OK || c->connected == CONN_NEED_SSLIZE || - c->connected == CONN_INPROGRESS || - c->connected == CONN_NEW || c->connected == CONN_UNTRUSTED)) { struct link_server *ls; struct link *l; - char *localip, *remoteip; - int localport, remoteport; if (!tag_written) { fprintf(f, BIP_OIDENTD_START); @@ -2226,18 +2217,11 @@ void oidentd_dump(list_t *connl) ls = (struct link_server*)la; 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", - remoteip, remoteport, localip, - localport); + c->remoteip, c->remoteport, c->localip, + c->localport); fprintf(f, "\treply \"%s\"\n", l->username); fprintf(f, "}\n"); - free(localip); - free(remoteip); } } if (tag_written) @@ -2245,7 +2229,6 @@ void oidentd_dump(list_t *connl) clean_oidentd: fclose(f); - free(filename); } #endif @@ -2455,7 +2438,7 @@ void irc_main(bip_t *bip) list_t *ready = wait_event(&bip->conn_list, &timeleft, &nc); #ifdef HAVE_OIDENTD if (nc) - oidentd_dump(&bip->conn_list); + oidentd_dump(bip); #endif while ((conn = list_remove_first(ready))) bip_on_event(bip, conn); diff --git a/src/irc.h b/src/irc.h index f034d2d..7135d24 100644 --- a/src/irc.h +++ b/src/irc.h @@ -246,6 +246,10 @@ typedef struct bip { hash_t users; list_t errors; struct link_client *reloading_client; + +#ifdef HAVE_OIDENTD + char *oidentdpath; +#endif } bip_t; void bip_init(bip_t *bip);