2
0

qemu-sockets.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. */
  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. #include "qemu_socket.h"
  22. #include "qemu-common.h" /* for qemu_isdigit */
  23. #ifndef AI_ADDRCONFIG
  24. # define AI_ADDRCONFIG 0
  25. #endif
  26. static int sockets_debug = 0;
  27. static const int on=1, off=0;
  28. static int inet_getport(struct addrinfo *e)
  29. {
  30. struct sockaddr_in *i4;
  31. struct sockaddr_in6 *i6;
  32. switch (e->ai_family) {
  33. case PF_INET6:
  34. i6 = (void*)e->ai_addr;
  35. return ntohs(i6->sin6_port);
  36. case PF_INET:
  37. i4 = (void*)e->ai_addr;
  38. return ntohs(i4->sin_port);
  39. default:
  40. return 0;
  41. }
  42. }
  43. static void inet_setport(struct addrinfo *e, int port)
  44. {
  45. struct sockaddr_in *i4;
  46. struct sockaddr_in6 *i6;
  47. switch (e->ai_family) {
  48. case PF_INET6:
  49. i6 = (void*)e->ai_addr;
  50. i6->sin6_port = htons(port);
  51. break;
  52. case PF_INET:
  53. i4 = (void*)e->ai_addr;
  54. i4->sin_port = htons(port);
  55. break;
  56. }
  57. }
  58. static const char *inet_strfamily(int family)
  59. {
  60. switch (family) {
  61. case PF_INET6: return "ipv6";
  62. case PF_INET: return "ipv4";
  63. case PF_UNIX: return "unix";
  64. }
  65. return "????";
  66. }
  67. static void inet_print_addrinfo(const char *tag, struct addrinfo *res)
  68. {
  69. struct addrinfo *e;
  70. char uaddr[INET6_ADDRSTRLEN+1];
  71. char uport[33];
  72. for (e = res; e != NULL; e = e->ai_next) {
  73. getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
  74. uaddr,INET6_ADDRSTRLEN,uport,32,
  75. NI_NUMERICHOST | NI_NUMERICSERV);
  76. fprintf(stderr,"%s: getaddrinfo: family %s, host %s, port %s\n",
  77. tag, inet_strfamily(e->ai_family), uaddr, uport);
  78. }
  79. }
  80. int inet_listen(const char *str, char *ostr, int olen,
  81. int socktype, int port_offset)
  82. {
  83. struct addrinfo ai,*res,*e;
  84. char addr[64];
  85. char port[33];
  86. char uaddr[INET6_ADDRSTRLEN+1];
  87. char uport[33];
  88. const char *opts, *h;
  89. int slisten,rc,pos,to,try_next;
  90. memset(&ai,0, sizeof(ai));
  91. ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
  92. ai.ai_family = PF_UNSPEC;
  93. ai.ai_socktype = socktype;
  94. /* parse address */
  95. if (str[0] == ':') {
  96. /* no host given */
  97. addr[0] = '\0';
  98. if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
  99. fprintf(stderr, "%s: portonly parse error (%s)\n",
  100. __FUNCTION__, str);
  101. return -1;
  102. }
  103. } else if (str[0] == '[') {
  104. /* IPv6 addr */
  105. if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
  106. fprintf(stderr, "%s: ipv6 parse error (%s)\n",
  107. __FUNCTION__, str);
  108. return -1;
  109. }
  110. ai.ai_family = PF_INET6;
  111. } else if (qemu_isdigit(str[0])) {
  112. /* IPv4 addr */
  113. if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
  114. fprintf(stderr, "%s: ipv4 parse error (%s)\n",
  115. __FUNCTION__, str);
  116. return -1;
  117. }
  118. ai.ai_family = PF_INET;
  119. } else {
  120. /* hostname */
  121. if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
  122. fprintf(stderr, "%s: hostname parse error (%s)\n",
  123. __FUNCTION__, str);
  124. return -1;
  125. }
  126. }
  127. /* parse options */
  128. opts = str + pos;
  129. h = strstr(opts, ",to=");
  130. to = h ? atoi(h+4) : 0;
  131. if (strstr(opts, ",ipv4"))
  132. ai.ai_family = PF_INET;
  133. if (strstr(opts, ",ipv6"))
  134. ai.ai_family = PF_INET6;
  135. /* lookup */
  136. if (port_offset)
  137. snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
  138. rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
  139. if (rc != 0) {
  140. fprintf(stderr,"%s: getaddrinfo(%s,%s): %s\n", __FUNCTION__,
  141. addr, port, gai_strerror(rc));
  142. return -1;
  143. }
  144. if (sockets_debug)
  145. inet_print_addrinfo(__FUNCTION__, res);
  146. /* create socket + bind */
  147. for (e = res; e != NULL; e = e->ai_next) {
  148. getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
  149. uaddr,INET6_ADDRSTRLEN,uport,32,
  150. NI_NUMERICHOST | NI_NUMERICSERV);
  151. slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
  152. if (slisten < 0) {
  153. fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
  154. inet_strfamily(e->ai_family), strerror(errno));
  155. continue;
  156. }
  157. setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
  158. #ifdef IPV6_V6ONLY
  159. if (e->ai_family == PF_INET6) {
  160. /* listen on both ipv4 and ipv6 */
  161. setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,sizeof(off));
  162. }
  163. #endif
  164. for (;;) {
  165. if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
  166. if (sockets_debug)
  167. fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
  168. inet_strfamily(e->ai_family), uaddr, inet_getport(e));
  169. goto listen;
  170. }
  171. try_next = to && (inet_getport(e) <= to + port_offset);
  172. if (!try_next || sockets_debug)
  173. fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
  174. inet_strfamily(e->ai_family), uaddr, inet_getport(e),
  175. strerror(errno));
  176. if (try_next) {
  177. inet_setport(e, inet_getport(e) + 1);
  178. continue;
  179. }
  180. break;
  181. }
  182. closesocket(slisten);
  183. }
  184. fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
  185. freeaddrinfo(res);
  186. return -1;
  187. listen:
  188. if (listen(slisten,1) != 0) {
  189. perror("listen");
  190. closesocket(slisten);
  191. return -1;
  192. }
  193. if (ostr) {
  194. if (e->ai_family == PF_INET6) {
  195. snprintf(ostr, olen, "[%s]:%d%s", uaddr,
  196. inet_getport(e) - port_offset, opts);
  197. } else {
  198. snprintf(ostr, olen, "%s:%d%s", uaddr,
  199. inet_getport(e) - port_offset, opts);
  200. }
  201. }
  202. freeaddrinfo(res);
  203. return slisten;
  204. }
  205. int inet_connect(const char *str, int socktype)
  206. {
  207. struct addrinfo ai,*res,*e;
  208. char addr[64];
  209. char port[33];
  210. char uaddr[INET6_ADDRSTRLEN+1];
  211. char uport[33];
  212. int sock,rc;
  213. memset(&ai,0, sizeof(ai));
  214. ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
  215. ai.ai_family = PF_UNSPEC;
  216. ai.ai_socktype = socktype;
  217. /* parse address */
  218. if (str[0] == '[') {
  219. /* IPv6 addr */
  220. if (2 != sscanf(str,"[%64[^]]]:%32[^,]",addr,port)) {
  221. fprintf(stderr, "%s: ipv6 parse error (%s)\n",
  222. __FUNCTION__, str);
  223. return -1;
  224. }
  225. ai.ai_family = PF_INET6;
  226. } else if (qemu_isdigit(str[0])) {
  227. /* IPv4 addr */
  228. if (2 != sscanf(str,"%64[0-9.]:%32[^,]",addr,port)) {
  229. fprintf(stderr, "%s: ipv4 parse error (%s)\n",
  230. __FUNCTION__, str);
  231. return -1;
  232. }
  233. ai.ai_family = PF_INET;
  234. } else {
  235. /* hostname */
  236. if (2 != sscanf(str,"%64[^:]:%32[^,]",addr,port)) {
  237. fprintf(stderr, "%s: hostname parse error (%s)\n",
  238. __FUNCTION__, str);
  239. return -1;
  240. }
  241. }
  242. /* parse options */
  243. if (strstr(str, ",ipv4"))
  244. ai.ai_family = PF_INET;
  245. if (strstr(str, ",ipv6"))
  246. ai.ai_family = PF_INET6;
  247. /* lookup */
  248. if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
  249. fprintf(stderr,"getaddrinfo(%s,%s): %s\n", gai_strerror(rc),
  250. addr, port);
  251. return -1;
  252. }
  253. if (sockets_debug)
  254. inet_print_addrinfo(__FUNCTION__, res);
  255. for (e = res; e != NULL; e = e->ai_next) {
  256. if (getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
  257. uaddr,INET6_ADDRSTRLEN,uport,32,
  258. NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
  259. fprintf(stderr,"%s: getnameinfo: oops\n", __FUNCTION__);
  260. continue;
  261. }
  262. sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
  263. if (sock < 0) {
  264. fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
  265. inet_strfamily(e->ai_family), strerror(errno));
  266. continue;
  267. }
  268. setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
  269. /* connect to peer */
  270. if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
  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);
  276. continue;
  277. }
  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);
  283. return sock;
  284. }
  285. freeaddrinfo(res);
  286. return -1;
  287. }
  288. #ifndef _WIN32
  289. int unix_listen(const char *str, char *ostr, int olen)
  290. {
  291. struct sockaddr_un un;
  292. char *path, *opts;
  293. int sock, fd, len;
  294. sock = socket(PF_UNIX, SOCK_STREAM, 0);
  295. if (sock < 0) {
  296. perror("socket(unix)");
  297. return -1;
  298. }
  299. opts = strchr(str, ',');
  300. if (opts) {
  301. len = opts - str;
  302. path = malloc(len+1);
  303. snprintf(path, len+1, "%.*s", len, str);
  304. } else
  305. path = strdup(str);
  306. memset(&un, 0, sizeof(un));
  307. un.sun_family = AF_UNIX;
  308. if (path && strlen(path)) {
  309. snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
  310. } else {
  311. char *tmpdir = getenv("TMPDIR");
  312. snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
  313. tmpdir ? tmpdir : "/tmp");
  314. /*
  315. * This dummy fd usage silences the mktemp() unsecure warning.
  316. * Using mkstemp() doesn't make things more secure here
  317. * though. bind() complains about existing files, so we have
  318. * to unlink first and thus re-open the race window. The
  319. * worst case possible is bind() failing, i.e. a DoS attack.
  320. */
  321. fd = mkstemp(un.sun_path); close(fd);
  322. }
  323. snprintf(ostr, olen, "%s%s", un.sun_path, opts ? opts : "");
  324. unlink(un.sun_path);
  325. if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
  326. fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
  327. goto err;
  328. }
  329. if (listen(sock, 1) < 0) {
  330. fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
  331. goto err;
  332. }
  333. if (sockets_debug)
  334. fprintf(stderr, "bind(unix:%s): OK\n", un.sun_path);
  335. free(path);
  336. return sock;
  337. err:
  338. free(path);
  339. closesocket(sock);
  340. return -1;
  341. }
  342. int unix_connect(const char *path)
  343. {
  344. struct sockaddr_un un;
  345. int sock;
  346. sock = socket(PF_UNIX, SOCK_STREAM, 0);
  347. if (sock < 0) {
  348. perror("socket(unix)");
  349. return -1;
  350. }
  351. memset(&un, 0, sizeof(un));
  352. un.sun_family = AF_UNIX;
  353. snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
  354. if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
  355. fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
  356. return -1;
  357. }
  358. if (sockets_debug)
  359. fprintf(stderr, "connect(unix:%s): OK\n", path);
  360. return sock;
  361. }
  362. #else
  363. int unix_listen(const char *path, char *ostr, int olen)
  364. {
  365. fprintf(stderr, "unix sockets are not available on windows\n");
  366. return -1;
  367. }
  368. int unix_connect(const char *path)
  369. {
  370. fprintf(stderr, "unix sockets are not available on windows\n");
  371. return -1;
  372. }
  373. #endif