xref: /netbsd-src/external/bsd/ppp/dist/pppd/plugins/minconn.c (revision 0f0dec65909b0e94c58491b896daf823c738d41d)
1a619718eSchristos /*
2a619718eSchristos  * minconn.c - pppd plugin to implement a `minconnect' option.
3a619718eSchristos  *
4*0f0dec65Schristos  * Copyright (c) 1999-2024 Paul Mackerras. All rights reserved.
5a619718eSchristos  *
6a619718eSchristos  * Redistribution and use in source and binary forms, with or without
7a619718eSchristos  * modification, are permitted provided that the following conditions
8a619718eSchristos  * are met:
9a619718eSchristos  *
10a619718eSchristos  * 1. Redistributions of source code must retain the above copyright
11a619718eSchristos  *    notice, this list of conditions and the following disclaimer.
12a619718eSchristos  *
13a619718eSchristos  * 2. Redistributions in binary form must reproduce the above copyright
14a619718eSchristos  *    notice, this list of conditions and the following disclaimer in
15a619718eSchristos  *    the documentation and/or other materials provided with the
16a619718eSchristos  *    distribution.
17a619718eSchristos  *
18a619718eSchristos  * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
19a619718eSchristos  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
20a619718eSchristos  * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
21a619718eSchristos  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
22a619718eSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
23a619718eSchristos  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
24a619718eSchristos  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25a619718eSchristos  */
26*0f0dec65Schristos 
27a619718eSchristos #include <stddef.h>
28a619718eSchristos #include <time.h>
29*0f0dec65Schristos #include <stdint.h>
30*0f0dec65Schristos #include <stdbool.h>
31*0f0dec65Schristos #include <stdarg.h>
32*0f0dec65Schristos #include <sys/types.h>
33a619718eSchristos 
34*0f0dec65Schristos #include <pppd/pppd.h>
35*0f0dec65Schristos #include <pppd/options.h>
36*0f0dec65Schristos 
37*0f0dec65Schristos #if !defined(SOL2)
38*0f0dec65Schristos #include <linux/ppp_defs.h>
39*0f0dec65Schristos #else
40*0f0dec65Schristos #include <net/ppp_defs.h>
41*0f0dec65Schristos #endif
42*0f0dec65Schristos 
43*0f0dec65Schristos char pppd_version[] = PPPD_VERSION;
44a619718eSchristos 
45a619718eSchristos static int minconnect = 0;
46a619718eSchristos 
47*0f0dec65Schristos static struct option my_options[] = {
48a619718eSchristos 	{ "minconnect", o_int, &minconnect,
49a619718eSchristos 	  "Set minimum connect time before idle timeout applies" },
50a619718eSchristos 	{ NULL }
51a619718eSchristos };
52a619718eSchristos 
53a619718eSchristos static int my_get_idle(struct ppp_idle *idle)
54a619718eSchristos {
55a619718eSchristos 	time_t t;
56a619718eSchristos 
57a619718eSchristos 	if (idle == NULL)
58*0f0dec65Schristos 		return minconnect ? minconnect: ppp_get_max_idle_time();
59a619718eSchristos 	t = idle->xmit_idle;
60a619718eSchristos 	if (idle->recv_idle < t)
61a619718eSchristos 		t = idle->recv_idle;
62*0f0dec65Schristos 	return ppp_get_max_idle_time() - t;
63a619718eSchristos }
64a619718eSchristos 
65a619718eSchristos void plugin_init(void)
66a619718eSchristos {
67a619718eSchristos 	info("plugin_init");
68*0f0dec65Schristos 	ppp_add_options(my_options);
69a619718eSchristos 	idle_time_hook = my_get_idle;
70a619718eSchristos }
71