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