summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Nordberg <linus@nordu.net>2010-10-10 15:53:37 +0200
committerLinus Nordberg <linus@nordu.net>2010-10-10 15:53:37 +0200
commit8a676ab2d88022aea68a2a31ac83dfe25d64e175 (patch)
tree5d644805b69e8dab4d44ed010706ccdb0e00a7d3
parent5d1a02eb41025020b8c25ab927baca978fb733a9 (diff)
Robustness fixes (and some callback invocation) by Luke Howard.
* lib/packet.c (_packet_create): Set packet identity properly. (_do_send): Return an int. (_do_send): Don't ignore rad_encode() errors. (_do_send): Do invoke rad_sign(). (_event_cb): Invoke callbacks. (_event_cb): Honour _do_send() return code. (_read_cb): Check packet (by invoking rad_packet_ok()). (_read_cb): Don't ignore rad_decode() errors. (_read_cb): Invoke callbacks.
-rw-r--r--lib/include/radsec/radsec-impl.h2
-rw-r--r--lib/packet.c39
2 files changed, 33 insertions, 8 deletions
diff --git a/lib/include/radsec/radsec-impl.h b/lib/include/radsec/radsec-impl.h
index b46bc47..d2ea095 100644
--- a/lib/include/radsec/radsec-impl.h
+++ b/lib/include/radsec/radsec-impl.h
@@ -63,9 +63,11 @@ struct rs_connection {
enum rs_conn_type type;
struct rs_credentials transport_credentials;
struct rs_conn_callbacks callbacks;
+ void *user_data;
struct rs_peer *peers;
struct rs_peer *active_peer;
struct rs_error *err;
+ int nextid;
};
struct rs_packet {
diff --git a/lib/packet.c b/lib/packet.c
index 54bfc01..6b95b73 100644
--- a/lib/packet.c
+++ b/lib/packet.c
@@ -24,7 +24,7 @@ _packet_create (struct rs_connection *conn, struct rs_packet **pkt_out)
rpkt = rad_alloc (1);
if (!rpkt)
return rs_err_conn_push (conn, RSE_NOMEM, __func__);
- rpkt->id = -1;
+ rpkt->id = conn->nextid++;
p = (struct rs_packet *) malloc (sizeof (struct rs_packet));
if (!p)
@@ -40,12 +40,17 @@ _packet_create (struct rs_connection *conn, struct rs_packet **pkt_out)
return RSE_OK;
}
-static void
+static int
_do_send (struct rs_packet *pkt)
{
int err;
- rad_encode (pkt->rpkt, NULL, pkt->conn->active_peer->secret);
+ if (rad_encode (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
+ return rs_err_conn_push_fl (pkg->conn, RSE_FR, __FILE__, __LINE__,
+ "rad_encode: %s", fr_strerror ());
+ if (rad_sign (pkt->rpkt, NULL, pkt->conn->active_peer->secret))
+ return rs_err_conn_push_fl (pkg->conn, RSE_FR, __FILE__, __LINE__,
+ "rad_sign: %s", fr_strerror ());
assert (pkt->rpkt);
#if defined (DEBUG)
{
@@ -63,16 +68,19 @@ _do_send (struct rs_packet *pkt)
err = bufferevent_write (pkt->conn->bev, pkt->rpkt->data,
pkt->rpkt->data_len);
if (err < 0)
- rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
- "bufferevent_write: %s", evutil_gai_strerror(err));
+ return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
+ "bufferevent_write: %s",
+ evutil_gai_strerror(err));
+ return RSE_OK;
}
static void
_event_cb (struct bufferevent *bev, short events, void *ctx)
{
- struct rs_packet *pkt = (struct rs_packet *) ctx;
+ struct rs_packet *pkt = (struct rs_packet *)ctx;
struct rs_connection *conn;
struct rs_peer *p;
+ int err;
assert (pkt);
assert (pkt->conn);
@@ -84,10 +92,17 @@ _event_cb (struct bufferevent *bev, short events, void *ctx)
if (events & BEV_EVENT_CONNECTED)
{
p->is_connected = 1;
+ if (conn->callbacks.connected_cb)
+ conn->callbacks.connected_cb (conn->user_data);
+
#if defined (DEBUG)
fprintf (stderr, "%s: connected\n", __func__);
#endif
- _do_send (pkt);
+ err = _do_send (pkt);
+ if (err)
+ return err;
+ if (conn->callbacks.sent_cb)
+ conn->callbacks.sent_cb (conn->user_data);
/* Packet will be freed in write callback. */
}
else if (events & BEV_EVENT_ERROR)
@@ -162,7 +177,14 @@ _read_cb (struct bufferevent *bev, void *ctx)
#if defined (DEBUG)
fprintf (stderr, "%s: complete packet read\n", __func__);
#endif
- rad_decode (pkt->rpkt, NULL, pkt->conn->active_peer->secret);
+ if (!rad_packet_ok (pkt->rpkt, 0) != 0)
+ return;
+ if (rad_decode (pkt->rpkt, NULL, pkt->conn->active_peer->secret) != 0)
+ return;
+
+ if (pkt->conn->callbacks.received_cb)
+ pkt->conn->callbacks.received_cb(pkt, pkt->conn->user_data);
+
if (event_base_loopbreak (pkt->conn->evb) < 0)
abort (); /* FIXME */
}
@@ -335,6 +357,7 @@ int
rs_packet_send (struct rs_packet *pkt, void *user_data)
{
struct rs_connection *conn;
+
assert (pkt);
conn = pkt->conn;