c04821df39f8dab1b09a410dbd83481f4ca57251
[libradsec.git] / lib / udp.c
1 /* Copyright 2011 NORDUnet A/S. All rights reserved.
2    See the file COPYING for licensing information.  */
3
4 #if defined HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <assert.h>
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <event2/event.h>
12 #include <radsec/radsec.h>
13 #include <radsec/radsec-impl.h>
14 #include "debug.h"
15 #include "event.h"
16 #include "compat.h"
17 #include "udp.h"
18
19 /* Send one packet, the first in queue.  */
20 static int
21 _send (struct rs_connection *conn, int fd)
22 {
23   ssize_t r = 0;
24   struct rs_packet *pkt = conn->out_queue;
25
26   assert (pkt->rpkt);
27   assert (pkt->rpkt->data);
28
29   /* Send.  */
30   r = compat_send (fd, pkt->rpkt->data, pkt->rpkt->data_len, 0);
31   if (r == -1)
32     {
33       int sockerr = evutil_socket_geterror (pkt->conn->fd);
34       if (sockerr != EAGAIN)
35         return rs_err_conn_push_fl (pkt->conn, RSE_SOCKERR, __FILE__, __LINE__,
36                                     "%d: send: %d (%s)", fd, sockerr,
37                                     evutil_socket_error_to_string (sockerr));
38     }
39
40   assert (r == pkt->rpkt->data_len);
41   /* Unlink the packet.  */
42   conn->out_queue = pkt->next;
43
44   /* If there are more packets in queue, add the write event again.  */
45   if (pkt->conn->out_queue)
46     {
47       r = event_add (pkt->conn->wev, NULL);
48       if (r < 0)
49         return rs_err_conn_push_fl (pkt->conn, RSE_EVENT, __FILE__, __LINE__,
50                                     "event_add: %s", evutil_gai_strerror (r));
51       rs_debug (("%s: re-adding the write event\n", __func__));
52     }
53
54   return RSE_OK;
55 }
56
57 /* Callback for conn->wev and conn->rev.  FIXME: Rename.
58
59    USER_DATA contains connection for EV_READ and a packet for
60    EV_WRITE.  This is because we don't have a connect/establish entry
61    point at the user level -- send implies connect so when we're
62    connected we need the packet to send.  */
63 static void
64 _evcb (evutil_socket_t fd, short what, void *user_data)
65 {
66   rs_debug (("%s: fd=%d what =", __func__, fd));
67   if (what & EV_TIMEOUT) rs_debug ((" TIMEOUT"));
68   if (what & EV_READ) rs_debug ((" READ"));
69   if (what & EV_WRITE) rs_debug ((" WRITE"));
70   rs_debug (("\n"));
71
72   if (what & EV_READ)
73     {
74       /* Read a single UDP packet and stick it in USER_DATA.  */
75       /* TODO: Verify that unsolicited packets are dropped.  */
76       struct rs_packet *pkt = (struct rs_packet *) user_data;
77       ssize_t r = 0;
78
79       assert (pkt);
80       assert (pkt->conn);
81
82       pkt->rpkt->data = rs_malloc (pkt->conn->ctx, 4096);
83       if (pkt->rpkt->data == NULL)
84         {
85           rs_err_conn_push_fl (pkt->conn, RSE_NOMEM, __FILE__, __LINE__, NULL);
86           return;
87         }
88       r = compat_recv (fd, pkt->rpkt->data, 4096, MSG_TRUNC);
89       if (r == -1)
90         {
91           int sockerr = evutil_socket_geterror (pkt->conn->fd);
92           if (sockerr == EAGAIN)
93             {
94               /* FIXME: Really shouldn't happen since we've been told
95                  that fd is readable!  */
96               rs_debug (("%s: EAGAIN reading UDP packet -- wot?"));
97               return;
98             }
99
100           /* Hard error.  */
101           rs_err_conn_push_fl (pkt->conn, RSE_SOCKERR, __FILE__, __LINE__,
102                                "%d: recv: %d (%s)", fd, sockerr,
103                                evutil_socket_error_to_string (sockerr));
104           event_del (pkt->conn->tev);
105           return;
106         }
107       event_del (pkt->conn->tev);
108       if (r < 20 || r > 4096)   /* Short or long packet.  */
109         {
110           rs_err_conn_push (pkt->conn, RSE_INVALID_PKT,
111                             "invalid packet length: %d",
112                             pkt->rpkt->data_len);
113           return;
114         }
115       pkt->rpkt->data_len = (pkt->rpkt->data[2] << 8) + pkt->rpkt->data[3];
116       if (!rad_packet_ok (pkt->rpkt, 0))
117         {
118           rs_err_conn_push_fl (pkt->conn, RSE_FR, __FILE__, __LINE__,
119                                "invalid packet: %s", fr_strerror ());
120           return;
121         }
122       /* Hand over message to user.  This changes ownership of pkt.
123          Don't touch it afterwards -- it might have been freed.  */
124       if (pkt->conn->callbacks.received_cb)
125         pkt->conn->callbacks.received_cb (pkt, pkt->conn->user_data);
126     }
127   else if (what & EV_WRITE)
128     {
129       struct rs_packet *pkt = (struct rs_packet *) user_data;
130       assert (pkt);
131       assert (pkt->conn);
132
133       if (!pkt->conn->is_connected)
134         event_on_connect (pkt->conn, pkt);
135
136       if (pkt->conn->out_queue)
137         if (_send (pkt->conn, fd) == RSE_OK)
138           if (pkt->conn->callbacks.sent_cb)
139             pkt->conn->callbacks.sent_cb (pkt->conn->user_data);
140     }
141
142 #if defined (DEBUG)
143   if (what & EV_TIMEOUT)
144     rs_debug (("%s: timeout on UDP event, shouldn't happen\n", __func__));
145 #endif
146 }
147
148 int
149 udp_init (struct rs_connection *conn, struct rs_packet *pkt)
150 {
151   assert (!conn->bev);
152
153   conn->rev = event_new (conn->evb, conn->fd, EV_READ|EV_PERSIST, _evcb, NULL);
154   conn->wev = event_new (conn->evb, conn->fd, EV_WRITE, _evcb, NULL);
155   if (!conn->rev || !conn->wev)
156     {
157       if (conn->rev)
158         {
159           event_free (conn->rev);
160           conn->rev = NULL;
161         }
162       /* ENOMEM _or_ EINVAL but EINVAL only if we use EV_SIGNAL, at
163          least for now (libevent-2.0.5).  */
164       return rs_err_conn_push_fl (conn, RSE_NOMEM, __FILE__, __LINE__, NULL);
165     }
166   return RSE_OK;
167 }
168
169 int
170 udp_init_retransmit_timer (struct rs_connection *conn)
171 {
172   assert (conn);
173
174   if (conn->tev)
175     event_free (conn->tev);
176   conn->tev = evtimer_new (conn->evb, event_retransmit_timeout_cb, conn);
177   if (!conn->tev)
178     return rs_err_conn_push_fl (conn, RSE_EVENT, __FILE__, __LINE__,
179                                 "evtimer_new");
180
181   return RSE_OK;
182 }