blob: 7857dcdf8d592695fd79ad3275761692a40c8417 [file] [log] [blame]
Mark McLoughlin42281ac2009-11-25 18:48:56 +00001/*
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 Capitulino42dcc542012-04-12 13:20:40 -030029#include "monitor.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000030#include "qemu-char.h"
31#include "qemu-common.h"
Markus Armbruster2f792012010-02-18 16:24:31 +010032#include "qemu-error.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000033#include "qemu-option.h"
34#include "qemu_socket.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000035
36typedef struct NetSocketState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010037 NetClientState nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +000038 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
46typedef struct NetSocketListenState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010047 NetClientState *peer;
Mark McLoughlin42281ac2009-11-25 18:48:56 +000048 char *model;
49 char *name;
50 int fd;
51} NetSocketListenState;
52
53/* XXX: we consider we can send the whole packet without blocking */
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010054static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin42281ac2009-11-25 18:48:56 +000055{
Mark McLoughlin564f63e2009-11-25 18:49:08 +000056 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Mark McLoughlin42281ac2009-11-25 18:48:56 +000057 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
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010064static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin42281ac2009-11-25 18:48:56 +000065{
Mark McLoughlin564f63e2009-11-25 18:49:08 +000066 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Mark McLoughlin42281ac2009-11-25 18:48:56 +000067
68 return sendto(s->fd, (const void *)buf, size, 0,
69 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
70}
71
72static 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 Swirl00aa0042011-07-23 20:04:29 +000080 size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +000081 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 McLoughlin564f63e2009-11-25 18:49:08 +0000128 qemu_send_packet(&s->nc, s->buf, s->packet_len);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000129 s->index = 0;
130 s->state = 0;
131 }
132 break;
133 }
134 }
135}
136
137static void net_socket_send_dgram(void *opaque)
138{
139 NetSocketState *s = opaque;
140 int size;
141
Blue Swirl00aa0042011-07-23 20:04:29 +0000142 size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000143 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 McLoughlin564f63e2009-11-25 18:49:08 +0000150 qemu_send_packet(&s->nc, s->buf, size);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000151}
152
Mike Ryan3a75e742010-12-01 11:16:47 -0800153static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000154{
155 struct ip_mreq imr;
156 int fd;
157 int val, ret;
Brad23ddf2b2011-08-07 11:06:43 +0000158#ifdef __OpenBSD__
159 unsigned char loop;
160#else
161 int loop;
162#endif
163
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000164 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000165 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
166 "does not contain a multicast address\n",
167 inet_ntoa(mcastaddr->sin_addr),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000168 (int)ntohl(mcastaddr->sin_addr.s_addr));
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000169 return -1;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000170
171 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100172 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000173 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 Hajnoczi842480d2011-12-07 15:01:48 +0000182 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
183 goto fail;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000184 }
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 Ryan3a75e742010-12-01 11:16:47 -0800194 if (localaddr) {
195 imr.imr_interface = *localaddr;
196 } else {
197 imr.imr_interface.s_addr = htonl(INADDR_ANY);
198 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000199
200 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
201 (const char *)&imr, sizeof(struct ip_mreq));
202 if (ret < 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000203 perror("setsockopt(IP_ADD_MEMBERSHIP)");
204 goto fail;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000205 }
206
207 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
Brad23ddf2b2011-08-07 11:06:43 +0000208 loop = 1;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000209 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
Brad23ddf2b2011-08-07 11:06:43 +0000210 (const char *)&loop, sizeof(loop));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000211 if (ret < 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000212 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
213 goto fail;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000214 }
215
Mike Ryan3a75e742010-12-01 11:16:47 -0800216 /* If a bind address is given, only send packets from that address */
217 if (localaddr != NULL) {
Blue Swirl4d22c6c2010-12-17 21:03:00 +0000218 ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
219 (const char *)localaddr, sizeof(*localaddr));
Mike Ryan3a75e742010-12-01 11:16:47 -0800220 if (ret < 0) {
221 perror("setsockopt(IP_MULTICAST_IF)");
222 goto fail;
223 }
224 }
225
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000226 socket_set_nonblock(fd);
227 return fd;
228fail:
229 if (fd >= 0)
230 closesocket(fd);
231 return -1;
232}
233
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100234static void net_socket_cleanup(NetClientState *nc)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000235{
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000236 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000237 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
238 close(s->fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000239}
240
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000241static NetClientInfo net_dgram_socket_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200242 .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000243 .size = sizeof(NetSocketState),
244 .receive = net_socket_receive_dgram,
245 .cleanup = net_socket_cleanup,
246};
247
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100248static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000249 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;
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100256 NetClientState *nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000257 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 Hajnoczi842480d2011-12-07 15:01:48 +0000265 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 Hajnoczie5d1fca2011-12-07 15:01:49 +0000270 goto err;
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000271 }
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 Hajnoczie5d1fca2011-12-07 15:01:49 +0000276 goto err;
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000277 }
278 /* clone newfd to fd, close newfd */
279 dup2(newfd, fd);
280 close(newfd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000281
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000282 } else {
283 fprintf(stderr,
284 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
285 fd, strerror(errno));
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000286 goto err;
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000287 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000288 }
289
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100290 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000291
292 snprintf(nc->info_str, sizeof(nc->info_str),
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000293 "socket: fd=%d (%s mcast=%s:%d)",
294 fd, is_connected ? "cloned" : "",
295 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000296
297 s = DO_UPCAST(NetSocketState, nc, nc);
298
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000299 s->fd = fd;
300
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000301 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
302
303 /* mcast: save bound address as dst */
Zhi Yong Wue34cde32012-07-20 14:25:52 +0100304 if (is_connected) {
305 s->dgram_dst = saddr;
306 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000307
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000308 return s;
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000309
310err:
311 closesocket(fd);
312 return NULL;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000313}
314
315static void net_socket_connect(void *opaque)
316{
317 NetSocketState *s = opaque;
318 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
319}
320
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000321static NetClientInfo net_socket_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200322 .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000323 .size = sizeof(NetSocketState),
324 .receive = net_socket_receive,
325 .cleanup = net_socket_cleanup,
326};
327
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100328static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000329 const char *model,
330 const char *name,
331 int fd, int is_connected)
332{
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100333 NetClientState *nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000334 NetSocketState *s;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000335
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100336 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000337
338 snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
339
340 s = DO_UPCAST(NetSocketState, nc, nc);
341
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000342 s->fd = fd;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000343
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000344 if (is_connected) {
345 net_socket_connect(s);
346 } else {
347 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
348 }
349 return s;
350}
351
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100352static NetSocketState *net_socket_fd_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000353 const char *model, const char *name,
354 int fd, int is_connected)
355{
356 int so_type = -1, optlen=sizeof(so_type);
357
358 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
359 (socklen_t *)&optlen)< 0) {
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000360 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
361 fd);
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000362 closesocket(fd);
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000363 return NULL;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000364 }
365 switch(so_type) {
366 case SOCK_DGRAM:
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100367 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000368 case SOCK_STREAM:
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100369 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000370 default:
371 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
372 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100373 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000374 }
375 return NULL;
376}
377
378static void net_socket_accept(void *opaque)
379{
380 NetSocketListenState *s = opaque;
381 NetSocketState *s1;
382 struct sockaddr_in saddr;
383 socklen_t len;
384 int fd;
385
386 for(;;) {
387 len = sizeof(saddr);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100388 fd = qemu_accept(s->fd, (struct sockaddr *)&saddr, &len);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000389 if (fd < 0 && errno != EINTR) {
390 return;
391 } else if (fd >= 0) {
392 break;
393 }
394 }
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100395 s1 = net_socket_fd_init(s->peer, s->model, s->name, fd, 1);
Stefan Hajnoczie5d1fca2011-12-07 15:01:49 +0000396 if (s1) {
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000397 snprintf(s1->nc.info_str, sizeof(s1->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000398 "socket: connection from %s:%d",
399 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
400 }
401}
402
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100403static int net_socket_listen_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000404 const char *model,
405 const char *name,
406 const char *host_str)
407{
408 NetSocketListenState *s;
409 int fd, val, ret;
410 struct sockaddr_in saddr;
411
412 if (parse_host_port(&saddr, host_str) < 0)
413 return -1;
414
Anthony Liguori7267c092011-08-20 22:09:37 -0500415 s = g_malloc0(sizeof(NetSocketListenState));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000416
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100417 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000418 if (fd < 0) {
419 perror("socket");
Zhi Hui Lic7ee8f62011-11-24 16:23:00 +0800420 g_free(s);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000421 return -1;
422 }
423 socket_set_nonblock(fd);
424
425 /* allow fast reuse */
426 val = 1;
427 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
428
429 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
430 if (ret < 0) {
431 perror("bind");
Zhi Hui Lic7ee8f62011-11-24 16:23:00 +0800432 g_free(s);
Peter Maydella46667e2011-12-24 23:47:11 +0000433 closesocket(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000434 return -1;
435 }
436 ret = listen(fd, 0);
437 if (ret < 0) {
438 perror("listen");
Zhi Hui Lic7ee8f62011-11-24 16:23:00 +0800439 g_free(s);
Peter Maydella46667e2011-12-24 23:47:11 +0000440 closesocket(fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000441 return -1;
442 }
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100443 s->peer = peer;
Anthony Liguori7267c092011-08-20 22:09:37 -0500444 s->model = g_strdup(model);
445 s->name = name ? g_strdup(name) : NULL;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000446 s->fd = fd;
447 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
448 return 0;
449}
450
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100451static int net_socket_connect_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000452 const char *model,
453 const char *name,
454 const char *host_str)
455{
456 NetSocketState *s;
457 int fd, connected, ret, err;
458 struct sockaddr_in saddr;
459
460 if (parse_host_port(&saddr, host_str) < 0)
461 return -1;
462
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100463 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000464 if (fd < 0) {
465 perror("socket");
466 return -1;
467 }
468 socket_set_nonblock(fd);
469
470 connected = 0;
471 for(;;) {
472 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
473 if (ret < 0) {
474 err = socket_error();
475 if (err == EINTR || err == EWOULDBLOCK) {
476 } else if (err == EINPROGRESS) {
477 break;
478#ifdef _WIN32
Pavel Dovgalukc7eb1f02011-02-21 14:46:44 +0300479 } else if (err == WSAEALREADY || err == WSAEINVAL) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000480 break;
481#endif
482 } else {
483 perror("connect");
484 closesocket(fd);
485 return -1;
486 }
487 } else {
488 connected = 1;
489 break;
490 }
491 }
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100492 s = net_socket_fd_init(peer, model, name, fd, connected);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000493 if (!s)
494 return -1;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000495 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000496 "socket: connect to %s:%d",
497 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
498 return 0;
499}
500
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100501static int net_socket_mcast_init(NetClientState *peer,
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000502 const char *model,
503 const char *name,
Mike Ryan3a75e742010-12-01 11:16:47 -0800504 const char *host_str,
505 const char *localaddr_str)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000506{
507 NetSocketState *s;
508 int fd;
509 struct sockaddr_in saddr;
Mike Ryan3a75e742010-12-01 11:16:47 -0800510 struct in_addr localaddr, *param_localaddr;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000511
512 if (parse_host_port(&saddr, host_str) < 0)
513 return -1;
514
Mike Ryan3a75e742010-12-01 11:16:47 -0800515 if (localaddr_str != NULL) {
516 if (inet_aton(localaddr_str, &localaddr) == 0)
517 return -1;
518 param_localaddr = &localaddr;
519 } else {
520 param_localaddr = NULL;
521 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000522
Mike Ryan3a75e742010-12-01 11:16:47 -0800523 fd = net_socket_mcast_create(&saddr, param_localaddr);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000524 if (fd < 0)
Stefan Hajnoczi842480d2011-12-07 15:01:48 +0000525 return -1;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000526
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100527 s = net_socket_fd_init(peer, model, name, fd, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000528 if (!s)
529 return -1;
530
531 s->dgram_dst = saddr;
532
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000533 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000534 "socket: mcast=%s:%d",
535 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
536 return 0;
537
538}
539
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100540static int net_socket_udp_init(NetClientState *peer,
Benjamin0e0e7fa2012-01-11 09:20:54 +0900541 const char *model,
542 const char *name,
543 const char *rhost,
544 const char *lhost)
545{
546 NetSocketState *s;
547 int fd, val, ret;
548 struct sockaddr_in laddr, raddr;
549
550 if (parse_host_port(&laddr, lhost) < 0) {
551 return -1;
552 }
553
554 if (parse_host_port(&raddr, rhost) < 0) {
555 return -1;
556 }
557
558 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
559 if (fd < 0) {
560 perror("socket(PF_INET, SOCK_DGRAM)");
561 return -1;
562 }
563 val = 1;
564 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
565 (const char *)&val, sizeof(val));
566 if (ret < 0) {
567 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
568 closesocket(fd);
569 return -1;
570 }
571 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
572 if (ret < 0) {
573 perror("bind");
574 closesocket(fd);
575 return -1;
576 }
577
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100578 s = net_socket_fd_init(peer, model, name, fd, 0);
Benjamin0e0e7fa2012-01-11 09:20:54 +0900579 if (!s) {
580 return -1;
581 }
582
583 s->dgram_dst = raddr;
584
585 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
586 "socket: udp=%s:%d",
587 inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
588 return 0;
589}
590
Laszlo Ersek1a0c0952012-07-17 16:17:21 +0200591int net_init_socket(const NetClientOptions *opts, const char *name,
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100592 NetClientState *peer)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000593{
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200594 const NetdevSocketOptions *sock;
595
596 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET);
597 sock = opts->socket;
598
599 if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
600 sock->has_udp != 1) {
601 error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
602 " is required");
603 return -1;
604 }
605
606 if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
607 error_report("localaddr= is only valid with mcast= or udp=");
608 return -1;
609 }
610
611 if (sock->has_fd) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000612 int fd;
613
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200614 fd = net_handle_fd_param(cur_mon, sock->fd);
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100615 if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000616 return -1;
617 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200618 return 0;
619 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000620
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200621 if (sock->has_listen) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100622 if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000623 return -1;
624 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200625 return 0;
626 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000627
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200628 if (sock->has_connect) {
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100629 if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200630 -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000631 return -1;
632 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200633 return 0;
634 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000635
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200636 if (sock->has_mcast) {
637 /* if sock->localaddr is missing, it has been initialized to "all bits
638 * zero" */
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100639 if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200640 sock->localaddr) == -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000641 return -1;
642 }
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200643 return 0;
644 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000645
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200646 assert(sock->has_udp);
647 if (!sock->has_localaddr) {
648 error_report("localaddr= is mandatory with udp=");
649 return -1;
650 }
Stefan Hajnoczid33d93b2012-07-24 16:35:05 +0100651 if (net_socket_udp_init(peer, "udp", name, sock->udp, sock->localaddr) ==
Laszlo Ersekbef8e8f2012-07-17 16:17:17 +0200652 -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000653 return -1;
654 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000655 return 0;
656}