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