1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/Exception.hpp" 10 #include <netinet/in.h> 11 #include <arpa/inet.h> 14 #include <sys/types.h> 15 #include <sys/socket.h> 18 #include <sys/ioctl.h> 23 namespace hmbdc {
namespace comm {
namespace inet {
32 std::string getLocalIpMatchMask(std::string
const& mask);
33 std::string getLocalIpThruName(std::string
const& name);
34 std::pair<std::string, uint16_t> getPeerIpPort(
int fd);
35 void extractRelicTo(msghdr& to, msghdr
const& from,
int sent);
39 getLocalIpMatchMask(std::string
const& mask) {
41 bool includeLoopback = mask !=
"0.0.0.0/0";
42 auto offset = mask.find(
'/');
44 uint32_t maskLen = 32;
46 if (offset != string::npos) {
47 ip_part = mask.substr(0, offset);
48 auto bit_part = mask.substr(offset + 1u);
49 maskLen = atoi(bit_part.c_str());
51 uint32_t targetIp = inet_addr(ip_part.c_str());
52 struct ifaddrs *ifaddr, *ifa;
54 char host[NI_MAXHOST];
56 if (getifaddrs(&ifaddr) == -1) {
57 HMBDC_THROW(runtime_error,
" getifaddrs failed ");
60 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
61 if (ifa->ifa_addr == NULL)
64 family = ifa->ifa_addr->sa_family;
66 if (family == AF_INET) {
67 s = getnameinfo(ifa->ifa_addr,
68 sizeof(
struct sockaddr_in),
69 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
71 HMBDC_THROW(runtime_error,
" getnameinfo failed ");
74 if (inet_aton(host, &addr)
76 || (targetIp << (32u - maskLen)) == (addr.s_addr << (32u - maskLen)))
77 && (includeLoopback || !(ifa->ifa_flags & IFF_LOOPBACK))) {
84 HMBDC_THROW(runtime_error, mask <<
" does not resolve locally");
89 getLocalIpThruName(std::string
const& name) {
91 struct ifaddrs *ifaddr, *ifa;
93 char host[NI_MAXHOST];
95 if (getifaddrs(&ifaddr) == -1) {
96 HMBDC_THROW(runtime_error,
" getifaddrs failed ");
99 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
100 if (ifa->ifa_addr == NULL)
103 family = ifa->ifa_addr->sa_family;
105 if (family == AF_INET
106 && strcmp(name.c_str(), ifa->ifa_name) == 0) {
107 s = getnameinfo(ifa->ifa_addr,
108 sizeof(
struct sockaddr_in),
109 host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
111 HMBDC_THROW(runtime_error,
" getnameinfo failed ");
116 HMBDC_THROW(runtime_error, name <<
" does not resolve locally");
120 std::pair<std::string, uint16_t>
121 getPeerIpPort(
int fd) {
123 auto addrlen =
sizeof(addr);
124 if (getpeername(fd, &addr, (socklen_t*)&addrlen) == -1) {
125 HMBDC_THROW(std::runtime_error,
"invalid socket errno=" << errno);
127 sockaddr_in* p = (sockaddr_in *)&addr;
128 char ipaddr[INET_ADDRSTRLEN];
129 if (inet_ntop(AF_INET, &(p->sin_addr), ipaddr, INET_ADDRSTRLEN)) {
131 HMBDC_THROW(std::runtime_error,
"no ip addr errno=" << errno);
133 return make_pair(std::string(ipaddr), p->sin_port);
138 extractRelicTo(msghdr& to, msghdr
const& from,
int sent) {
139 auto l = size_t(sent);
141 for (; l > 0 && i < (int)from.msg_iovlen; ++i) {
142 auto& msg = from.msg_iov[i];
143 if (msg.iov_len <= l) {
150 auto& msg = from.msg_iov[i];
151 to.msg_iov[0].iov_base = (
char*)msg.iov_base + l;
152 to.msg_iov[0].iov_len = msg.iov_len - l;
153 auto to_msg_iovlen = from.msg_iovlen - i;
155 memmove(to.msg_iov + 1
156 , from.msg_iov + i + 1
157 , (to_msg_iovlen - 1) *
sizeof(iovec)
159 to.msg_iovlen = to_msg_iovlen;
Definition: TypedString.hpp:84