Simplify logic to open the detail file. Fixes CID #720458
[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 RCSID("$Id$")
25
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/detail.h>
29 #include <freeradius-devel/process.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->packet->timestamp.tv_sec;
114         rtt *= USEC;
115         rtt += now.tv_usec;
116         rtt -= request->packet->timestamp.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         data->counter++;
161         radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
162
163         return 0;
164 }
165
166
167 /*
168  *      Open the detail file, if we can.
169  *
170  *      FIXME: create it, if it's not already there, so that the main
171  *      server select() will wake us up if there's anything to read.
172  */
173 static int detail_open(rad_listen_t *this)
174 {
175         struct stat st;
176         listen_detail_t *data = this->data;
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 #ifndef HAVE_GLOB_H
198                 return 0;
199 #else
200                 unsigned int i;
201                 int found;
202                 time_t chtime;
203                 char const *filename;
204                 glob_t files;
205
206                 DEBUG2("Polling for detail file %s", data->filename);
207
208                 memset(&files, 0, sizeof(files));
209                 if (glob(data->filename, 0, NULL, &files) != 0) {
210                         globfree(&files);
211                         return 0;
212                 }
213
214                 /*
215                  *      Loop over the glob'd files, looking for the
216                  *      oldest one.
217                  */
218                 chtime = 0;
219                 found = -1;
220                 for (i = 0; i < files.gl_pathc; i++) {
221                         if (stat(files.gl_pathv[i], &st) < 0) continue;
222
223                         if ((i == 0) ||
224                             (st.st_ctime < chtime)) {
225                                         chtime = st.st_ctime;
226                                         found = i;
227                         }
228                 }
229
230                 if (found < 0) {
231                         globfree(&files);
232                         return 0;
233                 }
234
235                 /*
236                  *      Rename detail to detail.work
237                  */
238                 filename = files.gl_pathv[found];
239
240                 DEBUG("Detail - Renaming %s -> %s", filename, data->filename_work);
241                 if (rename(filename, data->filename_work) < 0) {
242                         ERROR("Detail - Failed renaming %s to %s: %s",
243                               filename, data->filename_work, fr_syserror(errno));
244                         globfree(&files);
245                         return 0;
246                 }
247
248                 /*
249                  *      And try to open the filename.
250                  */
251                 this->fd = open(data->filename_work, O_RDWR);
252                 if (this->fd < 0) return 0;
253 #endif
254         } /* else detail.work existed, and we opened it */
255
256         rad_assert(data->vps == NULL);
257         rad_assert(data->fp == NULL);
258
259         data->state = STATE_UNLOCKED;
260
261         data->client_ip.af = AF_UNSPEC;
262         data->timestamp = 0;
263         data->offset = 0;
264         data->packets = 0;
265         data->tries = 0;
266
267         return 1;
268 }
269
270
271 /*
272  *      FIXME: add a configuration "exit when done" so that the detail
273  *      file reader can be used as a one-off tool to update stuff.
274  *
275  *      The time sequence for reading from the detail file is:
276  *
277  *      t_0             signalled that the server is idle, and we
278  *                      can read from the detail file.
279  *
280  *      t_rtt           the packet has been processed successfully,
281  *                      wait for t_delay to enforce load factor.
282  *
283  *      t_rtt + t_delay wait for signal that the server is idle.
284  *
285  */
286 int detail_recv(rad_listen_t *listener)
287 {
288         char            key[256], op[8], value[1024];
289         vp_cursor_t     cursor;
290         VALUE_PAIR      *vp;
291         RADIUS_PACKET   *packet;
292         char            buffer[2048];
293         listen_detail_t *data = listener->data;
294
295         /*
296          *      We may be in the main thread.  It needs to update the
297          *      timers before we try to read from the file again.
298          */
299         if (data->signal) return 0;
300
301         switch (data->state) {
302                 case STATE_UNOPENED:
303         open_file:
304                         rad_assert(listener->fd < 0);
305
306                         if (!detail_open(listener)) return 0;
307
308                         rad_assert(data->state == STATE_UNLOCKED);
309                         rad_assert(listener->fd >= 0);
310
311                         /* FALL-THROUGH */
312
313                         /*
314                          *      Try to lock fd.  If we can't, return.
315                          *      If we can, continue.  This means that
316                          *      the server doesn't block while waiting
317                          *      for the lock to open...
318                          */
319                 case STATE_UNLOCKED:
320                         /*
321                          *      Note that we do NOT block waiting for
322                          *      the lock.  We've re-named the file
323                          *      above, so we've already guaranteed
324                          *      that any *new* detail writer will not
325                          *      be opening this file.  The only
326                          *      purpose of the lock is to catch a race
327                          *      condition where the execution
328                          *      "ping-pongs" between radiusd &
329                          *      radrelay.
330                          */
331                         if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
332                                 /*
333                                  *      Close the FD.  The main loop
334                                  *      will wake up in a second and
335                                  *      try again.
336                                  */
337                                 close(listener->fd);
338                                 listener->fd = -1;
339                                 data->state = STATE_UNOPENED;
340                                 return 0;
341                         }
342
343                         data->fp = fdopen(listener->fd, "r");
344                         if (!data->fp) {
345                                 ERROR("FATAL: Failed to re-open detail file %s: %s",
346                                        data->filename, fr_syserror(errno));
347                                 fr_exit(1);
348                         }
349
350                         /*
351                          *      Look for the header
352                          */
353                         data->state = STATE_HEADER;
354                         data->delay_time = USEC;
355                         data->vps = NULL;
356
357                         /* FALL-THROUGH */
358
359                 case STATE_HEADER:
360                 do_header:
361                         data->tries = 0;
362                         if (!data->fp) {
363                                 data->state = STATE_UNOPENED;
364                                 goto open_file;
365                         }
366
367                         {
368                                 struct stat buf;
369
370                                 if (fstat(listener->fd, &buf) < 0) {
371                                         ERROR("Failed to stat "
372                                                "detail file %s: %s",
373                                                 data->filename,
374                                                 fr_syserror(errno));
375
376                                         goto cleanup;
377                                 }
378                                 if (((off_t) ftell(data->fp)) == buf.st_size) {
379                                         goto cleanup;
380                                 }
381                         }
382
383                         /*
384                          *      End of file.  Delete it, and re-set
385                          *      everything.
386                          */
387                         if (feof(data->fp)) {
388                         cleanup:
389                                 DEBUG("Detail - unlinking %s",
390                                       data->filename_work);
391                                 unlink(data->filename_work);
392                                 if (data->fp) fclose(data->fp);
393                                 data->fp = NULL;
394                                 listener->fd = -1;
395                                 data->state = STATE_UNOPENED;
396                                 rad_assert(data->vps == NULL);
397
398                                 if (data->one_shot) {
399                                         INFO("Finished reading \"one shot\" detail file - Exiting");
400                                         radius_signal_self(RADIUS_SIGNAL_SELF_EXIT);
401                                 }
402
403                                 return 0;
404                         }
405
406                         /*
407                          *      Else go read something.
408                          */
409                         break;
410
411                         /*
412                          *      Read more value-pair's, unless we're
413                          *      at EOF.  In that case, queue whatever
414                          *      we have.
415                          */
416                 case STATE_READING:
417                         if (data->fp && !feof(data->fp)) break;
418                         data->state = STATE_QUEUED;
419
420                         /* FALL-THROUGH */
421
422                 case STATE_QUEUED:
423                         goto alloc_packet;
424
425                         /*
426                          *      Periodically check what's going on.
427                          *      If the request is taking too long,
428                          *      retry it.
429                          */
430                 case STATE_RUNNING:
431                         if (time(NULL) < (data->running + data->retry_interval)) {
432                                 return 0;
433                         }
434
435                         DEBUG("No response to detail request.  Retrying");
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         fr_cursor_init(&cursor, &data->vps);
458
459         /*
460          *      Read a header, OR a value-pair.
461          */
462         while (fgets(buffer, sizeof(buffer), data->fp)) {
463                 data->offset = ftell(data->fp); /* for statistics */
464
465                 /*
466                  *      Badly formatted file: delete it.
467                  *
468                  *      FIXME: Maybe flag an error?
469                  */
470                 if (!strchr(buffer, '\n')) {
471                         pairfree(&data->vps);
472                         goto cleanup;
473                 }
474
475                 /*
476                  *      We're reading VP's, and got a blank line.
477                  *      Queue the packet.
478                  */
479                 if ((data->state == STATE_READING) &&
480                     (buffer[0] == '\n')) {
481                         data->state = STATE_QUEUED;
482                         break;
483                 }
484
485                 /*
486                  *      Look for date/time header, and read VP's if
487                  *      found.  If not, keep reading lines until we
488                  *      find one.
489                  */
490                 if (data->state == STATE_HEADER) {
491                         int y;
492
493                         if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
494                                 data->state = STATE_READING;
495                         }
496                         continue;
497                 }
498
499                 /*
500                  *      We have a full "attribute = value" line.
501                  *      If it doesn't look reasonable, skip it.
502                  *
503                  *      FIXME: print an error for badly formatted attributes?
504                  */
505                 if (sscanf(buffer, "%255s %7s %1023s", key, op, value) != 3) {
506                         WDEBUG2("Skipping badly formatted line %s",
507                                buffer);
508                         continue;
509                 }
510
511                 /*
512                  *      Should be =, :=, +=, ...
513                  */
514                 if (!strchr(op, '=')) continue;
515
516                 /*
517                  *      Skip non-protocol attributes.
518                  */
519                 if (!strcasecmp(key, "Request-Authenticator")) continue;
520
521                 /*
522                  *      Set the original client IP address, based on
523                  *      what's in the detail file.
524                  *
525                  *      Hmm... we don't set the server IP address.
526                  *      or port.  Oh well.
527                  */
528                 if (!strcasecmp(key, "Client-IP-Address")) {
529                         data->client_ip.af = AF_INET;
530                         if (ip_hton(value, AF_INET, &data->client_ip) < 0) {
531                                 ERROR("Failed parsing Client-IP-Address");
532
533                                 pairfree(&data->vps);
534                                 goto cleanup;
535                         }
536                         continue;
537                 }
538
539                 /*
540                  *      The original time at which we received the
541                  *      packet.  We need this to properly calculate
542                  *      Acct-Delay-Time.
543                  */
544                 if (!strcasecmp(key, "Timestamp")) {
545                         data->timestamp = atoi(value);
546
547                         vp = paircreate(data, PW_PACKET_ORIGINAL_TIMESTAMP, 0);
548                         if (vp) {
549                                 vp->vp_date = (uint32_t) data->timestamp;
550                                 vp->type = VT_DATA;
551                                 fr_cursor_insert(&cursor, vp);
552                         }
553                         continue;
554                 }
555
556                 /*
557                  *      Read one VP.
558                  *
559                  *      FIXME: do we want to check for non-protocol
560                  *      attributes like radsqlrelay does?
561                  */
562                 vp = NULL;
563                 if ((userparse(data, buffer, &vp) > 0) &&
564                     (vp != NULL)) {
565                         fr_cursor_insert(&cursor, vp);
566                 }
567         }
568
569         /*
570          *      Some kind of error.
571          *
572          *      FIXME: Leave the file in-place, and warn the
573          *      administrator?
574          */
575         if (ferror(data->fp)) goto cleanup;
576
577         data->tries = 0;
578         data->packets++;
579
580         /*
581          *      Process the packet.
582          */
583  alloc_packet:
584         data->tries++;
585
586         /*
587          *      The writer doesn't check that the record was
588          *      completely written.  If the disk is full, this can
589          *      result in a truncated record.  When that happens,
590          *      treat it as EOF.
591          */
592         if (data->state != STATE_QUEUED) {
593                 ERROR("Truncated record: treating it as EOF for detail file %s", data->filename_work);
594                 goto cleanup;
595         }
596
597         /*
598          *      We're done reading the file, but we didn't read
599          *      anything.  Clean up, and don't return anything.
600          */
601         if (!data->vps) {
602                 data->state = STATE_HEADER;
603                 if (!data->fp || feof(data->fp)) goto cleanup;
604                 return 0;
605         }
606
607         /*
608          *      Allocate the packet.  If we fail, it's a serious
609          *      problem.
610          */
611         packet = rad_alloc(NULL, 1);
612         if (!packet) {
613                 ERROR("FATAL: Failed allocating memory for detail");
614                 fr_exit(1);
615                 _exit(1);
616         }
617
618         memset(packet, 0, sizeof(*packet));
619         packet->sockfd = -1;
620         packet->src_ipaddr.af = AF_INET;
621         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
622         packet->code = PW_CODE_ACCOUNTING_REQUEST;
623         gettimeofday(&packet->timestamp, NULL);
624
625         /*
626          *      Remember where it came from, so that we don't
627          *      proxy it to the place it came from...
628          */
629         if (data->client_ip.af != AF_UNSPEC) {
630                 packet->src_ipaddr = data->client_ip;
631         }
632
633         vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0, TAG_ANY);
634         if (vp) {
635                 packet->src_ipaddr.af = AF_INET;
636                 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
637         } else {
638                 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0, TAG_ANY);
639                 if (vp) {
640                         packet->src_ipaddr.af = AF_INET6;
641                         memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
642                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
643                 }
644         }
645
646         vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
647         if (vp) {
648                 packet->dst_ipaddr.af = AF_INET;
649                 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
650         } else {
651                 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
652                 if (vp) {
653                         packet->dst_ipaddr.af = AF_INET6;
654                         memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
655                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
656                 }
657         }
658
659         /*
660          *      Generate packet ID, ports, IP via a counter.
661          */
662         packet->id = data->counter & 0xff;
663         packet->src_port = 1024 + ((data->counter >> 8) & 0xff);
664         packet->dst_port = 1024 + ((data->counter >> 16) & 0xff);
665
666         packet->dst_ipaddr.af = AF_INET;
667         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | ((data->counter >> 24) & 0xff));
668
669         /*
670          *      If everything's OK, this is a waste of memory.
671          *      Otherwise, it lets us re-send the original packet
672          *      contents, unmolested.
673          */
674         packet->vps = paircopy(packet, data->vps);
675
676         /*
677          *      Prefer the Event-Timestamp in the packet, if it
678          *      exists.  That is when the event occurred, whereas the
679          *      "Timestamp" field is when we wrote the packet to the
680          *      detail file, which could have been much later.
681          */
682         vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP, 0, TAG_ANY);
683         if (vp) {
684                 data->timestamp = vp->vp_integer;
685         }
686
687         /*
688          *      Look for Acct-Delay-Time, and update
689          *      based on Acct-Delay-Time += (time(NULL) - timestamp)
690          */
691         vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
692         if (!vp) {
693                 vp = paircreate(packet, PW_ACCT_DELAY_TIME, 0);
694                 rad_assert(vp != NULL);
695                 pairadd(&packet->vps, vp);
696         }
697         if (data->timestamp != 0) {
698                 vp->vp_integer += time(NULL) - data->timestamp;
699         }
700
701         /*
702          *      Set the transmission count.
703          */
704         vp = pairfind(packet->vps, PW_PACKET_TRANSMIT_COUNTER, 0, TAG_ANY);
705         if (!vp) {
706                 vp = paircreate(packet, PW_PACKET_TRANSMIT_COUNTER, 0);
707                 rad_assert(vp != NULL);
708                 pairadd(&packet->vps, vp);
709         }
710         vp->vp_integer = data->tries;
711
712         if (debug_flag) {
713                 fr_printf_log("detail_recv: Read packet from %s\n", data->filename_work);
714                 for (vp = fr_cursor_init(&cursor, &packet->vps);
715                      vp;
716                      vp = fr_cursor_next(&cursor)) {
717                         debug_pair(vp);
718                 }
719         }
720
721         /*
722          *      Don't bother doing limit checks, etc.
723          */
724         if (!request_receive(listener, packet, &data->detail_client,
725                              rad_accounting)) {
726                 rad_free(&packet);
727                 data->state = STATE_NO_REPLY;   /* try again later */
728                 return 0;
729         }
730
731         data->state = STATE_RUNNING;
732         data->running = packet->timestamp.tv_sec;
733
734         return 1;
735 }
736
737
738 /*
739  *      Free detail-specific stuff.
740  */
741 void detail_free(rad_listen_t *this)
742 {
743         listen_detail_t *data = this->data;
744
745         talloc_free(data->filename);
746         data->filename = NULL;
747         pairfree(&data->vps);
748
749         if (data->fp != NULL) {
750                 fclose(data->fp);
751                 data->fp = NULL;
752         }
753 }
754
755
756 int detail_print(rad_listen_t const *this, char *buffer, size_t bufsize)
757 {
758         if (!this->server) {
759                 return snprintf(buffer, bufsize, "%s",
760                                 ((listen_detail_t *)(this->data))->filename);
761         }
762
763         return snprintf(buffer, bufsize, "detail file %s as server %s",
764                         ((listen_detail_t *)(this->data))->filename,
765                         this->server);
766 }
767
768 /*
769  *      Overloaded to return delay times.
770  */
771 int detail_encode(rad_listen_t *this, UNUSED REQUEST *request)
772 {
773         listen_detail_t *data = this->data;
774
775         /*
776          *      We haven't sent a packet... delay things a bit.
777          */
778         if (!data->signal) {
779                 int delay = (data->poll_interval - 1) * USEC;
780
781                 /*
782                  *      Add +/- 0.25s of jitter
783                  */
784                 delay += (USEC * 3) / 4;
785                 delay += fr_rand() % (USEC / 2);
786
787                 DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
788                        data->filename,
789                        fr_int2str(state_names, data->state, "?"), data->signal,
790                        (delay / USEC), delay % USEC);
791
792                 return delay;
793         }
794
795         data->signal = 0;
796
797         DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
798                data->filename, fr_int2str(state_names, data->state, "?"),
799                data->signal,
800                data->delay_time / USEC,
801                data->delay_time % USEC);
802
803         return data->delay_time;
804 }
805
806
807 /*
808  *      Overloaded to return "should we fix delay times"
809  */
810 int detail_decode(rad_listen_t *this, UNUSED REQUEST *request)
811 {
812         listen_detail_t *data = this->data;
813
814         return data->signal;
815 }
816
817
818 static const CONF_PARSER detail_config[] = {
819         { "detail",   PW_TYPE_FILE_OUTPUT | PW_TYPE_DEPRECATED,
820           offsetof(listen_detail_t, filename), NULL,  NULL },
821         { "filename",   PW_TYPE_FILE_OUTPUT | PW_TYPE_REQUIRED,
822           offsetof(listen_detail_t, filename), NULL,  NULL },
823         { "load_factor",   PW_TYPE_INTEGER,
824           offsetof(listen_detail_t, load_factor), NULL, STRINGIFY(10)},
825         { "poll_interval",   PW_TYPE_INTEGER,
826           offsetof(listen_detail_t, poll_interval), NULL, STRINGIFY(1)},
827         { "retry_interval",   PW_TYPE_INTEGER,
828           offsetof(listen_detail_t, retry_interval), NULL, STRINGIFY(30)},
829         { "one_shot",   PW_TYPE_BOOLEAN,
830           offsetof(listen_detail_t, one_shot), NULL, NULL},
831         { "max_outstanding",   PW_TYPE_INTEGER,
832           offsetof(listen_detail_t, load_factor), NULL, NULL},
833
834         { NULL, -1, 0, NULL, NULL }             /* end the list */
835 };
836
837 extern bool check_config;
838
839 /*
840  *      Parse a detail section.
841  */
842 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
843 {
844         int             rcode;
845         listen_detail_t *data;
846         RADCLIENT       *client;
847         char buffer[2048];
848
849         if (check_config) return 0;
850
851         data = this->data;
852
853         rcode = cf_section_parse(cs, data, detail_config);
854         if (rcode < 0) {
855                 cf_log_err_cs(cs, "Failed parsing listen section");
856                 return -1;
857         }
858
859         /*
860          *      We don't do duplicate detection for "detail" sockets.
861          */
862         this->nodup = true;
863         this->synchronous = false;
864
865         if (!data->filename) {
866                 cf_log_err_cs(cs, "No detail file specified in listen section");
867                 return -1;
868         }
869
870         if ((data->load_factor < 1) || (data->load_factor > 100)) {
871                 cf_log_err_cs(cs, "Load factor must be between 1 and 100");
872                 return -1;
873         }
874
875         if ((data->poll_interval < 1) || (data->poll_interval > 20)) {
876                 cf_log_err_cs(cs, "poll_interval must be between 1 and 20");
877                 return -1;
878         }
879
880         if (data->max_outstanding == 0) data->max_outstanding = 1;
881
882         /*
883          *      If the filename is a glob, use "detail.work" as the
884          *      work file name.
885          */
886         if ((strchr(data->filename, '*') != NULL) ||
887             (strchr(data->filename, '[') != NULL)) {
888                 char *p;
889
890 #ifndef HAVE_GLOB_H
891                 WARN("Detail file \"%s\" appears to use file globbing, but it is not supported on this system.",
892                      data->filename);
893 #endif
894                 strlcpy(buffer, data->filename, sizeof(buffer));
895                 p = strrchr(buffer, FR_DIR_SEP);
896                 if (p) {
897                         p[1] = '\0';
898                 } else {
899                         buffer[0] = '\0';
900                 }
901                 strlcat(buffer, "detail.work",
902                         sizeof(buffer) - strlen(buffer));
903
904         } else {
905                 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
906         }
907
908         free(data->filename_work);
909         data->filename_work = strdup(buffer); /* FIXME: leaked */
910
911         data->vps = NULL;
912         data->fp = NULL;
913         data->state = STATE_UNOPENED;
914         data->delay_time = data->poll_interval * USEC;
915         data->signal = 1;
916
917         /*
918          *      Initialize the fake client.
919          */
920         client = &data->detail_client;
921         memset(client, 0, sizeof(*client));
922         client->ipaddr.af = AF_INET;
923         client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
924         client->prefix = 0;
925         client->longname = client->shortname = data->filename;
926         client->secret = client->shortname;
927         client->nas_type = strdup("none");
928
929         return 0;
930 }
931 #endif