summaryrefslogtreecommitdiff
path: root/src/inet.cc
blob: 1700a144732bc33aaf662ed36f79c37b633689a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "common.hh"

#include "inet.hh"
#include "io.hh"
#include "logger.hh"

#include <algorithm>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

namespace inet {

unique_fd accept(Logger* logger, int fd, bool make_nonblock) {
#if HAVE_ACCEPT4
  unique_fd ret(accept4(fd, nullptr, nullptr,
                        make_nonblock ? SOCK_NONBLOCK : 0));
  if (!ret)
    logger->warn("accept: %s", strerror(errno));
  return ret;
#else
  unique_fd ret(::accept(fd, nullptr, nullptr));
  if (ret) {
    if (make_nonblock && !io::make_nonblocking(ret.get())) {
      logger->warn("make nonblock failed: %s", strerror(errno));
      ret.reset();
    }
  } else {
    logger->warn("accept: %s", strerror(errno));
  }
  return ret;
#endif
}

bool bind_and_listen(Logger* logger,
                     std::string const& addr,
                     std::string const& port,
                     std::vector<unique_fd>* out) {
  out->clear();
  struct addrinfo hints = {};
  struct addrinfo* ret;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_PASSIVE;
  auto err = getaddrinfo(addr.empty() ? nullptr : addr.c_str(),
                         port.c_str(), &hints, &ret);
  if (err) {
    logger->warn("%s:%s: getaddrinfo: %s",
                 addr.c_str(), port.c_str(), gai_strerror(err));
    return false;
  }

  for (auto* rp = ret; rp != nullptr; rp = rp->ai_next) {
    unique_fd fd(socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol));
    if (!fd)
      continue;

    if (bind(fd.get(), rp->ai_addr, rp->ai_addrlen)) {
      logger->warn("%s:%s: bind: %s",
                   addr.c_str(), port.c_str(), strerror(errno));
      continue;
    }

    if (listen(fd.get(), 4096)) {
      logger->warn("%s:%s: listen: %s",
                   addr.c_str(), port.c_str(), strerror(errno));
      continue;
    }

    out->emplace_back(std::move(fd));
  }
  freeaddrinfo(ret);
  return !out->empty();
}

}  // namespace inet

namespace unix {

unique_fd bind_and_listen(Logger* logger,
                          std::string_view path) {
  unique_fd fd(socket(AF_UNIX, SOCK_STREAM, 0));
  if (fd) {
    sockaddr_un addr;
    if (path.size() >= sizeof(addr.sun_path)) {
      logger->warn("%.*s: Too long path",
                   static_cast<int>(path.size()), path.data());
      return unique_fd();
    }
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    std::copy_n(path.data(), path.size(), addr.sun_path);
    addr.sun_path[path.size()] = '\0';
    if (bind(fd.get(), reinterpret_cast<struct sockaddr*>(&addr),
             sizeof(addr))) {
      logger->warn("%.*s: bind: %s",
                   static_cast<int>(path.size()), path.data(), strerror(errno));
      return unique_fd();
    }
    if (listen(fd.get(), 4096)) {
      logger->warn("%.*s: listen: %s",
                   static_cast<int>(path.size()), path.data(), strerror(errno));
      return unique_fd();
    }
  }
  return fd;
}

}  // namespace unix