aliguori | 305b0eb | 2008-11-13 16:19:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * inet and unix socket functions for qemu |
| 3 | * |
| 4 | * (c) 2008 Gerd Hoffmann <kraxel@redhat.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; under version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <ctype.h> |
| 19 | #include <errno.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include "qemu_socket.h" |
blueswir1 | 47398b9 | 2008-11-22 20:04:24 +0000 | [diff] [blame] | 23 | #include "qemu-common.h" /* for qemu_isdigit */ |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 24 | |
| 25 | #ifndef AI_ADDRCONFIG |
| 26 | # define AI_ADDRCONFIG 0 |
| 27 | #endif |
| 28 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 29 | static const int on=1, off=0; |
| 30 | |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 31 | /* used temporarely until all users are converted to QemuOpts */ |
Blue Swirl | c139090 | 2009-09-12 09:58:51 +0000 | [diff] [blame] | 32 | static QemuOptsList dummy_opts = { |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 33 | .name = "dummy", |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 34 | .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head), |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 35 | .desc = { |
| 36 | { |
| 37 | .name = "path", |
| 38 | .type = QEMU_OPT_STRING, |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 39 | },{ |
| 40 | .name = "host", |
| 41 | .type = QEMU_OPT_STRING, |
| 42 | },{ |
| 43 | .name = "port", |
| 44 | .type = QEMU_OPT_STRING, |
| 45 | },{ |
| 46 | .name = "to", |
| 47 | .type = QEMU_OPT_NUMBER, |
| 48 | },{ |
| 49 | .name = "ipv4", |
| 50 | .type = QEMU_OPT_BOOL, |
| 51 | },{ |
| 52 | .name = "ipv6", |
| 53 | .type = QEMU_OPT_BOOL, |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 54 | }, |
| 55 | { /* end if list */ } |
| 56 | }, |
| 57 | }; |
| 58 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 59 | static int inet_getport(struct addrinfo *e) |
| 60 | { |
| 61 | struct sockaddr_in *i4; |
| 62 | struct sockaddr_in6 *i6; |
| 63 | |
| 64 | switch (e->ai_family) { |
| 65 | case PF_INET6: |
| 66 | i6 = (void*)e->ai_addr; |
| 67 | return ntohs(i6->sin6_port); |
| 68 | case PF_INET: |
| 69 | i4 = (void*)e->ai_addr; |
| 70 | return ntohs(i4->sin_port); |
| 71 | default: |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static void inet_setport(struct addrinfo *e, int port) |
| 77 | { |
| 78 | struct sockaddr_in *i4; |
| 79 | struct sockaddr_in6 *i6; |
| 80 | |
| 81 | switch (e->ai_family) { |
| 82 | case PF_INET6: |
| 83 | i6 = (void*)e->ai_addr; |
| 84 | i6->sin6_port = htons(port); |
| 85 | break; |
| 86 | case PF_INET: |
| 87 | i4 = (void*)e->ai_addr; |
| 88 | i4->sin_port = htons(port); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
Luiz Capitulino | c9c4b34 | 2010-01-20 11:42:38 -0200 | [diff] [blame] | 93 | const char *inet_strfamily(int family) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 94 | { |
| 95 | switch (family) { |
| 96 | case PF_INET6: return "ipv6"; |
| 97 | case PF_INET: return "ipv4"; |
| 98 | case PF_UNIX: return "unix"; |
| 99 | } |
Luiz Capitulino | c445321 | 2010-01-20 11:42:39 -0200 | [diff] [blame] | 100 | return "unknown"; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 103 | int inet_listen_opts(QemuOpts *opts, int port_offset) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 104 | { |
| 105 | struct addrinfo ai,*res,*e; |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 106 | const char *addr; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 107 | char port[33]; |
| 108 | char uaddr[INET6_ADDRSTRLEN+1]; |
| 109 | char uport[33]; |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 110 | int slisten,rc,to,try_next; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 111 | |
| 112 | memset(&ai,0, sizeof(ai)); |
| 113 | ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
| 114 | ai.ai_family = PF_UNSPEC; |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 115 | ai.ai_socktype = SOCK_STREAM; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 116 | |
Jens Osterkamp | e23a22e | 2010-04-12 10:51:01 +0200 | [diff] [blame] | 117 | if ((qemu_opt_get(opts, "host") == NULL) || |
| 118 | (qemu_opt_get(opts, "port") == NULL)) { |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 119 | fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__); |
| 120 | return -1; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 121 | } |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 122 | pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); |
| 123 | addr = qemu_opt_get(opts, "host"); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 124 | |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 125 | to = qemu_opt_get_number(opts, "to", 0); |
| 126 | if (qemu_opt_get_bool(opts, "ipv4", 0)) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 127 | ai.ai_family = PF_INET; |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 128 | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 129 | ai.ai_family = PF_INET6; |
| 130 | |
| 131 | /* lookup */ |
| 132 | if (port_offset) |
| 133 | snprintf(port, sizeof(port), "%d", atoi(port) + port_offset); |
| 134 | rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res); |
| 135 | if (rc != 0) { |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 136 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, |
| 137 | gai_strerror(rc)); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 138 | return -1; |
| 139 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 140 | |
| 141 | /* create socket + bind */ |
| 142 | for (e = res; e != NULL; e = e->ai_next) { |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 143 | getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
| 144 | uaddr,INET6_ADDRSTRLEN,uport,32, |
| 145 | NI_NUMERICHOST | NI_NUMERICSERV); |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 146 | slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 147 | if (slisten < 0) { |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 148 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, |
| 149 | inet_strfamily(e->ai_family), strerror(errno)); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 150 | continue; |
| 151 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 152 | |
| 153 | setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
| 154 | #ifdef IPV6_V6ONLY |
| 155 | if (e->ai_family == PF_INET6) { |
| 156 | /* listen on both ipv4 and ipv6 */ |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 157 | setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off, |
| 158 | sizeof(off)); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 159 | } |
| 160 | #endif |
| 161 | |
| 162 | for (;;) { |
| 163 | if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 164 | goto listen; |
| 165 | } |
| 166 | try_next = to && (inet_getport(e) <= to + port_offset); |
Markus Armbruster | 136faa3 | 2012-02-07 15:09:14 +0100 | [diff] [blame^] | 167 | if (!try_next) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 168 | fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__, |
| 169 | inet_strfamily(e->ai_family), uaddr, inet_getport(e), |
| 170 | strerror(errno)); |
| 171 | if (try_next) { |
| 172 | inet_setport(e, inet_getport(e) + 1); |
| 173 | continue; |
| 174 | } |
| 175 | break; |
| 176 | } |
| 177 | closesocket(slisten); |
| 178 | } |
| 179 | fprintf(stderr, "%s: FAILED\n", __FUNCTION__); |
| 180 | freeaddrinfo(res); |
| 181 | return -1; |
| 182 | |
| 183 | listen: |
| 184 | if (listen(slisten,1) != 0) { |
| 185 | perror("listen"); |
| 186 | closesocket(slisten); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 187 | freeaddrinfo(res); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 188 | return -1; |
| 189 | } |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 190 | snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset); |
| 191 | qemu_opt_set(opts, "host", uaddr); |
| 192 | qemu_opt_set(opts, "port", uport); |
| 193 | qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off"); |
| 194 | qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off"); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 195 | freeaddrinfo(res); |
| 196 | return slisten; |
| 197 | } |
| 198 | |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 199 | int inet_connect_opts(QemuOpts *opts) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 200 | { |
| 201 | struct addrinfo ai,*res,*e; |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 202 | const char *addr; |
| 203 | const char *port; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 204 | char uaddr[INET6_ADDRSTRLEN+1]; |
| 205 | char uport[33]; |
| 206 | int sock,rc; |
| 207 | |
| 208 | memset(&ai,0, sizeof(ai)); |
| 209 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
| 210 | ai.ai_family = PF_UNSPEC; |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 211 | ai.ai_socktype = SOCK_STREAM; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 212 | |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 213 | addr = qemu_opt_get(opts, "host"); |
| 214 | port = qemu_opt_get(opts, "port"); |
| 215 | if (addr == NULL || port == NULL) { |
| 216 | fprintf(stderr, "inet_connect: host and/or port not specified\n"); |
| 217 | return -1; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 220 | if (qemu_opt_get_bool(opts, "ipv4", 0)) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 221 | ai.ai_family = PF_INET; |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 222 | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 223 | ai.ai_family = PF_INET6; |
| 224 | |
| 225 | /* lookup */ |
| 226 | if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) { |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 227 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, |
| 228 | gai_strerror(rc)); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 229 | return -1; |
| 230 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 231 | |
| 232 | for (e = res; e != NULL; e = e->ai_next) { |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 233 | if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
| 234 | uaddr,INET6_ADDRSTRLEN,uport,32, |
| 235 | NI_NUMERICHOST | NI_NUMERICSERV) != 0) { |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 236 | fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 237 | continue; |
| 238 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 239 | sock = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 240 | if (sock < 0) { |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 241 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 242 | inet_strfamily(e->ai_family), strerror(errno)); |
| 243 | continue; |
| 244 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 245 | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
| 246 | |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 247 | /* connect to peer */ |
| 248 | if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) { |
Markus Armbruster | 136faa3 | 2012-02-07 15:09:14 +0100 | [diff] [blame^] | 249 | if (NULL == e->ai_next) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 250 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, |
| 251 | inet_strfamily(e->ai_family), |
| 252 | e->ai_canonname, uaddr, uport, strerror(errno)); |
| 253 | closesocket(sock); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 254 | continue; |
| 255 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 256 | freeaddrinfo(res); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 257 | return sock; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 258 | } |
| 259 | freeaddrinfo(res); |
| 260 | return -1; |
| 261 | } |
| 262 | |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 263 | int inet_dgram_opts(QemuOpts *opts) |
| 264 | { |
| 265 | struct addrinfo ai, *peer = NULL, *local = NULL; |
| 266 | const char *addr; |
| 267 | const char *port; |
| 268 | char uaddr[INET6_ADDRSTRLEN+1]; |
| 269 | char uport[33]; |
| 270 | int sock = -1, rc; |
| 271 | |
| 272 | /* lookup peer addr */ |
| 273 | memset(&ai,0, sizeof(ai)); |
| 274 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
| 275 | ai.ai_family = PF_UNSPEC; |
| 276 | ai.ai_socktype = SOCK_DGRAM; |
| 277 | |
| 278 | addr = qemu_opt_get(opts, "host"); |
| 279 | port = qemu_opt_get(opts, "port"); |
| 280 | if (addr == NULL || strlen(addr) == 0) { |
| 281 | addr = "localhost"; |
| 282 | } |
| 283 | if (port == NULL || strlen(port) == 0) { |
| 284 | fprintf(stderr, "inet_dgram: port not specified\n"); |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | if (qemu_opt_get_bool(opts, "ipv4", 0)) |
| 289 | ai.ai_family = PF_INET; |
| 290 | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
| 291 | ai.ai_family = PF_INET6; |
| 292 | |
| 293 | if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) { |
| 294 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, |
| 295 | gai_strerror(rc)); |
| 296 | return -1; |
| 297 | } |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 298 | |
| 299 | /* lookup local addr */ |
| 300 | memset(&ai,0, sizeof(ai)); |
| 301 | ai.ai_flags = AI_PASSIVE; |
| 302 | ai.ai_family = peer->ai_family; |
| 303 | ai.ai_socktype = SOCK_DGRAM; |
| 304 | |
| 305 | addr = qemu_opt_get(opts, "localaddr"); |
| 306 | port = qemu_opt_get(opts, "localport"); |
| 307 | if (addr == NULL || strlen(addr) == 0) { |
| 308 | addr = NULL; |
| 309 | } |
| 310 | if (!port || strlen(port) == 0) |
| 311 | port = "0"; |
| 312 | |
| 313 | if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) { |
| 314 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, |
| 315 | gai_strerror(rc)); |
| 316 | return -1; |
| 317 | } |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 318 | |
| 319 | /* create socket */ |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 320 | sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); |
Gerd Hoffmann | 7e1b35b | 2009-09-10 10:58:51 +0200 | [diff] [blame] | 321 | if (sock < 0) { |
| 322 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, |
| 323 | inet_strfamily(peer->ai_family), strerror(errno)); |
| 324 | goto err; |
| 325 | } |
| 326 | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
| 327 | |
| 328 | /* bind socket */ |
| 329 | if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen, |
| 330 | uaddr,INET6_ADDRSTRLEN,uport,32, |
| 331 | NI_NUMERICHOST | NI_NUMERICSERV) != 0) { |
| 332 | fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__); |
| 333 | goto err; |
| 334 | } |
| 335 | if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { |
| 336 | fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, |
| 337 | inet_strfamily(local->ai_family), uaddr, inet_getport(local)); |
| 338 | goto err; |
| 339 | } |
| 340 | |
| 341 | /* connect to peer */ |
| 342 | if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen, |
| 343 | uaddr, INET6_ADDRSTRLEN, uport, 32, |
| 344 | NI_NUMERICHOST | NI_NUMERICSERV) != 0) { |
| 345 | fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__); |
| 346 | goto err; |
| 347 | } |
| 348 | if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { |
| 349 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, |
| 350 | inet_strfamily(peer->ai_family), |
| 351 | peer->ai_canonname, uaddr, uport, strerror(errno)); |
| 352 | goto err; |
| 353 | } |
| 354 | |
| 355 | freeaddrinfo(local); |
| 356 | freeaddrinfo(peer); |
| 357 | return sock; |
| 358 | |
| 359 | err: |
| 360 | if (-1 != sock) |
| 361 | closesocket(sock); |
| 362 | if (local) |
| 363 | freeaddrinfo(local); |
| 364 | if (peer) |
| 365 | freeaddrinfo(peer); |
| 366 | return -1; |
| 367 | } |
| 368 | |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 369 | /* compatibility wrapper */ |
| 370 | static int inet_parse(QemuOpts *opts, const char *str) |
| 371 | { |
| 372 | const char *optstr, *h; |
| 373 | char addr[64]; |
| 374 | char port[33]; |
| 375 | int pos; |
| 376 | |
| 377 | /* parse address */ |
| 378 | if (str[0] == ':') { |
| 379 | /* no host given */ |
| 380 | addr[0] = '\0'; |
| 381 | if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { |
| 382 | fprintf(stderr, "%s: portonly parse error (%s)\n", |
| 383 | __FUNCTION__, str); |
| 384 | return -1; |
| 385 | } |
| 386 | } else if (str[0] == '[') { |
| 387 | /* IPv6 addr */ |
| 388 | if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) { |
| 389 | fprintf(stderr, "%s: ipv6 parse error (%s)\n", |
| 390 | __FUNCTION__, str); |
| 391 | return -1; |
| 392 | } |
Marcelo Tosatti | 2198a62 | 2010-02-09 15:31:46 -0200 | [diff] [blame] | 393 | qemu_opt_set(opts, "ipv6", "on"); |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 394 | } else if (qemu_isdigit(str[0])) { |
| 395 | /* IPv4 addr */ |
| 396 | if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { |
| 397 | fprintf(stderr, "%s: ipv4 parse error (%s)\n", |
| 398 | __FUNCTION__, str); |
| 399 | return -1; |
| 400 | } |
Marcelo Tosatti | 2198a62 | 2010-02-09 15:31:46 -0200 | [diff] [blame] | 401 | qemu_opt_set(opts, "ipv4", "on"); |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 402 | } else { |
| 403 | /* hostname */ |
| 404 | if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) { |
| 405 | fprintf(stderr, "%s: hostname parse error (%s)\n", |
| 406 | __FUNCTION__, str); |
| 407 | return -1; |
| 408 | } |
| 409 | } |
| 410 | qemu_opt_set(opts, "host", addr); |
| 411 | qemu_opt_set(opts, "port", port); |
| 412 | |
| 413 | /* parse options */ |
| 414 | optstr = str + pos; |
| 415 | h = strstr(optstr, ",to="); |
| 416 | if (h) |
| 417 | qemu_opt_set(opts, "to", h+4); |
| 418 | if (strstr(optstr, ",ipv4")) |
Marcelo Tosatti | 2198a62 | 2010-02-09 15:31:46 -0200 | [diff] [blame] | 419 | qemu_opt_set(opts, "ipv4", "on"); |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 420 | if (strstr(optstr, ",ipv6")) |
Marcelo Tosatti | 2198a62 | 2010-02-09 15:31:46 -0200 | [diff] [blame] | 421 | qemu_opt_set(opts, "ipv6", "on"); |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
Gerd Hoffmann | e5bc776 | 2009-09-10 10:58:41 +0200 | [diff] [blame] | 425 | int inet_listen(const char *str, char *ostr, int olen, |
| 426 | int socktype, int port_offset) |
| 427 | { |
| 428 | QemuOpts *opts; |
| 429 | char *optstr; |
| 430 | int sock = -1; |
| 431 | |
| 432 | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
| 433 | if (inet_parse(opts, str) == 0) { |
| 434 | sock = inet_listen_opts(opts, port_offset); |
| 435 | if (sock != -1 && ostr) { |
| 436 | optstr = strchr(str, ','); |
| 437 | if (qemu_opt_get_bool(opts, "ipv6", 0)) { |
| 438 | snprintf(ostr, olen, "[%s]:%s%s", |
| 439 | qemu_opt_get(opts, "host"), |
| 440 | qemu_opt_get(opts, "port"), |
| 441 | optstr ? optstr : ""); |
| 442 | } else { |
| 443 | snprintf(ostr, olen, "%s:%s%s", |
| 444 | qemu_opt_get(opts, "host"), |
| 445 | qemu_opt_get(opts, "port"), |
| 446 | optstr ? optstr : ""); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | qemu_opts_del(opts); |
| 451 | return sock; |
| 452 | } |
| 453 | |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame] | 454 | int inet_connect(const char *str, int socktype) |
| 455 | { |
| 456 | QemuOpts *opts; |
| 457 | int sock = -1; |
| 458 | |
| 459 | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
| 460 | if (inet_parse(opts, str) == 0) |
| 461 | sock = inet_connect_opts(opts); |
| 462 | qemu_opts_del(opts); |
| 463 | return sock; |
| 464 | } |
| 465 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 466 | #ifndef _WIN32 |
| 467 | |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 468 | int unix_listen_opts(QemuOpts *opts) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 469 | { |
| 470 | struct sockaddr_un un; |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 471 | const char *path = qemu_opt_get(opts, "path"); |
| 472 | int sock, fd; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 473 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 474 | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 475 | if (sock < 0) { |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 476 | perror("socket(unix)"); |
| 477 | return -1; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 478 | } |
| 479 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 480 | memset(&un, 0, sizeof(un)); |
| 481 | un.sun_family = AF_UNIX; |
| 482 | if (path && strlen(path)) { |
| 483 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
| 484 | } else { |
| 485 | char *tmpdir = getenv("TMPDIR"); |
| 486 | snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX", |
| 487 | tmpdir ? tmpdir : "/tmp"); |
| 488 | /* |
| 489 | * This dummy fd usage silences the mktemp() unsecure warning. |
| 490 | * Using mkstemp() doesn't make things more secure here |
| 491 | * though. bind() complains about existing files, so we have |
| 492 | * to unlink first and thus re-open the race window. The |
| 493 | * worst case possible is bind() failing, i.e. a DoS attack. |
| 494 | */ |
| 495 | fd = mkstemp(un.sun_path); close(fd); |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 496 | qemu_opt_set(opts, "path", un.sun_path); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 497 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 498 | |
| 499 | unlink(un.sun_path); |
| 500 | if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { |
| 501 | fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno)); |
| 502 | goto err; |
| 503 | } |
| 504 | if (listen(sock, 1) < 0) { |
| 505 | fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno)); |
| 506 | goto err; |
| 507 | } |
| 508 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 509 | return sock; |
| 510 | |
| 511 | err: |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 512 | closesocket(sock); |
| 513 | return -1; |
| 514 | } |
| 515 | |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 516 | int unix_connect_opts(QemuOpts *opts) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 517 | { |
| 518 | struct sockaddr_un un; |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 519 | const char *path = qemu_opt_get(opts, "path"); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 520 | int sock; |
| 521 | |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 522 | if (NULL == path) { |
| 523 | fprintf(stderr, "unix connect: no path specified\n"); |
| 524 | return -1; |
| 525 | } |
| 526 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 527 | sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 528 | if (sock < 0) { |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 529 | perror("socket(unix)"); |
| 530 | return -1; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | memset(&un, 0, sizeof(un)); |
| 534 | un.sun_family = AF_UNIX; |
| 535 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
| 536 | if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { |
| 537 | fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno)); |
Markus Armbruster | 9d94747 | 2011-11-11 10:40:07 +0100 | [diff] [blame] | 538 | close(sock); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 539 | return -1; |
| 540 | } |
| 541 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 542 | return sock; |
| 543 | } |
| 544 | |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 545 | /* compatibility wrapper */ |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 546 | int unix_listen(const char *str, char *ostr, int olen) |
| 547 | { |
| 548 | QemuOpts *opts; |
| 549 | char *path, *optstr; |
| 550 | int sock, len; |
| 551 | |
| 552 | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
| 553 | |
| 554 | optstr = strchr(str, ','); |
| 555 | if (optstr) { |
| 556 | len = optstr - str; |
| 557 | if (len) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 558 | path = g_malloc(len+1); |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 559 | snprintf(path, len+1, "%.*s", len, str); |
| 560 | qemu_opt_set(opts, "path", path); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 561 | g_free(path); |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 562 | } |
| 563 | } else { |
| 564 | qemu_opt_set(opts, "path", str); |
| 565 | } |
| 566 | |
| 567 | sock = unix_listen_opts(opts); |
| 568 | |
| 569 | if (sock != -1 && ostr) |
| 570 | snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : ""); |
| 571 | qemu_opts_del(opts); |
| 572 | return sock; |
| 573 | } |
| 574 | |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 575 | int unix_connect(const char *path) |
| 576 | { |
| 577 | QemuOpts *opts; |
| 578 | int sock; |
| 579 | |
| 580 | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
| 581 | qemu_opt_set(opts, "path", path); |
| 582 | sock = unix_connect_opts(opts); |
| 583 | qemu_opts_del(opts); |
| 584 | return sock; |
| 585 | } |
| 586 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 587 | #else |
| 588 | |
Gerd Hoffmann | 108af7b | 2009-09-10 10:58:39 +0200 | [diff] [blame] | 589 | int unix_listen_opts(QemuOpts *opts) |
| 590 | { |
| 591 | fprintf(stderr, "unix sockets are not available on windows\n"); |
Nick Thomas | b82eac9 | 2011-02-22 15:44:52 +0000 | [diff] [blame] | 592 | errno = ENOTSUP; |
Gerd Hoffmann | 108af7b | 2009-09-10 10:58:39 +0200 | [diff] [blame] | 593 | return -1; |
| 594 | } |
| 595 | |
| 596 | int unix_connect_opts(QemuOpts *opts) |
| 597 | { |
| 598 | fprintf(stderr, "unix sockets are not available on windows\n"); |
Nick Thomas | b82eac9 | 2011-02-22 15:44:52 +0000 | [diff] [blame] | 599 | errno = ENOTSUP; |
Gerd Hoffmann | 108af7b | 2009-09-10 10:58:39 +0200 | [diff] [blame] | 600 | return -1; |
| 601 | } |
| 602 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 603 | int unix_listen(const char *path, char *ostr, int olen) |
| 604 | { |
| 605 | fprintf(stderr, "unix sockets are not available on windows\n"); |
Nick Thomas | b82eac9 | 2011-02-22 15:44:52 +0000 | [diff] [blame] | 606 | errno = ENOTSUP; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 607 | return -1; |
| 608 | } |
| 609 | |
| 610 | int unix_connect(const char *path) |
| 611 | { |
| 612 | fprintf(stderr, "unix sockets are not available on windows\n"); |
Nick Thomas | b82eac9 | 2011-02-22 15:44:52 +0000 | [diff] [blame] | 613 | errno = ENOTSUP; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 614 | return -1; |
| 615 | } |
| 616 | |
| 617 | #endif |
Paolo Bonzini | 0706a4d | 2010-04-01 19:57:08 +0200 | [diff] [blame] | 618 | |
| 619 | #ifdef _WIN32 |
| 620 | static void socket_cleanup(void) |
| 621 | { |
| 622 | WSACleanup(); |
| 623 | } |
| 624 | #endif |
| 625 | |
| 626 | int socket_init(void) |
| 627 | { |
| 628 | #ifdef _WIN32 |
| 629 | WSADATA Data; |
| 630 | int ret, err; |
| 631 | |
| 632 | ret = WSAStartup(MAKEWORD(2,2), &Data); |
| 633 | if (ret != 0) { |
| 634 | err = WSAGetLastError(); |
| 635 | fprintf(stderr, "WSAStartup: %d\n", err); |
| 636 | return -1; |
| 637 | } |
| 638 | atexit(socket_cleanup); |
| 639 | #endif |
| 640 | return 0; |
| 641 | } |