Restructure code, moving most code out of packet.c
[radsecproxy.git] / lib / conn.c
1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2    See the file COPYING for licensing information.  */
3
4 #if defined HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <string.h>
9 #include <assert.h>
10 #include <debug.h>
11 #include <event2/event.h>
12 #include <event2/bufferevent.h>
13 #include <radsec/radsec.h>
14 #include <radsec/radsec-impl.h>
15 #include "event.h"
16 #include "tcp.h"
17
18 int
19 rs_conn_create (struct rs_context *ctx, struct rs_connection **conn,
20                 const char *config)
21 {
22   struct rs_connection *c;
23
24   c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
25   if (!c)
26     return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
27
28   memset (c, 0, sizeof(struct rs_connection));
29   c->ctx = ctx;
30   c->fd = -1;
31   if (config)
32     {
33       struct rs_realm *r = rs_conf_find_realm (ctx, config);
34       if (r)
35         {
36           struct rs_peer *p;
37
38           c->realm = r;
39           c->peers = r->peers;  /* FIXME: Copy instead?  */
40           for (p = c->peers; p; p = p->next)
41             p->conn = c;
42           c->tryagain = r->retries;
43         }
44       else
45         {
46           c->realm = rs_malloc (ctx, sizeof (struct rs_realm));
47           if (!c->realm)
48             return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
49                                        NULL);
50           memset (c->realm, 0, sizeof (struct rs_realm));
51         }
52     }
53
54   if (conn)
55     *conn = c;
56   return RSE_OK;
57 }
58
59 void
60 rs_conn_set_type (struct rs_connection *conn, rs_conn_type_t type)
61 {
62   assert (conn);
63   assert (conn->realm);
64   conn->realm->type = type;
65 }
66
67
68 struct rs_error *          /* FIXME: Return int as all the others?  */
69 _rs_resolv (struct evutil_addrinfo **addr, rs_conn_type_t type,
70             const char *hostname, const char *service)
71 {
72   int err;
73   struct evutil_addrinfo hints, *res = NULL;
74
75   memset (&hints, 0, sizeof(struct evutil_addrinfo));
76   hints.ai_family = AF_INET;   /* IPv4 only.  TODO: Set AF_UNSPEC.  */
77   hints.ai_flags = AI_ADDRCONFIG;
78   switch (type)
79     {
80     case RS_CONN_TYPE_NONE:
81       return _rs_err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
82     case RS_CONN_TYPE_TCP:
83       /* Fall through.  */
84     case RS_CONN_TYPE_TLS:
85       hints.ai_socktype = SOCK_STREAM;
86       hints.ai_protocol = IPPROTO_TCP;
87       break;
88     case RS_CONN_TYPE_UDP:
89       /* Fall through.  */
90     case RS_CONN_TYPE_DTLS:
91       hints.ai_socktype = SOCK_DGRAM;
92       hints.ai_protocol = IPPROTO_UDP;
93       break;
94     default:
95       return _rs_err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
96     }
97   err = evutil_getaddrinfo (hostname, service, &hints, &res);
98   if (err)
99     return _rs_err_create (RSE_BADADDR, __FILE__, __LINE__,
100                            "%s:%s: bad host name or service name (%s)",
101                            hostname, service, evutil_gai_strerror(err));
102   *addr = res;                  /* Simply use first result.  */
103   return NULL;
104 }
105
106 int
107 rs_conn_add_listener (struct rs_connection *conn, rs_conn_type_t type,
108                       const char *hostname, int port)
109 {
110   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
111 }
112
113
114 int
115 rs_conn_disconnect (struct rs_connection *conn)
116 {
117   int err = 0;
118
119   assert (conn);
120
121   err = evutil_closesocket (conn->fd);
122   conn->fd = -1;
123   return err;
124 }
125
126 int
127 rs_conn_destroy (struct rs_connection *conn)
128 {
129   int err = 0;
130
131   assert (conn);
132
133   /* NOTE: conn->realm is owned by context.  */
134   /* NOTE: conn->peers is owned by context.  */
135
136   if (conn->is_connected)
137     err = rs_conn_disconnect (conn);
138   if (conn->tev)
139     event_free (conn->tev);
140   if (conn->bev)
141     bufferevent_free (conn->bev);
142   if (conn->evb)
143     event_base_free (conn->evb);
144
145   /* TODO: free tls_ctx  */
146   /* TODO: free tls_ssl  */
147
148   return err;
149 }
150
151 int
152 rs_conn_set_eventbase (struct rs_connection *conn, struct event_base *eb)
153 {
154   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
155 }
156
157 void
158 rs_conn_set_callbacks (struct rs_connection *conn, struct rs_conn_callbacks *cb)
159 {
160   assert (conn);
161   conn->user_dispatch_flag = 1;
162   memcpy (&conn->callbacks, cb, sizeof (conn->callbacks));
163 }
164
165 void
166 rs_conn_del_callbacks (struct rs_connection *conn)
167 {
168   assert (conn);
169   conn->user_dispatch_flag = 0;
170   memset (&conn->callbacks, 0, sizeof (conn->callbacks));
171 }
172
173 struct rs_conn_callbacks *
174 rs_conn_get_callbacks(struct rs_connection *conn)
175 {
176   assert (conn);
177   return &conn->callbacks;
178 }
179
180 int
181 rs_conn_select_peer (struct rs_connection *conn, const char *name)
182 {
183   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
184 }
185
186 int
187 rs_conn_get_current_peer (struct rs_connection *conn, const char *name,
188                           size_t buflen)
189 {
190   return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
191 }
192
193 int rs_conn_fd (struct rs_connection *conn)
194 {
195   assert (conn);
196   assert (conn->active_peer);
197   return conn->fd;
198 }
199
200 static void
201 _rcb (struct rs_packet *packet, void *user_data)
202 {
203   struct rs_packet *pkt = (struct rs_packet *) user_data;
204   assert (pkt);
205   pkt->valid_flag = 1;
206   if (pkt->conn->bev)
207     bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
208   else
209     event_del (pkt->conn->rev);
210 }
211
212 /* Special function used in libradsec blocking dispatching mode,
213    i.e. with socket set to block on read/write and with no libradsec
214    callbacks registered.
215
216    For any other use of libradsec, a the received_cb callback should
217    be registered in the callbacks member of struct rs_connection.
218
219    On successful reception, verification and decoding of a RADIUS
220    message, PKT_OUT will upon return point at a pointer to a struct
221    rs_packet containing the message.
222
223    If anything goes wrong or if the read times out (TODO: explain),
224    PKT_OUT will point at the NULL pointer and one or more errors are
225    pushed on the connection (available through rs_err_conn_pop()).  */
226
227 int
228 rs_conn_receive_packet (struct rs_connection *conn,
229                         struct rs_packet *request,
230                         struct rs_packet **pkt_out)
231 {
232   int err = 0;
233   struct rs_packet *pkt = NULL;
234
235   assert (conn);
236   assert (conn->realm);
237   assert (!conn->user_dispatch_flag); /* Dispatching mode only.  */
238
239   if (rs_packet_create (conn, pkt_out))
240     return -1;
241   pkt = *pkt_out;
242   pkt->conn = conn;
243   pkt->original = request;
244
245   assert (conn->evb);
246   assert (conn->bev);
247   assert (conn->active_peer);
248   assert (conn->fd >= 0);
249
250   conn->callbacks.received_cb = _rcb;
251   conn->user_data = pkt;
252   if (conn->bev)
253     {
254       bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
255       bufferevent_setcb (conn->bev, tcp_read_cb, NULL, tcp_event_cb, pkt);
256       bufferevent_enable (conn->bev, EV_READ);
257     }
258   else
259     {
260       err = event_add (conn->rev, NULL);
261       if (err < 0)
262         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
263                                     "event_add: %s",
264                                     evutil_gai_strerror (err));
265     }
266
267   /* Dispatch.  */
268   rs_debug (("%s: entering event loop\n", __func__));
269   err = event_base_dispatch (conn->evb);
270   conn->callbacks.received_cb = NULL;
271   if (err < 0)
272     return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
273                                 "event_base_dispatch: %s",
274                                 evutil_gai_strerror (err));
275   rs_debug (("%s: event loop done\n", __func__));
276
277   if (!pkt->valid_flag)
278     return -1;
279
280 #if defined (DEBUG)
281       rs_dump_packet (pkt);
282 #endif
283
284   pkt->original = NULL;         /* FIXME: Why?  */
285   return RSE_OK;
286 }