root / net / socket.c @ 067404be
History | View | Annotate | Download (21.2 kB)
1 |
/*
|
---|---|
2 |
* QEMU System Emulator
|
3 |
*
|
4 |
* Copyright (c) 2003-2008 Fabrice Bellard
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
8 |
* in the Software without restriction, including without limitation the rights
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
11 |
* furnished to do so, subject to the following conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
#include "config-host.h" |
25 |
|
26 |
#include "net/net.h" |
27 |
#include "clients.h" |
28 |
#include "monitor/monitor.h" |
29 |
#include "qemu-common.h" |
30 |
#include "qemu/error-report.h" |
31 |
#include "qemu/option.h" |
32 |
#include "qemu/sockets.h" |
33 |
#include "qemu/iov.h" |
34 |
#include "qemu/main-loop.h" |
35 |
|
36 |
typedef struct NetSocketState { |
37 |
NetClientState nc; |
38 |
int listen_fd;
|
39 |
int fd;
|
40 |
int state; /* 0 = getting length, 1 = getting data */ |
41 |
unsigned int index; |
42 |
unsigned int packet_len; |
43 |
unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */ |
44 |
uint8_t buf[NET_BUFSIZE]; |
45 |
struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
46 |
IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */
|
47 |
bool read_poll; /* waiting to receive data? */ |
48 |
bool write_poll; /* waiting to transmit data? */ |
49 |
} NetSocketState; |
50 |
|
51 |
static void net_socket_accept(void *opaque); |
52 |
static void net_socket_writable(void *opaque); |
53 |
|
54 |
/* Only read packets from socket when peer can receive them */
|
55 |
static int net_socket_can_send(void *opaque) |
56 |
{ |
57 |
NetSocketState *s = opaque; |
58 |
|
59 |
return qemu_can_send_packet(&s->nc);
|
60 |
} |
61 |
|
62 |
static void net_socket_update_fd_handler(NetSocketState *s) |
63 |
{ |
64 |
qemu_set_fd_handler2(s->fd, |
65 |
s->read_poll ? net_socket_can_send : NULL,
|
66 |
s->read_poll ? s->send_fn : NULL,
|
67 |
s->write_poll ? net_socket_writable : NULL,
|
68 |
s); |
69 |
} |
70 |
|
71 |
static void net_socket_read_poll(NetSocketState *s, bool enable) |
72 |
{ |
73 |
s->read_poll = enable; |
74 |
net_socket_update_fd_handler(s); |
75 |
} |
76 |
|
77 |
static void net_socket_write_poll(NetSocketState *s, bool enable) |
78 |
{ |
79 |
s->write_poll = enable; |
80 |
net_socket_update_fd_handler(s); |
81 |
} |
82 |
|
83 |
static void net_socket_writable(void *opaque) |
84 |
{ |
85 |
NetSocketState *s = opaque; |
86 |
|
87 |
net_socket_write_poll(s, false);
|
88 |
|
89 |
qemu_flush_queued_packets(&s->nc); |
90 |
} |
91 |
|
92 |
static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
93 |
{ |
94 |
NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
95 |
uint32_t len = htonl(size); |
96 |
struct iovec iov[] = {
|
97 |
{ |
98 |
.iov_base = &len, |
99 |
.iov_len = sizeof(len),
|
100 |
}, { |
101 |
.iov_base = (void *)buf,
|
102 |
.iov_len = size, |
103 |
}, |
104 |
}; |
105 |
size_t remaining; |
106 |
ssize_t ret; |
107 |
|
108 |
remaining = iov_size(iov, 2) - s->send_index;
|
109 |
ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
|
110 |
|
111 |
if (ret == -1 && errno == EAGAIN) { |
112 |
ret = 0; /* handled further down */ |
113 |
} |
114 |
if (ret == -1) { |
115 |
s->send_index = 0;
|
116 |
return -errno;
|
117 |
} |
118 |
if (ret < (ssize_t)remaining) {
|
119 |
s->send_index += ret; |
120 |
net_socket_write_poll(s, true);
|
121 |
return 0; |
122 |
} |
123 |
s->send_index = 0;
|
124 |
return size;
|
125 |
} |
126 |
|
127 |
static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size) |
128 |
{ |
129 |
NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
130 |
ssize_t ret; |
131 |
|
132 |
do {
|
133 |
ret = qemu_sendto(s->fd, buf, size, 0,
|
134 |
(struct sockaddr *)&s->dgram_dst,
|
135 |
sizeof(s->dgram_dst));
|
136 |
} while (ret == -1 && errno == EINTR); |
137 |
|
138 |
if (ret == -1 && errno == EAGAIN) { |
139 |
net_socket_write_poll(s, true);
|
140 |
return 0; |
141 |
} |
142 |
return ret;
|
143 |
} |
144 |
|
145 |
static void net_socket_send(void *opaque) |
146 |
{ |
147 |
NetSocketState *s = opaque; |
148 |
int size, err;
|
149 |
unsigned l;
|
150 |
uint8_t buf1[NET_BUFSIZE]; |
151 |
const uint8_t *buf;
|
152 |
|
153 |
size = qemu_recv(s->fd, buf1, sizeof(buf1), 0); |
154 |
if (size < 0) { |
155 |
err = socket_error(); |
156 |
if (err != EWOULDBLOCK)
|
157 |
goto eoc;
|
158 |
} else if (size == 0) { |
159 |
/* end of connection */
|
160 |
eoc:
|
161 |
net_socket_read_poll(s, false);
|
162 |
net_socket_write_poll(s, false);
|
163 |
if (s->listen_fd != -1) { |
164 |
qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
|
165 |
} |
166 |
closesocket(s->fd); |
167 |
|
168 |
s->fd = -1;
|
169 |
s->state = 0;
|
170 |
s->index = 0;
|
171 |
s->packet_len = 0;
|
172 |
s->nc.link_down = true;
|
173 |
memset(s->buf, 0, sizeof(s->buf)); |
174 |
memset(s->nc.info_str, 0, sizeof(s->nc.info_str)); |
175 |
|
176 |
return;
|
177 |
} |
178 |
buf = buf1; |
179 |
while (size > 0) { |
180 |
/* reassemble a packet from the network */
|
181 |
switch(s->state) {
|
182 |
case 0: |
183 |
l = 4 - s->index;
|
184 |
if (l > size)
|
185 |
l = size; |
186 |
memcpy(s->buf + s->index, buf, l); |
187 |
buf += l; |
188 |
size -= l; |
189 |
s->index += l; |
190 |
if (s->index == 4) { |
191 |
/* got length */
|
192 |
s->packet_len = ntohl(*(uint32_t *)s->buf); |
193 |
s->index = 0;
|
194 |
s->state = 1;
|
195 |
} |
196 |
break;
|
197 |
case 1: |
198 |
l = s->packet_len - s->index; |
199 |
if (l > size)
|
200 |
l = size; |
201 |
if (s->index + l <= sizeof(s->buf)) { |
202 |
memcpy(s->buf + s->index, buf, l); |
203 |
} else {
|
204 |
fprintf(stderr, "serious error: oversized packet received,"
|
205 |
"connection terminated.\n");
|
206 |
s->state = 0;
|
207 |
goto eoc;
|
208 |
} |
209 |
|
210 |
s->index += l; |
211 |
buf += l; |
212 |
size -= l; |
213 |
if (s->index >= s->packet_len) {
|
214 |
qemu_send_packet(&s->nc, s->buf, s->packet_len); |
215 |
s->index = 0;
|
216 |
s->state = 0;
|
217 |
} |
218 |
break;
|
219 |
} |
220 |
} |
221 |
} |
222 |
|
223 |
static void net_socket_send_dgram(void *opaque) |
224 |
{ |
225 |
NetSocketState *s = opaque; |
226 |
int size;
|
227 |
|
228 |
size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0); |
229 |
if (size < 0) |
230 |
return;
|
231 |
if (size == 0) { |
232 |
/* end of connection */
|
233 |
net_socket_read_poll(s, false);
|
234 |
net_socket_write_poll(s, false);
|
235 |
return;
|
236 |
} |
237 |
qemu_send_packet(&s->nc, s->buf, size); |
238 |
} |
239 |
|
240 |
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr) |
241 |
{ |
242 |
struct ip_mreq imr;
|
243 |
int fd;
|
244 |
int val, ret;
|
245 |
#ifdef __OpenBSD__
|
246 |
unsigned char loop; |
247 |
#else
|
248 |
int loop;
|
249 |
#endif
|
250 |
|
251 |
if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
252 |
fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
|
253 |
"does not contain a multicast address\n",
|
254 |
inet_ntoa(mcastaddr->sin_addr), |
255 |
(int)ntohl(mcastaddr->sin_addr.s_addr));
|
256 |
return -1; |
257 |
|
258 |
} |
259 |
fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
|
260 |
if (fd < 0) { |
261 |
perror("socket(PF_INET, SOCK_DGRAM)");
|
262 |
return -1; |
263 |
} |
264 |
|
265 |
val = 1;
|
266 |
ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
267 |
if (ret < 0) { |
268 |
perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
269 |
goto fail;
|
270 |
} |
271 |
|
272 |
ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
273 |
if (ret < 0) { |
274 |
perror("bind");
|
275 |
goto fail;
|
276 |
} |
277 |
|
278 |
/* Add host to multicast group */
|
279 |
imr.imr_multiaddr = mcastaddr->sin_addr; |
280 |
if (localaddr) {
|
281 |
imr.imr_interface = *localaddr; |
282 |
} else {
|
283 |
imr.imr_interface.s_addr = htonl(INADDR_ANY); |
284 |
} |
285 |
|
286 |
ret = qemu_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
287 |
&imr, sizeof(struct ip_mreq)); |
288 |
if (ret < 0) { |
289 |
perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
290 |
goto fail;
|
291 |
} |
292 |
|
293 |
/* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
294 |
loop = 1;
|
295 |
ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
296 |
&loop, sizeof(loop));
|
297 |
if (ret < 0) { |
298 |
perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
299 |
goto fail;
|
300 |
} |
301 |
|
302 |
/* If a bind address is given, only send packets from that address */
|
303 |
if (localaddr != NULL) { |
304 |
ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
305 |
localaddr, sizeof(*localaddr));
|
306 |
if (ret < 0) { |
307 |
perror("setsockopt(IP_MULTICAST_IF)");
|
308 |
goto fail;
|
309 |
} |
310 |
} |
311 |
|
312 |
qemu_set_nonblock(fd); |
313 |
return fd;
|
314 |
fail:
|
315 |
if (fd >= 0) |
316 |
closesocket(fd); |
317 |
return -1; |
318 |
} |
319 |
|
320 |
static void net_socket_cleanup(NetClientState *nc) |
321 |
{ |
322 |
NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
323 |
if (s->fd != -1) { |
324 |
net_socket_read_poll(s, false);
|
325 |
net_socket_write_poll(s, false);
|
326 |
close(s->fd); |
327 |
s->fd = -1;
|
328 |
} |
329 |
if (s->listen_fd != -1) { |
330 |
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
331 |
closesocket(s->listen_fd); |
332 |
s->listen_fd = -1;
|
333 |
} |
334 |
} |
335 |
|
336 |
static NetClientInfo net_dgram_socket_info = {
|
337 |
.type = NET_CLIENT_OPTIONS_KIND_SOCKET, |
338 |
.size = sizeof(NetSocketState),
|
339 |
.receive = net_socket_receive_dgram, |
340 |
.cleanup = net_socket_cleanup, |
341 |
}; |
342 |
|
343 |
static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
|
344 |
const char *model, |
345 |
const char *name, |
346 |
int fd, int is_connected) |
347 |
{ |
348 |
struct sockaddr_in saddr;
|
349 |
int newfd;
|
350 |
socklen_t saddr_len; |
351 |
NetClientState *nc; |
352 |
NetSocketState *s; |
353 |
|
354 |
/* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
355 |
* Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
356 |
* by ONLY ONE process: we must "clone" this dgram socket --jjo
|
357 |
*/
|
358 |
|
359 |
if (is_connected) {
|
360 |
if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
361 |
/* must be bound */
|
362 |
if (saddr.sin_addr.s_addr == 0) { |
363 |
fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
|
364 |
"cannot setup multicast dst addr\n", fd);
|
365 |
goto err;
|
366 |
} |
367 |
/* clone dgram socket */
|
368 |
newfd = net_socket_mcast_create(&saddr, NULL);
|
369 |
if (newfd < 0) { |
370 |
/* error already reported by net_socket_mcast_create() */
|
371 |
goto err;
|
372 |
} |
373 |
/* clone newfd to fd, close newfd */
|
374 |
dup2(newfd, fd); |
375 |
close(newfd); |
376 |
|
377 |
} else {
|
378 |
fprintf(stderr, |
379 |
"qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
380 |
fd, strerror(errno)); |
381 |
goto err;
|
382 |
} |
383 |
} |
384 |
|
385 |
nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name); |
386 |
|
387 |
snprintf(nc->info_str, sizeof(nc->info_str),
|
388 |
"socket: fd=%d (%s mcast=%s:%d)",
|
389 |
fd, is_connected ? "cloned" : "", |
390 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
391 |
|
392 |
s = DO_UPCAST(NetSocketState, nc, nc); |
393 |
|
394 |
s->fd = fd; |
395 |
s->listen_fd = -1;
|
396 |
s->send_fn = net_socket_send_dgram; |
397 |
net_socket_read_poll(s, true);
|
398 |
|
399 |
/* mcast: save bound address as dst */
|
400 |
if (is_connected) {
|
401 |
s->dgram_dst = saddr; |
402 |
} |
403 |
|
404 |
return s;
|
405 |
|
406 |
err:
|
407 |
closesocket(fd); |
408 |
return NULL; |
409 |
} |
410 |
|
411 |
static void net_socket_connect(void *opaque) |
412 |
{ |
413 |
NetSocketState *s = opaque; |
414 |
s->send_fn = net_socket_send; |
415 |
net_socket_read_poll(s, true);
|
416 |
} |
417 |
|
418 |
static NetClientInfo net_socket_info = {
|
419 |
.type = NET_CLIENT_OPTIONS_KIND_SOCKET, |
420 |
.size = sizeof(NetSocketState),
|
421 |
.receive = net_socket_receive, |
422 |
.cleanup = net_socket_cleanup, |
423 |
}; |
424 |
|
425 |
static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
|
426 |
const char *model, |
427 |
const char *name, |
428 |
int fd, int is_connected) |
429 |
{ |
430 |
NetClientState *nc; |
431 |
NetSocketState *s; |
432 |
|
433 |
nc = qemu_new_net_client(&net_socket_info, peer, model, name); |
434 |
|
435 |
snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd); |
436 |
|
437 |
s = DO_UPCAST(NetSocketState, nc, nc); |
438 |
|
439 |
s->fd = fd; |
440 |
s->listen_fd = -1;
|
441 |
|
442 |
/* Disable Nagle algorithm on TCP sockets to reduce latency */
|
443 |
socket_set_nodelay(fd); |
444 |
|
445 |
if (is_connected) {
|
446 |
net_socket_connect(s); |
447 |
} else {
|
448 |
qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
449 |
} |
450 |
return s;
|
451 |
} |
452 |
|
453 |
static NetSocketState *net_socket_fd_init(NetClientState *peer,
|
454 |
const char *model, const char *name, |
455 |
int fd, int is_connected) |
456 |
{ |
457 |
int so_type = -1, optlen=sizeof(so_type); |
458 |
|
459 |
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
460 |
(socklen_t *)&optlen)< 0) {
|
461 |
fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
|
462 |
fd); |
463 |
closesocket(fd); |
464 |
return NULL; |
465 |
} |
466 |
switch(so_type) {
|
467 |
case SOCK_DGRAM:
|
468 |
return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
|
469 |
case SOCK_STREAM:
|
470 |
return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
|
471 |
default:
|
472 |
/* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
473 |
fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
474 |
return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
|
475 |
} |
476 |
return NULL; |
477 |
} |
478 |
|
479 |
static void net_socket_accept(void *opaque) |
480 |
{ |
481 |
NetSocketState *s = opaque; |
482 |
struct sockaddr_in saddr;
|
483 |
socklen_t len; |
484 |
int fd;
|
485 |
|
486 |
for(;;) {
|
487 |
len = sizeof(saddr);
|
488 |
fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
|
489 |
if (fd < 0 && errno != EINTR) { |
490 |
return;
|
491 |
} else if (fd >= 0) { |
492 |
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
493 |
break;
|
494 |
} |
495 |
} |
496 |
|
497 |
s->fd = fd; |
498 |
s->nc.link_down = false;
|
499 |
net_socket_connect(s); |
500 |
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
501 |
"socket: connection from %s:%d",
|
502 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
503 |
} |
504 |
|
505 |
static int net_socket_listen_init(NetClientState *peer, |
506 |
const char *model, |
507 |
const char *name, |
508 |
const char *host_str) |
509 |
{ |
510 |
NetClientState *nc; |
511 |
NetSocketState *s; |
512 |
struct sockaddr_in saddr;
|
513 |
int fd, val, ret;
|
514 |
|
515 |
if (parse_host_port(&saddr, host_str) < 0) |
516 |
return -1; |
517 |
|
518 |
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
|
519 |
if (fd < 0) { |
520 |
perror("socket");
|
521 |
return -1; |
522 |
} |
523 |
qemu_set_nonblock(fd); |
524 |
|
525 |
/* allow fast reuse */
|
526 |
val = 1;
|
527 |
qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
|
528 |
|
529 |
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
530 |
if (ret < 0) { |
531 |
perror("bind");
|
532 |
closesocket(fd); |
533 |
return -1; |
534 |
} |
535 |
ret = listen(fd, 0);
|
536 |
if (ret < 0) { |
537 |
perror("listen");
|
538 |
closesocket(fd); |
539 |
return -1; |
540 |
} |
541 |
|
542 |
nc = qemu_new_net_client(&net_socket_info, peer, model, name); |
543 |
s = DO_UPCAST(NetSocketState, nc, nc); |
544 |
s->fd = -1;
|
545 |
s->listen_fd = fd; |
546 |
s->nc.link_down = true;
|
547 |
|
548 |
qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
|
549 |
return 0; |
550 |
} |
551 |
|
552 |
static int net_socket_connect_init(NetClientState *peer, |
553 |
const char *model, |
554 |
const char *name, |
555 |
const char *host_str) |
556 |
{ |
557 |
NetSocketState *s; |
558 |
int fd, connected, ret, err;
|
559 |
struct sockaddr_in saddr;
|
560 |
|
561 |
if (parse_host_port(&saddr, host_str) < 0) |
562 |
return -1; |
563 |
|
564 |
fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
|
565 |
if (fd < 0) { |
566 |
perror("socket");
|
567 |
return -1; |
568 |
} |
569 |
qemu_set_nonblock(fd); |
570 |
|
571 |
connected = 0;
|
572 |
for(;;) {
|
573 |
ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
574 |
if (ret < 0) { |
575 |
err = socket_error(); |
576 |
if (err == EINTR || err == EWOULDBLOCK) {
|
577 |
} else if (err == EINPROGRESS) { |
578 |
break;
|
579 |
#ifdef _WIN32
|
580 |
} else if (err == WSAEALREADY || err == WSAEINVAL) { |
581 |
break;
|
582 |
#endif
|
583 |
} else {
|
584 |
perror("connect");
|
585 |
closesocket(fd); |
586 |
return -1; |
587 |
} |
588 |
} else {
|
589 |
connected = 1;
|
590 |
break;
|
591 |
} |
592 |
} |
593 |
s = net_socket_fd_init(peer, model, name, fd, connected); |
594 |
if (!s)
|
595 |
return -1; |
596 |
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
597 |
"socket: connect to %s:%d",
|
598 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
599 |
return 0; |
600 |
} |
601 |
|
602 |
static int net_socket_mcast_init(NetClientState *peer, |
603 |
const char *model, |
604 |
const char *name, |
605 |
const char *host_str, |
606 |
const char *localaddr_str) |
607 |
{ |
608 |
NetSocketState *s; |
609 |
int fd;
|
610 |
struct sockaddr_in saddr;
|
611 |
struct in_addr localaddr, *param_localaddr;
|
612 |
|
613 |
if (parse_host_port(&saddr, host_str) < 0) |
614 |
return -1; |
615 |
|
616 |
if (localaddr_str != NULL) { |
617 |
if (inet_aton(localaddr_str, &localaddr) == 0) |
618 |
return -1; |
619 |
param_localaddr = &localaddr; |
620 |
} else {
|
621 |
param_localaddr = NULL;
|
622 |
} |
623 |
|
624 |
fd = net_socket_mcast_create(&saddr, param_localaddr); |
625 |
if (fd < 0) |
626 |
return -1; |
627 |
|
628 |
s = net_socket_fd_init(peer, model, name, fd, 0);
|
629 |
if (!s)
|
630 |
return -1; |
631 |
|
632 |
s->dgram_dst = saddr; |
633 |
|
634 |
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
635 |
"socket: mcast=%s:%d",
|
636 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
637 |
return 0; |
638 |
|
639 |
} |
640 |
|
641 |
static int net_socket_udp_init(NetClientState *peer, |
642 |
const char *model, |
643 |
const char *name, |
644 |
const char *rhost, |
645 |
const char *lhost) |
646 |
{ |
647 |
NetSocketState *s; |
648 |
int fd, val, ret;
|
649 |
struct sockaddr_in laddr, raddr;
|
650 |
|
651 |
if (parse_host_port(&laddr, lhost) < 0) { |
652 |
return -1; |
653 |
} |
654 |
|
655 |
if (parse_host_port(&raddr, rhost) < 0) { |
656 |
return -1; |
657 |
} |
658 |
|
659 |
fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
|
660 |
if (fd < 0) { |
661 |
perror("socket(PF_INET, SOCK_DGRAM)");
|
662 |
return -1; |
663 |
} |
664 |
val = 1;
|
665 |
ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
666 |
&val, sizeof(val));
|
667 |
if (ret < 0) { |
668 |
perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
669 |
closesocket(fd); |
670 |
return -1; |
671 |
} |
672 |
ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); |
673 |
if (ret < 0) { |
674 |
perror("bind");
|
675 |
closesocket(fd); |
676 |
return -1; |
677 |
} |
678 |
qemu_set_nonblock(fd); |
679 |
|
680 |
s = net_socket_fd_init(peer, model, name, fd, 0);
|
681 |
if (!s) {
|
682 |
return -1; |
683 |
} |
684 |
|
685 |
s->dgram_dst = raddr; |
686 |
|
687 |
snprintf(s->nc.info_str, sizeof(s->nc.info_str),
|
688 |
"socket: udp=%s:%d",
|
689 |
inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port)); |
690 |
return 0; |
691 |
} |
692 |
|
693 |
int net_init_socket(const NetClientOptions *opts, const char *name, |
694 |
NetClientState *peer) |
695 |
{ |
696 |
const NetdevSocketOptions *sock;
|
697 |
|
698 |
assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET); |
699 |
sock = opts->socket; |
700 |
|
701 |
if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
|
702 |
sock->has_udp != 1) {
|
703 |
error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
|
704 |
" is required");
|
705 |
return -1; |
706 |
} |
707 |
|
708 |
if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
|
709 |
error_report("localaddr= is only valid with mcast= or udp=");
|
710 |
return -1; |
711 |
} |
712 |
|
713 |
if (sock->has_fd) {
|
714 |
int fd;
|
715 |
|
716 |
fd = monitor_handle_fd_param(cur_mon, sock->fd); |
717 |
if (fd == -1) { |
718 |
return -1; |
719 |
} |
720 |
qemu_set_nonblock(fd); |
721 |
if (!net_socket_fd_init(peer, "socket", name, fd, 1)) { |
722 |
return -1; |
723 |
} |
724 |
return 0; |
725 |
} |
726 |
|
727 |
if (sock->has_listen) {
|
728 |
if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) { |
729 |
return -1; |
730 |
} |
731 |
return 0; |
732 |
} |
733 |
|
734 |
if (sock->has_connect) {
|
735 |
if (net_socket_connect_init(peer, "socket", name, sock->connect) == |
736 |
-1) {
|
737 |
return -1; |
738 |
} |
739 |
return 0; |
740 |
} |
741 |
|
742 |
if (sock->has_mcast) {
|
743 |
/* if sock->localaddr is missing, it has been initialized to "all bits
|
744 |
* zero" */
|
745 |
if (net_socket_mcast_init(peer, "socket", name, sock->mcast, |
746 |
sock->localaddr) == -1) {
|
747 |
return -1; |
748 |
} |
749 |
return 0; |
750 |
} |
751 |
|
752 |
assert(sock->has_udp); |
753 |
if (!sock->has_localaddr) {
|
754 |
error_report("localaddr= is mandatory with udp=");
|
755 |
return -1; |
756 |
} |
757 |
if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) == |
758 |
-1) {
|
759 |
return -1; |
760 |
} |
761 |
return 0; |
762 |
} |