3
0

d6_socket.c 864 B

12345678910111213141516171819202122232425262728293031323334
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2011 Denys Vlasenko.
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. #include "common.h"
  8. #include "d6_common.h"
  9. #include <net/if.h>
  10. int FAST_FUNC d6_listen_socket(int port, const char *inf)
  11. {
  12. int fd;
  13. struct sockaddr_in6 addr;
  14. log1("opening listen socket on *:%d %s", port, inf);
  15. fd = xsocket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  16. setsockopt_reuseaddr(fd);
  17. if (setsockopt_broadcast(fd) == -1)
  18. bb_perror_msg_and_die("SO_BROADCAST");
  19. /* NB: bug 1032 says this doesn't work on ethernet aliases (ethN:M) */
  20. if (setsockopt_bindtodevice(fd, inf))
  21. xfunc_die(); /* warning is already printed */
  22. memset(&addr, 0, sizeof(addr));
  23. addr.sin6_family = AF_INET6;
  24. addr.sin6_port = htons(port);
  25. /* addr.sin6_addr is all-zeros */
  26. xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
  27. return fd;
  28. }