xref: /freebsd/contrib/ntp/libntp/lib_strbuf.c (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
1c0b746e5SOllivier Robert /*
2*f5f40dd6SCy Schubert  * lib_strbuf.c - init_lib() and library string storage
3c0b746e5SOllivier Robert  */
42b15cb3dSCy Schubert #ifdef HAVE_CONFIG_H
52b15cb3dSCy Schubert #include <config.h>
62b15cb3dSCy Schubert #endif
7c0b746e5SOllivier Robert 
8*f5f40dd6SCy Schubert #include <isc/mutex.h>
92b15cb3dSCy Schubert #include <isc/net.h>
102b15cb3dSCy Schubert #include <isc/result.h>
112b15cb3dSCy Schubert 
122b15cb3dSCy Schubert #include "ntp_fp.h"
13224ba2bdSOllivier Robert #include "ntp_stdlib.h"
14c0b746e5SOllivier Robert #include "lib_strbuf.h"
15c0b746e5SOllivier Robert 
16*f5f40dd6SCy Schubert #define LIB_NUMBUF	10
172b15cb3dSCy Schubert 
18c0b746e5SOllivier Robert /*
19c0b746e5SOllivier Robert  * Storage declarations
20c0b746e5SOllivier Robert  */
21*f5f40dd6SCy Schubert static char		lib_stringbuf_storage[LIB_NUMBUF][LIB_BUFLENGTH];
22*f5f40dd6SCy Schubert static char *		lib_stringbuf[LIB_NUMBUF];
23*f5f40dd6SCy Schubert int			lib_inited;
24*f5f40dd6SCy Schubert static isc_mutex_t	lib_mutex;
252b15cb3dSCy Schubert int			ipv4_works;
262b15cb3dSCy Schubert int			ipv6_works;
27*f5f40dd6SCy Schubert int			debug;
28c0b746e5SOllivier Robert 
29c0b746e5SOllivier Robert /*
30c0b746e5SOllivier Robert  * initialization routine.  Might be needed if the code is ROMized.
31c0b746e5SOllivier Robert  */
32c0b746e5SOllivier Robert void
33c0b746e5SOllivier Robert init_lib(void)
34c0b746e5SOllivier Robert {
35*f5f40dd6SCy Schubert 	u_int	u;
36*f5f40dd6SCy Schubert 
37*f5f40dd6SCy Schubert 	if (lib_inited) {
382b15cb3dSCy Schubert 		return;
39*f5f40dd6SCy Schubert 	}
402b15cb3dSCy Schubert 	ipv4_works = (ISC_R_SUCCESS == isc_net_probeipv4());
412b15cb3dSCy Schubert 	ipv6_works = (ISC_R_SUCCESS == isc_net_probeipv6());
422b15cb3dSCy Schubert 	init_systime();
43*f5f40dd6SCy Schubert 	/*
44*f5f40dd6SCy Schubert 	 * Avoid -Wrestrict warnings by keeping a pointer to each buffer
45*f5f40dd6SCy Schubert 	 * so the compiler can see copying from one buffer to another is
46*f5f40dd6SCy Schubert 	 * not violating restrict qualifiers on, e.g. memcpy() args.
47*f5f40dd6SCy Schubert 	 */
48*f5f40dd6SCy Schubert 	for (u = 0; u < COUNTOF(lib_stringbuf); u++) {
49*f5f40dd6SCy Schubert 		lib_stringbuf[u] = lib_stringbuf_storage[u];
50*f5f40dd6SCy Schubert 	}
51*f5f40dd6SCy Schubert 	isc_mutex_init(&lib_mutex);
522b15cb3dSCy Schubert 	lib_inited = TRUE;
53c0b746e5SOllivier Robert }
54*f5f40dd6SCy Schubert 
55*f5f40dd6SCy Schubert 
56*f5f40dd6SCy Schubert char *
57*f5f40dd6SCy Schubert lib_getbuf(void)
58*f5f40dd6SCy Schubert {
59*f5f40dd6SCy Schubert 	static int	lib_nextbuf;
60*f5f40dd6SCy Schubert 	int		mybuf;
61*f5f40dd6SCy Schubert 
62*f5f40dd6SCy Schubert 	if (!lib_inited) {
63*f5f40dd6SCy Schubert 		init_lib();
64*f5f40dd6SCy Schubert 	}
65*f5f40dd6SCy Schubert 	isc_mutex_lock(&lib_mutex);
66*f5f40dd6SCy Schubert 	mybuf = lib_nextbuf;
67*f5f40dd6SCy Schubert 	lib_nextbuf = (1 + mybuf) % COUNTOF(lib_stringbuf);
68*f5f40dd6SCy Schubert 	isc_mutex_unlock(&lib_mutex);
69*f5f40dd6SCy Schubert 	zero_mem(lib_stringbuf[mybuf], LIB_BUFLENGTH);
70*f5f40dd6SCy Schubert 
71*f5f40dd6SCy Schubert 	return lib_stringbuf[mybuf];
72*f5f40dd6SCy Schubert }