5 #include <netinet/in.h>
6 #include <netinet/in.h>
7 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <sys/socket.h>
13 #include <sys/types.h>
17 #include <linux/if_link.h>
45 struct hostent* hostent_sp;
49 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)")))
53 else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)")))
55 host = std::string(
"127.0.0.1");
57 else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}")))
59 host = mm[1].str().c_str();
63 host = std::string(
"127.0.0.1");
65 TLOG(TLVL_INFO) <<
"Resolving host " << host;
67 memset(&addr, 0,
sizeof(addr));
69 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}")))
70 inet_aton(host.c_str(), &addr);
73 hostent_sp = gethostbyname(host.c_str());
76 perror(
"gethostbyname");
79 addr = *(
struct in_addr*)(hostent_sp->h_addr_list[0]);
93 struct hostent* hostent_sp;
98 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)")))
102 else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)")))
104 host = std::string(
"127.0.0.1");
106 else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}")))
108 host = mm[1].str().c_str();
112 host = std::string(
"127.0.0.1");
114 TLOG(TLVL_INFO) <<
"Resolving ip " << host;
116 memset(&addr, 0,
sizeof(addr));
118 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}")))
120 in_addr desired_host;
121 inet_aton(host.c_str(), &desired_host);
122 struct ifaddrs *ifaddr, *ifa;
124 if (getifaddrs(&ifaddr) == -1)
126 perror(
"getifaddrs");
133 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
135 if (ifa->ifa_addr == NULL)
continue;
139 if (ifa->ifa_addr->sa_family == AF_INET)
141 auto if_addr = (
struct sockaddr_in*)ifa->ifa_addr;
142 auto sa = (
struct sockaddr_in*)ifa->ifa_netmask;
144 TLOG(15) <<
"IF: " << ifa->ifa_name <<
" Desired: " << desired_host.s_addr
145 <<
" netmask: " << sa->sin_addr.s_addr <<
" this interface: " << if_addr->sin_addr.s_addr;
147 if ((if_addr->sin_addr.s_addr & sa->sin_addr.s_addr) == (desired_host.s_addr & sa->sin_addr.s_addr))
149 TLOG(TLVL_INFO) <<
"Using interface " << ifa->ifa_name;
150 memcpy(&addr, &if_addr->sin_addr,
sizeof(addr));
157 TLOG(TLVL_WARNING) <<
"No matches for ip " << host <<
", using 0.0.0.0";
158 inet_aton(
"0.0.0.0", &addr);
166 hostent_sp = gethostbyname(host.c_str());
169 perror(
"gethostbyname");
172 addr = *(
struct in_addr*)(hostent_sp->h_addr_list[0]);
184 int ResolveHost(
char const* host_in,
int dflt_port, sockaddr_in& sin)
188 struct hostent* hostent_sp;
192 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)")))
195 port = strtoul(mm[2].str().c_str(), NULL, 0);
197 else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)")))
199 host = std::string(
"127.0.0.1");
200 port = strtoul(mm[1].str().c_str(), NULL, 0);
202 else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}")))
204 host = mm[1].str().c_str();
209 host = std::string(
"127.0.0.1");
212 TLOG(TLVL_INFO) <<
"Resolving host " << host <<
", on port " << port;
214 if (host ==
"localhost") host =
"127.0.0.1";
216 memset(&sin, 0,
sizeof(sin));
217 sin.sin_family = AF_INET;
218 sin.sin_port = htons(port);
220 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}")))
221 inet_aton(host.c_str(), &sin.sin_addr);
224 hostent_sp = gethostbyname(host.c_str());
227 perror(
"gethostbyname");
230 sin.sin_addr = *(
struct in_addr*)(hostent_sp->h_addr_list[0]);
243 int TCPConnect(
char const* host_in,
int dflt_port,
long flags = 0,
int sndbufsiz = 0)
246 struct sockaddr_in sin;
248 s_fd = socket(PF_INET, SOCK_STREAM , 0);
252 perror(
"socket error");
263 sts = connect(s_fd, (
struct sockaddr*)&sin,
sizeof(sin));
273 sts = fcntl(s_fd, F_SETFL, flags);
274 TLOG(TLVL_TRACE) <<
"TCPConnect fcntl(fd=" << s_fd <<
",flags=0x" << std::hex << flags << std::dec <<
") =" << sts;
280 socklen_t lenlen =
sizeof(len);
282 sts = getsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, &lenlen);
283 TLOG(TLVL_DEBUG) <<
"TCPConnect SNDBUF initial: " << len <<
" sts/errno=" << sts <<
"/" << errno
284 <<
" lenlen=" << lenlen;
286 sts = setsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, lenlen);
287 if (sts == -1) TLOG(TLVL_ERROR) <<
"Error with setsockopt SNDBUF " << errno;
289 sts = getsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, &lenlen);
290 if (len < (sndbufsiz * 2))
291 TLOG(TLVL_WARNING) <<
"SNDBUF " << len <<
" not expected (" << sndbufsiz <<
" sts/errno=" << sts <<
"/" << errno
294 TLOG(TLVL_DEBUG) <<
"SNDBUF " << len <<
" sts/errno=" << sts <<
"/" << errno;
299 #endif // TCPConnect_hh
int ResolveHost(char const *host_in, in_addr &addr)
Convert a string hostname to a in_addr suitable for socket communication.
int TCPConnect(char const *host_in, int dflt_port, long flags=0, int sndbufsiz=0)
Connect to a host on a given port.
int GetInterfaceForNetwork(char const *host_in, in_addr &addr)
Convert an IP address to the network address of the interface sharing the subnet mask.