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