summaryrefslogtreecommitdiff
path: root/svc_socket.c
blob: 6da587153760d6acfe92fd1cf60904900b58fdc4 (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
/* Copyright (C) 2002 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C 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.

   The GNU C 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 the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <rpc/rpc.h>
#include <sys/socket.h>

#include "common.h"
#include "pot.h"

static int get_service_port(u_long number, const char *proto)
{
	char servdata [1024];
	struct rpcent *rpcp = NULL;
	struct servent servbuf, *servp = NULL;
	int ret;

	rpcp = getrpcbynumber(number);
	if (rpcp != NULL) {
		/* First try name */
		ret = getservbyname_r(rpcp->r_name, proto, &servbuf, servdata,
		                       sizeof servdata, &servp);
		if ((ret != 0 || servp == NULL) && rpcp->r_aliases) {
			const char **a;

			/* Then we try aliases.	*/
			for (a = (const char **) rpcp->r_aliases; *a != NULL; a++) {
				ret = getservbyname_r(*a, proto, &servbuf, servdata,
						 sizeof servdata, &servp);
				if (ret == 0 && servp != NULL)
					break;
			}
		}
		if (ret == 0 && servp != NULL)
			return ntohs(servp->s_port);
	}
	return 0;
}

static struct addrinfo *svc_create_bindaddr(struct netconfig *nconf, int port)
{
	struct addrinfo *ai = NULL;
	struct addrinfo hint = {
		.ai_flags = AI_PASSIVE | AI_NUMERICSERV,
	};
	char portbuf[16];
	int err;

	if (!strcmp(nconf->nc_protofmly, NC_INET))
		hint.ai_family = AF_INET;
	else if (!strcmp(nconf->nc_protofmly, NC_INET6))
		hint.ai_family = AF_INET6;
	else {
		errstr(_("Unrecognized bind address family: %s\n"),
			nconf->nc_protofmly);
		return NULL;
	}

	if (!strcmp(nconf->nc_proto, NC_UDP))
		hint.ai_protocol = IPPROTO_UDP;
	else if (!strcmp(nconf->nc_proto, NC_TCP))
		hint.ai_protocol = IPPROTO_TCP;
	else {
		errstr(_("Unrecognized bind address protocol: %s\n"),
			nconf->nc_proto);
		return NULL;
	}

	if (nconf->nc_semantics == NC_TPI_CLTS)
		hint.ai_socktype = SOCK_DGRAM;
	else if (nconf->nc_semantics == NC_TPI_COTS_ORD)
		hint.ai_socktype = SOCK_STREAM;
	else {
		errstr(_("Unrecognized address semantics: %lu\n"),
			nconf->nc_semantics);
		return NULL;
	}

	snprintf(portbuf, sizeof(portbuf), "%u", port);
	err = getaddrinfo(NULL, portbuf, &hint, &ai);
	if (err) {
		errstr(_("Failed to construct bind address: %s\n"),
			gai_strerror(err));
		return NULL;
	}
	return ai;
}

static int svc_create_sock(struct addrinfo *ai)
{
	int fd;
	int optval = 1;

	fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
	if (fd < 0) {
		errstr(_("Error creating socket: %s\n"), strerror(errno));
		return -1;
	}

	if (ai->ai_family == AF_INET6) {
		if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
				&optval, sizeof(optval)) < 0) {
			errstr(_("Cannot set IPv6 socket options: %s\n"), strerror(errno));
			close(fd);
			return -1;
		}
	}

	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) {
		errstr(_("Cannot set socket options: %s\n"), strerror(errno));
		close(fd);
		return -1;
	}

	if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
		errstr(_("Cannot bind to address: %s\n"), strerror(errno));
		close(fd);
		return -1;
	}

	if (ai->ai_protocol == IPPROTO_TCP) {
		if (listen(fd, SOMAXCONN) < 0) {
			errstr(_("Cannot listen to address: %s\n"), strerror(errno));
			close(fd);
			return -1;
		}
	}

	return fd;
}

/*
 * Create service structure based on passed netconfig and port
 */
SVCXPRT *svc_create_nconf(rpcprog_t program, struct netconfig *nconf, int port)
{
	SVCXPRT *xprt;

	if (!port)
		port = get_service_port(program, nconf->nc_proto);

	if (port) {
		struct addrinfo *ai = svc_create_bindaddr(nconf, port);
		int fd;

		if (!ai)
			return NULL;

		fd = svc_create_sock(ai);
		freeaddrinfo(ai);
		if (fd < 0)
			return NULL;
		xprt = svc_tli_create(fd, nconf, NULL, 0, 0);
		if (!xprt) {
			close(fd);
			return NULL;
		}
	} else {
		xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
	}
	return xprt;
}