aboutsummaryrefslogtreecommitdiff
path: root/src/sockaddr.c
blob: 83f24502d39f0088641c96d9a6ee96b6d7fa2884 (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
/* grecs - Gray's Extensible Configuration System
   Copyright (C) 2007-2017 Sergey Poznyakoff

   Grecs is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Grecs 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with Grecs. If not, see <http://www.gnu.org/licenses/>. */

/* Network-specific functions */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include "grecs.h"

struct grecs_sockaddr *
grecs_sockaddr_new(size_t s)
{
	struct grecs_sockaddr *sp = grecs_malloc(sizeof(*sp));
	sp->next = NULL;
	sp->str = NULL;
	sp->sa = grecs_zalloc(s);
	sp->len = s;
	return sp;
}

void
grecs_sockaddr_free(struct grecs_sockaddr *p)
{
	while (p) {
		struct grecs_sockaddr *next = p->next;
		free(p->sa);
		free(p->str);
		free(p);
		p = next;
	}
}

static int
parse_unix(struct grecs_sockaddr **ret, const char *arg, const char *addrstr,
	   struct grecs_sockaddr_hints *gh, grecs_locus_t const *locus)
{
	struct sockaddr_un *s_un;
	size_t slen = strlen(addrstr);
	struct grecs_sockaddr *sp;
	
	if (slen >= sizeof s_un->sun_path) {
		grecs_error(locus, 0, _("socket path name too long: %s"), arg);
		return -1;
	}
	
	sp = grecs_sockaddr_new(sizeof(s_un[0]));
	s_un = (struct sockaddr_un *) sp->sa;
	s_un->sun_family = AF_UNIX;
	strcpy(s_un->sun_path, addrstr);

	*ret = sp;
	return 0;
}

static int
parse_inet(struct grecs_sockaddr **ret,
	   int family, const char *arg, const char *addrstr,
	   struct grecs_sockaddr_hints *gh, grecs_locus_t const *locus)
{
	int rc;
	struct addrinfo hints;
	struct addrinfo *res, *ap;
	const char *node = NULL;
	char *nodebuf = NULL;
	const char *service = NULL;
	struct grecs_sockaddr *head = NULL, *tail = NULL;
	char portbuf[64];

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = family;
	hints.ai_socktype = SOCK_STREAM;

	if ((family == AF_INET6 || family == AF_UNSPEC)
	    && addrstr[0] == '[') {
		char *p = strchr(addrstr + 1, ']');
		if (p && p > addrstr + 1) {
			size_t len;
			
			addrstr++;
			len = p - addrstr;
			nodebuf = grecs_malloc(len + 1);
			memcpy(nodebuf, addrstr, len);
			nodebuf[len] = 0;
			node = nodebuf;
			service = p + 1;
			family = AF_INET6;
		} else
			service = strchr(addrstr, ':');
	} else
		service = strrchr(addrstr, ':');

	if (service && *service) {
		if (*service != ':') {
			grecs_error(locus, 0,
				    _("%s: garbage near %s"), arg, service);
			return -1;
		}
		service++;
	}
	
	if (!node) {
		if (service) {
			size_t len = service - addrstr - 1;
			
			if (len == 0)
				node = NULL;
			else {
				nodebuf = grecs_malloc(len + 1);
				memcpy(nodebuf, addrstr, len);
				nodebuf[len] = 0;
				node = nodebuf;
			}
		} else {
			if (grecs_str_is_ipaddr(addrstr))
				node = addrstr;
			else if (grecs_str_is_num(addrstr)) {
				service = addrstr;
				hints.ai_flags |= AI_NUMERICSERV;
			}
		}
	}
	
	if (!service || !*service) {
		if (!node && addrstr[0])
			node = addrstr;
		if (gh->flags & GRECS_HINT_SERVICE) {
			service = gh->service;
		} else if (gh->flags & GRECS_HINT_PORT) {
			snprintf(portbuf, sizeof portbuf, "%hu", gh->port);
			service = portbuf;
			hints.ai_flags |= AI_NUMERICSERV;
		} else if (!(gh->flags & GRECS_AH_PASSIVE)) {
			grecs_error(locus, 0,
				    _("service not specified: %s"), arg);
			return -1;
		}
	}
	
	if (!node) {
		if (gh->flags & GRECS_AH_PASSIVE)
			hints.ai_flags |= AI_PASSIVE;
	}
	
	rc = getaddrinfo(node, service, &hints, &res);
	free(nodebuf);
	switch (rc) {
	case 0:
		break;
	case EAI_SYSTEM:
		grecs_error(locus, 0,
			    _("%s: cannot parse address: %s"),
			    arg, strerror(errno));
		break;
	case EAI_BADFLAGS:
	case EAI_SOCKTYPE:
		grecs_error(locus, 0,
			    "%s:%d: internal error converting %s",
			    __FILE__,__LINE__,arg);
		break;
	case EAI_MEMORY:
		grecs_alloc_die();
	default:
		grecs_error(locus, 0,
			    "%s: %s", arg, gai_strerror(rc));
		return -1;
	}

	for (ap = res; ap; ap = ap->ai_next) {
		if (family == AF_UNSPEC || ap->ai_addr->sa_family == family) {
			struct grecs_sockaddr *sp =
				grecs_sockaddr_new(ap->ai_addrlen);
			memcpy(sp->sa, ap->ai_addr, ap->ai_addrlen);
			sp->len = ap->ai_addrlen;
			if (!head)
				head = sp;
			else
				tail->next = sp;
			tail = sp;
		}
	}
	freeaddrinfo(res);
	*ret = head;
	return 0;
}

static int
parse_inet4(struct grecs_sockaddr **ret, const char *arg, const char *addrstr,
	    struct grecs_sockaddr_hints *gh, grecs_locus_t const *locus)
{
	return parse_inet(ret, AF_INET, arg, addrstr, gh, locus);
}

static int
parse_inet6(struct grecs_sockaddr **ret, const char *arg, const char *addrstr,
	    struct grecs_sockaddr_hints *gh, grecs_locus_t const *locus)
{
	return parse_inet(ret, AF_INET6, arg, addrstr, gh, locus);
}

struct schemetab {
	const char *scheme;
	size_t len;
	int (*parser)(struct grecs_sockaddr **ret,
		      const char *arg, const char *addr,
		      struct grecs_sockaddr_hints *gh,
		      grecs_locus_t const *locus);
};

struct schemetab schemetab[] = {
	{ "inet",  4, parse_inet4 },
	{ "inet4", 5, parse_inet4 },
	{ "inet6", 5, parse_inet6 },
	{ "unix",  4, parse_unix },
	{ NULL }
};

int
grecs_str_to_sockaddr(struct grecs_sockaddr **sap,
		      const char *arg, struct grecs_sockaddr_hints *gh,
		      grecs_locus_t const *locus)
{
	char *p;
	struct grecs_sockaddr_hints ghints;
	
	if (!gh) {
		memset(&ghints, 0, sizeof(ghints));
		if (grecs_default_port) {
			ghints.flags = GRECS_HINT_PORT;
			ghints.port = ntohs(grecs_default_port);
		}
		gh = &ghints;
	}
	
	p = strchr(arg, ':');
	if (p && p > arg && p[1] == '/' && p[2] == '/') {
		size_t len = p - arg;
		struct schemetab *sp;

		for (sp = schemetab; sp->scheme; sp++)
			if (len == sp->len &&
			    memcmp(arg, sp->scheme, len) == 0)
				return sp->parser(sap, arg, p + 3, gh, locus);
		grecs_error(locus, 0,
			    _("unknown or unsupported scheme: %s"), arg);
		return -1;
	}

	if (arg[0] == '/')
		return parse_unix(sap, arg, arg, gh, locus);
	else if (strlen(arg) > 5 && memcmp(arg, "unix:", 5) == 0) {
		if (arg[5] != '/')
			grecs_error(locus, 0,
				    _("%s: UNIX socket must be an absolute file name"),
				    arg);
		return parse_unix(sap, arg, arg + 5, gh, locus);
	}
	
	return parse_inet(sap, AF_UNSPEC, arg, arg, gh, locus);
}

#define S_UN_NAME(sa, salen) \
	((salen < offsetof(struct sockaddr_un,sun_path)) ?      \
	 "" : (sa)->sun_path)

static int
sockaddr_str(struct sockaddr *sa, socklen_t salen, char **pbuf, size_t *psz)
{
	int rc;
	
	switch (sa->sa_family) {
	case AF_INET:
	case AF_INET6: {
		char host[NI_MAXHOST];
		char srv[NI_MAXSERV];
		if (getnameinfo(sa, salen,
				host, sizeof(host), srv, sizeof(srv),
				NI_NUMERICHOST|NI_NUMERICSERV) == 0)
			rc = grecs_asprintf(pbuf, psz, "%s://%s:%s",
					    sa->sa_family == AF_INET ?
					    "inet" : "inet6",
					    host, srv);
		else
			rc = grecs_asprintf(pbuf, psz,
					    "%s://[getnameinfo failed]",
					    sa->sa_family == AF_INET ?
					    "inet" : "inet6");
		break;
	}
	case AF_UNIX: {
		struct sockaddr_un *s_un = (struct sockaddr_un *)sa;
		if (S_UN_NAME(s_un, salen)[0] == 0)
			rc = grecs_asprintf(pbuf, psz,
					    "unix://[anonymous socket]");
		else
			rc = grecs_asprintf(pbuf, psz, "unix://%s",
					    s_un->sun_path);
		break;
	}

	default:
		rc = grecs_asprintf(pbuf, psz, "family:%d", sa->sa_family);
	}
	return rc;
}

char const *
grecs_sockaddr_str(struct grecs_sockaddr *sa)
{
	if (!sa->str) {
		size_t sz = 0;
		if (sockaddr_str(sa->sa, sa->len, &sa->str, &sz)) {
			//FIXME
			abort();
		}
	}
	return sa->str;
}

Return to:

Send suggestions and report system problems to the System administrator.