summaryrefslogtreecommitdiff
path: root/libbb/xconnect.c
blob: 377e0657eb29fd6895225cb68a2716aaba380d2b (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
/* vi: set sw=4 ts=4: */
/*
 * Utility routines.
 *
 * Connect to host at port using address resolution from getaddrinfo
 *
 */

#include <netinet/in.h>
#include "libbb.h"

static const int one = 1;
int setsockopt_reuseaddr(int fd)
{
	return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
}
int setsockopt_broadcast(int fd)
{
	return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
}

void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
{
	if (connect(s, s_addr, addrlen) < 0) {
		if (ENABLE_FEATURE_CLEAN_UP)
			close(s);
		if (s_addr->sa_family == AF_INET)
			bb_perror_msg_and_die("%s (%s)",
				"cannot connect to remote host",
				inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
		bb_perror_msg_and_die("cannot connect to remote host");
	}
}

/* Return port number for a service.
 * If "port" is a number use it as the port.
 * If "port" is a name it is looked up in /etc/services, if it isnt found return
 * default_port */
unsigned bb_lookup_port(const char *port, const char *protocol, unsigned default_port)
{
	unsigned port_nr = default_port;
	if (port) {
		int old_errno;

		/* Since this is a lib function, we're not allowed to reset errno to 0.
		 * Doing so could break an app that is deferring checking of errno. */
		old_errno = errno;
		port_nr = bb_strtou(port, NULL, 10);
		if (errno || port_nr > 65535) {
			struct servent *tserv = getservbyname(port, protocol);
			port_nr = default_port;
			if (tserv)
				port_nr = ntohs(tserv->s_port);
		}
		errno = old_errno;
	}
	return (uint16_t)port_nr;
}


/* "Old" networking API - only IPv4 */


void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
{
	struct hostent *he;

	memset(s_in, 0, sizeof(struct sockaddr_in));
	s_in->sin_family = AF_INET;
	he = xgethostbyname(host);
	memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
}

int xconnect_tcp_v4(struct sockaddr_in *s_addr)
{
	int s = xsocket(AF_INET, SOCK_STREAM, 0);
	xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
	return s;
}


/* "New" networking API */


int get_nport(len_and_sockaddr *lsa)
{
#if ENABLE_FEATURE_IPV6
	if (lsa->sa.sa_family == AF_INET6) {
		return lsa->sin6.sin6_port;
	}
#endif
	if (lsa->sa.sa_family == AF_INET) {
		return lsa->sin.sin_port;
	}
	return -1;
	/* What? UNIX socket? IPX?? :) */
}

void set_nport(len_and_sockaddr *lsa, unsigned port)
{
#if ENABLE_FEATURE_IPV6
	if (lsa->sa.sa_family == AF_INET6) {
		lsa->sin6.sin6_port = port;
		return;
	}
#endif
	if (lsa->sa.sa_family == AF_INET) {
		lsa->sin.sin_port = port;
		return;
	}
	/* What? UNIX socket? IPX?? :) */
}

/* peer: "1.2.3.4[:port]", "www.google.com[:port]"
 * port: if neither of above specifies port #
 */
static len_and_sockaddr* str2sockaddr(const char *host, int port, int ai_flags)
{
	int rc;
	len_and_sockaddr *r; // = NULL;
	struct addrinfo *result = NULL;
	const char *org_host = host; /* only for error msg */
	const char *cp;
	struct addrinfo hint;

	/* Ugly parsing of host:addr */
	if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
		host++;
		cp = strchr(host, ']');
		if (!cp || cp[1] != ':') /* Malformed: must have [xx]:nn */
			bb_error_msg_and_die("bad address '%s'", org_host);
			//return r; /* return NULL */
	} else {
		cp = strrchr(host, ':');
		if (ENABLE_FEATURE_IPV6 && cp && strchr(host, ':') != cp) {
			/* There is more than one ':' (e.g. "::1") */
			cp = NULL; /* it's not a port spec */
		}
	}
	if (cp) {
		int sz = cp - host + 1;
		host = safe_strncpy(alloca(sz), host, sz);
		if (ENABLE_FEATURE_IPV6 && *cp != ':')
			cp++; /* skip ']' */
		cp++; /* skip ':' */
		port = xatou16(cp);
	}

	memset(&hint, 0 , sizeof(hint));
	/* hint.ai_family = AF_UNSPEC; - zero anyway */
#if !ENABLE_FEATURE_IPV6
	hint.ai_family = AF_INET; /* do not try to find IPv6 */
#endif
	/* Needed. Or else we will get each address thrice (or more)
	 * for each possible socket type (tcp,udp,raw...): */
	hint.ai_socktype = SOCK_STREAM;
	hint.ai_flags = ai_flags;
	rc = getaddrinfo(host, NULL, &hint, &result);
	if (rc || !result)
		bb_error_msg_and_die("bad address '%s'", org_host);
	r = xmalloc(offsetof(len_and_sockaddr, sa) + result->ai_addrlen);
	r->len = result->ai_addrlen;
	memcpy(&r->sa, result->ai_addr, result->ai_addrlen);
	set_nport(r, htons(port));
	freeaddrinfo(result);
	return r;
}

len_and_sockaddr* host2sockaddr(const char *host, int port)
{
	return str2sockaddr(host, port, 0);
}

static len_and_sockaddr* dotted2sockaddr(const char *host, int port)
{
	return str2sockaddr(host, port, NI_NUMERICHOST);
}

int xsocket_stream(len_and_sockaddr **lsap)
{
	len_and_sockaddr *lsa;
	int fd;
	int len = sizeof(struct sockaddr_in);
	int family = AF_INET;

#if ENABLE_FEATURE_IPV6
	fd = socket(AF_INET6, SOCK_STREAM, 0);
	if (fd >= 0) {
		len = sizeof(struct sockaddr_in6);
		family = AF_INET6;
	} else
#endif
	{
		fd = xsocket(AF_INET, SOCK_STREAM, 0);
	}
	lsa = xzalloc(offsetof(len_and_sockaddr, sa) + len);
	lsa->len = len;
	lsa->sa.sa_family = family;
	*lsap = lsa;
	return fd;
}

int create_and_bind_stream_or_die(const char *bindaddr, int port)
{
	int fd;
	len_and_sockaddr *lsa;

	if (bindaddr && bindaddr[0]) {
		lsa = dotted2sockaddr(bindaddr, port);
		/* currently NULL check is in str2sockaddr */
		//if (!lsa)
		//	bb_error_msg_and_die("bad address '%s'", bindaddr);
		/* user specified bind addr dictates family */
		fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
	} else {
		fd = xsocket_stream(&lsa);
		set_nport(lsa, htons(port));
	}
	setsockopt_reuseaddr(fd);
	xbind(fd, &lsa->sa, lsa->len);
	free(lsa);
	return fd;
}

int create_and_connect_stream_or_die(const char *peer, int port)
{
	int fd;
	len_and_sockaddr *lsa;

	lsa = host2sockaddr(peer, port);
	/* currently NULL check is in str2sockaddr */
	//if (!lsa)
	//	bb_error_msg_and_die("bad address '%s'", peer);
	fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
	setsockopt_reuseaddr(fd);
	xconnect(fd, &lsa->sa, lsa->len);
	free(lsa);
	return fd;
}

int xconnect_stream(const len_and_sockaddr *lsa)
{
	int fd = xsocket(lsa->sa.sa_family, SOCK_STREAM, 0);
	xconnect(fd, &lsa->sa, lsa->len);
	return fd;
}

static char* sockaddr2str(const struct sockaddr *sa, socklen_t salen, int flags)
{
	char host[128];
	char serv[16];
	int rc = getnameinfo(sa, salen,
			host, sizeof(host),
			serv, sizeof(serv),
			flags | NI_NUMERICSERV /* do not resolve port# */
	);
	if (rc) return NULL;
// We probably need to use [%s]:%s for IPv6...
	return xasprintf("%s:%s", host, serv);
}

char* xmalloc_sockaddr2host(const struct sockaddr *sa, socklen_t salen)
{
	return sockaddr2str(sa, salen, 0);
}

char* xmalloc_sockaddr2dotted(const struct sockaddr *sa, socklen_t salen)
{
	return sockaddr2str(sa, salen, NI_NUMERICHOST);
}