forked from steve-o/openpgm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetnetbyname.c
297 lines (268 loc) · 6.85 KB
/
getnetbyname.c
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
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
*
* Portable implementation of getnetbyname, returns a network address
* from a given name. On Unix systems the mapping is managed by a
* system service and may be sourced from a network service, cached
* locally, or read directly from a local file. On systems without
* such a service or when IPv6 is required a local file is supported
* with compatible parsing of the network-name mapping format.
*
* Copyright (c) 2010-2011 Miru Limited.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <impl/framework.h>
//#define GETNETBYNAME_DEBUG
/* locals */
#define MAXALIASES 35
#ifndef _WIN32
# define PATH_NETWORKS "/etc/networks"
#else
/* NB: 32-bit applications may read %systemroot%\SysWOW64\drivers\etc */
# define PATH_NETWORKS "%systemroot%\\system32\\drivers\\etc\\networks"
#endif
static FILE* netfh = NULL;
static char line[BUFSIZ+1];
static char *net_aliases[MAXALIASES];
static struct pgm_netent_t net;
static void _pgm_compat_setnetent (void);
static struct pgm_netent_t *_pgm_compat_getnetent (void);
static void _pgm_compat_endnetent (void);
static struct pgm_netent_t* _pgm_compat_getnetbyname (const char*);
static
void
_pgm_compat_setnetent (void)
{
if (NULL == netfh) {
char* netdb;
size_t envlen;
errno_t err;
/* permit path override */
err = pgm_dupenv_s (&netdb, &envlen, "PGM_NETDB");
if (0 != err || 0 == envlen) {
netdb = pgm_strdup (PATH_NETWORKS);
}
#ifdef _WIN32
{
char expanded[MAX_PATH];
if (0 == ExpandEnvironmentStrings ((LPCTSTR)netdb, (LPTSTR)expanded, sizeof (expanded))) {
const DWORD save_errno = GetLastError();
char winstr[1024];
pgm_warn (_("Cannot expand netdb path \"%s\": %s"),
netdb,
pgm_win_strerror (winstr, sizeof (winstr), save_errno));
/* fall through on original string */
} else {
free (netdb);
netdb = pgm_strdup (expanded);
}
}
#endif
err = pgm_fopen_s (&netfh, netdb, "r");
if (0 != err) {
char errbuf[1024];
pgm_warn (_("Opening netdb file \"%s\" failed: %s"),
netdb,
pgm_strerror_s (errbuf, sizeof (errbuf), err));
}
free (netdb);
} else {
rewind (netfh);
}
}
static
void
_pgm_compat_endnetent (void)
{
if (NULL != netfh) {
fclose (netfh);
netfh = NULL;
}
}
/* link-local 169.254.0.0 alias1 alias2
*
* returns address in host-byte order
*/
static
struct pgm_netent_t*
_pgm_compat_getnetent (void)
{
struct in_addr sin;
char *p, *cp, **q;
if (NULL == netfh) {
_pgm_compat_setnetent();
if (NULL == netfh)
return NULL;
}
again:
if (NULL == (p = fgets (line, BUFSIZ, netfh)))
return NULL;
if ('#' == *p) /* comment */
goto again;
cp = strpbrk (p, "#\n");
if (NULL == cp)
goto again;
*cp = '\0';
net.n_name = p;
cp = strpbrk (p, " \t");
if (NULL == cp)
goto again;
*cp++ = '\0';
while (' ' == *cp || '\t' == *cp)
cp++;
p = strpbrk (cp, " \t");
if (NULL != p)
*p++ = '\0';
/* returns address in network order */
if (0 == pgm_inet_network (cp, &sin)) {
struct sockaddr_in sa;
memset (&sa, 0, sizeof (sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = pgm_ntohl (sin.s_addr);
memcpy (&net.n_net, &sa, sizeof (sa));
} else if (0 != pgm_sa6_network (cp, (struct sockaddr_in6*)&net.n_net)) {
/* cannot resolve address, fail instead of returning junk address */
return NULL;
}
q = net.n_aliases = net_aliases;
if (NULL != p) {
/* some versions stick the address as the first alias, some default to NULL */
cp = p;
while (cp && *cp) {
if (' ' == *cp || '\t' == *cp) {
cp++;
continue;
}
if (q < &net_aliases[MAXALIASES - 1])
*q++ = cp;
cp = strpbrk (cp, " \t");
if (NULL != cp)
*cp++ = '\0';
}
}
*q = NULL;
return &net;
}
/* Lookup network by name in the /etc/networks database.
*
* returns 0 on success, returns -1 on invalid address.
*/
static
struct pgm_netent_t*
_pgm_compat_getnetbyname (
const char* name
)
{
struct pgm_netent_t *p;
char **cp;
if (NULL == name)
return NULL;
_pgm_compat_setnetent ();
while (NULL != (p = _pgm_compat_getnetent())) {
if (!strncmp (p->n_name, name, BUFSIZ))
break;
for (cp = p->n_aliases; *cp != 0; cp++)
if (!strncmp (*cp, name, BUFSIZ))
goto found;
}
found:
_pgm_compat_endnetent();
return p;
}
#ifdef HAVE_GETNETENT
static
struct pgm_netent_t*
_pgm_native_getnetbyname (
const char* name
)
{
struct sockaddr_in sa;
struct in_addr addr;
struct netent *ne;
char **cp, **q, **r;
size_t len;
if (NULL == name)
return NULL;
setnetent (0);
while (NULL != (ne = getnetent())) {
if (!strncmp (ne->n_name, name, BUFSIZ))
goto found;
for (cp = ne->n_aliases; *cp != 0; cp++)
if (!strncmp (*cp, name, BUFSIZ))
goto found;
}
endnetent();
return NULL;
found:
/* copy result into own global static buffer */
len = strlen (ne->n_name) + 1;
if (len > BUFSIZ)
return NULL;
net.n_name = memcpy (line, ne->n_name, len);
q = net.n_aliases = net_aliases;
r = ne->n_aliases;
while (*r) {
const size_t alias_len = strlen (*r) + 1;
if ((len + alias_len) > BUFSIZ)
break;
*q++ = memcpy (line + len, *r++, alias_len);
len += alias_len;
}
*q = NULL;
if (AF_INET != ne->n_addrtype)
return NULL;
memset (&sa, 0, sizeof (sa));
sa.sin_family = ne->n_addrtype;
addr = pgm_inet_makeaddr (ne->n_net, 0);
sa.sin_addr.s_addr = pgm_ntohl (addr.s_addr);
memcpy (&net.n_net, &sa, sizeof (sa));
endnetent();
return &net;
}
#endif
/* returns addresses in CIDR format in host-byte order, native implementations
* use pre-CIDR aligned format.
*
* ::getnetbyname(loopback) = 0.0.0.127
* pgm_getnetbyname(loopback) = 127.0.0.0
*
* returns first matching entry, whilst networks file could potentially contain
* multiple matching entries the native APIs do not support such functionality.
*
* cf. networks vs. hosts which getaddrinfo may return a list of results.
*/
struct pgm_netent_t*
pgm_getnetbyname (
const char* name
)
{
#ifdef HAVE_GETNETENT
char* netdb;
size_t envlen;
errno_t err;
err = pgm_dupenv_s (&netdb, &envlen, "PGM_NETDB");
free (netdb);
if (0 != err || 0 == envlen) {
/* default use native implementation */
return _pgm_native_getnetbyname (name);
}
#endif
return _pgm_compat_getnetbyname (name);
}
/* eof */