aboutsummaryrefslogtreecommitdiff
path: root/lib/url.c
blob: 8b5995c4a16fbf86337b6a65bc929ee506befd14 (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
/* This file is part of Smap.
   Copyright (C) 2006-2007, 2010, 2014 Sergey Poznyakoff

   Smap 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, or (at your option)
   any later version.

   Smap 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 Smap.  If not, see <http://www.gnu.org/licenses/>. */

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

#include <smap/url.h>

static char *url_error_string[] = {
	"no error",
	"not enough memory",                     /* SMAP_URLE_NOMEM */
	"malformed URL",                         /* SMAP_URLE_INVALID */
	"port is meaningless for UNIX sockets",  /* SMAP_URLE_PORT */
	"UNIX socket name too long",             /* SMAP_URLE_NAME2LONG */
	"UNIX socket name must be absolute",     /* SMAP_URLE_NOABS */
	"missing port number",                   /* SMAP_URLE_NOPORT */
	"bad port number",                       /* SMAP_URLE_BADPORT */
	"unknown port name",                     /* SMAP_URLE_UNKPORT */
	"unknown host name",                     /* SMAP_URLE_UNKHOST */
	"unsupported address family",            /* SMAP_URLE_BADFAMILY */
	"unsupported protocol",                  /* SMAP_URLE_BADPROTO */
};

const char *
smap_url_strerror(int er)
{
	if (er < 0
	    || er >= sizeof(url_error_string)/sizeof(url_error_string[0]))
		return "unrecognized error";
	return url_error_string[er];
}

static int
copy_part(const char *cstr, const char *p, char **pbuf)
{
	size_t len = p - cstr;
	char *buf = malloc(len + 1);
	if (!buf) {
		free(buf);
		return SMAP_URLE_NOMEM;
	}
	memcpy(buf, cstr, len);
	buf[len] = 0;
	*pbuf = buf;
	return 0;
}


static int
split_url(const char *proto, const char *cstr, char **pport, char **ppath)
{
	const char *p = strchr(cstr, ':');

	if (!p) {
		*pport = NULL;
		*ppath = strdup(cstr);
		return *ppath == NULL;
	} else if (copy_part(cstr, p, ppath))
		return SMAP_URLE_NOMEM;
	else
		cstr = p + 1;
	*pport = strdup(cstr);
	return *pport == NULL ? SMAP_URLE_NOMEM : 0;
}

int
split_connection(const char *cstr,
		 char **pproto, char **pport, char **ppath)
{
	const char *p;

	p = strchr(cstr, ':');
	if (!p) {
		*pproto = NULL;
		if (cstr[0] == '/') {
			if ((*pproto = strdup("unix")) == NULL)
				return -1;
			if ((*ppath = strdup(cstr)) == NULL) {
				free(*pproto);
				return SMAP_URLE_NOMEM;
			}
			*pport = 0;
		} else
			return SMAP_URLE_NOABS;
	} else {
		char *proto;
		if (copy_part(cstr, p, &proto))
			return SMAP_URLE_NOMEM;
		cstr = p + 1;
		if (cstr[0] == '/' && cstr[1] == '/') {
			int rc = split_url(proto, cstr + 2, pport, ppath);
			if (rc)
				free(proto);
			else
				*pproto = proto;
			return rc;
		} else {
			free(proto);
			return SMAP_URLE_INVALID;
		}
		*pproto = proto;
	}
	return 0;
}

int
parse_url0(const char *cstr,
	   char *proto, char *port, char *path,
	   struct sockaddr **psa, socklen_t *plen)
{
	union {
		struct sockaddr sa;
		struct sockaddr_in s_in;
		struct sockaddr_un s_un;
	} addr;
	socklen_t socklen;
	struct sockaddr *sa;

	if (!proto
	    || strcmp(proto, "unix") == 0 || strcmp(proto, "local") == 0) {
		if (port) {
			return SMAP_URLE_PORT;
			return -1;
		}

		if (strlen(path) > sizeof addr.s_un.sun_path)
			return SMAP_URLE_NAME2LONG;

		addr.sa.sa_family = PF_UNIX;
		socklen = sizeof(addr.s_un);
		strcpy(addr.s_un.sun_path, path);
	} else if (strcmp(proto, "inet") == 0) {
		short pnum;
		long num;
		char *p;

		addr.sa.sa_family = PF_INET;
		socklen = sizeof(addr.s_in);

		if (!port)
			return SMAP_URLE_NOPORT;

		num = pnum = strtol(port, &p, 0);
		if (*p == 0) {
			if (num != pnum)
				return SMAP_URLE_BADPORT;
			pnum = htons(pnum);
		} else {
			struct servent *sp = getservbyname(port, "tcp");
			if (!sp)
				return SMAP_URLE_UNKPORT;
			pnum = sp->s_port;
		}

		if (!path)
			addr.s_in.sin_addr.s_addr = INADDR_ANY;
		else {
			struct hostent *hp = gethostbyname(path);
			if (!hp)
				return SMAP_URLE_UNKHOST;
			addr.sa.sa_family = hp->h_addrtype;
			switch (hp->h_addrtype) {
			case AF_INET:
				memmove(&addr.s_in.sin_addr, hp->h_addr, 4);
				addr.s_in.sin_port = pnum;
				break;

			default:
				return SMAP_URLE_BADFAMILY;
			}
		}
	} else
		return SMAP_URLE_BADPROTO;
	sa = malloc(socklen);
	if (!sa)
		return SMAP_URLE_NOMEM;
	memcpy(sa, &addr, socklen);
	*psa = sa;
	*plen = socklen;
	return 0;
}

int
smap_url_parse(const char *cstr, struct sockaddr **psa, socklen_t *plen)
{
	int rc;
	char *proto = NULL, *port = NULL, *path = NULL;

	rc = split_connection(cstr, &proto, &port, &path);
	if (rc)
		return rc;
	rc = parse_url0(cstr, proto, port, path, psa, plen);
	free(proto);
	free(port);
	free(path);
	return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.