Lot of cleanups wrt init and killing links

Cleanup sample config (make cert).
Implement some free functions (log_free and link_kill).
Move utility macros in src/util.h
Reorganiize initialisation functions.
This commit is contained in:
Arnaud Cornet 2007-09-04 00:24:50 +02:00
parent 995b3f071a
commit d7e870fe67
7 changed files with 166 additions and 88 deletions

View File

@ -13,7 +13,7 @@ port = 7778;
# If you set this to true, you'll only be able to connect to bip
# with a SSL capable IRC client. Be sure to generate a certificate
# for bip with 'make cert'
# for bip using scripts/bipgenconfig.
client_side_ssl = false;
log_level = 3;

173
src/bip.c
View File

@ -25,22 +25,6 @@
#include "bip.h"
#include "line.h"
#define MOVE_STRING(dest, src) do {\
if (dest)\
free(dest);\
(dest) = (src);\
(src) = NULL;\
} while(0)
#define FREE(a) free(a); (a) = NULL;
#define MAYFREE(a) do { \
if (a) { \
free(a); \
(a) = NULL; \
} \
} while(0);
int sighup = 0;
char *conf_log_root;
@ -154,7 +138,6 @@ FILE *conf_global_log_file;
static pid_t daemonize(void)
{
char buf[4096];
switch (fork()) {
case -1:
fatal("Fork failed");
@ -550,6 +533,47 @@ static int add_user(bip_t *bip, list_t *data)
return 1;
}
static int validate_config(bip_t *bip)
{
/* nick username realname or default_{nick,username,realname} in user */
hash_iterator_t it, sit;
struct user *user;
struct link *link;
int r = 1;
for (hash_it_init(&bip->users, &it); (user = hash_it_item(&it));
hash_it_next(&it)) {
if (!user->default_nick || !user->default_username ||
!user->default_realname) {
for (hash_it_init(&user->connections, &sit);
(link = hash_it_item(&sit));
hash_it_next(&sit)) {
if ((!link->username &&
!user->default_username) ||
(!link->connect_nick &&
!user->default_nick) ||
(!link->realname &&
!user->default_realname))
link_kill(bip, link);
//conf_die("user: ... net: ... can i has nick/user/rael");
r = 0;
}
}
}
#warning CODE ME
#if 0
if (conf_backlog && !conf_log) {
if (conf_backlog_lines == 0) {
conf_die("You must set conf_backlog_lines if "
"conf_log = false and "
"conf_backlog = true");
}
}
#endif
return r;
}
int fireup(bip_t *bip, FILE *conf)
{
struct tuple *t;
@ -631,71 +655,7 @@ int fireup(bip_t *bip, FILE *conf)
free(root_list);
root_list = NULL;
#warning move me
#if 0
if (conf_backlog && !conf_log) {
if (conf_backlog_lines == 0) {
conf_die("You must set conf_backlog_lines if "
"conf_log = false and "
"conf_backlog = true");
}
}
#endif
if (!conf_log)
conf_memlog = 1;
else
conf_memlog = 0;
/*
check_networks(networkl);
check_clients(userl);
*/
if (!conf_biphome) {
char *home = getenv("HOME");
if (!home) {
conf_die("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");
}
if (!conf_log_root) {
char *ap = "/logs";
conf_log_root = malloc(strlen(conf_biphome) + strlen(ap) + 1);
strcpy(conf_log_root, conf_biphome);
strcat(conf_log_root, ap);
mylog(LOG_INFO, "Default log root: %s", conf_log_root);
}
if (!conf_pid_file) {
char *pid = "/bip.pid";
conf_pid_file = malloc(strlen(conf_biphome) + strlen(pid) + 1);
strcpy(conf_pid_file, conf_biphome);
strcat(conf_pid_file, pid);
mylog(LOG_INFO, "Default pid file: %s", conf_pid_file);
}
#ifdef HAVE_LIBSSL
conf_ssl_certfile = NULL; /* Make into a config option */
if (!conf_ssl_certfile) {
char *ap = "/bip.pem";
if (conf_ssl_certfile) {
free(conf_ssl_certfile);
conf_ssl_certfile = NULL;
}
conf_ssl_certfile = malloc(strlen(conf_biphome) +
strlen(ap) + 1);
strcpy(conf_ssl_certfile, conf_biphome);
strcat(conf_ssl_certfile, ap);
mylog(LOG_INFO, "Default SSL certificate file: %s",
conf_ssl_certfile);
}
#endif
if (!conf_log_format)
conf_log_format = "%u/%n/%Y-%m/%c.%d.log";
validate_config(bip);
return 1;
}
@ -853,9 +813,11 @@ void ircize(bip_t *bip)
static void log_file_setup(void)
{
char buf[4096];
if (conf_log_system) {
if (conf_global_log_file && conf_global_log_file != stderr)
fclose(conf_log_system);
fclose(conf_global_log_file);
snprintf(buf, 4095, "%s/bip.log", conf_log_root);
FILE *f = fopen(buf, "a");
if (!f)
@ -937,6 +899,7 @@ int main(int argc, char **argv)
fatal("%s config file not found", confpath);
}
r = fireup(&bip, conf);
fclose(conf);
if (!r) {
@ -944,6 +907,50 @@ int main(int argc, char **argv)
exit(28);
}
if (!conf_biphome) {
char *home = getenv("HOME");
if (!home) {
conf_die("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");
}
if (!conf_log_root) {
char *ap = "/logs";
conf_log_root = malloc(strlen(conf_biphome) + strlen(ap) + 1);
strcpy(conf_log_root, conf_biphome);
strcat(conf_log_root, ap);
mylog(LOG_INFO, "Default log root: %s", conf_log_root);
}
if (!conf_pid_file) {
char *pid = "/bip.pid";
conf_pid_file = malloc(strlen(conf_biphome) + strlen(pid) + 1);
strcpy(conf_pid_file, conf_biphome);
strcat(conf_pid_file, pid);
mylog(LOG_INFO, "Default pid file: %s", conf_pid_file);
}
#ifdef HAVE_LIBSSL
conf_ssl_certfile = NULL; /* Make into a config option */
if (!conf_ssl_certfile) {
char *ap = "/bip.pem";
if (conf_ssl_certfile) {
free(conf_ssl_certfile);
conf_ssl_certfile = NULL;
}
conf_ssl_certfile = malloc(strlen(conf_biphome) +
strlen(ap) + 1);
strcpy(conf_ssl_certfile, conf_biphome);
strcat(conf_ssl_certfile, ap);
mylog(LOG_INFO, "Default SSL certificate file: %s",
conf_ssl_certfile);
}
#endif
if (!conf_log_format)
conf_log_format = "%u/%n/%Y-%m/%c.%d.log";
check_dir(conf_log_root, 1);
fd = do_pid_stuff();
pid_t pid = 0;

View File

@ -882,7 +882,7 @@ static void create_socket(char *dsthostname, char *dstport, char *srchostname,
connecting_data_free(cdata);
cdata = NULL;
return;
}
}
if (srchostname || srcport) {
if ((err = getaddrinfo(srchostname, srcport, &hint,
@ -925,7 +925,7 @@ static void create_listening_socket(char *hostname, char *port,
if (err) {
mylog(LOG_DEBUG, "getaddrinfo(): %s", gai_strerror(err));
return;
}
}
for (cur = res ; cur ; cur = cur->ai_next) {
if ((cn->handle = socket(cur->ai_family, cur->ai_socktype,

View File

@ -530,7 +530,6 @@ static void irc_send_join(struct link_client *ic, struct channel *chan)
WRITE_LINE3(CONN(ic), P_SERV, "366", LINK(ic)->l_server->nick,
chan->name, "End of /NAMES list.");
}
static void write_init_string(connection_t *c, struct line *line, char *nick)
@ -1921,7 +1920,6 @@ static void irc_close(struct link_any *l)
irc_notify_disconnection(is);
else
LINK(is)->s_conn_attempt++;
printf("%d\n", LINK(is)->s_conn_attempt);
irc_server_shutdown(is);
log_disconnected(LINK(is)->log);
@ -2418,3 +2416,40 @@ struct link *irc_link_new()
return link;
}
void link_kill(bip_t *bip, struct link *link)
{
int i;
hash_remove(&link->user->connections, link->name);
free(link->name);
irc_close((struct link_any *)link->l_server);
while (link->l_clientc)
irc_close((struct link_any *)link->l_clientv[0]);
log_free(link->log);
MAYFREE(link->prev_nick);
MAYFREE(link->cli_nick);
void *p;
while ((p = list_remove_first(&link->init_strings)))
free(p);
while ((p = list_remove_first(&link->on_connect_send)))
free(p);
MAYFREE(link->no_client_away_msg);
MAYFREE(link->away_nick);
hash_clean(&link->chan_infos);
struct chan_infos *ci;
while ((ci = list_remove_first(&link->chan_infos_order)))
free(ci);
MAYFREE(link->username);
MAYFREE(link->realname);
MAYFREE(link->s_password);
MAYFREE(link->connect_nick);
MAYFREE(link->vhost);
#ifdef HAVE_LIBSSL
MAYFREE(link->ssl_check_store);
sk_X509_free(link->untrusted_certs);
#endif
}

View File

@ -248,6 +248,7 @@ int ischannel(char p);
void irc_client_close(struct link_client *);
void irc_client_free(struct link_client *);
struct link *irc_link_new();
void link_kill(bip_t *bip, struct link *);
void unbind_from_link(struct link_client *ic);
#endif

View File

@ -1148,3 +1148,21 @@ log_t *log_new(struct user *user, char *network)
list_add_last(log_all_logs, logdata);
return logdata;
}
void log_free(log_t *log)
{
hash_iterator_t it;
logfilegroup_t *lfg;
logfile_t *lf;
for (hash_it_init(&log->logfgs, &it); (lfg = hash_it_item(&it));
hash_it_next(&it)) {
log_reset(lfg);
if ((lf = list_remove_first(&lfg->file_group)))
logfile_free(lf);
free(lf);
}
hash_clean(&log->logfgs);
free(log);
}

View File

@ -56,6 +56,22 @@ typedef struct hash_iterator {
hash_t *hash;
} hash_iterator_t;
#define MOVE_STRING(dest, src) do {\
if (dest)\
free(dest);\
(dest) = (src);\
(src) = NULL;\
} while(0)
#define FREE(a) free(a); (a) = NULL;
#define MAYFREE(a) do { \
if (a) { \
free(a); \
(a) = NULL; \
} \
} while(0);
void list_init(list_t *list, int (*cmp)(void*,void*));
int list_ptr_cmp(void *a, void *b);
list_t *list_new(int (*cmp)(void *, void *));
@ -97,4 +113,5 @@ char *strmaydup(char *s);
void strucase(char *s);
int ischannel(char p);
#endif