Refactoring in preparation for handling more cases than client sending one packet.
[libradsec.git] / lib / radsec.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <libgen.h>
6
7 #include <freeradius/libradius.h>
8 #include <event2/event.h>
9 #include <event2/util.h>
10 #include "libradsec.h"
11 #include "libradsec-impl.h"
12
13 int
14 rs_context_create(struct rs_handle **ctx, const char *dict)
15 {
16   struct rs_handle *h;
17
18   if (ctx)
19     *ctx = NULL;
20   h = (struct rs_handle *) malloc (sizeof(struct rs_handle));
21   if (h)
22     {
23       char *buf1 = NULL, *buf2 = NULL;
24       char *dir, *fn;
25
26       buf1 = malloc (strlen (dict) + 1);
27       buf2 = malloc (strlen (dict) + 1);
28       if (!buf1 || !buf2)
29         {
30           free (h);
31           if (buf1)
32             free (buf1);
33           if (buf2)
34             free (buf2);
35           return RSE_NOMEM;
36         }
37       strcpy (buf1, dict);
38       dir = dirname (buf1);
39       strcpy (buf2, dict);
40       fn = basename (buf2);
41       if (dict_init (dir, fn) < 0)
42         {
43           free (h);
44           return RSE_SOME_ERROR;
45         }
46       free (buf1);
47       free (buf2);
48 #if defined (DEBUG)
49       fr_log_fp = stderr;
50       fr_debug_flag = 1;
51 #endif
52
53       memset (h, 0, sizeof(struct rs_handle));
54       fr_randinit (&h->fr_randctx, 0);
55       fr_rand_seed (NULL, 0);
56
57       if (ctx)
58         *ctx = h;
59     }
60   return h ? RSE_OK : RSE_NOMEM;
61 }
62
63 void rs_context_destroy(struct rs_handle *ctx)
64 {
65   free (ctx);
66 }
67
68 int rs_context_set_alloc_scheme(struct rs_handle *ctx, struct rs_alloc_scheme *scheme)
69 {
70   return rs_ctx_err_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__,
71                              "%s: NYI", __func__);
72 }
73
74 int rs_context_config_read(struct rs_handle *ctx, const char *config_file)
75 {
76   return rs_ctx_err_push_fl (ctx, RSE_NOSYS, __FILE__, __LINE__,
77                              "%s: NYI", __func__);
78 }
79
80 int rs_conn_create(struct rs_handle *ctx, struct rs_connection **conn)
81 {
82   struct rs_connection *c;
83
84   c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
85   if (c)
86     {
87       memset (c, 0, sizeof(struct rs_connection));
88       c->ctx = ctx;
89     }
90   if (conn)
91     *conn = c;
92   return c ? RSE_OK : rs_ctx_err_push (ctx, RSE_NOMEM, NULL);
93 }
94
95 struct addrinfo *
96 _resolv (struct rs_connection *conn, const char *hostname, int port)
97 {
98   int err;
99   char portstr[6];
100   struct evutil_addrinfo hints, *res = NULL;
101
102   snprintf (portstr, sizeof(portstr), "%d", port);
103   memset (&hints, 0, sizeof(struct evutil_addrinfo));
104   //hints.ai_family = AF_UNSPEC;        /* v4 or v6.  */
105   hints.ai_family = AF_INET;    /* FIXME: v4 only, while debuging */
106   hints.ai_flags = AI_ADDRCONFIG;
107   switch (conn->type)
108     {
109     case RS_CONN_TYPE_NONE:
110       rs_conn_err_push_fl (conn, RSE_INVALID_CONN, __FILE__, __LINE__, NULL);
111       return NULL;
112     case RS_CONN_TYPE_TCP:
113     case RS_CONN_TYPE_TLS:
114       hints.ai_socktype = SOCK_STREAM;
115       hints.ai_protocol = IPPROTO_TCP;
116       break;
117     case RS_CONN_TYPE_UDP:
118     case RS_CONN_TYPE_DTLS:
119       hints.ai_socktype = SOCK_DGRAM;
120       hints.ai_protocol = IPPROTO_UDP;
121       break;
122     }
123   err = evutil_getaddrinfo (hostname, portstr, &hints, &res);
124   if (err)
125     rs_conn_err_push_fl (conn, RSE_BADADDR, __FILE__, __LINE__,
126                          " %s:%d: bad host name or port (%s)",
127                          hostname, port, evutil_gai_strerror(err));
128   return res;                   /* Simply use first result.  */
129 }
130
131 static struct rs_peer *
132 _peer_new (struct rs_connection *conn, const char *hostname, int port)
133 {
134   struct rs_peer *p;
135   struct evutil_addrinfo *addr;
136
137   addr = _resolv (conn, hostname, port);
138   if (!addr)
139     return NULL;
140
141   p = (struct rs_peer *) malloc (sizeof(*p));
142   if (p)
143     {
144       memset (p, 0, sizeof(struct rs_peer));
145       p->conn = conn;
146       p->s = -1;
147       p->addr = addr;
148       p->next = conn->peers;
149       if (conn->peers)
150         conn->peers->next = p;
151       else
152         conn->peers = p;
153     }
154   else
155     {
156       evutil_freeaddrinfo (addr);
157       rs_conn_err_push_fl (conn, RSE_NOMEM, __FILE__, __LINE__, NULL);
158     }
159   return p;
160 }
161
162 int
163 rs_conn_add_server(struct rs_connection *conn, struct rs_peer **server,
164                    rs_conn_type_t type, const char *hostname, int port)
165 {
166   struct rs_peer *srv;
167
168   if (conn->type == RS_CONN_TYPE_NONE)
169     conn->type = type;
170   else if (conn->type != type)
171     return rs_conn_err_push (conn, RSE_CONN_TYPE_MISMATCH, NULL);
172
173   srv = _peer_new (conn, hostname, port);
174   if (srv)
175     {
176       srv->timeout = 10;
177       srv->tries = 3;
178     }
179   if (*server)
180     *server = srv;
181   return srv ? RSE_OK : rs_conn_err_push (conn, RSE_NOMEM, NULL);
182 }
183
184 void rs_server_set_timeout(struct rs_peer *server, int timeout)
185 {
186   server->timeout = timeout;
187 }
188 void rs_server_set_tries(struct rs_peer *server, int tries)
189 {
190   server->tries = tries;
191 }
192 int rs_server_set_secret(struct rs_peer *server, const char *secret)
193 {
194   if (server->secret)
195     free (server->secret);
196   server->secret = (char *) malloc (strlen(secret) + 1);
197   if (!server->secret)
198     return rs_conn_err_push (server->conn, RSE_NOMEM, NULL);
199   strcpy (server->secret, secret);
200   return RSE_OK;
201 }
202
203 int rs_conn_add_listener(struct rs_connection *conn, rs_conn_type_t type, const char *hostname, int port)
204 {
205   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
206                               "%s: NYI", __func__);
207 }
208
209 void
210 rs_conn_destroy(struct rs_connection *conn)
211 {
212   struct rs_peer *p;
213
214 #warning "TODO: Disconnect active_peer."
215
216   for (p = conn->peers; p; p = p->next)
217     {
218       if (p->addr)
219         evutil_freeaddrinfo (p->addr);
220       if (p->secret)
221         rs_free (conn->ctx, p->secret);
222     }
223
224   if (conn->evb)
225     event_base_free (conn->evb);
226 }
227
228 int rs_conn_set_eventbase(struct rs_connection *conn, struct event_base *eb)
229 {
230   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
231                               "%s: NYI", __func__);
232 }
233
234 int rs_conn_set_callbacks(struct rs_connection *conn, struct rs_conn_callbacks *cb)
235 {
236   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
237                               "%s: NYI", __func__);
238 }
239
240 int rs_conn_set_server(struct rs_connection *conn, const char *name)
241 {
242   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
243                               "%s: NYI", __func__);
244 }
245
246 int rs_conn_get_current_server(struct rs_connection *conn, const char *name, size_t buflen)
247 {
248   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
249                               "%s: NYI", __func__);
250 }
251