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