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