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