1 /* Copyright 2011 NORDUnet A/S. All rights reserved.
2 See the file COPYING for licensing information. */
4 #if defined HAVE_CONFIG_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>
15 #include <radsec/radsec.h>
16 #include <radsec/radsec-impl.h>
24 #include <event2/buffer.h>
28 _conn_timeout_cb (int fd, short event, void *data)
30 struct rs_connection *conn;
33 conn = (struct rs_connection *) data;
35 if (event & EV_TIMEOUT)
37 rs_debug (("%s: connection timeout on %p (fd %d) connecting to %p\n",
38 __func__, conn, conn->fd, conn->active_peer));
39 conn->is_connecting = 0;
40 rs_err_conn_push_fl (conn, RSE_TIMEOUT_CONN, __FILE__, __LINE__, NULL);
41 event_loopbreak (conn);
45 /* Read one RADIUS packet header. Return !0 on error. A return value
46 of 0 means that we need more data. */
48 _read_header (struct rs_packet *pkt)
52 n = bufferevent_read (pkt->conn->bev, pkt->hdr, RS_HEADER_LEN);
53 if (n == RS_HEADER_LEN)
55 pkt->flags |= rs_packet_hdr_read_flag;
56 pkt->rpkt->data_len = (pkt->hdr[2] << 8) + pkt->hdr[3];
57 if (pkt->rpkt->data_len < 20 || pkt->rpkt->data_len > 4096)
59 conn_close (&pkt->conn);
60 return rs_err_conn_push (pkt->conn, RSE_INVALID_PKT,
61 "invalid packet length: %d",
64 pkt->rpkt->data = rs_malloc (pkt->conn->ctx, pkt->rpkt->data_len);
67 conn_close (&pkt->conn);
68 return rs_err_conn_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__,
71 memcpy (pkt->rpkt->data, pkt->hdr, RS_HEADER_LEN);
72 bufferevent_setwatermark (pkt->conn->bev, EV_READ,
73 pkt->rpkt->data_len - RS_HEADER_LEN, 0);
74 rs_debug (("%s: packet header read, total pkt len=%d\n",
75 __func__, pkt->rpkt->data_len));
79 rs_debug (("%s: buffer frozen while reading header\n", __func__));
81 else /* Error: libevent gave us less than the low watermark. */
83 conn_close (&pkt->conn);
84 return rs_err_conn_push_fl (pkt->conn, RSE_INTERNAL, __FILE__, __LINE__,
85 "got %d octets reading header", n);
92 _read_packet (struct rs_packet *pkt)
96 rs_debug (("%s: trying to read %d octets of packet data\n", __func__,
97 pkt->rpkt->data_len - RS_HEADER_LEN));
99 n = bufferevent_read (pkt->conn->bev,
100 pkt->rpkt->data + RS_HEADER_LEN,
101 pkt->rpkt->data_len - RS_HEADER_LEN);
103 rs_debug (("%s: read %ld octets of packet data\n", __func__, n));
105 if (n == pkt->rpkt->data_len - RS_HEADER_LEN)
107 bufferevent_disable (pkt->conn->bev, EV_READ);
108 rs_debug (("%s: complete packet read\n", __func__));
109 pkt->flags &= ~rs_packet_hdr_read_flag;
110 memset (pkt->hdr, 0, sizeof(*pkt->hdr));
112 /* Checks done by rad_packet_ok:
113 - lenghts (FIXME: checks really ok for tcp?)
115 - attribute lengths >= 2
116 - attribute sizes adding up correctly */
117 if (!rad_packet_ok (pkt->rpkt, 0))
119 conn_close (&pkt->conn);
120 return rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
121 "invalid packet: %s", fr_strerror ());
125 /* Find out what happens if there's data left in the buffer. */
128 rest = evbuffer_get_length (bufferevent_get_input (pkt->conn->bev));
130 rs_debug (("%s: returning with %d octets left in buffer\n", __func__,
135 /* Hand over message to user. This changes ownership of pkt.
136 Don't touch it afterwards -- it might have been freed. */
137 if (pkt->conn->callbacks.received_cb)
138 pkt->conn->callbacks.received_cb (pkt, pkt->conn->user_data);
140 else if (n < 0) /* Buffer frozen. */
141 rs_debug (("%s: buffer frozen when reading packet\n", __func__));
142 else /* Short packet. */
143 rs_debug (("%s: waiting for another %d octets\n", __func__,
144 pkt->rpkt->data_len - RS_HEADER_LEN - n));
149 /* The read callback for TCP.
151 Read exactly one RADIUS message from BEV and store it in struct
152 rs_packet passed in USER_DATA.
154 Inform upper layer about successful reception of received RADIUS
155 message by invoking conn->callbacks.recevied_cb(), if !NULL. */
157 tcp_read_cb (struct bufferevent *bev, void *user_data)
159 struct rs_packet *pkt = (struct rs_packet *) user_data;
165 pkt->rpkt->sockfd = pkt->conn->fd;
166 pkt->rpkt->vps = NULL;
168 if ((pkt->flags & rs_packet_hdr_read_flag) == 0)
169 if (_read_header (pkt))
175 tcp_event_cb (struct bufferevent *bev, short events, void *user_data)
177 struct rs_packet *pkt = (struct rs_packet *) user_data;
178 struct rs_connection *conn = NULL;
179 struct rs_peer *p = NULL;
181 #if defined (RS_ENABLE_TLS)
182 unsigned long tlserr = 0;
187 assert (pkt->conn->active_peer);
189 p = conn->active_peer;
191 conn->is_connecting = 0;
192 if (events & BEV_EVENT_CONNECTED)
194 event_on_connect (conn, pkt);
196 else if (events & BEV_EVENT_EOF)
198 event_on_disconnect (conn);
200 else if (events & BEV_EVENT_TIMEOUT)
202 rs_debug (("%s: %p times out on %s\n", __func__, p,
203 (events & BEV_EVENT_READING) ? "read" : "write"));
204 rs_err_conn_push_fl (pkt->conn, RSE_TIMEOUT_IO, __FILE__, __LINE__, NULL);
206 else if (events & BEV_EVENT_ERROR)
208 sockerr = evutil_socket_geterror (conn->active_peer->fd);
209 if (sockerr == 0) /* FIXME: True that errno == 0 means closed? */
211 event_on_disconnect (conn);
215 rs_debug (("%s: %d: %d (%s)\n", __func__, conn->fd, sockerr,
216 evutil_socket_error_to_string (sockerr)));
217 rs_err_conn_push_fl (pkt->conn, RSE_SOCKERR, __FILE__, __LINE__,
218 "%d: %d (%s)", conn->fd, sockerr,
219 evutil_socket_error_to_string (sockerr));
221 #if defined (RS_ENABLE_TLS)
222 if (conn->tls_ssl) /* FIXME: correct check? */
224 for (tlserr = bufferevent_get_openssl_error (conn->bev);
226 tlserr = bufferevent_get_openssl_error (conn->bev))
228 rs_debug (("%s: openssl error: %s\n", __func__,
229 ERR_error_string (tlserr, NULL)));
230 rs_err_conn_push_fl (pkt->conn, RSE_SSLERR, __FILE__, __LINE__,
231 ERR_error_string (tlserr, NULL));
234 #endif /* RS_ENABLE_TLS */
235 event_loopbreak (conn);
239 if (events & BEV_EVENT_ERROR && events != BEV_EVENT_ERROR)
240 rs_debug (("%s: BEV_EVENT_ERROR and more: 0x%x\n", __func__, events));
245 tcp_write_cb (struct bufferevent *bev, void *ctx)
247 struct rs_packet *pkt = (struct rs_packet *) ctx;
252 if (pkt->conn->callbacks.sent_cb)
253 pkt->conn->callbacks.sent_cb (pkt->conn->user_data);
257 tcp_set_connect_timeout (struct rs_connection *conn)
260 conn->tev = evtimer_new (conn->evb, _conn_timeout_cb, conn);
262 return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
264 evtimer_add (conn->tev, &conn->timeout);