]>
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 | ||
26 | #include "net.h" | |
a245fc18 | 27 | #include "clients.h" |
42dcc547 | 28 | #include "monitor.h" |
42281ac9 MM |
29 | #include "qemu-char.h" |
30 | #include "qemu-common.h" | |
2f792016 | 31 | #include "qemu-error.h" |
42281ac9 MM |
32 | #include "qemu-option.h" |
33 | #include "qemu_socket.h" | |
45a7f54a | 34 | #include "iov.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) */ |
42281ac9 MM |
44 | uint8_t buf[4096]; |
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 | ||
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 | } | |
42281ac9 | 91 | |
4e68f7a0 | 92 | static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
42281ac9 | 93 | { |
564f63e3 | 94 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
45a7f54a SH |
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; | |
42281ac9 | 107 | |
45a7f54a SH |
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; | |
42281ac9 MM |
125 | } |
126 | ||
4e68f7a0 | 127 | static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size) |
42281ac9 | 128 | { |
564f63e3 | 129 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
213fd508 SH |
130 | ssize_t ret; |
131 | ||
132 | do { | |
73062dfe SW |
133 | ret = qemu_sendto(s->fd, buf, size, 0, |
134 | (struct sockaddr *)&s->dgram_dst, | |
135 | sizeof(s->dgram_dst)); | |
213fd508 SH |
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; | |
42281ac9 MM |
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[4096]; | |
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) { | |
564f63e3 | 214 | qemu_send_packet(&s->nc, s->buf, s->packet_len); |
42281ac9 MM |
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 | ||
00aa0040 | 228 | size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0); |
42281ac9 MM |
229 | if (size < 0) |
230 | return; | |
231 | if (size == 0) { | |
232 | /* end of connection */ | |
863f678f SH |
233 | net_socket_read_poll(s, false); |
234 | net_socket_write_poll(s, false); | |
42281ac9 MM |
235 | return; |
236 | } | |
564f63e3 | 237 | qemu_send_packet(&s->nc, s->buf, size); |
42281ac9 MM |
238 | } |
239 | ||
3a75e74c | 240 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr) |
42281ac9 MM |
241 | { |
242 | struct ip_mreq imr; | |
243 | int fd; | |
244 | int val, ret; | |
23ddf2bb B |
245 | #ifdef __OpenBSD__ |
246 | unsigned char loop; | |
247 | #else | |
248 | int loop; | |
249 | #endif | |
250 | ||
42281ac9 | 251 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
842480d4 SH |
252 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) " |
253 | "does not contain a multicast address\n", | |
254 | inet_ntoa(mcastaddr->sin_addr), | |
42281ac9 | 255 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
842480d4 | 256 | return -1; |
42281ac9 MM |
257 | |
258 | } | |
40ff6d7e | 259 | fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); |
42281ac9 MM |
260 | if (fd < 0) { |
261 | perror("socket(PF_INET, SOCK_DGRAM)"); | |
262 | return -1; | |
263 | } | |
264 | ||
265 | val = 1; | |
266 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, | |
267 | (const char *)&val, sizeof(val)); | |
268 | if (ret < 0) { | |
842480d4 SH |
269 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
270 | goto fail; | |
42281ac9 MM |
271 | } |
272 | ||
273 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); | |
274 | if (ret < 0) { | |
275 | perror("bind"); | |
276 | goto fail; | |
277 | } | |
278 | ||
279 | /* Add host to multicast group */ | |
280 | imr.imr_multiaddr = mcastaddr->sin_addr; | |
3a75e74c MR |
281 | if (localaddr) { |
282 | imr.imr_interface = *localaddr; | |
283 | } else { | |
284 | imr.imr_interface.s_addr = htonl(INADDR_ANY); | |
285 | } | |
42281ac9 MM |
286 | |
287 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
288 | (const char *)&imr, sizeof(struct ip_mreq)); | |
289 | if (ret < 0) { | |
842480d4 SH |
290 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
291 | goto fail; | |
42281ac9 MM |
292 | } |
293 | ||
294 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ | |
23ddf2bb | 295 | loop = 1; |
42281ac9 | 296 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
23ddf2bb | 297 | (const char *)&loop, sizeof(loop)); |
42281ac9 | 298 | if (ret < 0) { |
842480d4 SH |
299 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
300 | goto fail; | |
42281ac9 MM |
301 | } |
302 | ||
3a75e74c MR |
303 | /* If a bind address is given, only send packets from that address */ |
304 | if (localaddr != NULL) { | |
4d22c6c2 BS |
305 | ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
306 | (const char *)localaddr, sizeof(*localaddr)); | |
3a75e74c MR |
307 | if (ret < 0) { |
308 | perror("setsockopt(IP_MULTICAST_IF)"); | |
309 | goto fail; | |
310 | } | |
311 | } | |
312 | ||
42281ac9 MM |
313 | socket_set_nonblock(fd); |
314 | return fd; | |
315 | fail: | |
316 | if (fd >= 0) | |
317 | closesocket(fd); | |
318 | return -1; | |
319 | } | |
320 | ||
4e68f7a0 | 321 | static void net_socket_cleanup(NetClientState *nc) |
42281ac9 | 322 | { |
564f63e3 | 323 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
011de2b5 | 324 | if (s->fd != -1) { |
863f678f SH |
325 | net_socket_read_poll(s, false); |
326 | net_socket_write_poll(s, false); | |
011de2b5 ZYW |
327 | close(s->fd); |
328 | s->fd = -1; | |
329 | } | |
330 | if (s->listen_fd != -1) { | |
331 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); | |
332 | closesocket(s->listen_fd); | |
333 | s->listen_fd = -1; | |
334 | } | |
42281ac9 MM |
335 | } |
336 | ||
564f63e3 | 337 | static NetClientInfo net_dgram_socket_info = { |
2be64a68 | 338 | .type = NET_CLIENT_OPTIONS_KIND_SOCKET, |
564f63e3 MM |
339 | .size = sizeof(NetSocketState), |
340 | .receive = net_socket_receive_dgram, | |
341 | .cleanup = net_socket_cleanup, | |
342 | }; | |
343 | ||
4e68f7a0 | 344 | static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, |
42281ac9 MM |
345 | const char *model, |
346 | const char *name, | |
347 | int fd, int is_connected) | |
348 | { | |
349 | struct sockaddr_in saddr; | |
350 | int newfd; | |
351 | socklen_t saddr_len; | |
4e68f7a0 | 352 | NetClientState *nc; |
42281ac9 MM |
353 | NetSocketState *s; |
354 | ||
355 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it | |
356 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() | |
357 | * by ONLY ONE process: we must "clone" this dgram socket --jjo | |
358 | */ | |
359 | ||
360 | if (is_connected) { | |
842480d4 SH |
361 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
362 | /* must be bound */ | |
363 | if (saddr.sin_addr.s_addr == 0) { | |
364 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, " | |
365 | "cannot setup multicast dst addr\n", fd); | |
e5d1fca0 | 366 | goto err; |
842480d4 SH |
367 | } |
368 | /* clone dgram socket */ | |
369 | newfd = net_socket_mcast_create(&saddr, NULL); | |
370 | if (newfd < 0) { | |
371 | /* error already reported by net_socket_mcast_create() */ | |
e5d1fca0 | 372 | goto err; |
842480d4 SH |
373 | } |
374 | /* clone newfd to fd, close newfd */ | |
375 | dup2(newfd, fd); | |
376 | close(newfd); | |
377 | ||
378 | } else { | |
379 | fprintf(stderr, | |
380 | "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", | |
381 | fd, strerror(errno)); | |
e5d1fca0 | 382 | goto err; |
842480d4 | 383 | } |
42281ac9 MM |
384 | } |
385 | ||
ab5f3f84 | 386 | nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name); |
564f63e3 MM |
387 | |
388 | snprintf(nc->info_str, sizeof(nc->info_str), | |
842480d4 SH |
389 | "socket: fd=%d (%s mcast=%s:%d)", |
390 | fd, is_connected ? "cloned" : "", | |
391 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
564f63e3 MM |
392 | |
393 | s = DO_UPCAST(NetSocketState, nc, nc); | |
394 | ||
42281ac9 | 395 | s->fd = fd; |
011de2b5 | 396 | s->listen_fd = -1; |
863f678f SH |
397 | s->send_fn = net_socket_send_dgram; |
398 | net_socket_read_poll(s, true); | |
42281ac9 MM |
399 | |
400 | /* mcast: save bound address as dst */ | |
e34cde35 ZYW |
401 | if (is_connected) { |
402 | s->dgram_dst = saddr; | |
403 | } | |
42281ac9 | 404 | |
42281ac9 | 405 | return s; |
e5d1fca0 SH |
406 | |
407 | err: | |
408 | closesocket(fd); | |
409 | return NULL; | |
42281ac9 MM |
410 | } |
411 | ||
412 | static void net_socket_connect(void *opaque) | |
413 | { | |
414 | NetSocketState *s = opaque; | |
863f678f SH |
415 | s->send_fn = net_socket_send; |
416 | net_socket_read_poll(s, true); | |
42281ac9 MM |
417 | } |
418 | ||
564f63e3 | 419 | static NetClientInfo net_socket_info = { |
2be64a68 | 420 | .type = NET_CLIENT_OPTIONS_KIND_SOCKET, |
564f63e3 MM |
421 | .size = sizeof(NetSocketState), |
422 | .receive = net_socket_receive, | |
423 | .cleanup = net_socket_cleanup, | |
424 | }; | |
425 | ||
4e68f7a0 | 426 | static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, |
42281ac9 MM |
427 | const char *model, |
428 | const char *name, | |
429 | int fd, int is_connected) | |
430 | { | |
4e68f7a0 | 431 | NetClientState *nc; |
42281ac9 | 432 | NetSocketState *s; |
564f63e3 | 433 | |
ab5f3f84 | 434 | nc = qemu_new_net_client(&net_socket_info, peer, model, name); |
564f63e3 MM |
435 | |
436 | snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd); | |
437 | ||
438 | s = DO_UPCAST(NetSocketState, nc, nc); | |
439 | ||
42281ac9 | 440 | s->fd = fd; |
011de2b5 | 441 | s->listen_fd = -1; |
564f63e3 | 442 | |
42281ac9 MM |
443 | if (is_connected) { |
444 | net_socket_connect(s); | |
445 | } else { | |
446 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); | |
447 | } | |
448 | return s; | |
449 | } | |
450 | ||
4e68f7a0 | 451 | static NetSocketState *net_socket_fd_init(NetClientState *peer, |
42281ac9 MM |
452 | const char *model, const char *name, |
453 | int fd, int is_connected) | |
454 | { | |
455 | int so_type = -1, optlen=sizeof(so_type); | |
456 | ||
457 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, | |
458 | (socklen_t *)&optlen)< 0) { | |
842480d4 SH |
459 | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", |
460 | fd); | |
e5d1fca0 | 461 | closesocket(fd); |
842480d4 | 462 | return NULL; |
42281ac9 MM |
463 | } |
464 | switch(so_type) { | |
465 | case SOCK_DGRAM: | |
d33d93b2 | 466 | return net_socket_fd_init_dgram(peer, model, name, fd, is_connected); |
42281ac9 | 467 | case SOCK_STREAM: |
d33d93b2 | 468 | return net_socket_fd_init_stream(peer, model, name, fd, is_connected); |
42281ac9 MM |
469 | default: |
470 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ | |
471 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); | |
d33d93b2 | 472 | return net_socket_fd_init_stream(peer, model, name, fd, is_connected); |
42281ac9 MM |
473 | } |
474 | return NULL; | |
475 | } | |
476 | ||
477 | static void net_socket_accept(void *opaque) | |
478 | { | |
011de2b5 | 479 | NetSocketState *s = opaque; |
42281ac9 MM |
480 | struct sockaddr_in saddr; |
481 | socklen_t len; | |
482 | int fd; | |
483 | ||
484 | for(;;) { | |
485 | len = sizeof(saddr); | |
011de2b5 | 486 | fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len); |
42281ac9 MM |
487 | if (fd < 0 && errno != EINTR) { |
488 | return; | |
489 | } else if (fd >= 0) { | |
011de2b5 | 490 | qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
42281ac9 MM |
491 | break; |
492 | } | |
493 | } | |
011de2b5 ZYW |
494 | |
495 | s->fd = fd; | |
496 | s->nc.link_down = false; | |
497 | net_socket_connect(s); | |
498 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), | |
499 | "socket: connection from %s:%d", | |
500 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
42281ac9 MM |
501 | } |
502 | ||
4e68f7a0 | 503 | static int net_socket_listen_init(NetClientState *peer, |
42281ac9 MM |
504 | const char *model, |
505 | const char *name, | |
506 | const char *host_str) | |
507 | { | |
011de2b5 ZYW |
508 | NetClientState *nc; |
509 | NetSocketState *s; | |
42281ac9 | 510 | struct sockaddr_in saddr; |
011de2b5 | 511 | int fd, val, ret; |
42281ac9 MM |
512 | |
513 | if (parse_host_port(&saddr, host_str) < 0) | |
514 | return -1; | |
515 | ||
40ff6d7e | 516 | fd = qemu_socket(PF_INET, SOCK_STREAM, 0); |
42281ac9 MM |
517 | if (fd < 0) { |
518 | perror("socket"); | |
519 | return -1; | |
520 | } | |
521 | socket_set_nonblock(fd); | |
522 | ||
523 | /* allow fast reuse */ | |
524 | val = 1; | |
525 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); | |
526 | ||
527 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
528 | if (ret < 0) { | |
529 | perror("bind"); | |
a46667ea | 530 | closesocket(fd); |
42281ac9 MM |
531 | return -1; |
532 | } | |
533 | ret = listen(fd, 0); | |
534 | if (ret < 0) { | |
535 | perror("listen"); | |
a46667ea | 536 | closesocket(fd); |
42281ac9 MM |
537 | return -1; |
538 | } | |
011de2b5 ZYW |
539 | |
540 | nc = qemu_new_net_client(&net_socket_info, peer, model, name); | |
541 | s = DO_UPCAST(NetSocketState, nc, nc); | |
542 | s->fd = -1; | |
543 | s->listen_fd = fd; | |
544 | s->nc.link_down = true; | |
545 | ||
546 | qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); | |
42281ac9 MM |
547 | return 0; |
548 | } | |
549 | ||
4e68f7a0 | 550 | static int net_socket_connect_init(NetClientState *peer, |
42281ac9 MM |
551 | const char *model, |
552 | const char *name, | |
553 | const char *host_str) | |
554 | { | |
555 | NetSocketState *s; | |
556 | int fd, connected, ret, err; | |
557 | struct sockaddr_in saddr; | |
558 | ||
559 | if (parse_host_port(&saddr, host_str) < 0) | |
560 | return -1; | |
561 | ||
40ff6d7e | 562 | fd = qemu_socket(PF_INET, SOCK_STREAM, 0); |
42281ac9 MM |
563 | if (fd < 0) { |
564 | perror("socket"); | |
565 | return -1; | |
566 | } | |
567 | socket_set_nonblock(fd); | |
568 | ||
569 | connected = 0; | |
570 | for(;;) { | |
571 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
572 | if (ret < 0) { | |
573 | err = socket_error(); | |
574 | if (err == EINTR || err == EWOULDBLOCK) { | |
575 | } else if (err == EINPROGRESS) { | |
576 | break; | |
577 | #ifdef _WIN32 | |
c7eb1f02 | 578 | } else if (err == WSAEALREADY || err == WSAEINVAL) { |
42281ac9 MM |
579 | break; |
580 | #endif | |
581 | } else { | |
582 | perror("connect"); | |
583 | closesocket(fd); | |
584 | return -1; | |
585 | } | |
586 | } else { | |
587 | connected = 1; | |
588 | break; | |
589 | } | |
590 | } | |
d33d93b2 | 591 | s = net_socket_fd_init(peer, model, name, fd, connected); |
42281ac9 MM |
592 | if (!s) |
593 | return -1; | |
564f63e3 | 594 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
42281ac9 MM |
595 | "socket: connect to %s:%d", |
596 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
597 | return 0; | |
598 | } | |
599 | ||
4e68f7a0 | 600 | static int net_socket_mcast_init(NetClientState *peer, |
42281ac9 MM |
601 | const char *model, |
602 | const char *name, | |
3a75e74c MR |
603 | const char *host_str, |
604 | const char *localaddr_str) | |
42281ac9 MM |
605 | { |
606 | NetSocketState *s; | |
607 | int fd; | |
608 | struct sockaddr_in saddr; | |
3a75e74c | 609 | struct in_addr localaddr, *param_localaddr; |
42281ac9 MM |
610 | |
611 | if (parse_host_port(&saddr, host_str) < 0) | |
612 | return -1; | |
613 | ||
3a75e74c MR |
614 | if (localaddr_str != NULL) { |
615 | if (inet_aton(localaddr_str, &localaddr) == 0) | |
616 | return -1; | |
617 | param_localaddr = &localaddr; | |
618 | } else { | |
619 | param_localaddr = NULL; | |
620 | } | |
42281ac9 | 621 | |
3a75e74c | 622 | fd = net_socket_mcast_create(&saddr, param_localaddr); |
42281ac9 | 623 | if (fd < 0) |
842480d4 | 624 | return -1; |
42281ac9 | 625 | |
d33d93b2 | 626 | s = net_socket_fd_init(peer, model, name, fd, 0); |
42281ac9 MM |
627 | if (!s) |
628 | return -1; | |
629 | ||
630 | s->dgram_dst = saddr; | |
631 | ||
564f63e3 | 632 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
42281ac9 MM |
633 | "socket: mcast=%s:%d", |
634 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
635 | return 0; | |
636 | ||
637 | } | |
638 | ||
4e68f7a0 | 639 | static int net_socket_udp_init(NetClientState *peer, |
0e0e7fac B |
640 | const char *model, |
641 | const char *name, | |
642 | const char *rhost, | |
643 | const char *lhost) | |
644 | { | |
645 | NetSocketState *s; | |
646 | int fd, val, ret; | |
647 | struct sockaddr_in laddr, raddr; | |
648 | ||
649 | if (parse_host_port(&laddr, lhost) < 0) { | |
650 | return -1; | |
651 | } | |
652 | ||
653 | if (parse_host_port(&raddr, rhost) < 0) { | |
654 | return -1; | |
655 | } | |
656 | ||
657 | fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); | |
658 | if (fd < 0) { | |
659 | perror("socket(PF_INET, SOCK_DGRAM)"); | |
660 | return -1; | |
661 | } | |
662 | val = 1; | |
663 | ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, | |
664 | (const char *)&val, sizeof(val)); | |
665 | if (ret < 0) { | |
666 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); | |
667 | closesocket(fd); | |
668 | return -1; | |
669 | } | |
670 | ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); | |
671 | if (ret < 0) { | |
672 | perror("bind"); | |
673 | closesocket(fd); | |
674 | return -1; | |
675 | } | |
676 | ||
d33d93b2 | 677 | s = net_socket_fd_init(peer, model, name, fd, 0); |
0e0e7fac B |
678 | if (!s) { |
679 | return -1; | |
680 | } | |
681 | ||
682 | s->dgram_dst = raddr; | |
683 | ||
684 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), | |
685 | "socket: udp=%s:%d", | |
686 | inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port)); | |
687 | return 0; | |
688 | } | |
689 | ||
1a0c0958 | 690 | int net_init_socket(const NetClientOptions *opts, const char *name, |
4e68f7a0 | 691 | NetClientState *peer) |
42281ac9 | 692 | { |
bef8e8fe | 693 | const NetdevSocketOptions *sock; |
42281ac9 | 694 | |
bef8e8fe LE |
695 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET); |
696 | sock = opts->socket; | |
42281ac9 | 697 | |
bef8e8fe LE |
698 | if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast + |
699 | sock->has_udp != 1) { | |
700 | error_report("exactly one of fd=, listen=, connect=, mcast= or udp=" | |
701 | " is required"); | |
702 | return -1; | |
703 | } | |
42281ac9 | 704 | |
bef8e8fe LE |
705 | if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) { |
706 | error_report("localaddr= is only valid with mcast= or udp="); | |
707 | return -1; | |
708 | } | |
42281ac9 | 709 | |
bef8e8fe LE |
710 | if (sock->has_fd) { |
711 | int fd; | |
42281ac9 | 712 | |
a96ed02f | 713 | fd = monitor_handle_fd_param(cur_mon, sock->fd); |
d33d93b2 | 714 | if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) { |
42281ac9 MM |
715 | return -1; |
716 | } | |
bef8e8fe LE |
717 | return 0; |
718 | } | |
42281ac9 | 719 | |
bef8e8fe | 720 | if (sock->has_listen) { |
d33d93b2 | 721 | if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) { |
42281ac9 MM |
722 | return -1; |
723 | } | |
bef8e8fe LE |
724 | return 0; |
725 | } | |
42281ac9 | 726 | |
bef8e8fe | 727 | if (sock->has_connect) { |
d33d93b2 | 728 | if (net_socket_connect_init(peer, "socket", name, sock->connect) == |
bef8e8fe | 729 | -1) { |
42281ac9 MM |
730 | return -1; |
731 | } | |
bef8e8fe LE |
732 | return 0; |
733 | } | |
42281ac9 | 734 | |
bef8e8fe LE |
735 | if (sock->has_mcast) { |
736 | /* if sock->localaddr is missing, it has been initialized to "all bits | |
737 | * zero" */ | |
d33d93b2 | 738 | if (net_socket_mcast_init(peer, "socket", name, sock->mcast, |
bef8e8fe | 739 | sock->localaddr) == -1) { |
0e0e7fac B |
740 | return -1; |
741 | } | |
bef8e8fe LE |
742 | return 0; |
743 | } | |
0e0e7fac | 744 | |
bef8e8fe LE |
745 | assert(sock->has_udp); |
746 | if (!sock->has_localaddr) { | |
747 | error_report("localaddr= is mandatory with udp="); | |
748 | return -1; | |
749 | } | |
f0e3ac70 | 750 | if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) == |
bef8e8fe | 751 | -1) { |
42281ac9 MM |
752 | return -1; |
753 | } | |
42281ac9 MM |
754 | return 0; |
755 | } |