| 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 | #include "log.hxx" |
| 41 | |
| 42 | using namespace std; |
| 43 | |
| 44 | namespace libt2n |
| 45 | { |
| 46 | |
| 47 | socket_client_connection::socket_client_connection(int _port, const std::string& _server, |
| 48 | long long _connect_timeout_usec, int _max_retries, |
| 49 | std::ostream *_logstream, log_level_values _log_level) |
| 50 | : client_connection(), socket_handler(0,tcp_s) |
| 51 | { |
| 52 | max_retries=_max_retries; |
| 53 | connect_timeout_usec=_connect_timeout_usec; |
| 54 | |
| 55 | server=_server; |
| 56 | port=_port; |
| 57 | |
| 58 | set_logging(_logstream,_log_level); |
| 59 | |
| 60 | tcp_connect(max_retries); |
| 61 | |
| 62 | do_callbacks(new_connection); |
| 63 | } |
| 64 | |
| 65 | socket_client_connection::socket_client_connection(const std::string& _path, |
| 66 | long long _connect_timeout_usec, int _max_retries, |
| 67 | std::ostream *_logstream, log_level_values _log_level) |
| 68 | : client_connection(), socket_handler(0,unix_s) |
| 69 | { |
| 70 | max_retries=_max_retries; |
| 71 | connect_timeout_usec=_connect_timeout_usec; |
| 72 | |
| 73 | path=_path; |
| 74 | |
| 75 | set_logging(_logstream,_log_level); |
| 76 | |
| 77 | unix_connect(max_retries); |
| 78 | |
| 79 | do_callbacks(new_connection); |
| 80 | } |
| 81 | |
| 82 | void socket_client_connection::tcp_connect(int max_retries) |
| 83 | { |
| 84 | struct sockaddr_in sock_addr; |
| 85 | |
| 86 | sock_addr.sin_family = AF_INET; |
| 87 | sock_addr.sin_port = htons(port); |
| 88 | |
| 89 | // find the target ip |
| 90 | if (inet_aton(server.c_str(),&sock_addr.sin_addr)==0) |
| 91 | { |
| 92 | struct hostent *server_hent; |
| 93 | server_hent=gethostbyname(server.c_str()); |
| 94 | if (server_hent == NULL) |
| 95 | throw t2n_connect_error(string("can't find server ")+server); |
| 96 | |
| 97 | memcpy(&sock_addr.sin_addr,server_hent->h_addr_list[0],sizeof(sock_addr.sin_addr)); |
| 98 | } |
| 99 | |
| 100 | sock = socket(PF_INET, SOCK_STREAM, 0); |
| 101 | if (!sock) |
| 102 | throw t2n_connect_error(string("socket() error: ")+strerror(errno)); |
| 103 | |
| 104 | try |
| 105 | { |
| 106 | connect_with_timeout((struct sockaddr *) &sock_addr,sizeof(sock_addr)); |
| 107 | } |
| 108 | catch (t2n_connect_error &e) |
| 109 | { |
| 110 | // recurse if retries left |
| 111 | if (max_retries > 0) |
| 112 | { |
| 113 | LOGSTREAM(debug,"retrying connect after connect error"); |
| 114 | tcp_connect(max_retries-1); |
| 115 | } |
| 116 | else |
| 117 | LOGSTREAM(debug,"no more retries left after connect error"); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void socket_client_connection::unix_connect(int max_retries) |
| 122 | { |
| 123 | struct sockaddr_un unix_addr; |
| 124 | |
| 125 | unix_addr.sun_family = AF_UNIX; |
| 126 | strcpy (unix_addr.sun_path, path.c_str()); |
| 127 | |
| 128 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 129 | if (!sock) |
| 130 | throw t2n_connect_error(string("socket() error: ")+strerror(errno)); |
| 131 | |
| 132 | try |
| 133 | { |
| 134 | connect_with_timeout((struct sockaddr *) &unix_addr, sizeof(unix_addr)); |
| 135 | } |
| 136 | catch (t2n_connect_error &e) |
| 137 | { |
| 138 | // recurse if retries left |
| 139 | if (max_retries > 0) |
| 140 | { |
| 141 | LOGSTREAM(debug,"retrying connect after connect error"); |
| 142 | unix_connect(max_retries-1); |
| 143 | } |
| 144 | else |
| 145 | LOGSTREAM(debug,"no more retries left after connect error"); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void socket_client_connection::connect_with_timeout(struct sockaddr *sock_addr,unsigned int sockaddr_size) |
| 150 | { |
| 151 | set_socket_options(sock); |
| 152 | |
| 153 | LOGSTREAM(debug,"connect_with_timeout()"); |
| 154 | int ret=::connect(sock,sock_addr, sockaddr_size); |
| 155 | |
| 156 | if (ret < 0) |
| 157 | { |
| 158 | if (errno==EINPROGRESS) |
| 159 | { |
| 160 | LOGSTREAM(debug,"connect_with_timeout(): EINPROGRESS"); |
| 161 | |
| 162 | /* set timeout */ |
| 163 | struct timeval tval; |
| 164 | struct timeval *timeout_ptr; |
| 165 | |
| 166 | if (connect_timeout_usec == -1) |
| 167 | timeout_ptr = NULL; |
| 168 | else |
| 169 | { |
| 170 | timeout_ptr = &tval; |
| 171 | |
| 172 | // convert timeout from long long usec to int sec + int usec |
| 173 | tval.tv_sec = connect_timeout_usec / 1000000; |
| 174 | tval.tv_usec = connect_timeout_usec % 1000000; |
| 175 | } |
| 176 | |
| 177 | fd_set connect_socket_set; |
| 178 | FD_ZERO(&connect_socket_set); |
| 179 | FD_SET(sock,&connect_socket_set); |
| 180 | |
| 181 | int ret; |
| 182 | while ((ret=select(FD_SETSIZE, NULL, &connect_socket_set, NULL, timeout_ptr)) && |
| 183 | ret < 0 && errno==EINTR); |
| 184 | |
| 185 | if (ret < 0) |
| 186 | { |
| 187 | LOGSTREAM(debug,"connect_with_timeout(): select error: " << strerror(errno)); |
| 188 | throw t2n_connect_error(string("connect() error (select): ")+strerror(errno)); |
| 189 | } |
| 190 | |
| 191 | socklen_t sopt=sizeof(int); |
| 192 | int valopt; |
| 193 | ret=getsockopt(sock, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &sopt); |
| 194 | if (ret < 0 || valopt) |
| 195 | { |
| 196 | LOGSTREAM(debug,"connect_with_timeout(): getsockopt error: " << strerror(errno)); |
| 197 | throw t2n_connect_error(string("connect() error (getsockopt): ")+strerror(errno)); |
| 198 | } |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | LOGSTREAM(debug,"connect_with_timeout(): error: " << strerror(errno)); |
| 203 | throw t2n_connect_error(string("connect() error: ")+strerror(errno)); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | LOGSTREAM(debug,"connect_with_timeout(): success"); |
| 208 | } |
| 209 | |
| 210 | void socket_client_connection::close() |
| 211 | { |
| 212 | if (!client_connection::is_closed()) |
| 213 | { |
| 214 | socket_handler::close(); |
| 215 | client_connection::close(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** @brief try to reconnect the current connection with the same connection credentials (host and port or path) |
| 220 | */ |
| 221 | void socket_client_connection::reconnect() |
| 222 | { |
| 223 | LOGSTREAM(debug,"reconnect()"); |
| 224 | |
| 225 | // close the current connection if still open |
| 226 | close(); |
| 227 | |
| 228 | socket_type_value type=get_type(); |
| 229 | |
| 230 | if (type == tcp_s) |
| 231 | tcp_connect(max_retries); |
| 232 | else if (type == unix_s) |
| 233 | unix_connect(max_retries); |
| 234 | |
| 235 | LOGSTREAM(debug,"reconnect(): basic connection established"); |
| 236 | |
| 237 | reopen(); |
| 238 | |
| 239 | LOGSTREAM(debug,"reconnect() done, client_connection::is_closed() now " << client_connection::is_closed()); |
| 240 | |
| 241 | } |
| 242 | |
| 243 | } |