Retry if there was no response to the packet.
[freeradius.git] / src / main / detail.c
1 /*
2  * detail.c     Process the detail file
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2007  The FreeRADIUS server project
21  * Copyright 2007  Alan DeKok <aland@deployingradius.com>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/detail.h>
30 #include <freeradius-devel/rad_assert.h>
31
32 #ifdef HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35
36 #ifdef HAVE_GLOB_H
37 #include <glob.h>
38 #endif
39
40 #include <fcntl.h>
41
42 #ifdef WITH_DETAIL
43
44 #define USEC (1000000)
45
46 typedef enum detail_state_t {
47   STATE_UNOPENED = 0,
48   STATE_UNLOCKED,
49   STATE_HEADER,
50   STATE_READING,
51   STATE_QUEUED,
52   STATE_RUNNING,
53   STATE_NO_REPLY,
54   STATE_REPLIED
55 } detail_state_t;
56
57 static FR_NAME_NUMBER state_names[] = {
58         { "unopened", STATE_UNOPENED },
59         { "unlocked", STATE_UNLOCKED },
60         { "header", STATE_HEADER },
61         { "reading", STATE_READING },
62         { "queued", STATE_QUEUED },
63         { "running", STATE_RUNNING },
64         { "no-reply", STATE_NO_REPLY },
65         { "replied", STATE_REPLIED },
66
67         { NULL, 0 }
68 };
69
70 typedef struct listen_detail_t {
71         fr_event_t      *ev;    /* has to be first entry (ugh) */
72         int             delay_time;
73         char            *filename;
74         char            *filename_work;
75         VALUE_PAIR      *vps;
76         FILE            *fp;
77         detail_state_t  state;
78         time_t          timestamp;
79         time_t          running;
80         fr_ipaddr_t     client_ip;
81         int             load_factor; /* 1..100 */
82         int             signal;
83         int             poll_interval;
84         int             retry_interval;
85
86         int             has_rtt;
87         int             srtt;
88         int             rttvar;
89         struct timeval  last_packet;
90         RADCLIENT       detail_client;
91 } listen_detail_t;
92
93
94 /*
95  *      If we're limiting outstanding packets, then mark the response
96  *      as being sent.
97  */
98 int detail_send(rad_listen_t *listener, REQUEST *request)
99 {
100         int rtt;
101         struct timeval now;
102         listen_detail_t *data = listener->data;
103
104         rad_assert(request->listener == listener);
105         rad_assert(listener->send == detail_send);
106
107         /*
108          *      This request timed out.  Remember that, and tell the
109          *      caller it's OK to read more "detail" file stuff.
110          */
111         if (request->reply->code == 0) {
112                 data->delay_time = data->retry_interval * USEC;
113                 data->signal = 1;
114                 data->state = STATE_NO_REPLY;
115
116                 RDEBUG("Detail - No response configured for request %d.  Will retry in %d seconds",
117                        request->number, data->retry_interval);
118
119                 radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
120                 return 0;
121         }
122
123         /*
124          *      We call gettimeofday a lot.  But it should be OK,
125          *      because there's nothing else to do.
126          */
127         gettimeofday(&now, NULL);
128
129         /*
130          *      If we haven't sent a packet in the last second, reset
131          *      the RTT.
132          */
133         now.tv_sec -= 1;
134         if (timercmp(&data->last_packet, &now, <)) {
135                 data->has_rtt = FALSE;
136         }
137         now.tv_sec += 1;
138
139         /*
140          *      Only one detail packet may be outstanding at a time,
141          *      so it's safe to update some entries in the detail
142          *      structure.
143          *
144          *      We keep smoothed round trip time (SRTT), but not round
145          *      trip timeout (RTO).  We use SRTT to calculate a rough
146          *      load factor.
147          */
148         rtt = now.tv_sec - request->received.tv_sec;
149         rtt *= USEC;
150         rtt += now.tv_usec;
151         rtt -= request->received.tv_usec;
152
153         /*
154          *      If we're proxying, the RTT is our processing time,
155          *      plus the network delay there and back, plus the time
156          *      on the other end to process the packet.  Ideally, we
157          *      should remove the network delays from the RTT, but we
158          *      don't know what they are.
159          *
160          *      So, to be safe, we over-estimate the total cost of
161          *      processing the packet.
162          */
163         if (!data->has_rtt) {
164                 data->has_rtt = TRUE;
165                 data->srtt = rtt;
166                 data->rttvar = rtt / 2;
167
168         } else {
169                 data->rttvar -= data->rttvar >> 2;
170                 data->rttvar += (data->srtt - rtt);
171                 data->srtt -= data->srtt >> 3;
172                 data->srtt += rtt >> 3;
173         }
174
175         /*
176          *      Calculate the time we wait before sending the next
177          *      packet.
178          *
179          *      rtt / (rtt + delay) = load_factor / 100
180          */
181         data->delay_time = (data->srtt * (100 - data->load_factor)) / (data->load_factor);
182
183         /*
184          *      Cap delay at 4 packets/s.  If the end system can't
185          *      handle this, then it's very broken.
186          */
187         if (data->delay_time > (USEC / 4)) data->delay_time= USEC / 4;
188         
189         RDEBUG3("Received response for request %d.  Will read the next packet in %d seconds",
190                 request->number, data->delay_time / USEC);
191         
192         data->last_packet = now;
193         data->signal = 1;
194         data->state = STATE_REPLIED;
195         radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
196
197         return 0;
198 }
199
200
201 /*
202  *      Open the detail file, if we can.
203  *
204  *      FIXME: create it, if it's not already there, so that the main
205  *      server select() will wake us up if there's anything to read.
206  */
207 static int detail_open(rad_listen_t *this)
208 {
209         struct stat st;
210         listen_detail_t *data = this->data;
211         char *filename = data->filename;
212
213         rad_assert(data->state == STATE_UNOPENED);
214         data->delay_time = USEC;
215
216         /*
217          *      Open detail.work first, so we don't lose
218          *      accounting packets.  It's probably better to
219          *      duplicate them than to lose them.
220          *
221          *      Note that we're not writing to the file, but
222          *      we've got to open it for writing in order to
223          *      establish the lock, to prevent rlm_detail from
224          *      writing to it.
225          *
226          *      This also means that if we're doing globbing,
227          *      this file will be read && processed before the
228          *      file globbing is done.
229          */
230         this->fd = open(data->filename_work, O_RDWR);
231         if (this->fd < 0) {
232                 DEBUG2("Polling for detail file %s", filename);
233
234                 /*
235                  *      Try reading the detail file.  If it
236                  *      doesn't exist, we can't do anything.
237                  *
238                  *      Doing the stat will tell us if the file
239                  *      exists, even if we don't have permissions
240                  *      to read it.
241                  */
242                 if (stat(filename, &st) < 0) {
243 #ifdef HAVE_GLOB_H
244                         unsigned int i;
245                         int found;
246                         time_t chtime;
247                         glob_t files;
248
249                         memset(&files, 0, sizeof(files));
250                         if (glob(filename, 0, NULL, &files) != 0) {
251                                 return 0;
252                         }
253
254                         chtime = 0;
255                         found = -1;
256                         for (i = 0; i < files.gl_pathc; i++) {
257                                 if (stat(files.gl_pathv[i], &st) < 0) continue;
258
259                                 if ((i == 0) ||
260                                     (st.st_ctime < chtime)) {
261                                         chtime = st.st_ctime;
262                                         found = i;
263                                 }
264                         }
265
266                         if (found < 0) {
267                                 globfree(&files);
268                                 return 0;
269                         }
270
271                         filename = strdup(files.gl_pathv[found]);
272                         globfree(&files);
273 #else
274                         return 0;
275 #endif
276                 }
277
278                 /*
279                  *      Open it BEFORE we rename it, just to
280                  *      be safe...
281                  */
282                 this->fd = open(filename, O_RDWR);
283                 if (this->fd < 0) {
284                         radlog(L_ERR, "Detail - Failed to open %s: %s",
285                                filename, strerror(errno));
286                         if (filename != data->filename) free(filename);
287                         return 0;
288                 }
289
290                 /*
291                  *      Rename detail to detail.work
292                  */
293                 DEBUG("Detail - Renaming %s -> %s", filename, data->filename_work);
294                 if (rename(filename, data->filename_work) < 0) {
295                         if (filename != data->filename) free(filename);
296                         close(this->fd);
297                         this->fd = -1;
298                         return 0;
299                 }
300                 if (filename != data->filename) free(filename);
301         } /* else detail.work existed, and we opened it */
302
303         rad_assert(data->vps == NULL);
304         rad_assert(data->fp == NULL);
305
306         data->state = STATE_UNLOCKED;
307
308         data->client_ip.af = AF_UNSPEC;
309         data->timestamp = 0;
310
311         return 1;
312 }
313
314
315 /*
316  *      FIXME: add a configuration "exit when done" so that the detail
317  *      file reader can be used as a one-off tool to update stuff.
318  *
319  *      The time sequence for reading from the detail file is:
320  *
321  *      t_0             signalled that the server is idle, and we
322  *                      can read from the detail file.
323  *
324  *      t_rtt           the packet has been processed successfully,
325  *                      wait for t_delay to enforce load factor.
326  *                      
327  *      t_rtt + t_delay wait for signal that the server is idle.
328  *      
329  */
330 int detail_recv(rad_listen_t *listener,
331                 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
332 {
333         char            key[256], op[8], value[1024];
334         VALUE_PAIR      *vp, **tail;
335         RADIUS_PACKET   *packet;
336         char            buffer[2048];
337         listen_detail_t *data = listener->data;
338
339         /*
340          *      We may be in the main thread.  It needs to update the
341          *      timers before we try to read from the file again.
342          */
343         if (data->signal) return 0;
344
345         switch (data->state) {
346                 case STATE_UNOPENED:
347         open_file:
348                         rad_assert(listener->fd < 0);
349                         
350                         if (!detail_open(listener)) return 0;
351
352                         rad_assert(data->state == STATE_UNLOCKED);
353                         rad_assert(listener->fd >= 0);
354
355                         /* FALL-THROUGH */
356
357                         /*
358                          *      Try to lock fd.  If we can't, return.
359                          *      If we can, continue.  This means that
360                          *      the server doesn't block while waiting
361                          *      for the lock to open...
362                          */
363                 case STATE_UNLOCKED:
364                         /*
365                          *      Note that we do NOT block waiting for
366                          *      the lock.  We've re-named the file
367                          *      above, so we've already guaranteed
368                          *      that any *new* detail writer will not
369                          *      be opening this file.  The only
370                          *      purpose of the lock is to catch a race
371                          *      condition where the execution
372                          *      "ping-pongs" between radiusd &
373                          *      radrelay.
374                          */
375                         if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
376                                 /*
377                                  *      Close the FD.  The main loop
378                                  *      will wake up in a second and
379                                  *      try again.
380                                  */
381                                 close(listener->fd);
382                                 listener->fd = -1;
383                                 data->state = STATE_UNOPENED;
384                                 return 0;
385                         }
386
387                         data->fp = fdopen(listener->fd, "r");
388                         if (!data->fp) {
389                                 radlog(L_ERR, "FATAL: Failed to re-open detail file %s: %s",
390                                        data->filename, strerror(errno));
391                                 exit(1);
392                         }
393
394                         /*
395                          *      Look for the header
396                          */
397                         data->state = STATE_HEADER;
398                         data->delay_time = USEC;
399                         data->vps = NULL;
400
401                         /* FALL-THROUGH */
402
403                 case STATE_HEADER:
404                 do_header:
405                         if (!data->fp) {
406                                 data->state = STATE_UNOPENED;
407                                 goto open_file;
408                         }
409
410                         {
411                                 struct stat buf;
412                                 
413                                 fstat(listener->fd, &buf);
414                                 if (((off_t) ftell(data->fp)) == buf.st_size) {
415                                         goto cleanup;
416                                 }
417                         }
418
419                         /*
420                          *      End of file.  Delete it, and re-set
421                          *      everything.
422                          */
423                         if (feof(data->fp)) {
424                         cleanup:
425                                 DEBUG("Detail - unlinking %s",
426                                       data->filename_work);
427                                 unlink(data->filename_work);
428                                 if (data->fp) fclose(data->fp);
429                                 data->fp = NULL;
430                                 listener->fd = -1;
431                                 data->state = STATE_UNOPENED;
432                                 rad_assert(data->vps == NULL);
433                                 return 0;
434                         }
435
436                         /*
437                          *      Else go read something.
438                          */
439                         break;
440
441                         /*
442                          *      Read more value-pair's, unless we're
443                          *      at EOF.  In that case, queue whatever
444                          *      we have.
445                          */
446                 case STATE_READING:
447                         if (data->fp && !feof(data->fp)) break;
448                         data->state = STATE_QUEUED;
449
450                         /* FALL-THROUGH */
451
452                 case STATE_QUEUED:
453                         goto alloc_packet;
454
455                         /*
456                          *      Periodically check what's going on.
457                          *      If the request is taking too long,
458                          *      retry it.
459                          */
460                 case STATE_RUNNING:
461                         if (time(NULL) < (data->running + data->retry_interval)) {
462                                 return 0;
463                         }
464
465                         DEBUG("No response to detail request.  Retrying");
466                         data->state = STATE_NO_REPLY;
467                         /* FALL-THROUGH */
468
469                         /*
470                          *      If there's no reply, keep
471                          *      retransmitting the current packet
472                          *      forever.
473                          */
474                 case STATE_NO_REPLY:
475                         data->state = STATE_QUEUED;
476                         goto alloc_packet;
477                                 
478                         /*
479                          *      We have a reply.  Clean up the old
480                          *      request, and go read another one.
481                          */
482                 case STATE_REPLIED:
483                         pairfree(&data->vps);
484                         data->state = STATE_HEADER;
485                         goto do_header;
486         }
487         
488         tail = &data->vps;
489         while (*tail) tail = &(*tail)->next;
490
491         /*
492          *      Read a header, OR a value-pair.
493          */
494         while (fgets(buffer, sizeof(buffer), data->fp)) {
495                 /*
496                  *      Badly formatted file: delete it.
497                  *
498                  *      FIXME: Maybe flag an error?
499                  */
500                 if (!strchr(buffer, '\n')) {
501                         pairfree(&data->vps);
502                         goto cleanup;
503                 }
504
505                 /*
506                  *      We're reading VP's, and got a blank line.
507                  *      Queue the packet.
508                  */
509                 if ((data->state == STATE_READING) &&
510                     (buffer[0] == '\n')) {
511                         data->state = STATE_QUEUED;
512                         break;
513                 }
514
515                 /*
516                  *      Look for date/time header, and read VP's if
517                  *      found.  If not, keep reading lines until we
518                  *      find one.
519                  */
520                 if (data->state == STATE_HEADER) {
521                         int y;
522
523                         if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
524                                 data->state = STATE_READING;
525                         }
526                         continue;
527                 }
528
529                 /*
530                  *      We have a full "attribute = value" line.
531                  *      If it doesn't look reasonable, skip it.
532                  *
533                  *      FIXME: print an error for badly formatted attributes?
534                  */
535                 if (sscanf(buffer, "%255s %8s %1023s", key, op, value) != 3) {
536                         DEBUG2("WARNING: Skipping badly formatted line %s",
537                                buffer);
538                         continue;
539                 }
540
541                 /*
542                  *      Should be =, :=, +=, ...
543                  */
544                 if (!strchr(op, '=')) continue;
545
546                 /*
547                  *      Skip non-protocol attributes.
548                  */
549                 if (!strcasecmp(key, "Request-Authenticator")) continue;
550
551                 /*
552                  *      Set the original client IP address, based on
553                  *      what's in the detail file.
554                  *
555                  *      Hmm... we don't set the server IP address.
556                  *      or port.  Oh well.
557                  */
558                 if (!strcasecmp(key, "Client-IP-Address")) {
559                         data->client_ip.af = AF_INET;
560                         ip_hton(value, AF_INET, &data->client_ip);
561                         continue;
562                 }
563
564                 /*
565                  *      The original time at which we received the
566                  *      packet.  We need this to properly calculate
567                  *      Acct-Delay-Time.
568                  */
569                 if (!strcasecmp(key, "Timestamp")) {
570                         data->timestamp = atoi(value);
571
572                         vp = paircreate(PW_PACKET_ORIGINAL_TIMESTAMP,
573                                         PW_TYPE_DATE);
574                         if (vp) {
575                                 vp->vp_date = (uint32_t) data->timestamp;
576                                 *tail = vp;
577                                 tail = &(vp->next);
578                         }
579                         continue;
580                 }
581
582                 /*
583                  *      Read one VP.
584                  *
585                  *      FIXME: do we want to check for non-protocol
586                  *      attributes like radsqlrelay does?
587                  */
588                 vp = NULL;
589                 if ((userparse(buffer, &vp) > 0) &&
590                     (vp != NULL)) {
591                         *tail = vp;
592                         tail = &(vp->next);
593                 }
594         }
595
596         /*
597          *      Some kind of error.
598          *
599          *      FIXME: Leave the file in-place, and warn the
600          *      administrator?
601          */
602         if (ferror(data->fp)) goto cleanup;
603
604         /*
605          *      Process the packet.
606          */
607  alloc_packet:
608         rad_assert(data->state == STATE_QUEUED);
609
610         /*
611          *      We're done reading the file, but we didn't read
612          *      anything.  Clean up, and don't return anything.
613          */
614         if (!data->vps) {
615                 data->state = STATE_HEADER;
616                 if (feof(data->fp)) goto cleanup; 
617                 return 0;
618         }
619
620         /*
621          *      Allocate the packet.  If we fail, it's a serious
622          *      problem.
623          */
624         packet = rad_alloc(1);
625         if (!packet) {
626                 radlog(L_ERR, "FATAL: Failed allocating memory for detail");
627                 exit(1);
628         }
629
630         memset(packet, 0, sizeof(*packet));
631         packet->sockfd = -1;
632         packet->src_ipaddr.af = AF_INET;
633         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
634         packet->code = PW_ACCOUNTING_REQUEST;
635         packet->timestamp = time(NULL);
636
637         /*
638          *      Remember where it came from, so that we don't
639          *      proxy it to the place it came from...
640          */
641         if (data->client_ip.af != AF_UNSPEC) {
642                 packet->src_ipaddr = data->client_ip;
643         }
644
645         vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS);
646         if (vp) {
647                 packet->src_ipaddr.af = AF_INET;
648                 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
649         } else {
650                 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS);
651                 if (vp) {
652                         packet->src_ipaddr.af = AF_INET6;
653                         memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
654                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
655                 }
656         }
657
658         vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS);
659         if (vp) {
660                 packet->dst_ipaddr.af = AF_INET;
661                 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
662         } else {
663                 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS);
664                 if (vp) {
665                         packet->dst_ipaddr.af = AF_INET6;
666                         memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
667                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
668                 }
669         }
670
671         /*
672          *      We've got to give SOME value for Id & ports, so that
673          *      the packets can be added to the request queue.
674          *      However, we don't want to keep track of used/unused
675          *      id's and ports, as that's a lot of work.  This hack
676          *      ensures that (if we have real random numbers), that
677          *      there will be a collision on every 2^(16+15+15+24 - 1)
678          *      packets, on average.  That means we can read 2^37
679          *      packets before having a collision, which means it's
680          *      effectively impossible.
681          */
682         packet->id = fr_rand() & 0xffff;
683         packet->src_port = 1024 + (fr_rand() & 0x7fff);
684         packet->dst_port = 1024 + (fr_rand() & 0x7fff);
685
686         packet->dst_ipaddr.af = AF_INET;
687         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | (fr_rand() & 0xffffff));
688
689         /*
690          *      If everything's OK, this is a waste of memory.
691          *      Otherwise, it lets us re-send the original packet
692          *      contents, unmolested.
693          */
694         packet->vps = paircopy(data->vps);
695
696         /*
697          *      Look for Acct-Delay-Time, and update
698          *      based on Acct-Delay-Time += (time(NULL) - timestamp)
699          */
700         vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME);
701         if (!vp) {
702                 vp = paircreate(PW_ACCT_DELAY_TIME, PW_TYPE_INTEGER);
703                 rad_assert(vp != NULL);
704                 pairadd(&packet->vps, vp);
705         }
706         if (data->timestamp != 0) {
707                 vp->vp_integer += time(NULL) - data->timestamp;
708         }
709
710         *pfun = rad_accounting;
711
712         if (debug_flag) {
713                 fr_printf_log("detail_recv: Read packet from %s\n", data->filename_work);
714                 for (vp = packet->vps; vp; vp = vp->next) {
715                         debug_pair(vp);
716                 }
717         }
718
719         /*
720          *      FIXME: many of these checks may not be necessary when
721          *      reading from the detail file.
722          *
723          *      Try again later...
724          */
725         if (!received_request(listener, packet, prequest,
726                               &data->detail_client)) {
727                 rad_free(&packet);
728                 data->state = STATE_NO_REPLY;   /* try again later */
729                 return 0;
730         }
731
732         data->state = STATE_RUNNING;
733         data->running = packet->timestamp;
734
735         return 1;
736 }
737
738
739 /*
740  *      Free detail-specific stuff.
741  */
742 void detail_free(rad_listen_t *this)
743 {
744         listen_detail_t *data = this->data;
745
746         free(data->filename);
747         pairfree(&data->vps);
748
749         if (data->fp != NULL) fclose(data->fp);
750 }
751
752
753 int detail_print(rad_listen_t *this, char *buffer, size_t bufsize)
754 {
755         if (!this->server) {
756                 return snprintf(buffer, bufsize, "%s",
757                                 ((listen_detail_t *)(this->data))->filename);
758         }
759
760         return snprintf(buffer, bufsize, "detail file %s as server %s",
761                         ((listen_detail_t *)(this->data))->filename,
762                         this->server);
763 }
764
765 /*
766  *      Overloaded to return delay times.
767  */
768 int detail_encode(rad_listen_t *this, UNUSED REQUEST *request)
769 {
770         listen_detail_t *data = this->data;
771
772         /*
773          *      We haven't sent a packet... delay things a bit.
774          */
775         if (!data->signal) {
776                 int delay = (data->poll_interval - 1) * USEC;
777
778                 /*
779                  *      Add +/- 0.25s of jitter
780                  */
781                 delay += (USEC * 3) / 4;
782                 delay += fr_rand() % (USEC / 2);
783
784                 DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
785                        data->filename,
786                        fr_int2str(state_names, data->state, "?"), data->signal,
787                        (delay / USEC), delay % USEC);
788
789                 return delay;
790         }
791
792         data->signal = 0;
793         
794         DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
795                data->filename, fr_int2str(state_names, data->state, "?"),
796                data->signal,
797                data->delay_time / USEC,
798                data->delay_time % USEC);
799
800         return data->delay_time;
801 }
802
803
804 /*
805  *      Overloaded to return "should we fix delay times"
806  */
807 int detail_decode(rad_listen_t *this, UNUSED REQUEST *request)
808 {
809         listen_detail_t *data = this->data;
810
811         return data->signal;
812 }
813
814
815 static const CONF_PARSER detail_config[] = {
816         { "filename",   PW_TYPE_STRING_PTR,
817           offsetof(listen_detail_t, filename), NULL,  NULL },
818         { "load_factor",   PW_TYPE_INTEGER,
819           offsetof(listen_detail_t, load_factor), NULL, Stringify(10)},
820         { "poll_interval",   PW_TYPE_INTEGER,
821           offsetof(listen_detail_t, poll_interval), NULL, Stringify(1)},
822         { "retry_interval",   PW_TYPE_INTEGER,
823           offsetof(listen_detail_t, retry_interval), NULL, Stringify(30)},
824
825         { NULL, -1, 0, NULL, NULL }             /* end the list */
826 };
827
828
829 /*
830  *      Parse a detail section.
831  */
832 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
833 {
834         int             rcode;
835         listen_detail_t *data;
836         RADCLIENT       *client;
837         char buffer[2048];
838
839         if (!this->data) {
840                 this->data = rad_malloc(sizeof(*data));
841                 memset(this->data, 0, sizeof(*data));
842         }
843
844         data = this->data;
845
846         rcode = cf_section_parse(cs, data, detail_config);
847         if (rcode < 0) {
848                 cf_log_err(cf_sectiontoitem(cs), "Failed parsing listen section");
849                 return -1;
850         }
851
852         if (!data->filename) {
853                 cf_log_err(cf_sectiontoitem(cs), "No detail file specified in listen section");
854                 return -1;
855         }
856
857         if ((data->load_factor < 1) || (data->load_factor > 100)) {
858                 cf_log_err(cf_sectiontoitem(cs), "Load factor must be between 1 and 100");
859                 return -1;
860         }
861
862         if ((data->poll_interval < 1) || (data->poll_interval > 20)) {
863                 cf_log_err(cf_sectiontoitem(cs), "poll_interval must be between 1 and 20");
864                 return -1;
865         }
866
867         /*
868          *      If the filename is a glob, use "detail.work" as the
869          *      work file name.
870          */
871         if ((strchr(data->filename, '*') != NULL) ||
872             (strchr(data->filename, '[') != NULL)) {
873                 char *p;
874
875 #ifndef HAVE_GLOB_H
876                 radlog(L_INFO, "WARNING: Detail file \"%s\" appears to use file globbing, but it is not supported on this system.", data->filename);
877 #endif
878                 strlcpy(buffer, data->filename, sizeof(buffer));
879                 p = strrchr(buffer, FR_DIR_SEP);
880                 if (p) {
881                         p[1] = '\0';
882                 } else {
883                         buffer[0] = '\0';
884                 }
885                 strlcat(buffer, "detail.work",
886                         sizeof(buffer) - strlen(buffer));
887                         
888         } else {
889                 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
890         }
891
892         free(data->filename_work);
893         data->filename_work = strdup(buffer); /* FIXME: leaked */
894
895         data->vps = NULL;
896         data->fp = NULL;
897         data->state = STATE_UNOPENED;
898         data->delay_time = data->poll_interval * USEC;
899         data->signal = 1;
900
901         /*
902          *      Initialize the fake client.
903          */
904         client = &data->detail_client;
905         memset(client, 0, sizeof(*client));
906         client->ipaddr.af = AF_INET;
907         client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
908         client->prefix = 0;
909         client->longname = client->shortname = data->filename;
910         client->secret = client->shortname;
911         client->nastype = strdup("none");
912
913         return 0;
914 }
915 #endif