8 #define TRACE_NAME "TCPConnect"
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
25 #include "artdaq/DAQdata/Globals.hh"
32 struct hostent* hostent_sp;
36 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)")))
40 else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)")))
42 host = std::string(
"127.0.0.1");
44 else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}")))
46 host = mm[1].str().c_str();
50 host = std::string(
"127.0.0.1");
52 TLOG(TLVL_INFO) <<
"Resolving host " << host;
54 bzero((
char *)&addr,
sizeof(addr));
56 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}")))
57 inet_aton(host.c_str(), &addr);
60 hostent_sp = gethostbyname(host.c_str());
63 perror(
"gethostbyname");
66 addr = *(
struct in_addr *)(hostent_sp->h_addr_list[0]);
72 int ResolveHost(
char const* host_in,
int dflt_port, sockaddr_in& sin)
76 struct hostent* hostent_sp;
80 if (regex_match(host_in, mm, std::regex(
"([^:]+):(\\d+)")))
83 port = strtoul(mm[2].str().c_str(), NULL, 0);
85 else if (regex_match(host_in, mm, std::regex(
":{0,1}(\\d+)")))
87 host = std::string(
"127.0.0.1");
88 port = strtoul(mm[1].str().c_str(), NULL, 0);
90 else if (regex_match(host_in, mm, std::regex(
"([^:]+):{0,1}")))
92 host = mm[1].str().c_str();
97 host = std::string(
"127.0.0.1");
100 TLOG(TLVL_INFO) <<
"Resolving host " << host <<
", on port " << std::to_string(port);
102 if (host ==
"localhost") host =
"127.0.0.1";
104 bzero((
char *)&sin,
sizeof(sin));
105 sin.sin_family = AF_INET;
106 sin.sin_port = htons(port);
108 if (regex_match(host.c_str(), mm, std::regex(
"\\d+(\\.\\d+){3}")))
109 inet_aton(host.c_str(), &sin.sin_addr);
112 hostent_sp = gethostbyname(host.c_str());
115 perror(
"gethostbyname");
118 sin.sin_addr = *(
struct in_addr *)(hostent_sp->h_addr_list[0]);
130 struct sockaddr_in sin;
133 s_fd = socket(PF_INET, SOCK_STREAM, 0);
137 perror(
"socket error");
148 sts = connect(s_fd, (
struct sockaddr *)&sin,
sizeof(sin));
158 sts = fcntl(s_fd, F_SETFL, flags);
159 TLOG(TLVL_TRACE) <<
"TCPConnect fcntl(fd=" << s_fd <<
",flags=0x" << std::hex << flags << std::dec <<
") =" << sts;
165 socklen_t lenlen =
sizeof(len);
167 sts = getsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, &lenlen);
168 TLOG(TLVL_DEBUG) <<
"TCPConnect SNDBUF initial: "<< len <<
" sts/errno="<< sts <<
"/"<< errno <<
" lenlen="<< lenlen ;
170 sts = setsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, lenlen);
172 TLOG(TLVL_ERROR) <<
"Error with setsockopt SNDBUF "<< errno;
174 sts = getsockopt(s_fd, SOL_SOCKET, SO_SNDBUF, &len, &lenlen);
175 if (len < (sndbufsiz * 2))
176 TLOG(TLVL_WARNING) <<
"SNDBUF "<< len <<
" not expected ("<< sndbufsiz <<
" sts/errno="<< sts <<
"/"<< errno ;
178 TLOG(TLVL_DEBUG) <<
"SNDBUF "<< len <<
" sts/errno="<< sts <<
"/"<< errno ;
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.