diff options
| author | Linus Nordberg <linus@nordu.net> | 2011-03-09 10:14:20 +0100 | 
|---|---|---|
| committer | Linus Nordberg <linus@nordu.net> | 2011-03-09 10:14:20 +0100 | 
| commit | fac0219dad91c574417f78ec674aa0dd10949e15 (patch) | |
| tree | a30579377b88888eaacbc7489afe9f42a5f695a2 | |
| parent | 7385db38d82b26736ba56bc7aec4c79acc420824 (diff) | |
Rename and move around a few helper functions.
| -rw-r--r-- | lib/conf.c | 7 | ||||
| -rw-r--r-- | lib/conn.c | 41 | ||||
| -rw-r--r-- | lib/err.c | 75 | ||||
| -rw-r--r-- | lib/include/radsec/radsec-impl.h | 15 | ||||
| -rw-r--r-- | lib/peer.c | 31 | 
5 files changed, 88 insertions, 81 deletions
| @@ -9,6 +9,7 @@  #include <string.h>  #include <radsec/radsec.h>  #include <radsec/radsec-impl.h> +#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");  	}      } @@ -7,11 +7,11 @@  #include <string.h>  #include <assert.h> -#include <debug.h>  #include <event2/event.h>  #include <event2/bufferevent.h>  #include <radsec/radsec.h>  #include <radsec/radsec-impl.h> +#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) @@ -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 @@ -8,6 +8,7 @@  #include <assert.h>  #include <radsec/radsec.h>  #include <radsec/radsec-impl.h> +#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;  } | 
