Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 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" |
Luiz Capitulino | 42dcc54 | 2012-04-12 13:20:40 -0300 | [diff] [blame] | 29 | #include "monitor.h" |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 30 | #include "qemu-char.h" |
| 31 | #include "qemu-common.h" |
Markus Armbruster | 2f79201 | 2010-02-18 16:24:31 +0100 | [diff] [blame] | 32 | #include "qemu-error.h" |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 33 | #include "qemu-option.h" |
| 34 | #include "qemu_socket.h" |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 35 | |
| 36 | typedef struct NetSocketState { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 37 | VLANClientState nc; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 38 | int fd; |
| 39 | int state; /* 0 = getting length, 1 = getting data */ |
| 40 | unsigned int index; |
| 41 | unsigned int packet_len; |
| 42 | uint8_t buf[4096]; |
| 43 | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
| 44 | } NetSocketState; |
| 45 | |
| 46 | typedef struct NetSocketListenState { |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 47 | VLANClientState *peer; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 48 | char *model; |
| 49 | char *name; |
| 50 | int fd; |
| 51 | } NetSocketListenState; |
| 52 | |
| 53 | /* XXX: we consider we can send the whole packet without blocking */ |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 54 | static ssize_t net_socket_receive(VLANClientState *nc, const uint8_t *buf, size_t size) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 55 | { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 56 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 57 | uint32_t len; |
| 58 | len = htonl(size); |
| 59 | |
| 60 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
| 61 | return send_all(s->fd, buf, size); |
| 62 | } |
| 63 | |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 64 | static ssize_t net_socket_receive_dgram(VLANClientState *nc, const uint8_t *buf, size_t size) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 65 | { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 66 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 67 | |
| 68 | return sendto(s->fd, (const void *)buf, size, 0, |
| 69 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
| 70 | } |
| 71 | |
| 72 | static void net_socket_send(void *opaque) |
| 73 | { |
| 74 | NetSocketState *s = opaque; |
| 75 | int size, err; |
| 76 | unsigned l; |
| 77 | uint8_t buf1[4096]; |
| 78 | const uint8_t *buf; |
| 79 | |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 80 | size = qemu_recv(s->fd, buf1, sizeof(buf1), 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 81 | if (size < 0) { |
| 82 | err = socket_error(); |
| 83 | if (err != EWOULDBLOCK) |
| 84 | goto eoc; |
| 85 | } else if (size == 0) { |
| 86 | /* end of connection */ |
| 87 | eoc: |
| 88 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 89 | closesocket(s->fd); |
| 90 | return; |
| 91 | } |
| 92 | buf = buf1; |
| 93 | while (size > 0) { |
| 94 | /* reassemble a packet from the network */ |
| 95 | switch(s->state) { |
| 96 | case 0: |
| 97 | l = 4 - s->index; |
| 98 | if (l > size) |
| 99 | l = size; |
| 100 | memcpy(s->buf + s->index, buf, l); |
| 101 | buf += l; |
| 102 | size -= l; |
| 103 | s->index += l; |
| 104 | if (s->index == 4) { |
| 105 | /* got length */ |
| 106 | s->packet_len = ntohl(*(uint32_t *)s->buf); |
| 107 | s->index = 0; |
| 108 | s->state = 1; |
| 109 | } |
| 110 | break; |
| 111 | case 1: |
| 112 | l = s->packet_len - s->index; |
| 113 | if (l > size) |
| 114 | l = size; |
| 115 | if (s->index + l <= sizeof(s->buf)) { |
| 116 | memcpy(s->buf + s->index, buf, l); |
| 117 | } else { |
| 118 | fprintf(stderr, "serious error: oversized packet received," |
| 119 | "connection terminated.\n"); |
| 120 | s->state = 0; |
| 121 | goto eoc; |
| 122 | } |
| 123 | |
| 124 | s->index += l; |
| 125 | buf += l; |
| 126 | size -= l; |
| 127 | if (s->index >= s->packet_len) { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 128 | qemu_send_packet(&s->nc, s->buf, s->packet_len); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 129 | s->index = 0; |
| 130 | s->state = 0; |
| 131 | } |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void net_socket_send_dgram(void *opaque) |
| 138 | { |
| 139 | NetSocketState *s = opaque; |
| 140 | int size; |
| 141 | |
Blue Swirl | 00aa004 | 2011-07-23 20:04:29 +0000 | [diff] [blame] | 142 | size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 143 | if (size < 0) |
| 144 | return; |
| 145 | if (size == 0) { |
| 146 | /* end of connection */ |
| 147 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 148 | return; |
| 149 | } |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 150 | qemu_send_packet(&s->nc, s->buf, size); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 153 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 154 | { |
| 155 | struct ip_mreq imr; |
| 156 | int fd; |
| 157 | int val, ret; |
Brad | 23ddf2b | 2011-08-07 11:06:43 +0000 | [diff] [blame] | 158 | #ifdef __OpenBSD__ |
| 159 | unsigned char loop; |
| 160 | #else |
| 161 | int loop; |
| 162 | #endif |
| 163 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 164 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 165 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) " |
| 166 | "does not contain a multicast address\n", |
| 167 | inet_ntoa(mcastaddr->sin_addr), |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 168 | (int)ntohl(mcastaddr->sin_addr.s_addr)); |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 169 | return -1; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 170 | |
| 171 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 172 | fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 173 | if (fd < 0) { |
| 174 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | val = 1; |
| 179 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 180 | (const char *)&val, sizeof(val)); |
| 181 | if (ret < 0) { |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 182 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
| 183 | goto fail; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
| 187 | if (ret < 0) { |
| 188 | perror("bind"); |
| 189 | goto fail; |
| 190 | } |
| 191 | |
| 192 | /* Add host to multicast group */ |
| 193 | imr.imr_multiaddr = mcastaddr->sin_addr; |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 194 | if (localaddr) { |
| 195 | imr.imr_interface = *localaddr; |
| 196 | } else { |
| 197 | imr.imr_interface.s_addr = htonl(INADDR_ANY); |
| 198 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 199 | |
| 200 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
| 201 | (const char *)&imr, sizeof(struct ip_mreq)); |
| 202 | if (ret < 0) { |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 203 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
| 204 | goto fail; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
Brad | 23ddf2b | 2011-08-07 11:06:43 +0000 | [diff] [blame] | 208 | loop = 1; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 209 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
Brad | 23ddf2b | 2011-08-07 11:06:43 +0000 | [diff] [blame] | 210 | (const char *)&loop, sizeof(loop)); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 211 | if (ret < 0) { |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 212 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
| 213 | goto fail; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 216 | /* If a bind address is given, only send packets from that address */ |
| 217 | if (localaddr != NULL) { |
Blue Swirl | 4d22c6c | 2010-12-17 21:03:00 +0000 | [diff] [blame] | 218 | ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, |
| 219 | (const char *)localaddr, sizeof(*localaddr)); |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 220 | if (ret < 0) { |
| 221 | perror("setsockopt(IP_MULTICAST_IF)"); |
| 222 | goto fail; |
| 223 | } |
| 224 | } |
| 225 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 226 | socket_set_nonblock(fd); |
| 227 | return fd; |
| 228 | fail: |
| 229 | if (fd >= 0) |
| 230 | closesocket(fd); |
| 231 | return -1; |
| 232 | } |
| 233 | |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 234 | static void net_socket_cleanup(VLANClientState *nc) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 235 | { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 236 | NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 237 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
| 238 | close(s->fd); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 241 | static NetClientInfo net_dgram_socket_info = { |
Laszlo Ersek | 2be64a6 | 2012-07-17 16:17:12 +0200 | [diff] [blame] | 242 | .type = NET_CLIENT_OPTIONS_KIND_SOCKET, |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 243 | .size = sizeof(NetSocketState), |
| 244 | .receive = net_socket_receive_dgram, |
| 245 | .cleanup = net_socket_cleanup, |
| 246 | }; |
| 247 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 248 | static NetSocketState *net_socket_fd_init_dgram(VLANClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 249 | const char *model, |
| 250 | const char *name, |
| 251 | int fd, int is_connected) |
| 252 | { |
| 253 | struct sockaddr_in saddr; |
| 254 | int newfd; |
| 255 | socklen_t saddr_len; |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 256 | VLANClientState *nc; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 257 | NetSocketState *s; |
| 258 | |
| 259 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
| 260 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
| 261 | * by ONLY ONE process: we must "clone" this dgram socket --jjo |
| 262 | */ |
| 263 | |
| 264 | if (is_connected) { |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 265 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
| 266 | /* must be bound */ |
| 267 | if (saddr.sin_addr.s_addr == 0) { |
| 268 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, " |
| 269 | "cannot setup multicast dst addr\n", fd); |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 270 | goto err; |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 271 | } |
| 272 | /* clone dgram socket */ |
| 273 | newfd = net_socket_mcast_create(&saddr, NULL); |
| 274 | if (newfd < 0) { |
| 275 | /* error already reported by net_socket_mcast_create() */ |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 276 | goto err; |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 277 | } |
| 278 | /* clone newfd to fd, close newfd */ |
| 279 | dup2(newfd, fd); |
| 280 | close(newfd); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 281 | |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 282 | } else { |
| 283 | fprintf(stderr, |
| 284 | "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", |
| 285 | fd, strerror(errno)); |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 286 | goto err; |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 287 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Stefan Hajnoczi | ab5f3f8 | 2012-07-24 16:35:08 +0100 | [diff] [blame^] | 290 | nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name); |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 291 | |
| 292 | snprintf(nc->info_str, sizeof(nc->info_str), |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 293 | "socket: fd=%d (%s mcast=%s:%d)", |
| 294 | fd, is_connected ? "cloned" : "", |
| 295 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 296 | |
| 297 | s = DO_UPCAST(NetSocketState, nc, nc); |
| 298 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 299 | s->fd = fd; |
| 300 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 301 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
| 302 | |
| 303 | /* mcast: save bound address as dst */ |
| 304 | if (is_connected) s->dgram_dst=saddr; |
| 305 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 306 | return s; |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 307 | |
| 308 | err: |
| 309 | closesocket(fd); |
| 310 | return NULL; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static void net_socket_connect(void *opaque) |
| 314 | { |
| 315 | NetSocketState *s = opaque; |
| 316 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); |
| 317 | } |
| 318 | |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 319 | static NetClientInfo net_socket_info = { |
Laszlo Ersek | 2be64a6 | 2012-07-17 16:17:12 +0200 | [diff] [blame] | 320 | .type = NET_CLIENT_OPTIONS_KIND_SOCKET, |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 321 | .size = sizeof(NetSocketState), |
| 322 | .receive = net_socket_receive, |
| 323 | .cleanup = net_socket_cleanup, |
| 324 | }; |
| 325 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 326 | static NetSocketState *net_socket_fd_init_stream(VLANClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 327 | const char *model, |
| 328 | const char *name, |
| 329 | int fd, int is_connected) |
| 330 | { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 331 | VLANClientState *nc; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 332 | NetSocketState *s; |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 333 | |
Stefan Hajnoczi | ab5f3f8 | 2012-07-24 16:35:08 +0100 | [diff] [blame^] | 334 | nc = qemu_new_net_client(&net_socket_info, peer, model, name); |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 335 | |
| 336 | snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd); |
| 337 | |
| 338 | s = DO_UPCAST(NetSocketState, nc, nc); |
| 339 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 340 | s->fd = fd; |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 341 | |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 342 | if (is_connected) { |
| 343 | net_socket_connect(s); |
| 344 | } else { |
| 345 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
| 346 | } |
| 347 | return s; |
| 348 | } |
| 349 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 350 | static NetSocketState *net_socket_fd_init(VLANClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 351 | const char *model, const char *name, |
| 352 | int fd, int is_connected) |
| 353 | { |
| 354 | int so_type = -1, optlen=sizeof(so_type); |
| 355 | |
| 356 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
| 357 | (socklen_t *)&optlen)< 0) { |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 358 | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", |
| 359 | fd); |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 360 | closesocket(fd); |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 361 | return NULL; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 362 | } |
| 363 | switch(so_type) { |
| 364 | case SOCK_DGRAM: |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 365 | return net_socket_fd_init_dgram(peer, model, name, fd, is_connected); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 366 | case SOCK_STREAM: |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 367 | return net_socket_fd_init_stream(peer, model, name, fd, is_connected); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 368 | default: |
| 369 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ |
| 370 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 371 | return net_socket_fd_init_stream(peer, model, name, fd, is_connected); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 372 | } |
| 373 | return NULL; |
| 374 | } |
| 375 | |
| 376 | static void net_socket_accept(void *opaque) |
| 377 | { |
| 378 | NetSocketListenState *s = opaque; |
| 379 | NetSocketState *s1; |
| 380 | struct sockaddr_in saddr; |
| 381 | socklen_t len; |
| 382 | int fd; |
| 383 | |
| 384 | for(;;) { |
| 385 | len = sizeof(saddr); |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 386 | fd = qemu_accept(s->fd, (struct sockaddr *)&saddr, &len); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 387 | if (fd < 0 && errno != EINTR) { |
| 388 | return; |
| 389 | } else if (fd >= 0) { |
| 390 | break; |
| 391 | } |
| 392 | } |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 393 | s1 = net_socket_fd_init(s->peer, s->model, s->name, fd, 1); |
Stefan Hajnoczi | e5d1fca | 2011-12-07 15:01:49 +0000 | [diff] [blame] | 394 | if (s1) { |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 395 | snprintf(s1->nc.info_str, sizeof(s1->nc.info_str), |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 396 | "socket: connection from %s:%d", |
| 397 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 398 | } |
| 399 | } |
| 400 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 401 | static int net_socket_listen_init(VLANClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 402 | const char *model, |
| 403 | const char *name, |
| 404 | const char *host_str) |
| 405 | { |
| 406 | NetSocketListenState *s; |
| 407 | int fd, val, ret; |
| 408 | struct sockaddr_in saddr; |
| 409 | |
| 410 | if (parse_host_port(&saddr, host_str) < 0) |
| 411 | return -1; |
| 412 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 413 | s = g_malloc0(sizeof(NetSocketListenState)); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 414 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 415 | fd = qemu_socket(PF_INET, SOCK_STREAM, 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 416 | if (fd < 0) { |
| 417 | perror("socket"); |
Zhi Hui Li | c7ee8f6 | 2011-11-24 16:23:00 +0800 | [diff] [blame] | 418 | g_free(s); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 419 | return -1; |
| 420 | } |
| 421 | socket_set_nonblock(fd); |
| 422 | |
| 423 | /* allow fast reuse */ |
| 424 | val = 1; |
| 425 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
| 426 | |
| 427 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 428 | if (ret < 0) { |
| 429 | perror("bind"); |
Zhi Hui Li | c7ee8f6 | 2011-11-24 16:23:00 +0800 | [diff] [blame] | 430 | g_free(s); |
Peter Maydell | a46667e | 2011-12-24 23:47:11 +0000 | [diff] [blame] | 431 | closesocket(fd); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 432 | return -1; |
| 433 | } |
| 434 | ret = listen(fd, 0); |
| 435 | if (ret < 0) { |
| 436 | perror("listen"); |
Zhi Hui Li | c7ee8f6 | 2011-11-24 16:23:00 +0800 | [diff] [blame] | 437 | g_free(s); |
Peter Maydell | a46667e | 2011-12-24 23:47:11 +0000 | [diff] [blame] | 438 | closesocket(fd); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 439 | return -1; |
| 440 | } |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 441 | s->peer = peer; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 442 | s->model = g_strdup(model); |
| 443 | s->name = name ? g_strdup(name) : NULL; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 444 | s->fd = fd; |
| 445 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); |
| 446 | return 0; |
| 447 | } |
| 448 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 449 | static int net_socket_connect_init(VLANClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 450 | const char *model, |
| 451 | const char *name, |
| 452 | const char *host_str) |
| 453 | { |
| 454 | NetSocketState *s; |
| 455 | int fd, connected, ret, err; |
| 456 | struct sockaddr_in saddr; |
| 457 | |
| 458 | if (parse_host_port(&saddr, host_str) < 0) |
| 459 | return -1; |
| 460 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 461 | fd = qemu_socket(PF_INET, SOCK_STREAM, 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 462 | if (fd < 0) { |
| 463 | perror("socket"); |
| 464 | return -1; |
| 465 | } |
| 466 | socket_set_nonblock(fd); |
| 467 | |
| 468 | connected = 0; |
| 469 | for(;;) { |
| 470 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
| 471 | if (ret < 0) { |
| 472 | err = socket_error(); |
| 473 | if (err == EINTR || err == EWOULDBLOCK) { |
| 474 | } else if (err == EINPROGRESS) { |
| 475 | break; |
| 476 | #ifdef _WIN32 |
Pavel Dovgaluk | c7eb1f0 | 2011-02-21 14:46:44 +0300 | [diff] [blame] | 477 | } else if (err == WSAEALREADY || err == WSAEINVAL) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 478 | break; |
| 479 | #endif |
| 480 | } else { |
| 481 | perror("connect"); |
| 482 | closesocket(fd); |
| 483 | return -1; |
| 484 | } |
| 485 | } else { |
| 486 | connected = 1; |
| 487 | break; |
| 488 | } |
| 489 | } |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 490 | s = net_socket_fd_init(peer, model, name, fd, connected); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 491 | if (!s) |
| 492 | return -1; |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 493 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 494 | "socket: connect to %s:%d", |
| 495 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 496 | return 0; |
| 497 | } |
| 498 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 499 | static int net_socket_mcast_init(VLANClientState *peer, |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 500 | const char *model, |
| 501 | const char *name, |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 502 | const char *host_str, |
| 503 | const char *localaddr_str) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 504 | { |
| 505 | NetSocketState *s; |
| 506 | int fd; |
| 507 | struct sockaddr_in saddr; |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 508 | struct in_addr localaddr, *param_localaddr; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 509 | |
| 510 | if (parse_host_port(&saddr, host_str) < 0) |
| 511 | return -1; |
| 512 | |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 513 | if (localaddr_str != NULL) { |
| 514 | if (inet_aton(localaddr_str, &localaddr) == 0) |
| 515 | return -1; |
| 516 | param_localaddr = &localaddr; |
| 517 | } else { |
| 518 | param_localaddr = NULL; |
| 519 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 520 | |
Mike Ryan | 3a75e74 | 2010-12-01 11:16:47 -0800 | [diff] [blame] | 521 | fd = net_socket_mcast_create(&saddr, param_localaddr); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 522 | if (fd < 0) |
Stefan Hajnoczi | 842480d | 2011-12-07 15:01:48 +0000 | [diff] [blame] | 523 | return -1; |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 524 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 525 | s = net_socket_fd_init(peer, model, name, fd, 0); |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 526 | if (!s) |
| 527 | return -1; |
| 528 | |
| 529 | s->dgram_dst = saddr; |
| 530 | |
Mark McLoughlin | 564f63e | 2009-11-25 18:49:08 +0000 | [diff] [blame] | 531 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 532 | "socket: mcast=%s:%d", |
| 533 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
| 534 | return 0; |
| 535 | |
| 536 | } |
| 537 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 538 | static int net_socket_udp_init(VLANClientState *peer, |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 539 | const char *model, |
| 540 | const char *name, |
| 541 | const char *rhost, |
| 542 | const char *lhost) |
| 543 | { |
| 544 | NetSocketState *s; |
| 545 | int fd, val, ret; |
| 546 | struct sockaddr_in laddr, raddr; |
| 547 | |
| 548 | if (parse_host_port(&laddr, lhost) < 0) { |
| 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | if (parse_host_port(&raddr, rhost) < 0) { |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); |
| 557 | if (fd < 0) { |
| 558 | perror("socket(PF_INET, SOCK_DGRAM)"); |
| 559 | return -1; |
| 560 | } |
| 561 | val = 1; |
| 562 | ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
| 563 | (const char *)&val, sizeof(val)); |
| 564 | if (ret < 0) { |
| 565 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
| 566 | closesocket(fd); |
| 567 | return -1; |
| 568 | } |
| 569 | ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); |
| 570 | if (ret < 0) { |
| 571 | perror("bind"); |
| 572 | closesocket(fd); |
| 573 | return -1; |
| 574 | } |
| 575 | |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 576 | s = net_socket_fd_init(peer, model, name, fd, 0); |
Benjamin | 0e0e7fa | 2012-01-11 09:20:54 +0900 | [diff] [blame] | 577 | if (!s) { |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | s->dgram_dst = raddr; |
| 582 | |
| 583 | snprintf(s->nc.info_str, sizeof(s->nc.info_str), |
| 584 | "socket: udp=%s:%d", |
| 585 | inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port)); |
| 586 | return 0; |
| 587 | } |
| 588 | |
Laszlo Ersek | 1a0c095 | 2012-07-17 16:17:21 +0200 | [diff] [blame] | 589 | int net_init_socket(const NetClientOptions *opts, const char *name, |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 590 | VLANClientState *peer) |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 591 | { |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 592 | const NetdevSocketOptions *sock; |
| 593 | |
| 594 | assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET); |
| 595 | sock = opts->socket; |
| 596 | |
| 597 | if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast + |
| 598 | sock->has_udp != 1) { |
| 599 | error_report("exactly one of fd=, listen=, connect=, mcast= or udp=" |
| 600 | " is required"); |
| 601 | return -1; |
| 602 | } |
| 603 | |
| 604 | if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) { |
| 605 | error_report("localaddr= is only valid with mcast= or udp="); |
| 606 | return -1; |
| 607 | } |
| 608 | |
| 609 | if (sock->has_fd) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 610 | int fd; |
| 611 | |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 612 | fd = net_handle_fd_param(cur_mon, sock->fd); |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 613 | if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 614 | return -1; |
| 615 | } |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 616 | return 0; |
| 617 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 618 | |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 619 | if (sock->has_listen) { |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 620 | if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 621 | return -1; |
| 622 | } |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 623 | return 0; |
| 624 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 625 | |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 626 | if (sock->has_connect) { |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 627 | if (net_socket_connect_init(peer, "socket", name, sock->connect) == |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 628 | -1) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 629 | return -1; |
| 630 | } |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 631 | return 0; |
| 632 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 633 | |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 634 | if (sock->has_mcast) { |
| 635 | /* if sock->localaddr is missing, it has been initialized to "all bits |
| 636 | * zero" */ |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 637 | if (net_socket_mcast_init(peer, "socket", name, sock->mcast, |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 638 | sock->localaddr) == -1) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 639 | return -1; |
| 640 | } |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 641 | return 0; |
| 642 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 643 | |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 644 | assert(sock->has_udp); |
| 645 | if (!sock->has_localaddr) { |
| 646 | error_report("localaddr= is mandatory with udp="); |
| 647 | return -1; |
| 648 | } |
Stefan Hajnoczi | d33d93b | 2012-07-24 16:35:05 +0100 | [diff] [blame] | 649 | if (net_socket_udp_init(peer, "udp", name, sock->udp, sock->localaddr) == |
Laszlo Ersek | bef8e8f | 2012-07-17 16:17:17 +0200 | [diff] [blame] | 650 | -1) { |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 651 | return -1; |
| 652 | } |
Mark McLoughlin | 42281ac | 2009-11-25 18:48:56 +0000 | [diff] [blame] | 653 | return 0; |
| 654 | } |