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>
44 struct hostent* hostent_sp;
48 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)"))) {
50 }
else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)"))) {
51 host = std::string(
"127.0.0.1");
52 }
else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}"))) {
53 host = mm[1].str().c_str();
55 host = std::string(
"127.0.0.1");
57 TLOG(TLVL_INFO) <<
"Resolving host " << host;
59 memset(&addr, 0,
sizeof(addr));
61 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}")))
62 inet_aton(host.c_str(), &addr);
64 hostent_sp = gethostbyname(host.c_str());
66 perror(
"gethostbyname");
69 addr = *(
struct in_addr*)(hostent_sp->h_addr_list[0]);
82 struct hostent* hostent_sp;
87 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)"))) {
89 }
else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)"))) {
90 host = std::string(
"127.0.0.1");
91 }
else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}"))) {
92 host = mm[1].str().c_str();
94 host = std::string(
"127.0.0.1");
96 TLOG(TLVL_INFO) <<
"Resolving ip " << host;
98 memset(&addr, 0,
sizeof(addr));
100 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}"))) {
101 in_addr desired_host;
102 inet_aton(host.c_str(), &desired_host);
103 struct ifaddrs *ifaddr, *ifa;
105 if (getifaddrs(&ifaddr) == -1) {
106 perror(
"getifaddrs");
113 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
114 if (ifa->ifa_addr == NULL)
continue;
118 if (ifa->ifa_addr->sa_family == AF_INET) {
119 auto if_addr = (
struct sockaddr_in*)ifa->ifa_addr;
120 auto sa = (
struct sockaddr_in*)ifa->ifa_netmask;
122 TLOG(15) <<
"IF: " << ifa->ifa_name <<
" Desired: " << desired_host.s_addr
123 <<
" netmask: " << sa->sin_addr.s_addr <<
" this interface: " << if_addr->sin_addr.s_addr;
125 if ((if_addr->sin_addr.s_addr & sa->sin_addr.s_addr) == (desired_host.s_addr & sa->sin_addr.s_addr)) {
126 TLOG(TLVL_INFO) <<
"Using interface " << ifa->ifa_name;
127 memcpy(&addr, &if_addr->sin_addr,
sizeof(addr));
133 TLOG(TLVL_WARNING) <<
"No matches for ip " << host <<
", using 0.0.0.0";
134 inet_aton(
"0.0.0.0", &addr);
140 hostent_sp = gethostbyname(host.c_str());
142 perror(
"gethostbyname");
145 addr = *(
struct in_addr*)(hostent_sp->h_addr_list[0]);
157 int ResolveHost(
char const* host_in,
int dflt_port, sockaddr_in& sin) {
160 struct hostent* hostent_sp;
164 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)"))) {
166 port = strtoul(mm[2].str().c_str(), NULL, 0);
167 }
else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)"))) {
168 host = std::string(
"127.0.0.1");
169 port = strtoul(mm[1].str().c_str(), NULL, 0);
170 }
else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}"))) {
171 host = mm[1].str().c_str();
174 host = std::string(
"127.0.0.1");
177 TLOG(TLVL_INFO) <<
"Resolving host " << host <<
", on port " << port;
179 if (host ==
"localhost") host =
"127.0.0.1";
181 memset(&sin, 0,
sizeof(sin));
182 sin.sin_family = AF_INET;
183 sin.sin_port = htons(port);
185 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}")))
186 inet_aton(host.c_str(), &sin.sin_addr);
188 hostent_sp = gethostbyname(host.c_str());
190 perror(
"gethostbyname");
193 sin.sin_addr = *(
struct in_addr*)(hostent_sp->h_addr_list[0]);
206 int TCPConnect(
char const* host_in,
int dflt_port,
long flags = 0,
int sndbufsiz = 0) {
208 struct sockaddr_in sin;
210 s_fd = socket(PF_INET, SOCK_STREAM , 0);
213 perror(
"socket error");
223 sts = connect(s_fd, (
struct sockaddr*)&sin,
sizeof(sin));
231 sts = fcntl(s_fd, F_SETFL, flags);
232 TLOG(TLVL_TRACE) <<
"TCPConnect fcntl(fd=" << s_fd <<
",flags=0x" << std::hex << flags << std::dec <<
") =" << sts;
237 socklen_t lenlen =
sizeof(len);
239 sts = getsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, &lenlen);
240 TLOG(TLVL_DEBUG) <<
"TCPConnect SNDBUF initial: " << len <<
" sts/errno=" << sts <<
"/" << errno
241 <<
" lenlen=" << lenlen;
243 sts = setsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, lenlen);
244 if (sts == -1) TLOG(TLVL_ERROR) <<
"Error with setsockopt SNDBUF " << errno;
246 sts = getsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, &lenlen);
247 if (len < (sndbufsiz * 2))
248 TLOG(TLVL_WARNING) <<
"SNDBUF " << len <<
" not expected (" << sndbufsiz <<
" sts/errno=" << sts <<
"/" << errno
251 TLOG(TLVL_DEBUG) <<
"SNDBUF " << len <<
" sts/errno=" << sts <<
"/" << errno;
256 #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.