1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2 See the file COPYING for licensing information. */
4 #if defined HAVE_CONFIG_H
15 #include <freeradius/libradius.h>
16 #include <event2/event.h>
17 #include <event2/util.h>
18 #include <radsec/radsec.h>
19 #include <radsec/radsec-impl.h>
22 #include "rsp_debug.h"
23 #if defined (RS_ENABLE_TLS)
26 #include "../radsecproxy.h"
29 /* Public functions. */
31 rs_context_create (struct rs_context **ctx)
35 h = calloc (1, sizeof(*h));
39 #if defined (RS_ENABLE_TLS)
46 debug_init ("libradsec"); /* radsecproxy compat, FIXME: remove */
48 fr_randinit (&h->fr_randctx, 0);
49 fr_rand_seed (NULL, 0);
57 /** Initialize freeradius dictionary. */
59 rs_context_init_freeradius_dict (struct rs_context *ctx, const char *dict)
67 if (ctx->config != NULL)
68 dict = ctx->config->dictionary;
71 return rs_err_ctx_push_fl (ctx, RSE_INVAL, __FILE__, __LINE__,
72 "missing dictionary");
74 dictlen = strlen (dict);
75 dir = rs_calloc (ctx, 1, dictlen + 1);
76 fn = rs_calloc (ctx, 1, dictlen + 1);
77 if (dir == NULL || fn == NULL)
79 r = rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
82 strncpy (dir, dict, dictlen);
83 strncpy (fn, dict, dictlen);
85 if (dict_init (dirname (dir), basename (fn)) < 0)
87 r = rs_err_ctx_push_fl (ctx, RSE_FR, __FILE__, __LINE__, "dict_init");
99 struct rs_error * /* FIXME: Return int as all the others? */
100 rs_resolv (struct evutil_addrinfo **addr,
102 const char *hostname,
106 struct evutil_addrinfo hints, *res = NULL;
108 memset (&hints, 0, sizeof(struct evutil_addrinfo));
109 hints.ai_family = AF_INET; /* IPv4 only. TODO: Set AF_UNSPEC. */
110 hints.ai_flags = AI_ADDRCONFIG;
113 case RS_CONN_TYPE_NONE:
114 return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
115 case RS_CONN_TYPE_TCP:
117 case RS_CONN_TYPE_TLS:
118 hints.ai_socktype = SOCK_STREAM;
119 hints.ai_protocol = IPPROTO_TCP;
121 case RS_CONN_TYPE_UDP:
123 case RS_CONN_TYPE_DTLS:
124 hints.ai_socktype = SOCK_DGRAM;
125 hints.ai_protocol = IPPROTO_UDP;
128 return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
130 err = evutil_getaddrinfo (hostname, service, &hints, &res);
132 return err_create (RSE_BADADDR, __FILE__, __LINE__,
133 "%s:%s: bad host name or service name (%s)",
134 hostname, service, evutil_gai_strerror(err));
135 *addr = res; /* Simply use first result. */
140 rs_context_destroy (struct rs_context *ctx)
142 struct rs_realm *r = NULL;
143 struct rs_peer *p = NULL;
147 for (r = ctx->config->realms; r; )
149 struct rs_realm *tmp = r;
150 for (p = r->peers; p; )
152 struct rs_peer *tmp = p;
154 evutil_freeaddrinfo (p->addr);
166 if (ctx->config->cfg)
168 cfg_free (ctx->config->cfg);
169 ctx->config->cfg = NULL;
171 rs_free (ctx, ctx->config);
178 rs_context_set_alloc_scheme (struct rs_context *ctx,
179 struct rs_alloc_scheme *scheme)
181 return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);