summaryrefslogtreecommitdiffstats
path: root/contrib/bind/lib/irs/irp_pr.c
blob: 1de304e74be7dabef66dc1f9f371a0555f415d0d (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * Portions Copyright (c) 1996 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$Id: irp_pr.c,v 8.1 1999/01/18 07:46:54 vixie Exp $";
#endif /* LIBC_SCCS and not lint */

/* extern */

#include "port_before.h"

#include <syslog.h>
#include <sys/types.h>

#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <syslog.h>

#include <irs.h>
#include <irp.h>
#include <isc/memcluster.h>
#include <isc/irpmarshall.h>

#include "irs_p.h"
#include "lcl_p.h"
#include "irp_p.h"

#include "port_after.h"


#define MAXALIASES	35

/* Types */

struct pvt {
	struct irp_p	       *girpdata;
	int			warned;
	struct protoent		proto;
};

/* Forward */

static void			pr_close(struct irs_pr *);
static struct protoent *	pr_next(struct irs_pr *);
static struct protoent *	pr_byname(struct irs_pr *, const char *);
static struct protoent *	pr_bynumber(struct irs_pr *, int);
static void			pr_rewind(struct irs_pr *);
static void			pr_minimize(struct irs_pr *);

static void			free_proto(struct protoent *pr);

/* Public */



/*
 * struct irs_pr * irs_irp_pr(struct irs_acc *this)
 *
 */

struct irs_pr *
irs_irp_pr(struct irs_acc *this) {
	struct irs_pr *pr;
	struct pvt *pvt;

	if (!(pr = memget(sizeof *pr))) {
		errno = ENOMEM;
		return (NULL);
	}
	memset(pr, 0x0, sizeof *pr);

	if (!(pvt = memget(sizeof *pvt))) {
		memput(pr, sizeof *pr);
		errno = ENOMEM;
		return (NULL);
	}
	memset(pvt, 0, sizeof *pvt);
	pvt->girpdata = this->private;

	pr->private = pvt;
	pr->close = pr_close;
	pr->byname = pr_byname;
	pr->bynumber = pr_bynumber;
	pr->next = pr_next;
	pr->rewind = pr_rewind;
	pr->minimize = pr_minimize;
	return (pr);
}

/* Methods */



/*
 * void pr_close(struct irs_pr *this)
 *
 */

static void
pr_close(struct irs_pr *this) {
	struct pvt *pvt = (struct pvt *)this->private;

	pr_minimize(this);

	free_proto(&pvt->proto);

	memput(pvt, sizeof *pvt);
	memput(this, sizeof *this);
}



/*
 * struct protoent * pr_byname(struct irs_pr *this, const char *name)
 *
 */

static struct protoent *
pr_byname(struct irs_pr *this, const char *name) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct protoent *pr = &pvt->proto;
	char *body = NULL;
	size_t bodylen;
	int code;
	int i;
	char text[256];

	if (pr->p_name != NULL && strcmp(name, pr->p_name) == 0) {
		return (pr);
	}

	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
		return (NULL);
	}

	i = irs_irp_send_command(pvt->girpdata, "getprotobyname %s", name);
	if (i != 0)
		return (NULL);

	if (irs_irp_get_full_response(pvt->girpdata, &code,
				      text, sizeof text,
				      &body, &bodylen) != 0) {
		return (NULL);
	}

	if (code == IRPD_GETPROTO_OK) {
		free_proto(pr);
		if (irp_unmarshall_pr(pr, body) != 0) {
			pr = NULL;
		}
	} else {
		pr = NULL;
	}

	if (body != NULL) {
		memput(body, bodylen);
	}

	return (pr);
}



/*
 * struct protoent * pr_bynumber(struct irs_pr *this, int proto)
 *
 */

static struct protoent *
pr_bynumber(struct irs_pr *this, int proto) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct protoent *pr = &pvt->proto;
	char *body = NULL;
	size_t bodylen;
	int code;
	int i;
	char text[256];

	if (pr->p_name != NULL && proto == pr->p_proto) {
		return (pr);
	}

	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
		return (NULL);
	}

	i = irs_irp_send_command(pvt->girpdata, "getprotobynumber %d", proto);
	if (i != 0)
		return (NULL);

	if (irs_irp_get_full_response(pvt->girpdata, &code,
				      text, sizeof text,
				      &body, &bodylen) != 0) {
		return (NULL);
	}

	if (code == IRPD_GETPROTO_OK) {
		free_proto(pr);
		if (irp_unmarshall_pr(pr, body) != 0) {
			pr = NULL;
		}
	} else {
		pr = NULL;
	}

	if (body != NULL) {
		memput(body, bodylen);
	}

	return (pr);
}




/*
 * void pr_rewind(struct irs_pr *this)
 *
 */

static void
pr_rewind(struct irs_pr *this) {
	struct pvt *pvt = (struct pvt *)this->private;
	char text[256];
	int code;

	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
		return;
	}

	if (irs_irp_send_command(pvt->girpdata, "setprotoent") != 0) {
		return;
	}

	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
	if (code != IRPD_GETPROTO_SETOK) {
		if (irp_log_errors) {
			syslog(LOG_WARNING, "setprotoent failed: %s", text);
		}
	}

	return;
}




/*
 * struct protoent * pr_next(struct irs_pr *this)
 *
 * Notes:
 *
 *	Prepares the cache if necessary and returns the next item in it.
 *
 */

static struct protoent *
pr_next(struct irs_pr *this) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct protoent *pr = &pvt->proto;
	char *body;
	size_t bodylen;
	int code;
	char text[256];

	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
		return (NULL);
	}

	if (irs_irp_send_command(pvt->girpdata, "getprotoent") != 0) {
		return (NULL);
	}

	if (irs_irp_get_full_response(pvt->girpdata, &code,
				      text, sizeof text,
				      &body, &bodylen) != 0) {
		return (NULL);
	}

	if (code == IRPD_GETPROTO_OK) {
		free_proto(pr);
		if (irp_unmarshall_pr(pr, body) != 0) {
			pr = NULL;
		}
	} else {
		pr = NULL;
	}

	if (body != NULL) {
		memput(body, bodylen);
	}

	return (pr);
}




/*
 * void pr_minimize(struct irs_pr *this)
 *
 */

static void
pr_minimize(struct irs_pr *this) {
	struct pvt *pvt = (struct pvt *)this->private;

	irs_irp_disconnect(pvt->girpdata);
}






/*
 * static void free_proto(struct protoent *pw);
 *
 *	Deallocate all the memory irp_unmarshall_pr allocated.
 *
 */

static void
free_proto(struct protoent *pr) {
	char **p;

	if (pr == NULL)
		return;

	if (pr->p_name != NULL)
		free(pr->p_name);

	for (p = pr->p_aliases ; p != NULL && *p != NULL ; p++)
		free(*p);
}
OpenPOWER on IntegriCloud