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