summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnsl/nis/cache/cache.h
blob: 00637f84c99154475cefe029d053018795061520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_CACHE_H
#define	_CACHE_H

#pragma ident	"%Z%%M%	%I%	%E% SMI"


#ifdef __cplusplus
extern "C" {
#endif


#include <stdlib.h>
#include "../gen/nis_local.h"
#include "../gen/nis_clnt.h"
#include "nsl_stdio_prv.h"

#define	CLIENT_FILE	"/var/nis/client_info"

#define	PREF_ALL_VAL	0
#define	PREF_ONLY_VAL	1
#define	PREF_ALL	"all"
#define	PREF_ONLY	"pref_only"
#define	PREF_SRVR	"pref_srvr"
#define	PREF_TYPE	"pref_type"

#define	ONE_HOUR	(1 * 60 * 60)

// Semaphores for cache manager
#define	NIS_SEM_W_KEY 100303	// key for the writer semaphore
#define	NIS_W_NSEMS   1		// number of semaphores in the writer array

// in the writer array
#define	NIS_SEM_MGR_UP   0	// is the cache manager running?

#define	SEM_OWNER_READ	0400
#define	SEM_OWNER_ALTER	0200
#define	SEM_GROUP_READ	0040
#define	SEM_GROUP_ALTER	0020
#define	SEM_OTHER_READ	0004
#define	SEM_OTHER_ALTER	0002

extern int __nis_debuglevel;

struct HostEnt {
	char *name;
	char *interface;
	int rank;
	HostEnt *next;
};

class HostList {
    public:
	HostList();
	~HostList();
	void addHost(char *name, char *interface, int rank);
	int checkHost(char *value, char *interface, int *rank);
	int matchHost(char *name, char *uaddr, int *rank);
	int serves(directory_obj *dobj);
	int dumpList(__NSL_FILE *fp);
	void addOption(int value);
	void dumpOption(__NSL_FILE *fp);
	void deleteBackupList();
	void deleteList();
	void backupList();
	void restoreList();

	int pref_option;

	void *operator new(size_t bytes) { return calloc(1, bytes); }
	void operator delete(void *arg) { free(arg); }
    private:
	HostEnt *entries, *old_entries;
	int	old_pref_option;
};

class NisCache {
    public:
	NisCache();
	~NisCache();

	// This set of routines is the primary interface to the
	// cache classes.  The NisCache class provides default
	// implementations of them.  The derived classes override
	// them if necessary.

	virtual int okay();
	virtual nis_error searchDir(char *dname,
		    nis_bound_directory **binding, int near);
	virtual nis_error bindReplica(char *dname,
		    nis_bound_directory **binding);
	virtual nis_error bindMaster(char *dname,
		    nis_bound_directory **binding);
	virtual nis_error bindServer(nis_server *srv, int nsrv,
		    nis_bound_directory **binding);
	virtual int refreshBinding(nis_bound_directory *binding);
	virtual int refreshAddress(nis_bound_endpoint *bep);
	virtual int refreshCallback(nis_bound_endpoint *bep);
	virtual bool_t readColdStart();
	virtual bool_t readServerColdStart(uint32_t *exp_time);
	virtual void print();

	virtual void addBinding(nis_bound_directory *binding);
	virtual void removeBinding(nis_bound_directory *binding);

	virtual void activeAdd(nis_active_endpoint *act);
	virtual void activeRemove(endpoint *ep, int all);
	virtual int activeCheck(endpoint *ep);
	virtual int activeGet(endpoint *ep, nis_active_endpoint **act);

	virtual int getStaleEntries(nis_bound_directory ***binding);
	virtual int getAllEntries(nis_bound_directory ***binding);
	virtual int getAllActive(nis_active_endpoint ***actlist);
	virtual void addAddresses(nis_bound_directory *binding);
	virtual uint32_t loadPreferredServers();
	virtual uint32_t refreshCache();
	virtual int checkUp();

	virtual int resetBinding(nis_bound_directory *binding);


	// This is a set of helper routines for derived classes to
	// use.

	void printBinding(nis_bound_directory *binding);
	void printBinding_exptime(nis_bound_directory *binding,
							uint32_t exptime);
	void printActive(nis_active_endpoint *act);
	nis_error loadDirectory(char *dname);
	nis_error loadDirectory(char *dname, int force_finddir);
	nis_active_endpoint *activeDup(nis_active_endpoint *src);
	void activeFree(nis_active_endpoint *act);
	void *packBinding(nis_bound_directory *binding, int *len);
	nis_bound_directory *unpackBinding(void *data, int len);
	void *packActive(nis_active_endpoint *act, int *len);
	nis_active_endpoint *unpackActive(void *data, int len);
	void sortServers(nis_bound_directory *binding);
	nis_error pingServers(nis_server *srv, int nsrv, int quick);
	uint32_t expireTime(uint32_t ttl);
	int nextGeneration();
	void rerankServers();
	void writePreference(__NSL_FILE *fp);
	void mergePreference(char *value);
	void mergeOption(char *value);
	void resetPreference();
	void backupPreference();
	void delBackupPref();
	void restorePreference();
	uint32_t loadDotFile();

	nis_error createBinding(fd_result *res);
	nis_error createBinding(directory_obj *res);

	char *coldStartDir();

	// We define a new and delete routine so that we don't
	// depend on libC at run time.

	void *operator new(size_t bytes) { return calloc(1, bytes); }
	void operator delete(void *arg) { free(arg); }

    private:
	mutex_t gen_lock;
	int gen;
	char *cold_start_dir;
	HostList prefer;

	void extractAddresses(nis_bound_directory *binding);
	void printDirectorySpecial(directory_obj *dobj);
	int rankServer(nis_server *srv, endpoint *ep, void *local_interfaces);
};

#ifdef	__cplusplus
}
#endif

#endif	/* _CACHE_H */