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 */ |
| 33 | QemuOptsList dummy_opts = { |
| 34 | .name = "dummy", |
| 35 | .head = TAILQ_HEAD_INITIALIZER(dummy_opts.head), |
| 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 | |
| 119 | int inet_listen(const char *str, char *ostr, int olen, |
| 120 | int socktype, int port_offset) |
| 121 | { |
| 122 | struct addrinfo ai,*res,*e; |
| 123 | char addr[64]; |
| 124 | char port[33]; |
| 125 | char uaddr[INET6_ADDRSTRLEN+1]; |
| 126 | char uport[33]; |
| 127 | const char *opts, *h; |
| 128 | int slisten,rc,pos,to,try_next; |
| 129 | |
| 130 | memset(&ai,0, sizeof(ai)); |
| 131 | ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; |
| 132 | ai.ai_family = PF_UNSPEC; |
| 133 | ai.ai_socktype = socktype; |
| 134 | |
| 135 | /* parse address */ |
| 136 | if (str[0] == ':') { |
| 137 | /* no host given */ |
blueswir1 | bc575e9 | 2009-01-14 18:34:22 +0000 | [diff] [blame] | 138 | addr[0] = '\0'; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 139 | if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { |
| 140 | fprintf(stderr, "%s: portonly parse error (%s)\n", |
| 141 | __FUNCTION__, str); |
| 142 | return -1; |
| 143 | } |
| 144 | } else if (str[0] == '[') { |
| 145 | /* IPv6 addr */ |
| 146 | if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) { |
| 147 | fprintf(stderr, "%s: ipv6 parse error (%s)\n", |
| 148 | __FUNCTION__, str); |
| 149 | return -1; |
| 150 | } |
| 151 | ai.ai_family = PF_INET6; |
blueswir1 | 47398b9 | 2008-11-22 20:04:24 +0000 | [diff] [blame] | 152 | } else if (qemu_isdigit(str[0])) { |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 153 | /* IPv4 addr */ |
| 154 | if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { |
| 155 | fprintf(stderr, "%s: ipv4 parse error (%s)\n", |
| 156 | __FUNCTION__, str); |
| 157 | return -1; |
| 158 | } |
| 159 | ai.ai_family = PF_INET; |
| 160 | } else { |
| 161 | /* hostname */ |
| 162 | if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) { |
| 163 | fprintf(stderr, "%s: hostname parse error (%s)\n", |
| 164 | __FUNCTION__, str); |
| 165 | return -1; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* parse options */ |
| 170 | opts = str + pos; |
| 171 | h = strstr(opts, ",to="); |
| 172 | to = h ? atoi(h+4) : 0; |
| 173 | if (strstr(opts, ",ipv4")) |
| 174 | ai.ai_family = PF_INET; |
| 175 | if (strstr(opts, ",ipv6")) |
| 176 | ai.ai_family = PF_INET6; |
| 177 | |
| 178 | /* lookup */ |
| 179 | if (port_offset) |
| 180 | snprintf(port, sizeof(port), "%d", atoi(port) + port_offset); |
| 181 | rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res); |
| 182 | if (rc != 0) { |
| 183 | fprintf(stderr,"%s: getaddrinfo(%s,%s): %s\n", __FUNCTION__, |
| 184 | addr, port, gai_strerror(rc)); |
| 185 | return -1; |
| 186 | } |
| 187 | if (sockets_debug) |
| 188 | inet_print_addrinfo(__FUNCTION__, res); |
| 189 | |
| 190 | /* create socket + bind */ |
| 191 | for (e = res; e != NULL; e = e->ai_next) { |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 192 | getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
| 193 | uaddr,INET6_ADDRSTRLEN,uport,32, |
| 194 | NI_NUMERICHOST | NI_NUMERICSERV); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 195 | slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 196 | if (slisten < 0) { |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 197 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, |
| 198 | inet_strfamily(e->ai_family), strerror(errno)); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 199 | continue; |
| 200 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 201 | |
| 202 | setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
| 203 | #ifdef IPV6_V6ONLY |
| 204 | if (e->ai_family == PF_INET6) { |
| 205 | /* listen on both ipv4 and ipv6 */ |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 206 | setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off, |
| 207 | sizeof(off)); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 208 | } |
| 209 | #endif |
| 210 | |
| 211 | for (;;) { |
| 212 | if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { |
| 213 | if (sockets_debug) |
| 214 | fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 215 | inet_strfamily(e->ai_family), uaddr, inet_getport(e)); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 216 | goto listen; |
| 217 | } |
| 218 | try_next = to && (inet_getport(e) <= to + port_offset); |
| 219 | if (!try_next || sockets_debug) |
| 220 | fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__, |
| 221 | inet_strfamily(e->ai_family), uaddr, inet_getport(e), |
| 222 | strerror(errno)); |
| 223 | if (try_next) { |
| 224 | inet_setport(e, inet_getport(e) + 1); |
| 225 | continue; |
| 226 | } |
| 227 | break; |
| 228 | } |
| 229 | closesocket(slisten); |
| 230 | } |
| 231 | fprintf(stderr, "%s: FAILED\n", __FUNCTION__); |
| 232 | freeaddrinfo(res); |
| 233 | return -1; |
| 234 | |
| 235 | listen: |
| 236 | if (listen(slisten,1) != 0) { |
| 237 | perror("listen"); |
| 238 | closesocket(slisten); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 239 | freeaddrinfo(res); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | if (ostr) { |
| 243 | if (e->ai_family == PF_INET6) { |
| 244 | snprintf(ostr, olen, "[%s]:%d%s", uaddr, |
| 245 | inet_getport(e) - port_offset, opts); |
| 246 | } else { |
| 247 | snprintf(ostr, olen, "%s:%d%s", uaddr, |
| 248 | inet_getport(e) - port_offset, opts); |
| 249 | } |
| 250 | } |
| 251 | freeaddrinfo(res); |
| 252 | return slisten; |
| 253 | } |
| 254 | |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame^] | 255 | int inet_connect_opts(QemuOpts *opts) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 256 | { |
| 257 | struct addrinfo ai,*res,*e; |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame^] | 258 | const char *addr; |
| 259 | const char *port; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 260 | char uaddr[INET6_ADDRSTRLEN+1]; |
| 261 | char uport[33]; |
| 262 | int sock,rc; |
| 263 | |
| 264 | memset(&ai,0, sizeof(ai)); |
| 265 | ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; |
| 266 | ai.ai_family = PF_UNSPEC; |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame^] | 267 | ai.ai_socktype = SOCK_STREAM; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 268 | |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame^] | 269 | addr = qemu_opt_get(opts, "host"); |
| 270 | port = qemu_opt_get(opts, "port"); |
| 271 | if (addr == NULL || port == NULL) { |
| 272 | fprintf(stderr, "inet_connect: host and/or port not specified\n"); |
| 273 | return -1; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame^] | 276 | if (qemu_opt_get_bool(opts, "ipv4", 0)) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 277 | ai.ai_family = PF_INET; |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame^] | 278 | if (qemu_opt_get_bool(opts, "ipv6", 0)) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 279 | ai.ai_family = PF_INET6; |
| 280 | |
| 281 | /* lookup */ |
| 282 | if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) { |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame^] | 283 | fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port, |
| 284 | gai_strerror(rc)); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 285 | return -1; |
| 286 | } |
| 287 | if (sockets_debug) |
| 288 | inet_print_addrinfo(__FUNCTION__, res); |
| 289 | |
| 290 | for (e = res; e != NULL; e = e->ai_next) { |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 291 | if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, |
| 292 | uaddr,INET6_ADDRSTRLEN,uport,32, |
| 293 | NI_NUMERICHOST | NI_NUMERICSERV) != 0) { |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 294 | fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 295 | continue; |
| 296 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 297 | sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 298 | if (sock < 0) { |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 299 | fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 300 | inet_strfamily(e->ai_family), strerror(errno)); |
| 301 | continue; |
| 302 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 303 | setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); |
| 304 | |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 305 | /* connect to peer */ |
| 306 | if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) { |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 307 | if (sockets_debug || NULL == e->ai_next) |
| 308 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__, |
| 309 | inet_strfamily(e->ai_family), |
| 310 | e->ai_canonname, uaddr, uport, strerror(errno)); |
| 311 | closesocket(sock); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 312 | continue; |
| 313 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 314 | if (sockets_debug) |
| 315 | fprintf(stderr, "%s: connect(%s,%s,%s,%s): OK\n", __FUNCTION__, |
| 316 | inet_strfamily(e->ai_family), |
| 317 | e->ai_canonname, uaddr, uport); |
| 318 | freeaddrinfo(res); |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 319 | return sock; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 320 | } |
| 321 | freeaddrinfo(res); |
| 322 | return -1; |
| 323 | } |
| 324 | |
Gerd Hoffmann | f4c94c7 | 2009-09-10 10:58:40 +0200 | [diff] [blame^] | 325 | /* compatibility wrapper */ |
| 326 | static int inet_parse(QemuOpts *opts, const char *str) |
| 327 | { |
| 328 | const char *optstr, *h; |
| 329 | char addr[64]; |
| 330 | char port[33]; |
| 331 | int pos; |
| 332 | |
| 333 | /* parse address */ |
| 334 | if (str[0] == ':') { |
| 335 | /* no host given */ |
| 336 | addr[0] = '\0'; |
| 337 | if (1 != sscanf(str,":%32[^,]%n",port,&pos)) { |
| 338 | fprintf(stderr, "%s: portonly parse error (%s)\n", |
| 339 | __FUNCTION__, str); |
| 340 | return -1; |
| 341 | } |
| 342 | } else if (str[0] == '[') { |
| 343 | /* IPv6 addr */ |
| 344 | if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) { |
| 345 | fprintf(stderr, "%s: ipv6 parse error (%s)\n", |
| 346 | __FUNCTION__, str); |
| 347 | return -1; |
| 348 | } |
| 349 | qemu_opt_set(opts, "ipv6", "yes"); |
| 350 | } else if (qemu_isdigit(str[0])) { |
| 351 | /* IPv4 addr */ |
| 352 | if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) { |
| 353 | fprintf(stderr, "%s: ipv4 parse error (%s)\n", |
| 354 | __FUNCTION__, str); |
| 355 | return -1; |
| 356 | } |
| 357 | qemu_opt_set(opts, "ipv4", "yes"); |
| 358 | } else { |
| 359 | /* hostname */ |
| 360 | if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) { |
| 361 | fprintf(stderr, "%s: hostname parse error (%s)\n", |
| 362 | __FUNCTION__, str); |
| 363 | return -1; |
| 364 | } |
| 365 | } |
| 366 | qemu_opt_set(opts, "host", addr); |
| 367 | qemu_opt_set(opts, "port", port); |
| 368 | |
| 369 | /* parse options */ |
| 370 | optstr = str + pos; |
| 371 | h = strstr(optstr, ",to="); |
| 372 | if (h) |
| 373 | qemu_opt_set(opts, "to", h+4); |
| 374 | if (strstr(optstr, ",ipv4")) |
| 375 | qemu_opt_set(opts, "ipv4", "yes"); |
| 376 | if (strstr(optstr, ",ipv6")) |
| 377 | qemu_opt_set(opts, "ipv6", "yes"); |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | int inet_connect(const char *str, int socktype) |
| 382 | { |
| 383 | QemuOpts *opts; |
| 384 | int sock = -1; |
| 385 | |
| 386 | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
| 387 | if (inet_parse(opts, str) == 0) |
| 388 | sock = inet_connect_opts(opts); |
| 389 | qemu_opts_del(opts); |
| 390 | return sock; |
| 391 | } |
| 392 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 393 | #ifndef _WIN32 |
| 394 | |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 395 | int unix_listen_opts(QemuOpts *opts) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 396 | { |
| 397 | struct sockaddr_un un; |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 398 | const char *path = qemu_opt_get(opts, "path"); |
| 399 | int sock, fd; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 400 | |
| 401 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 402 | if (sock < 0) { |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 403 | perror("socket(unix)"); |
| 404 | return -1; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 405 | } |
| 406 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 407 | memset(&un, 0, sizeof(un)); |
| 408 | un.sun_family = AF_UNIX; |
| 409 | if (path && strlen(path)) { |
| 410 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
| 411 | } else { |
| 412 | char *tmpdir = getenv("TMPDIR"); |
| 413 | snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX", |
| 414 | tmpdir ? tmpdir : "/tmp"); |
| 415 | /* |
| 416 | * This dummy fd usage silences the mktemp() unsecure warning. |
| 417 | * Using mkstemp() doesn't make things more secure here |
| 418 | * though. bind() complains about existing files, so we have |
| 419 | * to unlink first and thus re-open the race window. The |
| 420 | * worst case possible is bind() failing, i.e. a DoS attack. |
| 421 | */ |
| 422 | fd = mkstemp(un.sun_path); close(fd); |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 423 | qemu_opt_set(opts, "path", un.sun_path); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 424 | } |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 425 | |
| 426 | unlink(un.sun_path); |
| 427 | if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { |
| 428 | fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno)); |
| 429 | goto err; |
| 430 | } |
| 431 | if (listen(sock, 1) < 0) { |
| 432 | fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno)); |
| 433 | goto err; |
| 434 | } |
| 435 | |
| 436 | if (sockets_debug) |
| 437 | fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 438 | return sock; |
| 439 | |
| 440 | err: |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 441 | closesocket(sock); |
| 442 | return -1; |
| 443 | } |
| 444 | |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 445 | int unix_connect_opts(QemuOpts *opts) |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 446 | { |
| 447 | struct sockaddr_un un; |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 448 | const char *path = qemu_opt_get(opts, "path"); |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 449 | int sock; |
| 450 | |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 451 | if (NULL == path) { |
| 452 | fprintf(stderr, "unix connect: no path specified\n"); |
| 453 | return -1; |
| 454 | } |
| 455 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 456 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 457 | if (sock < 0) { |
vibi | 39b6efc | 2009-05-06 15:27:03 +0530 | [diff] [blame] | 458 | perror("socket(unix)"); |
| 459 | return -1; |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | memset(&un, 0, sizeof(un)); |
| 463 | un.sun_family = AF_UNIX; |
| 464 | snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); |
| 465 | if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { |
| 466 | fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno)); |
| 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | if (sockets_debug) |
| 471 | fprintf(stderr, "connect(unix:%s): OK\n", path); |
| 472 | return sock; |
| 473 | } |
| 474 | |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 475 | /* compatibility wrapper */ |
Gerd Hoffmann | 62b6adf | 2009-09-10 10:58:38 +0200 | [diff] [blame] | 476 | int unix_listen(const char *str, char *ostr, int olen) |
| 477 | { |
| 478 | QemuOpts *opts; |
| 479 | char *path, *optstr; |
| 480 | int sock, len; |
| 481 | |
| 482 | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
| 483 | |
| 484 | optstr = strchr(str, ','); |
| 485 | if (optstr) { |
| 486 | len = optstr - str; |
| 487 | if (len) { |
| 488 | path = qemu_malloc(len+1); |
| 489 | snprintf(path, len+1, "%.*s", len, str); |
| 490 | qemu_opt_set(opts, "path", path); |
| 491 | qemu_free(path); |
| 492 | } |
| 493 | } else { |
| 494 | qemu_opt_set(opts, "path", str); |
| 495 | } |
| 496 | |
| 497 | sock = unix_listen_opts(opts); |
| 498 | |
| 499 | if (sock != -1 && ostr) |
| 500 | snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : ""); |
| 501 | qemu_opts_del(opts); |
| 502 | return sock; |
| 503 | } |
| 504 | |
Gerd Hoffmann | 2af2bf6 | 2009-09-10 10:58:37 +0200 | [diff] [blame] | 505 | int unix_connect(const char *path) |
| 506 | { |
| 507 | QemuOpts *opts; |
| 508 | int sock; |
| 509 | |
| 510 | opts = qemu_opts_create(&dummy_opts, NULL, 0); |
| 511 | qemu_opt_set(opts, "path", path); |
| 512 | sock = unix_connect_opts(opts); |
| 513 | qemu_opts_del(opts); |
| 514 | return sock; |
| 515 | } |
| 516 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 517 | #else |
| 518 | |
Gerd Hoffmann | 108af7b | 2009-09-10 10:58:39 +0200 | [diff] [blame] | 519 | int unix_listen_opts(QemuOpts *opts) |
| 520 | { |
| 521 | fprintf(stderr, "unix sockets are not available on windows\n"); |
| 522 | return -1; |
| 523 | } |
| 524 | |
| 525 | int unix_connect_opts(QemuOpts *opts) |
| 526 | { |
| 527 | fprintf(stderr, "unix sockets are not available on windows\n"); |
| 528 | return -1; |
| 529 | } |
| 530 | |
aliguori | d247d25 | 2008-11-11 20:46:40 +0000 | [diff] [blame] | 531 | int unix_listen(const char *path, char *ostr, int olen) |
| 532 | { |
| 533 | fprintf(stderr, "unix sockets are not available on windows\n"); |
| 534 | return -1; |
| 535 | } |
| 536 | |
| 537 | int unix_connect(const char *path) |
| 538 | { |
| 539 | fprintf(stderr, "unix sockets are not available on windows\n"); |
| 540 | return -1; |
| 541 | } |
| 542 | |
| 543 | #endif |