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