1 /* Copyright 2011,2013 NORDUnet A/S. All rights reserved.
2 See LICENSE for licensing information. */
4 #if defined HAVE_CONFIG_H
10 #include <sys/socket.h>
11 #include <event2/event.h>
12 #include <radius/client.h>
13 #include <radsec/radsec.h>
14 #include <radsec/radsec-impl.h>
20 /* Send one packet, the first in queue. */
22 _send (struct rs_connection *conn, int fd)
25 struct rs_message *msg = conn->out_queue;
28 assert (msg->rpkt->data);
31 r = compat_send (fd, msg->rpkt->data, msg->rpkt->length, 0);
34 int sockerr = evutil_socket_geterror (msg->conn->fd);
35 if (sockerr != EAGAIN)
36 return rs_err_conn_push_fl (msg->conn, RSE_SOCKERR, __FILE__, __LINE__,
37 "%d: send: %d (%s)", fd, sockerr,
38 evutil_socket_error_to_string (sockerr));
41 assert (r == msg->rpkt->length);
42 /* Unlink the message. */
43 conn->out_queue = msg->next;
45 /* If there are more messages in queue, add the write event again. */
46 if (msg->conn->out_queue)
48 r = event_add (msg->conn->base_.wev, NULL);
50 return rs_err_conn_push_fl (msg->conn, RSE_EVENT, __FILE__, __LINE__,
51 "event_add: %s", evutil_gai_strerror (r));
52 rs_debug (("%s: re-adding the write event\n", __func__));
58 /* Callback for conn->wev and conn->rev. FIXME: Rename.
60 USER_DATA contains connection for EV_READ and a message for
61 EV_WRITE. This is because we don't have a connect/establish entry
62 point at the user level -- send implies connect so when we're
63 connected we need the message to send. */
65 _evcb (evutil_socket_t fd, short what, void *user_data)
69 rs_debug (("%s: fd=%d what =", __func__, fd));
70 if (what & EV_TIMEOUT) rs_debug ((" TIMEOUT"));
71 if (what & EV_READ) rs_debug ((" READ"));
72 if (what & EV_WRITE) rs_debug ((" WRITE"));
77 /* Read a single UDP packet and stick it in USER_DATA. */
78 /* TODO: Verify that unsolicited packets are dropped. */
79 struct rs_message *msg = (struct rs_message *) user_data;
84 assert (msg->rpkt->data);
86 r = compat_recv (fd, msg->rpkt->data, RS_MAX_PACKET_LEN, MSG_TRUNC);
89 int sockerr = evutil_socket_geterror (msg->conn->fd);
90 if (sockerr == EAGAIN)
92 /* FIXME: Really shouldn't happen since we've been told
93 that fd is readable! */
94 rs_debug (("%s: EAGAIN reading UDP packet -- wot?"));
99 rs_err_conn_push_fl (msg->conn, RSE_SOCKERR, __FILE__, __LINE__,
100 "%d: recv: %d (%s)", fd, sockerr,
101 evutil_socket_error_to_string (sockerr));
102 event_del (msg->conn->tev);
105 event_del (msg->conn->tev);
106 if (r < 20 || r > RS_MAX_PACKET_LEN) /* Short or long packet. */
108 rs_err_conn_push (msg->conn, RSE_INVALID_MSG,
109 "invalid message length: %d",
113 msg->rpkt->length = (msg->rpkt->data[2] << 8) + msg->rpkt->data[3];
114 err = nr_packet_ok (msg->rpkt);
117 rs_err_conn_push_fl (msg->conn, err, __FILE__, __LINE__,
121 /* Hand over message to user. This changes ownership of msg.
122 Don't touch it afterwards -- it might have been freed. */
123 if (msg->conn->callbacks.received_cb)
124 msg->conn->callbacks.received_cb (msg, msg->conn->base_.user_data);
126 else if (what & EV_WRITE)
128 struct rs_message *msg = (struct rs_message *) user_data;
132 if (msg->conn->state == RS_CONN_STATE_CONNECTING)
133 event_on_connect (msg->conn, msg);
135 if (msg->conn->out_queue)
136 if (_send (msg->conn, fd) == RSE_OK)
137 if (msg->conn->callbacks.sent_cb)
138 msg->conn->callbacks.sent_cb (msg->conn->base_.user_data);
142 if (what & EV_TIMEOUT)
143 rs_debug (("%s: timeout on UDP event, shouldn't happen\n", __func__));
148 udp_init (struct rs_connection *conn, struct rs_message *msg)
150 assert (!conn->base_.bev);
152 conn->base_.rev = event_new (conn->base_.ctx->evb, conn->base_.fd,
153 EV_READ|EV_PERSIST, _evcb, NULL);
154 conn->base_.wev = event_new (conn->base_.ctx->evb, conn->base_.fd,
155 EV_WRITE, _evcb, NULL);
156 if (!conn->base_.rev || !conn->base_.wev)
160 event_free (conn->base_.rev);
161 conn->base_.rev = NULL;
163 /* ENOMEM _or_ EINVAL but EINVAL only if we use EV_SIGNAL, at
164 least for now (libevent-2.0.5). */
165 return rs_err_conn_push_fl (conn, RSE_NOMEM, __FILE__, __LINE__, NULL);
171 udp_init_retransmit_timer (struct rs_connection *conn)
174 assert (conn->base_.ctx);
175 assert (conn->base_.ctx->evb);
178 event_free (conn->tev);
180 evtimer_new (conn->base_.ctx->evb, event_retransmit_timeout_cb, conn);
182 return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,