Add formal argument 'secret' to two public functions.
[radsecproxy.git] / lib / request.c
1 /* Copyright 2010, 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 <stdint.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <sys/time.h>
12 #include <event2/event.h>
13 #include <radsec/radsec.h>
14 #include <radsec/radsec-impl.h>
15 #include <radsec/request.h>
16 #include <radsec/request-impl.h>
17 #include <radius/client.h>
18 #include "debug.h"
19 #include "conn.h"
20 #include "tcp.h"
21 #include "udp.h"
22
23 /* RFC 5080 2.2.1.  Retransmission Behavior.  */
24 #define IRT 2
25 #define MRC 5
26 #define MRT 16
27 #define MRD 30
28 #define RAND 100                /* Rand factor, milliseconds. */
29
30 int
31 rs_request_create (struct rs_connection *conn, struct rs_request **req_out)
32 {
33   struct rs_request *req = rs_malloc (conn->ctx, sizeof(*req));
34   assert (req_out);
35   if (!req)
36     return rs_err_conn_push_fl (conn, RSE_NOMEM, __FILE__, __LINE__, NULL);
37   memset (req, 0, sizeof(*req));
38   req->conn = conn;
39   *req_out = req;
40   return RSE_OK;
41 }
42
43 void
44 rs_request_add_reqpkt (struct rs_request *req, struct rs_packet *req_msg)
45 {
46   assert (req);
47   req->req_msg = req_msg;
48 }
49
50 int
51 rs_request_create_authn (struct rs_connection *conn,
52                          struct rs_request **req_out,
53                          const char *user_name,
54                          const char *user_pw,
55                          const char *secret)
56 {
57   struct rs_request *req = NULL;
58   assert (req_out);
59
60   if (rs_request_create (conn, &req))
61     return -1;
62
63   if (rs_packet_create_authn_request (conn, &req->req_msg, user_name, user_pw, secret))
64     return -1;
65
66   if (req_out)
67     *req_out = req;
68   return RSE_OK;
69 }
70
71 void
72 rs_request_destroy (struct rs_request *request)
73 {
74   assert (request);
75   assert (request->conn);
76   assert (request->conn->ctx);
77
78   if (request->req_msg)
79     rs_packet_destroy (request->req_msg);
80   rs_free (request->conn->ctx, request);
81 }
82
83 static void
84 _rand_rt (struct timeval *res, uint32_t rtprev, uint32_t factor)
85 {
86   uint32_t ms = rtprev * (nr_rand () % factor);
87   res->tv_sec = rtprev + ms / 1000;
88   res->tv_usec = (ms % 1000) * 1000;
89 }
90
91 int
92 rs_request_send (struct rs_request *request, struct rs_packet **resp_msg)
93 {
94   int r = 0;
95   struct rs_connection *conn = NULL;
96   int count = 0;
97   struct timeval rt = {0,0};
98   struct timeval end = {0,0};
99   struct timeval now = {0,0};
100   struct timeval tmp_tv = {0,0};
101   const struct timeval mrt_tv = {MRT,0};
102
103   if (!request || !request->conn || !request->req_msg || !resp_msg)
104     return rs_err_conn_push_fl (conn, RSE_INVAL, __FILE__, __LINE__, NULL);
105   conn = request->conn;
106   assert (!conn_user_dispatch_p (conn)); /* This function is high level.  */
107
108   gettimeofday (&end, NULL);
109   end.tv_sec += MRD;
110   _rand_rt (&rt, IRT, RAND);
111   while (1)
112     {
113       rs_conn_set_timeout (conn, &rt);
114
115       r = rs_packet_send (request->req_msg, NULL);
116       if (r == RSE_OK)
117         {
118           r = rs_conn_receive_packet (request->conn,
119                                       request->req_msg,
120                                       resp_msg);
121           if (r == RSE_OK)
122             break;              /* Success.  */
123
124           if (r != RSE_TIMEOUT_CONN && r != RSE_TIMEOUT_IO)
125             break;              /* Error.  */
126         }
127       else if (r != RSE_TIMEOUT_CONN && r != RSE_TIMEOUT_IO)
128         break;                  /* Error.  */
129
130       gettimeofday (&now, NULL);
131       if (++count > MRC || timercmp (&now, &end, >))
132         {
133           r = RSE_TIMEOUT;
134           break;                /* Timeout.  */
135         }
136
137       /* rt = 2 * rt + rand_rt (rt, RAND); */
138       timeradd (&rt, &rt, &rt);
139       _rand_rt (&tmp_tv, IRT, RAND);
140       timeradd (&rt, &tmp_tv, &rt);
141       if (timercmp (&rt, &mrt_tv, >))
142         _rand_rt (&rt, MRT, RAND);
143     }
144
145   timerclear (&rt);
146   rs_conn_set_timeout (conn, &rt);
147
148   rs_debug (("%s: returning %d\n", __func__, r));
149   return r;
150 }
151
152 struct rs_packet *
153 rs_request_get_reqmsg (const struct rs_request *request)
154 {
155   assert (request);
156   return request->req_msg;
157 }