The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
proto_detail_work.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: e79c02d30e5774de47b8076fd1d1080b57ad858f $
19 * @file proto_detail_work.c
20 * @brief Detail handler for files
21 *
22 * @copyright 2017 The FreeRADIUS server project.
23 * @copyright 2017 Alan DeKok (aland@deployingradius.com)
24 */
25#include <netdb.h>
26#include <freeradius-devel/server/pair.h>
27#include <freeradius-devel/server/main_loop.h>
28#include <freeradius-devel/io/application.h>
29#include <freeradius-devel/io/listen.h>
30#include <freeradius-devel/util/syserror.h>
31#include "proto_detail.h"
32
33#include <fcntl.h>
34#include <sys/stat.h>
35
36#ifndef NDEBUG
37#if 0
38/*
39 * When we want detailed debugging here, without detailed server
40 * debugging.
41 */
42#define MPRINT DEBUG
43#else
44#define MPRINT DEBUG3
45#endif
46#else
47// No debugging, just remove the mprint entirely
48#define MPRINT(_x, ...)
49#endif
50
51typedef struct {
52 proto_detail_work_thread_t *parent; //!< talloc_parent is SLOW!
53 fr_time_t timestamp; //!< when we read the entry.
54 off_t done_offset; //!< where we're tracking the status
55
56 int id; //!< for retransmission counters
57
58 uint8_t *packet; //!< for retransmissions
59 size_t packet_len; //!< for retransmissions
60
61 fr_retry_t retry; //!< our retry timers
62 fr_timer_t *ev; //!< retransmission timer
63 fr_dlist_t entry; //!< for the retransmission list
65
67 { FR_CONF_OFFSET("initial_rtx_time", proto_detail_work_t, retry_config.irt), .dflt = STRINGIFY(2) },
68 { FR_CONF_OFFSET("max_rtx_time", proto_detail_work_t, retry_config.mrt), .dflt = STRINGIFY(16) },
69
70 /*
71 * Retransmit indefinitely, as v2 and v3 did.
72 */
73 { FR_CONF_OFFSET("max_rtx_count", proto_detail_work_t, retry_config.mrc), .dflt = STRINGIFY(0) },
74 /*
75 * ...again same as v2 and v3.
76 */
77 { FR_CONF_OFFSET("max_rtx_duration", proto_detail_work_t, retry_config.mrd), .dflt = STRINGIFY(0) },
78 { FR_CONF_OFFSET("max_outstanding", proto_detail_work_t, max_outstanding), .dflt = STRINGIFY(1) },
80};
81
82
84 { FR_CONF_OFFSET_FLAGS("filename", CONF_FLAG_REQUIRED, proto_detail_work_t, filename_work ) },
85
86 { FR_CONF_OFFSET("track", proto_detail_work_t, track_progress ) },
87
88 { FR_CONF_OFFSET("retransmit", proto_detail_work_t, retransmit ), .dflt = "yes" },
89
90 { FR_CONF_POINTER("limit", 0, CONF_FLAG_SUBSECTION, NULL), .subcs = (void const *) limit_config },
92};
93
95
98 { .out = &dict_freeradius, .proto = "freeradius" },
99
101};
102
104
110
111/*
112 * All of the decoding is done by proto_detail.c
113 */
114static int mod_decode(void const *instance, request_t *request, UNUSED uint8_t *const data, UNUSED size_t data_len)
115{
116
119 fr_pair_t *vp;
120
121 request->client = inst->client;
122
123 request->packet->id = track->id;
124 request->reply->id = track->id;
126
128 vp->vp_uint32 = track->retry.count;
129
130 return 0;
131}
132
135 { 0 }
136};
137
140 { 0 }
141};
142
144 size_t buffer_len, size_t *leftover)
145{
148 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
149
150 ssize_t data_size;
151 size_t packet_len;
152 fr_detail_entry_t *track;
153 uint8_t *partial, *end, *next, *p, *record_end;
154 uint8_t *stopped_search;
155 off_t done_offset;
156
157 fr_assert(*leftover < buffer_len);
158 fr_assert(thread->fd >= 0);
159 fr_assert(thread->el);
160
161 MPRINT("AT COUNT %d offset %ld", thread->count, (long) thread->read_offset);
162
163 /*
164 * Process retransmissions before anything else in the
165 * file.
166 */
167 track = fr_dlist_head(&thread->list);
168 if (track) {
169 fr_dlist_remove(&thread->list, track);
170
171 /*
172 * Don't over-write "leftover" bytes!
173 */
174 if (*leftover) {
175 fr_assert(thread->leftover == 0);
176 if (!thread->leftover_buffer) MEM(thread->leftover_buffer = talloc_array(thread, uint8_t, buffer_len));
177
178 memcpy(thread->leftover_buffer, buffer, *leftover);
179 thread->leftover = *leftover;
180 *leftover = 0;
181 }
182
183 fr_assert(buffer_len >= track->packet_len);
184 memcpy(buffer, track->packet, track->packet_len);
185
186 DEBUG("Retrying packet %d (retransmission %u)", track->id, track->retry.count);
187 *packet_ctx = track;
188 *recv_time_p = track->timestamp;
189 return track->packet_len;
190 }
191
192 /*
193 * If we decide that we're closing, ignore everything
194 * else in the file. Someone extended the file on us
195 * without locking it first. So too bad for them.
196 */
197 if (thread->closing) {
198 if (inst->track_progress) thread->read_offset = lseek(thread->fd, 0, SEEK_END);
199 return 0;
200 }
201
202 /*
203 * Once a socket is ready, the network side tries to read
204 * many packets. So if we want to stop it from reading,
205 * we have to check this ourselves.
206 */
207 if (thread->outstanding >= inst->max_outstanding) {
208 fr_assert(thread->paused);
209 return 0;
210 }
211
212 /*
213 * If we've cached leftover data from the ring buffer,
214 * copy it back.
215 */
216 if (thread->leftover) {
217 fr_assert(*leftover == 0);
218 fr_assert(thread->leftover < buffer_len);
219
220 memcpy(buffer, thread->leftover_buffer, thread->leftover);
221 *leftover = thread->leftover;
222 thread->leftover = 0;
223 }
224
225 /*
226 * Seek to the current read offset.
227 */
228 (void) lseek(thread->fd, thread->read_offset, SEEK_SET);
229
230 /*
231 * There will be "leftover" bytes left over in the buffer
232 * from any previous read. At the start of the file,
233 * "leftover" will be zero.
234 */
235 partial = buffer + *leftover;
236
237 MPRINT("READ leftover %zu", *leftover);
238
239 /*
240 * Try to read as much data as possible.
241 */
242 if (!thread->eof) {
243 size_t room;
244
245 room = buffer_len - *leftover;
246
247 data_size = read(thread->fd, partial, room);
248 if (data_size < 0) {
249 ERROR("proto_detail (%s): Failed reading file %s: %s",
250 thread->name, thread->filename_work, fr_syserror(errno));
251 return -1;
252 }
253
254 MPRINT("GOT %zd bytes", data_size);
255
256 /*
257 * Remember the read offset, and whether we got EOF.
258 */
259 thread->read_offset = lseek(thread->fd, 0, SEEK_CUR);
260
261 /*
262 * Only set EOF if there's no more data in the buffer to manage.
263 */
264 thread->eof = (data_size == 0) || (thread->read_offset == thread->file_size) || ((size_t) data_size < room);
265 if (thread->eof) {
266 MPRINT("Set EOF data_size %ld vs room %zu", data_size, room);
267 MPRINT("Set EOF read %ld vs file %ld", (long) thread->read_offset, (long) thread->file_size);
268 }
269 end = partial + data_size;
270
271 } else {
272 MPRINT("READ UNTIL EOF");
273 /*
274 * We didn't read any more data from the file,
275 * but there should be data left in the buffer.
276 */
277 fr_assert(*leftover > 0);
278 end = buffer + *leftover;
279 }
280
281redo:
282 next = NULL;
283 stopped_search = end;
284
285 /*
286 * Look for "end of record" marker, starting from the
287 * beginning of the buffer.
288 *
289 * Note that all of the data MUST be printable, and raw
290 * LFs are forbidden in attribute contents.
291 */
292 fr_assert((buffer + thread->last_search) <= end);
293
294 MPRINT("Starting search from offset %zu", thread->last_search);
295
296 p = buffer + thread->last_search;
297 while (p < end) {
298 if (p[0] != '\n') {
299 p++;
300 continue;
301 }
302 if ((p + 1) == end) {
303 /*
304 * Remember the last LF, so if the next
305 * read starts with a LF, we can find the
306 * end of record marker.
307 */
308 stopped_search = p;
309 break; /* no more data */
310 }
311
312 if (p[1] == '\n') {
313 p[0] = '\0';
314 p[1] = '\0';
315 next = p + 2;
316 stopped_search = next;
317 thread->last_line++;
318 break;
319 }
320
321 /*
322 * If we're not at EOF, and we're not at end of
323 * record, every line MUST have a leading tab.
324 */
325 if (p[1] != '\t') {
326 ERROR("proto_detail (%s): Missing tab indent at %s[%u], offset from start of file %zu",
327 thread->name,
328 thread->filename_work, thread->last_line,
329 (size_t)((p - buffer) + thread->header_offset));
330 return -1;
331 }
332
333 /*
334 * Smash the \n with zero, so that each line can
335 * be parsed individually.
336 */
337 p[0] = '\0';
338
339 /*
340 * Skip the \n\t
341 */
342 if ((p + 2) >= end) {
343 stopped_search = p;
344 break;
345 }
346
347 p += 2;
348
349 /*
350 * Skip attribute name
351 */
352 while ((p < end) && !isspace((uint8_t) *p)) p++;
353
354 /*
355 * Not enough room for " = ", skip this sanity
356 * check, and just search for a \n on the next
357 * round through the loop.
358 */
359 if ((end - p) < 3) {
360 stopped_search = p;
361 break;
362 }
363
364 /*
365 * Check for " = ". If the line doesn't contain
366 * this, it's malformed.
367 */
368 if (memcmp(p, " = ", 3) != 0) {
369 ERROR("proto_detail (%s): Missing pair assignment operator at %s[%u], offset from start of file %zu: %.*s",
370 thread->name,
371 thread->filename_work, thread->last_line,
372 (size_t)((p - buffer) + thread->header_offset), (int) (end - p), p);
373 return -1;
374 }
375
376 /*
377 * Skip the " = ", and go back to the top of the
378 * loop where we check for the next \n.
379 */
380 p += 3;
381 }
382
383 thread->last_search = (stopped_search - buffer);
384
385 /*
386 * If there is a next record, remember how large this
387 * record is, and update "leftover" bytes.
388 */
389 if (next) {
390 packet_len = next - buffer;
391 *leftover = end - next;
392
393 MPRINT("FOUND next at %zu, leftover is %zu", packet_len, *leftover);
394
395 } else if (!thread->eof) {
396 if ((size_t) (end - buffer) == buffer_len) {
397 ERROR("proto_detail (%s): Too large entry (>%d bytes) found at offset %zu: %.*s of file %s",
398 thread->name, (int) buffer_len,
399 (size_t)((p - buffer) + thread->header_offset), (int) (end - p), p,
400 thread->filename_work);
401 return -1;
402 }
403
404 /*
405 * We're not at EOF, and there is no "next"
406 * entry. Remember all of the leftover data in
407 * the buffer, and ask the caller to call us when
408 * there's more data.
409 */
410 *leftover = end - buffer;
411 MPRINT("Not at EOF, and no next. Leftover is %zu", *leftover);
412 return 0;
413
414 } else {
415 /*
416 * Else we're at EOF, it's OK to not have an "end
417 * of record" marker. We just eat all of the
418 * remaining data.
419 */
420 packet_len = end - buffer;
421 *leftover = 0;
422
423 MPRINT("NO end of record, but at EOF, found %zu leftover is 0", packet_len);
424 }
425
426 /*
427 * Too big? Ignore it.
428 *
429 * @todo - skip the record, using memmove() etc.
430 */
431 if (packet_len > inst->parent->max_packet_size) {
432 DEBUG("Ignoring 'too large' entry at offset %zu of %s",
433 (size_t) thread->header_offset, thread->filename_work);
434 DEBUG("Entry size %lu is greater than allowed maximum %u",
435 packet_len, inst->parent->max_packet_size);
436 skip_record:
437 MPRINT("Skipping record");
438 if (next) {
439 memmove(buffer, next, (end - next));
440 data_size = (end - next);
441 *leftover = 0;
442 end = buffer + data_size;
443 thread->last_search = 0;
444
445 /*
446 * No more data, we're done.
447 */
448 if (end == buffer) return 0;
449 goto redo;
450 }
451
452 fr_assert(*leftover == 0);
453 packet_len = 0;
454 goto done;
455 }
456
457 /*
458 * Search for the "Timestamp" attribute. We overload
459 * that to track which entries have been used.
460 */
461 record_end = buffer + packet_len;
462 p = buffer;
463 done_offset = 0;
464
465 while (p < record_end) {
466 if (*p != '\0') {
467 p++;
468 continue;
469 }
470
471 p++;
472 if (p == record_end) break;
473
474 if (((record_end - p) >= 5) &&
475 (memcmp(p, "\tDone", 5) == 0)) {
476 goto skip_record;
477 }
478
479 if (((record_end - p) > 10) &&
480 (memcmp(p, "\tTimestamp", 10) == 0)) {
481 p++;
482 done_offset = thread->header_offset + (p - buffer);
483 }
484 }
485
486 /*
487 * Allocate the tracking entry.
488 */
489 MEM(track = talloc_zero(thread, fr_detail_entry_t));
490 track->parent = thread;
491 track->timestamp = fr_time();
492 track->id = thread->count++;
493
494 track->done_offset = done_offset;
495 if (inst->retransmit) {
496 MEM(track->packet = talloc_memdup(track, buffer, packet_len));
497 track->packet_len = packet_len;
498 }
499
500 /*
501 * We've read one more packet.
502 */
503 thread->header_offset += packet_len;
504
505 *packet_ctx = track;
506 *recv_time_p = track->timestamp;
507
508 thread->outstanding++;
509
510done:
511 /*
512 * If we're at EOF, mark us as "closing".
513 */
514 if (thread->eof) {
515 fr_assert(!thread->closing);
516 thread->closing = (*leftover == 0);
517 MPRINT("AT EOF, BUT CLOSING %d", thread->closing);
518 }
519
520 /*
521 * Pause reading until such time as we need more packets.
522 */
523 if (!thread->paused && (thread->outstanding >= inst->max_outstanding)) {
524 (void) fr_event_filter_update(thread->el, thread->fd, FR_EVENT_FILTER_IO, pause_read);
525 thread->paused = true;
526
527 /*
528 * Back up so that read() knows there's more data.
529 */
530 if (*leftover) (void) lseek(thread->fd, thread->read_offset - 1, SEEK_SET);
531 }
532
533 /*
534 * Next time, start searching from the start of the
535 * buffer.
536 */
537 thread->last_search = 0;
538
539 MPRINT("Returning NUM %u - %.*s", thread->outstanding, (int) packet_len, buffer);
540 return packet_len;
541}
542
543
544static void work_retransmit(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
545{
546 fr_detail_entry_t *track = talloc_get_type_abort(uctx, fr_detail_entry_t);
547 proto_detail_work_thread_t *thread = track->parent;
548
549 DEBUG("%s - retransmitting packet %d", thread->name, track->id);
550
551 fr_dlist_insert_tail(&thread->list, track);
552
553 if (thread->paused && (thread->outstanding < thread->inst->max_outstanding)) {
554 (void) fr_event_filter_update(thread->el, thread->fd, FR_EVENT_FILTER_IO, resume_read);
555 thread->paused = false;
556 }
557
558 fr_assert(thread->fd >= 0);
559
560 /*
561 * Seek to the START of the file, so that the FD will
562 * always return ready.
563 *
564 * The mod_read() function will take care of seeking to
565 * the correct read offset.
566 */
567 (void) lseek(thread->fd, 0, SEEK_SET);
568
569#ifdef __linux__
570 fr_network_listen_read(thread->nr, thread->li);
571#endif
572}
573
575 size_t buffer_len, UNUSED size_t written)
576{
579 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
581
582 if (buffer_len < 1) return -1;
583
584 fr_assert(thread->outstanding > 0);
585 fr_assert(thread->fd >= 0);
586
587 if (!buffer[0]) {
588 if (!inst->retransmit) goto mark_done;
589
590 if (fr_time_eq(track->retry.start, fr_time_wrap(0))) {
591 fr_retry_init(&track->retry, fr_time(), &inst->retry_config);
592 } else {
593 fr_retry_state_t state;
594
595 state = fr_retry_next(&track->retry, fr_time());
596 if (state == FR_RETRY_MRC) {
597 DEBUG("%s - packet %d failed after %u retransmissions",
598 thread->name, track->id, track->retry.count);
599 goto fail;
600
601 }
602
603 if (state == FR_RETRY_MRD) {
604 DEBUG("%s - packet %d failed after %u seconds",
605 thread->name, track->id,
606 (unsigned int) fr_time_delta_to_sec(inst->retry_config.mrd));
607 goto fail;
608 }
609 }
610
611 DEBUG("%s - packet %d failed during processing. Will retransmit in %.6fs",
612 thread->name, track->id, fr_time_delta_to_sec_double(track->retry.rt));
613
614 if (fr_timer_at(thread, thread->el->tl, &track->ev,
615 track->retry.next, false, work_retransmit, track) < 0) {
616 ERROR("%s - Failed inserting retransmission timeout", thread->name);
617 fail:
618 if (inst->track_progress && (track->done_offset > 0)) goto mark_done;
619 goto free_track;
620 }
621
622 if (!thread->paused && (thread->outstanding >= inst->max_outstanding)) {
623 (void) fr_event_filter_update(thread->el, thread->fd, FR_EVENT_FILTER_IO, pause_read);
624 thread->paused = true;
625 }
626
627 return 1;
628
629 } else if (inst->track_progress && (track->done_offset > 0)) {
630 mark_done:
631 /*
632 * Seek to the entry, mark it as done, and then seek to
633 * the point in the file where we were reading from.
634 */
635 (void) lseek(thread->fd, track->done_offset, SEEK_SET);
636 if (write(thread->fd, "Done", 4) < 0) {
637 ERROR("%s - Failed marking entry as done: %s", thread->name, fr_syserror(errno));
638 }
639 (void) lseek(thread->fd, thread->read_offset, SEEK_SET);
640 }
641
642free_track:
643 thread->outstanding--;
644
645 /*
646 * If we need to read some more packet, let's do so.
647 */
648 if (thread->paused && (thread->outstanding < inst->max_outstanding)) {
649 (void) fr_event_filter_update(thread->el, thread->fd, FR_EVENT_FILTER_IO, resume_read);
650 thread->paused = false;
651
652 /*
653 * And seek to the start of the file, so that the
654 * reader gets activated again. The reader will
655 * lseek() to the read offset, so this seek is fine.
656 */
657 (void) lseek(thread->fd, 0, SEEK_SET);
658 }
659
660 /*
661 * @todo - add a used / free pool for these
662 */
663 talloc_free(track);
664
665 /*
666 * Close the socket if we're at EOF, and there are no
667 * outstanding replies to deal with.
668 */
669 if (thread->closing && !thread->outstanding) {
670 MPRINT("WRITE ASKED TO CLOSE");
671 errno = ECONNRESET;
672 return -1;
673 }
674
675 MPRINT("WRITE RETURN B %zu", buffer_len);
676 return buffer_len;
677}
678
679/** Open a detail listener
680 *
681 */
683{
686 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
687
688 fr_dlist_init(&thread->list, fr_detail_entry_t, entry);
689
690 /*
691 * Open the file if we haven't already been given one.
692 */
693 if (thread->fd < 0) {
694 thread->filename_work = talloc_strdup(thread, inst->filename_work);
695
696 li->fd = thread->fd = open(thread->filename_work, inst->mode);
697 if (thread->fd < 0) {
698 cf_log_err(inst->cs, "Failed opening %s: %s", thread->filename_work, fr_syserror(errno));
699 return -1;
700 }
701 }
702
703 /*
704 * If we're tracking progress, learn where the EOF is.
705 */
706 if (inst->track_progress) {
707 struct stat buf;
708
709 if (fstat(thread->fd, &buf) < 0) {
710 cf_log_err(inst->cs, "Failed checking %s: %s", thread->filename_work, fr_syserror(errno));
711 return -1;
712 }
713
714 thread->file_size = buf.st_size;
715 } else {
716 /*
717 * Avoid triggering erroneous EOF.
718 */
719 thread->file_size = 1;
720 }
721
722 fr_assert(thread->name == NULL);
723 fr_assert(thread->filename_work != NULL);
724 thread->name = talloc_typed_asprintf(thread, "detail_work reading file %s", thread->filename_work);
725
726 /*
727 * Linux doesn't like us adding write callbacks for FDs
728 * which reference files. Since the callback is only
729 * used when the FD blocks, and files don't (mostly)
730 * block, we just mark this read-only.
731 *
732 * The code in src/lib/io/network.c will call the
733 * mod_write() callback on any write, even if the
734 * listener is marked "read_only"
735 */
736 li->listen->no_write_callback = true;
737
738 return 0;
739}
740
741
743{
745
746 /*
747 * One less worker... we check for "0" because of the
748 * hacks in proto_detail which let us start up with
749 * "transport = work" for debugging purposes.
750 */
751 if (thread->file_parent) {
752 pthread_mutex_lock(&thread->file_parent->worker_mutex);
753 if (thread->file_parent->num_workers > 0) thread->file_parent->num_workers--;
754 pthread_mutex_unlock(&thread->file_parent->worker_mutex);
755 }
756
757 DEBUG("Closing %sdetail worker file %s", thread->outstanding == 0 ? "and deleting " : "", thread->name);
758
759#ifdef NOTE_REVOKE
761#endif
762 fr_event_fd_delete(thread->el, thread->fd, FR_EVENT_FILTER_IO);
763
764 if (thread->outstanding == 0) unlink(thread->filename_work);
765
766 close(thread->fd);
767 thread->fd = -1;
768
769 if (inst->parent->exit_when_done) {
770 INFO("Done reading detail files, process will now exit");
771
772 /*
773 * The least hacky way of signalling that the server
774 * should exit. This is an almost identical code
775 * path as receiving a SIGTERM.
776 */
778 }
779
780 return 0;
781}
782
783
784/** Close a detail listener
785 *
786 */
788{
789 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
790
791 return mod_close_internal(thread);
792}
793
794#ifdef NOTE_REVOKE
795static void mod_revoke(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
796{
797 proto_detail_work_thread_t *thread = talloc_get_type_abort(uctx, proto_detail_work_thread_t);
798
799 /*
800 * The underlying file system is gone. Stop reading the
801 * file, destroy all of the IO handlers, and delete everything.
802 */
803 DEBUG("Detail worker %s had file system unmounted. Stopping.", thread->name);
804 mod_close_internal(thread);
805}
806#endif
807
808
809/** Set the event list for a new IO instance
810 *
811 * @param[in] li the listener
812 * @param[in] el the event list
813 * @param[in] nr context from the network side
814 */
816{
817 proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
818
819#ifdef NOTE_REVOKE
821
822 memset(&funcs, 0, sizeof(funcs));
823 funcs.revoke = mod_revoke;
824
825 if (fr_event_filter_insert(thread, NULL, el, thread->fd, FR_EVENT_FILTER_VNODE, &funcs, NULL, thread) < 0) {
826 WARN("Failed to add event watching for unmounted file system");
827 }
828#endif
829
830 thread->el = el;
831 thread->nr = nr;
832}
833
834
835static char const *mod_name(fr_listen_instance_t *li)
836
837{ proto_detail_work_thread_t *thread = talloc_get_type_abort(li->thread_instance, proto_detail_work_thread_t);
838
839 return thread->name;
840}
841
842static int mod_instantiate(module_inst_ctx_t const *mctx)
843{
844 proto_detail_work_t *inst = talloc_get_type_abort(mctx->mi->data, proto_detail_work_t);
845 fr_client_t *client;
846 CONF_SECTION *cs = mctx->mi->conf;
847 module_instance_t const *mi = mctx->mi;
848
849 inst->parent = talloc_get_type_abort(mi->parent->data, proto_detail_t);
850 inst->cs = cs;
851
852 if (inst->track_progress) {
853 inst->mode = O_RDWR;
854 } else {
855 inst->mode = O_RDONLY;
856 }
857
858 if (inst->retransmit) {
859 FR_TIME_DELTA_BOUND_CHECK("limit.initial_rtx_time", inst->retry_config.irt, >=, fr_time_delta_from_sec(1));
860 FR_TIME_DELTA_BOUND_CHECK("limit.initial_rtx_time", inst->retry_config.irt, <=, fr_time_delta_from_sec(60));
861
862 /*
863 * If you need more than this, just set it to
864 * "0", and check Packet-Transmit-Count manually.
865 */
866 FR_INTEGER_BOUND_CHECK("limit.max_rtx_count", inst->retry_config.mrc, <=, 20);
867 FR_TIME_DELTA_BOUND_CHECK("limit.max_rtx_duration", inst->retry_config.mrd, <=, fr_time_delta_from_sec(600));
868
869 /*
870 * This is a reasonable value.
871 */
872 FR_TIME_DELTA_BOUND_CHECK("limit.max_rtx_timer", inst->retry_config.mrt, <=, fr_time_delta_from_sec(30));
873 }
874
875 FR_INTEGER_BOUND_CHECK("limit.max_outstanding", inst->max_outstanding, >=, 1);
876
877 MEM(client = inst->client = talloc_zero(inst, fr_client_t));
878
879 client->ipaddr.af = AF_INET;
880 client->ipaddr.addr.v4.s_addr = htonl(INADDR_NONE);
881 client->src_ipaddr = client->ipaddr;
882
883 client->longname = client->shortname = client->secret = inst->filename_work;
884 client->nas_type = talloc_strdup(client, "other");
885
886 return 0;
887}
888
889/** Private interface for use by proto_detail_file
890 *
891 */
894 .common = {
895 .magic = MODULE_MAGIC_INIT,
896 .name = "detail_work",
898 .inst_size = sizeof(proto_detail_work_t),
899 .thread_inst_size = sizeof(proto_detail_work_thread_t),
900 .instantiate = mod_instantiate
901 },
902 .default_message_size = 65536,
903 .default_reply_size = 32,
904
905 .open = mod_open,
906 .close = mod_close,
907 .read = mod_read,
908 .decode = mod_decode,
909 .write = mod_write,
910 .event_list_set = mod_event_list_set,
911 .get_name = mod_name,
912};
static int const char char buffer[256]
Definition acutest.h:641
module_t common
Common fields to all loadable modules.
Definition app_io.h:34
Public structure describing an I/O path for a protocol.
Definition app_io.h:33
static int packet_ctx
the tests pass only the address of packet_ctx
static uint8_t request[4]
#define STRINGIFY(x)
Definition build.h:233
#define UNUSED
Definition build.h:415
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:722
#define FR_INTEGER_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:572
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition cf_parse.h:466
@ CONF_FLAG_SUBSECTION
Instead of putting the information into a configuration structure, the configuration file routines MA...
Definition cf_parse.h:460
#define FR_CONF_POINTER(_name, _type, _flags, _res_p)
conf_parser_t which parses a single CONF_PAIR producing a single global result
Definition cf_parse.h:350
#define FR_CONF_OFFSET_FLAGS(_name, _flags, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:268
#define FR_TIME_DELTA_BOUND_CHECK(_name, _var, _op, _bound)
Definition cf_parse.h:583
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:340
static uint64_t max_outstanding
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define DEBUG(fmt,...)
Definition dhcpclient.c:38
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:316
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:329
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:335
Specifies an attribute which must be present for the module to function.
Definition dict.h:315
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:328
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
#define fr_dlist_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:242
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
static void * fr_dlist_remove(fr_dlist_head_t *list_head, void *ptr)
Remove an item from the list.
Definition dlist.h:620
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:360
Entry in a doubly linked list.
Definition dlist.h:41
#define fr_event_filter_update(...)
Definition event.h:239
#define fr_event_filter_insert(...)
Definition event.h:234
#define FR_EVENT_RESUME(_s, _f)
Re-add the filter for a func from kevent.
Definition event.h:131
#define FR_EVENT_SUSPEND(_s, _f)
Temporarily remove the filter for a func from kevent.
Definition event.h:115
@ FR_EVENT_FILTER_VNODE
Filter for vnode subfilters.
Definition event.h:84
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:83
Callbacks for the FR_EVENT_FILTER_IO filter.
Definition event.h:188
Structure describing a modification to a filter's state.
Definition event.h:96
Callbacks for the FR_EVENT_FILTER_VNODE filter.
Definition event.h:195
talloc_free(hp)
int af
Address family.
Definition inet.h:64
union fr_ipaddr_t::@160 addr
void fr_network_listen_read(fr_network_t *nr, fr_listen_instance_t *li)
Signal the network to read from a listener.
Definition network.c:287
fr_ipaddr_t ipaddr
IPv4/IPv6 address of the host.
Definition client.h:83
char const * secret
Secret PSK.
Definition client.h:90
fr_ipaddr_t src_ipaddr
IPv4/IPv6 address to send responses from (family must match ipaddr).
Definition client.h:84
char const * nas_type
Type of client (arbitrary).
Definition client.h:125
char const * longname
Client identifier.
Definition client.h:87
char const * shortname
Client nickname.
Definition client.h:88
Describes a host allowed to send packets to the server.
Definition client.h:80
#define fr_time()
Definition event.c:58
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1204
Stores all information relating to an event list.
Definition event.c:375
fr_listen_t * listen
the listener this is a socket of.
Definition listen.h:65
bool no_write_callback
sometimes we don't need to do writes
Definition listen.h:43
int fd
file descriptor for this socket - set by open
Definition listen.h:67
void const * app_io_instance
I/O path configuration context.
Definition listen.h:34
void * thread_instance
thread / socket context
Definition listen.h:69
void main_loop_signal_raise(int flag)
Definition main_loop.c:78
@ RADIUS_SIGNAL_SELF_TERM
Definition main_loop.h:37
@ FR_TYPE_UINT32
32 Bit unsigned integer.
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
Detail master protocol handler.
struct proto_detail_work_s proto_detail_work_t
size_t last_search
where we last searched in the buffer MUST be offset, as the buffers can change.
proto_detail_work_thread_t * file_parent
thread instance of the directory reader that spawned us
bool paused
Is reading paused?
int count
number of packets we read from this file.
unsigned int last_line
line number of the last record read.
bool eof
are we at EOF on reading?
char const * name
debug name for printing
int num_workers
number of workers
uint16_t max_outstanding
number of packets to run in parallel
fr_event_list_t * el
for various timers
off_t file_size
size of the file
pthread_mutex_t worker_mutex
for the workers
char const * filename_work
work file name
off_t header_offset
offset of the current header we're reading
fr_listen_instance_t * li
talloc_parent() is slow
proto_detail_work_t const * inst
instance data
fr_dlist_head_t list
for retransmissions
int fd
file descriptor
bool closing
we should be closing the file
off_t read_offset
where we're reading from in filename_work
uint32_t outstanding
number of currently outstanding records;
fr_network_t * nr
for Linux-specific callbacks
fr_dict_attr_autoload_t proto_detail_work_dict_attr[]
static const conf_parser_t file_listen_config[]
#define MPRINT
static int mod_decode(void const *instance, request_t *request, UNUSED uint8_t *const data, UNUSED size_t data_len)
static fr_dict_t const * dict_freeradius
static void work_retransmit(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
static int mod_open(fr_listen_instance_t *li)
Open a detail listener.
proto_detail_work_thread_t * parent
talloc_parent is SLOW!
fr_dlist_t entry
for the retransmission list
static int mod_close(fr_listen_instance_t *li)
Close a detail listener.
size_t packet_len
for retransmissions
fr_retry_t retry
our retry timers
uint8_t * packet
for retransmissions
fr_time_t timestamp
when we read the entry.
static ssize_t mod_read(fr_listen_instance_t *li, void **packet_ctx, fr_time_t *recv_time_p, uint8_t *buffer, size_t buffer_len, size_t *leftover)
static void mod_event_list_set(fr_listen_instance_t *li, fr_event_list_t *el, void *nr)
Set the event list for a new IO instance.
fr_dict_autoload_t proto_detail_work_dict[]
static char const * mod_name(fr_listen_instance_t *li)
static fr_event_update_t pause_read[]
static conf_parser_t limit_config[]
fr_timer_t * ev
retransmission timer
static ssize_t mod_write(fr_listen_instance_t *li, void *packet_ctx, UNUSED fr_time_t request_time, uint8_t *buffer, size_t buffer_len, UNUSED size_t written)
fr_app_io_t proto_detail_work
Private interface for use by proto_detail_file.
static int mod_close_internal(proto_detail_work_thread_t *thread)
int id
for retransmission counters
off_t done_offset
where we're tracking the status
static fr_dict_attr_t const * attr_packet_transmit_counter
static int mod_instantiate(module_inst_ctx_t const *mctx)
static fr_event_update_t resume_read[]
#define fr_assert(_expr)
Definition rad_assert.h:37
#define pair_update_request(_attr, _da)
#define WARN(fmt,...)
static bool done
Definition radclient.c:80
#define INFO(fmt,...)
Definition radict.c:67
#define REQUEST_VERIFY(_x)
Definition request.h:310
static conf_parser_t retry_config[]
Definition rlm_tacacs.c:38
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:355
void * data
Module's instance data.
Definition module.h:297
module_instance_t const * parent
Parent module's instance (if any).
Definition module.h:363
conf_parser_t const * config
How to convert a CONF_SECTION to a module instance.
Definition module.h:207
Module instance data.
Definition module.h:291
eap_aka_sim_process_conf_t * inst
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...)
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:621
#define talloc_get_type_abort_const
Definition talloc.h:117
#define talloc_strdup(_ctx, _str)
Definition talloc.h:149
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:599
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_eq(_a, _b)
Definition time.h:241
static int64_t fr_time_delta_to_sec(fr_time_delta_t delta)
Definition time.h:656
static double fr_time_delta_to_sec_double(fr_time_delta_t delta)
Seconds as a double, for printing and for arithmetic which need not be exact.
Definition time.h:663
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:49
A timer event.
Definition timer.c:83
#define fr_timer_at(...)
Definition timer.h:81
static fr_event_list_t * el
fr_retry_state_t fr_retry_next(fr_retry_t *r, fr_time_t now)
Initialize a retransmission counter.
Definition retry.c:110
void fr_retry_init(fr_retry_t *r, fr_time_t now, fr_retry_config_t const *config)
Initialize a retransmission counter.
Definition retry.c:36
fr_time_t start
when we started the retransmission
Definition retry.h:53
fr_time_delta_t rt
retransmit interval
Definition retry.h:57
uint32_t count
number of sent packets
Definition retry.h:58
fr_retry_state_t
Definition retry.h:45
@ FR_RETRY_MRC
reached maximum retransmission count
Definition retry.h:47
@ FR_RETRY_MRD
reached maximum retransmission duration
Definition retry.h:48
fr_time_t next
when the next timer should be set
Definition retry.h:55
static fr_slen_t data
Definition value.h:1366