xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib_strbuf.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: lib_strbuf.c,v 1.6 2024/08/18 20:47:13 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
4*eabc0478Schristos  * lib_strbuf.c - init_lib() and library string storage
5abb0f93cSkardel  */
6abb0f93cSkardel #ifdef HAVE_CONFIG_H
7abb0f93cSkardel #include <config.h>
8abb0f93cSkardel #endif
9abb0f93cSkardel 
10*eabc0478Schristos #include <isc/mutex.h>
11abb0f93cSkardel #include <isc/net.h>
12abb0f93cSkardel #include <isc/result.h>
138585484eSchristos 
148585484eSchristos #include "ntp_fp.h"
15abb0f93cSkardel #include "ntp_stdlib.h"
16abb0f93cSkardel #include "lib_strbuf.h"
17abb0f93cSkardel 
18*eabc0478Schristos #define LIB_NUMBUF	10
198585484eSchristos 
20abb0f93cSkardel /*
21abb0f93cSkardel  * Storage declarations
22abb0f93cSkardel  */
23*eabc0478Schristos static char		lib_stringbuf_storage[LIB_NUMBUF][LIB_BUFLENGTH];
24*eabc0478Schristos static char *		lib_stringbuf[LIB_NUMBUF];
25*eabc0478Schristos int			lib_inited;
26*eabc0478Schristos static isc_mutex_t	lib_mutex;
27abb0f93cSkardel int			ipv4_works;
28abb0f93cSkardel int			ipv6_works;
29*eabc0478Schristos int			debug;
30abb0f93cSkardel 
31abb0f93cSkardel /*
32abb0f93cSkardel  * initialization routine.  Might be needed if the code is ROMized.
33abb0f93cSkardel  */
34abb0f93cSkardel void
35abb0f93cSkardel init_lib(void)
36abb0f93cSkardel {
37*eabc0478Schristos 	u_int	u;
38*eabc0478Schristos 
39*eabc0478Schristos 	if (lib_inited) {
408585484eSchristos 		return;
41*eabc0478Schristos 	}
42abb0f93cSkardel 	ipv4_works = (ISC_R_SUCCESS == isc_net_probeipv4());
43abb0f93cSkardel 	ipv6_works = (ISC_R_SUCCESS == isc_net_probeipv6());
448585484eSchristos 	init_systime();
45*eabc0478Schristos 	/*
46*eabc0478Schristos 	 * Avoid -Wrestrict warnings by keeping a pointer to each buffer
47*eabc0478Schristos 	 * so the compiler can see copying from one buffer to another is
48*eabc0478Schristos 	 * not violating restrict qualifiers on, e.g. memcpy() args.
49*eabc0478Schristos 	 */
50*eabc0478Schristos 	for (u = 0; u < COUNTOF(lib_stringbuf); u++) {
51*eabc0478Schristos 		lib_stringbuf[u] = lib_stringbuf_storage[u];
52*eabc0478Schristos 	}
53*eabc0478Schristos 	(void)isc_mutex_init(&lib_mutex);
548585484eSchristos 	lib_inited = TRUE;
55abb0f93cSkardel }
56*eabc0478Schristos 
57*eabc0478Schristos 
58*eabc0478Schristos char *
59*eabc0478Schristos lib_getbuf(void)
60*eabc0478Schristos {
61*eabc0478Schristos 	static int	lib_nextbuf;
62*eabc0478Schristos 	int		mybuf;
63*eabc0478Schristos 
64*eabc0478Schristos 	if (!lib_inited) {
65*eabc0478Schristos 		init_lib();
66*eabc0478Schristos 	}
67*eabc0478Schristos 	isc_mutex_lock(&lib_mutex);
68*eabc0478Schristos 	mybuf = lib_nextbuf;
69*eabc0478Schristos 	lib_nextbuf = (1 + mybuf) % COUNTOF(lib_stringbuf);
70*eabc0478Schristos 	isc_mutex_unlock(&lib_mutex);
71*eabc0478Schristos 	zero_mem(lib_stringbuf[mybuf], LIB_BUFLENGTH);
72*eabc0478Schristos 
73*eabc0478Schristos 	return lib_stringbuf[mybuf];
74*eabc0478Schristos }
75