port to new RADIUS client library
[radsecproxy.git] / lib / event.c
1 /* Copyright 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 <assert.h>
9 #include <string.h>
10 #include <errno.h>
11
12 #include <event2/event.h>
13 #include <event2/bufferevent.h>
14 #if defined (RS_ENABLE_TLS)
15 #include <event2/bufferevent_ssl.h>
16 #include <openssl/err.h>
17 #endif
18 #include <radsec/radsec.h>
19 #include <radsec/radsec-impl.h>
20 #include "tcp.h"
21 #include "udp.h"
22 #if defined (RS_ENABLE_TLS)
23 #include "tls.h"
24 #endif
25 #include "event.h"
26 #include "packet.h"
27 #include "conn.h"
28 #include "debug.h"
29
30 #if defined (DEBUG)
31 extern int _event_debug_mode_on;
32 #endif
33
34 static void
35 _evlog_cb (int severity, const char *msg)
36 {
37   const char *sevstr;
38   switch (severity)
39     {
40     case _EVENT_LOG_DEBUG:
41 #if !defined (DEBUG_LEVENT)
42       return;
43 #endif
44       sevstr = "debug";
45       break;
46     case _EVENT_LOG_MSG:
47       sevstr = "msg";
48       break;
49     case _EVENT_LOG_WARN:
50       sevstr = "warn";
51       break;
52     case _EVENT_LOG_ERR:
53       sevstr = "err";
54       break;
55     default:
56       sevstr = "???";
57       break;
58     }
59   fprintf (stderr, "libevent: [%s] %s\n", sevstr, msg); /* FIXME: stderr?  */
60 }
61
62 void
63 event_conn_timeout_cb (int fd, short event, void *data)
64 {
65   struct rs_connection *conn = NULL;
66
67   assert (data);
68   conn = (struct rs_connection *) data;
69
70   if (event & EV_TIMEOUT)
71     {
72       rs_debug (("%s: connection timeout on %p (fd %d) connecting to %p\n",
73                  __func__, conn, conn->fd, conn->active_peer));
74       conn->is_connecting = 0;
75       rs_err_conn_push_fl (conn, RSE_TIMEOUT_CONN, __FILE__, __LINE__, NULL);
76       event_loopbreak (conn);
77     }
78 }
79
80 void
81 event_retransmit_timeout_cb (int fd, short event, void *data)
82 {
83   struct rs_connection *conn = NULL;
84
85   assert (data);
86   conn = (struct rs_connection *) data;
87
88   if (event & EV_TIMEOUT)
89     {
90       rs_debug (("%s: retransmission timeout on %p (fd %d) sending to %p\n",
91                  __func__, conn, conn->fd, conn->active_peer));
92       rs_err_conn_push_fl (conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
93       event_loopbreak (conn);
94     }
95 }
96
97 int
98 event_init_socket (struct rs_connection *conn, struct rs_peer *p)
99 {
100   if (conn->fd != -1)
101     return RSE_OK;
102
103   assert (p->addr);
104   conn->fd = socket (p->addr->ai_family, p->addr->ai_socktype,
105                      p->addr->ai_protocol);
106   if (conn->fd < 0)
107     return rs_err_conn_push_fl (conn, RSE_SOCKERR, __FILE__, __LINE__,
108                                 "socket: %d (%s)",
109                                 errno, strerror (errno));
110   if (evutil_make_socket_nonblocking (conn->fd) < 0)
111     {
112       evutil_closesocket (conn->fd);
113       conn->fd = -1;
114       return rs_err_conn_push_fl (conn, RSE_SOCKERR, __FILE__, __LINE__,
115                                   "evutil_make_socket_nonblocking: %d (%s)",
116                                   errno, strerror (errno));
117     }
118   return RSE_OK;
119 }
120
121 int
122 event_init_bufferevent (struct rs_connection *conn, struct rs_peer *peer)
123 {
124   if (conn->bev)
125     return RSE_OK;
126
127   if (conn->realm->type == RS_CONN_TYPE_TCP)
128     {
129       conn->bev = bufferevent_socket_new (conn->evb, conn->fd, 0);
130       if (!conn->bev)
131         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
132                                     "bufferevent_socket_new");
133     }
134 #if defined (RS_ENABLE_TLS)
135   else if (conn->realm->type == RS_CONN_TYPE_TLS)
136     {
137       if (rs_tls_init (conn))
138         return -1;
139       /* Would be convenient to pass BEV_OPT_CLOSE_ON_FREE but things
140          seem to break when be_openssl_ctrl() (in libevent) calls
141          SSL_set_bio() after BIO_new_socket() with flag=1.  */
142       conn->bev =
143         bufferevent_openssl_socket_new (conn->evb, conn->fd, conn->tls_ssl,
144                                         BUFFEREVENT_SSL_CONNECTING, 0);
145       if (!conn->bev)
146         return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
147                                     "bufferevent_openssl_socket_new");
148     }
149 #endif  /* RS_ENABLE_TLS */
150   else
151     {
152       return rs_err_conn_push_fl (conn, RSE_INTERNAL, __FILE__, __LINE__,
153                                   "%s: unknown connection type: %d", __func__,
154                                   conn->realm->type);
155     }
156
157   return RSE_OK;
158 }
159
160 void
161 event_do_connect (struct rs_connection *conn)
162 {
163   struct rs_peer *p;
164   int err, sockerr;
165
166   assert (conn);
167   assert (conn->active_peer);
168   p = conn->active_peer;
169
170 #if defined (DEBUG)
171   {
172     char host[80], serv[80];
173
174     getnameinfo (p->addr->ai_addr,
175                  p->addr->ai_addrlen,
176                  host, sizeof(host), serv, sizeof(serv),
177                  0 /* NI_NUMERICHOST|NI_NUMERICSERV*/);
178     rs_debug (("%s: connecting to %s:%s\n", __func__, host, serv));
179   }
180 #endif
181
182   if (p->conn->bev)             /* TCP */
183     {
184       conn_activate_timeout (conn); /* Connect timeout.  */
185       err = bufferevent_socket_connect (p->conn->bev, p->addr->ai_addr,
186                                         p->addr->ai_addrlen);
187       if (err < 0)
188         rs_err_conn_push_fl (p->conn, RSE_EVENT, __FILE__, __LINE__,
189                              "bufferevent_socket_connect: %s",
190                              evutil_gai_strerror (err));
191       else
192         p->conn->is_connecting = 1;
193     }
194   else                          /* UDP */
195     {
196       err = connect (p->conn->fd, p->addr->ai_addr, p->addr->ai_addrlen);
197       if (err < 0)
198         {
199           sockerr = evutil_socket_geterror (p->conn->fd);
200           rs_debug (("%s: %d: connect: %d (%s)\n", __func__, p->conn->fd,
201                      sockerr, evutil_socket_error_to_string (sockerr)));
202           rs_err_conn_push_fl (p->conn, RSE_SOCKERR, __FILE__, __LINE__,
203                                "%d: connect: %d (%s)", p->conn->fd, sockerr,
204                                evutil_socket_error_to_string (sockerr));
205         }
206     }
207 }
208
209 int
210 event_loopbreak (struct rs_connection *conn)
211 {
212   int err = event_base_loopbreak (conn->evb);
213   if (err < 0)
214     rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
215                          "event_base_loopbreak: %s",
216                          evutil_gai_strerror (err));
217   return err;
218 }
219
220
221 void
222 event_on_disconnect (struct rs_connection *conn)
223 {
224   conn->is_connecting = 0;
225   conn->is_connected = 0;
226   rs_debug (("%s: %p disconnected\n", __func__, conn->active_peer));
227   if (conn->callbacks.disconnected_cb)
228     conn->callbacks.disconnected_cb (conn->user_data);
229 }
230
231 void
232 event_on_connect (struct rs_connection *conn, struct rs_packet *pkt)
233 {
234   assert (!conn->is_connecting);
235   conn->is_connected = 1;
236   rs_debug (("%s: %p connected\n", __func__, conn->active_peer));
237
238   if (conn->callbacks.connected_cb)
239     conn->callbacks.connected_cb (conn->user_data);
240
241   if (pkt)
242     packet_do_send (pkt);
243 }
244
245 int
246 event_init_eventbase (struct rs_connection *conn)
247 {
248   assert (conn);
249   if (conn->evb)
250     return RSE_OK;
251
252 #if defined (DEBUG)
253   if (!_event_debug_mode_on)
254     event_enable_debug_mode ();
255 #endif
256   event_set_log_callback (_evlog_cb);
257   conn->evb = event_base_new ();
258   if (!conn->evb)
259     return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
260                                 "event_base_new");
261
262   return RSE_OK;
263 }