19
19
* as published by the Free Software Foundation; either version
20
20
* 2 of the License, or (at your option) any later version.
22
* Copyright (C) 2001-2004 Alexandre Cassen, <acassen@linux-vs.org>
22
* Copyright (C) 2001-2005 Alexandre Cassen, <acassen@linux-vs.org>
25
25
#include "layer4.h"
77
77
enum connect_result
78
tcp_socket_state(int fd, thread * thread, uint32_t addr_ip, uint16_t addr_port,
78
tcp_socket_state(int fd, thread * thread_obj, uint32_t addr_ip, uint16_t addr_port,
79
79
int (*func) (struct _thread *))
86
86
/* Handle connection timeout */
87
if (thread->type == THREAD_WRITE_TIMEOUT) {
87
if (thread_obj->type == THREAD_WRITE_TIMEOUT) {
88
88
DBG("TCP connection timeout to [%s:%d].\n",
89
89
inet_ntop2(addr_ip), ntohs(addr_port));
90
close(thread_obj->u.fd);
91
91
return connect_timeout;
94
94
/* Check file descriptor */
95
95
slen = sizeof (status);
97
(thread->u.fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen) < 0)
97
(thread_obj->u.fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen) < 0)
100
100
/* Connection failed !!! */
102
102
DBG("TCP connection failed to [%s:%d].\n",
103
103
inet_ntop2(addr_ip), ntohs(addr_port));
104
close(thread_obj->u.fd);
105
105
return connect_error;
114
114
DBG("TCP connection to [%s:%d] still IN_PROGRESS.\n",
115
115
inet_ntop2(addr_ip), ntohs(addr_port));
117
timer_min = timer_sub_now(thread->sands);
118
thread_add_write(thread->master, func, THREAD_ARG(thread)
119
, thread->u.fd, TIMER_LONG(timer_min));
117
timer_min = timer_sub_now(thread_obj->sands);
118
thread_add_write(thread_obj->master, func, THREAD_ARG(thread_obj)
119
, thread_obj->u.fd, TIMER_LONG(timer_min));
120
120
return connect_in_progress;
127
tcp_connection_state(int fd, enum connect_result status, thread * thread,
127
tcp_connection_state(int fd, enum connect_result status, thread * thread_obj,
128
128
int (*func) (struct _thread *)
136
136
case connect_success:
137
thread_add_write(thread->master, func, THREAD_ARG(thread),
137
thread_add_write(thread_obj->master, func, THREAD_ARG(thread_obj),
141
141
/* Checking non-blocking connect, we wait until socket is writable */
142
142
case connect_in_progress:
143
thread_add_write(thread->master, func, THREAD_ARG(thread),
143
thread_add_write(thread_obj->master, func, THREAD_ARG(thread_obj),
153
tcp_check_thread(thread * thread)
153
tcp_check_thread(thread * thread_obj)
155
SOCK *sock = THREAD_ARG(thread);
155
SOCK *sock_obj = THREAD_ARG(thread_obj);
159
tcp_socket_state(thread->u.fd, thread, req->addr_ip, req->addr_port,
159
tcp_socket_state(thread_obj->u.fd, thread_obj, req->addr_ip, req->addr_port,
160
160
tcp_check_thread);
161
switch (sock->status) {
161
switch (sock_obj->status) {
162
162
case connect_error:
163
163
DBG("Error connecting server [%s:%d].\n",
164
164
inet_ntop2(req->addr_ip), ntohs(req->addr_port));
165
thread_add_terminate_event(thread->master);
165
thread_add_terminate_event(thread_obj->master);
169
169
case connect_timeout:
170
170
DBG("Timeout connecting server [%s:%d].\n",
171
171
inet_ntop2(req->addr_ip), ntohs(req->addr_port));
172
thread_add_terminate_event(thread->master);
172
thread_add_terminate_event(thread_obj->master);
176
176
case connect_success:{
178
ret = ssl_connect(thread);
178
ret = ssl_connect(thread_obj);
181
181
/* Remote WEB server is connected.
182
182
* Unlock eventual locked socket.
185
thread_add_event(thread->master,
186
http_request_thread, sock, 0);
185
thread_add_event(thread_obj->master,
186
http_request_thread, sock_obj, 0);
188
188
DBG("Connection trouble to: [%s:%d].\n",
189
189
inet_ntop2(req->addr_ip),
190
190
ntohs(req->addr_port));
192
192
ssl_printerr(SSL_get_error
194
sock->status = connect_error;
193
(sock_obj->ssl, ret));
194
sock_obj->status = connect_error;
205
tcp_connect_thread(thread * thread)
205
tcp_connect_thread(thread * thread_obj)
207
SOCK *sock = THREAD_ARG(thread);
207
SOCK *sock_obj = THREAD_ARG(thread_obj);
209
if ((sock->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
209
if ((sock_obj->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
210
210
DBG("WEB connection fail to create socket.\n");
214
sock->status = tcp_connect(sock->fd, req->addr_ip, req->addr_port);
214
sock->status = tcp_connect(sock_obj->fd, req->addr_ip, req->addr_port);
216
216
/* handle tcp connection status & register check worker thread */
217
tcp_connection_state(sock->fd, sock->status, thread, tcp_check_thread,
217
tcp_connection_state(sock_obj->fd, sock_obj->status, thread_obj, tcp_check_thread,
218
218
HTTP_CNX_TIMEOUT);