aboutsummaryrefslogtreecommitdiff
path: root/contrib/bind9/lib/bind/irs/getprotoent.c
blob: 9e3d77589294c3c2ee1ebf17d6b58d5485770e54 (plain) (blame)
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
/*
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1996,1999 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 ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC 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(LINT) && !defined(CODECENTER)
static const char rcsid[] = "$Id: getprotoent.c,v 1.3.18.1 2005/04/27 05:00:58 sra Exp $";
#endif

/* Imports */

#include "port_before.h"

#if !defined(__BIND_NOSTATIC)

#include <sys/types.h>

#include <netinet/in.h>
#include <arpa/nameser.h>

#include <errno.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>

#include <irs.h>

#include "port_after.h"

#include "irs_data.h"

/* Forward */

static struct net_data *init(void);

/* Public */

struct protoent *
getprotoent() {
	struct net_data *net_data = init();

	return (getprotoent_p(net_data));
}

struct protoent *
getprotobyname(const char *name) {
	struct net_data *net_data = init();

	return (getprotobyname_p(name, net_data));
}

struct protoent *
getprotobynumber(int proto) {
	struct net_data *net_data = init();

	return (getprotobynumber_p(proto, net_data));
}

void
setprotoent(int stayopen) {
	struct net_data *net_data = init();

	setprotoent_p(stayopen, net_data);
}

void
endprotoent() {
	struct net_data *net_data = init();

	endprotoent_p(net_data);
}

/* Shared private. */

struct protoent *
getprotoent_p(struct net_data *net_data) {
	struct irs_pr *pr;

	if (!net_data || !(pr = net_data->pr))
		return (NULL);
	net_data->pr_last = (*pr->next)(pr);
	return (net_data->pr_last);
}

struct protoent *
getprotobyname_p(const char *name, struct net_data *net_data) {
	struct irs_pr *pr;
	char **pap;

	if (!net_data || !(pr = net_data->pr))
		return (NULL);
	if (net_data->pr_stayopen && net_data->pr_last) {
		if (!strcmp(net_data->pr_last->p_name, name))
			return (net_data->pr_last);
		for (pap = net_data->pr_last->p_aliases; pap && *pap; pap++)
			if (!strcmp(name, *pap))
				return (net_data->pr_last);
	}
	net_data->pr_last = (*pr->byname)(pr, name);
	if (!net_data->pr_stayopen)
		endprotoent();
	return (net_data->pr_last);
}

struct protoent *
getprotobynumber_p(int proto, struct net_data *net_data) {
	struct irs_pr *pr;

	if (!net_data || !(pr = net_data->pr))
		return (NULL);
	if (net_data->pr_stayopen && net_data->pr_last)
		if (net_data->pr_last->p_proto == proto)
			return (net_data->pr_last);
	net_data->pr_last = (*pr->bynumber)(pr, proto);
	if (!net_data->pr_stayopen)
		endprotoent();
	return (net_data->pr_last);
}

void
setprotoent_p(int stayopen, struct net_data *net_data) {
	struct irs_pr *pr;

	if (!net_data || !(pr = net_data->pr))
		return;
	(*pr->rewind)(pr);
	net_data->pr_stayopen = (stayopen != 0);
	if (stayopen == 0)
		net_data_minimize(net_data);
}

void
endprotoent_p(struct net_data *net_data) {
	struct irs_pr *pr;

	if ((net_data != NULL) && ((pr = net_data->pr) != NULL))
		(*pr->minimize)(pr);
}

/* Private */

static struct net_data *
init() {
	struct net_data *net_data;

	if (!(net_data = net_data_init(NULL)))
		goto error;
	if (!net_data->pr) {
		net_data->pr = (*net_data->irs->pr_map)(net_data->irs);

		if (!net_data->pr || !net_data->res) {
 error:		
			errno = EIO;
			return (NULL);
		}
		(*net_data->pr->res_set)(net_data->pr, net_data->res, NULL);
	}
	
	return (net_data);
}

#endif /*__BIND_NOSTATIC*/

/*! \file */