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