This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
nc-vsock.c
324 lines (278 loc) · 6.28 KB
/
nc-vsock.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
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
/*
* nc-vsock - netcat-like utility for AF_VSOCK
* Copyright (C) 2016 Stefan Hajnoczi <stefanha@redhat.com>
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netdb.h>
#include <linux/vm_sockets.h>
static int parse_cid(const char *cid_str)
{
char *end = NULL;
long cid = strtol(cid_str, &end, 10);
if (cid_str != end && *end == '\0') {
return cid;
} else {
fprintf(stderr, "invalid cid: %s\n", cid_str);
return -1;
}
}
static int parse_port(const char *port_str)
{
char *end = NULL;
long port = strtol(port_str, &end, 10);
if (port_str != end && *end == '\0') {
return port;
} else {
fprintf(stderr, "invalid port number: %s\n", port_str);
return -1;
}
}
static int vsock_listen(const char *port_str)
{
int listen_fd;
int client_fd;
struct sockaddr_vm sa_listen = {
.svm_family = AF_VSOCK,
.svm_cid = VMADDR_CID_ANY,
};
struct sockaddr_vm sa_client;
socklen_t socklen_client = sizeof(sa_client);
int port = parse_port(port_str);
if (port < 0) {
return -1;
}
sa_listen.svm_port = port;
listen_fd = socket(AF_VSOCK, SOCK_STREAM, 0);
if (listen_fd < 0) {
perror("socket");
return -1;
}
if (bind(listen_fd, (struct sockaddr*)&sa_listen, sizeof(sa_listen)) != 0) {
perror("bind");
close(listen_fd);
return -1;
}
if (listen(listen_fd, 1) != 0) {
perror("listen");
close(listen_fd);
return -1;
}
client_fd = accept(listen_fd, (struct sockaddr*)&sa_client, &socklen_client);
if (client_fd < 0) {
perror("accept");
close(listen_fd);
return -1;
}
fprintf(stderr, "Connection from cid %u port %u...\n", sa_client.svm_cid, sa_client.svm_port);
close(listen_fd);
return client_fd;
}
static int tcp_connect(const char *node, const char *service)
{
int fd;
int ret;
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res = NULL;
struct addrinfo *addrinfo;
ret = getaddrinfo(node, service, &hints, &res);
if (ret != 0) {
fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(ret));
return -1;
}
for (addrinfo = res; addrinfo; addrinfo = addrinfo->ai_next) {
fd = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
if (fd < 0) {
perror("socket");
continue;
}
if (connect(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
perror("connect");
close(fd);
continue;
}
break;
}
freeaddrinfo(res);
return fd;
}
static int vsock_connect(const char *cid_str, const char *port_str)
{
int fd;
int cid;
int port;
struct sockaddr_vm sa = {
.svm_family = AF_VSOCK,
};
cid = parse_cid(cid_str);
if (cid < 0) {
return -1;
}
sa.svm_cid = cid;
port = parse_port(port_str);
if (port < 0) {
return -1;
}
sa.svm_port = port;
fd = socket(AF_VSOCK, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
return -1;
}
if (connect(fd, (struct sockaddr*)&sa, sizeof(sa)) != 0) {
perror("connect");
close(fd);
return -1;
}
return fd;
}
static int get_remote_fd(int argc, char **argv)
{
if (argc >= 3 && strcmp(argv[1], "-l") == 0) {
int remote_fd = vsock_listen(argv[2]);
if (remote_fd < 0) {
return -1;
}
if (argc == 6 && strcmp(argv[3], "-t") == 0) {
int fd = tcp_connect(argv[4], argv[5]);
if (fd < 0) {
return -1;
}
if (dup2(fd, STDIN_FILENO) < 0 ||
dup2(fd, STDOUT_FILENO) < 0) {
perror("dup2");
close(fd);
close(remote_fd);
return -1;
}
}
return remote_fd;
} else if (argc == 3) {
return vsock_connect(argv[1], argv[2]);
} else {
fprintf(stderr, "usage: %s [-l <port> [-t <dst> <dstport>] | <cid> <port>]\n", argv[0]);
return -1;
}
}
static void set_nonblock(int fd, bool enable)
{
int ret;
int flags;
ret = fcntl(fd, F_GETFL);
if (ret < 0) {
perror("fcntl");
return;
}
flags = ret & ~O_NONBLOCK;
if (enable) {
flags |= O_NONBLOCK;
}
fcntl(fd, F_SETFL, flags);
}
static int xfer_data(int in_fd, int out_fd)
{
char buf[4096];
char *send_ptr = buf;
ssize_t nbytes;
ssize_t remaining;
nbytes = read(in_fd, buf, sizeof(buf));
if (nbytes <= 0) {
return -1;
}
remaining = nbytes;
while (remaining > 0) {
nbytes = write(out_fd, send_ptr, remaining);
if (nbytes < 0 && errno == EAGAIN) {
nbytes = 0;
} else if (nbytes <= 0) {
return -1;
}
if (remaining > nbytes) {
/* Wait for fd to become writeable again */
for (;;) {
fd_set wfds;
FD_ZERO(&wfds);
FD_SET(out_fd, &wfds);
if (select(out_fd + 1, NULL, &wfds, NULL, NULL) < 0) {
if (errno == EINTR) {
continue;
} else {
perror("select");
return -1;
}
}
if (FD_ISSET(out_fd, &wfds)) {
break;
}
}
}
send_ptr += nbytes;
remaining -= nbytes;
}
return 0;
}
static void main_loop(int remote_fd)
{
fd_set rfds;
int nfds = remote_fd + 1;
set_nonblock(STDIN_FILENO, true);
set_nonblock(STDOUT_FILENO, true);
set_nonblock(remote_fd, true);
for (;;) {
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
FD_SET(remote_fd, &rfds);
if (select(nfds, &rfds, NULL, NULL, NULL) < 0) {
if (errno == EINTR) {
continue;
} else {
perror("select");
return;
}
}
if (FD_ISSET(STDIN_FILENO, &rfds)) {
if (xfer_data(STDIN_FILENO, remote_fd) < 0) {
return;
}
}
if (FD_ISSET(remote_fd, &rfds)) {
if (xfer_data(remote_fd, STDOUT_FILENO) < 0) {
return;
}
}
}
}
int main(int argc, char **argv)
{
int remote_fd = get_remote_fd(argc, argv);
if (remote_fd < 0) {
return EXIT_FAILURE;
}
main_loop(remote_fd);
return EXIT_SUCCESS;
}