]>
Commit | Line | Data |
---|---|---|
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 | /* Allow multiple sockets to bind the same multicast ip and port by setting | |
266 | * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set | |
267 | * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR | |
268 | * only on posix systems. | |
269 | */ | |
270 | val = 1; | |
271 | ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); | |
272 | if (ret < 0) { | |
273 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); | |
274 | goto fail; | |
275 | } | |
276 | ||
277 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); | |
278 | if (ret < 0) { | |
279 | perror("bind"); | |
280 | goto fail; | |
281 | } | |
282 | ||
283 | /* Add host to multicast group */ | |
284 | imr.imr_multiaddr = mcastaddr->sin_addr; | |
285 | if (localaddr) { | |
286 | imr.imr_interface = *localaddr; | |
287 | } else { | |
288 | imr.imr_interface.s_addr = htonl(INADDR_ANY); | |
289 | } | |
290 | ||
291 | ret = qemu_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
292 | &imr, sizeof(struct ip_mreq)); | |
293 | if (ret < 0) { | |
294 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); | |
295 | goto fail; | |
296 | } | |
297 | ||
298 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ | |
299 | loop = 1; | |
300 | ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, | |
301 | &loop, sizeof(loop)); | |
302 | if (ret < 0) { | |
303 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); | |
304 | goto fail; | |
305 | } | |
306 | ||
307 | /* If a bind address is given, only send packets from that address */ | |
308 | if (localaddr != NULL) { | |
309 | ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, | |
310 | localaddr, sizeof(*localaddr)); | |
311 | if (ret < 0) { | |
312 | perror("setsockopt(IP_MULTICAST_IF)"); | |
313 | goto fail; | |
314 | } | |
315 | } | |
316 | ||
317 | qemu_set_nonblock(fd); | |
318 | return fd; | |
319 | fail: | |
320 | if (fd >= 0) | |
321 | closesocket(fd); | |
322 | return -1; | |
323 | } | |
324 | ||
325 | static void net_socket_cleanup(NetClientState *nc) | |
326 | { | |
327 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); | |
328 | if (s->fd != -1) { | |
329 | net_socket_read_poll(s, false); | |
330 | net_socket_write_poll(s, false); | |
331 | close(s->fd); | |
332 | s->fd = -1; | |
333 | } | |
334 | if (s->listen_fd != -1) { | |
335 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); | |
336 | closesocket(s->listen_fd); | |
337 | s->listen_fd = -1; | |
338 | } | |
339 | } | |
340 | ||
341 | static NetClientInfo net_dgram_socket_info = { | |
342 | .type = NET_CLIENT_OPTIONS_KIND_SOCKET, | |
343 | .size = sizeof(NetSocketState), | |
344 | .receive = net_socket_receive_dgram, | |
345 | .cleanup = net_socket_cleanup, | |
346 | }; | |
347 | ||
348 | static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, | |
349 | const char *model, | |
350 | const char *name, | |
351 | int fd, int is_connected) | |
352 | { | |
353 | struct sockaddr_in saddr; | |
354 | int newfd; | |
355 | socklen_t saddr_len = sizeof(saddr); | |
356 | NetClientState *nc; | |
357 | NetSocketState *s; | |
358 | ||
359 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it | |
360 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() | |
361 | * by ONLY ONE process: we must "clone" this dgram socket --jjo | |
362 | */ | |
363 | ||
364 | if (is_connected) { | |
365 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { | |
366 | /* must be bound */ | |
367 | if (saddr.sin_addr.s_addr == 0) { | |
368 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, " | |
369 | "cannot setup multicast dst addr\n", fd); | |
370 | goto err; | |
371 | } | |
372 | /* clone dgram socket */ | |
373 | newfd = net_socket_mcast_create(&saddr, NULL); | |
374 | if (newfd < 0) { | |
375 | /* error already reported by net_socket_mcast_create() */ | |
376 | goto err; | |
377 | } | |
378 | /* clone newfd to fd, close newfd */ | |
379 | dup2(newfd, fd); | |
380 | close(newfd); | |
381 | ||
382 | } else { | |
383 | fprintf(stderr, | |
384 | "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", | |
385 | fd, strerror(errno)); | |
386 | goto err; | |
387 | } | |
388 | } | |
389 | ||
390 | nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name); | |
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 | snprintf(nc->info_str, sizeof(nc->info_str), | |
403 | "socket: fd=%d (cloned mcast=%s:%d)", | |
404 | fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
405 | } else { | |
406 | snprintf(nc->info_str, sizeof(nc->info_str), | |
407 | "socket: fd=%d", fd); | |
408 | } | |
409 | ||
410 | return s; | |
411 | ||
412 | err: | |
413 | closesocket(fd); | |
414 | return NULL; | |
415 | } | |
416 | ||
417 | static void net_socket_connect(void *opaque) | |
418 | { | |
419 | NetSocketState *s = opaque; | |
420 | s->send_fn = net_socket_send; | |
421 | net_socket_read_poll(s, true); | |
422 | } | |
423 | ||
424 | static NetClientInfo net_socket_info = { | |
425 | .type = NET_CLIENT_OPTIONS_KIND_SOCKET, | |
426 | .size = sizeof(NetSocketState), | |
427 | .receive = net_socket_receive, | |
428 | .cleanup = net_socket_cleanup, | |
429 | }; | |
430 | ||
431 | static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, | |
432 | const char *model, | |
433 | const char *name, | |
434 | int fd, int is_connected) | |
435 | { | |
436 | NetClientState *nc; | |
437 | NetSocketState *s; | |
438 | ||
439 | nc = qemu_new_net_client(&net_socket_info, peer, model, name); | |
440 | ||
441 | snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd); | |
442 | ||
443 | s = DO_UPCAST(NetSocketState, nc, nc); | |
444 | ||
445 | s->fd = fd; | |
446 | s->listen_fd = -1; | |
447 | ||
448 | /* Disable Nagle algorithm on TCP sockets to reduce latency */ | |
449 | socket_set_nodelay(fd); | |
450 | ||
451 | if (is_connected) { | |
452 | net_socket_connect(s); | |
453 | } else { | |
454 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); | |
455 | } | |
456 | return s; | |
457 | } | |
458 | ||
459 | static NetSocketState *net_socket_fd_init(NetClientState *peer, | |
460 | const char *model, const char *name, | |
461 | int fd, int is_connected) | |
462 | { | |
463 | int so_type = -1, optlen=sizeof(so_type); | |
464 | ||
465 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, | |
466 | (socklen_t *)&optlen)< 0) { | |
467 | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", | |
468 | fd); | |
469 | closesocket(fd); | |
470 | return NULL; | |
471 | } | |
472 | switch(so_type) { | |
473 | case SOCK_DGRAM: | |
474 | return net_socket_fd_init_dgram(peer, model, name, fd, is_connected); | |
475 | case SOCK_STREAM: | |
476 | return net_socket_fd_init_stream(peer, model, name, fd, is_connected); | |
477 | default: | |
478 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ | |
479 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); | |
480 | return net_socket_fd_init_stream(peer, model, name, fd, is_connected); | |
481 | } | |
482 | return NULL; | |
483 | } | |
484 | ||
485 | static void net_socket_accept(void *opaque) | |
486 | { | |
487 | NetSocketState *s = opaque; | |
488 | struct sockaddr_in saddr; | |
489 | socklen_t len; | |
490 | int fd; | |
491 | ||
492 | for(;;) { | |
493 | len = sizeof(saddr); | |
494 | fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len); | |
495 | if (fd < 0 && errno != EINTR) { | |
496 | return; | |
497 | } else if (fd >= 0) { | |
498 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); | |
499 | break; | |
500 | } | |
501 | } | |
502 | ||
503 | s->fd = fd; | |
504 | s->nc.link_down = false; | |
505 | net_socket_connect(s); | |
506 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), | |
507 | "socket: connection from %s:%d", | |
508 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
509 | } | |
510 | ||
511 | static int net_socket_listen_init(NetClientState *peer, | |
512 | const char *model, | |
513 | const char *name, | |
514 | const char *host_str) | |
515 | { | |
516 | NetClientState *nc; | |
517 | NetSocketState *s; | |
518 | struct sockaddr_in saddr; | |
519 | int fd, ret; | |
520 | ||
521 | if (parse_host_port(&saddr, host_str) < 0) | |
522 | return -1; | |
523 | ||
524 | fd = qemu_socket(PF_INET, SOCK_STREAM, 0); | |
525 | if (fd < 0) { | |
526 | perror("socket"); | |
527 | return -1; | |
528 | } | |
529 | qemu_set_nonblock(fd); | |
530 | ||
531 | socket_set_fast_reuse(fd); | |
532 | ||
533 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
534 | if (ret < 0) { | |
535 | perror("bind"); | |
536 | closesocket(fd); | |
537 | return -1; | |
538 | } | |
539 | ret = listen(fd, 0); | |
540 | if (ret < 0) { | |
541 | perror("listen"); | |
542 | closesocket(fd); | |
543 | return -1; | |
544 | } | |
545 | ||
546 | nc = qemu_new_net_client(&net_socket_info, peer, model, name); | |
547 | s = DO_UPCAST(NetSocketState, nc, nc); | |
548 | s->fd = -1; | |
549 | s->listen_fd = fd; | |
550 | s->nc.link_down = true; | |
551 | ||
552 | qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); | |
553 | return 0; | |
554 | } | |
555 | ||
556 | static int net_socket_connect_init(NetClientState *peer, | |
557 | const char *model, | |
558 | const char *name, | |
559 | const char *host_str) | |
560 | { | |
561 | NetSocketState *s; | |
562 | int fd, connected, ret, err; | |
563 | struct sockaddr_in saddr; | |
564 | ||
565 | if (parse_host_port(&saddr, host_str) < 0) | |
566 | return -1; | |
567 | ||
568 | fd = qemu_socket(PF_INET, SOCK_STREAM, 0); | |
569 | if (fd < 0) { | |
570 | perror("socket"); | |
571 | return -1; | |
572 | } | |
573 | qemu_set_nonblock(fd); | |
574 | ||
575 | connected = 0; | |
576 | for(;;) { | |
577 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
578 | if (ret < 0) { | |
579 | err = socket_error(); | |
580 | if (err == EINTR || err == EWOULDBLOCK) { | |
581 | } else if (err == EINPROGRESS) { | |
582 | break; | |
583 | #ifdef _WIN32 | |
584 | } else if (err == WSAEALREADY || err == WSAEINVAL) { | |
585 | break; | |
586 | #endif | |
587 | } else { | |
588 | perror("connect"); | |
589 | closesocket(fd); | |
590 | return -1; | |
591 | } | |
592 | } else { | |
593 | connected = 1; | |
594 | break; | |
595 | } | |
596 | } | |
597 | s = net_socket_fd_init(peer, model, name, fd, connected); | |
598 | if (!s) | |
599 | return -1; | |
600 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), | |
601 | "socket: connect to %s:%d", | |
602 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
603 | return 0; | |
604 | } | |
605 | ||
606 | static int net_socket_mcast_init(NetClientState *peer, | |
607 | const char *model, | |
608 | const char *name, | |
609 | const char *host_str, | |
610 | const char *localaddr_str) | |
611 | { | |
612 | NetSocketState *s; | |
613 | int fd; | |
614 | struct sockaddr_in saddr; | |
615 | struct in_addr localaddr, *param_localaddr; | |
616 | ||
617 | if (parse_host_port(&saddr, host_str) < 0) | |
618 | return -1; | |
619 | ||
620 | if (localaddr_str != NULL) { | |
621 | if (inet_aton(localaddr_str, &localaddr) == 0) | |
622 | return -1; | |
623 | param_localaddr = &localaddr; | |
624 | } else { | |
625 | param_localaddr = NULL; | |
626 | } | |
627 | ||
628 | fd = net_socket_mcast_create(&saddr, param_localaddr); | |
629 | if (fd < 0) | |
630 | return -1; | |
631 | ||
632 | s = net_socket_fd_init(peer, model, name, fd, 0); | |
633 | if (!s) | |
634 | return -1; | |
635 | ||
636 | s->dgram_dst = saddr; | |
637 | ||
638 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), | |
639 | "socket: mcast=%s:%d", | |
640 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
641 | return 0; | |
642 | ||
643 | } | |
644 | ||
645 | static int net_socket_udp_init(NetClientState *peer, | |
646 | const char *model, | |
647 | const char *name, | |
648 | const char *rhost, | |
649 | const char *lhost) | |
650 | { | |
651 | NetSocketState *s; | |
652 | int fd, ret; | |
653 | struct sockaddr_in laddr, raddr; | |
654 | ||
655 | if (parse_host_port(&laddr, lhost) < 0) { | |
656 | return -1; | |
657 | } | |
658 | ||
659 | if (parse_host_port(&raddr, rhost) < 0) { | |
660 | return -1; | |
661 | } | |
662 | ||
663 | fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); | |
664 | if (fd < 0) { | |
665 | perror("socket(PF_INET, SOCK_DGRAM)"); | |
666 | return -1; | |
667 | } | |
668 | ||
669 | ret = socket_set_fast_reuse(fd); | |
670 | if (ret < 0) { | |
671 | closesocket(fd); | |
672 | return -1; | |
673 | } | |
674 | ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); | |
675 | if (ret < 0) { | |
676 | perror("bind"); | |
677 | closesocket(fd); | |
678 | return -1; | |
679 | } | |
680 | qemu_set_nonblock(fd); | |
681 | ||
682 | s = net_socket_fd_init(peer, model, name, fd, 0); | |
683 | if (!s) { | |
684 | return -1; | |
685 | } | |
686 | ||
687 | s->dgram_dst = raddr; | |
688 | ||
689 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), | |
690 | "socket: udp=%s:%d", | |
691 | inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port)); | |
692 | return 0; | |
693 | } | |
694 | ||
695 | int net_init_socket(const NetClientOptions *opts, const char *name, | |
696 | NetClientState *peer) | |
697 | { | |
698 | Error *err = NULL; | |
699 | const NetdevSocketOptions *sock; | |
700 | ||
701 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET); | |
702 | sock = opts->socket; | |
703 | ||
704 | if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast + | |
705 | sock->has_udp != 1) { | |
706 | error_report("exactly one of fd=, listen=, connect=, mcast= or udp=" | |
707 | " is required"); | |
708 | return -1; | |
709 | } | |
710 | ||
711 | if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) { | |
712 | error_report("localaddr= is only valid with mcast= or udp="); | |
713 | return -1; | |
714 | } | |
715 | ||
716 | if (sock->has_fd) { | |
717 | int fd; | |
718 | ||
719 | fd = monitor_fd_param(cur_mon, sock->fd, &err); | |
720 | if (fd == -1) { | |
721 | error_report_err(err); | |
722 | return -1; | |
723 | } | |
724 | qemu_set_nonblock(fd); | |
725 | if (!net_socket_fd_init(peer, "socket", name, fd, 1)) { | |
726 | return -1; | |
727 | } | |
728 | return 0; | |
729 | } | |
730 | ||
731 | if (sock->has_listen) { | |
732 | if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) { | |
733 | return -1; | |
734 | } | |
735 | return 0; | |
736 | } | |
737 | ||
738 | if (sock->has_connect) { | |
739 | if (net_socket_connect_init(peer, "socket", name, sock->connect) == | |
740 | -1) { | |
741 | return -1; | |
742 | } | |
743 | return 0; | |
744 | } | |
745 | ||
746 | if (sock->has_mcast) { | |
747 | /* if sock->localaddr is missing, it has been initialized to "all bits | |
748 | * zero" */ | |
749 | if (net_socket_mcast_init(peer, "socket", name, sock->mcast, | |
750 | sock->localaddr) == -1) { | |
751 | return -1; | |
752 | } | |
753 | return 0; | |
754 | } | |
755 | ||
756 | assert(sock->has_udp); | |
757 | if (!sock->has_localaddr) { | |
758 | error_report("localaddr= is mandatory with udp="); | |
759 | return -1; | |
760 | } | |
761 | if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) == | |
762 | -1) { | |
763 | return -1; | |
764 | } | |
765 | return 0; | |
766 | } |