socket.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "net/net.h"
  26. #include "clients.h"
  27. #include "monitor/monitor.h"
  28. #include "qemu-common.h"
  29. #include "qemu/error-report.h"
  30. #include "qemu/option.h"
  31. #include "qemu/sockets.h"
  32. #include "qemu/iov.h"
  33. #include "qemu/main-loop.h"
  34. typedef struct NetSocketState {
  35. NetClientState nc;
  36. int listen_fd;
  37. int fd;
  38. int state; /* 0 = getting length, 1 = getting data */
  39. unsigned int index;
  40. unsigned int packet_len;
  41. unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */
  42. uint8_t buf[NET_BUFSIZE];
  43. struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
  44. IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */
  45. bool read_poll; /* waiting to receive data? */
  46. bool write_poll; /* waiting to transmit data? */
  47. } NetSocketState;
  48. static void net_socket_accept(void *opaque);
  49. static void net_socket_writable(void *opaque);
  50. static void net_socket_update_fd_handler(NetSocketState *s)
  51. {
  52. qemu_set_fd_handler(s->fd,
  53. s->read_poll ? s->send_fn : NULL,
  54. s->write_poll ? net_socket_writable : NULL,
  55. s);
  56. }
  57. static void net_socket_read_poll(NetSocketState *s, bool enable)
  58. {
  59. s->read_poll = enable;
  60. net_socket_update_fd_handler(s);
  61. }
  62. static void net_socket_write_poll(NetSocketState *s, bool enable)
  63. {
  64. s->write_poll = enable;
  65. net_socket_update_fd_handler(s);
  66. }
  67. static void net_socket_writable(void *opaque)
  68. {
  69. NetSocketState *s = opaque;
  70. net_socket_write_poll(s, false);
  71. qemu_flush_queued_packets(&s->nc);
  72. }
  73. static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
  74. {
  75. NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
  76. uint32_t len = htonl(size);
  77. struct iovec iov[] = {
  78. {
  79. .iov_base = &len,
  80. .iov_len = sizeof(len),
  81. }, {
  82. .iov_base = (void *)buf,
  83. .iov_len = size,
  84. },
  85. };
  86. size_t remaining;
  87. ssize_t ret;
  88. remaining = iov_size(iov, 2) - s->send_index;
  89. ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
  90. if (ret == -1 && errno == EAGAIN) {
  91. ret = 0; /* handled further down */
  92. }
  93. if (ret == -1) {
  94. s->send_index = 0;
  95. return -errno;
  96. }
  97. if (ret < (ssize_t)remaining) {
  98. s->send_index += ret;
  99. net_socket_write_poll(s, true);
  100. return 0;
  101. }
  102. s->send_index = 0;
  103. return size;
  104. }
  105. static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
  106. {
  107. NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
  108. ssize_t ret;
  109. do {
  110. ret = qemu_sendto(s->fd, buf, size, 0,
  111. (struct sockaddr *)&s->dgram_dst,
  112. sizeof(s->dgram_dst));
  113. } while (ret == -1 && errno == EINTR);
  114. if (ret == -1 && errno == EAGAIN) {
  115. net_socket_write_poll(s, true);
  116. return 0;
  117. }
  118. return ret;
  119. }
  120. static void net_socket_send_completed(NetClientState *nc, ssize_t len)
  121. {
  122. NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
  123. if (!s->read_poll) {
  124. net_socket_read_poll(s, true);
  125. }
  126. }
  127. static void net_socket_send(void *opaque)
  128. {
  129. NetSocketState *s = opaque;
  130. int size, err;
  131. unsigned l;
  132. uint8_t buf1[NET_BUFSIZE];
  133. const uint8_t *buf;
  134. size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
  135. if (size < 0) {
  136. err = socket_error();
  137. if (err != EWOULDBLOCK)
  138. goto eoc;
  139. } else if (size == 0) {
  140. /* end of connection */
  141. eoc:
  142. net_socket_read_poll(s, false);
  143. net_socket_write_poll(s, false);
  144. if (s->listen_fd != -1) {
  145. qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
  146. }
  147. closesocket(s->fd);
  148. s->fd = -1;
  149. s->state = 0;
  150. s->index = 0;
  151. s->packet_len = 0;
  152. s->nc.link_down = true;
  153. memset(s->buf, 0, sizeof(s->buf));
  154. memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
  155. return;
  156. }
  157. buf = buf1;
  158. while (size > 0) {
  159. /* reassemble a packet from the network */
  160. switch(s->state) {
  161. case 0:
  162. l = 4 - s->index;
  163. if (l > size)
  164. l = size;
  165. memcpy(s->buf + s->index, buf, l);
  166. buf += l;
  167. size -= l;
  168. s->index += l;
  169. if (s->index == 4) {
  170. /* got length */
  171. s->packet_len = ntohl(*(uint32_t *)s->buf);
  172. s->index = 0;
  173. s->state = 1;
  174. }
  175. break;
  176. case 1:
  177. l = s->packet_len - s->index;
  178. if (l > size)
  179. l = size;
  180. if (s->index + l <= sizeof(s->buf)) {
  181. memcpy(s->buf + s->index, buf, l);
  182. } else {
  183. fprintf(stderr, "serious error: oversized packet received,"
  184. "connection terminated.\n");
  185. s->state = 0;
  186. goto eoc;
  187. }
  188. s->index += l;
  189. buf += l;
  190. size -= l;
  191. if (s->index >= s->packet_len) {
  192. s->index = 0;
  193. s->state = 0;
  194. if (qemu_send_packet_async(&s->nc, s->buf, s->packet_len,
  195. net_socket_send_completed) == 0) {
  196. net_socket_read_poll(s, false);
  197. break;
  198. }
  199. }
  200. break;
  201. }
  202. }
  203. }
  204. static void net_socket_send_dgram(void *opaque)
  205. {
  206. NetSocketState *s = opaque;
  207. int size;
  208. size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
  209. if (size < 0)
  210. return;
  211. if (size == 0) {
  212. /* end of connection */
  213. net_socket_read_poll(s, false);
  214. net_socket_write_poll(s, false);
  215. return;
  216. }
  217. if (qemu_send_packet_async(&s->nc, s->buf, size,
  218. net_socket_send_completed) == 0) {
  219. net_socket_read_poll(s, false);
  220. }
  221. }
  222. static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)
  223. {
  224. struct ip_mreq imr;
  225. int fd;
  226. int val, ret;
  227. #ifdef __OpenBSD__
  228. unsigned char loop;
  229. #else
  230. int loop;
  231. #endif
  232. if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
  233. fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
  234. "does not contain a multicast address\n",
  235. inet_ntoa(mcastaddr->sin_addr),
  236. (int)ntohl(mcastaddr->sin_addr.s_addr));
  237. return -1;
  238. }
  239. fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
  240. if (fd < 0) {
  241. perror("socket(PF_INET, SOCK_DGRAM)");
  242. return -1;
  243. }
  244. /* Allow multiple sockets to bind the same multicast ip and port by setting
  245. * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
  246. * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
  247. * only on posix systems.
  248. */
  249. val = 1;
  250. ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  251. if (ret < 0) {
  252. perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
  253. goto fail;
  254. }
  255. ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
  256. if (ret < 0) {
  257. perror("bind");
  258. goto fail;
  259. }
  260. /* Add host to multicast group */
  261. imr.imr_multiaddr = mcastaddr->sin_addr;
  262. if (localaddr) {
  263. imr.imr_interface = *localaddr;
  264. } else {
  265. imr.imr_interface.s_addr = htonl(INADDR_ANY);
  266. }
  267. ret = qemu_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  268. &imr, sizeof(struct ip_mreq));
  269. if (ret < 0) {
  270. perror("setsockopt(IP_ADD_MEMBERSHIP)");
  271. goto fail;
  272. }
  273. /* Force mcast msgs to loopback (eg. several QEMUs in same host */
  274. loop = 1;
  275. ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
  276. &loop, sizeof(loop));
  277. if (ret < 0) {
  278. perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
  279. goto fail;
  280. }
  281. /* If a bind address is given, only send packets from that address */
  282. if (localaddr != NULL) {
  283. ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
  284. localaddr, sizeof(*localaddr));
  285. if (ret < 0) {
  286. perror("setsockopt(IP_MULTICAST_IF)");
  287. goto fail;
  288. }
  289. }
  290. qemu_set_nonblock(fd);
  291. return fd;
  292. fail:
  293. if (fd >= 0)
  294. closesocket(fd);
  295. return -1;
  296. }
  297. static void net_socket_cleanup(NetClientState *nc)
  298. {
  299. NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
  300. if (s->fd != -1) {
  301. net_socket_read_poll(s, false);
  302. net_socket_write_poll(s, false);
  303. close(s->fd);
  304. s->fd = -1;
  305. }
  306. if (s->listen_fd != -1) {
  307. qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
  308. closesocket(s->listen_fd);
  309. s->listen_fd = -1;
  310. }
  311. }
  312. static NetClientInfo net_dgram_socket_info = {
  313. .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
  314. .size = sizeof(NetSocketState),
  315. .receive = net_socket_receive_dgram,
  316. .cleanup = net_socket_cleanup,
  317. };
  318. static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
  319. const char *model,
  320. const char *name,
  321. int fd, int is_connected)
  322. {
  323. struct sockaddr_in saddr;
  324. int newfd;
  325. socklen_t saddr_len = sizeof(saddr);
  326. NetClientState *nc;
  327. NetSocketState *s;
  328. /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
  329. * Because this may be "shared" socket from a "master" process, datagrams would be recv()
  330. * by ONLY ONE process: we must "clone" this dgram socket --jjo
  331. */
  332. if (is_connected) {
  333. if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
  334. /* must be bound */
  335. if (saddr.sin_addr.s_addr == 0) {
  336. fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
  337. "cannot setup multicast dst addr\n", fd);
  338. goto err;
  339. }
  340. /* clone dgram socket */
  341. newfd = net_socket_mcast_create(&saddr, NULL);
  342. if (newfd < 0) {
  343. /* error already reported by net_socket_mcast_create() */
  344. goto err;
  345. }
  346. /* clone newfd to fd, close newfd */
  347. dup2(newfd, fd);
  348. close(newfd);
  349. } else {
  350. fprintf(stderr,
  351. "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
  352. fd, strerror(errno));
  353. goto err;
  354. }
  355. }
  356. nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
  357. s = DO_UPCAST(NetSocketState, nc, nc);
  358. s->fd = fd;
  359. s->listen_fd = -1;
  360. s->send_fn = net_socket_send_dgram;
  361. net_socket_read_poll(s, true);
  362. /* mcast: save bound address as dst */
  363. if (is_connected) {
  364. s->dgram_dst = saddr;
  365. snprintf(nc->info_str, sizeof(nc->info_str),
  366. "socket: fd=%d (cloned mcast=%s:%d)",
  367. fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
  368. } else {
  369. snprintf(nc->info_str, sizeof(nc->info_str),
  370. "socket: fd=%d", fd);
  371. }
  372. return s;
  373. err:
  374. closesocket(fd);
  375. return NULL;
  376. }
  377. static void net_socket_connect(void *opaque)
  378. {
  379. NetSocketState *s = opaque;
  380. s->send_fn = net_socket_send;
  381. net_socket_read_poll(s, true);
  382. }
  383. static NetClientInfo net_socket_info = {
  384. .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
  385. .size = sizeof(NetSocketState),
  386. .receive = net_socket_receive,
  387. .cleanup = net_socket_cleanup,
  388. };
  389. static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
  390. const char *model,
  391. const char *name,
  392. int fd, int is_connected)
  393. {
  394. NetClientState *nc;
  395. NetSocketState *s;
  396. nc = qemu_new_net_client(&net_socket_info, peer, model, name);
  397. snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
  398. s = DO_UPCAST(NetSocketState, nc, nc);
  399. s->fd = fd;
  400. s->listen_fd = -1;
  401. /* Disable Nagle algorithm on TCP sockets to reduce latency */
  402. socket_set_nodelay(fd);
  403. if (is_connected) {
  404. net_socket_connect(s);
  405. } else {
  406. qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
  407. }
  408. return s;
  409. }
  410. static NetSocketState *net_socket_fd_init(NetClientState *peer,
  411. const char *model, const char *name,
  412. int fd, int is_connected)
  413. {
  414. int so_type = -1, optlen=sizeof(so_type);
  415. if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
  416. (socklen_t *)&optlen)< 0) {
  417. fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
  418. fd);
  419. closesocket(fd);
  420. return NULL;
  421. }
  422. switch(so_type) {
  423. case SOCK_DGRAM:
  424. return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
  425. case SOCK_STREAM:
  426. return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
  427. default:
  428. /* who knows ... this could be a eg. a pty, do warn and continue as stream */
  429. fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
  430. return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
  431. }
  432. return NULL;
  433. }
  434. static void net_socket_accept(void *opaque)
  435. {
  436. NetSocketState *s = opaque;
  437. struct sockaddr_in saddr;
  438. socklen_t len;
  439. int fd;
  440. for(;;) {
  441. len = sizeof(saddr);
  442. fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
  443. if (fd < 0 && errno != EINTR) {
  444. return;
  445. } else if (fd >= 0) {
  446. qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
  447. break;
  448. }
  449. }
  450. s->fd = fd;
  451. s->nc.link_down = false;
  452. net_socket_connect(s);
  453. snprintf(s->nc.info_str, sizeof(s->nc.info_str),
  454. "socket: connection from %s:%d",
  455. inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
  456. }
  457. static int net_socket_listen_init(NetClientState *peer,
  458. const char *model,
  459. const char *name,
  460. const char *host_str)
  461. {
  462. NetClientState *nc;
  463. NetSocketState *s;
  464. struct sockaddr_in saddr;
  465. int fd, ret;
  466. if (parse_host_port(&saddr, host_str) < 0)
  467. return -1;
  468. fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
  469. if (fd < 0) {
  470. perror("socket");
  471. return -1;
  472. }
  473. qemu_set_nonblock(fd);
  474. socket_set_fast_reuse(fd);
  475. ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
  476. if (ret < 0) {
  477. perror("bind");
  478. closesocket(fd);
  479. return -1;
  480. }
  481. ret = listen(fd, 0);
  482. if (ret < 0) {
  483. perror("listen");
  484. closesocket(fd);
  485. return -1;
  486. }
  487. nc = qemu_new_net_client(&net_socket_info, peer, model, name);
  488. s = DO_UPCAST(NetSocketState, nc, nc);
  489. s->fd = -1;
  490. s->listen_fd = fd;
  491. s->nc.link_down = true;
  492. qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
  493. return 0;
  494. }
  495. static int net_socket_connect_init(NetClientState *peer,
  496. const char *model,
  497. const char *name,
  498. const char *host_str)
  499. {
  500. NetSocketState *s;
  501. int fd, connected, ret, err;
  502. struct sockaddr_in saddr;
  503. if (parse_host_port(&saddr, host_str) < 0)
  504. return -1;
  505. fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
  506. if (fd < 0) {
  507. perror("socket");
  508. return -1;
  509. }
  510. qemu_set_nonblock(fd);
  511. connected = 0;
  512. for(;;) {
  513. ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
  514. if (ret < 0) {
  515. err = socket_error();
  516. if (err == EINTR || err == EWOULDBLOCK) {
  517. } else if (err == EINPROGRESS) {
  518. break;
  519. #ifdef _WIN32
  520. } else if (err == WSAEALREADY || err == WSAEINVAL) {
  521. break;
  522. #endif
  523. } else {
  524. perror("connect");
  525. closesocket(fd);
  526. return -1;
  527. }
  528. } else {
  529. connected = 1;
  530. break;
  531. }
  532. }
  533. s = net_socket_fd_init(peer, model, name, fd, connected);
  534. if (!s)
  535. return -1;
  536. snprintf(s->nc.info_str, sizeof(s->nc.info_str),
  537. "socket: connect to %s:%d",
  538. inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
  539. return 0;
  540. }
  541. static int net_socket_mcast_init(NetClientState *peer,
  542. const char *model,
  543. const char *name,
  544. const char *host_str,
  545. const char *localaddr_str)
  546. {
  547. NetSocketState *s;
  548. int fd;
  549. struct sockaddr_in saddr;
  550. struct in_addr localaddr, *param_localaddr;
  551. if (parse_host_port(&saddr, host_str) < 0)
  552. return -1;
  553. if (localaddr_str != NULL) {
  554. if (inet_aton(localaddr_str, &localaddr) == 0)
  555. return -1;
  556. param_localaddr = &localaddr;
  557. } else {
  558. param_localaddr = NULL;
  559. }
  560. fd = net_socket_mcast_create(&saddr, param_localaddr);
  561. if (fd < 0)
  562. return -1;
  563. s = net_socket_fd_init(peer, model, name, fd, 0);
  564. if (!s)
  565. return -1;
  566. s->dgram_dst = saddr;
  567. snprintf(s->nc.info_str, sizeof(s->nc.info_str),
  568. "socket: mcast=%s:%d",
  569. inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
  570. return 0;
  571. }
  572. static int net_socket_udp_init(NetClientState *peer,
  573. const char *model,
  574. const char *name,
  575. const char *rhost,
  576. const char *lhost)
  577. {
  578. NetSocketState *s;
  579. int fd, ret;
  580. struct sockaddr_in laddr, raddr;
  581. if (parse_host_port(&laddr, lhost) < 0) {
  582. return -1;
  583. }
  584. if (parse_host_port(&raddr, rhost) < 0) {
  585. return -1;
  586. }
  587. fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
  588. if (fd < 0) {
  589. perror("socket(PF_INET, SOCK_DGRAM)");
  590. return -1;
  591. }
  592. ret = socket_set_fast_reuse(fd);
  593. if (ret < 0) {
  594. closesocket(fd);
  595. return -1;
  596. }
  597. ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
  598. if (ret < 0) {
  599. perror("bind");
  600. closesocket(fd);
  601. return -1;
  602. }
  603. qemu_set_nonblock(fd);
  604. s = net_socket_fd_init(peer, model, name, fd, 0);
  605. if (!s) {
  606. return -1;
  607. }
  608. s->dgram_dst = raddr;
  609. snprintf(s->nc.info_str, sizeof(s->nc.info_str),
  610. "socket: udp=%s:%d",
  611. inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
  612. return 0;
  613. }
  614. int net_init_socket(const NetClientOptions *opts, const char *name,
  615. NetClientState *peer, Error **errp)
  616. {
  617. /* FIXME error_setg(errp, ...) on failure */
  618. Error *err = NULL;
  619. const NetdevSocketOptions *sock;
  620. assert(opts->type == NET_CLIENT_OPTIONS_KIND_SOCKET);
  621. sock = opts->u.socket;
  622. if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
  623. sock->has_udp != 1) {
  624. error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
  625. " is required");
  626. return -1;
  627. }
  628. if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
  629. error_report("localaddr= is only valid with mcast= or udp=");
  630. return -1;
  631. }
  632. if (sock->has_fd) {
  633. int fd;
  634. fd = monitor_fd_param(cur_mon, sock->fd, &err);
  635. if (fd == -1) {
  636. error_report_err(err);
  637. return -1;
  638. }
  639. qemu_set_nonblock(fd);
  640. if (!net_socket_fd_init(peer, "socket", name, fd, 1)) {
  641. return -1;
  642. }
  643. return 0;
  644. }
  645. if (sock->has_listen) {
  646. if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
  647. return -1;
  648. }
  649. return 0;
  650. }
  651. if (sock->has_connect) {
  652. if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
  653. -1) {
  654. return -1;
  655. }
  656. return 0;
  657. }
  658. if (sock->has_mcast) {
  659. /* if sock->localaddr is missing, it has been initialized to "all bits
  660. * zero" */
  661. if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
  662. sock->localaddr) == -1) {
  663. return -1;
  664. }
  665. return 0;
  666. }
  667. assert(sock->has_udp);
  668. if (!sock->has_localaddr) {
  669. error_report("localaddr= is mandatory with udp=");
  670. return -1;
  671. }
  672. if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) ==
  673. -1) {
  674. return -1;
  675. }
  676. return 0;
  677. }