forked from grobian/carbon-c-relay
/
receptor.c
207 lines (181 loc) · 5.47 KB
/
receptor.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
/*
* Copyright 2013-2017 Fabian Groffen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <errno.h>
#include "relay.h"
#ifndef TMPDIR
# define TMPDIR "/tmp"
#endif
#define SOCKFILE ".s.carbon-c-relay"
/**
* Opens up listener sockets. Returns the socket fds in ret, and
* updates retlen. If opening sockets failed, -1 is returned. The
* caller should ensure retlen is at least 1, and ret should be an array
* large enough to hold it.
*/
int
bindlisten(
int ret_stream[], int *retlen_stream,
int ret_dgram[], int *retlen_dgram,
const char *interface, unsigned short port, unsigned int backlog)
{
int sock;
int optval;
struct timeval tv;
struct addrinfo hint;
struct addrinfo *res, *resw;
char buf[128];
char saddr[INET6_ADDRSTRLEN];
int err;
int binderr = 0;
int curlen_stream = 0;
int curlen_dgram = 0;
int socktypes[] = {SOCK_STREAM, SOCK_DGRAM, 0};
int *socktype = socktypes;
tv.tv_sec = 0;
tv.tv_usec = 500 * 1000;
for (; *socktype != 0 && binderr == 0; socktype++) {
memset(&hint, 0, sizeof(hint));
hint.ai_family = PF_UNSPEC;
hint.ai_socktype = *socktype;
hint.ai_protocol = 0;
hint.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV | AI_PASSIVE;
snprintf(buf, sizeof(buf), "%u", port);
if ((err = getaddrinfo(interface, buf, &hint, &res)) != 0) {
logerr("getaddrinfo(%s, %s, ...) failed: %s\n",
interface == NULL ? "NULL" : interface,
buf, gai_strerror(err));
return -1;
}
for (resw = res; resw != NULL; resw = resw->ai_next) {
if (resw->ai_family != PF_INET && resw->ai_family != PF_INET6)
continue;
if (resw->ai_protocol != IPPROTO_TCP && resw->ai_protocol != IPPROTO_UDP)
continue;
if ((sock = socket(resw->ai_family, resw->ai_socktype, resw->ai_protocol)) < 0) {
while (curlen_dgram > 0)
close(ret_dgram[--curlen_dgram]);
while (curlen_stream > 0)
close(ret_stream[--curlen_stream]);
binderr = 1;
break;
}
(void) setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
optval = 1; /* allow takeover */
(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (resw->ai_family == PF_INET6) {
optval = 1;
(void) setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval));
}
snprintf(saddr, sizeof(saddr), "(unknown)");
switch (resw->ai_family) {
case PF_INET:
inet_ntop(resw->ai_family,
&((struct sockaddr_in *)resw->ai_addr)->sin_addr,
saddr, sizeof(saddr));
break;
case PF_INET6:
inet_ntop(resw->ai_family,
&((struct sockaddr_in6 *)resw->ai_addr)->sin6_addr,
saddr, sizeof(saddr));
break;
}
if (bind(sock, resw->ai_addr, resw->ai_addrlen) < 0) {
logerr("failed to bind on %s%d %s port %s\n",
resw->ai_protocol == IPPROTO_TCP ? "tcp" : "udp",
resw->ai_family == PF_INET6 ? 6 : 4, saddr, buf);
close(sock);
while (curlen_dgram > 0)
close(ret_dgram[--curlen_dgram]);
while (curlen_stream > 0)
close(ret_stream[--curlen_stream]);
binderr = 1;
break;
}
if (resw->ai_protocol == IPPROTO_TCP) {
if (listen(sock, backlog) < 0) {
close(sock);
while (curlen_dgram > 0)
close(ret_dgram[--curlen_dgram]);
while (curlen_stream > 0)
close(ret_stream[--curlen_stream]);
binderr = 1;
break;
}
if (curlen_stream < *retlen_stream) {
logout("listening on tcp%d %s port %s\n",
resw->ai_family == PF_INET6 ? 6 : 4, saddr, buf);
ret_stream[curlen_stream++] = sock;
}
} else {
if (curlen_dgram < *retlen_dgram) {
logout("listening on udp%d %s port %s\n",
resw->ai_family == PF_INET6 ? 6 : 4, saddr, buf);
ret_dgram[curlen_dgram++] = sock;
}
}
}
freeaddrinfo(res);
}
if (curlen_stream + curlen_dgram == 0)
return -1;
/* fake loop to simplify breakout below */
while (curlen_stream < *retlen_stream) {
struct sockaddr_un server;
#ifndef PF_LOCAL
# define PF_LOCAL PF_UNIX
#endif
if ((sock = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0)
break;
snprintf(buf, sizeof(buf), "%s/%s.%u", TMPDIR, SOCKFILE, port);
memset(&server, 0, sizeof(struct sockaddr_un));
server.sun_family = PF_LOCAL;
strncpy(server.sun_path, buf, sizeof(server.sun_path) - 1);
unlink(buf); /* avoid address already in use */
if (bind(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) < 0) {
logerr("failed to bind for %s: %s\n",
buf, strerror(errno));
close(sock);
break;
}
if (listen(sock, backlog) < 0) {
close(sock);
break;
}
logout("listening on UNIX socket %s\n", buf);
ret_stream[curlen_stream++] = sock;
break;
}
*retlen_stream = curlen_stream;
*retlen_dgram = curlen_dgram;
return 0;
}
void
destroy_usock(unsigned short port)
{
char buf[512];
snprintf(buf, sizeof(buf), "%s/%s.%u", TMPDIR, SOCKFILE, port);
unlink(buf);
}