WIP.
[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 "libradsec.h"
9 #include "libradsec-impl.h"
10
11 int
12 rs_context_create(struct rs_handle **ctx, const char *dict)
13 {
14   struct rs_handle *h;
15
16   if (ctx)
17     *ctx = NULL;
18   h = (struct rs_handle *) malloc (sizeof(struct rs_handle));
19   if (h)
20     {
21       char *buf1 = NULL, *buf2 = NULL;
22       char *dir, *fn;
23
24       buf1 = malloc (strlen (dict) + 1);
25       buf2 = malloc (strlen (dict) + 1);
26       if (!buf1 || !buf2)
27         {
28           free (h);
29           if (buf1)
30             free (buf1);
31           if (buf2)
32             free (buf2);
33           return RSE_NOMEM;
34         }
35       strcpy (buf1, dict);
36       dir = dirname (buf1);
37       strcpy (buf2, dict);
38       fn = basename (buf2);
39       if (dict_init (dir, fn) < 0)
40         {
41           free (h);
42           return RSE_SOME_ERROR;
43         }
44       free (buf1);
45       free (buf2);
46 #if defined (DEBUG)
47       fr_log_fp = stderr;
48       fr_debug_flag = 1;
49 #endif
50
51       memset (h, 0, sizeof(struct rs_handle));
52       fr_randinit (&h->fr_randctx, 0);
53       fr_rand_seed (NULL, 0);
54
55       if (ctx)
56         *ctx = h;
57     }
58   return h ? RSE_OK : RSE_NOMEM;
59 }
60
61 void rs_context_destroy(struct rs_handle *ctx)
62 {
63   free (ctx);
64 }
65
66 int rs_context_set_alloc_scheme(struct rs_handle *ctx, struct rs_alloc_scheme *scheme)
67 {
68   return RSE_NOSYS;
69 }
70
71 int rs_context_config_read(struct rs_handle *ctx, const char *config_file)
72 {
73   return RSE_NOSYS;
74 }
75
76 int rs_conn_create(struct rs_handle *ctx, struct rs_connection **conn)
77 {
78   struct rs_connection *c;
79
80   c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
81   if (c)
82     {
83       memset (c, 0, sizeof(struct rs_connection));
84       c->ctx = ctx;
85       c->peers.next = &c->peers;
86     }
87   if (conn)
88     *conn = c;
89   return c ? RSE_OK : rs_ctx_err_push (ctx, RSE_NOMEM, NULL);
90 }
91
92 struct addrinfo *
93 _resolv (const char *host, int port)
94 {
95   return NULL;
96 }
97
98 int rs_conn_add_server(struct rs_connection *conn, struct rs_peer **server, rs_conn_type_t type, const char *host, int port)
99 {
100   struct rs_peer *srv;
101
102   if (conn->type == RS_CONN_TYPE_NONE)
103     conn->type = type;
104   else if (conn->type != type)
105     return rs_conn_err_push (conn, RSE_CONN_TYPE_MISMATCH, NULL);
106
107   srv = (struct rs_peer *) malloc (sizeof(struct rs_peer));
108   if (srv)
109     {
110       memset (srv, 0, sizeof(struct rs_peer));
111       srv->conn = conn;
112       srv->addr = _resolv (host, port);
113       srv->timeout = 10;
114       srv->tries = 3;
115       srv->next = conn->peers.next;
116       conn->peers.next = srv;
117     }
118   if (*server)
119     *server = srv;
120   return srv ? RSE_OK : rs_conn_err_push (conn, RSE_NOMEM, NULL);
121 }
122
123 void rs_server_set_timeout(struct rs_peer *server, int timeout)
124 {
125   server->timeout = timeout;
126 }
127 void rs_server_set_tries(struct rs_peer *server, int tries)
128 {
129   server->tries = tries;
130 }
131 int rs_server_set_secret(struct rs_peer *server, const char *secret)
132 {
133   if (server->secret)
134     free (server->secret);
135   server->secret = (char *) malloc (strlen(secret) + 1);
136   if (!server->secret)
137     return rs_conn_err_push (server->conn, RSE_NOMEM, NULL);
138   strcpy (server->secret, secret);
139   return RSE_OK;
140 }
141
142 int rs_conn_add_listener(struct rs_connection *conn, rs_conn_type_t type, const char *host, int port)
143 {
144   return RSE_NOSYS;
145 }
146
147 int rs_conn_destroy(struct rs_connection  *conn)
148 {
149   return RSE_NOSYS;
150 }
151
152 int rs_conn_set_eventbase(struct rs_connection *conn, struct event_base *eb)
153 {
154   return RSE_NOSYS;
155 }
156
157 int rs_conn_set_callbacks(struct rs_connection *conn, struct rs_conn_callbacks *cb)
158 {
159   return RSE_NOSYS;
160 }
161
162 int rs_conn_set_server(struct rs_connection *conn, const char *name)
163 {
164   return RSE_NOSYS;
165 }
166
167 int rs_conn_get_server(const struct rs_connection *conn, const char *name, size_t buflen)
168 {
169   return RSE_NOSYS;
170 }
171
172 int rs_conn_open(struct rs_connection *conn)
173 {
174   return rs_conn_err_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__,
175                               "%s: NYI", __func__);
176 }