libt2n: (gerd) improve docu, cleanup for public release
[libt2n] / src / socket_client.cpp
CommitLineData
7087e187
GE
1/***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
3 * gve@intra2net.com *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20#include <stdio.h>
21#include <errno.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <sys/un.h>
27#include <sys/time.h>
28#include <arpa/inet.h>
29#include <netinet/in.h>
30#include <netdb.h>
31#include <fcntl.h>
32#include <time.h>
33#include <pwd.h>
34#include <grp.h>
35
36#include <sstream>
37
38#include "socket_client.hxx"
39#include "t2n_exception.hxx"
40
41using namespace std;
42
43namespace libt2n
44{
45
b604df5f
GE
46socket_client_connection::socket_client_connection(int _port, const std::string& _server,
47 long long _connect_timeout_usec, int _max_retries)
7087e187
GE
48 : client_connection(), socket_handler(0,tcp_s)
49{
50 max_retries=_max_retries;
b604df5f 51 connect_timeout_usec=_connect_timeout_usec;
7087e187
GE
52
53 server=_server;
54 port=_port;
55
b604df5f 56 tcp_connect(max_retries);
7087e187
GE
57}
58
b604df5f
GE
59socket_client_connection::socket_client_connection(const std::string& _path,
60 long long _connect_timeout_usec, int _max_retries)
7087e187
GE
61 : client_connection(), socket_handler(0,unix_s)
62{
63 max_retries=_max_retries;
b604df5f 64 connect_timeout_usec=_connect_timeout_usec;
7087e187
GE
65
66 path=_path;
67
b604df5f 68 unix_connect(max_retries);
7087e187
GE
69}
70
b604df5f 71void socket_client_connection::tcp_connect(int max_retries)
7087e187 72{
b604df5f
GE
73 struct sockaddr_in sock_addr;
74
75 sock_addr.sin_family = AF_INET;
76 sock_addr.sin_port = htons(port);
77
78 // find the target ip
79 if (inet_aton(server.c_str(),&sock_addr.sin_addr)==0)
7087e187 80 {
b604df5f
GE
81 struct hostent *server_hent;
82 server_hent=gethostbyname(server.c_str());
83 if (server_hent == NULL)
84 throw t2n_connect_error(string("can't find server ")+server);
7087e187 85
b604df5f
GE
86 memcpy(&sock_addr.sin_addr,server_hent->h_addr_list[0],sizeof(sock_addr.sin_addr));
87 }
7087e187 88
b604df5f
GE
89 sock = socket(PF_INET, SOCK_STREAM, 0);
90 if (!sock)
91 throw t2n_connect_error(string("socket() error: ")+strerror(errno));
7087e187 92
b604df5f
GE
93 try
94 {
95 connect_with_timeout((struct sockaddr *) &sock_addr,sizeof(sock_addr));
7087e187 96 }
b604df5f 97 catch (t2n_connect_error &e)
7087e187 98 {
b604df5f
GE
99 // recurse if retries left
100 if (max_retries > 0)
101 tcp_connect(max_retries-1);
102 }
7087e187 103
b604df5f
GE
104 do_callbacks(new_connection);
105}
7087e187 106
b604df5f
GE
107void socket_client_connection::unix_connect(int max_retries)
108{
109 struct sockaddr_un unix_addr;
7087e187 110
b604df5f
GE
111 unix_addr.sun_family = AF_UNIX;
112 strcpy (unix_addr.sun_path, path.c_str());
7087e187 113
b604df5f
GE
114 sock = socket(PF_UNIX, SOCK_STREAM, 0);
115 if (!sock)
116 throw t2n_connect_error(string("socket() error: ")+strerror(errno));
7087e187 117
b604df5f
GE
118 try
119 {
120 connect_with_timeout((struct sockaddr *) &unix_addr, sizeof(unix_addr));
121 }
122 catch (t2n_connect_error &e)
123 {
124 // recurse if retries left
125 if (max_retries > 0)
126 tcp_connect(max_retries-1);
7087e187 127 }
7087e187 128
b604df5f
GE
129 do_callbacks(new_connection);
130}
131
132void socket_client_connection::connect_with_timeout(struct sockaddr *sock_addr,unsigned int sockaddr_size)
133{
7087e187 134 set_socket_options(sock);
91730468 135
b604df5f
GE
136 int ret=::connect(sock,sock_addr, sockaddr_size);
137
138 if (ret < 0)
139 {
140 if (errno==EINPROGRESS)
141 {
142 /* set timeout */
143 struct timeval tval;
144 struct timeval *timeout_ptr;
145
146 if (connect_timeout_usec == -1)
147 timeout_ptr = NULL;
148 else
149 {
150 timeout_ptr = &tval;
151
152 // convert timeout from long long usec to int sec + int usec
153 tval.tv_sec = connect_timeout_usec / 1000000;
154 tval.tv_usec = connect_timeout_usec % 1000000;
155 }
156
157 fd_set connect_socket_set;
158 FD_ZERO(&connect_socket_set);
159 FD_SET(sock,&connect_socket_set);
160
161 int ret;
162 while ((ret=select(FD_SETSIZE, NULL, &connect_socket_set, NULL, timeout_ptr)) &&
163 ret < 0 && errno==EINTR);
164
165 if (ret < 0)
166 throw t2n_connect_error(string("connect() error (select): ")+strerror(errno));
167
168 socklen_t sopt=sizeof(int);
169 int valopt;
170 ret=getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &sopt);
171 if (ret < 0 || valopt)
172 throw t2n_connect_error(string("connect() error (getsockopt): ")+strerror(errno));
173 }
174 else
175 throw t2n_connect_error(string("connect() error: ")+strerror(errno));
176 }
7087e187
GE
177}
178
179void socket_client_connection::close()
180{
181 if (!client_connection::is_closed())
182 {
183 socket_handler::close();
184 client_connection::close();
185 }
186}
187
188}