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>
20 #if defined (RS_ENABLE_TLS)
25 #include "../radsecproxy.h"
27 #include "rsp_debug.h"
30 /* Public functions. */
32 rs_context_create (struct rs_context **ctx, const char *dict)
36 char *buf1 = NULL, *buf2 = NULL;
43 h = (struct rs_context *) malloc (sizeof(struct rs_context));
47 /* Initialize freeradius dictionary. */
48 buf1 = malloc (strlen (dict) + 1);
49 buf2 = malloc (strlen (dict) + 1);
59 if (dict_init (dir, fn) < 0)
67 #if defined (RS_ENABLE_TLS)
74 debug_init ("libradsec"); /* radsecproxy compat, FIXME: remove */
76 memset (h, 0, sizeof(struct rs_context));
77 fr_randinit (&h->fr_randctx, 0);
78 fr_rand_seed (NULL, 0);
95 struct rs_error * /* FIXME: Return int as all the others? */
96 rs_resolv (struct evutil_addrinfo **addr,
102 struct evutil_addrinfo hints, *res = NULL;
104 memset (&hints, 0, sizeof(struct evutil_addrinfo));
105 hints.ai_family = AF_INET; /* IPv4 only. TODO: Set AF_UNSPEC. */
106 hints.ai_flags = AI_ADDRCONFIG;
109 case RS_CONN_TYPE_NONE:
110 return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
111 case RS_CONN_TYPE_TCP:
113 case RS_CONN_TYPE_TLS:
114 hints.ai_socktype = SOCK_STREAM;
115 hints.ai_protocol = IPPROTO_TCP;
117 case RS_CONN_TYPE_UDP:
119 case RS_CONN_TYPE_DTLS:
120 hints.ai_socktype = SOCK_DGRAM;
121 hints.ai_protocol = IPPROTO_UDP;
124 return err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
126 err = evutil_getaddrinfo (hostname, service, &hints, &res);
128 return err_create (RSE_BADADDR, __FILE__, __LINE__,
129 "%s:%s: bad host name or service name (%s)",
130 hostname, service, evutil_gai_strerror(err));
131 *addr = res; /* Simply use first result. */
136 _rs_peer_destroy (struct rs_peer *p)
140 assert (p->conn->ctx);
142 /* NOTE: The peer object doesn't own conn, nor realm. */
143 /* NOTE: secret is owned by config */
146 evutil_freeaddrinfo (p->addr);
149 rs_free (p->conn->ctx, p);
153 rs_context_destroy (struct rs_context *ctx)
155 struct rs_realm *r = NULL;
156 struct rs_peer *p = NULL;
158 for (r = ctx->realms; r; )
160 struct rs_realm *tmp = r;
161 for (p = r->peers; p; )
163 struct rs_peer *tmp = p;
165 _rs_peer_destroy (tmp);
179 rs_context_set_alloc_scheme (struct rs_context *ctx,
180 struct rs_alloc_scheme *scheme)
182 return rs_err_ctx_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__, NULL);