Do less work on check_config
[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 /*
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         char c = 0;
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 (%s): No response to request.  Will retry in %d seconds",
82                        data->name, data->retry_interval);
83         } else {
84                 int rtt;
85                 struct timeval now;
86                 /*
87                  *      We call gettimeofday a lot.  But it should be OK,
88                  *      because there's nothing else to do.
89                  */
90                 gettimeofday(&now, NULL);
91
92                 /*
93                  *      If we haven't sent a packet in the last second, reset
94                  *      the RTT.
95                  */
96                 now.tv_sec -= 1;
97                 if (timercmp(&data->last_packet, &now, <)) {
98                         data->has_rtt = false;
99                 }
100                 now.tv_sec += 1;
101
102                 /*
103                  *      Only one detail packet may be outstanding at a time,
104                  *      so it's safe to update some entries in the detail
105                  *      structure.
106                  *
107                  *      We keep smoothed round trip time (SRTT), but not round
108                  *      trip timeout (RTO).  We use SRTT to calculate a rough
109                  *      load factor.
110                  */
111                 rtt = now.tv_sec - request->packet->timestamp.tv_sec;
112                 rtt *= USEC;
113                 rtt += now.tv_usec;
114                 rtt -= request->packet->timestamp.tv_usec;
115
116                 /*
117                  *      If we're proxying, the RTT is our processing time,
118                  *      plus the network delay there and back, plus the time
119                  *      on the other end to process the packet.  Ideally, we
120                  *      should remove the network delays from the RTT, but we
121                  *      don't know what they are.
122                  *
123                  *      So, to be safe, we over-estimate the total cost of
124                  *      processing the packet.
125                  */
126                 if (!data->has_rtt) {
127                         data->has_rtt = true;
128                         data->srtt = rtt;
129                         data->rttvar = rtt / 2;
130
131                 } else {
132                         data->rttvar -= data->rttvar >> 2;
133                         data->rttvar += (data->srtt - rtt);
134                         data->srtt -= data->srtt >> 3;
135                         data->srtt += rtt >> 3;
136                 }
137
138                 /*
139                  *      Calculate the time we wait before sending the next
140                  *      packet.
141                  *
142                  *      rtt / (rtt + delay) = load_factor / 100
143                  */
144                 data->delay_time = (data->srtt * (100 - data->load_factor)) / (data->load_factor);
145
146                 /*
147                  *      Cap delay at no less than 4 packets/s.  If the
148                  *      end system can't handle this, then it's very
149                  *      broken.
150                  */
151                 if (data->delay_time > (USEC / 4)) data->delay_time= USEC / 4;
152
153                 RDEBUG3("detail (%s): Received response for request %d.  Will read the next packet in %d seconds",
154                         data->name, request->number, data->delay_time / USEC);
155
156                 data->last_packet = now;
157                 data->signal = 1;
158                 data->state = STATE_REPLIED;
159                 data->counter++;
160         }
161
162         if (write(data->child_pipe[1], &c, 1) < 0) {
163                 RERROR("detail (%s): Failed writing ack to reader thread: %s", data->name, fr_syserror(errno));
164         }
165
166         return 0;
167 }
168
169
170 /*
171  *      Open the detail file, if we can.
172  *
173  *      FIXME: create it, if it's not already there, so that the main
174  *      server select() will wake us up if there's anything to read.
175  */
176 static int detail_open(rad_listen_t *this)
177 {
178         struct stat st;
179         listen_detail_t *data = this->data;
180
181         rad_assert(data->state == STATE_UNOPENED);
182         data->delay_time = USEC;
183
184         /*
185          *      Open detail.work first, so we don't lose
186          *      accounting packets.  It's probably better to
187          *      duplicate them than to lose them.
188          *
189          *      Note that we're not writing to the file, but
190          *      we've got to open it for writing in order to
191          *      establish the lock, to prevent rlm_detail from
192          *      writing to it.
193          *
194          *      This also means that if we're doing globbing,
195          *      this file will be read && processed before the
196          *      file globbing is done.
197          */
198         data->fp = NULL;
199         data->work_fd = open(data->filename_work, O_RDWR);
200         if (data->work_fd < 0) {
201 #ifndef HAVE_GLOB_H
202                 return 0;
203 #else
204                 unsigned int    i;
205                 int             found;
206                 time_t          chtime;
207                 char const      *filename;
208                 glob_t          files;
209
210                 DEBUG2("detail (%s): Polling for detail file", data->name);
211
212                 memset(&files, 0, sizeof(files));
213                 if (glob(data->filename, 0, NULL, &files) != 0) {
214                 noop:
215                         globfree(&files);
216                         return 0;
217                 }
218
219                 /*
220                  *      Loop over the glob'd files, looking for the
221                  *      oldest one.
222                  */
223                 chtime = 0;
224                 found = -1;
225                 for (i = 0; i < files.gl_pathc; i++) {
226                         if (stat(files.gl_pathv[i], &st) < 0) continue;
227
228                         if ((i == 0) || (st.st_ctime < chtime)) {
229                                 chtime = st.st_ctime;
230                                 found = i;
231                         }
232                 }
233
234                 if (found < 0) goto noop;
235
236                 /*
237                  *      Rename detail to detail.work
238                  */
239                 filename = files.gl_pathv[found];
240
241                 DEBUG("detail (%s): Renaming %s -> %s", data->name, filename, data->filename_work);
242                 if (rename(filename, data->filename_work) < 0) {
243                         ERROR("detail (%s): Failed renaming %s to %s: %s",
244                               data->name, filename, data->filename_work, fr_syserror(errno));
245                         goto noop;
246                 }
247
248                 globfree(&files);       /* Shouldn't be using anything in files now */
249
250                 /*
251                  *      And try to open the filename.
252                  */
253                 data->work_fd = open(data->filename_work, O_RDWR);
254                 if (data->work_fd < 0) return 0;
255 #endif
256         } /* else detail.work existed, and we opened it */
257
258         rad_assert(data->vps == NULL);
259         rad_assert(data->fp == NULL);
260
261         data->state = STATE_UNLOCKED;
262
263         data->client_ip.af = AF_UNSPEC;
264         data->timestamp = 0;
265         data->offset = data->last_offset = data->timestamp_offset = 0;
266         data->packets = 0;
267         data->tries = 0;
268         data->done_entry = false;
269
270         return 1;
271 }
272
273
274 /*
275  *      FIXME: add a configuration "exit when done" so that the detail
276  *      file reader can be used as a one-off tool to update stuff.
277  *
278  *      The time sequence for reading from the detail file is:
279  *
280  *      t_0             signalled that the server is idle, and we
281  *                      can read from the detail file.
282  *
283  *      t_rtt           the packet has been processed successfully,
284  *                      wait for t_delay to enforce load factor.
285  *
286  *      t_rtt + t_delay wait for signal that the server is idle.
287  *
288  */
289 int detail_recv(rad_listen_t *listener)
290 {
291         char c = 0;
292         ssize_t rcode;
293         RADIUS_PACKET *packet;
294         listen_detail_t *data = listener->data;
295         RAD_REQUEST_FUNP fun = NULL;
296
297         /*
298          *      Block until there's a packet ready.
299          */
300         rcode = read(data->master_pipe[0], &packet, sizeof(packet));
301         if (rcode <= 0) return rcode;
302
303         if (DEBUG_ENABLED2) {
304                 VALUE_PAIR *vp;
305                 vp_cursor_t cursor;
306
307                 DEBUG2("detail (%s): Read packet from %s", data->name, data->filename_work);
308                 for (vp = fr_cursor_init(&cursor, &packet->vps);
309                      vp;
310                      vp = fr_cursor_next(&cursor)) {
311                         debug_pair(vp);
312                 }
313         }
314         rad_assert(packet != NULL);
315
316         switch (packet->code) {
317         case PW_CODE_ACCOUNTING_REQUEST:
318                 fun = rad_accounting;
319                 break;
320
321         case PW_CODE_COA_REQUEST:
322         case PW_CODE_DISCONNECT_REQUEST:
323                 fun = rad_coa_recv;
324                 break;
325
326         default:
327                 data->state = STATE_REPLIED;
328                 goto signal_thread;
329         }
330
331         if (!request_receive(NULL, listener, packet, &data->detail_client, fun)) {
332                 data->state = STATE_NO_REPLY;   /* try again later */
333
334         signal_thread:
335                 rad_free(&packet);
336                 if (write(data->child_pipe[1], &c, 1) < 0) {
337                         ERROR("detail (%s): Failed writing ack to reader thread: %s", data->name,
338                               fr_syserror(errno));
339                 }
340         }
341
342         /*
343          *      Wait for the child thread to write an answer to the pipe
344          */
345         return 0;
346 }
347
348 static RADIUS_PACKET *detail_poll(rad_listen_t *listener)
349 {
350         char            key[256], op[8], value[1024];
351         vp_cursor_t     cursor;
352         VALUE_PAIR      *vp;
353         RADIUS_PACKET   *packet;
354         char            buffer[2048];
355         listen_detail_t *data = listener->data;
356
357         switch (data->state) {
358         case STATE_UNOPENED:
359 open_file:
360                 rad_assert(data->work_fd < 0);
361
362                 if (!detail_open(listener)) return NULL;
363
364                 rad_assert(data->state == STATE_UNLOCKED);
365                 rad_assert(data->work_fd >= 0);
366
367                 /* FALL-THROUGH */
368
369         /*
370          *      Try to lock fd.  If we can't, return.
371          *      If we can, continue.  This means that
372          *      the server doesn't block while waiting
373          *      for the lock to open...
374          */
375         case STATE_UNLOCKED:
376                 /*
377                  *      Note that we do NOT block waiting for
378                  *      the lock.  We've re-named the file
379                  *      above, so we've already guaranteed
380                  *      that any *new* detail writer will not
381                  *      be opening this file.  The only
382                  *      purpose of the lock is to catch a race
383                  *      condition where the execution
384                  *      "ping-pongs" between radiusd &
385                  *      radrelay.
386                  */
387                 if (rad_lockfd_nonblock(data->work_fd, 0) < 0) {
388                         /*
389                          *      Close the FD.  The main loop
390                          *      will wake up in a second and
391                          *      try again.
392                          */
393                         close(data->work_fd);
394                         data->fp = NULL;
395                         data->work_fd = -1;
396                         data->state = STATE_UNOPENED;
397                         return NULL;
398                 }
399
400                 /*
401                  *      Only open for writing if we're
402                  *      marking requests as completed.
403                  */
404                 data->fp = fdopen(data->work_fd, data->track ? "r+" : "r");
405                 if (!data->fp) {
406                         ERROR("detail (%s): FATAL: Failed to re-open detail file: %s",
407                               data->name, fr_syserror(errno));
408                         fr_exit(1);
409                 }
410
411                 /*
412                  *      Look for the header
413                  */
414                 data->state = STATE_HEADER;
415                 data->delay_time = USEC;
416                 data->vps = NULL;
417
418                 /* FALL-THROUGH */
419
420         case STATE_HEADER:
421         do_header:
422                 data->done_entry = false;
423                 data->timestamp_offset = 0;
424
425                 data->tries = 0;
426                 if (!data->fp) {
427                         data->state = STATE_UNOPENED;
428                         goto open_file;
429                 }
430
431                 {
432                         struct stat buf;
433
434                         if (fstat(data->work_fd, &buf) < 0) {
435                                 ERROR("detail (%s): Failed to stat detail file: %s",
436                                       data->name, fr_syserror(errno));
437
438                                 goto cleanup;
439                         }
440                         if (((off_t) ftell(data->fp)) == buf.st_size) {
441                                 goto cleanup;
442                         }
443                 }
444
445                 /*
446                  *      End of file.  Delete it, and re-set
447                  *      everything.
448                  */
449                 if (feof(data->fp)) {
450                 cleanup:
451                         DEBUG("detail (%s): Unlinking %s", data->name, data->filename_work);
452                         unlink(data->filename_work);
453                         if (data->fp) fclose(data->fp);
454                         data->fp = NULL;
455                         data->work_fd = -1;
456                         data->state = STATE_UNOPENED;
457                         rad_assert(data->vps == NULL);
458
459                         if (data->one_shot) {
460                                 INFO("detail (%s): Finished reading \"one shot\" detail file - Exiting", data->name);
461                                 radius_signal_self(RADIUS_SIGNAL_SELF_EXIT);
462                         }
463
464                         return NULL;
465                 }
466
467                 /*
468                  *      Else go read something.
469                  */
470                 break;
471
472         /*
473          *      Read more value-pair's, unless we're
474          *      at EOF.  In that case, queue whatever
475          *      we have.
476          */
477         case STATE_READING:
478                 if (data->fp && !feof(data->fp)) break;
479                 data->state = STATE_QUEUED;
480
481                 /* FALL-THROUGH */
482
483         case STATE_QUEUED:
484                 goto alloc_packet;
485
486         /*
487          *      Periodically check what's going on.
488          *      If the request is taking too long,
489          *      retry it.
490          */
491         case STATE_RUNNING:
492                 if (time(NULL) < (data->running + (int)data->retry_interval)) {
493                         return NULL;
494                 }
495
496                 DEBUG("detail (%s): No response to detail request.  Retrying", data->name);
497                 /* FALL-THROUGH */
498
499         /*
500          *      If there's no reply, keep
501          *      retransmitting the current packet
502          *      forever.
503          */
504         case STATE_NO_REPLY:
505                 data->state = STATE_QUEUED;
506                 goto alloc_packet;
507
508         /*
509          *      We have a reply.  Clean up the old
510          *      request, and go read another one.
511          */
512         case STATE_REPLIED:
513                 if (data->track) {
514                         rad_assert(data->fp != NULL);
515
516                         if (fseek(data->fp, data->timestamp_offset, SEEK_SET) < 0) {
517                                 WARN("detail (%s): Failed seeking to timestamp offset: %s",
518                                      data->name, fr_syserror(errno));
519                         } else if (fwrite("\tDone", 1, 5, data->fp) < 5) {
520                                 WARN("detail (%s): Failed marking request as done: %s",
521                                      data->name, fr_syserror(errno));
522                         } else if (fflush(data->fp) != 0) {
523                                 WARN("detail (%s): Failed flushing marked detail file to disk: %s",
524                                      data->name, fr_syserror(errno));
525                         }
526
527                         if (fseek(data->fp, data->offset, SEEK_SET) < 0) {
528                                 WARN("detail (%s): Failed seeking to next detail request: %s",
529                                      data->name, fr_syserror(errno));
530                         }
531                 }
532
533                 pairfree(&data->vps);
534                 data->state = STATE_HEADER;
535                 goto do_header;
536         }
537
538         fr_cursor_init(&cursor, &data->vps);
539
540         /*
541          *      Read a header, OR a value-pair.
542          */
543         while (fgets(buffer, sizeof(buffer), data->fp)) {
544                 data->last_offset = data->offset;
545                 data->offset = ftell(data->fp); /* for statistics */
546
547                 /*
548                  *      Badly formatted file: delete it.
549                  *
550                  *      FIXME: Maybe flag an error?
551                  */
552                 if (!strchr(buffer, '\n')) {
553                         pairfree(&data->vps);
554                         goto cleanup;
555                 }
556
557                 /*
558                  *      We're reading VP's, and got a blank line.
559                  *      Queue the packet.
560                  */
561                 if ((data->state == STATE_READING) &&
562                     (buffer[0] == '\n')) {
563                         data->state = STATE_QUEUED;
564                         break;
565                 }
566
567                 /*
568                  *      Look for date/time header, and read VP's if
569                  *      found.  If not, keep reading lines until we
570                  *      find one.
571                  */
572                 if (data->state == STATE_HEADER) {
573                         int y;
574
575                         if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
576                                 data->state = STATE_READING;
577                         }
578                         continue;
579                 }
580
581                 /*
582                  *      We have a full "attribute = value" line.
583                  *      If it doesn't look reasonable, skip it.
584                  *
585                  *      FIXME: print an error for badly formatted attributes?
586                  */
587                 if (sscanf(buffer, "%255s %7s %1023s", key, op, value) != 3) {
588                         WARN("detail (%s): Skipping badly formatted line %s", data->name, buffer);
589                         continue;
590                 }
591
592                 /*
593                  *      Should be =, :=, +=, ...
594                  */
595                 if (!strchr(op, '=')) continue;
596
597                 /*
598                  *      Skip non-protocol attributes.
599                  */
600                 if (!strcasecmp(key, "Request-Authenticator")) continue;
601
602                 /*
603                  *      Set the original client IP address, based on
604                  *      what's in the detail file.
605                  *
606                  *      Hmm... we don't set the server IP address.
607                  *      or port.  Oh well.
608                  */
609                 if (!strcasecmp(key, "Client-IP-Address")) {
610                         data->client_ip.af = AF_INET;
611                         if (ip_hton(&data->client_ip, AF_INET, value, false) < 0) {
612                                 ERROR("detail (%s): Failed parsing Client-IP-Address", data->name);
613
614                                 pairfree(&data->vps);
615                                 goto cleanup;
616                         }
617                         continue;
618                 }
619
620                 /*
621                  *      The original time at which we received the
622                  *      packet.  We need this to properly calculate
623                  *      Acct-Delay-Time.
624                  */
625                 if (!strcasecmp(key, "Timestamp")) {
626                         data->timestamp = atoi(value);
627                         data->timestamp_offset = data->last_offset;
628
629                         vp = paircreate(data, PW_PACKET_ORIGINAL_TIMESTAMP, 0);
630                         if (vp) {
631                                 vp->vp_date = (uint32_t) data->timestamp;
632                                 vp->type = VT_DATA;
633                                 fr_cursor_insert(&cursor, vp);
634                         }
635                         continue;
636                 }
637
638                 if (!strcasecmp(key, "Donestamp")) {
639                         data->timestamp = atoi(value);
640                         data->done_entry = true;
641                         continue;
642                 }
643
644                 /*
645                  *      Read one VP.
646                  *
647                  *      FIXME: do we want to check for non-protocol
648                  *      attributes like radsqlrelay does?
649                  */
650                 vp = NULL;
651                 if ((userparse(data, buffer, &vp) > 0) &&
652                     (vp != NULL)) {
653                         fr_cursor_merge(&cursor, vp);
654                 }
655         }
656
657         /*
658          *      Some kind of error.
659          *
660          *      FIXME: Leave the file in-place, and warn the
661          *      administrator?
662          */
663         if (ferror(data->fp)) goto cleanup;
664
665         data->tries = 0;
666         data->packets++;
667
668         /*
669          *      Process the packet.
670          */
671  alloc_packet:
672         if (data->done_entry) {
673                 DEBUG2("detail (%s): Skipping record for timestamp %lu", data->name, data->timestamp);
674                 pairfree(&data->vps);
675                 data->state = STATE_HEADER;
676                 goto do_header;
677         }
678
679         data->tries++;
680
681         /*
682          *      The writer doesn't check that the record was
683          *      completely written.  If the disk is full, this can
684          *      result in a truncated record.  When that happens,
685          *      treat it as EOF.
686          */
687         if (data->state != STATE_QUEUED) {
688                 ERROR("detail (%s): Truncated record: treating it as EOF for detail file %s",
689                       data->name, data->filename_work);
690                 pairfree(&data->vps);
691                 goto cleanup;
692         }
693
694         /*
695          *      We're done reading the file, but we didn't read
696          *      anything.  Clean up, and don't return anything.
697          */
698         if (!data->vps) {
699                 data->state = STATE_HEADER;
700                 if (!data->fp || feof(data->fp)) goto cleanup;
701                 return NULL;
702         }
703
704         /*
705          *      Allocate the packet.  If we fail, it's a serious
706          *      problem.
707          */
708         packet = rad_alloc(NULL, true);
709         if (!packet) {
710                 ERROR("detail (%s): FATAL: Failed allocating memory for detail", data->name);
711                 fr_exit(1);
712         }
713
714         memset(packet, 0, sizeof(*packet));
715         packet->sockfd = -1;
716         packet->src_ipaddr.af = AF_INET;
717         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
718
719         /*
720          *      If everything's OK, this is a waste of memory.
721          *      Otherwise, it lets us re-send the original packet
722          *      contents, unmolested.
723          */
724         packet->vps = paircopy(packet, data->vps);
725
726         packet->code = PW_CODE_ACCOUNTING_REQUEST;
727         vp = pairfind(packet->vps, PW_PACKET_TYPE, 0, TAG_ANY);
728         if (vp) packet->code = vp->vp_integer;
729
730         gettimeofday(&packet->timestamp, NULL);
731
732         /*
733          *      Remember where it came from, so that we don't
734          *      proxy it to the place it came from...
735          */
736         if (data->client_ip.af != AF_UNSPEC) {
737                 packet->src_ipaddr = data->client_ip;
738         }
739
740         vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0, TAG_ANY);
741         if (vp) {
742                 packet->src_ipaddr.af = AF_INET;
743                 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
744                 packet->src_ipaddr.prefix = 32;
745         } else {
746                 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0, TAG_ANY);
747                 if (vp) {
748                         packet->src_ipaddr.af = AF_INET6;
749                         memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
750                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
751                         packet->src_ipaddr.prefix = 128;
752                 }
753         }
754
755         vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
756         if (vp) {
757                 packet->dst_ipaddr.af = AF_INET;
758                 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
759                 packet->dst_ipaddr.prefix = 32;
760         } else {
761                 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
762                 if (vp) {
763                         packet->dst_ipaddr.af = AF_INET6;
764                         memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
765                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
766                         packet->dst_ipaddr.prefix = 128;
767                 }
768         }
769
770         /*
771          *      Generate packet ID, ports, IP via a counter.
772          */
773         packet->id = data->counter & 0xff;
774         packet->src_port = 1024 + ((data->counter >> 8) & 0xff);
775         packet->dst_port = 1024 + ((data->counter >> 16) & 0xff);
776
777         packet->dst_ipaddr.af = AF_INET;
778         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | ((data->counter >> 24) & 0xff));
779
780         /*
781          *      Create / update accounting attributes.
782          */
783         if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
784                 /*
785                  *      Prefer the Event-Timestamp in the packet, if it
786                  *      exists.  That is when the event occurred, whereas the
787                  *      "Timestamp" field is when we wrote the packet to the
788                  *      detail file, which could have been much later.
789                  */
790                 vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP, 0, TAG_ANY);
791                 if (vp) {
792                         data->timestamp = vp->vp_integer;
793                 }
794
795                 /*
796                  *      Look for Acct-Delay-Time, and update
797                  *      based on Acct-Delay-Time += (time(NULL) - timestamp)
798                  */
799                 vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
800                 if (!vp) {
801                         vp = paircreate(packet, PW_ACCT_DELAY_TIME, 0);
802                         rad_assert(vp != NULL);
803                         pairadd(&packet->vps, vp);
804                 }
805                 if (data->timestamp != 0) {
806                         vp->vp_integer += time(NULL) - data->timestamp;
807                 }
808         }
809
810         /*
811          *      Set the transmission count.
812          */
813         vp = pairfind(packet->vps, PW_PACKET_TRANSMIT_COUNTER, 0, TAG_ANY);
814         if (!vp) {
815                 vp = paircreate(packet, PW_PACKET_TRANSMIT_COUNTER, 0);
816                 rad_assert(vp != NULL);
817                 pairadd(&packet->vps, vp);
818         }
819         vp->vp_integer = data->tries;
820
821         data->state = STATE_RUNNING;
822         data->running = packet->timestamp.tv_sec;
823
824         return packet;
825 }
826
827 /*
828  *      Free detail-specific stuff.
829  */
830 void detail_free(rad_listen_t *this)
831 {
832         listen_detail_t *data = this->data;
833
834         if (!check_config) {
835                 ssize_t ret;
836                 void *arg = NULL;
837
838                 /*
839                  *      Mark the child pipes as unusable
840                  */
841                 close(data->child_pipe[0]);
842                 close(data->child_pipe[1]);
843                 data->child_pipe[0] = -1;
844
845                 /*
846                  *      Tell it to stop (interrupting its sleep)
847                  */
848                 pthread_kill(data->pthread_id, SIGTERM);
849
850                 /*
851                  *      Wait for it to acknowledge that it's stopped.
852                  */
853                 ret = read(data->master_pipe[0], &arg, sizeof(arg));
854                 if (ret < 0) {
855                         ERROR("detail (%s): Reader thread exited without informing the master: %s",
856                               data->name, fr_syserror(errno));
857                 } else if (ret != sizeof(arg)) {
858                         ERROR("detail (%s): Invalid thread pointer received from reader thread during exit",
859                               data->name);
860                         ERROR("detail (%s): Expected %zu bytes, got %zi bytes", data->name, sizeof(arg), ret);
861                 }
862
863                 close(data->master_pipe[0]);
864                 close(data->master_pipe[1]);
865
866                 if (arg) pthread_join(data->pthread_id, &arg);
867         }
868
869         if (data->fp != NULL) {
870                 fclose(data->fp);
871                 data->fp = NULL;
872         }
873 }
874
875
876 int detail_print(rad_listen_t const *this, char *buffer, size_t bufsize)
877 {
878         if (!this->server) {
879                 return snprintf(buffer, bufsize, "%s",
880                                 ((listen_detail_t *)(this->data))->filename);
881         }
882
883         return snprintf(buffer, bufsize, "detail file %s as server %s",
884                         ((listen_detail_t *)(this->data))->filename,
885                         this->server);
886 }
887
888
889 /*
890  *      Delay while waiting for a file to be ready
891  */
892 static int detail_delay(listen_detail_t *data)
893 {
894         int delay = (data->poll_interval - 1) * USEC;
895
896         /*
897          *      Add +/- 0.25s of jitter
898          */
899         delay += (USEC * 3) / 4;
900         delay += fr_rand() % (USEC / 2);
901
902         DEBUG2("detail (%s): Detail listener state %s waiting %d.%06d sec",
903                data->name,
904                fr_int2str(state_names, data->state, "?"),
905                (delay / USEC), delay % USEC);
906
907         return delay;
908 }
909
910 /*
911  *      Overloaded to return delay times.
912  */
913 int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
914 {
915         return 0;
916 }
917
918 /*
919  *      Overloaded to return "should we fix delay times"
920  */
921 int detail_decode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
922 {
923         return 0;
924 }
925
926
927 static void *detail_handler_thread(void *arg)
928 {
929         char c;
930         rad_listen_t *this = arg;
931         listen_detail_t *data = this->data;
932
933         while (true) {
934                 RADIUS_PACKET *packet;
935
936                 while ((packet = detail_poll(this)) == NULL) {
937                         usleep(detail_delay(data));
938
939                         /*
940                          *      If we're supposed to exit then tell
941                          *      the master thread we've exited.
942                          */
943                         if (data->child_pipe[0] < 0) {
944                                 packet = NULL;
945                                 if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
946                                         ERROR("detail (%s): Failed writing exit status to master: %s",
947                                               data->name, fr_syserror(errno));
948                                 }
949                                 return NULL;
950                         }
951                 }
952
953                 /*
954                  *      Keep retrying forever.
955                  *
956                  *      FIXME: cap the retries.
957                  */
958                 do {
959                         if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
960                                 ERROR("detail (%s): Failed passing detail packet pointer to master: %s",
961                                       data->name, fr_syserror(errno));
962                         }
963
964                         if (read(data->child_pipe[0], &c, 1) < 0) {
965                                 ERROR("detail (%s): Failed getting detail packet ack from master: %s",
966                                       data->name, fr_syserror(errno));
967                                 break;
968                         }
969
970                         if (data->delay_time > 0) usleep(data->delay_time);
971
972                         packet = detail_poll(this);
973                         if (!packet) break;
974                 } while (data->state != STATE_REPLIED);
975         }
976
977         return NULL;
978 }
979
980
981 static const CONF_PARSER detail_config[] = {
982         { "detail", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_DEPRECATED, listen_detail_t, filename), NULL },
983         { "filename", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_REQUIRED, listen_detail_t, filename), NULL },
984         { "load_factor", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), STRINGIFY(10) },
985         { "poll_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, poll_interval), STRINGIFY(1) },
986         { "retry_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, retry_interval), STRINGIFY(30) },
987         { "one_shot", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, one_shot), "no" },
988         { "track", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, track), "no" },
989
990         { NULL, -1, 0, NULL, NULL }             /* end the list */
991 };
992
993 /*
994  *      Parse a detail section.
995  */
996 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
997 {
998         int             rcode;
999         listen_detail_t *data;
1000         RADCLIENT       *client;
1001         char            buffer[2048];
1002
1003         data = this->data;
1004
1005         rcode = cf_section_parse(cs, data, detail_config);
1006         if (rcode < 0) {
1007                 cf_log_err_cs(cs, "Failed parsing listen section");
1008                 return -1;
1009         }
1010
1011         data->name = cf_section_name2(cs);
1012         if (!data->name) data->name = data->filename;
1013
1014         /*
1015          *      We don't do duplicate detection for "detail" sockets.
1016          */
1017         this->nodup = true;
1018         this->synchronous = false;
1019
1020         if (!data->filename) {
1021                 cf_log_err_cs(cs, "No detail file specified in listen section");
1022                 return -1;
1023         }
1024
1025         FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, >=, 1);
1026         FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, <=, 100);
1027
1028         FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, >=, 1);
1029         FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, <=, 60);
1030
1031         FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, >=, 4);
1032         FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, <=, 3600);
1033
1034         /*
1035          *      Only checking the config.  Don't start threads or anything else.
1036          */
1037         if (check_config) return 0;
1038
1039         /*
1040          *      If the filename is a glob, use "detail.work" as the
1041          *      work file name.
1042          */
1043         if ((strchr(data->filename, '*') != NULL) ||
1044             (strchr(data->filename, '[') != NULL)) {
1045                 char *p;
1046
1047 #ifndef HAVE_GLOB_H
1048                 WARN("detail (%s): File \"%s\" appears to use file globbing, but it is not supported on this system",
1049                      data->name, data->filename);
1050 #endif
1051                 strlcpy(buffer, data->filename, sizeof(buffer));
1052                 p = strrchr(buffer, FR_DIR_SEP);
1053                 if (p) {
1054                         p[1] = '\0';
1055                 } else {
1056                         buffer[0] = '\0';
1057                 }
1058                 strlcat(buffer, "detail.work",
1059                         sizeof(buffer) - strlen(buffer));
1060
1061         } else {
1062                 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
1063         }
1064
1065         data->filename_work = talloc_strdup(data, buffer);
1066
1067         data->work_fd = -1;
1068         data->vps = NULL;
1069         data->fp = NULL;
1070         data->state = STATE_UNOPENED;
1071         data->delay_time = data->poll_interval * USEC;
1072         data->signal = 1;
1073
1074         /*
1075          *      Initialize the fake client.
1076          */
1077         client = &data->detail_client;
1078         memset(client, 0, sizeof(*client));
1079         client->ipaddr.af = AF_INET;
1080         client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
1081         client->ipaddr.prefix = 0;
1082         client->longname = client->shortname = data->filename;
1083         client->secret = client->shortname;
1084         client->nas_type = talloc_strdup(data, "none"); /* Part of 'data' not dynamically allocated */
1085
1086         /*
1087          *      Create the communication pipes.
1088          */
1089         if (pipe(data->master_pipe) < 0) {
1090                 ERROR("detail (%s): Error opening internal pipe: %s", data->name, fr_syserror(errno));
1091                 fr_exit(1);
1092         }
1093
1094         if (pipe(data->child_pipe) < 0) {
1095                 ERROR("detail (%s): Error opening internal pipe: %s", data->name, fr_syserror(errno));
1096                 fr_exit(1);
1097         }
1098
1099         pthread_create(&data->pthread_id, NULL, detail_handler_thread, this);
1100
1101         this->fd = data->master_pipe[0];
1102
1103         return 0;
1104 }
1105 #endif