1 /* Copyright 2010, 2011 NORDUnet A/S. All rights reserved.
2 See the file COPYING for licensing information. */
4 #if defined HAVE_CONFIG_H
11 #include <event2/event.h>
12 #include <event2/bufferevent.h>
13 #include <radsec/radsec.h>
14 #include <radsec/radsec-impl.h>
21 conn_close (struct rs_connection **connp)
26 r = rs_conn_destroy (*connp);
33 conn_user_dispatch_p (const struct rs_connection *conn)
37 return (conn->callbacks.connected_cb ||
38 conn->callbacks.disconnected_cb ||
39 conn->callbacks.received_cb ||
40 conn->callbacks.sent_cb);
44 rs_conn_create (struct rs_context *ctx, struct rs_connection **conn,
47 struct rs_connection *c;
49 c = (struct rs_connection *) malloc (sizeof(struct rs_connection));
51 return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
53 memset (c, 0, sizeof(struct rs_connection));
58 struct rs_realm *r = rs_conf_find_realm (ctx, config);
64 c->peers = r->peers; /* FIXME: Copy instead? */
65 for (p = c->peers; p; p = p->next)
67 c->tryagain = r->retries;
71 c->realm = rs_malloc (ctx, sizeof (struct rs_realm));
73 return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__,
75 memset (c->realm, 0, sizeof (struct rs_realm));
85 rs_conn_set_type (struct rs_connection *conn, rs_conn_type_t type)
89 conn->realm->type = type;
93 struct rs_error * /* FIXME: Return int as all the others? */
94 _rs_resolv (struct evutil_addrinfo **addr, rs_conn_type_t type,
95 const char *hostname, const char *service)
98 struct evutil_addrinfo hints, *res = NULL;
100 memset (&hints, 0, sizeof(struct evutil_addrinfo));
101 hints.ai_family = AF_INET; /* IPv4 only. TODO: Set AF_UNSPEC. */
102 hints.ai_flags = AI_ADDRCONFIG;
105 case RS_CONN_TYPE_NONE:
106 return _rs_err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
107 case RS_CONN_TYPE_TCP:
109 case RS_CONN_TYPE_TLS:
110 hints.ai_socktype = SOCK_STREAM;
111 hints.ai_protocol = IPPROTO_TCP;
113 case RS_CONN_TYPE_UDP:
115 case RS_CONN_TYPE_DTLS:
116 hints.ai_socktype = SOCK_DGRAM;
117 hints.ai_protocol = IPPROTO_UDP;
120 return _rs_err_create (RSE_INVALID_CONN, __FILE__, __LINE__, NULL, NULL);
122 err = evutil_getaddrinfo (hostname, service, &hints, &res);
124 return _rs_err_create (RSE_BADADDR, __FILE__, __LINE__,
125 "%s:%s: bad host name or service name (%s)",
126 hostname, service, evutil_gai_strerror(err));
127 *addr = res; /* Simply use first result. */
132 rs_conn_add_listener (struct rs_connection *conn, rs_conn_type_t type,
133 const char *hostname, int port)
135 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
140 rs_conn_disconnect (struct rs_connection *conn)
146 err = evutil_closesocket (conn->fd);
152 rs_conn_destroy (struct rs_connection *conn)
158 /* NOTE: conn->realm is owned by context. */
159 /* NOTE: conn->peers is owned by context. */
161 if (conn->is_connected)
162 err = rs_conn_disconnect (conn);
164 event_free (conn->tev);
166 bufferevent_free (conn->bev);
168 event_base_free (conn->evb);
170 /* TODO: free tls_ctx */
171 /* TODO: free tls_ssl */
177 rs_conn_set_eventbase (struct rs_connection *conn, struct event_base *eb)
179 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
183 rs_conn_set_callbacks (struct rs_connection *conn, struct rs_conn_callbacks *cb)
186 memcpy (&conn->callbacks, cb, sizeof (conn->callbacks));
190 rs_conn_del_callbacks (struct rs_connection *conn)
193 memset (&conn->callbacks, 0, sizeof (conn->callbacks));
196 struct rs_conn_callbacks *
197 rs_conn_get_callbacks(struct rs_connection *conn)
200 return &conn->callbacks;
204 rs_conn_select_peer (struct rs_connection *conn, const char *name)
206 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
210 rs_conn_get_current_peer (struct rs_connection *conn, const char *name,
213 return rs_err_conn_push_fl (conn, RSE_NOSYS, __FILE__, __LINE__, NULL);
216 int rs_conn_fd (struct rs_connection *conn)
219 assert (conn->active_peer);
224 _rcb (struct rs_packet *packet, void *user_data)
226 struct rs_packet *pkt = (struct rs_packet *) user_data;
230 pkt->flags |= rs_packet_received_flag;
232 bufferevent_disable (pkt->conn->bev, EV_WRITE|EV_READ);
234 event_del (pkt->conn->rev);
237 /* Special function used in libradsec blocking dispatching mode,
238 i.e. with socket set to block on read/write and with no libradsec
239 callbacks registered.
241 For any other use of libradsec, a the received_cb callback should
242 be registered in the callbacks member of struct rs_connection.
244 On successful reception of a RADIUS message it will be verified
245 against REQ_MSG, if !NULL.
247 If PKT_OUT is !NULL it will upon return point at a pointer to a
248 struct rs_packet containing the message.
250 If anything goes wrong or if the read times out (TODO: explain),
251 PKT_OUT will not be changed and one or more errors are pushed on
252 the connection (available through rs_err_conn_pop()). */
254 rs_conn_receive_packet (struct rs_connection *conn,
255 struct rs_packet *req_msg,
256 struct rs_packet **pkt_out)
259 struct rs_packet *pkt = NULL;
262 assert (conn->realm);
263 assert (!conn_user_dispatch_p (conn)); /* Dispatching mode only. */
265 if (rs_packet_create (conn, &pkt))
271 assert (conn->active_peer);
272 assert (conn->fd >= 0);
274 conn->callbacks.received_cb = _rcb;
275 conn->user_data = pkt;
276 pkt->flags &= ~rs_packet_received_flag;
280 bufferevent_setwatermark (conn->bev, EV_READ, RS_HEADER_LEN, 0);
281 bufferevent_setcb (conn->bev, tcp_read_cb, NULL, tcp_event_cb, pkt);
282 bufferevent_enable (conn->bev, EV_READ);
286 err = event_add (conn->rev, NULL);
288 return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
290 evutil_gai_strerror (err));
294 rs_debug (("%s: entering event loop\n", __func__));
295 err = event_base_dispatch (conn->evb);
296 conn->callbacks.received_cb = NULL;
298 return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
299 "event_base_dispatch: %s",
300 evutil_gai_strerror (err));
301 rs_debug (("%s: event loop done\n", __func__));
303 if ((pkt->flags & rs_packet_received_flag) == 0
305 && packet_verify_response (pkt->conn, pkt, req_msg) != RSE_OK))
307 assert (rs_err_conn_peek_code (pkt->conn));