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)
37 h = (struct rs_context *) malloc (sizeof(struct rs_context));
41 #if defined (RS_ENABLE_TLS)
48 debug_init ("libradsec"); /* radsecproxy compat, FIXME: remove */
50 memset (h, 0, sizeof(struct rs_context));
51 fr_randinit (&h->fr_randctx, 0);
52 fr_rand_seed (NULL, 0);
60 /** Initialize freeradius dictionary. */
62 rs_context_init_freeradius_dict (struct rs_context *ctx, const char *dict)
70 if (ctx->config != NULL)
71 dict = ctx->config->dictionary;
74 return rs_err_ctx_push_fl (ctx, RSE_INVAL, __FILE__, __LINE__,
75 "missing dictionary");
77 dictlen = strlen (dict);
78 dir = rs_calloc (ctx, 1, dictlen + 1);
79 fn = rs_calloc (ctx, 1, dictlen + 1);
80 if (dir == NULL || fn == NULL)
82 r = rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
85 strncpy (dir, dict, dictlen);
86 strncpy (fn, dict, dictlen);
88 if (dict_init (dirname (dir), basename (fn)) < 0)
90 r = rs_err_ctx_push_fl (ctx, RSE_FR, __FILE__, __LINE__, "dict_init");
102 struct rs_error * /* FIXME: Return int as all the others? */
103 rs_resolv (struct evutil_addrinfo **addr,
105 const char *hostname,
109 struct evutil_addrinfo hints, *res = NULL;
111 memset (&hints, 0, sizeof(struct evutil_addrinfo));
112 hints.ai_family = AF_INET; /* IPv4 only. TODO: Set AF_UNSPEC. */
113 hints.ai_flags = AI_ADDRCONFIG;
116 case RS_CONN_TYPE_NONE:
117 return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
118 case RS_CONN_TYPE_TCP:
120 case RS_CONN_TYPE_TLS:
121 hints.ai_socktype = SOCK_STREAM;
122 hints.ai_protocol = IPPROTO_TCP;
124 case RS_CONN_TYPE_UDP:
126 case RS_CONN_TYPE_DTLS:
127 hints.ai_socktype = SOCK_DGRAM;
128 hints.ai_protocol = IPPROTO_UDP;
131 return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
133 err = evutil_getaddrinfo (hostname, service, &hints, &res);
135 return err_create (RSE_BADADDR, __FILE__, __LINE__,
136 "%s:%s: bad host name or service name (%s)",
137 hostname, service, evutil_gai_strerror(err));
138 *addr = res; /* Simply use first result. */
143 rs_context_destroy (struct rs_context *ctx)
145 struct rs_realm *r = NULL;
146 struct rs_peer *p = NULL;
150 for (r = ctx->config->realms; r; )
152 struct rs_realm *tmp = r;
153 for (p = r->peers; p; )
155 struct rs_peer *tmp = p;
157 evutil_freeaddrinfo (p->addr);
169 if (ctx->config->cfg)
171 cfg_free (ctx->config->cfg);
172 ctx->config->cfg = NULL;
174 rs_free (ctx, ctx->config);
181 rs_context_set_alloc_scheme (struct rs_context *ctx,
182 struct rs_alloc_scheme *scheme)
184 return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);