From: Linus Nordberg Date: Wed, 9 Mar 2011 09:14:20 +0000 (+0100) Subject: Rename and move around a few helper functions. X-Git-Tag: libradsec-0.0.1~24^2~17 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=radsecproxy.git;a=commitdiff_plain;h=fac0219dad91c574417f78ec674aa0dd10949e15 Rename and move around a few helper functions. --- diff --git a/lib/conf.c b/lib/conf.c index 1d394ba..7e27f0b 100644 --- a/lib/conf.c +++ b/lib/conf.c @@ -9,6 +9,7 @@ #include #include #include +#include "peer.h" #include "debug.h" #if 0 @@ -112,15 +113,15 @@ rs_context_read_config(struct rs_context *ctx, const char *config_file) /* Add peers, one per server stanza. */ for (j = 0; j < cfg_size (cfg_config, "server"); j++) { - struct rs_peer *p = _rs_peer_create (ctx, &r->peers); + struct rs_peer *p = peer_create (ctx, &r->peers); if (!p) return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL); p->realm = r; cfg_server = cfg_getnsec (cfg_config, "server", j); - _rs_resolv (&p->addr, r->type, cfg_getstr (cfg_server, "hostname"), - cfg_getstr (cfg_server, "service")); + rs_resolv (&p->addr, r->type, cfg_getstr (cfg_server, "hostname"), + cfg_getstr (cfg_server, "service")); p->secret = cfg_getstr (cfg_server, "secret"); } } diff --git a/lib/conn.c b/lib/conn.c index 0786de2..85cd7d5 100644 --- a/lib/conn.c +++ b/lib/conn.c @@ -7,11 +7,11 @@ #include #include -#include #include #include #include #include +#include "debug.h" #include "conn.h" #include "event.h" #include "packet.h" @@ -90,45 +90,6 @@ rs_conn_set_type (struct rs_connection *conn, rs_conn_type_t type) conn->realm->type = type; } - -struct rs_error * /* FIXME: Return int as all the others? */ -_rs_resolv (struct evutil_addrinfo **addr, rs_conn_type_t type, - const char *hostname, const char *service) -{ - int err; - struct evutil_addrinfo hints, *res = NULL; - - memset (&hints, 0, sizeof(struct evutil_addrinfo)); - hints.ai_family = AF_INET; /* IPv4 only. TODO: Set AF_UNSPEC. */ - hints.ai_flags = AI_ADDRCONFIG; - switch (type) - { - case RS_CONN_TYPE_NONE: - return _rs_err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL); - case RS_CONN_TYPE_TCP: - /* Fall through. */ - case RS_CONN_TYPE_TLS: - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = IPPROTO_TCP; - break; - case RS_CONN_TYPE_UDP: - /* Fall through. */ - case RS_CONN_TYPE_DTLS: - hints.ai_socktype = SOCK_DGRAM; - hints.ai_protocol = IPPROTO_UDP; - break; - default: - return _rs_err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL); - } - err = evutil_getaddrinfo (hostname, service, &hints, &res); - if (err) - return _rs_err_create (RSE_BADADDR, __FILE__, __LINE__, - "%s:%s: bad host name or service name (%s)", - hostname, service, evutil_gai_strerror(err)); - *addr = res; /* Simply use first result. */ - return NULL; -} - int rs_conn_add_listener (struct rs_connection *conn, rs_conn_type_t type, const char *hostname, int port) diff --git a/lib/err.c b/lib/err.c index 1f2cbc0..eecebb1 100644 --- a/lib/err.c +++ b/lib/err.c @@ -39,7 +39,7 @@ static struct rs_error * _err_vcreate (unsigned int code, const char *file, int line, const char *fmt, va_list args) { - struct rs_error *err; + struct rs_error *err = NULL; err = malloc (sizeof(struct rs_error)); if (err) @@ -69,15 +69,19 @@ _err_vcreate (unsigned int code, const char *file, int line, const char *fmt, } struct rs_error * -_rs_err_create (unsigned int code, const char *file, int line, const char *fmt, - ...) +err_create (unsigned int code, + const char *file, + int line, + const char *fmt, + ...) { - struct rs_error *err; + struct rs_error *err = NULL; va_list args; va_start (args, fmt); err = _err_vcreate (code, file, line, fmt, args); va_end (args); + return err; } @@ -87,36 +91,52 @@ _ctx_err_vpush_fl (struct rs_context *ctx, int code, const char *file, { struct rs_error *err = _err_vcreate (code, file, line, fmt, args); - if (err) - ctx->err = err; - return code; + if (!err) + return RSE_NOMEM; + + /* TODO: Implement a stack. */ + if (ctx->err) + rs_err_free (ctx->err); + ctx->err = err; + + return err->code; } int rs_err_ctx_push (struct rs_context *ctx, int code, const char *fmt, ...) { + int r = 0; va_list args; + va_start (args, fmt); - _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args); + r = _ctx_err_vpush_fl (ctx, code, NULL, 0, fmt, args); va_end (args); - return code; + + return r; } int rs_err_ctx_push_fl (struct rs_context *ctx, int code, const char *file, int line, const char *fmt, ...) { + int r = 0; va_list args; + va_start (args, fmt); - _ctx_err_vpush_fl (ctx, code, file, line, fmt, args); + r = _ctx_err_vpush_fl (ctx, code, file, line, fmt, args); va_end (args); - return code; + + return r; } int -_rs_err_conn_push_err (struct rs_connection *conn, struct rs_error *err) +err_conn_push_err (struct rs_connection *conn, struct rs_error *err) { + + if (conn->err) + rs_err_free (conn->err); conn->err = err; /* FIXME: use a stack */ + return err->code; } @@ -126,30 +146,37 @@ _conn_err_vpush_fl (struct rs_connection *conn, int code, const char *file, { struct rs_error *err = _err_vcreate (code, file, line, fmt, args); - if (err) - _rs_err_conn_push_err (conn, err); - return code; + if (!err) + return RSE_NOMEM; + + return err_conn_push_err (conn, err); } int rs_err_conn_push (struct rs_connection *conn, int code, const char *fmt, ...) { + int r = 0; + va_list args; va_start (args, fmt); - _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args); + r = _conn_err_vpush_fl (conn, code, NULL, 0, fmt, args); va_end (args); - return code; + + return r; } int rs_err_conn_push_fl (struct rs_connection *conn, int code, const char *file, int line, const char *fmt, ...) { + int r = 0; + va_list args; va_start (args, fmt); - _conn_err_vpush_fl (conn, code, file, line, fmt, args); + r = _conn_err_vpush_fl (conn, code, file, line, fmt, args); va_end (args); - return code; + + return r; } struct rs_error * @@ -161,6 +188,7 @@ rs_err_ctx_pop (struct rs_context *ctx) return NULL; /* FIXME: RSE_INVALID_CTX. */ err = ctx->err; ctx->err = NULL; + return err; } @@ -173,6 +201,7 @@ rs_err_conn_pop (struct rs_connection *conn) return NULL; /* FIXME: RSE_INVALID_CONN */ err = conn->err; conn->err = NULL; + return err; } @@ -183,8 +212,8 @@ rs_err_conn_peek_code (struct rs_connection *conn) return -1; /* FIXME: RSE_INVALID_CONN */ if (conn->err) return conn->err->code; - else - return RSE_OK; + + return RSE_OK; } void @@ -199,6 +228,7 @@ rs_err_msg (struct rs_error *err) { if (!err) return NULL; + return err->buf; } @@ -212,6 +242,7 @@ rs_err_code (struct rs_error *err, int dofree_flag) code = err->code; if (dofree_flag) - rs_err_free(err); + rs_err_free (err); + return code; } diff --git a/lib/include/radsec/radsec-impl.h b/lib/include/radsec/radsec-impl.h index 49f9a35..9bcd208 100644 --- a/lib/include/radsec/radsec-impl.h +++ b/lib/include/radsec/radsec-impl.h @@ -113,18 +113,11 @@ struct rs_attr { VALUE_PAIR *vp; }; -/* Nonpublic functions. */ -struct rs_error *_rs_resolv(struct evutil_addrinfo **addr, - rs_conn_type_t type, const char *hostname, +/* Nonpublic functions (in radsec.c -- FIXME: move?). */ +struct rs_error *rs_resolv (struct evutil_addrinfo **addr, + rs_conn_type_t type, + const char *hostname, const char *service); -struct rs_peer *_rs_peer_create(struct rs_context *ctx, - struct rs_peer **rootp); -struct rs_error *_rs_err_create(unsigned int code, const char *file, - int line, const char *fmt, ...); -int _rs_err_conn_push_err(struct rs_connection *conn, - struct rs_error *err); - - #if defined (__cplusplus) } #endif diff --git a/lib/peer.c b/lib/peer.c index 0a1d2ec..bcd5c97 100644 --- a/lib/peer.c +++ b/lib/peer.c @@ -8,6 +8,7 @@ #include #include #include +#include "err.h" #include "peer.h" struct rs_peer * @@ -23,18 +24,38 @@ peer_pick_peer (struct rs_connection *conn) return conn->active_peer; } +struct rs_peer * +peer_create (struct rs_context *ctx, struct rs_peer **rootp) +{ + struct rs_peer *p; + + p = (struct rs_peer *) rs_malloc (ctx, sizeof(*p)); + if (p) + { + memset (p, 0, sizeof(struct rs_peer)); + if (*rootp) + { + p->next = (*rootp)->next; + (*rootp)->next = p; + } + else + *rootp = p; + } + return p; +} + /* Public functions. */ int rs_peer_create (struct rs_connection *conn, struct rs_peer **peer_out) { struct rs_peer *peer; - peer = _rs_peer_create (conn->ctx, &conn->peers); + peer = peer_create (conn->ctx, &conn->peers); if (peer) { peer->conn = conn; - peer->realm->timeout = 2; - peer->realm->retries = 2; + peer->realm->timeout = 2; /* FIXME: Why? */ + peer->realm->retries = 2; /* FIXME: Why? */ } else return rs_err_conn_push_fl (conn, RSE_NOMEM, __FILE__, __LINE__, NULL); @@ -52,9 +73,9 @@ rs_peer_set_address (struct rs_peer *peer, const char *hostname, assert (peer); assert (peer->realm); - err = _rs_resolv (&peer->addr, peer->realm->type, hostname, service); + err = rs_resolv (&peer->addr, peer->realm->type, hostname, service); if (err) - return _rs_err_conn_push_err (peer->conn, err); + return err_conn_push_err (peer->conn, err); return RSE_OK; }