rename LIBT2N_CPPFLAGS to LIBT2N_CFLAGS since this is the variable defined by PKG_CHE...
[libt2n] / src / socket_client.cpp
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
41 using namespace std;
42
43 namespace libt2n
44 {
45
46 socket_client_connection::socket_client_connection(int _port, const std::string& _server, 
47             long long _connect_timeout_usec, int _max_retries)
48     : client_connection(), socket_handler(0,tcp_s)
49 {
50     max_retries=_max_retries;
51     connect_timeout_usec=_connect_timeout_usec;
52
53     server=_server;
54     port=_port;
55
56     tcp_connect(max_retries);
57 }
58
59 socket_client_connection::socket_client_connection(const std::string& _path,
60             long long _connect_timeout_usec, int _max_retries)
61     : client_connection(), socket_handler(0,unix_s)
62 {
63     max_retries=_max_retries;
64     connect_timeout_usec=_connect_timeout_usec;
65
66     path=_path;
67
68     unix_connect(max_retries);
69 }
70
71 void socket_client_connection::tcp_connect(int max_retries)
72 {
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)
80     {
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);
85
86         memcpy(&sock_addr.sin_addr,server_hent->h_addr_list[0],sizeof(sock_addr.sin_addr));
87     }
88
89     sock = socket(PF_INET, SOCK_STREAM, 0);
90     if (!sock)
91         throw t2n_connect_error(string("socket() error: ")+strerror(errno));
92
93     try
94     {
95         connect_with_timeout((struct sockaddr *) &sock_addr,sizeof(sock_addr));
96     }
97     catch (t2n_connect_error &e)
98     {
99         // recurse if retries left
100         if (max_retries > 0)
101             tcp_connect(max_retries-1);
102     }
103
104     do_callbacks(new_connection);
105 }
106
107 void socket_client_connection::unix_connect(int max_retries)
108 {
109     struct sockaddr_un unix_addr;
110
111     unix_addr.sun_family = AF_UNIX;
112     strcpy (unix_addr.sun_path, path.c_str());
113
114     sock = socket(PF_UNIX, SOCK_STREAM, 0);
115     if (!sock)
116         throw t2n_connect_error(string("socket() error: ")+strerror(errno));
117
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);
127     }
128
129     do_callbacks(new_connection);
130 }
131
132 void socket_client_connection::connect_with_timeout(struct sockaddr *sock_addr,unsigned int sockaddr_size)
133 {
134     set_socket_options(sock);
135
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     }
177 }
178
179 void socket_client_connection::close()
180 {
181     if (!client_connection::is_closed())
182     {
183         socket_handler::close();
184         client_connection::close();
185     }
186 }
187
188 }