2
0

socket.c 21 KB

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