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